xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision d80ece959af10c1aa984cf3c9a69f33acf9f641b)
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;
191485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
1926cf0e42fSMatthew G. Knepley   PetscErrorCode  ierr;
1936cf0e42fSMatthew G. Knepley 
1946cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
195485ad865SMatthew G. Knepley   ierr = DMPlexGetInteriorCellStratum(dm, &cStart, &cEnd);CHKERRQ(ierr);
1966cf0e42fSMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1976cf0e42fSMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1986cf0e42fSMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1996cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2006cf0e42fSMatthew G. Knepley     IS              pointIS;
2016cf0e42fSMatthew G. Knepley     const PetscInt *points;
2026cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2036cf0e42fSMatthew G. Knepley 
2046cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
2056cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2066cf0e42fSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2076cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2086cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
20922eabd52SMatthew G. Knepley       PetscInt  closureSize, point, cl;
2106cf0e42fSMatthew G. Knepley 
2116cf0e42fSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
21222eabd52SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
21322eabd52SMatthew G. Knepley         point = closure[cl*2];
21422eabd52SMatthew G. Knepley         if ((point >= cStart) && (point < cEnd)) {ierr = DMLabelSetValue(label, point, values[v]);CHKERRQ(ierr); break;}
21522eabd52SMatthew G. Knepley       }
2166cf0e42fSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
2176cf0e42fSMatthew G. Knepley     }
2186cf0e42fSMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2196cf0e42fSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2206cf0e42fSMatthew G. Knepley   }
2216cf0e42fSMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
2226cf0e42fSMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
2236cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2246cf0e42fSMatthew G. Knepley }
2256cf0e42fSMatthew G. Knepley 
226f402d5e4SToby Isaac /*@
227f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
228f402d5e4SToby Isaac 
229f402d5e4SToby Isaac   Input Parameters:
230f402d5e4SToby Isaac + dm - The DM
231f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
232f402d5e4SToby Isaac 
233f402d5e4SToby Isaac   Output Parameter:
234f402d5e4SToby Isaac . label - A DMLabel without cells
235f402d5e4SToby Isaac 
236f402d5e4SToby Isaac   Level: developer
237f402d5e4SToby Isaac 
238f402d5e4SToby Isaac   Note: This undoes DMPlexLabelAddCells()
239f402d5e4SToby Isaac 
240f402d5e4SToby Isaac .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
241f402d5e4SToby Isaac @*/
242f402d5e4SToby Isaac PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
243f402d5e4SToby Isaac {
244f402d5e4SToby Isaac   IS              valueIS;
245f402d5e4SToby Isaac   const PetscInt *values;
246485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
247f402d5e4SToby Isaac   PetscErrorCode  ierr;
248f402d5e4SToby Isaac 
249f402d5e4SToby Isaac   PetscFunctionBegin;
250485ad865SMatthew G. Knepley   ierr = DMPlexGetInteriorCellStratum(dm, &cStart, &cEnd);CHKERRQ(ierr);
251f402d5e4SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
252f402d5e4SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
253f402d5e4SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
254f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
255f402d5e4SToby Isaac     IS              pointIS;
256f402d5e4SToby Isaac     const PetscInt *points;
257f402d5e4SToby Isaac     PetscInt        numPoints, p;
258f402d5e4SToby Isaac 
259f402d5e4SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
260f402d5e4SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
261f402d5e4SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
262f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
263f402d5e4SToby Isaac       PetscInt point = points[p];
264f402d5e4SToby Isaac 
265f402d5e4SToby Isaac       if (point >= cStart && point < cEnd) {
266f402d5e4SToby Isaac         ierr = DMLabelClearValue(label,point,values[v]);CHKERRQ(ierr);
267f402d5e4SToby Isaac       }
268f402d5e4SToby Isaac     }
269f402d5e4SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
270f402d5e4SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
271f402d5e4SToby Isaac   }
272f402d5e4SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
273f402d5e4SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
274f402d5e4SToby Isaac   PetscFunctionReturn(0);
275f402d5e4SToby Isaac }
276f402d5e4SToby Isaac 
27759eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
27859eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
2792582d50cSToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
280cd0c2139SMatthew G Knepley {
2812582d50cSToby Isaac   PetscInt d, off = 0;
2822582d50cSToby Isaac 
2832582d50cSToby Isaac   PetscFunctionBegin;
28459eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
2850974a383SToby Isaac   for (d = 0; d < depth; d++) {
2862582d50cSToby Isaac     PetscInt firstd = d;
2870974a383SToby Isaac     PetscInt firstStart = depthShift[2*d];
2882582d50cSToby Isaac     PetscInt e;
2892582d50cSToby Isaac 
2902582d50cSToby Isaac     for (e = d+1; e <= depth; e++) {
2912582d50cSToby Isaac       if (depthShift[2*e] < firstStart) {
2922582d50cSToby Isaac         firstd = e;
2932582d50cSToby Isaac         firstStart = depthShift[2*d];
2942582d50cSToby Isaac       }
2952582d50cSToby Isaac     }
2962582d50cSToby Isaac     if (firstd != d) {
2972582d50cSToby Isaac       PetscInt swap[2];
2982582d50cSToby Isaac 
2992582d50cSToby Isaac       e = firstd;
3002582d50cSToby Isaac       swap[0] = depthShift[2*d];
3012582d50cSToby Isaac       swap[1] = depthShift[2*d+1];
3022582d50cSToby Isaac       depthShift[2*d]   = depthShift[2*e];
3032582d50cSToby Isaac       depthShift[2*d+1] = depthShift[2*e+1];
3042582d50cSToby Isaac       depthShift[2*e]   = swap[0];
3052582d50cSToby Isaac       depthShift[2*e+1] = swap[1];
3062582d50cSToby Isaac     }
3072582d50cSToby Isaac   }
3082582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
3090974a383SToby Isaac   for (d = 0; d <= depth; d++) {
3102582d50cSToby Isaac     off += depthShift[2*d+1];
31159eef20bSToby Isaac     depthShift[2*d+1] = depthShift[2*d] + off;
3122582d50cSToby Isaac   }
3132582d50cSToby Isaac   PetscFunctionReturn(0);
3142582d50cSToby Isaac }
3152582d50cSToby Isaac 
3162582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3172582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3182582d50cSToby Isaac {
3192582d50cSToby Isaac   PetscInt d;
3202582d50cSToby Isaac   PetscInt newOff = 0;
3212582d50cSToby Isaac 
3222582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3232582d50cSToby Isaac     if (p < depthShift[2*d]) return p + newOff;
3242582d50cSToby Isaac     else newOff = depthShift[2*d+1] - depthShift[2*d];
3252582d50cSToby Isaac   }
3260974a383SToby Isaac   return p + newOff;
3272582d50cSToby Isaac }
3282582d50cSToby Isaac 
3292582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3302582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3312582d50cSToby Isaac {
3322582d50cSToby Isaac   PetscInt d;
3332582d50cSToby Isaac   PetscInt newOff = 0;
3342582d50cSToby Isaac 
3352582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3362582d50cSToby Isaac     if (p < depthShift[2*d+1]) return p + newOff;
3372582d50cSToby Isaac     else newOff = depthShift[2*d] - depthShift[2*d+1];
3382582d50cSToby Isaac   }
3390974a383SToby Isaac   return p + newOff;
340cd0c2139SMatthew G Knepley }
341cd0c2139SMatthew G Knepley 
342cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
343cd0c2139SMatthew G Knepley {
344cd0c2139SMatthew G Knepley   PetscInt       depth = 0, d, pStart, pEnd, p;
345fa8e8ae5SToby Isaac   DMLabel        depthLabel;
346cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
347cd0c2139SMatthew G Knepley 
348cd0c2139SMatthew G Knepley   PetscFunctionBegin;
349cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
350cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
351cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
352cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3530974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
354cd0c2139SMatthew G Knepley   ierr = DMPlexSetChart(dmNew, pStart, pEnd);CHKERRQ(ierr);
355fa8e8ae5SToby Isaac   ierr = DMCreateLabel(dmNew,"depth");CHKERRQ(ierr);
356fa8e8ae5SToby Isaac   ierr = DMPlexGetDepthLabel(dmNew,&depthLabel);CHKERRQ(ierr);
357cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
358cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
359fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
360fa8e8ae5SToby Isaac     IS pIS;
361fa8e8ae5SToby Isaac 
362cd0c2139SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
363fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
364fa8e8ae5SToby Isaac     pEndNew = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
365fa8e8ae5SToby Isaac     ierr = ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS);CHKERRQ(ierr);
366fa8e8ae5SToby Isaac     ierr = DMLabelSetStratumIS(depthLabel, d, pIS);CHKERRQ(ierr);
367fa8e8ae5SToby Isaac     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
368cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
3692582d50cSToby Isaac       PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
370cd0c2139SMatthew G Knepley       PetscInt size;
371cd0c2139SMatthew G Knepley 
372cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
373cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(dmNew, newp, size);CHKERRQ(ierr);
374cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
375cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(dmNew, newp, size);CHKERRQ(ierr);
376cd0c2139SMatthew G Knepley     }
377cd0c2139SMatthew G Knepley   }
378cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
379cd0c2139SMatthew G Knepley }
380cd0c2139SMatthew G Knepley 
381cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
382cd0c2139SMatthew G Knepley {
3832582d50cSToby Isaac   PetscInt      *newpoints;
3842582d50cSToby Isaac   PetscInt       depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
385cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
386cd0c2139SMatthew G Knepley 
387cd0c2139SMatthew G Knepley   PetscFunctionBegin;
388cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
389cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
390cd0c2139SMatthew G Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
391dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
3922582d50cSToby Isaac   ierr = PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints);CHKERRQ(ierr);
393cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
394cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
395cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
396cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
3972582d50cSToby Isaac     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
398cd0c2139SMatthew G Knepley 
399cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
400cd0c2139SMatthew G Knepley     ierr = DMPlexGetCone(dm, p, &points);CHKERRQ(ierr);
401cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeOrientation(dm, p, &orientations);CHKERRQ(ierr);
402cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4032582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
404cd0c2139SMatthew G Knepley     }
405cd0c2139SMatthew G Knepley     ierr = DMPlexSetCone(dmNew, newp, newpoints);CHKERRQ(ierr);
406cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dmNew, newp, orientations);CHKERRQ(ierr);
407cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
408dcbb62e8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmNew, newp, &sizeNew);CHKERRQ(ierr);
409cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, p, &points);CHKERRQ(ierr);
410cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4112582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
412cd0c2139SMatthew G Knepley     }
413dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
414cd0c2139SMatthew G Knepley     ierr = DMPlexSetSupport(dmNew, newp, newpoints);CHKERRQ(ierr);
415cd0c2139SMatthew G Knepley   }
4162582d50cSToby Isaac   ierr = PetscFree(newpoints);CHKERRQ(ierr);
417cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
418cd0c2139SMatthew G Knepley }
419cd0c2139SMatthew G Knepley 
420cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
421cd0c2139SMatthew G Knepley {
422cd0c2139SMatthew G Knepley   PetscSection   coordSection, newCoordSection;
423cd0c2139SMatthew G Knepley   Vec            coordinates, newCoordinates;
424cd0c2139SMatthew G Knepley   PetscScalar   *coords, *newCoords;
425f2b8cce1SMatthew G. Knepley   PetscInt       coordSize, sStart, sEnd;
426f2b8cce1SMatthew G. Knepley   PetscInt       dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
427f2b8cce1SMatthew G. Knepley   PetscBool      hasCells;
428cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
429cd0c2139SMatthew G Knepley 
430cd0c2139SMatthew G Knepley   PetscFunctionBegin;
431f2b8cce1SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
432c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(dmNew, dim);CHKERRQ(ierr);
433cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
434cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
435cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
436f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
437cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);CHKERRQ(ierr);
438f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew);CHKERRQ(ierr);
43969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
440cd0c2139SMatthew G Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);CHKERRQ(ierr);
441cd0c2139SMatthew G Knepley   ierr = PetscSectionSetNumFields(newCoordSection, 1);CHKERRQ(ierr);
442cd0c2139SMatthew G Knepley   ierr = PetscSectionSetFieldComponents(newCoordSection, 0, dim);CHKERRQ(ierr);
443f2b8cce1SMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
444f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
445f2b8cce1SMatthew G. Knepley   ierr = PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew);CHKERRQ(ierr);
446f2b8cce1SMatthew G. Knepley   if (hasCells) {
447f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
448f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
449f2b8cce1SMatthew G. Knepley 
450f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
451f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetDof(newCoordSection, cNew, dof);CHKERRQ(ierr);
452f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof);CHKERRQ(ierr);
453f2b8cce1SMatthew G. Knepley     }
454f2b8cce1SMatthew G. Knepley   }
455cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
456cd0c2139SMatthew G Knepley     ierr = PetscSectionSetDof(newCoordSection, v, dim);CHKERRQ(ierr);
457cd0c2139SMatthew G Knepley     ierr = PetscSectionSetFieldDof(newCoordSection, v, 0, dim);CHKERRQ(ierr);
458cd0c2139SMatthew G Knepley   }
459cd0c2139SMatthew G Knepley   ierr = PetscSectionSetUp(newCoordSection);CHKERRQ(ierr);
46046e270d4SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection);CHKERRQ(ierr);
461cd0c2139SMatthew G Knepley   ierr = PetscSectionGetStorageSize(newCoordSection, &coordSize);CHKERRQ(ierr);
4628b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
463cd0c2139SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) newCoordinates, "coordinates");CHKERRQ(ierr);
464cd0c2139SMatthew G Knepley   ierr = VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
4654e90ef8eSMatthew G. Knepley   ierr = VecSetBlockSize(newCoordinates, dim);CHKERRQ(ierr);
4662eb5907fSJed Brown   ierr = VecSetType(newCoordinates,VECSTANDARD);CHKERRQ(ierr);
467cd0c2139SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dmNew, newCoordinates);CHKERRQ(ierr);
468cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
469cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
470cd0c2139SMatthew G Knepley   ierr = VecGetArray(newCoordinates, &newCoords);CHKERRQ(ierr);
471f2b8cce1SMatthew G. Knepley   if (hasCells) {
472f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
473f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
474f2b8cce1SMatthew G. Knepley 
475f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
476f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, c, &off);CHKERRQ(ierr);
477f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(newCoordSection, cNew, &noff);CHKERRQ(ierr);
478f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
479f2b8cce1SMatthew G. Knepley     }
480f2b8cce1SMatthew G. Knepley   }
481cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
482cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
483cd0c2139SMatthew G Knepley 
484cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
485cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
4862582d50cSToby Isaac     ierr = PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff);CHKERRQ(ierr);
487f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
488cd0c2139SMatthew G Knepley   }
489cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
490cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(newCoordinates, &newCoords);CHKERRQ(ierr);
491cd0c2139SMatthew G Knepley   ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
492cd0c2139SMatthew G Knepley   ierr = PetscSectionDestroy(&newCoordSection);CHKERRQ(ierr);
493cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
494cd0c2139SMatthew G Knepley }
495cd0c2139SMatthew G Knepley 
496cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
497cd0c2139SMatthew G Knepley {
4982582d50cSToby Isaac   PetscInt           depth = 0;
499cd0c2139SMatthew G Knepley   PetscSF            sfPoint, sfPointNew;
500cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
501cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
502cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
503cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
504cd0c2139SMatthew G Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, totShift = 0;
505cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
506cd0c2139SMatthew G Knepley 
507cd0c2139SMatthew G Knepley   PetscFunctionBegin;
508cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
509cd0c2139SMatthew G Knepley   /* Step 9: Convert pointSF */
510cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
511cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dmNew, &sfPointNew);CHKERRQ(ierr);
512cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
513cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
5146e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd,depth,depthShift) - pEnd;
515cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
516dcca6d9dSJed Brown     ierr = PetscMalloc2(numRoots,&newLocation,pEnd-pStart,&newRemoteLocation);CHKERRQ(ierr);
5172582d50cSToby Isaac     for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
518cd0c2139SMatthew G Knepley     ierr = PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
519cd0c2139SMatthew G Knepley     ierr = PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
520785e854fSJed Brown     ierr = PetscMalloc1(numLeaves,    &glocalPoints);CHKERRQ(ierr);
521785e854fSJed Brown     ierr = PetscMalloc1(numLeaves, &gremotePoints);CHKERRQ(ierr);
522cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
5232582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
524cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
525cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
526cd0c2139SMatthew G Knepley     }
527cd0c2139SMatthew G Knepley     ierr = PetscFree2(newLocation,newRemoteLocation);CHKERRQ(ierr);
528cd0c2139SMatthew G Knepley     ierr = PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
529cd0c2139SMatthew G Knepley   }
530cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
531cd0c2139SMatthew G Knepley }
532cd0c2139SMatthew G Knepley 
533cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
534cd0c2139SMatthew G Knepley {
535cd0c2139SMatthew G Knepley   PetscSF            sfPoint;
536cd0c2139SMatthew G Knepley   DMLabel            vtkLabel, ghostLabel;
537cd0c2139SMatthew G Knepley   const PetscSFNode *leafRemote;
538cd0c2139SMatthew G Knepley   const PetscInt    *leafLocal;
5392582d50cSToby Isaac   PetscInt           depth = 0, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
540cd0c2139SMatthew G Knepley   PetscMPIInt        rank;
541cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
542cd0c2139SMatthew G Knepley 
543cd0c2139SMatthew G Knepley   PetscFunctionBegin;
544cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
545cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
546c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
547cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
548cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
549cd0c2139SMatthew G Knepley     const char     *lname;
550fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
551cd0c2139SMatthew G Knepley     IS              valueIS;
552cd0c2139SMatthew G Knepley     const PetscInt *values;
553cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
554cd0c2139SMatthew G Knepley 
555c58f1c22SToby Isaac     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
556cd0c2139SMatthew G Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
557cd0c2139SMatthew G Knepley     if (isDepth) continue;
558fa8e8ae5SToby Isaac     ierr = PetscStrcmp(lname, "dim", &isDim);CHKERRQ(ierr);
559fa8e8ae5SToby Isaac     if (isDim) continue;
560c58f1c22SToby Isaac     ierr = DMCreateLabel(dmNew, lname);CHKERRQ(ierr);
561c58f1c22SToby Isaac     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
562c58f1c22SToby Isaac     ierr = DMGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr);
56364a6a1a4SToby Isaac     ierr = DMLabelGetDefaultValue(label,&val);CHKERRQ(ierr);
56464a6a1a4SToby Isaac     ierr = DMLabelSetDefaultValue(newlabel,val);CHKERRQ(ierr);
565cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
566cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
567cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
568cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
569cd0c2139SMatthew G Knepley       IS              pointIS;
570cd0c2139SMatthew G Knepley       const PetscInt *points;
571cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
572cd0c2139SMatthew G Knepley 
573cd0c2139SMatthew G Knepley       ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr);
574cd0c2139SMatthew G Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
575cd0c2139SMatthew G Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
576cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
5772582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
578cd0c2139SMatthew G Knepley 
579cd0c2139SMatthew G Knepley         ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr);
580cd0c2139SMatthew G Knepley       }
581cd0c2139SMatthew G Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
582cd0c2139SMatthew G Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
583cd0c2139SMatthew G Knepley     }
584cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
585cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
586cd0c2139SMatthew G Knepley   }
587cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
588cd0c2139SMatthew G Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
589cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
590cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
591cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr);
592c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "vtk");CHKERRQ(ierr);
593c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "ghost");CHKERRQ(ierr);
594c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr);
595c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr);
596cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
597cd0c2139SMatthew G Knepley     for (; c < leafLocal[l] && c < cEnd; ++c) {
598cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
599cd0c2139SMatthew G Knepley     }
600cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
601cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
602cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
603cd0c2139SMatthew G Knepley     } else {
604cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr);
605cd0c2139SMatthew G Knepley     }
606cd0c2139SMatthew G Knepley   }
607cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
608cd0c2139SMatthew G Knepley     ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
609cd0c2139SMatthew G Knepley   }
610cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr);
611cd0c2139SMatthew G Knepley   for (f = fStart; f < fEnd; ++f) {
612cd0c2139SMatthew G Knepley     PetscInt numCells;
613cd0c2139SMatthew G Knepley 
614cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr);
615cd0c2139SMatthew G Knepley     if (numCells < 2) {
616cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);
617cd0c2139SMatthew G Knepley     } else {
618cd0c2139SMatthew G Knepley       const PetscInt *cells = NULL;
619cd0c2139SMatthew G Knepley       PetscInt        vA, vB;
620cd0c2139SMatthew G Knepley 
621cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr);
622cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr);
623cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr);
62490664b96SToby Isaac       if (vA != 1 && vB != 1) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);}
625cd0c2139SMatthew G Knepley     }
626cd0c2139SMatthew G Knepley   }
627cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
628cd0c2139SMatthew G Knepley }
629cd0c2139SMatthew G Knepley 
630ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
631ca04dac2SToby Isaac {
632ca04dac2SToby Isaac   DM             refTree;
633ca04dac2SToby Isaac   PetscSection   pSec;
634ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
635ca04dac2SToby Isaac   PetscErrorCode ierr;
636ca04dac2SToby Isaac 
637ca04dac2SToby Isaac   PetscFunctionBegin;
638ca04dac2SToby Isaac   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
639ca04dac2SToby Isaac   ierr = DMPlexSetReferenceTree(dmNew,refTree);CHKERRQ(ierr);
640ca04dac2SToby Isaac   ierr = DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL);CHKERRQ(ierr);
641ca04dac2SToby Isaac   if (pSec) {
6422582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
643fb4630b5SToby Isaac     PetscInt *childIDsShifted;
644ca04dac2SToby Isaac     PetscSection pSecShifted;
645ca04dac2SToby Isaac 
646ca04dac2SToby Isaac     ierr = PetscSectionGetChart(pSec,&pStart,&pEnd);CHKERRQ(ierr);
647ca04dac2SToby Isaac     ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
6482582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
6492582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
650fb4630b5SToby Isaac     ierr = PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted);CHKERRQ(ierr);
651ca04dac2SToby Isaac     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted);CHKERRQ(ierr);
652ca04dac2SToby Isaac     ierr = PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted);CHKERRQ(ierr);
653ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
654fb4630b5SToby Isaac       /* start off assuming no children */
655fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,p,0);CHKERRQ(ierr);
656fb4630b5SToby Isaac     }
657fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
658fb4630b5SToby Isaac       PetscInt dof;
659fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
660ca04dac2SToby Isaac 
661ca04dac2SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
662fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,pNew,dof);CHKERRQ(ierr);
663ca04dac2SToby Isaac     }
664ca04dac2SToby Isaac     ierr = PetscSectionSetUp(pSecShifted);CHKERRQ(ierr);
665fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
666fb4630b5SToby Isaac       PetscInt dof;
667fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
668fb4630b5SToby Isaac 
669fb4630b5SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
670fb4630b5SToby Isaac       if (dof) {
671fb4630b5SToby Isaac         PetscInt off, offNew;
672fb4630b5SToby Isaac 
673fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSec,p,&off);CHKERRQ(ierr);
674fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSecShifted,pNew,&offNew);CHKERRQ(ierr);
675fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
676fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
677fb4630b5SToby Isaac       }
678fb4630b5SToby Isaac     }
679fb4630b5SToby Isaac     ierr = DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted);CHKERRQ(ierr);
680fb4630b5SToby Isaac     ierr = PetscFree2(parentsShifted,childIDsShifted);CHKERRQ(ierr);
681e6885bbbSToby Isaac     ierr = PetscSectionDestroy(&pSecShifted);CHKERRQ(ierr);
682ca04dac2SToby Isaac   }
683ca04dac2SToby Isaac   PetscFunctionReturn(0);
684ca04dac2SToby Isaac }
685ca04dac2SToby Isaac 
686cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
687cd0c2139SMatthew G Knepley {
688da97024aSMatthew G. Knepley   PetscSF               sf;
689cd0c2139SMatthew G Knepley   IS                    valueIS;
690da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
691cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
6922582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
69390b157c4SStefano Zampini   PetscBool             isper;
69490b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
69590b157c4SStefano Zampini   const DMBoundaryType *bd;
696cd0c2139SMatthew G Knepley   PetscErrorCode        ierr;
697cd0c2139SMatthew G Knepley 
698cd0c2139SMatthew G Knepley   PetscFunctionBegin;
699da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
700da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
701da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
70246c796b9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
703cd0c2139SMatthew G Knepley   /* Count ghost cells */
704cd0c2139SMatthew G Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
705cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr);
706cd0c2139SMatthew G Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
7074a6cfa73SMatthew G. Knepley   Ng   = 0;
708cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
70946c796b9SMatthew G. Knepley     IS              faceIS;
71046c796b9SMatthew G. Knepley     const PetscInt *faces;
71146c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
712cd0c2139SMatthew G Knepley 
71346c796b9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
71446c796b9SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
71546c796b9SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
71646c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
717ca04dac2SToby Isaac       PetscInt numChildren;
718ca04dac2SToby Isaac 
719da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
720ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
721ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
722ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
72346c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
72446c796b9SMatthew G. Knepley     }
7254a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
72646c796b9SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
727cd0c2139SMatthew G Knepley   }
728cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7292582d50cSToby Isaac   ierr = PetscMalloc1(2*(depth+1), &depthShift);CHKERRQ(ierr);
7302582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
73159eef20bSToby Isaac     PetscInt dEnd;
7322582d50cSToby Isaac 
7330974a383SToby Isaac     ierr = DMPlexGetDepthStratum(dm,d,NULL,&dEnd);CHKERRQ(ierr);
73459eef20bSToby Isaac     depthShift[2*d]   = dEnd;
7352582d50cSToby Isaac     depthShift[2*d+1] = 0;
7362582d50cSToby Isaac   }
7372582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
7382582d50cSToby Isaac   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
739cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
740cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
741cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
742485ad865SMatthew G. Knepley   ierr = DMPlexSetGhostCellStratum(gdm, cEnd, PETSC_DETERMINE);CHKERRQ(ierr);
7434a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
744cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr);
745cd0c2139SMatthew G Knepley   }
746cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
747cd0c2139SMatthew G Knepley     IS              faceIS;
748cd0c2139SMatthew G Knepley     const PetscInt *faces;
749cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
750cd0c2139SMatthew G Knepley 
751cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
752cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
753cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
754cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
755ca04dac2SToby Isaac       PetscInt size, numChildren;
756cd0c2139SMatthew G Knepley 
757da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
758ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
759ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
76046c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
761cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr);
7624553fd90SToby Isaac       if (size != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
7634a6cfa73SMatthew G. Knepley       ierr = DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);CHKERRQ(ierr);
764cd0c2139SMatthew G Knepley     }
765cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
766cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
767cd0c2139SMatthew G Knepley   }
768cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
769cd0c2139SMatthew G Knepley   ierr = DMSetUp(gdm);CHKERRQ(ierr);
770cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
771cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
772cd0c2139SMatthew G Knepley   ghostCell = cEnd;
773cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
774cd0c2139SMatthew G Knepley     IS              faceIS;
775cd0c2139SMatthew G Knepley     const PetscInt *faces;
776cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
777cd0c2139SMatthew G Knepley 
778cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
779cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
780cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
78146c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
782ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
783cd0c2139SMatthew G Knepley 
784da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
785ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
786ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
78746c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
788cd0c2139SMatthew G Knepley       ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr);
789cd0c2139SMatthew G Knepley       ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr);
79046c796b9SMatthew G. Knepley       ++ghostCell;
791cd0c2139SMatthew G Knepley     }
792cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
793cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
794cd0c2139SMatthew G Knepley   }
795cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
796cd0c2139SMatthew G Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
797cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
798cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
799cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
800ca04dac2SToby Isaac   ierr = DMPlexShiftTree_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
801cd0c2139SMatthew G Knepley   ierr = PetscFree(depthShift);CHKERRQ(ierr);
802966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
80390b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
80490b157c4SStefano Zampini   ierr = DMSetPeriodicity(gdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
8054a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
806cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
807cd0c2139SMatthew G Knepley }
808cd0c2139SMatthew G Knepley 
809cd0c2139SMatthew G Knepley /*@C
810cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
811cd0c2139SMatthew G Knepley 
812cd0c2139SMatthew G Knepley   Collective on dm
813cd0c2139SMatthew G Knepley 
814cd0c2139SMatthew G Knepley   Input Parameters:
815cd0c2139SMatthew G Knepley + dm - The original DM
816cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
817cd0c2139SMatthew G Knepley 
818cd0c2139SMatthew G Knepley   Output Parameters:
819cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
820cd0c2139SMatthew G Knepley - dmGhosted - The new DM
821cd0c2139SMatthew G Knepley 
822cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
823cd0c2139SMatthew G Knepley 
824cd0c2139SMatthew G Knepley   Level: developer
825cd0c2139SMatthew G Knepley 
826cd0c2139SMatthew G Knepley .seealso: DMCreate()
82731266bc0SMatthew G. Knepley @*/
828cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
829cd0c2139SMatthew G Knepley {
830cd0c2139SMatthew G Knepley   DM             gdm;
831cd0c2139SMatthew G Knepley   DMLabel        label;
832cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
833*d80ece95SMatthew G. Knepley   PetscInt       dim, Ng = 0, cMax, fMax, eMax, vMax;
834b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
835cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
836cd0c2139SMatthew G Knepley 
837cd0c2139SMatthew G Knepley   PetscFunctionBegin;
838cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8394a6cfa73SMatthew G. Knepley   if (numGhostCells) PetscValidPointer(numGhostCells, 3);
840cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
841cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr);
842cd0c2139SMatthew G Knepley   ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr);
843c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
844c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(gdm, dim);CHKERRQ(ierr);
845b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
846b0441da4SMatthew G. Knepley   ierr = DMSetBasicAdjacency(gdm, useCone,  useClosure);CHKERRQ(ierr);
847c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
848cd0c2139SMatthew G Knepley   if (!label) {
849cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
850c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
851c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
852e752be1aSMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
853cd0c2139SMatthew G Knepley   }
854*d80ece95SMatthew G. Knepley   ierr = DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm);CHKERRQ(ierr);
855a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, gdm);CHKERRQ(ierr);
856b0441da4SMatthew G. Knepley   ierr = DMCopyDisc(dm, gdm);CHKERRQ(ierr);
857*d80ece95SMatthew G. Knepley   /* Copy the hybrid bounds */
858*d80ece95SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
859*d80ece95SMatthew G. Knepley   if (fMax >= 0) fMax += Ng;
860*d80ece95SMatthew G. Knepley   if (eMax >= 0) eMax += Ng;
861*d80ece95SMatthew G. Knepley   if (vMax >= 0) vMax += Ng;
862*d80ece95SMatthew G. Knepley   ierr = DMPlexSetHybridBounds(gdm, cMax, fMax, eMax, vMax);CHKERRQ(ierr);
863cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
864*d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
865cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
866cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
867cd0c2139SMatthew G Knepley }
868cd0c2139SMatthew G Knepley 
869607ab7a9SMatthew G. Knepley /*
870faedd622SMatthew G. Knepley   We are adding three kinds of points here:
871607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
872faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
873607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
874a6ae58d1SMatthew G. Knepley 
875a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
876a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
877607ab7a9SMatthew G. Knepley */
8787db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
879cd0c2139SMatthew G Knepley {
880cd0c2139SMatthew G Knepley   MPI_Comm         comm;
881607ab7a9SMatthew G. Knepley   IS               valueIS;
882607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;       /* The number of depths for which we have replicated points */
883607ab7a9SMatthew G. Knepley   const PetscInt  *values;          /* List of depths for which we have replicated points */
88418c5995bSMatthew G. Knepley   IS              *splitIS;
88518c5995bSMatthew G. Knepley   IS              *unsplitIS;
886607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
88718c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
88836dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
88936dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
890607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
89118c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
892cd0c2139SMatthew G Knepley   PetscSection     coordSection;
893cd0c2139SMatthew G Knepley   Vec              coordinates;
894cd0c2139SMatthew G Knepley   PetscScalar     *coords;
895a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
896a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
897607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
898607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
899607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
90018c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
901cd0c2139SMatthew G Knepley   PetscErrorCode   ierr;
902cd0c2139SMatthew G Knepley 
903cd0c2139SMatthew G Knepley   PetscFunctionBegin;
904cd0c2139SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
905c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
906607ab7a9SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
907fd4b9f15SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
908cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
909607ab7a9SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
9102582d50cSToby Isaac   ierr = PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);CHKERRQ(ierr);
911dcca6d9dSJed 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);
912a6ae58d1SMatthew 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);
913607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
914607ab7a9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr);
915a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
916a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
917607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
91818c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
919607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
920a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
921607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
92218c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
92318c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
92418c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
92559eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
92659eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
92759eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
92859eef20bSToby Isaac     depthShift[2*d+1]     = 0;
929607ab7a9SMatthew G. Knepley   }
930cd0c2139SMatthew G Knepley   if (label) {
931cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
932cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr);
933cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
934cd0c2139SMatthew G Knepley   }
935cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
936cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
937cd0c2139SMatthew G Knepley 
938cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
93918c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, dep, &splitIS[dep]);CHKERRQ(ierr);
94018c5995bSMatthew G. Knepley     if (splitIS[dep]) {
94118c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr);
94218c5995bSMatthew G. Knepley       ierr = ISGetIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);
94318c5995bSMatthew G. Knepley     }
94418c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);CHKERRQ(ierr);
94518c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
94618c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);CHKERRQ(ierr);
94718c5995bSMatthew G. Knepley       ierr = ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);
948cd0c2139SMatthew G Knepley     }
949cd0c2139SMatthew G Knepley   }
950607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
95118c5995bSMatthew 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   */
9522582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
9533d3eaba7SBarry Smith   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
95459eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
95559eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
95659eef20bSToby Isaac    * added points */
9572582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
958cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
959cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
960cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
961cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
962cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
9632582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
9644c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
965cd0c2139SMatthew G Knepley       const PetscInt *support;
9664c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
967cd0c2139SMatthew G Knepley 
968cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
969cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr);
970cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
971cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr);
972cd0c2139SMatthew G Knepley       if (dep == depth-1) {
9734c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
9744c367dbcSMatthew G. Knepley 
975cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
9764c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr);
977cd0c2139SMatthew G Knepley       } else if (dep == 0) {
9784c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
979cd0c2139SMatthew G Knepley 
980cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
9814c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
982cd0c2139SMatthew G Knepley           PetscInt val;
983cd0c2139SMatthew G Knepley 
984cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
9854c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
9864c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
9874c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
988cd0c2139SMatthew G Knepley         }
9894c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
9904c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
9914c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
9924c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
9934c367dbcSMatthew G. Knepley         /* Add hybrid edge */
9944c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
9954c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
996cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
9974c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
9984c367dbcSMatthew G. Knepley 
999cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
10004c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1001cd0c2139SMatthew G Knepley           PetscInt val;
1002cd0c2139SMatthew G Knepley 
1003cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
10044c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
10054c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
10064c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1007cd0c2139SMatthew G Knepley         }
10084c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
10094c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
10104c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
10114c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
10124c367dbcSMatthew G. Knepley         /* Add hybrid face */
10134c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
10144c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1015cd0c2139SMatthew G Knepley       }
1016cd0c2139SMatthew G Knepley     }
1017cd0c2139SMatthew G Knepley   }
101818c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
101918c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
102018c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
10212582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
102218c5995bSMatthew G. Knepley       const PetscInt *support;
1023da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
102418c5995bSMatthew G. Knepley 
102518c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
102618c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
102739254ff6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
102818c5995bSMatthew G. Knepley       if (dep == 0) {
102918c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
103018c5995bSMatthew G. Knepley 
103139254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
103239254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
103339254ff6SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
103439254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
103539254ff6SMatthew G. Knepley         }
103639254ff6SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
103718c5995bSMatthew G. Knepley         /* Add hybrid edge */
103818c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
1039e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1040e1757548SMatthew G. Knepley           PetscInt val;
1041e1757548SMatthew G. Knepley 
1042e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1043e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1044da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1045da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1046e1757548SMatthew G. Knepley         }
1047e1757548SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
104818c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
104918c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1050cd0c2139SMatthew G Knepley         PetscInt       val;
1051cd0c2139SMatthew G Knepley 
1052da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1053cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1054da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1055da1dd7e4SMatthew G. Knepley           else              ++qf;
1056cd0c2139SMatthew G Knepley         }
105718c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
1058da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
105918c5995bSMatthew G. Knepley         /* Add hybrid face */
1060da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1061da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1062da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1063da1dd7e4SMatthew G. Knepley         }
106418c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
1065da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1066cd0c2139SMatthew G Knepley       }
1067cd0c2139SMatthew G Knepley     }
1068cd0c2139SMatthew G Knepley   }
1069cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
1070cd0c2139SMatthew G Knepley   ierr = DMSetUp(sdm);CHKERRQ(ierr);
1071cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1072dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
1073dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);CHKERRQ(ierr);
1074cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1075cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1076cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1077cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10782582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
10794c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1080cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
10814c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1082cd0c2139SMatthew G Knepley 
1083cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1084cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
1085cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
1086cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1087cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
1088cd0c2139SMatthew G Knepley       if (dep == depth-1) {
108996a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
10904c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1091cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1092cd0c2139SMatthew G Knepley 
1093cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
1094cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(sdm, newp,  &supportF);CHKERRQ(ierr);
1095cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr);
1096cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1097cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1098cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
10994c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
110018c5995bSMatthew G. Knepley           if (v < 0) {
110118c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
110218c5995bSMatthew 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);
11032582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
110496a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
110518c5995bSMatthew G. Knepley           } else {
11064c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1107163235baSMatthew G. Knepley             if (dep > 1) {
1108163235baSMatthew G. Knepley               const PetscInt *econe;
1109163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1110163235baSMatthew G. Knepley 
1111163235baSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[q], &econeSize);CHKERRQ(ierr);
1112163235baSMatthew G. Knepley               ierr = DMPlexGetCone(dm, cone[q], &econe);CHKERRQ(ierr);
1113163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
1114163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);CHKERRQ(ierr);
1115163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);CHKERRQ(ierr);
1116163235baSMatthew G. Knepley                 if (vs >= 0) continue;
1117163235baSMatthew 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);
1118163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1119163235baSMatthew G. Knepley               }
1120163235baSMatthew G. Knepley             }
1121cd0c2139SMatthew G Knepley           }
1122cd0c2139SMatthew G Knepley         }
1123cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr);
1124cd0c2139SMatthew G Knepley         ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr);
1125e537020bSMatthew G. Knepley         /* Face support */
1126cd0c2139SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
1127cd0c2139SMatthew G Knepley           PetscInt val;
1128cd0c2139SMatthew G Knepley 
1129cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr);
1130cd0c2139SMatthew G Knepley           if (val < 0) {
1131cd0c2139SMatthew G Knepley             /* Split old face:   Replace negative side cell with cohesive cell */
11324c367dbcSMatthew G. Knepley              ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr);
1133cd0c2139SMatthew G Knepley           } else {
1134cd0c2139SMatthew G Knepley             /* Split new face:   Replace positive side cell with cohesive cell */
11354c367dbcSMatthew G. Knepley             ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr);
1136e537020bSMatthew G. Knepley             /* Get orientation for cohesive face */
1137e537020bSMatthew G. Knepley             {
1138e537020bSMatthew G. Knepley               const PetscInt *ncone, *nconeO;
1139e537020bSMatthew G. Knepley               PetscInt        nconeSize, nc;
1140e537020bSMatthew G. Knepley 
1141e537020bSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr);
1142e537020bSMatthew G. Knepley               ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr);
1143e537020bSMatthew G. Knepley               ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr);
1144e537020bSMatthew G. Knepley               for (nc = 0; nc < nconeSize; ++nc) {
1145e537020bSMatthew G. Knepley                 if (ncone[nc] == oldp) {
1146e537020bSMatthew G. Knepley                   coneONew[0] = nconeO[nc];
1147e537020bSMatthew G. Knepley                   break;
1148cd0c2139SMatthew G Knepley                 }
1149cd0c2139SMatthew G Knepley               }
1150e537020bSMatthew 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]);
1151e537020bSMatthew G. Knepley             }
1152e537020bSMatthew G. Knepley           }
1153e537020bSMatthew G. Knepley         }
11544c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1155fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
11564c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
11574c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1158e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
115918c5995bSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
116018c5995bSMatthew G. Knepley           if (v < 0) {
116118c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
116218c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
116318c5995bSMatthew G. Knepley             coneONew[2+q] = 0;
116418c5995bSMatthew G. Knepley           } else {
116518c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
116618c5995bSMatthew G. Knepley           }
1167e537020bSMatthew G. Knepley           coneONew[2+q] = 0;
1168e537020bSMatthew G. Knepley         }
11694c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr);
11704c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr);
117196a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
117296a07cd0SMatthew G. Knepley         if (hasUnsplit) {ierr = DMLabelSetValue(label, -hybcell, dim);CHKERRQ(ierr);}
1173cd0c2139SMatthew G Knepley       } else if (dep == 0) {
11744c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1175cd0c2139SMatthew G Knepley 
1176cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
11774c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1178cd0c2139SMatthew G Knepley           PetscInt val;
1179cd0c2139SMatthew G Knepley 
1180cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1181cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
11822582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1183cd0c2139SMatthew G Knepley           }
1184cd0c2139SMatthew G Knepley         }
11854c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
1186cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1187cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
11884c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1189cd0c2139SMatthew G Knepley           PetscInt val, edge;
1190cd0c2139SMatthew G Knepley 
1191cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1192cd0c2139SMatthew G Knepley           if (val == 1) {
11934c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
1194cd0c2139SMatthew G Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
11954c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1196cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
11972582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1198cd0c2139SMatthew G Knepley           }
1199cd0c2139SMatthew G Knepley         }
12004c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
1201cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
12024c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1203cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1204cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
12054c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
12064c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12074c367dbcSMatthew G. Knepley           PetscInt val, edge;
12084c367dbcSMatthew G. Knepley 
12094c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12104c367dbcSMatthew G. Knepley           if (val == 1) {
12114c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
12124c367dbcSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
12134c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
12144c367dbcSMatthew G. Knepley           }
12154c367dbcSMatthew G. Knepley         }
12164c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1217cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
12184c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
12194c367dbcSMatthew G. Knepley 
1220cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1221cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1222cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
12234c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
1224e1757548SMatthew G. Knepley           if (v < 0) {
1225e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1226e1757548SMatthew 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);
12272582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1228e1757548SMatthew G. Knepley           } else {
12294c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1230cd0c2139SMatthew G Knepley           }
1231e1757548SMatthew G. Knepley         }
1232cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr);
1233cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1234cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1235cd0c2139SMatthew G Knepley           PetscInt val;
1236cd0c2139SMatthew G Knepley 
1237cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12384c367dbcSMatthew G. Knepley           if (val == dim-1) {
12392582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
12404c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
12412582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1242cd0c2139SMatthew G Knepley           }
1243cd0c2139SMatthew G Knepley         }
1244b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1245cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1246cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1247cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1248cd0c2139SMatthew G Knepley           PetscInt val, face;
1249cd0c2139SMatthew G Knepley 
1250cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1251cd0c2139SMatthew G Knepley           if (val == dim-1) {
12524c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1253cd0c2139SMatthew G Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
12544c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1255cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
12562582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1257cd0c2139SMatthew G Knepley           }
1258cd0c2139SMatthew G Knepley         }
1259b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1260cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
12614c367dbcSMatthew G. Knepley         /* Hybrid face */
12624c367dbcSMatthew G. Knepley         coneNew[0] = newp;
12634c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
12644c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
12654c367dbcSMatthew G. Knepley           PetscInt vertex;
12664c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr);
1267e1757548SMatthew G. Knepley           if (vertex < 0) {
1268e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);CHKERRQ(ierr);
1269e1757548SMatthew 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);
1270e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1271e1757548SMatthew G. Knepley           } else {
12724c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
12734c367dbcSMatthew G. Knepley           }
1274e1757548SMatthew G. Knepley         }
12754c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
12764c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12774c367dbcSMatthew G. Knepley           PetscInt val, face;
12784c367dbcSMatthew G. Knepley 
12794c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12804c367dbcSMatthew G. Knepley           if (val == dim-1) {
12814c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
12824c367dbcSMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
12834c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
12844c367dbcSMatthew G. Knepley           }
12854c367dbcSMatthew G. Knepley         }
12864c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1287cd0c2139SMatthew G Knepley       }
1288cd0c2139SMatthew G Knepley     }
1289cd0c2139SMatthew G Knepley   }
129018c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
129118c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
129218c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
12932582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
129418c5995bSMatthew G. Knepley       const PetscInt *cone, *support, *ornt;
1295e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
129618c5995bSMatthew G. Knepley 
129718c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
129818c5995bSMatthew G. Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
129918c5995bSMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
130018c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
130118c5995bSMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
130218c5995bSMatthew G. Knepley       if (dep == 0) {
130318c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
130418c5995bSMatthew G. Knepley 
130518c5995bSMatthew G. Knepley         /* Unsplit vertex */
130618c5995bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
130718c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
13082582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
130918c5995bSMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
131018c5995bSMatthew G. Knepley           if (e >= 0) {
131118c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
131218c5995bSMatthew G. Knepley           }
131318c5995bSMatthew G. Knepley         }
131418c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
131518c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
131618c5995bSMatthew G. Knepley         if (q != supportSizeNew) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
131718c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
131818c5995bSMatthew G. Knepley         /* Hybrid edge */
131918c5995bSMatthew G. Knepley         coneNew[0] = newp;
132018c5995bSMatthew G. Knepley         coneNew[1] = newp;
132118c5995bSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
132218c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
132318c5995bSMatthew G. Knepley           PetscInt val, edge;
132418c5995bSMatthew G. Knepley 
132518c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
132618c5995bSMatthew G. Knepley           if (val == 1) {
132718c5995bSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
132818c5995bSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
132918c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1330e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
1331e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);CHKERRQ(ierr);
1332e1757548SMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1333e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
133418c5995bSMatthew G. Knepley           }
133518c5995bSMatthew G. Knepley         }
133618c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1337e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1338e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1339e1757548SMatthew G. Knepley 
1340da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1341e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1342e1757548SMatthew G. Knepley           PetscInt val, face;
1343e1757548SMatthew G. Knepley 
1344e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1345e1757548SMatthew G. Knepley           if (val == dim-1) {
1346e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1347e1757548SMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
13482582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1349e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1350e1757548SMatthew G. Knepley           } else {
13512582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1352e1757548SMatthew G. Knepley           }
1353e1757548SMatthew G. Knepley         }
1354e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1355e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1356da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
1357da1dd7e4SMatthew 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);
1358e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1359e1757548SMatthew G. Knepley         /* Add hybrid face */
1360e1757548SMatthew G. Knepley         coneNew[0] = newp;
1361212cc919SMatthew G. Knepley         coneNew[1] = newp;
1362e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1363e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1364212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1365e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[1], 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[1]);
1367e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1368e1757548SMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
1369da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1370da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1371da1dd7e4SMatthew G. Knepley 
1372da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1373da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
1374da1dd7e4SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1375da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1376da1dd7e4SMatthew G. Knepley           }
1377da1dd7e4SMatthew G. Knepley         }
1378da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);CHKERRQ(ierr);
1379da1dd7e4SMatthew 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);
1380e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1381cd0c2139SMatthew G Knepley       }
1382cd0c2139SMatthew G Knepley     }
1383cd0c2139SMatthew G Knepley   }
1384cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1385cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1386cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1387cd0c2139SMatthew G Knepley     IS              pIS;
1388cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1389cd0c2139SMatthew G Knepley     const PetscInt *points;
1390cd0c2139SMatthew G Knepley 
1391cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
1392cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr);
1393cd0c2139SMatthew G Knepley     if (!pIS) continue;
1394cd0c2139SMatthew G Knepley     dep  = -dep - shift;
1395cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr);
1396cd0c2139SMatthew G Knepley     ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr);
1397cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1398cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
13992582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1400cd0c2139SMatthew G Knepley       const PetscInt *cone;
1401cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
140250cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1403cd0c2139SMatthew G Knepley 
1404cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1405cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
1406cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr);
1407cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr);
1408cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1409e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1410cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1411cd0c2139SMatthew G Knepley 
1412cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr);
1413cd0c2139SMatthew G Knepley         if (val == dep-1) {
1414cd0c2139SMatthew G Knepley           ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr);
1415cd0c2139SMatthew G Knepley           if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1416cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
1417cd0c2139SMatthew G Knepley           ierr     = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr);
141850cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1419cd0c2139SMatthew G Knepley         }
1420cd0c2139SMatthew G Knepley       }
14214a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
14224a189a86SMatthew G. Knepley       /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1423cd0c2139SMatthew G Knepley     }
1424cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr);
1425cd0c2139SMatthew G Knepley     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
1426cd0c2139SMatthew G Knepley   }
1427fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
1428cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
142969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr);
1430cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr);
1431cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1432cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
14332582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1434cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1435cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1436cd0c2139SMatthew G Knepley 
1437cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr);
1438cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr);
1439cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr);
1440cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1441cd0c2139SMatthew G Knepley   }
1442cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1443fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
1444cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1445fa8e8ae5SToby Isaac   /* Step 9: Labels */
1446cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1447c58f1c22SToby Isaac   ierr = DMGetNumLabels(sdm, &numLabels);CHKERRQ(ierr);
1448cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1449cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
14502582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1451cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1452cd0c2139SMatthew G Knepley       PetscInt       l;
1453cd0c2139SMatthew G Knepley 
14547db7e0a7SMatthew G. Knepley       if (splitLabel) {
14557db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
14567db7e0a7SMatthew G. Knepley 
14577db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, newp,    val);CHKERRQ(ierr);
14587db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, splitp, -val);CHKERRQ(ierr);
14597db7e0a7SMatthew G. Knepley       }
1460cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1461cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1462cd0c2139SMatthew G Knepley         const char *lname;
1463cd0c2139SMatthew G Knepley         PetscInt    val;
14649a356370SMatthew G. Knepley         PetscBool   isDepth;
1465cd0c2139SMatthew G Knepley 
1466c58f1c22SToby Isaac         ierr = DMGetLabelName(sdm, l, &lname);CHKERRQ(ierr);
14679a356370SMatthew G. Knepley         ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
14689a356370SMatthew G. Knepley         if (isDepth) continue;
1469c58f1c22SToby Isaac         ierr = DMGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr);
1470cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr);
1471cd0c2139SMatthew G Knepley         if (val >= 0) {
1472cd0c2139SMatthew G Knepley           ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr);
1473cd0c2139SMatthew G Knepley         }
1474cd0c2139SMatthew G Knepley       }
1475cd0c2139SMatthew G Knepley     }
1476cd0c2139SMatthew G Knepley   }
1477cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1478cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1479cd0c2139SMatthew G Knepley 
1480cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
148118c5995bSMatthew G. Knepley     if (splitIS[dep]) {ierr = ISRestoreIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);}
148218c5995bSMatthew G. Knepley     ierr = ISDestroy(&splitIS[dep]);CHKERRQ(ierr);
148318c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {ierr = ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);}
148418c5995bSMatthew G. Knepley     ierr = ISDestroy(&unsplitIS[dep]);CHKERRQ(ierr);
1485cd0c2139SMatthew G Knepley   }
1486cd0c2139SMatthew G Knepley   if (label) {
1487cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
1488cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1489cd0c2139SMatthew G Knepley   }
14900d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
14910d4d4d06SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr);
149236dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
14930d4d4d06SMatthew G. Knepley   }
14940e49e2e2SMatthew 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);
1495dcbb62e8SMatthew G. Knepley   ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr);
14962582d50cSToby Isaac   ierr = PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld);CHKERRQ(ierr);
149718c5995bSMatthew G. Knepley   ierr = PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);CHKERRQ(ierr);
1498cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1499cd0c2139SMatthew G Knepley }
1500cd0c2139SMatthew G Knepley 
1501cd0c2139SMatthew G Knepley /*@C
1502cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1503cd0c2139SMatthew G Knepley 
1504cd0c2139SMatthew G Knepley   Collective on dm
1505cd0c2139SMatthew G Knepley 
1506cd0c2139SMatthew G Knepley   Input Parameters:
1507cd0c2139SMatthew G Knepley + dm - The original DM
150853156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1509cd0c2139SMatthew G Knepley 
1510cd0c2139SMatthew G Knepley   Output Parameters:
15117db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1512cd0c2139SMatthew G Knepley - dmSplit - The new DM
1513cd0c2139SMatthew G Knepley 
1514cd0c2139SMatthew G Knepley   Level: developer
1515cd0c2139SMatthew G Knepley 
15162be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1517cd0c2139SMatthew G Knepley @*/
15187db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1519cd0c2139SMatthew G Knepley {
1520cd0c2139SMatthew G Knepley   DM             sdm;
1521cd0c2139SMatthew G Knepley   PetscInt       dim;
1522cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
1523cd0c2139SMatthew G Knepley 
1524cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1525cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
152653156dfcSMatthew G. Knepley   PetscValidPointer(dmSplit, 3);
1527cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr);
1528cd0c2139SMatthew G Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
1529c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1530c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(sdm, dim);CHKERRQ(ierr);
1531cd0c2139SMatthew G Knepley   switch (dim) {
1532cd0c2139SMatthew G Knepley   case 2:
1533cd0c2139SMatthew G Knepley   case 3:
15347db7e0a7SMatthew G. Knepley     ierr = DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm);CHKERRQ(ierr);
1535cd0c2139SMatthew G Knepley     break;
1536cd0c2139SMatthew G Knepley   default:
1537cd0c2139SMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1538cd0c2139SMatthew G Knepley   }
1539cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1540cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1541cd0c2139SMatthew G Knepley }
1542cd0c2139SMatthew G Knepley 
15430f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
15440f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
15450f66a230SMatthew G. Knepley {
15460f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
15470f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
15480f66a230SMatthew G. Knepley   PetscErrorCode  ierr;
15490f66a230SMatthew G. Knepley 
15500f66a230SMatthew G. Knepley   PetscFunctionBegin;
15510f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
1552c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
15530f66a230SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
15540f66a230SMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
15550f66a230SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
15560f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
15570f66a230SMatthew G. Knepley     if (cone[c] == face) {
15580f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
15590f66a230SMatthew G. Knepley 
15600f66a230SMatthew G. Knepley       if (subdm) {
15610f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
15620f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
15630f66a230SMatthew G. Knepley 
15640f66a230SMatthew G. Knepley         ierr = PetscFindInt(cell, numSubpoints, subpoints, &subpoint);CHKERRQ(ierr);
15650f66a230SMatthew G. Knepley         ierr = PetscFindInt(face, numSubpoints, subpoints, &subface);CHKERRQ(ierr);
15660f66a230SMatthew G. Knepley         ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
15670f66a230SMatthew G. Knepley         ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr);
15680f66a230SMatthew G. Knepley         ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr);
15690f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
15700f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
15710f66a230SMatthew G. Knepley             o = subornt[0];
15720f66a230SMatthew G. Knepley             break;
15730f66a230SMatthew G. Knepley           }
15740f66a230SMatthew G. Knepley         }
15750f66a230SMatthew 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);
15760f66a230SMatthew G. Knepley       }
15770f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
15780f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
15790f66a230SMatthew G. Knepley       break;
15800f66a230SMatthew G. Knepley     }
15810f66a230SMatthew G. Knepley   }
15820f66a230SMatthew 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);
15830f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
15840f66a230SMatthew G. Knepley }
15850f66a230SMatthew G. Knepley 
1586cd0c2139SMatthew G Knepley /*@
15870f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1588cd0c2139SMatthew G Knepley   to complete the surface
1589cd0c2139SMatthew G Knepley 
1590cd0c2139SMatthew G Knepley   Input Parameters:
1591cd0c2139SMatthew G Knepley + dm     - The DM
15920f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
15930f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1594bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
159547946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1596cd0c2139SMatthew G Knepley 
1597cd0c2139SMatthew G Knepley   Output Parameter:
1598cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1599cd0c2139SMatthew G Knepley 
16000f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
16010f66a230SMatthew G. Knepley 
1602cd0c2139SMatthew G Knepley   Level: developer
1603cd0c2139SMatthew G Knepley 
16042be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1605cd0c2139SMatthew G Knepley @*/
16060f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1607cd0c2139SMatthew G Knepley {
1608d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
16098630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
161047946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1611bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1612370472baSMatthew G. Knepley   PetscInt       *pMax;
1613370472baSMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, dim, depth, pSize, dep, cStart, cEnd, cMax, fStart, fEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1614cd0c2139SMatthew G Knepley   PetscErrorCode  ierr;
1615cd0c2139SMatthew G Knepley 
1616cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1617370472baSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1618370472baSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1619370472baSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
1620370472baSMatthew G. Knepley   ierr = PetscMalloc1(pSize,&pMax);CHKERRQ(ierr);
1621370472baSMatthew 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);
1622d90583fdSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1623c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
162447946fd8SMatthew G. Knepley   if (subdm) {
162547946fd8SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
162647946fd8SMatthew G. Knepley     if (subpointIS) {
162747946fd8SMatthew G. Knepley       ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
162847946fd8SMatthew G. Knepley       ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
162947946fd8SMatthew G. Knepley     }
163047946fd8SMatthew G. Knepley   }
1631d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1632cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr);
1633dc86033cSMatthew G. Knepley   if (!dimIS) {
1634dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
16358630cb1aSMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1636dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1637dc86033cSMatthew G. Knepley   }
1638cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1639cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1640d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1641cd0c2139SMatthew G Knepley     const PetscInt *support;
1642cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1643cd0c2139SMatthew G Knepley 
1644cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr);
1645c4419245SMatthew G. Knepley #if 0
1646c4419245SMatthew G. Knepley     if (supportSize != 2) {
1647c4419245SMatthew G. Knepley       const PetscInt *lp;
1648c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1649c4419245SMatthew G. Knepley 
1650c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1651c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
1652c4419245SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL);CHKERRQ(ierr);
1653c4419245SMatthew G. Knepley       ierr = PetscFindInt(points[p], Nlp, lp, &pind);CHKERRQ(ierr);
1654c4419245SMatthew 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);
1655c4419245SMatthew G. Knepley     }
1656c4419245SMatthew G. Knepley #endif
1657cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr);
1658cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
16590f66a230SMatthew G. Knepley       const PetscInt *cone;
1660cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
16610f66a230SMatthew G. Knepley       PetscBool       pos;
1662cd0c2139SMatthew G Knepley 
16630f66a230SMatthew G. Knepley       ierr = GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);CHKERRQ(ierr);
16640f66a230SMatthew G. Knepley       if (pos) {ierr = DMLabelSetValue(label, support[s],  rev*(shift+dim));CHKERRQ(ierr);}
16650f66a230SMatthew G. Knepley       else     {ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr);}
16660f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
16670f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
1668cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
1669cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
1670cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1671cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1672cd0c2139SMatthew G Knepley 
1673cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1674cd0c2139SMatthew G Knepley         if (val == -1) {
1675cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1676cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1677cd0c2139SMatthew G Knepley 
1678cd0c2139SMatthew G Knepley           ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1679cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1680cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1681a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1682cd0c2139SMatthew G Knepley 
1683cd0c2139SMatthew G Knepley             ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1684a0541d8aSMatthew G. Knepley             if (blabel) {ierr = DMLabelGetValue(blabel, clp, &bval);CHKERRQ(ierr);}
1685a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1686cd0c2139SMatthew G Knepley               ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr);
1687cd0c2139SMatthew G Knepley               break;
1688cd0c2139SMatthew G Knepley             }
1689cd0c2139SMatthew G Knepley           }
1690cd0c2139SMatthew G Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1691cd0c2139SMatthew G Knepley         }
1692cd0c2139SMatthew G Knepley       }
1693cd0c2139SMatthew G Knepley     }
1694cd0c2139SMatthew G Knepley   }
16950f66a230SMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
16960f66a230SMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
169747946fd8SMatthew G. Knepley   if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);}
169847946fd8SMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1699a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
170086200784SMatthew G. Knepley   if (blabel) {
1701a0541d8aSMatthew G. Knepley     ierr = DMLabelGetStratumIS(blabel, 1, &dimIS);CHKERRQ(ierr);
1702a0541d8aSMatthew G. Knepley     ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1703a0541d8aSMatthew G. Knepley     ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1704a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1705a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1706a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1707a0541d8aSMatthew G. Knepley 
1708a0541d8aSMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1709a0541d8aSMatthew G. Knepley       if (bval >= 0) {
1710f7019248SMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1711f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1712f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1713f7019248SMatthew G. Knepley           ierr = DMLabelClearValue(blabel, point, bval);CHKERRQ(ierr);
1714f7019248SMatthew G. Knepley         }
1715f7019248SMatthew G. Knepley       }
1716f7019248SMatthew G. Knepley     }
1717f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1718f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1719f7019248SMatthew G. Knepley       PetscInt       val, bval;
1720f7019248SMatthew G. Knepley 
1721f7019248SMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1722f7019248SMatthew G. Knepley       if (bval >= 0) {
172386200784SMatthew G. Knepley         const PetscInt *cone,    *support;
172486200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
172586200784SMatthew G. Knepley 
1726a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
1727a0541d8aSMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1728a0541d8aSMatthew 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);
1729a0541d8aSMatthew G. Knepley         ierr = DMLabelClearValue(label, point, val);CHKERRQ(ierr);
1730a0541d8aSMatthew G. Knepley         ierr = DMLabelSetValue(label, point, shift2+val);CHKERRQ(ierr);
17312c06a818SMatthew G. Knepley         /* Check for cross-edge
17322c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
173386200784SMatthew G. Knepley         if (val != 0) continue;
173486200784SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
173586200784SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
173686200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
173786200784SMatthew G. Knepley           ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
173886200784SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
173986200784SMatthew G. Knepley           if (coneSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
174086200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[0], &valA);CHKERRQ(ierr);
174186200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[1], &valB);CHKERRQ(ierr);
174286200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, support[s], &valE);CHKERRQ(ierr);
174309816f77SMatthew G. Knepley           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) {ierr = DMLabelSetValue(blabel, support[s], 2);CHKERRQ(ierr);}
174486200784SMatthew G. Knepley         }
1745a0541d8aSMatthew G. Knepley       }
1746a0541d8aSMatthew G. Knepley     }
1747a0541d8aSMatthew G. Knepley     ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1748a0541d8aSMatthew G. Knepley     ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1749a0541d8aSMatthew G. Knepley   }
1750cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
1751c4d480a2SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1752cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1753d24f5ce5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1754d24f5ce5SMatthew G. Knepley   cMax = cMax < 0 ? cEnd : cMax;
17550f66a230SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1756cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr);
175786200784SMatthew G. Knepley   if (blabel) {ierr = DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);CHKERRQ(ierr);}
175886200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
175986200784SMatthew G. Knepley     IS vertIS = dimIS;
176086200784SMatthew G. Knepley 
176186200784SMatthew G. Knepley     ierr = ISExpand(vertIS, crossEdgeIS, &dimIS);CHKERRQ(ierr);
176286200784SMatthew G. Knepley     ierr = ISDestroy(&crossEdgeIS);CHKERRQ(ierr);
176386200784SMatthew G. Knepley     ierr = ISDestroy(&vertIS);CHKERRQ(ierr);
176486200784SMatthew G. Knepley   }
1765dc86033cSMatthew G. Knepley   if (!dimIS) {
1766dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
1767dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1768dc86033cSMatthew G. Knepley   }
1769cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1770cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1771d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1772cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
177386200784SMatthew G. Knepley     PetscInt  starSize, s;
1774cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1775cd0c2139SMatthew G Knepley 
1776d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1777cd0c2139SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1778cd0c2139SMatthew G Knepley     while (again) {
1779cd0c2139SMatthew G Knepley       if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1780cd0c2139SMatthew G Knepley       again = 0;
1781cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1782cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1783cd0c2139SMatthew G Knepley         const PetscInt *cone;
1784cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1785cd0c2139SMatthew G Knepley 
1786d24f5ce5SMatthew G. Knepley         if ((point < cStart) || (point >= cMax)) continue;
1787cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1788cd0c2139SMatthew G Knepley         if (val != -1) continue;
1789d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
1790cd0c2139SMatthew G Knepley         ierr  = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1791cd0c2139SMatthew G Knepley         ierr  = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
1792cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
1793cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr);
1794cd0c2139SMatthew G Knepley           if (val != -1) {
1795d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1796166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1797d7c8f101SMatthew G. Knepley 
1798db8edaafSMatthew 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);
179937a6de01SMatthew G. Knepley             if (val > 0) side =  1;
180037a6de01SMatthew G. Knepley             else         side = -1;
180137a6de01SMatthew G. Knepley             ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr);
1802d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
1803d7c8f101SMatthew G. Knepley             ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr);
1804d7c8f101SMatthew G. Knepley             ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr);
1805d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
1806d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
1807d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
1808d7c8f101SMatthew G. Knepley 
1809166d9d0cSMatthew G. Knepley               ierr = DMLabelGetValue(label, ccone[cc], &val);CHKERRQ(ierr);
1810166d9d0cSMatthew G. Knepley               if (val != -1) continue;
1811d7c8f101SMatthew G. Knepley               ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1812d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
1813d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
1814d7c8f101SMatthew G. Knepley 
1815d7c8f101SMatthew G. Knepley                 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1816d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
1817d7c8f101SMatthew G. Knepley                 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr);
181837a6de01SMatthew G. Knepley                 break;
181937a6de01SMatthew G. Knepley               }
1820d7c8f101SMatthew G. Knepley               ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1821cd0c2139SMatthew G Knepley             }
1822cd0c2139SMatthew G Knepley             again = 1;
1823cd0c2139SMatthew G Knepley             break;
1824cd0c2139SMatthew G Knepley           }
1825cd0c2139SMatthew G Knepley         }
1826cd0c2139SMatthew G Knepley       }
1827cd0c2139SMatthew G Knepley     }
1828cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
1829cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1830cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
1831cd0c2139SMatthew G Knepley 
1832cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1833cd0c2139SMatthew G Knepley       if (val == -1) {
1834cd0c2139SMatthew G Knepley         PetscInt  *sstar = NULL;
1835cd0c2139SMatthew G Knepley         PetscInt   sstarSize, ss;
1836cd0c2139SMatthew G Knepley         PetscBool  marked = PETSC_FALSE;
1837cd0c2139SMatthew G Knepley 
1838cd0c2139SMatthew G Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1839cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
1840cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
1841cd0c2139SMatthew G Knepley 
1842d24f5ce5SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cMax)) continue;
1843cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr);
1844cd0c2139SMatthew 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);
1845d90583fdSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1846cd0c2139SMatthew G Knepley           if (val > 0) {
1847cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point,   shift+dep);CHKERRQ(ierr);
1848cd0c2139SMatthew G Knepley           } else {
1849cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr);
1850cd0c2139SMatthew G Knepley           }
1851cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
1852cd0c2139SMatthew G Knepley           break;
1853cd0c2139SMatthew G Knepley         }
1854cd0c2139SMatthew G Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1855370472baSMatthew G. Knepley         ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1856370472baSMatthew G. Knepley         if (point < pMax[dep] && !marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1857cd0c2139SMatthew G Knepley       }
1858cd0c2139SMatthew G Knepley     }
1859cd0c2139SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1860cd0c2139SMatthew G Knepley   }
1861cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1862cd0c2139SMatthew G Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
186318c5995bSMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
186418c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);CHKERRQ(ierr);
186518c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);CHKERRQ(ierr);
186618c5995bSMatthew G. Knepley   ierr = ISExpand(facePosIS, faceNegIS, &dimIS);CHKERRQ(ierr);
186718c5995bSMatthew G. Knepley   ierr = ISDestroy(&facePosIS);CHKERRQ(ierr);
186818c5995bSMatthew G. Knepley   ierr = ISDestroy(&faceNegIS);CHKERRQ(ierr);
186918c5995bSMatthew G. Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
187018c5995bSMatthew G. Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
187118c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
187218c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
187318c5995bSMatthew G. Knepley     const PetscInt *support;
187418c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
187518c5995bSMatthew G. Knepley 
187618c5995bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
187718c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
187818c5995bSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
187918c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[0], &valA);CHKERRQ(ierr);
188018c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[1], &valB);CHKERRQ(ierr);
188118c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
188218c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
188318c5995bSMatthew G. Knepley     /* Split the face */
188418c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &valA);CHKERRQ(ierr);
188518c5995bSMatthew G. Knepley     ierr = DMLabelClearValue(label, point, valA);CHKERRQ(ierr);
188618c5995bSMatthew G. Knepley     ierr = DMLabelSetValue(label, point, dim-1);CHKERRQ(ierr);
1887e1757548SMatthew G. Knepley     /* Label its closure:
1888e1757548SMatthew G. Knepley       unmarked: label as unsplit
1889e1757548SMatthew G. Knepley       incident: relabel as split
1890e1757548SMatthew G. Knepley       split:    do nothing
1891e1757548SMatthew G. Knepley     */
189218c5995bSMatthew G. Knepley     {
189318c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
189418c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
189518c5995bSMatthew G. Knepley 
189618c5995bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
189718c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
189818c5995bSMatthew G. Knepley         ierr = DMLabelGetValue(label, closure[cl], &valA);CHKERRQ(ierr);
18998771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
190018c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
190118c5995bSMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], shift2+dep);CHKERRQ(ierr);
19028771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1903e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
1904e1757548SMatthew G. Knepley           ierr = DMLabelClearValue(label, closure[cl], valA);CHKERRQ(ierr);
1905e1757548SMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], dep);CHKERRQ(ierr);
1906e1757548SMatthew G. Knepley         }
190718c5995bSMatthew G. Knepley       }
190818c5995bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
190918c5995bSMatthew G. Knepley     }
191018c5995bSMatthew G. Knepley   }
191118c5995bSMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
191218c5995bSMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1913370472baSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
1914cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1915cd0c2139SMatthew G Knepley }
1916cd0c2139SMatthew G Knepley 
1917720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
1918720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
1919720e594eSMatthew G. Knepley {
1920720e594eSMatthew G. Knepley   IS              subpointIS;
1921720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
1922720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
1923720e594eSMatthew G. Knepley   PetscErrorCode  ierr;
1924720e594eSMatthew G. Knepley 
1925720e594eSMatthew G. Knepley   PetscFunctionBegin;
1926720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
1927720e594eSMatthew G. Knepley   ierr = DMLabelGetDefaultValue(label, &defaultValue);CHKERRQ(ierr);
1928720e594eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
1929720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
1930720e594eSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1931720e594eSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1932720e594eSMatthew G. Knepley   ierr = ISGetIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1933720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1934720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
1935720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
1936720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
1937720e594eSMatthew G. Knepley 
1938720e594eSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1939720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
1940720e594eSMatthew G. Knepley       PetscInt value = 0;
1941720e594eSMatthew G. Knepley 
1942720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
1943720e594eSMatthew G. Knepley       ierr = DMLabelGetValue(label, closure[cl], &value);CHKERRQ(ierr);
1944720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
1945720e594eSMatthew G. Knepley     }
1946720e594eSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1947720e594eSMatthew G. Knepley     if (invalidCell) {
1948720e594eSMatthew G. Knepley       ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1949720e594eSMatthew G. Knepley       ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1950720e594eSMatthew G. Knepley       ierr = DMDestroy(&subdm);CHKERRQ(ierr);
1951720e594eSMatthew G. Knepley       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %D has all of its vertices on the submesh.", dmpoints[c]);
1952720e594eSMatthew G. Knepley     }
1953720e594eSMatthew G. Knepley   }
1954720e594eSMatthew G. Knepley   ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1955720e594eSMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1956720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
1957720e594eSMatthew G. Knepley }
1958720e594eSMatthew G. Knepley 
1959720e594eSMatthew G. Knepley 
1960c08575a3SMatthew G. Knepley /*@
19613cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
19623cf72582SMatthew G. Knepley 
19633cf72582SMatthew G. Knepley   Collective on dm
19643cf72582SMatthew G. Knepley 
19653cf72582SMatthew G. Knepley   Input Parameters:
19663cf72582SMatthew G. Knepley + dm - The original DM
1967720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
1968720e594eSMatthew G. Knepley - bdlabel - The optional label specifying the interface boundary vertices
19693cf72582SMatthew G. Knepley 
19703cf72582SMatthew G. Knepley   Output Parameters:
19717db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
19727db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
1973720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
1974720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
19753cf72582SMatthew G. Knepley 
19766eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
19776eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
19786eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
19796eccb800SMatthew Knepley   one vertex 3 on the surface would be 6 (101) and 3 (0) in hybridLabel. If an edge 9 from the negative side of the
19806eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
19816eccb800SMatthew Knepley 
19826eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
19836eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
19846eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
19856eccb800SMatthew Knepley 
19866eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
19876eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
19886eccb800SMatthew Knepley 
19893cf72582SMatthew G. Knepley   Level: developer
19903cf72582SMatthew G. Knepley 
19916eccb800SMatthew Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMPlexGetSubpointMap(), DMCreate()
19923cf72582SMatthew G. Knepley @*/
19937db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
19943cf72582SMatthew G. Knepley {
19953cf72582SMatthew G. Knepley   DM             idm;
19967db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
19973cf72582SMatthew G. Knepley   PetscInt       dim;
19983cf72582SMatthew G. Knepley   PetscErrorCode ierr;
19993cf72582SMatthew G. Knepley 
20003cf72582SMatthew G. Knepley   PetscFunctionBegin;
20013cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2002720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
2003720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
20047db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
20057db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
20067db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
2007c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2008158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm);CHKERRQ(ierr);
2009720e594eSMatthew G. Knepley   ierr = DMPlexCheckValidSubmesh_Private(dm, label, idm);CHKERRQ(ierr);
20103cf72582SMatthew G. Knepley   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
20113cf72582SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
20123cf72582SMatthew G. Knepley   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
20133cf72582SMatthew G. Knepley   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
20147db7e0a7SMatthew G. Knepley   if (splitLabel) {
20157db7e0a7SMatthew G. Knepley     const char *name;
20167db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
20177db7e0a7SMatthew G. Knepley 
2018d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) hlabel, &name);CHKERRQ(ierr);
20197db7e0a7SMatthew G. Knepley     ierr = PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
20207db7e0a7SMatthew G. Knepley     ierr = PetscStrcat(sname, " split");CHKERRQ(ierr);
2021d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, sname, &slabel);CHKERRQ(ierr);
20227db7e0a7SMatthew G. Knepley   }
2023720e594eSMatthew G. Knepley   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm);CHKERRQ(ierr);
2024720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
2025720e594eSMatthew G. Knepley   else             {ierr = DMDestroy(&idm);CHKERRQ(ierr);}
20267db7e0a7SMatthew G. Knepley   ierr = DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid);CHKERRQ(ierr);
20273cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
202853aa2e23SMatthew G. Knepley   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
20297db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
2030cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2031cd0c2139SMatthew G Knepley }
2032cd0c2139SMatthew G Knepley 
2033efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2034efa14ee0SMatthew G Knepley 
2035efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2036efa14ee0SMatthew G Knepley */
2037830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2038efa14ee0SMatthew G Knepley {
2039fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2040efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
204177d178adSMatthew G. Knepley   PetscInt        *pStart, *pEnd, *pMax, pSize;
2042efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2043efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2044efa14ee0SMatthew G Knepley 
2045efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2046efa14ee0SMatthew G Knepley   *numFaces = 0;
2047efa14ee0SMatthew G Knepley   *nFV      = 0;
2048efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2049c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
205077d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
2051dcca6d9dSJed Brown   ierr = PetscMalloc3(pSize,&pStart,pSize,&pEnd,pSize,&pMax);CHKERRQ(ierr);
2052d92e47f8SMatthew 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);
2053efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
2054efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2055efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2056efa14ee0SMatthew G Knepley   }
2057efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
2058fed694aaSMatthew G. Knepley   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
2059efa14ee0SMatthew G Knepley   if (subvertexIS) {
2060efa14ee0SMatthew G Knepley     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2061efa14ee0SMatthew G Knepley     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2062efa14ee0SMatthew G Knepley   }
2063efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2064efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
20650298fd71SBarry Smith     PetscInt      *star   = NULL;
2066efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2067efa14ee0SMatthew G Knepley 
2068efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2069efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2070efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2071efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2072efa14ee0SMatthew G Knepley     }
2073efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2074efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
20750298fd71SBarry Smith       PetscInt      *closure = NULL;
2076efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2077efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2078efa14ee0SMatthew G Knepley 
2079efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
208065560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
208182f516ccSBarry Smith       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
2082efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2083efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2084efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2085efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2086efa14ee0SMatthew G Knepley 
2087efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2088efa14ee0SMatthew G Knepley           ++numCorners;
2089efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2090830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2091efa14ee0SMatthew G Knepley         }
2092efa14ee0SMatthew G Knepley       }
209318ad9376SMatthew G. Knepley       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
209482f516ccSBarry 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);
2095efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2096007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2097007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2098007baee2SMatthew G. Knepley 
2099efa14ee0SMatthew G Knepley         ++(*numFaces);
2100efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2101efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
2102efa14ee0SMatthew G Knepley         }
2103007baee2SMatthew G. Knepley         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2104007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2105007baee2SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
2106007baee2SMatthew G. Knepley         }
2107007baee2SMatthew G. Knepley         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2108efa14ee0SMatthew G Knepley       }
2109efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2110efa14ee0SMatthew G Knepley     }
2111efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2112efa14ee0SMatthew G Knepley   }
2113efa14ee0SMatthew G Knepley   if (subvertexIS) {
2114efa14ee0SMatthew G Knepley     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2115efa14ee0SMatthew G Knepley   }
2116efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2117efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2118efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2119efa14ee0SMatthew G Knepley }
2120efa14ee0SMatthew G Knepley 
2121158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2122efa14ee0SMatthew G Knepley {
212334b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2124efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2125efa14ee0SMatthew G Knepley   PetscInt        *pStart, *pEnd, *pMax;
2126efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2127efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2128efa14ee0SMatthew G Knepley 
2129efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2130c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2131dcca6d9dSJed Brown   ierr = PetscMalloc3(dim+1,&pStart,dim+1,&pEnd,dim+1,&pMax);CHKERRQ(ierr);
21320298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
2133efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2134efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2135efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2136efa14ee0SMatthew G Knepley   }
2137efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
213834b4c39eSMatthew G. Knepley   if (vertexLabel) {
2139830e53efSMatthew G. Knepley     ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
2140efa14ee0SMatthew G Knepley     if (subvertexIS) {
2141efa14ee0SMatthew G Knepley       ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2142efa14ee0SMatthew G Knepley       ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2143efa14ee0SMatthew G Knepley     }
214434b4c39eSMatthew G. Knepley   }
2145efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2146efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
21470298fd71SBarry Smith     PetscInt      *star   = NULL;
2148efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2149efa14ee0SMatthew G Knepley 
2150efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2151efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2152efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2153158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2154158acfadSMatthew G. Knepley 
2155158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2156158acfadSMatthew G. Knepley         if (markedFaces) {
2157158acfadSMatthew G. Knepley           ierr = DMLabelGetValue(vertexLabel, point, &faceLoc);CHKERRQ(ierr);
2158158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2159158acfadSMatthew G. Knepley         }
2160158acfadSMatthew G. Knepley         star[numFaces++] = point;
2161158acfadSMatthew G. Knepley       }
2162efa14ee0SMatthew G Knepley     }
2163efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2164efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
21650298fd71SBarry Smith       PetscInt      *closure = NULL;
2166efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2167efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2168efa14ee0SMatthew G Knepley 
2169efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
2170efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
217182f516ccSBarry Smith       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2172efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2173efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2174efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2175efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2176efa14ee0SMatthew G Knepley 
2177efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2178efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2179830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2180efa14ee0SMatthew G Knepley         }
2181efa14ee0SMatthew G Knepley       }
2182efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2183efa14ee0SMatthew G Knepley         const PetscInt *support;
2184efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2185efa14ee0SMatthew G Knepley 
2186efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2187efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2188efa14ee0SMatthew G Knepley 
2189efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2190efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2191efa14ee0SMatthew G Knepley               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2192efa14ee0SMatthew G Knepley               break;
2193efa14ee0SMatthew G Knepley             }
2194efa14ee0SMatthew G Knepley           }
2195efa14ee0SMatthew G Knepley         }
2196efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2197efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2198efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2199efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2200efa14ee0SMatthew G Knepley         }
2201efa14ee0SMatthew G Knepley       }
2202efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2203efa14ee0SMatthew G Knepley     }
2204efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2205efa14ee0SMatthew G Knepley   }
220634b4c39eSMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);}
2207efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2208efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2209efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2210efa14ee0SMatthew G Knepley }
2211efa14ee0SMatthew G Knepley 
221227c04023SMatthew 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)
2213766ab985SMatthew G. Knepley {
221427c04023SMatthew G. Knepley   DMLabel         label = NULL;
2215766ab985SMatthew G. Knepley   const PetscInt *cone;
22169fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2217766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
2218766ab985SMatthew G. Knepley 
2219812bfc34SJed Brown   PetscFunctionBegin;
2220c0ed958bSJed Brown   *numFaces = 0;
2221c0ed958bSJed Brown   *nFV = 0;
2222c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
2223fed694aaSMatthew G. Knepley   *subCells = NULL;
2224c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2225766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2226766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2227766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
222827c04023SMatthew G. Knepley   if (label) {
222927c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
223027c04023SMatthew G. Knepley       PetscInt val;
223127c04023SMatthew G. Knepley 
223227c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
223327c04023SMatthew G. Knepley       if (val == value) {
223427c04023SMatthew G. Knepley         ++(*numFaces);
223527c04023SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
223627c04023SMatthew G. Knepley       }
223727c04023SMatthew G. Knepley     }
223827c04023SMatthew G. Knepley   } else {
2239766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
224027c04023SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
224127c04023SMatthew G. Knepley   }
2242785e854fSJed Brown   ierr = PetscMalloc1(*numFaces *2, subCells);CHKERRQ(ierr);
22439fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
22449fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2245766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2246766ab985SMatthew G. Knepley     const PetscInt *cells;
2247766ab985SMatthew G. Knepley     PetscInt        numCells;
2248766ab985SMatthew G. Knepley 
224927c04023SMatthew G. Knepley     if (label) {
225027c04023SMatthew G. Knepley       PetscInt val;
225127c04023SMatthew G. Knepley 
225227c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
225327c04023SMatthew G. Knepley       if (val != value) continue;
225427c04023SMatthew G. Knepley     }
2255766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2256766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2257766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
2258766ab985SMatthew G. Knepley     }
2259766ab985SMatthew G. Knepley     /* Negative face */
2260766ab985SMatthew G. Knepley     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
226127234c99SMatthew G. Knepley     /* Not true in parallel
226227234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2263766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2264766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
226527234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2266766ab985SMatthew G. Knepley     }
2267766ab985SMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2268766ab985SMatthew G. Knepley     /* Positive face is not included */
2269766ab985SMatthew G. Knepley   }
2270766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2271766ab985SMatthew G. Knepley }
2272766ab985SMatthew G. Knepley 
22733982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2274766ab985SMatthew G. Knepley {
2275766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2276766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2277766ab985SMatthew G. Knepley   PetscErrorCode ierr;
2278766ab985SMatthew G. Knepley 
2279812bfc34SJed Brown   PetscFunctionBegin;
2280c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2281766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2282766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2283766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2284dcca6d9dSJed Brown   ierr = PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);CHKERRQ(ierr);
2285b3154360SMatthew G. Knepley   for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);}
2286766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2287766ab985SMatthew G. Knepley     const PetscInt *cone;
2288766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2289b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2290766ab985SMatthew G. Knepley 
229127c04023SMatthew G. Knepley     if (label) {
229227c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
229327c04023SMatthew G. Knepley       if (val != value) continue;
229427c04023SMatthew G. Knepley     }
2295766ab985SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2296766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2297b3154360SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr);
2298b3154360SMatthew G. Knepley     if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2299b3154360SMatthew G. Knepley     /* Negative face */
2300766ab985SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2301766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2302766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2303766ab985SMatthew G. Knepley 
2304766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2305766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2306766ab985SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2307766ab985SMatthew G. Knepley           break;
2308766ab985SMatthew G. Knepley         }
2309766ab985SMatthew G. Knepley       }
2310766ab985SMatthew G. Knepley     }
2311766ab985SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2312766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2313766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2314766ab985SMatthew G. Knepley       const PetscInt *support;
2315766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2316766ab985SMatthew G. Knepley 
2317766ab985SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
2318711de394SMatthew G. Knepley       /* if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2319766ab985SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
2320766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2321766ab985SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2322766ab985SMatthew G. Knepley       }
2323766ab985SMatthew G. Knepley     }
2324766ab985SMatthew G. Knepley   }
2325766ab985SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2326766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2327766ab985SMatthew G. Knepley }
2328766ab985SMatthew G. Knepley 
2329c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2330e6ccafaeSMatthew G Knepley {
233182f516ccSBarry Smith   MPI_Comm       comm;
2332e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2333e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2334e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2335e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
2336e6ccafaeSMatthew G Knepley 
233782f516ccSBarry Smith   PetscFunctionBegin;
233882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2339c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
2340367003a6SStefano Zampini   if (debug) {ierr = PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
2341e6ccafaeSMatthew G Knepley 
2342ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2343ddeab2a6SMatthew G. Knepley     /* Triangle */
2344e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2345e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2346ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2347ddeab2a6SMatthew G. Knepley     /* Triangle */
2348ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2349ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2350ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2351ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2352ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2353ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2354e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2355e6ccafaeSMatthew G Knepley     /* Quadratic line */
2356e6ccafaeSMatthew G Knepley     faceSize  = 1;
2357e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2358e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2359e6ccafaeSMatthew G Knepley     /* Quads */
2360e6ccafaeSMatthew G Knepley     faceSize = 2;
2361e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2362e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2363e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2364e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2365e6ccafaeSMatthew G Knepley     } else {
2366e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2367e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2368e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2369e6ccafaeSMatthew G Knepley     }
2370e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2371e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2372e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2373e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2374e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2375e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2376e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2377e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2378e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2379e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2380e6ccafaeSMatthew G Knepley     };
2381e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2382e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2383e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2384e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2385e6ccafaeSMatthew G Knepley     };
2386e6ccafaeSMatthew G Knepley 
2387e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2388e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
2389e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2390e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2391e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2392e6ccafaeSMatthew G Knepley 
2393e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2394e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2395e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2396e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2397e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2398e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2399e6ccafaeSMatthew G Knepley               break;
2400e6ccafaeSMatthew G Knepley             }
2401e6ccafaeSMatthew G Knepley           }
2402e6ccafaeSMatthew G Knepley         }
2403e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2404e6ccafaeSMatthew G Knepley         break;
2405e6ccafaeSMatthew G Knepley       }
2406e6ccafaeSMatthew G Knepley     }
2407e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2408e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2409e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2410e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2411e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2412e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2413e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2414e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2415e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2416e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2417e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2418e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2419e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2420e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2421e6ccafaeSMatthew G Knepley     };
2422e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2423e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2424e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2425e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2426e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2427e6ccafaeSMatthew G Knepley     };
2428e6ccafaeSMatthew G Knepley 
2429e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2430e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
2431e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2432e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2433e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2434e6ccafaeSMatthew G Knepley 
2435e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2436e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2437e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2438e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2439e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2440e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2441e6ccafaeSMatthew G Knepley               break;
2442e6ccafaeSMatthew G Knepley             }
2443e6ccafaeSMatthew G Knepley           }
2444e6ccafaeSMatthew G Knepley         }
2445e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2446e6ccafaeSMatthew G Knepley         break;
2447e6ccafaeSMatthew G Knepley       }
2448e6ccafaeSMatthew G Knepley     }
2449e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2450e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2451e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2452e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2453e6ccafaeSMatthew G Knepley     /* Hexes
2454e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2455e6ccafaeSMatthew G Knepley        pointing up at the second.
2456e6ccafaeSMatthew G Knepley 
2457e6ccafaeSMatthew G Knepley           7---6
2458e6ccafaeSMatthew G Knepley          /|  /|
2459e6ccafaeSMatthew G Knepley         4---5 |
2460ddeab2a6SMatthew G. Knepley         | 1-|-2
2461e6ccafaeSMatthew G Knepley         |/  |/
2462ddeab2a6SMatthew G. Knepley         0---3
2463e6ccafaeSMatthew G Knepley 
2464e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2465e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2466e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2467e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2468e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2469e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2470e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2471ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2472ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2473ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2474ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2475e6ccafaeSMatthew G Knepley     };
2476e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2477ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2478e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2479ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2480ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2481ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2482ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2483e6ccafaeSMatthew G Knepley     };
2484e6ccafaeSMatthew G Knepley 
2485e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2486e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
2487e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2488e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2489e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2490e6ccafaeSMatthew G Knepley 
2491e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2492e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2493e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2494e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2495e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2496e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2497e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2498e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2499e6ccafaeSMatthew G Knepley               break;
2500e6ccafaeSMatthew G Knepley             }
2501e6ccafaeSMatthew G Knepley           }
2502e6ccafaeSMatthew G Knepley         }
2503e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2504e6ccafaeSMatthew G Knepley         break;
2505e6ccafaeSMatthew G Knepley       }
2506e6ccafaeSMatthew G Knepley     }
2507e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2508e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2509e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2510e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2511e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2512e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2513e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2514e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2515e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2516e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2517e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2518e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2519e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2520e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2521e6ccafaeSMatthew G Knepley     };
2522e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2523e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2524e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2525e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2526e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2527e6ccafaeSMatthew G Knepley     };
2528e6ccafaeSMatthew G Knepley 
2529e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2530e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
2531e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2532e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2533e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2534e6ccafaeSMatthew G Knepley 
2535e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2536e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2537e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2538e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2539e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2540e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2541e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2542e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2543e6ccafaeSMatthew G Knepley               break;
2544e6ccafaeSMatthew G Knepley             }
2545e6ccafaeSMatthew G Knepley           }
2546e6ccafaeSMatthew G Knepley         }
2547e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2548e6ccafaeSMatthew G Knepley         break;
2549e6ccafaeSMatthew G Knepley       }
2550e6ccafaeSMatthew G Knepley     }
2551e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2552e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2553e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2554e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2555e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2556e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2557e6ccafaeSMatthew G Knepley        pointing up at the second.
2558e6ccafaeSMatthew G Knepley 
2559e6ccafaeSMatthew G Knepley          7---6
2560e6ccafaeSMatthew G Knepley         /|  /|
2561e6ccafaeSMatthew G Knepley        4---5 |
2562e6ccafaeSMatthew G Knepley        | 3-|-2
2563e6ccafaeSMatthew G Knepley        |/  |/
2564e6ccafaeSMatthew G Knepley        0---1
2565e6ccafaeSMatthew G Knepley 
2566e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2567e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2568e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2569e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2570e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2571e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2572e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2573e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2574e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2575e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2576e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2577e6ccafaeSMatthew G Knepley     };
2578e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2579e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2580e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2581e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2582e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2583e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2584e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2585e6ccafaeSMatthew G Knepley     };
2586e6ccafaeSMatthew G Knepley 
2587e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2588e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
2589e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2590e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2591e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2592e6ccafaeSMatthew G Knepley 
2593e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2594e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2595e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2596e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2597e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2598e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2599e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2600e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2601e6ccafaeSMatthew G Knepley               break;
2602e6ccafaeSMatthew G Knepley             }
2603e6ccafaeSMatthew G Knepley           }
2604e6ccafaeSMatthew G Knepley         }
2605e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2606e6ccafaeSMatthew G Knepley         break;
2607e6ccafaeSMatthew G Knepley       }
2608e6ccafaeSMatthew G Knepley     }
2609e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2610e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2611e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2612e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2613e6ccafaeSMatthew G Knepley   if (!posOrient) {
2614e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
2615e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2616e6ccafaeSMatthew G Knepley   } else {
2617e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
2618e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2619e6ccafaeSMatthew G Knepley   }
2620e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2621e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2622e6ccafaeSMatthew G Knepley }
2623e6ccafaeSMatthew G Knepley 
2624c08575a3SMatthew G. Knepley /*@
2625c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2626c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2627c08575a3SMatthew G. Knepley 
2628c08575a3SMatthew G. Knepley   Not collective
2629c08575a3SMatthew G. Knepley 
2630c08575a3SMatthew G. Knepley   Input Parameters:
2631c08575a3SMatthew G. Knepley + dm           - The original mesh
2632c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2633c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2634c08575a3SMatthew G. Knepley . face         - The face vertices
2635c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2636c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2637c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2638c08575a3SMatthew G. Knepley 
2639c08575a3SMatthew G. Knepley   Output Parameter:
2640c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2641c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2642c08575a3SMatthew G. Knepley 
2643c08575a3SMatthew G. Knepley   Level: developer
2644c08575a3SMatthew G. Knepley 
2645c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2646c08575a3SMatthew G. Knepley @*/
2647e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2648e6ccafaeSMatthew G Knepley {
26490298fd71SBarry Smith   const PetscInt *cone = NULL;
2650e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2651e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2652e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2653e6ccafaeSMatthew G Knepley 
2654e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2655e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
2656e6ccafaeSMatthew G Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2657e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2658e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2659e6ccafaeSMatthew G Knepley 
2660e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2661e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2662e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2663e6ccafaeSMatthew G Knepley       }
2664e6ccafaeSMatthew G Knepley     }
2665e6ccafaeSMatthew G Knepley     if (found) {
2666e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2667e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2668e6ccafaeSMatthew G Knepley       ++v2;
2669e6ccafaeSMatthew G Knepley     } else {
2670e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2671e6ccafaeSMatthew G Knepley     }
2672e6ccafaeSMatthew G Knepley   }
2673e6ccafaeSMatthew G Knepley   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
2674e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2675e6ccafaeSMatthew G Knepley }
2676e6ccafaeSMatthew G Knepley 
2677e6ccafaeSMatthew G Knepley /*
2678cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2679e6ccafaeSMatthew G Knepley 
2680e6ccafaeSMatthew G Knepley   Not collective
2681e6ccafaeSMatthew G Knepley 
2682e6ccafaeSMatthew G Knepley   Input Parameters:
2683e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2684e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2685e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2686e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2687e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2688e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2689e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2690e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2691e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2692e6ccafaeSMatthew G Knepley 
2693e6ccafaeSMatthew G Knepley   Output Parameters:
2694e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2695e6ccafaeSMatthew G Knepley 
2696e6ccafaeSMatthew G Knepley   Level: developer
2697e6ccafaeSMatthew G Knepley */
2698cd0c2139SMatthew 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)
2699e6ccafaeSMatthew G Knepley {
270082f516ccSBarry Smith   MPI_Comm        comm;
2701e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2702e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2703e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2704e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2705e6ccafaeSMatthew G Knepley 
2706e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
270782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2708e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
2709e6ccafaeSMatthew G Knepley   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2710e6ccafaeSMatthew G Knepley #if 0
2711e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2712e6ccafaeSMatthew G Knepley   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2713e6ccafaeSMatthew G Knepley #else
2714e6ccafaeSMatthew G Knepley   {
2715e6ccafaeSMatthew G Knepley     PetscInt f;
2716e6ccafaeSMatthew G Knepley 
2717e6ccafaeSMatthew G Knepley     numFaces = 0;
271869291d52SBarry Smith     ierr     = DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2719e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2720e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2721e6ccafaeSMatthew G Knepley 
2722e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
2723e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
2724e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2725e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2726e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2727e6ccafaeSMatthew G Knepley         PetscInt       v;
2728e6ccafaeSMatthew G Knepley 
2729e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2730e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2731e6ccafaeSMatthew G Knepley         }
2732e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2733e6ccafaeSMatthew G Knepley       }
2734e6ccafaeSMatthew G Knepley       if (d == dof) {
2735e6ccafaeSMatthew G Knepley         numFaces               = 1;
2736e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2737e6ccafaeSMatthew G Knepley       }
2738e6ccafaeSMatthew G Knepley     }
2739e6ccafaeSMatthew G Knepley   }
2740e6ccafaeSMatthew G Knepley #endif
2741e6ccafaeSMatthew G Knepley   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2742e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2743e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2744766ab985SMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
2745e6ccafaeSMatthew G Knepley   } else {
2746e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2747e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2748e6ccafaeSMatthew G Knepley 
274969291d52SBarry Smith     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2750e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2751e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2752e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2753e6ccafaeSMatthew G Knepley     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
2754e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2755e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2756e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2757e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2758e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2759e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2760e6ccafaeSMatthew G Knepley           break;
2761e6ccafaeSMatthew G Knepley         }
2762e6ccafaeSMatthew G Knepley       }
2763e6ccafaeSMatthew G Knepley       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2764e6ccafaeSMatthew G Knepley     }
2765e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2766e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
276769291d52SBarry Smith     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2768e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2769e6ccafaeSMatthew G Knepley   }
2770ef07cca7SMatthew G. Knepley #if 0
2771e6ccafaeSMatthew G Knepley   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2772ef07cca7SMatthew G. Knepley #else
277369291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2774ef07cca7SMatthew G. Knepley #endif
2775e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2776e6ccafaeSMatthew G Knepley }
2777e6ccafaeSMatthew G Knepley 
277853156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2779e6ccafaeSMatthew G Knepley {
278082f516ccSBarry Smith   MPI_Comm        comm;
278153156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2782efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2783efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2784efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2785fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2786efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2787e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2788e6ccafaeSMatthew G Knepley 
2789e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
279082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2791efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2792d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2793efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2794efa14ee0SMatthew G Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
279553156dfcSMatthew G. Knepley   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2796efa14ee0SMatthew G Knepley   /* Setup chart */
2797efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2798efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2799efa14ee0SMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2800efa14ee0SMatthew G Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2801e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2802e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2803efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2804e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2805efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2806efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2807efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2808efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2809e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2810e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2811e6ccafaeSMatthew G Knepley   }
2812e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2813e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2814e6ccafaeSMatthew G Knepley   }
2815e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2816e6ccafaeSMatthew G Knepley   /* Create face cones */
2817efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
28180298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
281969291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2820e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2821e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2822efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
28230298fd71SBarry Smith     PetscInt      *closure = NULL;
2824efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2825e6ccafaeSMatthew G Knepley 
2826e6ccafaeSMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2827efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2828efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2829e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2830e6ccafaeSMatthew G Knepley 
2831efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2832efa14ee0SMatthew G Knepley         ++numCorners;
2833efa14ee0SMatthew G Knepley         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2834efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2835efa14ee0SMatthew G Knepley           closure[faceSize] = point;
283665560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2837e6ccafaeSMatthew G Knepley           ++faceSize;
2838e6ccafaeSMatthew G Knepley         }
2839e6ccafaeSMatthew G Knepley       }
2840e6ccafaeSMatthew G Knepley     }
2841efa14ee0SMatthew G Knepley     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2842efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2843cd0c2139SMatthew G Knepley       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2844e6ccafaeSMatthew G Knepley     }
284565560c7fSMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2846e6ccafaeSMatthew G Knepley   }
284769291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2848e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2849e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2850e6ccafaeSMatthew G Knepley   /* Build coordinates */
2851efa14ee0SMatthew G Knepley   {
2852efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2853efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2854efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2855285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
285624640c55SToby Isaac     const char  *name;
2857efa14ee0SMatthew G Knepley 
285869d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2859e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
286069d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2861285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2862285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2863285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2864efa14ee0SMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2865efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
2866efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
2867efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
2868efa14ee0SMatthew G Knepley       PetscInt       dof;
2869efa14ee0SMatthew G Knepley 
2870efa14ee0SMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2871efa14ee0SMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2872285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2873e6ccafaeSMatthew G Knepley     }
2874e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2875e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
28768b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
287724640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
287824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
2879e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
28802eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2881830e53efSMatthew G. Knepley     if (coordSize) {
2882e6ccafaeSMatthew G Knepley       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2883e6ccafaeSMatthew G Knepley       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2884efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
2885efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
2886efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
2887efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
2888e6ccafaeSMatthew G Knepley 
2889e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2890e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2891efa14ee0SMatthew G Knepley         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2892efa14ee0SMatthew G Knepley         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2893efa14ee0SMatthew 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);
2894e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2895e6ccafaeSMatthew G Knepley       }
2896e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2897e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
28983b399e24SMatthew G. Knepley     }
2899e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2900e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2901e6ccafaeSMatthew G Knepley   }
2902efa14ee0SMatthew G Knepley   /* Cleanup */
2903efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2904efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2905efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2906efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
2907e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2908e6ccafaeSMatthew G Knepley }
2909e6ccafaeSMatthew G Knepley 
29103982b651SMatthew G. Knepley PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
29113982b651SMatthew G. Knepley {
29123982b651SMatthew G. Knepley   PetscInt       subPoint;
29133982b651SMatthew G. Knepley   PetscErrorCode ierr;
29143982b651SMatthew G. Knepley 
29153982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
29163982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
29173982b651SMatthew G. Knepley }
29183982b651SMatthew G. Knepley 
2919158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
2920e6ccafaeSMatthew G Knepley {
292182f516ccSBarry Smith   MPI_Comm         comm;
292253156dfcSMatthew G. Knepley   DMLabel          subpointMap;
2923efa14ee0SMatthew G Knepley   IS              *subpointIS;
2924efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
29253982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
29260d366550SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, cMax, cEnd, dim, p, d, v;
29270d366550SMatthew G. Knepley   PetscMPIInt      rank;
2928e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
2929e6ccafaeSMatthew G Knepley 
2930e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
293182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
29320d366550SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2933efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2934d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2935efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2936bec263e5SMatthew G. Knepley   if (cellHeight) {
29373982b651SMatthew G. Knepley     if (isCohesive) {ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
2938158acfadSMatthew G. Knepley     else            {ierr = DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm);CHKERRQ(ierr);}
2939bec263e5SMatthew G. Knepley   } else {
2940bec263e5SMatthew G. Knepley     DMLabel         depth;
2941bec263e5SMatthew G. Knepley     IS              pointIS;
2942bec263e5SMatthew G. Knepley     const PetscInt *points;
2943bec263e5SMatthew G. Knepley     PetscInt        numPoints;
2944bec263e5SMatthew G. Knepley 
2945bec263e5SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
2946bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, value, &numPoints);CHKERRQ(ierr);
2947bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, value, &pointIS);CHKERRQ(ierr);
2948bec263e5SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2949bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2950bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
2951bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
2952bec263e5SMatthew G. Knepley 
2953bec263e5SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2954bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
2955bec263e5SMatthew G. Knepley         ierr = DMLabelGetValue(depth, closure[c], &pdim);CHKERRQ(ierr);
2956bec263e5SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, closure[c], pdim);CHKERRQ(ierr);
2957bec263e5SMatthew G. Knepley       }
2958bec263e5SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2959bec263e5SMatthew G. Knepley     }
2960bec263e5SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2961bec263e5SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2962bec263e5SMatthew G. Knepley   }
29630d366550SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
29640d366550SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
29650d366550SMatthew G. Knepley   cMax = (cMax < 0) ? cEnd : cMax;
2966efa14ee0SMatthew G Knepley   /* Setup chart */
2967c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2968dcca6d9dSJed Brown   ierr = PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);CHKERRQ(ierr);
2969e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2970e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
2971e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
2972e6ccafaeSMatthew G Knepley   }
2973e6ccafaeSMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
2974bec263e5SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, cellHeight);CHKERRQ(ierr);
2975e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2976e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
2977e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2978e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2979e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2980e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2981e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
29820ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
2983e6ccafaeSMatthew G Knepley   }
2984e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2985e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
2986e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
2987e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
2988e6ccafaeSMatthew G Knepley       const PetscInt *cone;
2989e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
2990e6ccafaeSMatthew G Knepley 
2991e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2992e6ccafaeSMatthew G Knepley       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
2993bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
2994e6ccafaeSMatthew G Knepley         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
2995e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2996e6ccafaeSMatthew G Knepley           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
2997e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
2998e6ccafaeSMatthew G Knepley         }
2999e6ccafaeSMatthew G Knepley         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
3000e6ccafaeSMatthew G Knepley       }
3001e6ccafaeSMatthew G Knepley     }
3002e6ccafaeSMatthew G Knepley   }
3003d67d17b1SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
3004e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3005e6ccafaeSMatthew G Knepley   /* Set cones */
30060298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
30073982b651SMatthew G. Knepley   ierr = PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);CHKERRQ(ierr);
3008e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3009e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3010e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3011e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
30120e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
30130d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3014e6ccafaeSMatthew G Knepley 
30150d366550SMatthew G. Knepley       if (d == dim-1) {
30160d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
30170d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
30180d366550SMatthew G. Knepley 
30190d366550SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
30200d366550SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
30210d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
30220d366550SMatthew G. Knepley           if ((support[s] < cMax) || (support[s] >= cEnd)) continue;
30230d366550SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc);CHKERRQ(ierr);
30240d366550SMatthew G. Knepley           if (subc >= 0) {
30250d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
30260d366550SMatthew G. Knepley 
30277f79407eSBarry Smith             ierr = DMPlexGetCone(dm, ccell, &cone);CHKERRQ(ierr);
30287f79407eSBarry Smith             ierr = DMPlexGetConeSize(dm, ccell, &coneSize);CHKERRQ(ierr);
30297f79407eSBarry Smith             ierr = DMPlexGetConeOrientation(dm, ccell, &ornt);CHKERRQ(ierr);
30300d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
30310d366550SMatthew G. Knepley               if (cone[c] == point) {
30320d366550SMatthew G. Knepley                 fornt = ornt[c];
30330d366550SMatthew G. Knepley                 break;
30340d366550SMatthew G. Knepley               }
30350d366550SMatthew G. Knepley             }
30360d366550SMatthew G. Knepley             break;
30370d366550SMatthew G. Knepley           }
30380d366550SMatthew G. Knepley         }
30390d366550SMatthew G. Knepley       }
3040e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3041e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
3042e6ccafaeSMatthew G Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
30430e49e2e2SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
3044e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3045e6ccafaeSMatthew G Knepley         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
304601a2673eSMatthew G. Knepley         if (subc >= 0) {
304701a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
30483982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
304901a2673eSMatthew G. Knepley           ++coneSizeNew;
305001a2673eSMatthew G. Knepley         }
3051e6ccafaeSMatthew G Knepley       }
3052e6ccafaeSMatthew G Knepley       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
30530d366550SMatthew G. Knepley       if (fornt < 0) {
30540d366550SMatthew G. Knepley         /* This should be replaced by a call to DMPlexReverseCell() */
30550d366550SMatthew G. Knepley #if 0
3056a129c400SMatthew G. Knepley         ierr = DMPlexReverseCell(subdm, subpoint);CHKERRQ(ierr);
3057a129c400SMatthew G. Knepley #else
3058c0b40891SMatthew G. Knepley         for (c = 0; c < coneSizeNew/2 + coneSizeNew%2; ++c) {
3059a129c400SMatthew G. Knepley           PetscInt faceSize, tmp;
30600d366550SMatthew G. Knepley 
30610d366550SMatthew G. Knepley           tmp        = coneNew[c];
30620d366550SMatthew G. Knepley           coneNew[c] = coneNew[coneSizeNew-1-c];
30630d366550SMatthew G. Knepley           coneNew[coneSizeNew-1-c] = tmp;
3064a129c400SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, cone[c], &faceSize);CHKERRQ(ierr);
3065a129c400SMatthew G. Knepley           tmp        = orntNew[c] >= 0 ? -(faceSize-orntNew[c]) : faceSize+orntNew[c];
3066a129c400SMatthew G. Knepley           orntNew[c] = orntNew[coneSizeNew-1-c] >= 0 ? -(faceSize-orntNew[coneSizeNew-1-c]) : faceSize+orntNew[coneSizeNew-1-c];
30670d366550SMatthew G. Knepley           orntNew[coneSizeNew-1-c] = tmp;
30680d366550SMatthew G. Knepley         }
30690d366550SMatthew G. Knepley       }
3070e6ccafaeSMatthew G Knepley       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
30713982b651SMatthew G. Knepley       ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr);
3072a129c400SMatthew G. Knepley #endif
3073e6ccafaeSMatthew G Knepley     }
3074e6ccafaeSMatthew G Knepley   }
30753982b651SMatthew G. Knepley   ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr);
3076e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3077e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3078e6ccafaeSMatthew G Knepley   /* Build coordinates */
3079e6ccafaeSMatthew G Knepley   {
3080e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3081e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3082e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3083c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
308424640c55SToby Isaac     const char  *name;
3085e6ccafaeSMatthew G Knepley 
3086c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
308769d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3088e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
308969d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3090285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3091285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3092285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3093e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
3094e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3095e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3096e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3097e6ccafaeSMatthew G Knepley       PetscInt       dof;
3098e6ccafaeSMatthew G Knepley 
3099e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3100e6ccafaeSMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3101285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3102e6ccafaeSMatthew G Knepley     }
3103e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3104e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
31058b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
310624640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
310724640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3108e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3109c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
31102eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3111e6ccafaeSMatthew G Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3112e6ccafaeSMatthew G Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3113e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3114e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3115e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3116e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3117e6ccafaeSMatthew G Knepley 
3118e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3119e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3120e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3121e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3122e6ccafaeSMatthew 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);
3123efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3124e6ccafaeSMatthew G Knepley     }
3125e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3126e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3127e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3128e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3129e6ccafaeSMatthew G Knepley   }
31303982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
31313982b651SMatthew G. Knepley   {
31323982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
31333982b651SMatthew G. Knepley     IS                 subpIS;
31343982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
31353982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
31363982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
31373982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
31383982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
31393982b651SMatthew G. Knepley     PetscMPIInt        rank;
31403982b651SMatthew G. Knepley 
31413982b651SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
31423982b651SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
31433982b651SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
31443982b651SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
31453982b651SMatthew G. Knepley     ierr = DMPlexGetChart(subdm, NULL, &numSubroots);CHKERRQ(ierr);
31463982b651SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpIS);CHKERRQ(ierr);
31473982b651SMatthew G. Knepley     if (subpIS) {
31483982b651SMatthew G. Knepley       ierr = ISGetIndices(subpIS, &subpoints);CHKERRQ(ierr);
31493982b651SMatthew G. Knepley       ierr = ISGetLocalSize(subpIS, &numSubpoints);CHKERRQ(ierr);
31503982b651SMatthew G. Knepley     }
31513982b651SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
31523982b651SMatthew G. Knepley     if (numRoots >= 0) {
31533982b651SMatthew G. Knepley       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
31543982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
31553982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
31563982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
31573982b651SMatthew G. Knepley       }
31583982b651SMatthew G. Knepley       /* Set subleaves */
31593982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
31603982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
31613982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
31623982b651SMatthew G. Knepley 
31633982b651SMatthew G. Knepley         if (subpoint < 0) continue;
31643982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
31653982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
31663982b651SMatthew G. Knepley         ++numSubleaves;
31673982b651SMatthew G. Knepley       }
31683982b651SMatthew G. Knepley       /* Must put in owned subpoints */
31693982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
31703982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
31713982b651SMatthew G. Knepley 
31723982b651SMatthew G. Knepley         if (subpoint < 0) {
31733982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
31743982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
31753982b651SMatthew G. Knepley         } else {
31763982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
31773982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
31783982b651SMatthew G. Knepley         }
31793982b651SMatthew G. Knepley       }
31803982b651SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
31813982b651SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
31823982b651SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
31833982b651SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
31843982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &slocalPoints);CHKERRQ(ierr);
31853982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &sremotePoints);CHKERRQ(ierr);
31863982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
31873982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
31883982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
31893982b651SMatthew G. Knepley 
31903982b651SMatthew G. Knepley         if (subpoint < 0) continue;
31913982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
31923982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
31933982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
31943982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
31953982b651SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
31963982b651SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
31973982b651SMatthew G. Knepley         ++sl;
31983982b651SMatthew G. Knepley       }
31993982b651SMatthew G. Knepley       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
32003982b651SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
32013982b651SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
32023982b651SMatthew G. Knepley     }
32033982b651SMatthew G. Knepley     if (subpIS) {
32043982b651SMatthew G. Knepley       ierr = ISRestoreIndices(subpIS, &subpoints);CHKERRQ(ierr);
32053982b651SMatthew G. Knepley       ierr = ISDestroy(&subpIS);CHKERRQ(ierr);
32063982b651SMatthew G. Knepley     }
32073982b651SMatthew G. Knepley   }
3208efa14ee0SMatthew G Knepley   /* Cleanup */
3209e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32100ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3211e6ccafaeSMatthew G Knepley     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
3212e6ccafaeSMatthew G Knepley   }
3213efa14ee0SMatthew G Knepley   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
3214e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3215e6ccafaeSMatthew G Knepley }
3216e6ccafaeSMatthew G Knepley 
3217158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
32183982b651SMatthew G. Knepley {
32193982b651SMatthew G. Knepley   PetscErrorCode ierr;
32203982b651SMatthew G. Knepley 
32213982b651SMatthew G. Knepley   PetscFunctionBegin;
3222158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm);CHKERRQ(ierr);
32233982b651SMatthew G. Knepley   PetscFunctionReturn(0);
32243982b651SMatthew G. Knepley }
32253982b651SMatthew G. Knepley 
3226d0fa310fSMatthew G. Knepley /*@
3227e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3228e6ccafaeSMatthew G Knepley 
3229e6ccafaeSMatthew G Knepley   Input Parameters:
3230e6ccafaeSMatthew G Knepley + dm           - The original mesh
3231158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3232158acfadSMatthew G. Knepley . value        - The label value to use
3233158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3234e6ccafaeSMatthew G Knepley 
3235e6ccafaeSMatthew G Knepley   Output Parameter:
3236e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3237e6ccafaeSMatthew G Knepley 
3238e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3239e6ccafaeSMatthew G Knepley 
3240e6ccafaeSMatthew G Knepley   Level: developer
3241e6ccafaeSMatthew G Knepley 
3242c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3243830e53efSMatthew G. Knepley @*/
3244158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3245e6ccafaeSMatthew G Knepley {
3246dd2763adSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3247e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3248e6ccafaeSMatthew G Knepley 
3249e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3250e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3251766ab985SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3252c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3253e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
325482f516ccSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3255e6ccafaeSMatthew G Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3256c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3257dd2763adSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3258dd2763adSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3259e6ccafaeSMatthew G Knepley   if (depth == dim) {
3260158acfadSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm);CHKERRQ(ierr);
3261e6ccafaeSMatthew G Knepley   } else {
3262830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3263e6ccafaeSMatthew G Knepley   }
3264e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3265e6ccafaeSMatthew G Knepley }
3266e6ccafaeSMatthew G Knepley 
326727c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3268766ab985SMatthew G. Knepley {
3269766ab985SMatthew G. Knepley   MPI_Comm        comm;
3270766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3271766ab985SMatthew G. Knepley   IS              subvertexIS;
3272766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3273fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3274766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
327587feddfdSMatthew G. Knepley   PetscInt        cMax, c, f;
3276766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
3277766ab985SMatthew G. Knepley 
3278766ab985SMatthew G. Knepley   PetscFunctionBegin;
3279766ab985SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
3280766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3281d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3282766ab985SMatthew G. Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3283766ab985SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
328427c04023SMatthew G. Knepley   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
3285766ab985SMatthew G. Knepley   /* Setup chart */
3286766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
3287766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
3288766ab985SMatthew G. Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
3289766ab985SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
3290766ab985SMatthew G. Knepley   /* Set cone sizes */
3291766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3292766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3293766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3294766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
3295766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3296766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3297766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
3298766ab985SMatthew G. Knepley   }
3299766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3300766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
3301766ab985SMatthew G. Knepley   }
3302766ab985SMatthew G. Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3303766ab985SMatthew G. Knepley   /* Create face cones */
3304766ab985SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
330587feddfdSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
330669291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3307766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3308766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3309766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
331087feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
331187feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3312766ab985SMatthew G. Knepley 
331387feddfdSMatthew G. Knepley     if (cell < cMax) continue;
331487feddfdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
331587feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
331687feddfdSMatthew G. Knepley       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
331787feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
331887feddfdSMatthew G. Knepley     }
331987feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
332087feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
332187feddfdSMatthew G. Knepley     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
332227234c99SMatthew G. Knepley     /* Not true in parallel
332327234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
332487feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
332587feddfdSMatthew G. Knepley       PetscInt negsubcell;
3326766ab985SMatthew G. Knepley 
332787feddfdSMatthew G. Knepley       if (cells[p] >= cMax) continue;
332887feddfdSMatthew G. Knepley       /* I know this is a crap search */
332987feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
333087feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3331766ab985SMatthew G. Knepley       }
333287feddfdSMatthew G. Knepley       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
333387feddfdSMatthew G. Knepley       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
3334766ab985SMatthew G. Knepley     }
333587feddfdSMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
333687feddfdSMatthew G. Knepley     ++newFacePoint;
3337766ab985SMatthew G. Knepley   }
333869291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3339766ab985SMatthew G. Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3340766ab985SMatthew G. Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3341766ab985SMatthew G. Knepley   /* Build coordinates */
3342766ab985SMatthew G. Knepley   {
3343766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3344766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3345766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3346c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
334724640c55SToby Isaac     const char  *name;
3348766ab985SMatthew G. Knepley 
3349c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
335069d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3351766ab985SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
335269d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3353285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3354285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3355285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3356766ab985SMatthew G. Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3357766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3358766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3359766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3360766ab985SMatthew G. Knepley       PetscInt       dof;
3361766ab985SMatthew G. Knepley 
3362766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3363766ab985SMatthew G. Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3364285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3365766ab985SMatthew G. Knepley     }
3366766ab985SMatthew G. Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3367766ab985SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
33688b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
336924640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
337024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3371766ab985SMatthew G. Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3372c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
33732eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3374766ab985SMatthew G. Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3375766ab985SMatthew G. Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3376766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3377766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3378766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3379766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3380766ab985SMatthew G. Knepley 
3381766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3382766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3383766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3384766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3385766ab985SMatthew 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);
3386766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3387766ab985SMatthew G. Knepley     }
3388766ab985SMatthew G. Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3389766ab985SMatthew G. Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3390766ab985SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3391766ab985SMatthew G. Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3392766ab985SMatthew G. Knepley   }
3393aca35d17SMatthew G. Knepley   /* Build SF */
3394aca35d17SMatthew G. Knepley   CHKMEMQ;
3395aca35d17SMatthew G. Knepley   {
3396aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3397aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3398bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3399aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3400bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
340149c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3402bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3403aca35d17SMatthew G. Knepley 
3404bdcf2095SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3405aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
3406aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
3407aca35d17SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3408aca35d17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
3409aca35d17SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
3410aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3411aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3412dcca6d9dSJed Brown       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
3413bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3414bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3415bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3416bdcf2095SMatthew G. Knepley       }
34179e0823b2SMatthew G. Knepley       /* Set subleaves */
3418aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3419aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3420bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3421aca35d17SMatthew G. Knepley 
3422aca35d17SMatthew G. Knepley         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3423bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3424bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3425bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3426bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3427aca35d17SMatthew G. Knepley       }
34289e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
34299e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
34309e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
34319e0823b2SMatthew G. Knepley 
34329e0823b2SMatthew G. Knepley         if (subPoint < 0) {
34339e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
34349e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
34359e0823b2SMatthew G. Knepley         } else {
34369e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
34379e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
34389e0823b2SMatthew G. Knepley         }
3439bdcf2095SMatthew G. Knepley       }
3440bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3441bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3442bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3443bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3444785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves,    &slocalPoints);CHKERRQ(ierr);
3445785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves, &sremotePoints);CHKERRQ(ierr);
344649c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3447bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3448bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3449aca35d17SMatthew G. Knepley 
3450aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
345149c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3452aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3453bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3454bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
3455bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3456bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3457aca35d17SMatthew G. Knepley         ++sl;
3458aca35d17SMatthew G. Knepley       }
3459bdcf2095SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
346049c26ae4SMatthew G. Knepley       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
346149c26ae4SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3462aca35d17SMatthew G. Knepley     }
3463aca35d17SMatthew G. Knepley   }
3464aca35d17SMatthew G. Knepley   CHKMEMQ;
3465766ab985SMatthew G. Knepley   /* Cleanup */
3466766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3467766ab985SMatthew G. Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3468766ab985SMatthew G. Knepley   ierr = PetscFree(subCells);CHKERRQ(ierr);
3469766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3470766ab985SMatthew G. Knepley }
3471766ab985SMatthew G. Knepley 
34723982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3473766ab985SMatthew G. Knepley {
34743982b651SMatthew G. Knepley   DMLabel        label = NULL;
3475766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3476766ab985SMatthew G. Knepley 
3477766ab985SMatthew G. Knepley   PetscFunctionBegin;
3478c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
3479158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm);CHKERRQ(ierr);
3480766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3481766ab985SMatthew G. Knepley }
3482766ab985SMatthew G. Knepley 
3483c08575a3SMatthew G. Knepley /*@C
348427c04023SMatthew 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.
3485766ab985SMatthew G. Knepley 
3486766ab985SMatthew G. Knepley   Input Parameters:
3487766ab985SMatthew G. Knepley + dm          - The original mesh
348827c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
34897afc1a8bSJed Brown . label       - A label name, or NULL
349027c04023SMatthew G. Knepley - value  - A label value
3491766ab985SMatthew G. Knepley 
3492766ab985SMatthew G. Knepley   Output Parameter:
3493766ab985SMatthew G. Knepley . subdm - The surface mesh
3494766ab985SMatthew G. Knepley 
3495766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3496766ab985SMatthew G. Knepley 
3497766ab985SMatthew G. Knepley   Level: developer
3498766ab985SMatthew G. Knepley 
3499766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3500c08575a3SMatthew G. Knepley @*/
350127c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3502766ab985SMatthew G. Knepley {
3503c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3504766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3505766ab985SMatthew G. Knepley 
3506766ab985SMatthew G. Knepley   PetscFunctionBegin;
3507766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
350827c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3509c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3510766ab985SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3511766ab985SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3512766ab985SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3513c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3514c0e8cf5fSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3515c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3516766ab985SMatthew G. Knepley   if (depth == dim) {
3517b3154360SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr);
3518766ab985SMatthew G. Knepley   } else {
351927c04023SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
3520e6ccafaeSMatthew G Knepley   }
3521e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3522e6ccafaeSMatthew G Knepley }
3523e6ccafaeSMatthew G Knepley 
3524bec263e5SMatthew G. Knepley /*@
3525bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3526bec263e5SMatthew G. Knepley 
3527bec263e5SMatthew G. Knepley   Input Parameters:
3528bec263e5SMatthew G. Knepley + dm        - The original mesh
3529bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3530bec263e5SMatthew G. Knepley - value     - The label value to use
3531bec263e5SMatthew G. Knepley 
3532bec263e5SMatthew G. Knepley   Output Parameter:
3533bec263e5SMatthew G. Knepley . subdm - The new mesh
3534bec263e5SMatthew G. Knepley 
3535bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3536bec263e5SMatthew G. Knepley 
3537bec263e5SMatthew G. Knepley   Level: developer
3538bec263e5SMatthew G. Knepley 
3539c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3540bec263e5SMatthew G. Knepley @*/
3541bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3542bec263e5SMatthew G. Knepley {
3543bec263e5SMatthew G. Knepley   PetscInt       dim;
3544bec263e5SMatthew G. Knepley   PetscErrorCode ierr;
3545bec263e5SMatthew G. Knepley 
3546bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3547bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3548bec263e5SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3549bec263e5SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3550bec263e5SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), subdm);CHKERRQ(ierr);
3551bec263e5SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3552bec263e5SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim);CHKERRQ(ierr);
3553bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3554158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm);CHKERRQ(ierr);
3555bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3556bec263e5SMatthew G. Knepley }
3557bec263e5SMatthew G. Knepley 
355864beef6dSMatthew G. Knepley /*@
355964beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
356064beef6dSMatthew G. Knepley 
356164beef6dSMatthew G. Knepley   Input Parameter:
356264beef6dSMatthew G. Knepley . dm - The submesh DM
356364beef6dSMatthew G. Knepley 
356464beef6dSMatthew G. Knepley   Output Parameter:
356564beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
356664beef6dSMatthew G. Knepley 
356764beef6dSMatthew G. Knepley   Level: developer
356864beef6dSMatthew G. Knepley 
356964beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
357064beef6dSMatthew G. Knepley @*/
3571e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3572e6ccafaeSMatthew G Knepley {
3573e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3574e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3575e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
357665663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3577e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3578e6ccafaeSMatthew G Knepley }
3579e6ccafaeSMatthew G Knepley 
3580c08575a3SMatthew G. Knepley /*@
3581c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3582c08575a3SMatthew G. Knepley 
3583c08575a3SMatthew G. Knepley   Input Parameters:
3584c08575a3SMatthew G. Knepley + dm - The submesh DM
3585c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3586c08575a3SMatthew G. Knepley 
3587c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3588c08575a3SMatthew G. Knepley 
3589c08575a3SMatthew G. Knepley   Level: developer
3590c08575a3SMatthew G. Knepley 
3591c08575a3SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3592c08575a3SMatthew G. Knepley @*/
3593e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3594e6ccafaeSMatthew G Knepley {
3595e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3596285d324eSMatthew G. Knepley   DMLabel        tmp;
3597e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3598e6ccafaeSMatthew G Knepley 
3599e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3600e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3601285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3602e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3603d67d17b1SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) mesh->subpointMap);CHKERRQ(ierr);
3604285d324eSMatthew G. Knepley   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
3605e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3606e6ccafaeSMatthew G Knepley }
3607e6ccafaeSMatthew G Knepley 
360864beef6dSMatthew G. Knepley /*@
3609e6ccafaeSMatthew G Knepley   DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data
3610e6ccafaeSMatthew G Knepley 
3611e6ccafaeSMatthew G Knepley   Input Parameter:
3612e6ccafaeSMatthew G Knepley . dm - The submesh DM
3613e6ccafaeSMatthew G Knepley 
3614e6ccafaeSMatthew G Knepley   Output Parameter:
36150298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3616e6ccafaeSMatthew G Knepley 
36173982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
361864beef6dSMatthew G. Knepley 
361964beef6dSMatthew G. Knepley   Level: developer
362064beef6dSMatthew G. Knepley 
362164beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
362264beef6dSMatthew G. Knepley @*/
3623e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3624e6ccafaeSMatthew G Knepley {
362582f516ccSBarry Smith   MPI_Comm        comm;
3626e6ccafaeSMatthew G Knepley   DMLabel         subpointMap;
3627e6ccafaeSMatthew G Knepley   IS              is;
3628e6ccafaeSMatthew G Knepley   const PetscInt *opoints;
3629e6ccafaeSMatthew G Knepley   PetscInt       *points, *depths;
3630e6ccafaeSMatthew G Knepley   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3631e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
3632e6ccafaeSMatthew G Knepley 
3633e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3634e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3635e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
363682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
36370298fd71SBarry Smith   *subpointIS = NULL;
3638e6ccafaeSMatthew G Knepley   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
3639e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3640fed694aaSMatthew G. Knepley   if (subpointMap && depth >= 0) {
3641e6ccafaeSMatthew G Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3642e6ccafaeSMatthew G Knepley     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
364369291d52SBarry Smith     ierr = DMGetWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3644e6ccafaeSMatthew G Knepley     depths[0] = depth;
3645e6ccafaeSMatthew G Knepley     depths[1] = 0;
3646e6ccafaeSMatthew G Knepley     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3647785e854fSJed Brown     ierr = PetscMalloc1(pEnd, &points);CHKERRQ(ierr);
3648e6ccafaeSMatthew G Knepley     for(d = 0, off = 0; d <= depth; ++d) {
3649e6ccafaeSMatthew G Knepley       const PetscInt dep = depths[d];
3650e6ccafaeSMatthew G Knepley 
3651e6ccafaeSMatthew G Knepley       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
3652e6ccafaeSMatthew G Knepley       ierr = DMLabelGetStratumSize(subpointMap, dep, &n);CHKERRQ(ierr);
36531e21b6fdSMatthew G Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3654e6ccafaeSMatthew 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);
3655e6ccafaeSMatthew G Knepley       } else {
36561e21b6fdSMatthew G Knepley         if (!n) {
36571e21b6fdSMatthew G Knepley           if (d == 0) {
36581e21b6fdSMatthew G Knepley             /* Missing cells */
36591e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
36601e21b6fdSMatthew G Knepley           } else {
36611e21b6fdSMatthew G Knepley             /* Missing faces */
36621e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
36631e21b6fdSMatthew G Knepley           }
36641e21b6fdSMatthew G Knepley         }
3665e6ccafaeSMatthew G Knepley       }
3666e6ccafaeSMatthew G Knepley       if (n) {
3667e6ccafaeSMatthew G Knepley         ierr = DMLabelGetStratumIS(subpointMap, dep, &is);CHKERRQ(ierr);
3668e6ccafaeSMatthew G Knepley         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
3669e6ccafaeSMatthew G Knepley         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3670e6ccafaeSMatthew G Knepley         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
3671e6ccafaeSMatthew G Knepley         ierr = ISDestroy(&is);CHKERRQ(ierr);
3672e6ccafaeSMatthew G Knepley       }
3673e6ccafaeSMatthew G Knepley     }
367469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3675e6ccafaeSMatthew G Knepley     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3676fed694aaSMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
3677e6ccafaeSMatthew G Knepley   }
3678e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3679e6ccafaeSMatthew G Knepley }
3680559a1558SMatthew G. Knepley 
3681559a1558SMatthew G. Knepley /*@
3682559a1558SMatthew G. Knepley   DMPlexGetSubpoint - Return the subpoint corresponding to a point in the original mesh. If the DM
3683559a1558SMatthew G. Knepley                       is not a submesh, just return the input point.
3684559a1558SMatthew G. Knepley 
3685559a1558SMatthew G. Knepley   Note collective
3686559a1558SMatthew G. Knepley 
3687559a1558SMatthew G. Knepley   Input Parameters:
3688559a1558SMatthew G. Knepley + dm - The submesh DM
3689559a1558SMatthew G. Knepley - p  - The point in the original, from which the submesh was created
3690559a1558SMatthew G. Knepley 
3691559a1558SMatthew G. Knepley   Output Parameter:
3692559a1558SMatthew G. Knepley . subp - The point in the submesh
3693559a1558SMatthew G. Knepley 
3694559a1558SMatthew G. Knepley   Level: developer
3695559a1558SMatthew G. Knepley 
3696559a1558SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap(), DMPlexCreateSubpointIS()
3697559a1558SMatthew G. Knepley @*/
3698559a1558SMatthew G. Knepley PetscErrorCode DMPlexGetSubpoint(DM dm, PetscInt p, PetscInt *subp)
3699559a1558SMatthew G. Knepley {
3700559a1558SMatthew G. Knepley   DMLabel        spmap;
3701559a1558SMatthew G. Knepley   PetscErrorCode ierr;
3702559a1558SMatthew G. Knepley 
370344171101SMatthew G. Knepley   PetscFunctionBegin;
3704559a1558SMatthew G. Knepley   *subp = p;
3705559a1558SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
3706559a1558SMatthew G. Knepley   if (spmap) {
3707559a1558SMatthew G. Knepley     IS              subpointIS;
3708559a1558SMatthew G. Knepley     const PetscInt *subpoints;
3709559a1558SMatthew G. Knepley     PetscInt        numSubpoints;
3710559a1558SMatthew G. Knepley 
3711559a1558SMatthew G. Knepley     /* TODO Cache the IS, making it look like an index */
3712559a1558SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(dm, &subpointIS);CHKERRQ(ierr);
3713559a1558SMatthew G. Knepley     ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
3714559a1558SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3715559a1558SMatthew G. Knepley     ierr = PetscFindInt(p, numSubpoints, subpoints, subp);CHKERRQ(ierr);
3716559a1558SMatthew G. Knepley     if (*subp < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", p);
3717559a1558SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3718559a1558SMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
3719559a1558SMatthew G. Knepley   }
3720559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3721559a1558SMatthew G. Knepley }
372244171101SMatthew G. Knepley 
372344171101SMatthew G. Knepley /*@
372444171101SMatthew G. Knepley   DMPlexGetAuxiliaryPoint - For a given point in the DM, return the matching point in the auxiliary DM.
372544171101SMatthew G. Knepley 
372644171101SMatthew G. Knepley   Note collective
372744171101SMatthew G. Knepley 
372844171101SMatthew G. Knepley   Input Parameters:
372944171101SMatthew G. Knepley + dm    - The DM
373044171101SMatthew G. Knepley . dmAux - The related auxiliary DM
373144171101SMatthew G. Knepley - p     - The point in the original DM
373244171101SMatthew G. Knepley 
373344171101SMatthew G. Knepley   Output Parameter:
373444171101SMatthew G. Knepley . subp - The point in the auxiliary DM
373544171101SMatthew G. Knepley 
373644171101SMatthew G. Knepley   Notes: If the DM is a submesh, we assume the dmAux is as well and just return the point. If only dmAux is a submesh,
373744171101SMatthew G. Knepley   then we map the point back to the original space.
373844171101SMatthew G. Knepley 
373944171101SMatthew G. Knepley   Level: developer
374044171101SMatthew G. Knepley 
374144171101SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap(), DMPlexCreateSubpointIS()
374244171101SMatthew G. Knepley @*/
374344171101SMatthew G. Knepley PetscErrorCode DMPlexGetAuxiliaryPoint(DM dm, DM dmAux, PetscInt p, PetscInt *subp)
374444171101SMatthew G. Knepley {
374544171101SMatthew G. Knepley   DMLabel        spmap;
374644171101SMatthew G. Knepley   PetscErrorCode ierr;
374744171101SMatthew G. Knepley 
374844171101SMatthew G. Knepley   PetscFunctionBegin;
374944171101SMatthew G. Knepley   *subp = p;
375044171101SMatthew G. Knepley   /* If dm is a submesh, do not get subpoint */
375144171101SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
375244171101SMatthew G. Knepley   if (dmAux && !spmap) {
375344171101SMatthew G. Knepley     PetscInt h;
375444171101SMatthew G. Knepley 
375544171101SMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dmAux, &h);CHKERRQ(ierr);
375644171101SMatthew G. Knepley     ierr = DMPlexGetSubpointMap(dmAux, &spmap);CHKERRQ(ierr);
375744171101SMatthew G. Knepley     if (spmap && !h) {ierr = DMLabelGetValue(spmap, p, subp);CHKERRQ(ierr);}
375844171101SMatthew G. Knepley     else             {ierr = DMPlexGetSubpoint(dmAux, p, subp);CHKERRQ(ierr);}
375944171101SMatthew G. Knepley   }
376044171101SMatthew G. Knepley   PetscFunctionReturn(0);
376144171101SMatthew G. Knepley }
3762