xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 559a1558ba4e4cd6f39f4336c756d8eb59ac12ff)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>    /*I      "petscdmplex.h"    I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>   /*I      "petscdmlabel.h"   I*/
30c312b8eSJed Brown #include <petscsf.h>
4e6ccafaeSMatthew G Knepley 
5e752be1aSMatthew G. Knepley static PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label)
630560a7bSMatthew G. Knepley {
730560a7bSMatthew G. Knepley   PetscInt       fStart, fEnd, f;
830560a7bSMatthew G. Knepley   PetscErrorCode ierr;
930560a7bSMatthew G. Knepley 
1030560a7bSMatthew G. Knepley   PetscFunctionBegin;
1130560a7bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1230560a7bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr);
1330560a7bSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1430560a7bSMatthew G. Knepley     PetscInt supportSize;
1530560a7bSMatthew G. Knepley 
1630560a7bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
17e752be1aSMatthew G. Knepley     if (supportSize == 1) {
18e752be1aSMatthew G. Knepley       if (val < 0) {
19e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
20e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
21e752be1aSMatthew G. Knepley 
22e752be1aSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
23e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize*2; cl += 2) {
24e752be1aSMatthew G. Knepley           ierr = DMLabelGetValue(label, closure[cl], &cval);CHKERRQ(ierr);
25e752be1aSMatthew G. Knepley           if (cval < 0) continue;
26e752be1aSMatthew G. Knepley           ierr = DMLabelSetValue(label, f, cval);CHKERRQ(ierr);
27e752be1aSMatthew G. Knepley           break;
28e752be1aSMatthew G. Knepley         }
29e752be1aSMatthew G. Knepley         if (cl == clSize*2) {ierr = DMLabelSetValue(label, f, 1);CHKERRQ(ierr);}
30e752be1aSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
31e752be1aSMatthew G. Knepley       } else {
32e752be1aSMatthew G. Knepley         ierr = DMLabelSetValue(label, f, val);CHKERRQ(ierr);
33e752be1aSMatthew G. Knepley       }
34e752be1aSMatthew G. Knepley     }
3530560a7bSMatthew G. Knepley   }
3630560a7bSMatthew G. Knepley   PetscFunctionReturn(0);
3730560a7bSMatthew G. Knepley }
3830560a7bSMatthew G. Knepley 
39cd0c2139SMatthew G Knepley /*@
40cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
41cd0c2139SMatthew G Knepley 
42cd0c2139SMatthew G Knepley   Not Collective
43cd0c2139SMatthew G Knepley 
44cd0c2139SMatthew G Knepley   Input Parameter:
45e752be1aSMatthew G. Knepley + dm - The original DM
46e752be1aSMatthew G. Knepley - val - The marker value, or PETSC_DETERMINE to use some value in the closure (or 1 if none are found)
47cd0c2139SMatthew G Knepley 
48cd0c2139SMatthew G Knepley   Output Parameter:
49e752be1aSMatthew G. Knepley . label - The DMLabel marking boundary faces with the given value
50cd0c2139SMatthew G Knepley 
51cd0c2139SMatthew G Knepley   Level: developer
52cd0c2139SMatthew G Knepley 
53c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMCreateLabel()
5409f723d9SJed Brown @*/
55e752be1aSMatthew G. Knepley PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
56cd0c2139SMatthew G Knepley {
57cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
58cd0c2139SMatthew G Knepley 
59cd0c2139SMatthew G Knepley   PetscFunctionBegin;
60e752be1aSMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces_Internal(dm, val, 0, label);CHKERRQ(ierr);
61cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
62cd0c2139SMatthew G Knepley }
63cd0c2139SMatthew G Knepley 
64c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
65b0bf5782SToby Isaac {
66b0bf5782SToby Isaac   IS              valueIS;
67ac51f24eSSander Arens   PetscSF         sfPoint;
68b0bf5782SToby Isaac   const PetscInt *values;
69ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
70b0bf5782SToby Isaac   PetscErrorCode  ierr;
71b0bf5782SToby Isaac 
72b0bf5782SToby Isaac   PetscFunctionBegin;
73b0bf5782SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
74b0bf5782SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
75b0bf5782SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
76b0bf5782SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
77b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
78b0bf5782SToby Isaac     IS              pointIS;
79b0bf5782SToby Isaac     const PetscInt *points;
80b0bf5782SToby Isaac     PetscInt        numPoints, p;
81b0bf5782SToby Isaac 
82b0bf5782SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
83b0bf5782SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
84b0bf5782SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
85b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
86b0bf5782SToby Isaac       PetscInt  q = points[p];
87b0bf5782SToby Isaac       PetscInt *closure = NULL;
88b0bf5782SToby Isaac       PetscInt  closureSize, c;
89b0bf5782SToby Isaac 
90b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
91b0bf5782SToby Isaac         continue;
92b0bf5782SToby Isaac       }
93b0bf5782SToby Isaac       ierr = DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
94b0bf5782SToby Isaac       for (c = 0; c < closureSize*2; c += 2) {
95b0bf5782SToby Isaac         ierr = DMLabelSetValue(label, closure[c], values[v]);CHKERRQ(ierr);
96b0bf5782SToby Isaac       }
97b0bf5782SToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
98b0bf5782SToby Isaac     }
99b0bf5782SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
100b0bf5782SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
101b0bf5782SToby Isaac   }
102b0bf5782SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
103b0bf5782SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
104ac51f24eSSander Arens   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
105ac51f24eSSander Arens   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
106ac51f24eSSander Arens   if (nroots >= 0) {
10726279d81SSanderA     DMLabel         lblRoots, lblLeaves;
10826279d81SSanderA     IS              valueIS, pointIS;
10926279d81SSanderA     const PetscInt *values;
11026279d81SSanderA     PetscInt        numValues, v;
11126279d81SSanderA     PetscErrorCode  ierr;
11226279d81SSanderA 
11326279d81SSanderA     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
11426279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
11526279d81SSanderA     ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr);
11626279d81SSanderA     ierr = DMLabelGetValueIS(lblLeaves, &valueIS);CHKERRQ(ierr);
11726279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
11826279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
11926279d81SSanderA     for (v = 0; v < numValues; ++v) {
12026279d81SSanderA       const PetscInt value = values[v];
12126279d81SSanderA 
12226279d81SSanderA       ierr = DMLabelGetStratumIS(lblLeaves, value, &pointIS);CHKERRQ(ierr);
12326279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
12426279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
12526279d81SSanderA     }
12626279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
12726279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
12826279d81SSanderA     ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr);
12926279d81SSanderA     /* Push point contributions from roots into remote leaves */
13026279d81SSanderA     ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr);
13126279d81SSanderA     ierr = DMLabelGetValueIS(lblRoots, &valueIS);CHKERRQ(ierr);
13226279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
13326279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
13426279d81SSanderA     for (v = 0; v < numValues; ++v) {
13526279d81SSanderA       const PetscInt value = values[v];
13626279d81SSanderA 
13726279d81SSanderA       ierr = DMLabelGetStratumIS(lblRoots, value, &pointIS);CHKERRQ(ierr);
13826279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
13926279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
14026279d81SSanderA     }
14126279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
14226279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
14326279d81SSanderA     ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr);
14426279d81SSanderA   }
145b0bf5782SToby Isaac   PetscFunctionReturn(0);
146b0bf5782SToby Isaac }
147b0bf5782SToby Isaac 
1482be2b188SMatthew G Knepley /*@
1492be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
1502be2b188SMatthew G Knepley 
1512be2b188SMatthew G Knepley   Input Parameters:
1522be2b188SMatthew G Knepley + dm - The DM
1532be2b188SMatthew G Knepley - label - A DMLabel marking the surface points
1542be2b188SMatthew G Knepley 
1552be2b188SMatthew G Knepley   Output Parameter:
1562be2b188SMatthew G Knepley . label - A DMLabel marking all surface points in the transitive closure
1572be2b188SMatthew G Knepley 
1582be2b188SMatthew G Knepley   Level: developer
1592be2b188SMatthew G Knepley 
1602be2b188SMatthew G Knepley .seealso: DMPlexLabelCohesiveComplete()
1612be2b188SMatthew G Knepley @*/
1622be2b188SMatthew G Knepley PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
1632be2b188SMatthew G Knepley {
1642be2b188SMatthew G Knepley   PetscErrorCode ierr;
1652be2b188SMatthew G Knepley 
1662be2b188SMatthew G Knepley   PetscFunctionBegin;
167b0bf5782SToby Isaac   ierr = DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE);CHKERRQ(ierr);
1682be2b188SMatthew G Knepley   PetscFunctionReturn(0);
1692be2b188SMatthew G Knepley }
1702be2b188SMatthew G Knepley 
1716cf0e42fSMatthew G. Knepley /*@
1726cf0e42fSMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking faces on a surface, we add a cell for each face
1736cf0e42fSMatthew G. Knepley 
1746cf0e42fSMatthew G. Knepley   Input Parameters:
1756cf0e42fSMatthew G. Knepley + dm - The DM
1766cf0e42fSMatthew G. Knepley - label - A DMLabel marking the surface points
1776cf0e42fSMatthew G. Knepley 
1786cf0e42fSMatthew G. Knepley   Output Parameter:
1796cf0e42fSMatthew G. Knepley . label - A DMLabel incorporating cells
1806cf0e42fSMatthew G. Knepley 
1816cf0e42fSMatthew G. Knepley   Level: developer
1826cf0e42fSMatthew G. Knepley 
1836cf0e42fSMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
1846cf0e42fSMatthew G. Knepley 
1856cf0e42fSMatthew G. Knepley .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
1866cf0e42fSMatthew G. Knepley @*/
1876cf0e42fSMatthew G. Knepley PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
1886cf0e42fSMatthew G. Knepley {
1896cf0e42fSMatthew G. Knepley   IS              valueIS;
1906cf0e42fSMatthew G. Knepley   const PetscInt *values;
19122eabd52SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, cEndInterior;
1926cf0e42fSMatthew G. Knepley   PetscErrorCode  ierr;
1936cf0e42fSMatthew G. Knepley 
1946cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
1956cf0e42fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
19622eabd52SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
19722eabd52SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1986cf0e42fSMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1996cf0e42fSMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
2006cf0e42fSMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
2016cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2026cf0e42fSMatthew G. Knepley     IS              pointIS;
2036cf0e42fSMatthew G. Knepley     const PetscInt *points;
2046cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2056cf0e42fSMatthew G. Knepley 
2066cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
2076cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2086cf0e42fSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2096cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2106cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
21122eabd52SMatthew G. Knepley       PetscInt  closureSize, point, cl;
2126cf0e42fSMatthew G. Knepley 
2136cf0e42fSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
21422eabd52SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
21522eabd52SMatthew G. Knepley         point = closure[cl*2];
21622eabd52SMatthew G. Knepley         if ((point >= cStart) && (point < cEnd)) {ierr = DMLabelSetValue(label, point, values[v]);CHKERRQ(ierr); break;}
21722eabd52SMatthew G. Knepley       }
2186cf0e42fSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
2196cf0e42fSMatthew G. Knepley     }
2206cf0e42fSMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2216cf0e42fSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2226cf0e42fSMatthew G. Knepley   }
2236cf0e42fSMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
2246cf0e42fSMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
2256cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2266cf0e42fSMatthew G. Knepley }
2276cf0e42fSMatthew G. Knepley 
228f402d5e4SToby Isaac /*@
229f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
230f402d5e4SToby Isaac 
231f402d5e4SToby Isaac   Input Parameters:
232f402d5e4SToby Isaac + dm - The DM
233f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
234f402d5e4SToby Isaac 
235f402d5e4SToby Isaac   Output Parameter:
236f402d5e4SToby Isaac . label - A DMLabel without cells
237f402d5e4SToby Isaac 
238f402d5e4SToby Isaac   Level: developer
239f402d5e4SToby Isaac 
240f402d5e4SToby Isaac   Note: This undoes DMPlexLabelAddCells()
241f402d5e4SToby Isaac 
242f402d5e4SToby Isaac .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
243f402d5e4SToby Isaac @*/
244f402d5e4SToby Isaac PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
245f402d5e4SToby Isaac {
246f402d5e4SToby Isaac   IS              valueIS;
247f402d5e4SToby Isaac   const PetscInt *values;
248f402d5e4SToby Isaac   PetscInt        numValues, v, cStart, cEnd, cEndInterior;
249f402d5e4SToby Isaac   PetscErrorCode  ierr;
250f402d5e4SToby Isaac 
251f402d5e4SToby Isaac   PetscFunctionBegin;
252f402d5e4SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
253f402d5e4SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
254f402d5e4SToby Isaac   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
255f402d5e4SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
256f402d5e4SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
257f402d5e4SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
258f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
259f402d5e4SToby Isaac     IS              pointIS;
260f402d5e4SToby Isaac     const PetscInt *points;
261f402d5e4SToby Isaac     PetscInt        numPoints, p;
262f402d5e4SToby Isaac 
263f402d5e4SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
264f402d5e4SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
265f402d5e4SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
266f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
267f402d5e4SToby Isaac       PetscInt point = points[p];
268f402d5e4SToby Isaac 
269f402d5e4SToby Isaac       if (point >= cStart && point < cEnd) {
270f402d5e4SToby Isaac         ierr = DMLabelClearValue(label,point,values[v]);CHKERRQ(ierr);
271f402d5e4SToby Isaac       }
272f402d5e4SToby Isaac     }
273f402d5e4SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
274f402d5e4SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
275f402d5e4SToby Isaac   }
276f402d5e4SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
277f402d5e4SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
278f402d5e4SToby Isaac   PetscFunctionReturn(0);
279f402d5e4SToby Isaac }
280f402d5e4SToby Isaac 
28159eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
28259eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
2832582d50cSToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
284cd0c2139SMatthew G Knepley {
2852582d50cSToby Isaac   PetscInt d, off = 0;
2862582d50cSToby Isaac 
2872582d50cSToby Isaac   PetscFunctionBegin;
28859eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
2890974a383SToby Isaac   for (d = 0; d < depth; d++) {
2902582d50cSToby Isaac     PetscInt firstd = d;
2910974a383SToby Isaac     PetscInt firstStart = depthShift[2*d];
2922582d50cSToby Isaac     PetscInt e;
2932582d50cSToby Isaac 
2942582d50cSToby Isaac     for (e = d+1; e <= depth; e++) {
2952582d50cSToby Isaac       if (depthShift[2*e] < firstStart) {
2962582d50cSToby Isaac         firstd = e;
2972582d50cSToby Isaac         firstStart = depthShift[2*d];
2982582d50cSToby Isaac       }
2992582d50cSToby Isaac     }
3002582d50cSToby Isaac     if (firstd != d) {
3012582d50cSToby Isaac       PetscInt swap[2];
3022582d50cSToby Isaac 
3032582d50cSToby Isaac       e = firstd;
3042582d50cSToby Isaac       swap[0] = depthShift[2*d];
3052582d50cSToby Isaac       swap[1] = depthShift[2*d+1];
3062582d50cSToby Isaac       depthShift[2*d]   = depthShift[2*e];
3072582d50cSToby Isaac       depthShift[2*d+1] = depthShift[2*e+1];
3082582d50cSToby Isaac       depthShift[2*e]   = swap[0];
3092582d50cSToby Isaac       depthShift[2*e+1] = swap[1];
3102582d50cSToby Isaac     }
3112582d50cSToby Isaac   }
3122582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
3130974a383SToby Isaac   for (d = 0; d <= depth; d++) {
3142582d50cSToby Isaac     off += depthShift[2*d+1];
31559eef20bSToby Isaac     depthShift[2*d+1] = depthShift[2*d] + off;
3162582d50cSToby Isaac   }
3172582d50cSToby Isaac   PetscFunctionReturn(0);
3182582d50cSToby Isaac }
3192582d50cSToby Isaac 
3202582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3212582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3222582d50cSToby Isaac {
3232582d50cSToby Isaac   PetscInt d;
3242582d50cSToby Isaac   PetscInt newOff = 0;
3252582d50cSToby Isaac 
3262582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3272582d50cSToby Isaac     if (p < depthShift[2*d]) return p + newOff;
3282582d50cSToby Isaac     else newOff = depthShift[2*d+1] - depthShift[2*d];
3292582d50cSToby Isaac   }
3300974a383SToby Isaac   return p + newOff;
3312582d50cSToby Isaac }
3322582d50cSToby Isaac 
3332582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3342582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3352582d50cSToby Isaac {
3362582d50cSToby Isaac   PetscInt d;
3372582d50cSToby Isaac   PetscInt newOff = 0;
3382582d50cSToby Isaac 
3392582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3402582d50cSToby Isaac     if (p < depthShift[2*d+1]) return p + newOff;
3412582d50cSToby Isaac     else newOff = depthShift[2*d] - depthShift[2*d+1];
3422582d50cSToby Isaac   }
3430974a383SToby Isaac   return p + newOff;
344cd0c2139SMatthew G Knepley }
345cd0c2139SMatthew G Knepley 
346cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
347cd0c2139SMatthew G Knepley {
348cd0c2139SMatthew G Knepley   PetscInt       depth = 0, d, pStart, pEnd, p;
349cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
350cd0c2139SMatthew G Knepley 
351cd0c2139SMatthew G Knepley   PetscFunctionBegin;
352cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
353cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
354cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
355cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3560974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
357cd0c2139SMatthew G Knepley   ierr = DMPlexSetChart(dmNew, pStart, pEnd);CHKERRQ(ierr);
358cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
359cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
360cd0c2139SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
361cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
3622582d50cSToby Isaac       PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
363cd0c2139SMatthew G Knepley       PetscInt size;
364cd0c2139SMatthew G Knepley 
365cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
366cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(dmNew, newp, size);CHKERRQ(ierr);
367cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
368cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(dmNew, newp, size);CHKERRQ(ierr);
369cd0c2139SMatthew G Knepley     }
370cd0c2139SMatthew G Knepley   }
371cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
372cd0c2139SMatthew G Knepley }
373cd0c2139SMatthew G Knepley 
374cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
375cd0c2139SMatthew G Knepley {
3762582d50cSToby Isaac   PetscInt      *newpoints;
3772582d50cSToby Isaac   PetscInt       depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
378cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
379cd0c2139SMatthew G Knepley 
380cd0c2139SMatthew G Knepley   PetscFunctionBegin;
381cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
382cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
383cd0c2139SMatthew G Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
384dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
3852582d50cSToby Isaac   ierr = PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints);CHKERRQ(ierr);
386cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
387cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
388cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
389cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
3902582d50cSToby Isaac     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
391cd0c2139SMatthew G Knepley 
392cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
393cd0c2139SMatthew G Knepley     ierr = DMPlexGetCone(dm, p, &points);CHKERRQ(ierr);
394cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeOrientation(dm, p, &orientations);CHKERRQ(ierr);
395cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
3962582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
397cd0c2139SMatthew G Knepley     }
398cd0c2139SMatthew G Knepley     ierr = DMPlexSetCone(dmNew, newp, newpoints);CHKERRQ(ierr);
399cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dmNew, newp, orientations);CHKERRQ(ierr);
400cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
401dcbb62e8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmNew, newp, &sizeNew);CHKERRQ(ierr);
402cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, p, &points);CHKERRQ(ierr);
403cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4042582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
405cd0c2139SMatthew G Knepley     }
406dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
407cd0c2139SMatthew G Knepley     ierr = DMPlexSetSupport(dmNew, newp, newpoints);CHKERRQ(ierr);
408cd0c2139SMatthew G Knepley   }
4092582d50cSToby Isaac   ierr = PetscFree(newpoints);CHKERRQ(ierr);
410cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
411cd0c2139SMatthew G Knepley }
412cd0c2139SMatthew G Knepley 
413cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
414cd0c2139SMatthew G Knepley {
415cd0c2139SMatthew G Knepley   PetscSection   coordSection, newCoordSection;
416cd0c2139SMatthew G Knepley   Vec            coordinates, newCoordinates;
417cd0c2139SMatthew G Knepley   PetscScalar   *coords, *newCoords;
418f2b8cce1SMatthew G. Knepley   PetscInt       coordSize, sStart, sEnd;
419f2b8cce1SMatthew G. Knepley   PetscInt       dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
420f2b8cce1SMatthew G. Knepley   PetscBool      hasCells;
421cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
422cd0c2139SMatthew G Knepley 
423cd0c2139SMatthew G Knepley   PetscFunctionBegin;
424f2b8cce1SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
425c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(dmNew, dim);CHKERRQ(ierr);
426cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
427cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
428cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
429f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
430cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);CHKERRQ(ierr);
431f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew);CHKERRQ(ierr);
43269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
433cd0c2139SMatthew G Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);CHKERRQ(ierr);
434cd0c2139SMatthew G Knepley   ierr = PetscSectionSetNumFields(newCoordSection, 1);CHKERRQ(ierr);
435cd0c2139SMatthew G Knepley   ierr = PetscSectionSetFieldComponents(newCoordSection, 0, dim);CHKERRQ(ierr);
436f2b8cce1SMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
437f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
438f2b8cce1SMatthew G. Knepley   ierr = PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew);CHKERRQ(ierr);
439f2b8cce1SMatthew G. Knepley   if (hasCells) {
440f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
441f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
442f2b8cce1SMatthew G. Knepley 
443f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
444f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetDof(newCoordSection, cNew, dof);CHKERRQ(ierr);
445f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof);CHKERRQ(ierr);
446f2b8cce1SMatthew G. Knepley     }
447f2b8cce1SMatthew G. Knepley   }
448cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
449cd0c2139SMatthew G Knepley     ierr = PetscSectionSetDof(newCoordSection, v, dim);CHKERRQ(ierr);
450cd0c2139SMatthew G Knepley     ierr = PetscSectionSetFieldDof(newCoordSection, v, 0, dim);CHKERRQ(ierr);
451cd0c2139SMatthew G Knepley   }
452cd0c2139SMatthew G Knepley   ierr = PetscSectionSetUp(newCoordSection);CHKERRQ(ierr);
45346e270d4SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection);CHKERRQ(ierr);
454cd0c2139SMatthew G Knepley   ierr = PetscSectionGetStorageSize(newCoordSection, &coordSize);CHKERRQ(ierr);
4558b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
456cd0c2139SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) newCoordinates, "coordinates");CHKERRQ(ierr);
457cd0c2139SMatthew G Knepley   ierr = VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
4584e90ef8eSMatthew G. Knepley   ierr = VecSetBlockSize(newCoordinates, dim);CHKERRQ(ierr);
4592eb5907fSJed Brown   ierr = VecSetType(newCoordinates,VECSTANDARD);CHKERRQ(ierr);
460cd0c2139SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dmNew, newCoordinates);CHKERRQ(ierr);
461cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
462cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
463cd0c2139SMatthew G Knepley   ierr = VecGetArray(newCoordinates, &newCoords);CHKERRQ(ierr);
464f2b8cce1SMatthew G. Knepley   if (hasCells) {
465f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
466f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
467f2b8cce1SMatthew G. Knepley 
468f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
469f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, c, &off);CHKERRQ(ierr);
470f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(newCoordSection, cNew, &noff);CHKERRQ(ierr);
471f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
472f2b8cce1SMatthew G. Knepley     }
473f2b8cce1SMatthew G. Knepley   }
474cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
475cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
476cd0c2139SMatthew G Knepley 
477cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
478cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
4792582d50cSToby Isaac     ierr = PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff);CHKERRQ(ierr);
480f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
481cd0c2139SMatthew G Knepley   }
482cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
483cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(newCoordinates, &newCoords);CHKERRQ(ierr);
484cd0c2139SMatthew G Knepley   ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
485cd0c2139SMatthew G Knepley   ierr = PetscSectionDestroy(&newCoordSection);CHKERRQ(ierr);
486cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
487cd0c2139SMatthew G Knepley }
488cd0c2139SMatthew G Knepley 
489cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
490cd0c2139SMatthew G Knepley {
4912582d50cSToby Isaac   PetscInt           depth = 0;
492cd0c2139SMatthew G Knepley   PetscSF            sfPoint, sfPointNew;
493cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
494cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
495cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
496cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
497cd0c2139SMatthew G Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, totShift = 0;
498cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
499cd0c2139SMatthew G Knepley 
500cd0c2139SMatthew G Knepley   PetscFunctionBegin;
501cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
502cd0c2139SMatthew G Knepley   /* Step 9: Convert pointSF */
503cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
504cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dmNew, &sfPointNew);CHKERRQ(ierr);
505cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
506cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
5076e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd,depth,depthShift) - pEnd;
508cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
509dcca6d9dSJed Brown     ierr = PetscMalloc2(numRoots,&newLocation,pEnd-pStart,&newRemoteLocation);CHKERRQ(ierr);
5102582d50cSToby Isaac     for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
511cd0c2139SMatthew G Knepley     ierr = PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
512cd0c2139SMatthew G Knepley     ierr = PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
513785e854fSJed Brown     ierr = PetscMalloc1(numLeaves,    &glocalPoints);CHKERRQ(ierr);
514785e854fSJed Brown     ierr = PetscMalloc1(numLeaves, &gremotePoints);CHKERRQ(ierr);
515cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
5162582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
517cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
518cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
519cd0c2139SMatthew G Knepley     }
520cd0c2139SMatthew G Knepley     ierr = PetscFree2(newLocation,newRemoteLocation);CHKERRQ(ierr);
521cd0c2139SMatthew G Knepley     ierr = PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
522cd0c2139SMatthew G Knepley   }
523cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
524cd0c2139SMatthew G Knepley }
525cd0c2139SMatthew G Knepley 
526cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
527cd0c2139SMatthew G Knepley {
528cd0c2139SMatthew G Knepley   PetscSF            sfPoint;
529cd0c2139SMatthew G Knepley   DMLabel            vtkLabel, ghostLabel;
530cd0c2139SMatthew G Knepley   const PetscSFNode *leafRemote;
531cd0c2139SMatthew G Knepley   const PetscInt    *leafLocal;
5322582d50cSToby Isaac   PetscInt           depth = 0, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
533cd0c2139SMatthew G Knepley   PetscMPIInt        rank;
534cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
535cd0c2139SMatthew G Knepley 
536cd0c2139SMatthew G Knepley   PetscFunctionBegin;
537cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
538cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
539c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
540cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
541cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
542cd0c2139SMatthew G Knepley     const char     *lname;
543cd0c2139SMatthew G Knepley     PetscBool       isDepth;
544cd0c2139SMatthew G Knepley     IS              valueIS;
545cd0c2139SMatthew G Knepley     const PetscInt *values;
546cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
547cd0c2139SMatthew G Knepley 
548c58f1c22SToby Isaac     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
549cd0c2139SMatthew G Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
550cd0c2139SMatthew G Knepley     if (isDepth) continue;
551c58f1c22SToby Isaac     ierr = DMCreateLabel(dmNew, lname);CHKERRQ(ierr);
552c58f1c22SToby Isaac     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
553c58f1c22SToby Isaac     ierr = DMGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr);
55464a6a1a4SToby Isaac     ierr = DMLabelGetDefaultValue(label,&val);CHKERRQ(ierr);
55564a6a1a4SToby Isaac     ierr = DMLabelSetDefaultValue(newlabel,val);CHKERRQ(ierr);
556cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
557cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
558cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
559cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
560cd0c2139SMatthew G Knepley       IS              pointIS;
561cd0c2139SMatthew G Knepley       const PetscInt *points;
562cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
563cd0c2139SMatthew G Knepley 
564cd0c2139SMatthew G Knepley       ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr);
565cd0c2139SMatthew G Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
566cd0c2139SMatthew G Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
567cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
5682582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
569cd0c2139SMatthew G Knepley 
570cd0c2139SMatthew G Knepley         ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr);
571cd0c2139SMatthew G Knepley       }
572cd0c2139SMatthew G Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
573cd0c2139SMatthew G Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
574cd0c2139SMatthew G Knepley     }
575cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
576cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
577cd0c2139SMatthew G Knepley   }
578cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
579cd0c2139SMatthew G Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
580cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
581cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
582cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr);
583c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "vtk");CHKERRQ(ierr);
584c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "ghost");CHKERRQ(ierr);
585c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr);
586c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr);
587cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
588cd0c2139SMatthew G Knepley     for (; c < leafLocal[l] && c < cEnd; ++c) {
589cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
590cd0c2139SMatthew G Knepley     }
591cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
592cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
593cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
594cd0c2139SMatthew G Knepley     } else {
595cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr);
596cd0c2139SMatthew G Knepley     }
597cd0c2139SMatthew G Knepley   }
598cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
599cd0c2139SMatthew G Knepley     ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
600cd0c2139SMatthew G Knepley   }
601cd0c2139SMatthew G Knepley   if (0) {
602cd0c2139SMatthew G Knepley     ierr = DMLabelView(vtkLabel, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
603cd0c2139SMatthew G Knepley   }
604cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr);
605cd0c2139SMatthew G Knepley   for (f = fStart; f < fEnd; ++f) {
606cd0c2139SMatthew G Knepley     PetscInt numCells;
607cd0c2139SMatthew G Knepley 
608cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr);
609cd0c2139SMatthew G Knepley     if (numCells < 2) {
610cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);
611cd0c2139SMatthew G Knepley     } else {
612cd0c2139SMatthew G Knepley       const PetscInt *cells = NULL;
613cd0c2139SMatthew G Knepley       PetscInt        vA, vB;
614cd0c2139SMatthew G Knepley 
615cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr);
616cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr);
617cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr);
61890664b96SToby Isaac       if (vA != 1 && vB != 1) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);}
619cd0c2139SMatthew G Knepley     }
620cd0c2139SMatthew G Knepley   }
621cd0c2139SMatthew G Knepley   if (0) {
622cd0c2139SMatthew G Knepley     ierr = DMLabelView(ghostLabel, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
623cd0c2139SMatthew G Knepley   }
624cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
625cd0c2139SMatthew G Knepley }
626cd0c2139SMatthew G Knepley 
627ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
628ca04dac2SToby Isaac {
629ca04dac2SToby Isaac   DM             refTree;
630ca04dac2SToby Isaac   PetscSection   pSec;
631ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
632ca04dac2SToby Isaac   PetscErrorCode ierr;
633ca04dac2SToby Isaac 
634ca04dac2SToby Isaac   PetscFunctionBegin;
635ca04dac2SToby Isaac   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
636ca04dac2SToby Isaac   ierr = DMPlexSetReferenceTree(dmNew,refTree);CHKERRQ(ierr);
637ca04dac2SToby Isaac   ierr = DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL);CHKERRQ(ierr);
638ca04dac2SToby Isaac   if (pSec) {
6392582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
640fb4630b5SToby Isaac     PetscInt *childIDsShifted;
641ca04dac2SToby Isaac     PetscSection pSecShifted;
642ca04dac2SToby Isaac 
643ca04dac2SToby Isaac     ierr = PetscSectionGetChart(pSec,&pStart,&pEnd);CHKERRQ(ierr);
644ca04dac2SToby Isaac     ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
6452582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
6462582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
647fb4630b5SToby Isaac     ierr = PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted);CHKERRQ(ierr);
648ca04dac2SToby Isaac     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted);CHKERRQ(ierr);
649ca04dac2SToby Isaac     ierr = PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted);CHKERRQ(ierr);
650ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
651fb4630b5SToby Isaac       /* start off assuming no children */
652fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,p,0);CHKERRQ(ierr);
653fb4630b5SToby Isaac     }
654fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
655fb4630b5SToby Isaac       PetscInt dof;
656fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
657ca04dac2SToby Isaac 
658ca04dac2SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
659fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,pNew,dof);CHKERRQ(ierr);
660ca04dac2SToby Isaac     }
661ca04dac2SToby Isaac     ierr = PetscSectionSetUp(pSecShifted);CHKERRQ(ierr);
662fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
663fb4630b5SToby Isaac       PetscInt dof;
664fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
665fb4630b5SToby Isaac 
666fb4630b5SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
667fb4630b5SToby Isaac       if (dof) {
668fb4630b5SToby Isaac         PetscInt off, offNew;
669fb4630b5SToby Isaac 
670fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSec,p,&off);CHKERRQ(ierr);
671fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSecShifted,pNew,&offNew);CHKERRQ(ierr);
672fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
673fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
674fb4630b5SToby Isaac       }
675fb4630b5SToby Isaac     }
676fb4630b5SToby Isaac     ierr = DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted);CHKERRQ(ierr);
677fb4630b5SToby Isaac     ierr = PetscFree2(parentsShifted,childIDsShifted);CHKERRQ(ierr);
678e6885bbbSToby Isaac     ierr = PetscSectionDestroy(&pSecShifted);CHKERRQ(ierr);
679ca04dac2SToby Isaac   }
680ca04dac2SToby Isaac   PetscFunctionReturn(0);
681ca04dac2SToby Isaac }
682ca04dac2SToby Isaac 
683cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
684cd0c2139SMatthew G Knepley {
685da97024aSMatthew G. Knepley   PetscSF               sf;
686cd0c2139SMatthew G Knepley   IS                    valueIS;
687da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
688cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
6892582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
69090b157c4SStefano Zampini   PetscBool             isper;
69190b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
69290b157c4SStefano Zampini   const DMBoundaryType *bd;
693cd0c2139SMatthew G Knepley   PetscErrorCode        ierr;
694cd0c2139SMatthew G Knepley 
695cd0c2139SMatthew G Knepley   PetscFunctionBegin;
696da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
697da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
698da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
69946c796b9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
700cd0c2139SMatthew G Knepley   /* Count ghost cells */
701cd0c2139SMatthew G Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
702cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr);
703cd0c2139SMatthew G Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
7044a6cfa73SMatthew G. Knepley   Ng   = 0;
705cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
70646c796b9SMatthew G. Knepley     IS              faceIS;
70746c796b9SMatthew G. Knepley     const PetscInt *faces;
70846c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
709cd0c2139SMatthew G Knepley 
71046c796b9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
71146c796b9SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
71246c796b9SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
71346c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
714ca04dac2SToby Isaac       PetscInt numChildren;
715ca04dac2SToby Isaac 
716da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
717ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
718ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
719ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
72046c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
72146c796b9SMatthew G. Knepley     }
7224a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
72346c796b9SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
724cd0c2139SMatthew G Knepley   }
725cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7262582d50cSToby Isaac   ierr = PetscMalloc1(2*(depth+1), &depthShift);CHKERRQ(ierr);
7272582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
72859eef20bSToby Isaac     PetscInt dEnd;
7292582d50cSToby Isaac 
7300974a383SToby Isaac     ierr = DMPlexGetDepthStratum(dm,d,NULL,&dEnd);CHKERRQ(ierr);
73159eef20bSToby Isaac     depthShift[2*d]   = dEnd;
7322582d50cSToby Isaac     depthShift[2*d+1] = 0;
7332582d50cSToby Isaac   }
7342582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
7352582d50cSToby Isaac   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
736cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
737cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
738cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
7395acea1aeSMatthew G. Knepley   ierr = DMPlexSetHybridBounds(gdm, cEnd, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7404a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
741cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr);
742cd0c2139SMatthew G Knepley   }
743cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
744cd0c2139SMatthew G Knepley     IS              faceIS;
745cd0c2139SMatthew G Knepley     const PetscInt *faces;
746cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
747cd0c2139SMatthew G Knepley 
748cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
749cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
750cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
751cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
752ca04dac2SToby Isaac       PetscInt size, numChildren;
753cd0c2139SMatthew G Knepley 
754da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
755ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
756ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
75746c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
758cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr);
7594553fd90SToby Isaac       if (size != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
7604a6cfa73SMatthew G. Knepley       ierr = DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);CHKERRQ(ierr);
761cd0c2139SMatthew G Knepley     }
762cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
763cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
764cd0c2139SMatthew G Knepley   }
765cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
766cd0c2139SMatthew G Knepley   ierr = DMSetUp(gdm);CHKERRQ(ierr);
767cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
768cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
769cd0c2139SMatthew G Knepley   ghostCell = cEnd;
770cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
771cd0c2139SMatthew G Knepley     IS              faceIS;
772cd0c2139SMatthew G Knepley     const PetscInt *faces;
773cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
774cd0c2139SMatthew G Knepley 
775cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
776cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
777cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
77846c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
779ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
780cd0c2139SMatthew G Knepley 
781da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
782ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
783ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
78446c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
785cd0c2139SMatthew G Knepley       ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr);
786cd0c2139SMatthew G Knepley       ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr);
78746c796b9SMatthew G. Knepley       ++ghostCell;
788cd0c2139SMatthew G Knepley     }
789cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
790cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
791cd0c2139SMatthew G Knepley   }
792cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
793cd0c2139SMatthew G Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
794cd0c2139SMatthew G Knepley   /* Step 7: Stratify */
795cd0c2139SMatthew G Knepley   ierr = DMPlexStratify(gdm);CHKERRQ(ierr);
796cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
797cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
798cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
799ca04dac2SToby Isaac   ierr = DMPlexShiftTree_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
800cd0c2139SMatthew G Knepley   ierr = PetscFree(depthShift);CHKERRQ(ierr);
801966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
80290b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
80390b157c4SStefano Zampini   ierr = DMSetPeriodicity(gdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
8044a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
805cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
806cd0c2139SMatthew G Knepley }
807cd0c2139SMatthew G Knepley 
808cd0c2139SMatthew G Knepley /*@C
809cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
810cd0c2139SMatthew G Knepley 
811cd0c2139SMatthew G Knepley   Collective on dm
812cd0c2139SMatthew G Knepley 
813cd0c2139SMatthew G Knepley   Input Parameters:
814cd0c2139SMatthew G Knepley + dm - The original DM
815cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
816cd0c2139SMatthew G Knepley 
817cd0c2139SMatthew G Knepley   Output Parameters:
818cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
819cd0c2139SMatthew G Knepley - dmGhosted - The new DM
820cd0c2139SMatthew G Knepley 
821cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
822cd0c2139SMatthew G Knepley 
823cd0c2139SMatthew G Knepley   Level: developer
824cd0c2139SMatthew G Knepley 
825cd0c2139SMatthew G Knepley .seealso: DMCreate()
82631266bc0SMatthew G. Knepley @*/
827cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
828cd0c2139SMatthew G Knepley {
829cd0c2139SMatthew G Knepley   DM             gdm;
830cd0c2139SMatthew G Knepley   DMLabel        label;
831cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
832cd0c2139SMatthew G Knepley   PetscInt       dim;
833a42b08eeSMatthew G. Knepley   PetscBool      flag;
834cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
835cd0c2139SMatthew G Knepley 
836cd0c2139SMatthew G Knepley   PetscFunctionBegin;
837cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8384a6cfa73SMatthew G. Knepley   if (numGhostCells) PetscValidPointer(numGhostCells, 3);
839cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
840cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr);
841cd0c2139SMatthew G Knepley   ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr);
842c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
843c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(gdm, dim);CHKERRQ(ierr);
844a42b08eeSMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseCone(dm, &flag);CHKERRQ(ierr);
845a42b08eeSMatthew G. Knepley   ierr = DMPlexSetAdjacencyUseCone(gdm, flag);CHKERRQ(ierr);
846a42b08eeSMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseClosure(dm, &flag);CHKERRQ(ierr);
847a42b08eeSMatthew G. Knepley   ierr = DMPlexSetAdjacencyUseClosure(gdm, flag);CHKERRQ(ierr);
848c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
849cd0c2139SMatthew G Knepley   if (!label) {
850cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
851c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
852c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
853e752be1aSMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
854cd0c2139SMatthew G Knepley   }
855cd0c2139SMatthew G Knepley   ierr = DMPlexConstructGhostCells_Internal(dm, label, numGhostCells, gdm);CHKERRQ(ierr);
856a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, gdm);CHKERRQ(ierr);
857cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
858cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
859cd0c2139SMatthew G Knepley }
860cd0c2139SMatthew G Knepley 
861607ab7a9SMatthew G. Knepley /*
862faedd622SMatthew G. Knepley   We are adding three kinds of points here:
863607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
864faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
865607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
866a6ae58d1SMatthew G. Knepley 
867a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
868a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
869607ab7a9SMatthew G. Knepley */
870cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DM sdm)
871cd0c2139SMatthew G Knepley {
872cd0c2139SMatthew G Knepley   MPI_Comm         comm;
873607ab7a9SMatthew G. Knepley   IS               valueIS;
874607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;       /* The number of depths for which we have replicated points */
875607ab7a9SMatthew G. Knepley   const PetscInt  *values;          /* List of depths for which we have replicated points */
87618c5995bSMatthew G. Knepley   IS              *splitIS;
87718c5995bSMatthew G. Knepley   IS              *unsplitIS;
878607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
87918c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
88036dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
88136dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
882607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
88318c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
884cd0c2139SMatthew G Knepley   PetscSection     coordSection;
885cd0c2139SMatthew G Knepley   Vec              coordinates;
886cd0c2139SMatthew G Knepley   PetscScalar     *coords;
887a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
888a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
889607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
890607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
891607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
89218c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
893cd0c2139SMatthew G Knepley   PetscErrorCode   ierr;
894cd0c2139SMatthew G Knepley 
895cd0c2139SMatthew G Knepley   PetscFunctionBegin;
896cd0c2139SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
897c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
898607ab7a9SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
899fd4b9f15SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
900cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
901607ab7a9SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
9022582d50cSToby Isaac   ierr = PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);CHKERRQ(ierr);
903dcca6d9dSJed 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);
904a6ae58d1SMatthew 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);
905607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
906607ab7a9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr);
907a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
908a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
909607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
91018c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
911607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
912a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
913607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
91418c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
91518c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
91618c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
91759eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
91859eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
91959eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
92059eef20bSToby Isaac     depthShift[2*d+1]     = 0;
921607ab7a9SMatthew G. Knepley   }
922cd0c2139SMatthew G Knepley   if (label) {
923cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
924cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr);
925cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
926cd0c2139SMatthew G Knepley   }
927cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
928cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
929cd0c2139SMatthew G Knepley 
930cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
93118c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, dep, &splitIS[dep]);CHKERRQ(ierr);
93218c5995bSMatthew G. Knepley     if (splitIS[dep]) {
93318c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr);
93418c5995bSMatthew G. Knepley       ierr = ISGetIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);
93518c5995bSMatthew G. Knepley     }
93618c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);CHKERRQ(ierr);
93718c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
93818c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);CHKERRQ(ierr);
93918c5995bSMatthew G. Knepley       ierr = ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);
940cd0c2139SMatthew G Knepley     }
941cd0c2139SMatthew G Knepley   }
942607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
94318c5995bSMatthew 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   */
9442582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
9453d3eaba7SBarry Smith   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
94659eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
94759eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
94859eef20bSToby Isaac    * added points */
9492582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
950cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
951cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
952cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
953cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
954cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
9552582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
9564c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
957cd0c2139SMatthew G Knepley       const PetscInt *support;
9584c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
959cd0c2139SMatthew G Knepley 
960cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
961cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr);
962cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
963cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr);
964cd0c2139SMatthew G Knepley       if (dep == depth-1) {
9654c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
9664c367dbcSMatthew G. Knepley 
967cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
9684c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr);
969cd0c2139SMatthew G Knepley       } else if (dep == 0) {
9704c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
971cd0c2139SMatthew G Knepley 
972cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
9734c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
974cd0c2139SMatthew G Knepley           PetscInt val;
975cd0c2139SMatthew G Knepley 
976cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
9774c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
9784c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
9794c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
980cd0c2139SMatthew G Knepley         }
9814c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
9824c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
9834c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
9844c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
9854c367dbcSMatthew G. Knepley         /* Add hybrid edge */
9864c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
9874c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
988cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
9894c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
9904c367dbcSMatthew G. Knepley 
991cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
9924c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
993cd0c2139SMatthew G Knepley           PetscInt val;
994cd0c2139SMatthew G Knepley 
995cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
9964c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
9974c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
9984c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
999cd0c2139SMatthew G Knepley         }
10004c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
10014c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
10024c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
10034c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
10044c367dbcSMatthew G. Knepley         /* Add hybrid face */
10054c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
10064c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1007cd0c2139SMatthew G Knepley       }
1008cd0c2139SMatthew G Knepley     }
1009cd0c2139SMatthew G Knepley   }
101018c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
101118c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
101218c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
10132582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
101418c5995bSMatthew G. Knepley       const PetscInt *support;
1015da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
101618c5995bSMatthew G. Knepley 
101718c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
101818c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
101939254ff6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
102018c5995bSMatthew G. Knepley       if (dep == 0) {
102118c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
102218c5995bSMatthew G. Knepley 
102339254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
102439254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
102539254ff6SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
102639254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
102739254ff6SMatthew G. Knepley         }
102839254ff6SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
102918c5995bSMatthew G. Knepley         /* Add hybrid edge */
103018c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
1031e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1032e1757548SMatthew G. Knepley           PetscInt val;
1033e1757548SMatthew G. Knepley 
1034e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1035e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1036da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1037da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1038e1757548SMatthew G. Knepley         }
1039e1757548SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
104018c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
104118c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1042cd0c2139SMatthew G Knepley         PetscInt       val;
1043cd0c2139SMatthew G Knepley 
1044da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1045cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1046da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1047da1dd7e4SMatthew G. Knepley           else              ++qf;
1048cd0c2139SMatthew G Knepley         }
104918c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
1050da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
105118c5995bSMatthew G. Knepley         /* Add hybrid face */
1052da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1053da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1054da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1055da1dd7e4SMatthew G. Knepley         }
105618c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
1057da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1058cd0c2139SMatthew G Knepley       }
1059cd0c2139SMatthew G Knepley     }
1060cd0c2139SMatthew G Knepley   }
1061cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
1062cd0c2139SMatthew G Knepley   ierr = DMSetUp(sdm);CHKERRQ(ierr);
1063cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1064dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
1065dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);CHKERRQ(ierr);
1066cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1067cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1068cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1069cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10702582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
10714c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1072cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
10734c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1074cd0c2139SMatthew G Knepley 
1075cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1076cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
1077cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
1078cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1079cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
1080cd0c2139SMatthew G Knepley       if (dep == depth-1) {
108196a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
10824c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1083cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1084cd0c2139SMatthew G Knepley 
1085cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
1086cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(sdm, newp,  &supportF);CHKERRQ(ierr);
1087cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr);
1088cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1089cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1090cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
10914c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
109218c5995bSMatthew G. Knepley           if (v < 0) {
109318c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
109418c5995bSMatthew 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);
10952582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
109696a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
109718c5995bSMatthew G. Knepley           } else {
10984c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1099163235baSMatthew G. Knepley             if (dep > 1) {
1100163235baSMatthew G. Knepley               const PetscInt *econe;
1101163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1102163235baSMatthew G. Knepley 
1103163235baSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[q], &econeSize);CHKERRQ(ierr);
1104163235baSMatthew G. Knepley               ierr = DMPlexGetCone(dm, cone[q], &econe);CHKERRQ(ierr);
1105163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
1106163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);CHKERRQ(ierr);
1107163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);CHKERRQ(ierr);
1108163235baSMatthew G. Knepley                 if (vs >= 0) continue;
1109163235baSMatthew 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);
1110163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1111163235baSMatthew G. Knepley               }
1112163235baSMatthew G. Knepley             }
1113cd0c2139SMatthew G Knepley           }
1114cd0c2139SMatthew G Knepley         }
1115cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr);
1116cd0c2139SMatthew G Knepley         ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr);
1117e537020bSMatthew G. Knepley         /* Face support */
1118cd0c2139SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
1119cd0c2139SMatthew G Knepley           PetscInt val;
1120cd0c2139SMatthew G Knepley 
1121cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr);
1122cd0c2139SMatthew G Knepley           if (val < 0) {
1123cd0c2139SMatthew G Knepley             /* Split old face:   Replace negative side cell with cohesive cell */
11244c367dbcSMatthew G. Knepley              ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr);
1125cd0c2139SMatthew G Knepley           } else {
1126cd0c2139SMatthew G Knepley             /* Split new face:   Replace positive side cell with cohesive cell */
11274c367dbcSMatthew G. Knepley             ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr);
1128e537020bSMatthew G. Knepley             /* Get orientation for cohesive face */
1129e537020bSMatthew G. Knepley             {
1130e537020bSMatthew G. Knepley               const PetscInt *ncone, *nconeO;
1131e537020bSMatthew G. Knepley               PetscInt        nconeSize, nc;
1132e537020bSMatthew G. Knepley 
1133e537020bSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr);
1134e537020bSMatthew G. Knepley               ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr);
1135e537020bSMatthew G. Knepley               ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr);
1136e537020bSMatthew G. Knepley               for (nc = 0; nc < nconeSize; ++nc) {
1137e537020bSMatthew G. Knepley                 if (ncone[nc] == oldp) {
1138e537020bSMatthew G. Knepley                   coneONew[0] = nconeO[nc];
1139e537020bSMatthew G. Knepley                   break;
1140cd0c2139SMatthew G Knepley                 }
1141cd0c2139SMatthew G Knepley               }
1142e537020bSMatthew 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]);
1143e537020bSMatthew G. Knepley             }
1144e537020bSMatthew G. Knepley           }
1145e537020bSMatthew G. Knepley         }
11464c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1147fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
11484c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
11494c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1150e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
115118c5995bSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
115218c5995bSMatthew G. Knepley           if (v < 0) {
115318c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
115418c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
115518c5995bSMatthew G. Knepley             coneONew[2+q] = 0;
115618c5995bSMatthew G. Knepley           } else {
115718c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
115818c5995bSMatthew G. Knepley           }
1159e537020bSMatthew G. Knepley           coneONew[2+q] = 0;
1160e537020bSMatthew G. Knepley         }
11614c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr);
11624c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr);
116396a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
116496a07cd0SMatthew G. Knepley         if (hasUnsplit) {ierr = DMLabelSetValue(label, -hybcell, dim);CHKERRQ(ierr);}
1165cd0c2139SMatthew G Knepley       } else if (dep == 0) {
11664c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1167cd0c2139SMatthew G Knepley 
1168cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
11694c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1170cd0c2139SMatthew G Knepley           PetscInt val;
1171cd0c2139SMatthew G Knepley 
1172cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1173cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
11742582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1175cd0c2139SMatthew G Knepley           }
1176cd0c2139SMatthew G Knepley         }
11774c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
1178cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1179cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
11804c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1181cd0c2139SMatthew G Knepley           PetscInt val, edge;
1182cd0c2139SMatthew G Knepley 
1183cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1184cd0c2139SMatthew G Knepley           if (val == 1) {
11854c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
1186cd0c2139SMatthew G Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
11874c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1188cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
11892582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1190cd0c2139SMatthew G Knepley           }
1191cd0c2139SMatthew G Knepley         }
11924c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
1193cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
11944c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1195cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1196cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
11974c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
11984c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
11994c367dbcSMatthew G. Knepley           PetscInt val, edge;
12004c367dbcSMatthew G. Knepley 
12014c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12024c367dbcSMatthew G. Knepley           if (val == 1) {
12034c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
12044c367dbcSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
12054c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
12064c367dbcSMatthew G. Knepley           }
12074c367dbcSMatthew G. Knepley         }
12084c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1209cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
12104c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
12114c367dbcSMatthew G. Knepley 
1212cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1213cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1214cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
12154c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
1216e1757548SMatthew G. Knepley           if (v < 0) {
1217e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1218e1757548SMatthew 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);
12192582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1220e1757548SMatthew G. Knepley           } else {
12214c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1222cd0c2139SMatthew G Knepley           }
1223e1757548SMatthew G. Knepley         }
1224cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr);
1225cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1226cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1227cd0c2139SMatthew G Knepley           PetscInt val;
1228cd0c2139SMatthew G Knepley 
1229cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12304c367dbcSMatthew G. Knepley           if (val == dim-1) {
12312582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
12324c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
12332582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1234cd0c2139SMatthew G Knepley           }
1235cd0c2139SMatthew G Knepley         }
1236b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1237cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1238cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1239cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1240cd0c2139SMatthew G Knepley           PetscInt val, face;
1241cd0c2139SMatthew G Knepley 
1242cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1243cd0c2139SMatthew G Knepley           if (val == dim-1) {
12444c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1245cd0c2139SMatthew G Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
12464c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1247cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
12482582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1249cd0c2139SMatthew G Knepley           }
1250cd0c2139SMatthew G Knepley         }
1251b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1252cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
12534c367dbcSMatthew G. Knepley         /* Hybrid face */
12544c367dbcSMatthew G. Knepley         coneNew[0] = newp;
12554c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
12564c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
12574c367dbcSMatthew G. Knepley           PetscInt vertex;
12584c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr);
1259e1757548SMatthew G. Knepley           if (vertex < 0) {
1260e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);CHKERRQ(ierr);
1261e1757548SMatthew 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);
1262e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1263e1757548SMatthew G. Knepley           } else {
12644c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
12654c367dbcSMatthew G. Knepley           }
1266e1757548SMatthew G. Knepley         }
12674c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
12684c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12694c367dbcSMatthew G. Knepley           PetscInt val, face;
12704c367dbcSMatthew G. Knepley 
12714c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12724c367dbcSMatthew G. Knepley           if (val == dim-1) {
12734c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
12744c367dbcSMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
12754c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
12764c367dbcSMatthew G. Knepley           }
12774c367dbcSMatthew G. Knepley         }
12784c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1279cd0c2139SMatthew G Knepley       }
1280cd0c2139SMatthew G Knepley     }
1281cd0c2139SMatthew G Knepley   }
128218c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
128318c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
128418c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
12852582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
128618c5995bSMatthew G. Knepley       const PetscInt *cone, *support, *ornt;
1287e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
128818c5995bSMatthew G. Knepley 
128918c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
129018c5995bSMatthew G. Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
129118c5995bSMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
129218c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
129318c5995bSMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
129418c5995bSMatthew G. Knepley       if (dep == 0) {
129518c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
129618c5995bSMatthew G. Knepley 
129718c5995bSMatthew G. Knepley         /* Unsplit vertex */
129818c5995bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
129918c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
13002582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
130118c5995bSMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
130218c5995bSMatthew G. Knepley           if (e >= 0) {
130318c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
130418c5995bSMatthew G. Knepley           }
130518c5995bSMatthew G. Knepley         }
130618c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
130718c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
130818c5995bSMatthew G. Knepley         if (q != supportSizeNew) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
130918c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
131018c5995bSMatthew G. Knepley         /* Hybrid edge */
131118c5995bSMatthew G. Knepley         coneNew[0] = newp;
131218c5995bSMatthew G. Knepley         coneNew[1] = newp;
131318c5995bSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
131418c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
131518c5995bSMatthew G. Knepley           PetscInt val, edge;
131618c5995bSMatthew G. Knepley 
131718c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
131818c5995bSMatthew G. Knepley           if (val == 1) {
131918c5995bSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
132018c5995bSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
132118c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1322e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
1323e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);CHKERRQ(ierr);
1324e1757548SMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1325e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
132618c5995bSMatthew G. Knepley           }
132718c5995bSMatthew G. Knepley         }
132818c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1329e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1330e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1331e1757548SMatthew G. Knepley 
1332da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1333e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1334e1757548SMatthew G. Knepley           PetscInt val, face;
1335e1757548SMatthew G. Knepley 
1336e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1337e1757548SMatthew G. Knepley           if (val == dim-1) {
1338e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1339e1757548SMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
13402582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1341e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1342e1757548SMatthew G. Knepley           } else {
13432582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1344e1757548SMatthew G. Knepley           }
1345e1757548SMatthew G. Knepley         }
1346e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1347e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1348da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
1349da1dd7e4SMatthew 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);
1350e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1351e1757548SMatthew G. Knepley         /* Add hybrid face */
1352e1757548SMatthew G. Knepley         coneNew[0] = newp;
1353212cc919SMatthew G. Knepley         coneNew[1] = newp;
1354e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1355e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1356212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1357e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1358e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1359e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1360e1757548SMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
1361da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1362da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1363da1dd7e4SMatthew G. Knepley 
1364da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1365da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
1366da1dd7e4SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1367da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1368da1dd7e4SMatthew G. Knepley           }
1369da1dd7e4SMatthew G. Knepley         }
1370da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);CHKERRQ(ierr);
1371da1dd7e4SMatthew 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);
1372e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1373cd0c2139SMatthew G Knepley       }
1374cd0c2139SMatthew G Knepley     }
1375cd0c2139SMatthew G Knepley   }
1376cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1377cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1378cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1379cd0c2139SMatthew G Knepley     IS              pIS;
1380cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1381cd0c2139SMatthew G Knepley     const PetscInt *points;
1382cd0c2139SMatthew G Knepley 
1383cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
1384cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr);
1385cd0c2139SMatthew G Knepley     if (!pIS) continue;
1386cd0c2139SMatthew G Knepley     dep  = -dep - shift;
1387cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr);
1388cd0c2139SMatthew G Knepley     ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr);
1389cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1390cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
13912582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1392cd0c2139SMatthew G Knepley       const PetscInt *cone;
1393cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
139450cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1395cd0c2139SMatthew G Knepley 
1396cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1397cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
1398cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr);
1399cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr);
1400cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1401e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1402cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1403cd0c2139SMatthew G Knepley 
1404cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr);
1405cd0c2139SMatthew G Knepley         if (val == dep-1) {
1406cd0c2139SMatthew G Knepley           ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr);
1407cd0c2139SMatthew G Knepley           if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1408cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
1409cd0c2139SMatthew G Knepley           ierr     = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr);
141050cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1411cd0c2139SMatthew G Knepley         }
1412cd0c2139SMatthew G Knepley       }
14134a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
14144a189a86SMatthew G. Knepley       /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1415cd0c2139SMatthew G Knepley     }
1416cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr);
1417cd0c2139SMatthew G Knepley     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
1418cd0c2139SMatthew G Knepley   }
1419cd0c2139SMatthew G Knepley   /* Step 7: Stratify */
1420cd0c2139SMatthew G Knepley   ierr = DMPlexStratify(sdm);CHKERRQ(ierr);
1421cd0c2139SMatthew G Knepley   /* Step 8: Coordinates */
1422cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
142369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr);
1424cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr);
1425cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1426cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
14272582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1428cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1429cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1430cd0c2139SMatthew G Knepley 
1431cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr);
1432cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr);
1433cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr);
1434cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1435cd0c2139SMatthew G Knepley   }
1436cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1437cd0c2139SMatthew G Knepley   /* Step 9: SF, if I can figure this out we can split the mesh in parallel */
1438cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1439cd0c2139SMatthew G Knepley   /* Step 10: Labels */
1440cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1441c58f1c22SToby Isaac   ierr = DMGetNumLabels(sdm, &numLabels);CHKERRQ(ierr);
1442cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1443cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
14442582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1445cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1446cd0c2139SMatthew G Knepley       PetscInt       l;
1447cd0c2139SMatthew G Knepley 
1448cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1449cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1450cd0c2139SMatthew G Knepley         const char *lname;
1451cd0c2139SMatthew G Knepley         PetscInt    val;
14529a356370SMatthew G. Knepley         PetscBool   isDepth;
1453cd0c2139SMatthew G Knepley 
1454c58f1c22SToby Isaac         ierr = DMGetLabelName(sdm, l, &lname);CHKERRQ(ierr);
14559a356370SMatthew G. Knepley         ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
14569a356370SMatthew G. Knepley         if (isDepth) continue;
1457c58f1c22SToby Isaac         ierr = DMGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr);
1458cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr);
1459cd0c2139SMatthew G Knepley         if (val >= 0) {
1460cd0c2139SMatthew G Knepley           ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr);
1461cd0c2139SMatthew G Knepley         }
1462cd0c2139SMatthew G Knepley       }
1463cd0c2139SMatthew G Knepley     }
1464cd0c2139SMatthew G Knepley   }
1465cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1466cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1467cd0c2139SMatthew G Knepley 
1468cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
146918c5995bSMatthew G. Knepley     if (splitIS[dep]) {ierr = ISRestoreIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);}
147018c5995bSMatthew G. Knepley     ierr = ISDestroy(&splitIS[dep]);CHKERRQ(ierr);
147118c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {ierr = ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);}
147218c5995bSMatthew G. Knepley     ierr = ISDestroy(&unsplitIS[dep]);CHKERRQ(ierr);
1473cd0c2139SMatthew G Knepley   }
1474cd0c2139SMatthew G Knepley   if (label) {
1475cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
1476cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1477cd0c2139SMatthew G Knepley   }
14780d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
14790d4d4d06SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr);
148036dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
14810d4d4d06SMatthew G. Knepley   }
14820e49e2e2SMatthew 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);
1483dcbb62e8SMatthew G. Knepley   ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr);
14842582d50cSToby Isaac   ierr = PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld);CHKERRQ(ierr);
148518c5995bSMatthew G. Knepley   ierr = PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);CHKERRQ(ierr);
1486cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1487cd0c2139SMatthew G Knepley }
1488cd0c2139SMatthew G Knepley 
1489cd0c2139SMatthew G Knepley /*@C
1490cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1491cd0c2139SMatthew G Knepley 
1492cd0c2139SMatthew G Knepley   Collective on dm
1493cd0c2139SMatthew G Knepley 
1494cd0c2139SMatthew G Knepley   Input Parameters:
1495cd0c2139SMatthew G Knepley + dm - The original DM
149653156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1497cd0c2139SMatthew G Knepley 
1498cd0c2139SMatthew G Knepley   Output Parameters:
1499cd0c2139SMatthew G Knepley - dmSplit - The new DM
1500cd0c2139SMatthew G Knepley 
1501cd0c2139SMatthew G Knepley   Level: developer
1502cd0c2139SMatthew G Knepley 
15032be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1504cd0c2139SMatthew G Knepley @*/
1505cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DM *dmSplit)
1506cd0c2139SMatthew G Knepley {
1507cd0c2139SMatthew G Knepley   DM             sdm;
1508cd0c2139SMatthew G Knepley   PetscInt       dim;
1509cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
1510cd0c2139SMatthew G Knepley 
1511cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1512cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
151353156dfcSMatthew G. Knepley   PetscValidPointer(dmSplit, 3);
1514cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr);
1515cd0c2139SMatthew G Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
1516c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1517c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(sdm, dim);CHKERRQ(ierr);
1518cd0c2139SMatthew G Knepley   switch (dim) {
1519cd0c2139SMatthew G Knepley   case 2:
1520cd0c2139SMatthew G Knepley   case 3:
1521cd0c2139SMatthew G Knepley     ierr = DMPlexConstructCohesiveCells_Internal(dm, label, sdm);CHKERRQ(ierr);
1522cd0c2139SMatthew G Knepley     break;
1523cd0c2139SMatthew G Knepley   default:
1524cd0c2139SMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1525cd0c2139SMatthew G Knepley   }
1526cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1527cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1528cd0c2139SMatthew G Knepley }
1529cd0c2139SMatthew G Knepley 
15300f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
15310f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
15320f66a230SMatthew G. Knepley {
15330f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
15340f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
15350f66a230SMatthew G. Knepley   PetscErrorCode  ierr;
15360f66a230SMatthew G. Knepley 
15370f66a230SMatthew G. Knepley   PetscFunctionBegin;
15380f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
1539c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
15400f66a230SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
15410f66a230SMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
15420f66a230SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
15430f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
15440f66a230SMatthew G. Knepley     if (cone[c] == face) {
15450f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
15460f66a230SMatthew G. Knepley 
15470f66a230SMatthew G. Knepley       if (subdm) {
15480f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
15490f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
15500f66a230SMatthew G. Knepley 
15510f66a230SMatthew G. Knepley         ierr = PetscFindInt(cell, numSubpoints, subpoints, &subpoint);CHKERRQ(ierr);
15520f66a230SMatthew G. Knepley         ierr = PetscFindInt(face, numSubpoints, subpoints, &subface);CHKERRQ(ierr);
15530f66a230SMatthew G. Knepley         ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
15540f66a230SMatthew G. Knepley         ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr);
15550f66a230SMatthew G. Knepley         ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr);
15560f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
15570f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
15580f66a230SMatthew G. Knepley             o = subornt[0];
15590f66a230SMatthew G. Knepley             break;
15600f66a230SMatthew G. Knepley           }
15610f66a230SMatthew G. Knepley         }
15620f66a230SMatthew 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);
15630f66a230SMatthew G. Knepley       }
15640f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
15650f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
15660f66a230SMatthew G. Knepley       break;
15670f66a230SMatthew G. Knepley     }
15680f66a230SMatthew G. Knepley   }
15690f66a230SMatthew 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);
15700f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
15710f66a230SMatthew G. Knepley }
15720f66a230SMatthew G. Knepley 
1573cd0c2139SMatthew G Knepley /*@
15740f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1575cd0c2139SMatthew G Knepley   to complete the surface
1576cd0c2139SMatthew G Knepley 
1577cd0c2139SMatthew G Knepley   Input Parameters:
1578cd0c2139SMatthew G Knepley + dm     - The DM
15790f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
15800f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1581bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
158247946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1583cd0c2139SMatthew G Knepley 
1584cd0c2139SMatthew G Knepley   Output Parameter:
1585cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1586cd0c2139SMatthew G Knepley 
15870f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
15880f66a230SMatthew G. Knepley 
1589cd0c2139SMatthew G Knepley   Level: developer
1590cd0c2139SMatthew G Knepley 
15912be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1592cd0c2139SMatthew G Knepley @*/
15930f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1594cd0c2139SMatthew G Knepley {
1595d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
15968630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
159747946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1598bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1599370472baSMatthew G. Knepley   PetscInt       *pMax;
1600370472baSMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, dim, depth, pSize, dep, cStart, cEnd, cMax, fStart, fEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1601cd0c2139SMatthew G Knepley   PetscErrorCode  ierr;
1602cd0c2139SMatthew G Knepley 
1603cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1604370472baSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1605370472baSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1606370472baSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
1607370472baSMatthew G. Knepley   ierr = PetscMalloc1(pSize,&pMax);CHKERRQ(ierr);
1608370472baSMatthew 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);
1609d90583fdSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1610c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
161147946fd8SMatthew G. Knepley   if (subdm) {
161247946fd8SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
161347946fd8SMatthew G. Knepley     if (subpointIS) {
161447946fd8SMatthew G. Knepley       ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
161547946fd8SMatthew G. Knepley       ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
161647946fd8SMatthew G. Knepley     }
161747946fd8SMatthew G. Knepley   }
1618d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1619cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr);
1620dc86033cSMatthew G. Knepley   if (!dimIS) {
1621dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
16228630cb1aSMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1623dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1624dc86033cSMatthew G. Knepley   }
1625cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1626cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1627d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1628cd0c2139SMatthew G Knepley     const PetscInt *support;
1629cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1630cd0c2139SMatthew G Knepley 
1631cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr);
1632cd0c2139SMatthew G Knepley     if (supportSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports", points[p], supportSize);
1633cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr);
1634cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
16350f66a230SMatthew G. Knepley       const PetscInt *cone;
1636cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
16370f66a230SMatthew G. Knepley       PetscBool       pos;
1638cd0c2139SMatthew G Knepley 
16390f66a230SMatthew G. Knepley       ierr = GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);CHKERRQ(ierr);
16400f66a230SMatthew G. Knepley       if (pos) {ierr = DMLabelSetValue(label, support[s],  rev*(shift+dim));CHKERRQ(ierr);}
16410f66a230SMatthew G. Knepley       else     {ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr);}
16420f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
16430f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
1644cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
1645cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
1646cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1647cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1648cd0c2139SMatthew G Knepley 
1649cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1650cd0c2139SMatthew G Knepley         if (val == -1) {
1651cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1652cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1653cd0c2139SMatthew G Knepley 
1654cd0c2139SMatthew G Knepley           ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1655cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1656cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1657a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1658cd0c2139SMatthew G Knepley 
1659cd0c2139SMatthew G Knepley             ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1660a0541d8aSMatthew G. Knepley             if (blabel) {ierr = DMLabelGetValue(blabel, clp, &bval);CHKERRQ(ierr);}
1661a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1662cd0c2139SMatthew G Knepley               ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr);
1663cd0c2139SMatthew G Knepley               break;
1664cd0c2139SMatthew G Knepley             }
1665cd0c2139SMatthew G Knepley           }
1666cd0c2139SMatthew G Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1667cd0c2139SMatthew G Knepley         }
1668cd0c2139SMatthew G Knepley       }
1669cd0c2139SMatthew G Knepley     }
1670cd0c2139SMatthew G Knepley   }
16710f66a230SMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
16720f66a230SMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
167347946fd8SMatthew G. Knepley   if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);}
167447946fd8SMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1675a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
167686200784SMatthew G. Knepley   if (blabel) {
1677a0541d8aSMatthew G. Knepley     ierr = DMLabelGetStratumIS(blabel, 1, &dimIS);CHKERRQ(ierr);
1678a0541d8aSMatthew G. Knepley     ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1679a0541d8aSMatthew G. Knepley     ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1680a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1681a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1682a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1683a0541d8aSMatthew G. Knepley 
1684a0541d8aSMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1685a0541d8aSMatthew G. Knepley       if (bval >= 0) {
1686f7019248SMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1687f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1688f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1689f7019248SMatthew G. Knepley           ierr = DMLabelClearValue(blabel, point, bval);CHKERRQ(ierr);
1690f7019248SMatthew G. Knepley         }
1691f7019248SMatthew G. Knepley       }
1692f7019248SMatthew G. Knepley     }
1693f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1694f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1695f7019248SMatthew G. Knepley       PetscInt       val, bval;
1696f7019248SMatthew G. Knepley 
1697f7019248SMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1698f7019248SMatthew G. Knepley       if (bval >= 0) {
169986200784SMatthew G. Knepley         const PetscInt *cone,    *support;
170086200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
170186200784SMatthew G. Knepley 
1702a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
1703a0541d8aSMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1704a0541d8aSMatthew 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);
1705a0541d8aSMatthew G. Knepley         ierr = DMLabelClearValue(label, point, val);CHKERRQ(ierr);
1706a0541d8aSMatthew G. Knepley         ierr = DMLabelSetValue(label, point, shift2+val);CHKERRQ(ierr);
17072c06a818SMatthew G. Knepley         /* Check for cross-edge
17082c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
170986200784SMatthew G. Knepley         if (val != 0) continue;
171086200784SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
171186200784SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
171286200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
171386200784SMatthew G. Knepley           ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
171486200784SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
171586200784SMatthew G. Knepley           if (coneSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
171686200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[0], &valA);CHKERRQ(ierr);
171786200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[1], &valB);CHKERRQ(ierr);
171886200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, support[s], &valE);CHKERRQ(ierr);
171909816f77SMatthew G. Knepley           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) {ierr = DMLabelSetValue(blabel, support[s], 2);CHKERRQ(ierr);}
172086200784SMatthew G. Knepley         }
1721a0541d8aSMatthew G. Knepley       }
1722a0541d8aSMatthew G. Knepley     }
1723a0541d8aSMatthew G. Knepley     ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1724a0541d8aSMatthew G. Knepley     ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1725a0541d8aSMatthew G. Knepley   }
1726cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
1727c4d480a2SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1728cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1729d24f5ce5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1730d24f5ce5SMatthew G. Knepley   cMax = cMax < 0 ? cEnd : cMax;
17310f66a230SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1732cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr);
173386200784SMatthew G. Knepley   if (blabel) {ierr = DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);CHKERRQ(ierr);}
173486200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
173586200784SMatthew G. Knepley     IS vertIS = dimIS;
173686200784SMatthew G. Knepley 
173786200784SMatthew G. Knepley     ierr = ISExpand(vertIS, crossEdgeIS, &dimIS);CHKERRQ(ierr);
173886200784SMatthew G. Knepley     ierr = ISDestroy(&crossEdgeIS);CHKERRQ(ierr);
173986200784SMatthew G. Knepley     ierr = ISDestroy(&vertIS);CHKERRQ(ierr);
174086200784SMatthew G. Knepley   }
1741dc86033cSMatthew G. Knepley   if (!dimIS) {
1742dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
1743dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1744dc86033cSMatthew G. Knepley   }
1745cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1746cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1747d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1748cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
174986200784SMatthew G. Knepley     PetscInt  starSize, s;
1750cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1751cd0c2139SMatthew G Knepley 
1752d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1753cd0c2139SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1754cd0c2139SMatthew G Knepley     while (again) {
1755cd0c2139SMatthew G Knepley       if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1756cd0c2139SMatthew G Knepley       again = 0;
1757cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1758cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1759cd0c2139SMatthew G Knepley         const PetscInt *cone;
1760cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1761cd0c2139SMatthew G Knepley 
1762d24f5ce5SMatthew G. Knepley         if ((point < cStart) || (point >= cMax)) continue;
1763cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1764cd0c2139SMatthew G Knepley         if (val != -1) continue;
1765d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
1766cd0c2139SMatthew G Knepley         ierr  = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1767cd0c2139SMatthew G Knepley         ierr  = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
1768cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
1769cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr);
1770cd0c2139SMatthew G Knepley           if (val != -1) {
1771d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1772166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1773d7c8f101SMatthew G. Knepley 
1774db8edaafSMatthew 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);
177537a6de01SMatthew G. Knepley             if (val > 0) side =  1;
177637a6de01SMatthew G. Knepley             else         side = -1;
177737a6de01SMatthew G. Knepley             ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr);
1778d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
1779d7c8f101SMatthew G. Knepley             ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr);
1780d7c8f101SMatthew G. Knepley             ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr);
1781d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
1782d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
1783d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
1784d7c8f101SMatthew G. Knepley 
1785166d9d0cSMatthew G. Knepley               ierr = DMLabelGetValue(label, ccone[cc], &val);CHKERRQ(ierr);
1786166d9d0cSMatthew G. Knepley               if (val != -1) continue;
1787d7c8f101SMatthew G. Knepley               ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1788d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
1789d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
1790d7c8f101SMatthew G. Knepley 
1791d7c8f101SMatthew G. Knepley                 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1792d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
1793d7c8f101SMatthew G. Knepley                 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr);
179437a6de01SMatthew G. Knepley                 break;
179537a6de01SMatthew G. Knepley               }
1796d7c8f101SMatthew G. Knepley               ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1797cd0c2139SMatthew G Knepley             }
1798cd0c2139SMatthew G Knepley             again = 1;
1799cd0c2139SMatthew G Knepley             break;
1800cd0c2139SMatthew G Knepley           }
1801cd0c2139SMatthew G Knepley         }
1802cd0c2139SMatthew G Knepley       }
1803cd0c2139SMatthew G Knepley     }
1804cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
1805cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1806cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
1807cd0c2139SMatthew G Knepley 
1808cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1809cd0c2139SMatthew G Knepley       if (val == -1) {
1810cd0c2139SMatthew G Knepley         PetscInt  *sstar = NULL;
1811cd0c2139SMatthew G Knepley         PetscInt   sstarSize, ss;
1812cd0c2139SMatthew G Knepley         PetscBool  marked = PETSC_FALSE;
1813cd0c2139SMatthew G Knepley 
1814cd0c2139SMatthew G Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1815cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
1816cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
1817cd0c2139SMatthew G Knepley 
1818d24f5ce5SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cMax)) continue;
1819cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr);
1820cd0c2139SMatthew 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);
1821d90583fdSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1822cd0c2139SMatthew G Knepley           if (val > 0) {
1823cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point,   shift+dep);CHKERRQ(ierr);
1824cd0c2139SMatthew G Knepley           } else {
1825cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr);
1826cd0c2139SMatthew G Knepley           }
1827cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
1828cd0c2139SMatthew G Knepley           break;
1829cd0c2139SMatthew G Knepley         }
1830cd0c2139SMatthew G Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1831370472baSMatthew G. Knepley         ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1832370472baSMatthew G. Knepley         if (point < pMax[dep] && !marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1833cd0c2139SMatthew G Knepley       }
1834cd0c2139SMatthew G Knepley     }
1835cd0c2139SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1836cd0c2139SMatthew G Knepley   }
1837cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1838cd0c2139SMatthew G Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
183918c5995bSMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
184018c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);CHKERRQ(ierr);
184118c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);CHKERRQ(ierr);
184218c5995bSMatthew G. Knepley   ierr = ISExpand(facePosIS, faceNegIS, &dimIS);CHKERRQ(ierr);
184318c5995bSMatthew G. Knepley   ierr = ISDestroy(&facePosIS);CHKERRQ(ierr);
184418c5995bSMatthew G. Knepley   ierr = ISDestroy(&faceNegIS);CHKERRQ(ierr);
184518c5995bSMatthew G. Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
184618c5995bSMatthew G. Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
184718c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
184818c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
184918c5995bSMatthew G. Knepley     const PetscInt *support;
185018c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
185118c5995bSMatthew G. Knepley 
185218c5995bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
185318c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
185418c5995bSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
185518c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[0], &valA);CHKERRQ(ierr);
185618c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[1], &valB);CHKERRQ(ierr);
185718c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
185818c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
185918c5995bSMatthew G. Knepley     /* Split the face */
186018c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &valA);CHKERRQ(ierr);
186118c5995bSMatthew G. Knepley     ierr = DMLabelClearValue(label, point, valA);CHKERRQ(ierr);
186218c5995bSMatthew G. Knepley     ierr = DMLabelSetValue(label, point, dim-1);CHKERRQ(ierr);
1863e1757548SMatthew G. Knepley     /* Label its closure:
1864e1757548SMatthew G. Knepley       unmarked: label as unsplit
1865e1757548SMatthew G. Knepley       incident: relabel as split
1866e1757548SMatthew G. Knepley       split:    do nothing
1867e1757548SMatthew G. Knepley     */
186818c5995bSMatthew G. Knepley     {
186918c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
187018c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
187118c5995bSMatthew G. Knepley 
187218c5995bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
187318c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
187418c5995bSMatthew G. Knepley         ierr = DMLabelGetValue(label, closure[cl], &valA);CHKERRQ(ierr);
18758771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
187618c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
187718c5995bSMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], shift2+dep);CHKERRQ(ierr);
18788771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1879e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
1880e1757548SMatthew G. Knepley           ierr = DMLabelClearValue(label, closure[cl], valA);CHKERRQ(ierr);
1881e1757548SMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], dep);CHKERRQ(ierr);
1882e1757548SMatthew G. Knepley         }
188318c5995bSMatthew G. Knepley       }
188418c5995bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
188518c5995bSMatthew G. Knepley     }
188618c5995bSMatthew G. Knepley   }
188718c5995bSMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
188818c5995bSMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1889370472baSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
1890cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1891cd0c2139SMatthew G Knepley }
1892cd0c2139SMatthew G Knepley 
1893c08575a3SMatthew G. Knepley /*@
18943cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
18953cf72582SMatthew G. Knepley 
18963cf72582SMatthew G. Knepley   Collective on dm
18973cf72582SMatthew G. Knepley 
18983cf72582SMatthew G. Knepley   Input Parameters:
18993cf72582SMatthew G. Knepley + dm - The original DM
19003cf72582SMatthew G. Knepley - labelName - The label specifying the interface vertices
19013cf72582SMatthew G. Knepley 
19023cf72582SMatthew G. Knepley   Output Parameters:
19033cf72582SMatthew G. Knepley + hybridLabel - The label fully marking the interface
19043cf72582SMatthew G. Knepley - dmHybrid - The new DM
19053cf72582SMatthew G. Knepley 
19063cf72582SMatthew G. Knepley   Level: developer
19073cf72582SMatthew G. Knepley 
19083cf72582SMatthew G. Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMCreate()
19093cf72582SMatthew G. Knepley @*/
19103cf72582SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel *hybridLabel, DM *dmHybrid)
19113cf72582SMatthew G. Knepley {
19123cf72582SMatthew G. Knepley   DM             idm;
19133cf72582SMatthew G. Knepley   DMLabel        subpointMap, hlabel;
19143cf72582SMatthew G. Knepley   PetscInt       dim;
19153cf72582SMatthew G. Knepley   PetscErrorCode ierr;
19163cf72582SMatthew G. Knepley 
19173cf72582SMatthew G. Knepley   PetscFunctionBegin;
19183cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19193cf72582SMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 3);
19203cf72582SMatthew G. Knepley   PetscValidPointer(dmHybrid, 4);
1921c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
19223cf72582SMatthew G. Knepley   ierr = DMPlexCreateSubmesh(dm, label, 1, &idm);CHKERRQ(ierr);
19233cf72582SMatthew G. Knepley   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
19243cf72582SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
19253cf72582SMatthew G. Knepley   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
19263cf72582SMatthew G. Knepley   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
19270f66a230SMatthew G. Knepley   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, NULL, PETSC_FALSE, idm);CHKERRQ(ierr);
19283cf72582SMatthew G. Knepley   ierr = DMDestroy(&idm);CHKERRQ(ierr);
19293cf72582SMatthew G. Knepley   ierr = DMPlexConstructCohesiveCells(dm, hlabel, dmHybrid);CHKERRQ(ierr);
19303cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
193153aa2e23SMatthew G. Knepley   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
1932cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1933cd0c2139SMatthew G Knepley }
1934cd0c2139SMatthew G Knepley 
1935efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
1936efa14ee0SMatthew G Knepley 
1937efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
1938efa14ee0SMatthew G Knepley */
1939830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
1940efa14ee0SMatthew G Knepley {
1941fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
1942efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
194377d178adSMatthew G. Knepley   PetscInt        *pStart, *pEnd, *pMax, pSize;
1944efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
1945efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
1946efa14ee0SMatthew G Knepley 
1947efa14ee0SMatthew G Knepley   PetscFunctionBegin;
1948efa14ee0SMatthew G Knepley   *numFaces = 0;
1949efa14ee0SMatthew G Knepley   *nFV      = 0;
1950efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1951c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
195277d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
1953dcca6d9dSJed Brown   ierr = PetscMalloc3(pSize,&pStart,pSize,&pEnd,pSize,&pMax);CHKERRQ(ierr);
1954d92e47f8SMatthew 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);
1955efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
1956efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
1957efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
1958efa14ee0SMatthew G Knepley   }
1959efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
1960fed694aaSMatthew G. Knepley   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
1961efa14ee0SMatthew G Knepley   if (subvertexIS) {
1962efa14ee0SMatthew G Knepley     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
1963efa14ee0SMatthew G Knepley     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
1964efa14ee0SMatthew G Knepley   }
1965efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
1966efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
19670298fd71SBarry Smith     PetscInt      *star   = NULL;
1968efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
1969efa14ee0SMatthew G Knepley 
1970efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1971efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1972efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
1973efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
1974efa14ee0SMatthew G Knepley     }
1975efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
1976efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
19770298fd71SBarry Smith       PetscInt      *closure = NULL;
1978efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
1979efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
1980efa14ee0SMatthew G Knepley 
1981efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
198265560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
198382f516ccSBarry Smith       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
1984efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1985efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
1986efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
1987efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
1988efa14ee0SMatthew G Knepley 
1989efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
1990efa14ee0SMatthew G Knepley           ++numCorners;
1991efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
1992830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
1993efa14ee0SMatthew G Knepley         }
1994efa14ee0SMatthew G Knepley       }
199518ad9376SMatthew G. Knepley       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
199682f516ccSBarry 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);
1997efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
1998007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
1999007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2000007baee2SMatthew G. Knepley 
2001efa14ee0SMatthew G Knepley         ++(*numFaces);
2002efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2003efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
2004efa14ee0SMatthew G Knepley         }
2005007baee2SMatthew G. Knepley         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2006007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2007007baee2SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
2008007baee2SMatthew G. Knepley         }
2009007baee2SMatthew G. Knepley         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2010efa14ee0SMatthew G Knepley       }
2011efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2012efa14ee0SMatthew G Knepley     }
2013efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2014efa14ee0SMatthew G Knepley   }
2015efa14ee0SMatthew G Knepley   if (subvertexIS) {
2016efa14ee0SMatthew G Knepley     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2017efa14ee0SMatthew G Knepley   }
2018efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2019efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2020efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2021efa14ee0SMatthew G Knepley }
2022efa14ee0SMatthew G Knepley 
2023830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, DM subdm)
2024efa14ee0SMatthew G Knepley {
202534b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2026efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2027efa14ee0SMatthew G Knepley   PetscInt        *pStart, *pEnd, *pMax;
2028efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2029efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2030efa14ee0SMatthew G Knepley 
2031efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2032c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2033dcca6d9dSJed Brown   ierr = PetscMalloc3(dim+1,&pStart,dim+1,&pEnd,dim+1,&pMax);CHKERRQ(ierr);
20340298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
2035efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2036efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2037efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2038efa14ee0SMatthew G Knepley   }
2039efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
204034b4c39eSMatthew G. Knepley   if (vertexLabel) {
2041830e53efSMatthew G. Knepley     ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
2042efa14ee0SMatthew G Knepley     if (subvertexIS) {
2043efa14ee0SMatthew G Knepley       ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2044efa14ee0SMatthew G Knepley       ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2045efa14ee0SMatthew G Knepley     }
204634b4c39eSMatthew G. Knepley   }
2047efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2048efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
20490298fd71SBarry Smith     PetscInt      *star   = NULL;
2050efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2051efa14ee0SMatthew G Knepley 
2052efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2053efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2054efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2055efa14ee0SMatthew G Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) star[numFaces++] = point;
2056efa14ee0SMatthew G Knepley     }
2057efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2058efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
20590298fd71SBarry Smith       PetscInt      *closure = NULL;
2060efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2061efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2062efa14ee0SMatthew G Knepley 
2063efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
2064efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
206582f516ccSBarry Smith       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2066efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2067efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2068efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2069efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2070efa14ee0SMatthew G Knepley 
2071efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2072efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2073830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2074efa14ee0SMatthew G Knepley         }
2075efa14ee0SMatthew G Knepley       }
2076efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2077efa14ee0SMatthew G Knepley         const PetscInt *support;
2078efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2079efa14ee0SMatthew G Knepley 
2080efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2081efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2082efa14ee0SMatthew G Knepley 
2083efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2084efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2085efa14ee0SMatthew G Knepley               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2086efa14ee0SMatthew G Knepley               break;
2087efa14ee0SMatthew G Knepley             }
2088efa14ee0SMatthew G Knepley           }
2089efa14ee0SMatthew G Knepley         }
2090efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2091efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2092efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2093efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2094efa14ee0SMatthew G Knepley         }
2095efa14ee0SMatthew G Knepley       }
2096efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2097efa14ee0SMatthew G Knepley     }
2098efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2099efa14ee0SMatthew G Knepley   }
210034b4c39eSMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);}
2101efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2102efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2103efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2104efa14ee0SMatthew G Knepley }
2105efa14ee0SMatthew G Knepley 
210627c04023SMatthew 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)
2107766ab985SMatthew G. Knepley {
210827c04023SMatthew G. Knepley   DMLabel         label = NULL;
2109766ab985SMatthew G. Knepley   const PetscInt *cone;
21109fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2111766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
2112766ab985SMatthew G. Knepley 
2113812bfc34SJed Brown   PetscFunctionBegin;
2114c0ed958bSJed Brown   *numFaces = 0;
2115c0ed958bSJed Brown   *nFV = 0;
2116c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
2117fed694aaSMatthew G. Knepley   *subCells = NULL;
2118c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2119766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2120766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2121766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
212227c04023SMatthew G. Knepley   if (label) {
212327c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
212427c04023SMatthew G. Knepley       PetscInt val;
212527c04023SMatthew G. Knepley 
212627c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
212727c04023SMatthew G. Knepley       if (val == value) {
212827c04023SMatthew G. Knepley         ++(*numFaces);
212927c04023SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
213027c04023SMatthew G. Knepley       }
213127c04023SMatthew G. Knepley     }
213227c04023SMatthew G. Knepley   } else {
2133766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
213427c04023SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
213527c04023SMatthew G. Knepley   }
2136785e854fSJed Brown   ierr = PetscMalloc1(*numFaces *2, subCells);CHKERRQ(ierr);
21379fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
21389fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2139766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2140766ab985SMatthew G. Knepley     const PetscInt *cells;
2141766ab985SMatthew G. Knepley     PetscInt        numCells;
2142766ab985SMatthew G. Knepley 
214327c04023SMatthew G. Knepley     if (label) {
214427c04023SMatthew G. Knepley       PetscInt val;
214527c04023SMatthew G. Knepley 
214627c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
214727c04023SMatthew G. Knepley       if (val != value) continue;
214827c04023SMatthew G. Knepley     }
2149766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2150766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2151766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
2152766ab985SMatthew G. Knepley     }
2153766ab985SMatthew G. Knepley     /* Negative face */
2154766ab985SMatthew G. Knepley     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
215527234c99SMatthew G. Knepley     /* Not true in parallel
215627234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2157766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2158766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
215927234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2160766ab985SMatthew G. Knepley     }
2161766ab985SMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2162766ab985SMatthew G. Knepley     /* Positive face is not included */
2163766ab985SMatthew G. Knepley   }
2164766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2165766ab985SMatthew G. Knepley }
2166766ab985SMatthew G. Knepley 
21673982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2168766ab985SMatthew G. Knepley {
2169766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2170766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2171766ab985SMatthew G. Knepley   PetscErrorCode ierr;
2172766ab985SMatthew G. Knepley 
2173812bfc34SJed Brown   PetscFunctionBegin;
2174c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2175766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2176766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2177766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2178dcca6d9dSJed Brown   ierr = PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);CHKERRQ(ierr);
2179b3154360SMatthew G. Knepley   for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);}
2180766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2181766ab985SMatthew G. Knepley     const PetscInt *cone;
2182766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2183b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2184766ab985SMatthew G. Knepley 
218527c04023SMatthew G. Knepley     if (label) {
218627c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
218727c04023SMatthew G. Knepley       if (val != value) continue;
218827c04023SMatthew G. Knepley     }
2189766ab985SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2190766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2191b3154360SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr);
2192b3154360SMatthew G. Knepley     if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2193b3154360SMatthew G. Knepley     /* Negative face */
2194766ab985SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2195766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2196766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2197766ab985SMatthew G. Knepley 
2198766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2199766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2200766ab985SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2201766ab985SMatthew G. Knepley           break;
2202766ab985SMatthew G. Knepley         }
2203766ab985SMatthew G. Knepley       }
2204766ab985SMatthew G. Knepley     }
2205766ab985SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2206766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2207766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2208766ab985SMatthew G. Knepley       const PetscInt *support;
2209766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2210766ab985SMatthew G. Knepley 
2211766ab985SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
2212711de394SMatthew G. Knepley       /* if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2213766ab985SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
2214766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2215766ab985SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2216766ab985SMatthew G. Knepley       }
2217766ab985SMatthew G. Knepley     }
2218766ab985SMatthew G. Knepley   }
2219766ab985SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2220766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2221766ab985SMatthew G. Knepley }
2222766ab985SMatthew G. Knepley 
2223c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2224e6ccafaeSMatthew G Knepley {
222582f516ccSBarry Smith   MPI_Comm       comm;
2226e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2227e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2228e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2229e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
2230e6ccafaeSMatthew G Knepley 
223182f516ccSBarry Smith   PetscFunctionBegin;
223282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2233c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
2234e6ccafaeSMatthew G Knepley   if (debug) {PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
2235e6ccafaeSMatthew G Knepley 
2236ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2237ddeab2a6SMatthew G. Knepley     /* Triangle */
2238e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2239e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2240ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2241ddeab2a6SMatthew G. Knepley     /* Triangle */
2242ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2243ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2244ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2245ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2246ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2247ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2248e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2249e6ccafaeSMatthew G Knepley     /* Quadratic line */
2250e6ccafaeSMatthew G Knepley     faceSize  = 1;
2251e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2252e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2253e6ccafaeSMatthew G Knepley     /* Quads */
2254e6ccafaeSMatthew G Knepley     faceSize = 2;
2255e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2256e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2257e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2258e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2259e6ccafaeSMatthew G Knepley     } else {
2260e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2261e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2262e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2263e6ccafaeSMatthew G Knepley     }
2264e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2265e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2266e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2267e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2268e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2269e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2270e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2271e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2272e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2273e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2274e6ccafaeSMatthew G Knepley     };
2275e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2276e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2277e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2278e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2279e6ccafaeSMatthew G Knepley     };
2280e6ccafaeSMatthew G Knepley 
2281e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2282e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
2283e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2284e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2285e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2286e6ccafaeSMatthew G Knepley 
2287e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2288e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2289e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2290e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2291e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2292e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2293e6ccafaeSMatthew G Knepley               break;
2294e6ccafaeSMatthew G Knepley             }
2295e6ccafaeSMatthew G Knepley           }
2296e6ccafaeSMatthew G Knepley         }
2297e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2298e6ccafaeSMatthew G Knepley         break;
2299e6ccafaeSMatthew G Knepley       }
2300e6ccafaeSMatthew G Knepley     }
2301e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2302e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2303e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2304e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2305e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2306e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2307e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2308e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2309e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2310e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2311e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2312e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2313e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2314e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2315e6ccafaeSMatthew G Knepley     };
2316e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2317e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2318e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2319e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2320e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2321e6ccafaeSMatthew G Knepley     };
2322e6ccafaeSMatthew G Knepley 
2323e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2324e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
2325e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2326e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2327e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2328e6ccafaeSMatthew G Knepley 
2329e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2330e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2331e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2332e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2333e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2334e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2335e6ccafaeSMatthew G Knepley               break;
2336e6ccafaeSMatthew G Knepley             }
2337e6ccafaeSMatthew G Knepley           }
2338e6ccafaeSMatthew G Knepley         }
2339e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2340e6ccafaeSMatthew G Knepley         break;
2341e6ccafaeSMatthew G Knepley       }
2342e6ccafaeSMatthew G Knepley     }
2343e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2344e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2345e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2346e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2347e6ccafaeSMatthew G Knepley     /* Hexes
2348e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2349e6ccafaeSMatthew G Knepley        pointing up at the second.
2350e6ccafaeSMatthew G Knepley 
2351e6ccafaeSMatthew G Knepley           7---6
2352e6ccafaeSMatthew G Knepley          /|  /|
2353e6ccafaeSMatthew G Knepley         4---5 |
2354ddeab2a6SMatthew G. Knepley         | 1-|-2
2355e6ccafaeSMatthew G Knepley         |/  |/
2356ddeab2a6SMatthew G. Knepley         0---3
2357e6ccafaeSMatthew G Knepley 
2358e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2359e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2360e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2361e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2362e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2363e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2364e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2365ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2366ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2367ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2368ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2369e6ccafaeSMatthew G Knepley     };
2370e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2371ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2372e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2373ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2374ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2375ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2376ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2377e6ccafaeSMatthew G Knepley     };
2378e6ccafaeSMatthew G Knepley 
2379e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2380e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
2381e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2382e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2383e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2384e6ccafaeSMatthew G Knepley 
2385e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2386e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2387e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2388e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2389e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2390e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2391e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2392e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2393e6ccafaeSMatthew G Knepley               break;
2394e6ccafaeSMatthew G Knepley             }
2395e6ccafaeSMatthew G Knepley           }
2396e6ccafaeSMatthew G Knepley         }
2397e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2398e6ccafaeSMatthew G Knepley         break;
2399e6ccafaeSMatthew G Knepley       }
2400e6ccafaeSMatthew G Knepley     }
2401e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2402e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2403e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2404e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2405e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2406e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2407e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2408e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2409e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2410e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2411e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2412e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2413e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2414e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2415e6ccafaeSMatthew G Knepley     };
2416e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2417e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2418e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2419e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2420e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2421e6ccafaeSMatthew G Knepley     };
2422e6ccafaeSMatthew G Knepley 
2423e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2424e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
2425e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2426e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2427e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2428e6ccafaeSMatthew G Knepley 
2429e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2430e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2431e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2432e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2433e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2434e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2435e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2436e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2437e6ccafaeSMatthew G Knepley               break;
2438e6ccafaeSMatthew G Knepley             }
2439e6ccafaeSMatthew G Knepley           }
2440e6ccafaeSMatthew G Knepley         }
2441e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2442e6ccafaeSMatthew G Knepley         break;
2443e6ccafaeSMatthew G Knepley       }
2444e6ccafaeSMatthew G Knepley     }
2445e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2446e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2447e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2448e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2449e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2450e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2451e6ccafaeSMatthew G Knepley        pointing up at the second.
2452e6ccafaeSMatthew G Knepley 
2453e6ccafaeSMatthew G Knepley          7---6
2454e6ccafaeSMatthew G Knepley         /|  /|
2455e6ccafaeSMatthew G Knepley        4---5 |
2456e6ccafaeSMatthew G Knepley        | 3-|-2
2457e6ccafaeSMatthew G Knepley        |/  |/
2458e6ccafaeSMatthew G Knepley        0---1
2459e6ccafaeSMatthew G Knepley 
2460e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2461e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2462e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2463e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2464e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2465e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2466e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2467e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2468e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2469e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2470e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2471e6ccafaeSMatthew G Knepley     };
2472e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2473e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2474e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2475e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2476e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2477e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2478e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2479e6ccafaeSMatthew G Knepley     };
2480e6ccafaeSMatthew G Knepley 
2481e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2482e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
2483e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2484e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2485e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2486e6ccafaeSMatthew G Knepley 
2487e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2488e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2489e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2490e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2491e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2492e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2493e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2494e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2495e6ccafaeSMatthew G Knepley               break;
2496e6ccafaeSMatthew G Knepley             }
2497e6ccafaeSMatthew G Knepley           }
2498e6ccafaeSMatthew G Knepley         }
2499e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2500e6ccafaeSMatthew G Knepley         break;
2501e6ccafaeSMatthew G Knepley       }
2502e6ccafaeSMatthew G Knepley     }
2503e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2504e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2505e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2506e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2507e6ccafaeSMatthew G Knepley   if (!posOrient) {
2508e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
2509e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2510e6ccafaeSMatthew G Knepley   } else {
2511e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
2512e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2513e6ccafaeSMatthew G Knepley   }
2514e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2515e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2516e6ccafaeSMatthew G Knepley }
2517e6ccafaeSMatthew G Knepley 
2518c08575a3SMatthew G. Knepley /*@
2519c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2520c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2521c08575a3SMatthew G. Knepley 
2522c08575a3SMatthew G. Knepley   Not collective
2523c08575a3SMatthew G. Knepley 
2524c08575a3SMatthew G. Knepley   Input Parameters:
2525c08575a3SMatthew G. Knepley + dm           - The original mesh
2526c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2527c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2528c08575a3SMatthew G. Knepley . face         - The face vertices
2529c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2530c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2531c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2532c08575a3SMatthew G. Knepley 
2533c08575a3SMatthew G. Knepley   Output Parameter:
2534c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2535c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2536c08575a3SMatthew G. Knepley 
2537c08575a3SMatthew G. Knepley   Level: developer
2538c08575a3SMatthew G. Knepley 
2539c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2540c08575a3SMatthew G. Knepley @*/
2541e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2542e6ccafaeSMatthew G Knepley {
25430298fd71SBarry Smith   const PetscInt *cone = NULL;
2544e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2545e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2546e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2547e6ccafaeSMatthew G Knepley 
2548e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2549e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
2550e6ccafaeSMatthew G Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2551e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2552e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2553e6ccafaeSMatthew G Knepley 
2554e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2555e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2556e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2557e6ccafaeSMatthew G Knepley       }
2558e6ccafaeSMatthew G Knepley     }
2559e6ccafaeSMatthew G Knepley     if (found) {
2560e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2561e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2562e6ccafaeSMatthew G Knepley       ++v2;
2563e6ccafaeSMatthew G Knepley     } else {
2564e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2565e6ccafaeSMatthew G Knepley     }
2566e6ccafaeSMatthew G Knepley   }
2567e6ccafaeSMatthew G Knepley   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
2568e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2569e6ccafaeSMatthew G Knepley }
2570e6ccafaeSMatthew G Knepley 
2571e6ccafaeSMatthew G Knepley /*
2572cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2573e6ccafaeSMatthew G Knepley 
2574e6ccafaeSMatthew G Knepley   Not collective
2575e6ccafaeSMatthew G Knepley 
2576e6ccafaeSMatthew G Knepley   Input Parameters:
2577e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2578e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2579e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2580e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2581e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2582e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2583e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2584e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2585e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2586e6ccafaeSMatthew G Knepley 
2587e6ccafaeSMatthew G Knepley   Output Parameters:
2588e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2589e6ccafaeSMatthew G Knepley 
2590e6ccafaeSMatthew G Knepley   Level: developer
2591e6ccafaeSMatthew G Knepley */
2592cd0c2139SMatthew 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)
2593e6ccafaeSMatthew G Knepley {
259482f516ccSBarry Smith   MPI_Comm        comm;
2595e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2596e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2597e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2598e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2599e6ccafaeSMatthew G Knepley 
2600e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
260182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2602e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
2603e6ccafaeSMatthew G Knepley   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2604e6ccafaeSMatthew G Knepley #if 0
2605e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2606e6ccafaeSMatthew G Knepley   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2607e6ccafaeSMatthew G Knepley #else
2608e6ccafaeSMatthew G Knepley   {
2609e6ccafaeSMatthew G Knepley     PetscInt f;
2610e6ccafaeSMatthew G Knepley 
2611e6ccafaeSMatthew G Knepley     numFaces = 0;
261269291d52SBarry Smith     ierr     = DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2613e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2614e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2615e6ccafaeSMatthew G Knepley 
2616e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
2617e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
2618e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2619e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2620e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2621e6ccafaeSMatthew G Knepley         PetscInt       v;
2622e6ccafaeSMatthew G Knepley 
2623e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2624e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2625e6ccafaeSMatthew G Knepley         }
2626e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2627e6ccafaeSMatthew G Knepley       }
2628e6ccafaeSMatthew G Knepley       if (d == dof) {
2629e6ccafaeSMatthew G Knepley         numFaces               = 1;
2630e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2631e6ccafaeSMatthew G Knepley       }
2632e6ccafaeSMatthew G Knepley     }
2633e6ccafaeSMatthew G Knepley   }
2634e6ccafaeSMatthew G Knepley #endif
2635e6ccafaeSMatthew G Knepley   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2636e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2637e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2638766ab985SMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
2639e6ccafaeSMatthew G Knepley   } else {
2640e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2641e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2642e6ccafaeSMatthew G Knepley 
264369291d52SBarry Smith     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2644e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2645e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2646e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2647e6ccafaeSMatthew G Knepley     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
2648e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2649e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2650e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2651e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2652e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2653e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2654e6ccafaeSMatthew G Knepley           break;
2655e6ccafaeSMatthew G Knepley         }
2656e6ccafaeSMatthew G Knepley       }
2657e6ccafaeSMatthew G Knepley       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2658e6ccafaeSMatthew G Knepley     }
2659e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2660e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
266169291d52SBarry Smith     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2662e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2663e6ccafaeSMatthew G Knepley   }
2664ef07cca7SMatthew G. Knepley #if 0
2665e6ccafaeSMatthew G Knepley   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2666ef07cca7SMatthew G. Knepley #else
266769291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2668ef07cca7SMatthew G. Knepley #endif
2669e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2670e6ccafaeSMatthew G Knepley }
2671e6ccafaeSMatthew G Knepley 
267253156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2673e6ccafaeSMatthew G Knepley {
267482f516ccSBarry Smith   MPI_Comm        comm;
267553156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2676efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2677efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2678efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2679fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2680efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2681e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2682e6ccafaeSMatthew G Knepley 
2683e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
268482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2685efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2686efa14ee0SMatthew G Knepley   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
2687efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2688efa14ee0SMatthew G Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
268953156dfcSMatthew G. Knepley   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2690efa14ee0SMatthew G Knepley   /* Setup chart */
2691efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2692efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2693efa14ee0SMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2694efa14ee0SMatthew G Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2695e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2696e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2697efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2698e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2699efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2700efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2701efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2702efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2703e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2704e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2705e6ccafaeSMatthew G Knepley   }
2706e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2707e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2708e6ccafaeSMatthew G Knepley   }
2709e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2710e6ccafaeSMatthew G Knepley   /* Create face cones */
2711efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
27120298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
271369291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2714e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2715e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2716efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
27170298fd71SBarry Smith     PetscInt      *closure = NULL;
2718efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2719e6ccafaeSMatthew G Knepley 
2720e6ccafaeSMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2721efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2722efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2723e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2724e6ccafaeSMatthew G Knepley 
2725efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2726efa14ee0SMatthew G Knepley         ++numCorners;
2727efa14ee0SMatthew G Knepley         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2728efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2729efa14ee0SMatthew G Knepley           closure[faceSize] = point;
273065560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2731e6ccafaeSMatthew G Knepley           ++faceSize;
2732e6ccafaeSMatthew G Knepley         }
2733e6ccafaeSMatthew G Knepley       }
2734e6ccafaeSMatthew G Knepley     }
2735efa14ee0SMatthew G Knepley     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2736efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2737cd0c2139SMatthew G Knepley       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2738e6ccafaeSMatthew G Knepley     }
273965560c7fSMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2740e6ccafaeSMatthew G Knepley   }
274169291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2742e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2743e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2744e6ccafaeSMatthew G Knepley   /* Build coordinates */
2745efa14ee0SMatthew G Knepley   {
2746efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2747efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2748efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2749285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
275024640c55SToby Isaac     const char  *name;
2751efa14ee0SMatthew G Knepley 
275269d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2753e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
275469d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2755285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2756285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2757285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2758efa14ee0SMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2759efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
2760efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
2761efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
2762efa14ee0SMatthew G Knepley       PetscInt       dof;
2763efa14ee0SMatthew G Knepley 
2764efa14ee0SMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2765efa14ee0SMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2766285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2767e6ccafaeSMatthew G Knepley     }
2768e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2769e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
27708b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
277124640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
277224640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
2773e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
27742eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2775830e53efSMatthew G. Knepley     if (coordSize) {
2776e6ccafaeSMatthew G Knepley       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2777e6ccafaeSMatthew G Knepley       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2778efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
2779efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
2780efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
2781efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
2782e6ccafaeSMatthew G Knepley 
2783e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2784e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2785efa14ee0SMatthew G Knepley         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2786efa14ee0SMatthew G Knepley         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2787efa14ee0SMatthew 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);
2788e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2789e6ccafaeSMatthew G Knepley       }
2790e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2791e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
27923b399e24SMatthew G. Knepley     }
2793e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2794e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2795e6ccafaeSMatthew G Knepley   }
2796efa14ee0SMatthew G Knepley   /* Cleanup */
2797efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2798efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2799efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2800efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
2801e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2802e6ccafaeSMatthew G Knepley }
2803e6ccafaeSMatthew G Knepley 
28043982b651SMatthew G. Knepley PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
28053982b651SMatthew G. Knepley {
28063982b651SMatthew G. Knepley   PetscInt       subPoint;
28073982b651SMatthew G. Knepley   PetscErrorCode ierr;
28083982b651SMatthew G. Knepley 
28093982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
28103982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
28113982b651SMatthew G. Knepley }
28123982b651SMatthew G. Knepley 
2813bec263e5SMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
2814e6ccafaeSMatthew G Knepley {
281582f516ccSBarry Smith   MPI_Comm         comm;
281653156dfcSMatthew G. Knepley   DMLabel          subpointMap;
2817efa14ee0SMatthew G Knepley   IS              *subpointIS;
2818efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
28193982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
28200d366550SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, cMax, cEnd, dim, p, d, v;
28210d366550SMatthew G. Knepley   PetscMPIInt      rank;
2822e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
2823e6ccafaeSMatthew G Knepley 
2824e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
282582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
28260d366550SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2827efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2828e6ccafaeSMatthew G Knepley   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
2829efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2830bec263e5SMatthew G. Knepley   if (cellHeight) {
28313982b651SMatthew G. Knepley     if (isCohesive) {ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
28323982b651SMatthew G. Knepley     else            {ierr = DMPlexMarkSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
2833bec263e5SMatthew G. Knepley   } else {
2834bec263e5SMatthew G. Knepley     DMLabel         depth;
2835bec263e5SMatthew G. Knepley     IS              pointIS;
2836bec263e5SMatthew G. Knepley     const PetscInt *points;
2837bec263e5SMatthew G. Knepley     PetscInt        numPoints;
2838bec263e5SMatthew G. Knepley 
2839bec263e5SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
2840bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, value, &numPoints);CHKERRQ(ierr);
2841bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, value, &pointIS);CHKERRQ(ierr);
2842bec263e5SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2843bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2844bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
2845bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
2846bec263e5SMatthew G. Knepley 
2847bec263e5SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2848bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
2849bec263e5SMatthew G. Knepley         ierr = DMLabelGetValue(depth, closure[c], &pdim);CHKERRQ(ierr);
2850bec263e5SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, closure[c], pdim);CHKERRQ(ierr);
2851bec263e5SMatthew G. Knepley       }
2852bec263e5SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2853bec263e5SMatthew G. Knepley     }
2854bec263e5SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2855bec263e5SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2856bec263e5SMatthew G. Knepley   }
2857bec263e5SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
28580d366550SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
28590d366550SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
28600d366550SMatthew G. Knepley   cMax = (cMax < 0) ? cEnd : cMax;
2861efa14ee0SMatthew G Knepley   /* Setup chart */
2862c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2863dcca6d9dSJed Brown   ierr = PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);CHKERRQ(ierr);
2864e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2865e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
2866e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
2867e6ccafaeSMatthew G Knepley   }
2868e6ccafaeSMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
2869bec263e5SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, cellHeight);CHKERRQ(ierr);
2870e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2871e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
2872e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2873e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2874e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2875e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2876e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
28770ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
2878e6ccafaeSMatthew G Knepley   }
2879e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2880e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
2881e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
2882e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
2883e6ccafaeSMatthew G Knepley       const PetscInt *cone;
2884e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
2885e6ccafaeSMatthew G Knepley 
2886e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2887e6ccafaeSMatthew G Knepley       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
2888bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
2889e6ccafaeSMatthew G Knepley         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
2890e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2891e6ccafaeSMatthew G Knepley           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
2892e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
2893e6ccafaeSMatthew G Knepley         }
2894e6ccafaeSMatthew G Knepley         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
2895e6ccafaeSMatthew G Knepley       }
2896e6ccafaeSMatthew G Knepley     }
2897e6ccafaeSMatthew G Knepley   }
2898e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2899e6ccafaeSMatthew G Knepley   /* Set cones */
29000298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
29013982b651SMatthew G. Knepley   ierr = PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);CHKERRQ(ierr);
2902e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2903e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
2904e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
2905e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
29060e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
29070d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
2908e6ccafaeSMatthew G Knepley 
29090d366550SMatthew G. Knepley       if (d == dim-1) {
29100d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
29110d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
29120d366550SMatthew G. Knepley 
29130d366550SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
29140d366550SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
29150d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
29160d366550SMatthew G. Knepley           if ((support[s] < cMax) || (support[s] >= cEnd)) continue;
29170d366550SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc);CHKERRQ(ierr);
29180d366550SMatthew G. Knepley           if (subc >= 0) {
29190d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
29200d366550SMatthew G. Knepley 
29217f79407eSBarry Smith             ierr = DMPlexGetCone(dm, ccell, &cone);CHKERRQ(ierr);
29227f79407eSBarry Smith             ierr = DMPlexGetConeSize(dm, ccell, &coneSize);CHKERRQ(ierr);
29237f79407eSBarry Smith             ierr = DMPlexGetConeOrientation(dm, ccell, &ornt);CHKERRQ(ierr);
29240d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
29250d366550SMatthew G. Knepley               if (cone[c] == point) {
29260d366550SMatthew G. Knepley                 fornt = ornt[c];
29270d366550SMatthew G. Knepley                 break;
29280d366550SMatthew G. Knepley               }
29290d366550SMatthew G. Knepley             }
29300d366550SMatthew G. Knepley             break;
29310d366550SMatthew G. Knepley           }
29320d366550SMatthew G. Knepley         }
29330d366550SMatthew G. Knepley       }
2934e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2935e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
2936e6ccafaeSMatthew G Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
29370e49e2e2SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
2938e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2939e6ccafaeSMatthew G Knepley         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
294001a2673eSMatthew G. Knepley         if (subc >= 0) {
294101a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
29423982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
294301a2673eSMatthew G. Knepley           ++coneSizeNew;
294401a2673eSMatthew G. Knepley         }
2945e6ccafaeSMatthew G Knepley       }
2946e6ccafaeSMatthew G Knepley       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
29470d366550SMatthew G. Knepley       if (fornt < 0) {
29480d366550SMatthew G. Knepley         /* This should be replaced by a call to DMPlexReverseCell() */
29490d366550SMatthew G. Knepley #if 0
2950a129c400SMatthew G. Knepley         ierr = DMPlexReverseCell(subdm, subpoint);CHKERRQ(ierr);
2951a129c400SMatthew G. Knepley #else
2952c0b40891SMatthew G. Knepley         for (c = 0; c < coneSizeNew/2 + coneSizeNew%2; ++c) {
2953a129c400SMatthew G. Knepley           PetscInt faceSize, tmp;
29540d366550SMatthew G. Knepley 
29550d366550SMatthew G. Knepley           tmp        = coneNew[c];
29560d366550SMatthew G. Knepley           coneNew[c] = coneNew[coneSizeNew-1-c];
29570d366550SMatthew G. Knepley           coneNew[coneSizeNew-1-c] = tmp;
2958a129c400SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, cone[c], &faceSize);CHKERRQ(ierr);
2959a129c400SMatthew G. Knepley           tmp        = orntNew[c] >= 0 ? -(faceSize-orntNew[c]) : faceSize+orntNew[c];
2960a129c400SMatthew G. Knepley           orntNew[c] = orntNew[coneSizeNew-1-c] >= 0 ? -(faceSize-orntNew[coneSizeNew-1-c]) : faceSize+orntNew[coneSizeNew-1-c];
29610d366550SMatthew G. Knepley           orntNew[coneSizeNew-1-c] = tmp;
29620d366550SMatthew G. Knepley         }
29630d366550SMatthew G. Knepley       }
2964e6ccafaeSMatthew G Knepley       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
29653982b651SMatthew G. Knepley       ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr);
2966a129c400SMatthew G. Knepley #endif
2967e6ccafaeSMatthew G Knepley     }
2968e6ccafaeSMatthew G Knepley   }
29693982b651SMatthew G. Knepley   ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr);
2970e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2971e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2972e6ccafaeSMatthew G Knepley   /* Build coordinates */
2973e6ccafaeSMatthew G Knepley   {
2974e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
2975e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
2976e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
2977c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
297824640c55SToby Isaac     const char  *name;
2979e6ccafaeSMatthew G Knepley 
2980c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
298169d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2982e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
298369d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2984285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2985285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2986285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2987e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
2988e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
2989e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
2990e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
2991e6ccafaeSMatthew G Knepley       PetscInt       dof;
2992e6ccafaeSMatthew G Knepley 
2993e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2994e6ccafaeSMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2995285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2996e6ccafaeSMatthew G Knepley     }
2997e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2998e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
29998b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
300024640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
300124640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3002e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3003c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
30042eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3005e6ccafaeSMatthew G Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3006e6ccafaeSMatthew G Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3007e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3008e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3009e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3010e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3011e6ccafaeSMatthew G Knepley 
3012e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3013e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3014e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3015e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3016e6ccafaeSMatthew 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);
3017efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3018e6ccafaeSMatthew G Knepley     }
3019e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3020e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3021e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3022e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3023e6ccafaeSMatthew G Knepley   }
30243982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
30253982b651SMatthew G. Knepley   {
30263982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
30273982b651SMatthew G. Knepley     IS                 subpIS;
30283982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
30293982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
30303982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
30313982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
30323982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
30333982b651SMatthew G. Knepley     PetscMPIInt        rank;
30343982b651SMatthew G. Knepley 
30353982b651SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
30363982b651SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
30373982b651SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
30383982b651SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
30393982b651SMatthew G. Knepley     ierr = DMPlexGetChart(subdm, NULL, &numSubroots);CHKERRQ(ierr);
30403982b651SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpIS);CHKERRQ(ierr);
30413982b651SMatthew G. Knepley     if (subpIS) {
30423982b651SMatthew G. Knepley       ierr = ISGetIndices(subpIS, &subpoints);CHKERRQ(ierr);
30433982b651SMatthew G. Knepley       ierr = ISGetLocalSize(subpIS, &numSubpoints);CHKERRQ(ierr);
30443982b651SMatthew G. Knepley     }
30453982b651SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
30463982b651SMatthew G. Knepley     if (numRoots >= 0) {
30473982b651SMatthew G. Knepley       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
30483982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
30493982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
30503982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
30513982b651SMatthew G. Knepley       }
30523982b651SMatthew G. Knepley       /* Set subleaves */
30533982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
30543982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
30553982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
30563982b651SMatthew G. Knepley 
30573982b651SMatthew G. Knepley         if (subpoint < 0) continue;
30583982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
30593982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
30603982b651SMatthew G. Knepley         ++numSubleaves;
30613982b651SMatthew G. Knepley       }
30623982b651SMatthew G. Knepley       /* Must put in owned subpoints */
30633982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
30643982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
30653982b651SMatthew G. Knepley 
30663982b651SMatthew G. Knepley         if (subpoint < 0) {
30673982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
30683982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
30693982b651SMatthew G. Knepley         } else {
30703982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
30713982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
30723982b651SMatthew G. Knepley         }
30733982b651SMatthew G. Knepley       }
30743982b651SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
30753982b651SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
30763982b651SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
30773982b651SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
30783982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &slocalPoints);CHKERRQ(ierr);
30793982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &sremotePoints);CHKERRQ(ierr);
30803982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
30813982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
30823982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
30833982b651SMatthew G. Knepley 
30843982b651SMatthew G. Knepley         if (subpoint < 0) continue;
30853982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
30863982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
30873982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
30883982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
30893982b651SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
30903982b651SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
30913982b651SMatthew G. Knepley         ++sl;
30923982b651SMatthew G. Knepley       }
30933982b651SMatthew G. Knepley       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
30943982b651SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
30953982b651SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
30963982b651SMatthew G. Knepley     }
30973982b651SMatthew G. Knepley     if (subpIS) {
30983982b651SMatthew G. Knepley       ierr = ISRestoreIndices(subpIS, &subpoints);CHKERRQ(ierr);
30993982b651SMatthew G. Knepley       ierr = ISDestroy(&subpIS);CHKERRQ(ierr);
31003982b651SMatthew G. Knepley     }
31013982b651SMatthew G. Knepley   }
3102efa14ee0SMatthew G Knepley   /* Cleanup */
3103e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
31040ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3105e6ccafaeSMatthew G Knepley     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
3106e6ccafaeSMatthew G Knepley   }
3107efa14ee0SMatthew G Knepley   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
3108e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3109e6ccafaeSMatthew G Knepley }
3110e6ccafaeSMatthew G Knepley 
31113982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
31123982b651SMatthew G. Knepley {
31133982b651SMatthew G. Knepley   PetscErrorCode ierr;
31143982b651SMatthew G. Knepley 
31153982b651SMatthew G. Knepley   PetscFunctionBegin;
3116bec263e5SMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, PETSC_FALSE, 1, subdm);CHKERRQ(ierr);
31173982b651SMatthew G. Knepley   PetscFunctionReturn(0);
31183982b651SMatthew G. Knepley }
31193982b651SMatthew G. Knepley 
3120d0fa310fSMatthew G. Knepley /*@
3121e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3122e6ccafaeSMatthew G Knepley 
3123e6ccafaeSMatthew G Knepley   Input Parameters:
3124e6ccafaeSMatthew G Knepley + dm           - The original mesh
3125830e53efSMatthew G. Knepley . vertexLabel  - The DMLabel marking vertices contained in the surface
3126830e53efSMatthew G. Knepley - value        - The label value to use
3127e6ccafaeSMatthew G Knepley 
3128e6ccafaeSMatthew G Knepley   Output Parameter:
3129e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3130e6ccafaeSMatthew G Knepley 
3131e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3132e6ccafaeSMatthew G Knepley 
3133e6ccafaeSMatthew G Knepley   Level: developer
3134e6ccafaeSMatthew G Knepley 
3135c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3136830e53efSMatthew G. Knepley @*/
313753156dfcSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, DM *subdm)
3138e6ccafaeSMatthew G Knepley {
3139dd2763adSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3140e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3141e6ccafaeSMatthew G Knepley 
3142e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3143e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3144766ab985SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3145c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3146e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
314782f516ccSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3148e6ccafaeSMatthew G Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3149c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3150dd2763adSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3151dd2763adSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3152e6ccafaeSMatthew G Knepley   if (depth == dim) {
3153830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3154e6ccafaeSMatthew G Knepley   } else {
3155830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3156e6ccafaeSMatthew G Knepley   }
3157e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3158e6ccafaeSMatthew G Knepley }
3159e6ccafaeSMatthew G Knepley 
316027c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3161766ab985SMatthew G. Knepley {
3162766ab985SMatthew G. Knepley   MPI_Comm        comm;
3163766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3164766ab985SMatthew G. Knepley   IS              subvertexIS;
3165766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3166fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3167766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
316887feddfdSMatthew G. Knepley   PetscInt        cMax, c, f;
3169766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
3170766ab985SMatthew G. Knepley 
3171766ab985SMatthew G. Knepley   PetscFunctionBegin;
3172766ab985SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
3173766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3174766ab985SMatthew G. Knepley   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
3175766ab985SMatthew G. Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3176766ab985SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
317727c04023SMatthew G. Knepley   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
3178766ab985SMatthew G. Knepley   /* Setup chart */
3179766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
3180766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
3181766ab985SMatthew G. Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
3182766ab985SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
3183766ab985SMatthew G. Knepley   /* Set cone sizes */
3184766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3185766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3186766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3187766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
3188766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3189766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3190766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
3191766ab985SMatthew G. Knepley   }
3192766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3193766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
3194766ab985SMatthew G. Knepley   }
3195766ab985SMatthew G. Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3196766ab985SMatthew G. Knepley   /* Create face cones */
3197766ab985SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
319887feddfdSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
319969291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3200766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3201766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3202766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
320387feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
320487feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3205766ab985SMatthew G. Knepley 
320687feddfdSMatthew G. Knepley     if (cell < cMax) continue;
320787feddfdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
320887feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
320987feddfdSMatthew G. Knepley       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
321087feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
321187feddfdSMatthew G. Knepley     }
321287feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
321387feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
321487feddfdSMatthew G. Knepley     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
321527234c99SMatthew G. Knepley     /* Not true in parallel
321627234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
321787feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
321887feddfdSMatthew G. Knepley       PetscInt negsubcell;
3219766ab985SMatthew G. Knepley 
322087feddfdSMatthew G. Knepley       if (cells[p] >= cMax) continue;
322187feddfdSMatthew G. Knepley       /* I know this is a crap search */
322287feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
322387feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3224766ab985SMatthew G. Knepley       }
322587feddfdSMatthew G. Knepley       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
322687feddfdSMatthew G. Knepley       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
3227766ab985SMatthew G. Knepley     }
322887feddfdSMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
322987feddfdSMatthew G. Knepley     ++newFacePoint;
3230766ab985SMatthew G. Knepley   }
323169291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3232766ab985SMatthew G. Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3233766ab985SMatthew G. Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3234766ab985SMatthew G. Knepley   /* Build coordinates */
3235766ab985SMatthew G. Knepley   {
3236766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3237766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3238766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3239c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
324024640c55SToby Isaac     const char  *name;
3241766ab985SMatthew G. Knepley 
3242c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
324369d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3244766ab985SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
324569d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3246285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3247285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3248285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3249766ab985SMatthew G. Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3250766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3251766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3252766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3253766ab985SMatthew G. Knepley       PetscInt       dof;
3254766ab985SMatthew G. Knepley 
3255766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3256766ab985SMatthew G. Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3257285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3258766ab985SMatthew G. Knepley     }
3259766ab985SMatthew G. Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3260766ab985SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
32618b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
326224640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
326324640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3264766ab985SMatthew G. Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3265c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
32662eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3267766ab985SMatthew G. Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3268766ab985SMatthew G. Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3269766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3270766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3271766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3272766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3273766ab985SMatthew G. Knepley 
3274766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3275766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3276766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3277766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3278766ab985SMatthew 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);
3279766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3280766ab985SMatthew G. Knepley     }
3281766ab985SMatthew G. Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3282766ab985SMatthew G. Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3283766ab985SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3284766ab985SMatthew G. Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3285766ab985SMatthew G. Knepley   }
3286aca35d17SMatthew G. Knepley   /* Build SF */
3287aca35d17SMatthew G. Knepley   CHKMEMQ;
3288aca35d17SMatthew G. Knepley   {
3289aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3290aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3291bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3292aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3293bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
329449c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3295bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3296aca35d17SMatthew G. Knepley 
3297bdcf2095SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3298aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
3299aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
3300aca35d17SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3301aca35d17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
3302aca35d17SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
3303aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3304aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3305dcca6d9dSJed Brown       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
3306bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3307bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3308bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3309bdcf2095SMatthew G. Knepley       }
33109e0823b2SMatthew G. Knepley       /* Set subleaves */
3311aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3312aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3313bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3314aca35d17SMatthew G. Knepley 
3315aca35d17SMatthew G. Knepley         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3316bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3317bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3318bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3319bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3320aca35d17SMatthew G. Knepley       }
33219e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
33229e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
33239e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
33249e0823b2SMatthew G. Knepley 
33259e0823b2SMatthew G. Knepley         if (subPoint < 0) {
33269e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
33279e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
33289e0823b2SMatthew G. Knepley         } else {
33299e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
33309e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
33319e0823b2SMatthew G. Knepley         }
3332bdcf2095SMatthew G. Knepley       }
3333bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3334bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3335bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3336bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3337785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves,    &slocalPoints);CHKERRQ(ierr);
3338785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves, &sremotePoints);CHKERRQ(ierr);
333949c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3340bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3341bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3342aca35d17SMatthew G. Knepley 
3343aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
334449c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3345aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3346bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3347bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
3348bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3349bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3350aca35d17SMatthew G. Knepley         ++sl;
3351aca35d17SMatthew G. Knepley       }
3352bdcf2095SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
335349c26ae4SMatthew G. Knepley       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
335449c26ae4SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3355aca35d17SMatthew G. Knepley     }
3356aca35d17SMatthew G. Knepley   }
3357aca35d17SMatthew G. Knepley   CHKMEMQ;
3358766ab985SMatthew G. Knepley   /* Cleanup */
3359766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3360766ab985SMatthew G. Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3361766ab985SMatthew G. Knepley   ierr = PetscFree(subCells);CHKERRQ(ierr);
3362766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3363766ab985SMatthew G. Knepley }
3364766ab985SMatthew G. Knepley 
33653982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3366766ab985SMatthew G. Knepley {
33673982b651SMatthew G. Knepley   DMLabel        label = NULL;
3368766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3369766ab985SMatthew G. Knepley 
3370766ab985SMatthew G. Knepley   PetscFunctionBegin;
3371c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
3372bec263e5SMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_TRUE, 1, subdm);CHKERRQ(ierr);
3373766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3374766ab985SMatthew G. Knepley }
3375766ab985SMatthew G. Knepley 
3376c08575a3SMatthew G. Knepley /*@C
337727c04023SMatthew 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.
3378766ab985SMatthew G. Knepley 
3379766ab985SMatthew G. Knepley   Input Parameters:
3380766ab985SMatthew G. Knepley + dm          - The original mesh
338127c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
33827afc1a8bSJed Brown . label       - A label name, or NULL
338327c04023SMatthew G. Knepley - value  - A label value
3384766ab985SMatthew G. Knepley 
3385766ab985SMatthew G. Knepley   Output Parameter:
3386766ab985SMatthew G. Knepley . subdm - The surface mesh
3387766ab985SMatthew G. Knepley 
3388766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3389766ab985SMatthew G. Knepley 
3390766ab985SMatthew G. Knepley   Level: developer
3391766ab985SMatthew G. Knepley 
3392766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3393c08575a3SMatthew G. Knepley @*/
339427c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3395766ab985SMatthew G. Knepley {
3396c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3397766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3398766ab985SMatthew G. Knepley 
3399766ab985SMatthew G. Knepley   PetscFunctionBegin;
3400766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
340127c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3402c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3403766ab985SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3404766ab985SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3405766ab985SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3406c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3407c0e8cf5fSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3408c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3409766ab985SMatthew G. Knepley   if (depth == dim) {
3410b3154360SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr);
3411766ab985SMatthew G. Knepley   } else {
341227c04023SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
3413e6ccafaeSMatthew G Knepley   }
3414e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3415e6ccafaeSMatthew G Knepley }
3416e6ccafaeSMatthew G Knepley 
3417bec263e5SMatthew G. Knepley /*@
3418bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3419bec263e5SMatthew G. Knepley 
3420bec263e5SMatthew G. Knepley   Input Parameters:
3421bec263e5SMatthew G. Knepley + dm        - The original mesh
3422bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3423bec263e5SMatthew G. Knepley - value     - The label value to use
3424bec263e5SMatthew G. Knepley 
3425bec263e5SMatthew G. Knepley   Output Parameter:
3426bec263e5SMatthew G. Knepley . subdm - The new mesh
3427bec263e5SMatthew G. Knepley 
3428bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3429bec263e5SMatthew G. Knepley 
3430bec263e5SMatthew G. Knepley   Level: developer
3431bec263e5SMatthew G. Knepley 
3432c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3433bec263e5SMatthew G. Knepley @*/
3434bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3435bec263e5SMatthew G. Knepley {
3436bec263e5SMatthew G. Knepley   PetscInt       dim;
3437bec263e5SMatthew G. Knepley   PetscErrorCode ierr;
3438bec263e5SMatthew G. Knepley 
3439bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3440bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3441bec263e5SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3442bec263e5SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3443bec263e5SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), subdm);CHKERRQ(ierr);
3444bec263e5SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3445bec263e5SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim);CHKERRQ(ierr);
3446bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3447bec263e5SMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, 0, *subdm);CHKERRQ(ierr);
3448bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3449bec263e5SMatthew G. Knepley }
3450bec263e5SMatthew G. Knepley 
345164beef6dSMatthew G. Knepley /*@
345264beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
345364beef6dSMatthew G. Knepley 
345464beef6dSMatthew G. Knepley   Input Parameter:
345564beef6dSMatthew G. Knepley . dm - The submesh DM
345664beef6dSMatthew G. Knepley 
345764beef6dSMatthew G. Knepley   Output Parameter:
345864beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
345964beef6dSMatthew G. Knepley 
346064beef6dSMatthew G. Knepley   Level: developer
346164beef6dSMatthew G. Knepley 
346264beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
346364beef6dSMatthew G. Knepley @*/
3464e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3465e6ccafaeSMatthew G Knepley {
3466e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3467e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3468e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
346965663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3470e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3471e6ccafaeSMatthew G Knepley }
3472e6ccafaeSMatthew G Knepley 
3473c08575a3SMatthew G. Knepley /*@
3474c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3475c08575a3SMatthew G. Knepley 
3476c08575a3SMatthew G. Knepley   Input Parameters:
3477c08575a3SMatthew G. Knepley + dm - The submesh DM
3478c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3479c08575a3SMatthew G. Knepley 
3480c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3481c08575a3SMatthew G. Knepley 
3482c08575a3SMatthew G. Knepley   Level: developer
3483c08575a3SMatthew G. Knepley 
3484c08575a3SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3485c08575a3SMatthew G. Knepley @*/
3486e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3487e6ccafaeSMatthew G Knepley {
3488e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3489285d324eSMatthew G. Knepley   DMLabel        tmp;
3490e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3491e6ccafaeSMatthew G Knepley 
3492e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3493e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3494285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3495e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3496285d324eSMatthew G. Knepley   ++mesh->subpointMap->refct;
3497285d324eSMatthew G. Knepley   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
3498e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3499e6ccafaeSMatthew G Knepley }
3500e6ccafaeSMatthew G Knepley 
350164beef6dSMatthew G. Knepley /*@
3502e6ccafaeSMatthew G Knepley   DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data
3503e6ccafaeSMatthew G Knepley 
3504e6ccafaeSMatthew G Knepley   Input Parameter:
3505e6ccafaeSMatthew G Knepley . dm - The submesh DM
3506e6ccafaeSMatthew G Knepley 
3507e6ccafaeSMatthew G Knepley   Output Parameter:
35080298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3509e6ccafaeSMatthew G Knepley 
35103982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
351164beef6dSMatthew G. Knepley 
351264beef6dSMatthew G. Knepley   Level: developer
351364beef6dSMatthew G. Knepley 
351464beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
351564beef6dSMatthew G. Knepley @*/
3516e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3517e6ccafaeSMatthew G Knepley {
351882f516ccSBarry Smith   MPI_Comm        comm;
3519e6ccafaeSMatthew G Knepley   DMLabel         subpointMap;
3520e6ccafaeSMatthew G Knepley   IS              is;
3521e6ccafaeSMatthew G Knepley   const PetscInt *opoints;
3522e6ccafaeSMatthew G Knepley   PetscInt       *points, *depths;
3523e6ccafaeSMatthew G Knepley   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3524e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
3525e6ccafaeSMatthew G Knepley 
3526e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3527e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3528e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
352982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
35300298fd71SBarry Smith   *subpointIS = NULL;
3531e6ccafaeSMatthew G Knepley   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
3532e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3533fed694aaSMatthew G. Knepley   if (subpointMap && depth >= 0) {
3534e6ccafaeSMatthew G Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3535e6ccafaeSMatthew G Knepley     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
353669291d52SBarry Smith     ierr = DMGetWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3537e6ccafaeSMatthew G Knepley     depths[0] = depth;
3538e6ccafaeSMatthew G Knepley     depths[1] = 0;
3539e6ccafaeSMatthew G Knepley     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3540785e854fSJed Brown     ierr = PetscMalloc1(pEnd, &points);CHKERRQ(ierr);
3541e6ccafaeSMatthew G Knepley     for(d = 0, off = 0; d <= depth; ++d) {
3542e6ccafaeSMatthew G Knepley       const PetscInt dep = depths[d];
3543e6ccafaeSMatthew G Knepley 
3544e6ccafaeSMatthew G Knepley       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
3545e6ccafaeSMatthew G Knepley       ierr = DMLabelGetStratumSize(subpointMap, dep, &n);CHKERRQ(ierr);
35461e21b6fdSMatthew G Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3547e6ccafaeSMatthew 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);
3548e6ccafaeSMatthew G Knepley       } else {
35491e21b6fdSMatthew G Knepley         if (!n) {
35501e21b6fdSMatthew G Knepley           if (d == 0) {
35511e21b6fdSMatthew G Knepley             /* Missing cells */
35521e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
35531e21b6fdSMatthew G Knepley           } else {
35541e21b6fdSMatthew G Knepley             /* Missing faces */
35551e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
35561e21b6fdSMatthew G Knepley           }
35571e21b6fdSMatthew G Knepley         }
3558e6ccafaeSMatthew G Knepley       }
3559e6ccafaeSMatthew G Knepley       if (n) {
3560e6ccafaeSMatthew G Knepley         ierr = DMLabelGetStratumIS(subpointMap, dep, &is);CHKERRQ(ierr);
3561e6ccafaeSMatthew G Knepley         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
3562e6ccafaeSMatthew G Knepley         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3563e6ccafaeSMatthew G Knepley         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
3564e6ccafaeSMatthew G Knepley         ierr = ISDestroy(&is);CHKERRQ(ierr);
3565e6ccafaeSMatthew G Knepley       }
3566e6ccafaeSMatthew G Knepley     }
356769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3568e6ccafaeSMatthew G Knepley     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3569fed694aaSMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
3570e6ccafaeSMatthew G Knepley   }
3571e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3572e6ccafaeSMatthew G Knepley }
3573*559a1558SMatthew G. Knepley 
3574*559a1558SMatthew G. Knepley /*@
3575*559a1558SMatthew G. Knepley   DMPlexGetSubpoint - Return the subpoint corresponding to a point in the original mesh. If the DM
3576*559a1558SMatthew G. Knepley                       is not a submesh, just return the input point.
3577*559a1558SMatthew G. Knepley 
3578*559a1558SMatthew G. Knepley   Note collective
3579*559a1558SMatthew G. Knepley 
3580*559a1558SMatthew G. Knepley   Input Parameters:
3581*559a1558SMatthew G. Knepley + dm - The submesh DM
3582*559a1558SMatthew G. Knepley - p  - The point in the original, from which the submesh was created
3583*559a1558SMatthew G. Knepley 
3584*559a1558SMatthew G. Knepley   Output Parameter:
3585*559a1558SMatthew G. Knepley . subp - The point in the submesh
3586*559a1558SMatthew G. Knepley 
3587*559a1558SMatthew G. Knepley   Level: developer
3588*559a1558SMatthew G. Knepley 
3589*559a1558SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap(), DMPlexCreateSubpointIS()
3590*559a1558SMatthew G. Knepley @*/
3591*559a1558SMatthew G. Knepley PetscErrorCode DMPlexGetSubpoint(DM dm, PetscInt p, PetscInt *subp)
3592*559a1558SMatthew G. Knepley {
3593*559a1558SMatthew G. Knepley   DMLabel        spmap;
3594*559a1558SMatthew G. Knepley   PetscErrorCode ierr;
3595*559a1558SMatthew G. Knepley 
3596*559a1558SMatthew G. Knepley   *subp = p;
3597*559a1558SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
3598*559a1558SMatthew G. Knepley   if (spmap) {
3599*559a1558SMatthew G. Knepley     IS              subpointIS;
3600*559a1558SMatthew G. Knepley     const PetscInt *subpoints;
3601*559a1558SMatthew G. Knepley     PetscInt        numSubpoints;
3602*559a1558SMatthew G. Knepley 
3603*559a1558SMatthew G. Knepley     /* TODO Cache the IS, making it look like an index */
3604*559a1558SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(dm, &subpointIS);CHKERRQ(ierr);
3605*559a1558SMatthew G. Knepley     ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
3606*559a1558SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3607*559a1558SMatthew G. Knepley     ierr = PetscFindInt(p, numSubpoints, subpoints, subp);CHKERRQ(ierr);
3608*559a1558SMatthew G. Knepley     if (*subp < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", p);
3609*559a1558SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3610*559a1558SMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
3611*559a1558SMatthew G. Knepley   }
3612*559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3613*559a1558SMatthew G. Knepley }
3614