xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision a6e0b375ecc2eaaf9144b397944953a8bc18461c)
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   ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr);
1230560a7bSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1330560a7bSMatthew G. Knepley     PetscInt supportSize;
1430560a7bSMatthew G. Knepley 
1530560a7bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
16e752be1aSMatthew G. Knepley     if (supportSize == 1) {
17e752be1aSMatthew G. Knepley       if (val < 0) {
18e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
19e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
20e752be1aSMatthew G. Knepley 
21e752be1aSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
22e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize*2; cl += 2) {
23e752be1aSMatthew G. Knepley           ierr = DMLabelGetValue(label, closure[cl], &cval);CHKERRQ(ierr);
24e752be1aSMatthew G. Knepley           if (cval < 0) continue;
25e752be1aSMatthew G. Knepley           ierr = DMLabelSetValue(label, f, cval);CHKERRQ(ierr);
26e752be1aSMatthew G. Knepley           break;
27e752be1aSMatthew G. Knepley         }
28e752be1aSMatthew G. Knepley         if (cl == clSize*2) {ierr = DMLabelSetValue(label, f, 1);CHKERRQ(ierr);}
29e752be1aSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
30e752be1aSMatthew G. Knepley       } else {
31e752be1aSMatthew G. Knepley         ierr = DMLabelSetValue(label, f, val);CHKERRQ(ierr);
32e752be1aSMatthew G. Knepley       }
33e752be1aSMatthew G. Knepley     }
3430560a7bSMatthew G. Knepley   }
3530560a7bSMatthew G. Knepley   PetscFunctionReturn(0);
3630560a7bSMatthew G. Knepley }
3730560a7bSMatthew G. Knepley 
38cd0c2139SMatthew G Knepley /*@
39cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
40cd0c2139SMatthew G Knepley 
41cd0c2139SMatthew G Knepley   Not Collective
42cd0c2139SMatthew G Knepley 
43cd0c2139SMatthew G Knepley   Input Parameter:
44e752be1aSMatthew G. Knepley + dm - The original DM
45e752be1aSMatthew G. Knepley - val - The marker value, or PETSC_DETERMINE to use some value in the closure (or 1 if none are found)
46cd0c2139SMatthew G Knepley 
47cd0c2139SMatthew G Knepley   Output Parameter:
48e752be1aSMatthew G. Knepley . label - The DMLabel marking boundary faces with the given value
49cd0c2139SMatthew G Knepley 
50cd0c2139SMatthew G Knepley   Level: developer
51cd0c2139SMatthew G Knepley 
52c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMCreateLabel()
5309f723d9SJed Brown @*/
54e752be1aSMatthew G. Knepley PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
55cd0c2139SMatthew G Knepley {
56827c4036SVaclav Hapla   DMPlexInterpolatedFlag  flg;
57cd0c2139SMatthew G Knepley   PetscErrorCode          ierr;
58cd0c2139SMatthew G Knepley 
59cd0c2139SMatthew G Knepley   PetscFunctionBegin;
60827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61827c4036SVaclav Hapla   ierr = DMPlexIsInterpolated(dm, &flg);CHKERRQ(ierr);
62827c4036SVaclav Hapla   if (flg != DMPLEX_INTERPOLATED_FULL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
63e752be1aSMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces_Internal(dm, val, 0, label);CHKERRQ(ierr);
64cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
65cd0c2139SMatthew G Knepley }
66cd0c2139SMatthew G Knepley 
67c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
68b0bf5782SToby Isaac {
69b0bf5782SToby Isaac   IS              valueIS;
70ac51f24eSSander Arens   PetscSF         sfPoint;
71b0bf5782SToby Isaac   const PetscInt *values;
72ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
73b0bf5782SToby Isaac   PetscErrorCode  ierr;
74b0bf5782SToby Isaac 
75b0bf5782SToby Isaac   PetscFunctionBegin;
76b0bf5782SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
77b0bf5782SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
78b0bf5782SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
79b0bf5782SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
80b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
81b0bf5782SToby Isaac     IS              pointIS;
82b0bf5782SToby Isaac     const PetscInt *points;
83b0bf5782SToby Isaac     PetscInt        numPoints, p;
84b0bf5782SToby Isaac 
85b0bf5782SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
86b0bf5782SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
87b0bf5782SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
88b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
89b0bf5782SToby Isaac       PetscInt  q = points[p];
90b0bf5782SToby Isaac       PetscInt *closure = NULL;
91b0bf5782SToby Isaac       PetscInt  closureSize, c;
92b0bf5782SToby Isaac 
93b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
94b0bf5782SToby Isaac         continue;
95b0bf5782SToby Isaac       }
96b0bf5782SToby Isaac       ierr = DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
97b0bf5782SToby Isaac       for (c = 0; c < closureSize*2; c += 2) {
98b0bf5782SToby Isaac         ierr = DMLabelSetValue(label, closure[c], values[v]);CHKERRQ(ierr);
99b0bf5782SToby Isaac       }
100b0bf5782SToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
101b0bf5782SToby Isaac     }
102b0bf5782SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
103b0bf5782SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
104b0bf5782SToby Isaac   }
105b0bf5782SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
106b0bf5782SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
107ac51f24eSSander Arens   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
108ac51f24eSSander Arens   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
109ac51f24eSSander Arens   if (nroots >= 0) {
11026279d81SSanderA     DMLabel         lblRoots, lblLeaves;
11126279d81SSanderA     IS              valueIS, pointIS;
11226279d81SSanderA     const PetscInt *values;
11326279d81SSanderA     PetscInt        numValues, v;
11426279d81SSanderA     PetscErrorCode  ierr;
11526279d81SSanderA 
11626279d81SSanderA     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
11726279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
11826279d81SSanderA     ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr);
11926279d81SSanderA     ierr = DMLabelGetValueIS(lblLeaves, &valueIS);CHKERRQ(ierr);
12026279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
12126279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
12226279d81SSanderA     for (v = 0; v < numValues; ++v) {
12326279d81SSanderA       const PetscInt value = values[v];
12426279d81SSanderA 
12526279d81SSanderA       ierr = DMLabelGetStratumIS(lblLeaves, value, &pointIS);CHKERRQ(ierr);
12626279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
12726279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
12826279d81SSanderA     }
12926279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
13026279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
13126279d81SSanderA     ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr);
13226279d81SSanderA     /* Push point contributions from roots into remote leaves */
13326279d81SSanderA     ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr);
13426279d81SSanderA     ierr = DMLabelGetValueIS(lblRoots, &valueIS);CHKERRQ(ierr);
13526279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
13626279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
13726279d81SSanderA     for (v = 0; v < numValues; ++v) {
13826279d81SSanderA       const PetscInt value = values[v];
13926279d81SSanderA 
14026279d81SSanderA       ierr = DMLabelGetStratumIS(lblRoots, value, &pointIS);CHKERRQ(ierr);
14126279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
14226279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
14326279d81SSanderA     }
14426279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
14526279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
14626279d81SSanderA     ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr);
14726279d81SSanderA   }
148b0bf5782SToby Isaac   PetscFunctionReturn(0);
149b0bf5782SToby Isaac }
150b0bf5782SToby Isaac 
1512be2b188SMatthew G Knepley /*@
1522be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
1532be2b188SMatthew G Knepley 
1542be2b188SMatthew G Knepley   Input Parameters:
1552be2b188SMatthew G Knepley + dm - The DM
1562be2b188SMatthew G Knepley - label - A DMLabel marking the surface points
1572be2b188SMatthew G Knepley 
1582be2b188SMatthew G Knepley   Output Parameter:
1592be2b188SMatthew G Knepley . label - A DMLabel marking all surface points in the transitive closure
1602be2b188SMatthew G Knepley 
1612be2b188SMatthew G Knepley   Level: developer
1622be2b188SMatthew G Knepley 
1632be2b188SMatthew G Knepley .seealso: DMPlexLabelCohesiveComplete()
1642be2b188SMatthew G Knepley @*/
1652be2b188SMatthew G Knepley PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
1662be2b188SMatthew G Knepley {
1672be2b188SMatthew G Knepley   PetscErrorCode ierr;
1682be2b188SMatthew G Knepley 
1692be2b188SMatthew G Knepley   PetscFunctionBegin;
170b0bf5782SToby Isaac   ierr = DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE);CHKERRQ(ierr);
1712be2b188SMatthew G Knepley   PetscFunctionReturn(0);
1722be2b188SMatthew G Knepley }
1732be2b188SMatthew G Knepley 
1746cf0e42fSMatthew G. Knepley /*@
175*a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
1766cf0e42fSMatthew G. Knepley 
1776cf0e42fSMatthew G. Knepley   Input Parameters:
1786cf0e42fSMatthew G. Knepley + dm - The DM
1796cf0e42fSMatthew G. Knepley - label - A DMLabel marking the surface points
1806cf0e42fSMatthew G. Knepley 
1816cf0e42fSMatthew G. Knepley   Output Parameter:
1826cf0e42fSMatthew G. Knepley . label - A DMLabel incorporating cells
1836cf0e42fSMatthew G. Knepley 
1846cf0e42fSMatthew G. Knepley   Level: developer
1856cf0e42fSMatthew G. Knepley 
1866cf0e42fSMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
1876cf0e42fSMatthew G. Knepley 
188*a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddFaceCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
1896cf0e42fSMatthew G. Knepley @*/
1906cf0e42fSMatthew G. Knepley PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
1916cf0e42fSMatthew G. Knepley {
1926cf0e42fSMatthew G. Knepley   IS              valueIS;
1936cf0e42fSMatthew G. Knepley   const PetscInt *values;
194485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
1956cf0e42fSMatthew G. Knepley   PetscErrorCode  ierr;
1966cf0e42fSMatthew G. Knepley 
1976cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
198485ad865SMatthew G. Knepley   ierr = DMPlexGetInteriorCellStratum(dm, &cStart, &cEnd);CHKERRQ(ierr);
1996cf0e42fSMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
2006cf0e42fSMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
2016cf0e42fSMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
2026cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2036cf0e42fSMatthew G. Knepley     IS              pointIS;
2046cf0e42fSMatthew G. Knepley     const PetscInt *points;
2056cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2066cf0e42fSMatthew G. Knepley 
2076cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
2086cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2096cf0e42fSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2106cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2116cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
212*a6e0b375SMatthew G. Knepley       PetscInt  closureSize, cl;
2136cf0e42fSMatthew G. Knepley 
2146cf0e42fSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
21522eabd52SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
216*a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl*2];
217*a6e0b375SMatthew G. Knepley         if ((cell >= cStart) && (cell < cEnd)) {ierr = DMLabelSetValue(label, cell, values[v]);CHKERRQ(ierr); break;}
21822eabd52SMatthew G. Knepley       }
2196cf0e42fSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
2206cf0e42fSMatthew G. Knepley     }
2216cf0e42fSMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2226cf0e42fSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2236cf0e42fSMatthew G. Knepley   }
2246cf0e42fSMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
2256cf0e42fSMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
2266cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2276cf0e42fSMatthew G. Knepley }
2286cf0e42fSMatthew G. Knepley 
229f402d5e4SToby Isaac /*@
230*a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
231*a6e0b375SMatthew G. Knepley 
232*a6e0b375SMatthew G. Knepley   Input Parameters:
233*a6e0b375SMatthew G. Knepley + dm - The DM
234*a6e0b375SMatthew G. Knepley - label - A DMLabel marking the surface points
235*a6e0b375SMatthew G. Knepley 
236*a6e0b375SMatthew G. Knepley   Output Parameter:
237*a6e0b375SMatthew G. Knepley . label - A DMLabel incorporating cells
238*a6e0b375SMatthew G. Knepley 
239*a6e0b375SMatthew G. Knepley   Level: developer
240*a6e0b375SMatthew G. Knepley 
241*a6e0b375SMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
242*a6e0b375SMatthew G. Knepley 
243*a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
244*a6e0b375SMatthew G. Knepley @*/
245*a6e0b375SMatthew G. Knepley PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
246*a6e0b375SMatthew G. Knepley {
247*a6e0b375SMatthew G. Knepley   IS              valueIS;
248*a6e0b375SMatthew G. Knepley   const PetscInt *values;
249*a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
250*a6e0b375SMatthew G. Knepley   PetscErrorCode  ierr;
251*a6e0b375SMatthew G. Knepley 
252*a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
253*a6e0b375SMatthew G. Knepley   ierr = DMPlexGetInteriorCellStratum(dm, &cStart, &cEnd);CHKERRQ(ierr);
254*a6e0b375SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
255*a6e0b375SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
256*a6e0b375SMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
257*a6e0b375SMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
258*a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
259*a6e0b375SMatthew G. Knepley     IS              pointIS;
260*a6e0b375SMatthew G. Knepley     const PetscInt *points;
261*a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
262*a6e0b375SMatthew G. Knepley 
263*a6e0b375SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
264*a6e0b375SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
265*a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
266*a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
267*a6e0b375SMatthew G. Knepley       const PetscInt face = points[p];
268*a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
269*a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
270*a6e0b375SMatthew G. Knepley 
271*a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
272*a6e0b375SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
273*a6e0b375SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
274*a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl*2];
275*a6e0b375SMatthew G. Knepley         if ((cell >= cStart) && (cell < cEnd)) {ierr = DMLabelSetValue(label, cell, values[v]);CHKERRQ(ierr); break;}
276*a6e0b375SMatthew G. Knepley       }
277*a6e0b375SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
278*a6e0b375SMatthew G. Knepley     }
279*a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
280*a6e0b375SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
281*a6e0b375SMatthew G. Knepley   }
282*a6e0b375SMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
283*a6e0b375SMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
284*a6e0b375SMatthew G. Knepley   PetscFunctionReturn(0);
285*a6e0b375SMatthew G. Knepley }
286*a6e0b375SMatthew G. Knepley 
287*a6e0b375SMatthew G. Knepley /*@
288f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
289f402d5e4SToby Isaac 
290f402d5e4SToby Isaac   Input Parameters:
291f402d5e4SToby Isaac + dm - The DM
292f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
293f402d5e4SToby Isaac 
294f402d5e4SToby Isaac   Output Parameter:
295f402d5e4SToby Isaac . label - A DMLabel without cells
296f402d5e4SToby Isaac 
297f402d5e4SToby Isaac   Level: developer
298f402d5e4SToby Isaac 
299*a6e0b375SMatthew G. Knepley   Note: This undoes DMPlexLabelAddCells() or DMPlexLabelAddFaceCells()
300f402d5e4SToby Isaac 
301f402d5e4SToby Isaac .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
302f402d5e4SToby Isaac @*/
303f402d5e4SToby Isaac PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
304f402d5e4SToby Isaac {
305f402d5e4SToby Isaac   IS              valueIS;
306f402d5e4SToby Isaac   const PetscInt *values;
307485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
308f402d5e4SToby Isaac   PetscErrorCode  ierr;
309f402d5e4SToby Isaac 
310f402d5e4SToby Isaac   PetscFunctionBegin;
311485ad865SMatthew G. Knepley   ierr = DMPlexGetInteriorCellStratum(dm, &cStart, &cEnd);CHKERRQ(ierr);
312f402d5e4SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
313f402d5e4SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
314f402d5e4SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
315f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
316f402d5e4SToby Isaac     IS              pointIS;
317f402d5e4SToby Isaac     const PetscInt *points;
318f402d5e4SToby Isaac     PetscInt        numPoints, p;
319f402d5e4SToby Isaac 
320f402d5e4SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
321f402d5e4SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
322f402d5e4SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
323f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
324f402d5e4SToby Isaac       PetscInt point = points[p];
325f402d5e4SToby Isaac 
326f402d5e4SToby Isaac       if (point >= cStart && point < cEnd) {
327f402d5e4SToby Isaac         ierr = DMLabelClearValue(label,point,values[v]);CHKERRQ(ierr);
328f402d5e4SToby Isaac       }
329f402d5e4SToby Isaac     }
330f402d5e4SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
331f402d5e4SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
332f402d5e4SToby Isaac   }
333f402d5e4SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
334f402d5e4SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
335f402d5e4SToby Isaac   PetscFunctionReturn(0);
336f402d5e4SToby Isaac }
337f402d5e4SToby Isaac 
33859eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
33959eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
3402582d50cSToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
341cd0c2139SMatthew G Knepley {
3422582d50cSToby Isaac   PetscInt d, off = 0;
3432582d50cSToby Isaac 
3442582d50cSToby Isaac   PetscFunctionBegin;
34559eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
3460974a383SToby Isaac   for (d = 0; d < depth; d++) {
3472582d50cSToby Isaac     PetscInt firstd = d;
3480974a383SToby Isaac     PetscInt firstStart = depthShift[2*d];
3492582d50cSToby Isaac     PetscInt e;
3502582d50cSToby Isaac 
3512582d50cSToby Isaac     for (e = d+1; e <= depth; e++) {
3522582d50cSToby Isaac       if (depthShift[2*e] < firstStart) {
3532582d50cSToby Isaac         firstd = e;
3542582d50cSToby Isaac         firstStart = depthShift[2*d];
3552582d50cSToby Isaac       }
3562582d50cSToby Isaac     }
3572582d50cSToby Isaac     if (firstd != d) {
3582582d50cSToby Isaac       PetscInt swap[2];
3592582d50cSToby Isaac 
3602582d50cSToby Isaac       e = firstd;
3612582d50cSToby Isaac       swap[0] = depthShift[2*d];
3622582d50cSToby Isaac       swap[1] = depthShift[2*d+1];
3632582d50cSToby Isaac       depthShift[2*d]   = depthShift[2*e];
3642582d50cSToby Isaac       depthShift[2*d+1] = depthShift[2*e+1];
3652582d50cSToby Isaac       depthShift[2*e]   = swap[0];
3662582d50cSToby Isaac       depthShift[2*e+1] = swap[1];
3672582d50cSToby Isaac     }
3682582d50cSToby Isaac   }
3692582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
3700974a383SToby Isaac   for (d = 0; d <= depth; d++) {
3712582d50cSToby Isaac     off += depthShift[2*d+1];
37259eef20bSToby Isaac     depthShift[2*d+1] = depthShift[2*d] + off;
3732582d50cSToby Isaac   }
3742582d50cSToby Isaac   PetscFunctionReturn(0);
3752582d50cSToby Isaac }
3762582d50cSToby Isaac 
3772582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3782582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3792582d50cSToby Isaac {
3802582d50cSToby Isaac   PetscInt d;
3812582d50cSToby Isaac   PetscInt newOff = 0;
3822582d50cSToby Isaac 
3832582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3842582d50cSToby Isaac     if (p < depthShift[2*d]) return p + newOff;
3852582d50cSToby Isaac     else newOff = depthShift[2*d+1] - depthShift[2*d];
3862582d50cSToby Isaac   }
3870974a383SToby Isaac   return p + newOff;
3882582d50cSToby Isaac }
3892582d50cSToby Isaac 
3902582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3912582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3922582d50cSToby Isaac {
3932582d50cSToby Isaac   PetscInt d;
3942582d50cSToby Isaac   PetscInt newOff = 0;
3952582d50cSToby Isaac 
3962582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3972582d50cSToby Isaac     if (p < depthShift[2*d+1]) return p + newOff;
3982582d50cSToby Isaac     else newOff = depthShift[2*d] - depthShift[2*d+1];
3992582d50cSToby Isaac   }
4000974a383SToby Isaac   return p + newOff;
401cd0c2139SMatthew G Knepley }
402cd0c2139SMatthew G Knepley 
403cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
404cd0c2139SMatthew G Knepley {
405cd0c2139SMatthew G Knepley   PetscInt       depth = 0, d, pStart, pEnd, p;
406fa8e8ae5SToby Isaac   DMLabel        depthLabel;
407cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
408cd0c2139SMatthew G Knepley 
409cd0c2139SMatthew G Knepley   PetscFunctionBegin;
410cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
411cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
412cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
413cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
4140974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
415cd0c2139SMatthew G Knepley   ierr = DMPlexSetChart(dmNew, pStart, pEnd);CHKERRQ(ierr);
416fa8e8ae5SToby Isaac   ierr = DMCreateLabel(dmNew,"depth");CHKERRQ(ierr);
417fa8e8ae5SToby Isaac   ierr = DMPlexGetDepthLabel(dmNew,&depthLabel);CHKERRQ(ierr);
418cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
419cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
420fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
421fa8e8ae5SToby Isaac     IS pIS;
422fa8e8ae5SToby Isaac 
423cd0c2139SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
424fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
425fa8e8ae5SToby Isaac     pEndNew = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
426fa8e8ae5SToby Isaac     ierr = ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS);CHKERRQ(ierr);
427fa8e8ae5SToby Isaac     ierr = DMLabelSetStratumIS(depthLabel, d, pIS);CHKERRQ(ierr);
428fa8e8ae5SToby Isaac     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
429cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
4302582d50cSToby Isaac       PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
431cd0c2139SMatthew G Knepley       PetscInt size;
432cd0c2139SMatthew G Knepley 
433cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
434cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(dmNew, newp, size);CHKERRQ(ierr);
435cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
436cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(dmNew, newp, size);CHKERRQ(ierr);
437cd0c2139SMatthew G Knepley     }
438cd0c2139SMatthew G Knepley   }
439cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
440cd0c2139SMatthew G Knepley }
441cd0c2139SMatthew G Knepley 
442cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
443cd0c2139SMatthew G Knepley {
4442582d50cSToby Isaac   PetscInt      *newpoints;
4452582d50cSToby Isaac   PetscInt       depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
446cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
447cd0c2139SMatthew G Knepley 
448cd0c2139SMatthew G Knepley   PetscFunctionBegin;
449cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
450cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
451cd0c2139SMatthew G Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
452dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
4532582d50cSToby Isaac   ierr = PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints);CHKERRQ(ierr);
454cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
455cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
456cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
457cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
4582582d50cSToby Isaac     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
459cd0c2139SMatthew G Knepley 
460cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
461cd0c2139SMatthew G Knepley     ierr = DMPlexGetCone(dm, p, &points);CHKERRQ(ierr);
462cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeOrientation(dm, p, &orientations);CHKERRQ(ierr);
463cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4642582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
465cd0c2139SMatthew G Knepley     }
466cd0c2139SMatthew G Knepley     ierr = DMPlexSetCone(dmNew, newp, newpoints);CHKERRQ(ierr);
467cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dmNew, newp, orientations);CHKERRQ(ierr);
468cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
469dcbb62e8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmNew, newp, &sizeNew);CHKERRQ(ierr);
470cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, p, &points);CHKERRQ(ierr);
471cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4722582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
473cd0c2139SMatthew G Knepley     }
474dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
475cd0c2139SMatthew G Knepley     ierr = DMPlexSetSupport(dmNew, newp, newpoints);CHKERRQ(ierr);
476cd0c2139SMatthew G Knepley   }
4772582d50cSToby Isaac   ierr = PetscFree(newpoints);CHKERRQ(ierr);
478cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
479cd0c2139SMatthew G Knepley }
480cd0c2139SMatthew G Knepley 
481cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
482cd0c2139SMatthew G Knepley {
483cd0c2139SMatthew G Knepley   PetscSection   coordSection, newCoordSection;
484cd0c2139SMatthew G Knepley   Vec            coordinates, newCoordinates;
485cd0c2139SMatthew G Knepley   PetscScalar   *coords, *newCoords;
486f2b8cce1SMatthew G. Knepley   PetscInt       coordSize, sStart, sEnd;
487f2b8cce1SMatthew G. Knepley   PetscInt       dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
488f2b8cce1SMatthew G. Knepley   PetscBool      hasCells;
489cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
490cd0c2139SMatthew G Knepley 
491cd0c2139SMatthew G Knepley   PetscFunctionBegin;
492f2b8cce1SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
493c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(dmNew, dim);CHKERRQ(ierr);
494cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
495cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
496cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
497f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
498cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);CHKERRQ(ierr);
499f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew);CHKERRQ(ierr);
50069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
501cd0c2139SMatthew G Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);CHKERRQ(ierr);
502cd0c2139SMatthew G Knepley   ierr = PetscSectionSetNumFields(newCoordSection, 1);CHKERRQ(ierr);
503cd0c2139SMatthew G Knepley   ierr = PetscSectionSetFieldComponents(newCoordSection, 0, dim);CHKERRQ(ierr);
504f2b8cce1SMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
505f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
506f2b8cce1SMatthew G. Knepley   ierr = PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew);CHKERRQ(ierr);
507f2b8cce1SMatthew G. Knepley   if (hasCells) {
508f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
509f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
510f2b8cce1SMatthew G. Knepley 
511f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
512f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetDof(newCoordSection, cNew, dof);CHKERRQ(ierr);
513f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof);CHKERRQ(ierr);
514f2b8cce1SMatthew G. Knepley     }
515f2b8cce1SMatthew G. Knepley   }
516cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
517cd0c2139SMatthew G Knepley     ierr = PetscSectionSetDof(newCoordSection, v, dim);CHKERRQ(ierr);
518cd0c2139SMatthew G Knepley     ierr = PetscSectionSetFieldDof(newCoordSection, v, 0, dim);CHKERRQ(ierr);
519cd0c2139SMatthew G Knepley   }
520cd0c2139SMatthew G Knepley   ierr = PetscSectionSetUp(newCoordSection);CHKERRQ(ierr);
52146e270d4SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection);CHKERRQ(ierr);
522cd0c2139SMatthew G Knepley   ierr = PetscSectionGetStorageSize(newCoordSection, &coordSize);CHKERRQ(ierr);
5238b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
524cd0c2139SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) newCoordinates, "coordinates");CHKERRQ(ierr);
525cd0c2139SMatthew G Knepley   ierr = VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
5264e90ef8eSMatthew G. Knepley   ierr = VecSetBlockSize(newCoordinates, dim);CHKERRQ(ierr);
5272eb5907fSJed Brown   ierr = VecSetType(newCoordinates,VECSTANDARD);CHKERRQ(ierr);
528cd0c2139SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dmNew, newCoordinates);CHKERRQ(ierr);
529cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
530cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
531cd0c2139SMatthew G Knepley   ierr = VecGetArray(newCoordinates, &newCoords);CHKERRQ(ierr);
532f2b8cce1SMatthew G. Knepley   if (hasCells) {
533f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
534f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
535f2b8cce1SMatthew G. Knepley 
536f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
537f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, c, &off);CHKERRQ(ierr);
538f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(newCoordSection, cNew, &noff);CHKERRQ(ierr);
539f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
540f2b8cce1SMatthew G. Knepley     }
541f2b8cce1SMatthew G. Knepley   }
542cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
543cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
544cd0c2139SMatthew G Knepley 
545cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
546cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
5472582d50cSToby Isaac     ierr = PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff);CHKERRQ(ierr);
548f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
549cd0c2139SMatthew G Knepley   }
550cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
551cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(newCoordinates, &newCoords);CHKERRQ(ierr);
552cd0c2139SMatthew G Knepley   ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
553cd0c2139SMatthew G Knepley   ierr = PetscSectionDestroy(&newCoordSection);CHKERRQ(ierr);
554cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
555cd0c2139SMatthew G Knepley }
556cd0c2139SMatthew G Knepley 
557cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
558cd0c2139SMatthew G Knepley {
5592582d50cSToby Isaac   PetscInt           depth = 0;
560cd0c2139SMatthew G Knepley   PetscSF            sfPoint, sfPointNew;
561cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
562cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
563cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
564cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
565cd0c2139SMatthew G Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, totShift = 0;
566cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
567cd0c2139SMatthew G Knepley 
568cd0c2139SMatthew G Knepley   PetscFunctionBegin;
569cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
570cd0c2139SMatthew G Knepley   /* Step 9: Convert pointSF */
571cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
572cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dmNew, &sfPointNew);CHKERRQ(ierr);
573cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
574cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
5756e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd,depth,depthShift) - pEnd;
576cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
577dcca6d9dSJed Brown     ierr = PetscMalloc2(numRoots,&newLocation,pEnd-pStart,&newRemoteLocation);CHKERRQ(ierr);
5782582d50cSToby Isaac     for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
579cd0c2139SMatthew G Knepley     ierr = PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
580cd0c2139SMatthew G Knepley     ierr = PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
581785e854fSJed Brown     ierr = PetscMalloc1(numLeaves,    &glocalPoints);CHKERRQ(ierr);
582785e854fSJed Brown     ierr = PetscMalloc1(numLeaves, &gremotePoints);CHKERRQ(ierr);
583cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
5842582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
585cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
586cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
587cd0c2139SMatthew G Knepley     }
588cd0c2139SMatthew G Knepley     ierr = PetscFree2(newLocation,newRemoteLocation);CHKERRQ(ierr);
589cd0c2139SMatthew G Knepley     ierr = PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
590cd0c2139SMatthew G Knepley   }
591cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
592cd0c2139SMatthew G Knepley }
593cd0c2139SMatthew G Knepley 
594cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
595cd0c2139SMatthew G Knepley {
596cd0c2139SMatthew G Knepley   PetscSF            sfPoint;
597cd0c2139SMatthew G Knepley   DMLabel            vtkLabel, ghostLabel;
598cd0c2139SMatthew G Knepley   const PetscSFNode *leafRemote;
599cd0c2139SMatthew G Knepley   const PetscInt    *leafLocal;
6002582d50cSToby Isaac   PetscInt           depth = 0, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
601cd0c2139SMatthew G Knepley   PetscMPIInt        rank;
602cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
603cd0c2139SMatthew G Knepley 
604cd0c2139SMatthew G Knepley   PetscFunctionBegin;
605cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
606cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
607c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
608cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
609cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
610cd0c2139SMatthew G Knepley     const char     *lname;
611fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
612cd0c2139SMatthew G Knepley     IS              valueIS;
613cd0c2139SMatthew G Knepley     const PetscInt *values;
614cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
615cd0c2139SMatthew G Knepley 
616c58f1c22SToby Isaac     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
617cd0c2139SMatthew G Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
618cd0c2139SMatthew G Knepley     if (isDepth) continue;
619fa8e8ae5SToby Isaac     ierr = PetscStrcmp(lname, "dim", &isDim);CHKERRQ(ierr);
620fa8e8ae5SToby Isaac     if (isDim) continue;
621c58f1c22SToby Isaac     ierr = DMCreateLabel(dmNew, lname);CHKERRQ(ierr);
622c58f1c22SToby Isaac     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
623c58f1c22SToby Isaac     ierr = DMGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr);
62464a6a1a4SToby Isaac     ierr = DMLabelGetDefaultValue(label,&val);CHKERRQ(ierr);
62564a6a1a4SToby Isaac     ierr = DMLabelSetDefaultValue(newlabel,val);CHKERRQ(ierr);
626cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
627cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
628cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
629cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
630cd0c2139SMatthew G Knepley       IS              pointIS;
631cd0c2139SMatthew G Knepley       const PetscInt *points;
632cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
633cd0c2139SMatthew G Knepley 
634cd0c2139SMatthew G Knepley       ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr);
635cd0c2139SMatthew G Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
636cd0c2139SMatthew G Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
637cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
6382582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
639cd0c2139SMatthew G Knepley 
640cd0c2139SMatthew G Knepley         ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr);
641cd0c2139SMatthew G Knepley       }
642cd0c2139SMatthew G Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
643cd0c2139SMatthew G Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
644cd0c2139SMatthew G Knepley     }
645cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
646cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
647cd0c2139SMatthew G Knepley   }
648cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
649cd0c2139SMatthew G Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
650cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
651cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
652cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr);
653c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "vtk");CHKERRQ(ierr);
654c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "ghost");CHKERRQ(ierr);
655c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr);
656c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr);
657cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
658cd0c2139SMatthew G Knepley     for (; c < leafLocal[l] && c < cEnd; ++c) {
659cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
660cd0c2139SMatthew G Knepley     }
661cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
662cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
663cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
664cd0c2139SMatthew G Knepley     } else {
665cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr);
666cd0c2139SMatthew G Knepley     }
667cd0c2139SMatthew G Knepley   }
668cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
669cd0c2139SMatthew G Knepley     ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
670cd0c2139SMatthew G Knepley   }
671cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr);
672cd0c2139SMatthew G Knepley   for (f = fStart; f < fEnd; ++f) {
673cd0c2139SMatthew G Knepley     PetscInt numCells;
674cd0c2139SMatthew G Knepley 
675cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr);
676cd0c2139SMatthew G Knepley     if (numCells < 2) {
677cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);
678cd0c2139SMatthew G Knepley     } else {
679cd0c2139SMatthew G Knepley       const PetscInt *cells = NULL;
680cd0c2139SMatthew G Knepley       PetscInt        vA, vB;
681cd0c2139SMatthew G Knepley 
682cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr);
683cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr);
684cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr);
68590664b96SToby Isaac       if (vA != 1 && vB != 1) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);}
686cd0c2139SMatthew G Knepley     }
687cd0c2139SMatthew G Knepley   }
688cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
689cd0c2139SMatthew G Knepley }
690cd0c2139SMatthew G Knepley 
691ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
692ca04dac2SToby Isaac {
693ca04dac2SToby Isaac   DM             refTree;
694ca04dac2SToby Isaac   PetscSection   pSec;
695ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
696ca04dac2SToby Isaac   PetscErrorCode ierr;
697ca04dac2SToby Isaac 
698ca04dac2SToby Isaac   PetscFunctionBegin;
699ca04dac2SToby Isaac   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
700ca04dac2SToby Isaac   ierr = DMPlexSetReferenceTree(dmNew,refTree);CHKERRQ(ierr);
701ca04dac2SToby Isaac   ierr = DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL);CHKERRQ(ierr);
702ca04dac2SToby Isaac   if (pSec) {
7032582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
704fb4630b5SToby Isaac     PetscInt *childIDsShifted;
705ca04dac2SToby Isaac     PetscSection pSecShifted;
706ca04dac2SToby Isaac 
707ca04dac2SToby Isaac     ierr = PetscSectionGetChart(pSec,&pStart,&pEnd);CHKERRQ(ierr);
708ca04dac2SToby Isaac     ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
7092582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
7102582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
711fb4630b5SToby Isaac     ierr = PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted);CHKERRQ(ierr);
712ca04dac2SToby Isaac     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted);CHKERRQ(ierr);
713ca04dac2SToby Isaac     ierr = PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted);CHKERRQ(ierr);
714ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
715fb4630b5SToby Isaac       /* start off assuming no children */
716fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,p,0);CHKERRQ(ierr);
717fb4630b5SToby Isaac     }
718fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
719fb4630b5SToby Isaac       PetscInt dof;
720fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
721ca04dac2SToby Isaac 
722ca04dac2SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
723fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,pNew,dof);CHKERRQ(ierr);
724ca04dac2SToby Isaac     }
725ca04dac2SToby Isaac     ierr = PetscSectionSetUp(pSecShifted);CHKERRQ(ierr);
726fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
727fb4630b5SToby Isaac       PetscInt dof;
728fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
729fb4630b5SToby Isaac 
730fb4630b5SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
731fb4630b5SToby Isaac       if (dof) {
732fb4630b5SToby Isaac         PetscInt off, offNew;
733fb4630b5SToby Isaac 
734fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSec,p,&off);CHKERRQ(ierr);
735fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSecShifted,pNew,&offNew);CHKERRQ(ierr);
736fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
737fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
738fb4630b5SToby Isaac       }
739fb4630b5SToby Isaac     }
740fb4630b5SToby Isaac     ierr = DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted);CHKERRQ(ierr);
741fb4630b5SToby Isaac     ierr = PetscFree2(parentsShifted,childIDsShifted);CHKERRQ(ierr);
742e6885bbbSToby Isaac     ierr = PetscSectionDestroy(&pSecShifted);CHKERRQ(ierr);
743ca04dac2SToby Isaac   }
744ca04dac2SToby Isaac   PetscFunctionReturn(0);
745ca04dac2SToby Isaac }
746ca04dac2SToby Isaac 
747cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
748cd0c2139SMatthew G Knepley {
749da97024aSMatthew G. Knepley   PetscSF               sf;
750cd0c2139SMatthew G Knepley   IS                    valueIS;
751da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
752cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
7532582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
75490b157c4SStefano Zampini   PetscBool             isper;
75590b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
75690b157c4SStefano Zampini   const DMBoundaryType *bd;
757cd0c2139SMatthew G Knepley   PetscErrorCode        ierr;
758cd0c2139SMatthew G Knepley 
759cd0c2139SMatthew G Knepley   PetscFunctionBegin;
760da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
761da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
762da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
76346c796b9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
764cd0c2139SMatthew G Knepley   /* Count ghost cells */
765cd0c2139SMatthew G Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
766cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr);
767cd0c2139SMatthew G Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
7684a6cfa73SMatthew G. Knepley   Ng   = 0;
769cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
77046c796b9SMatthew G. Knepley     IS              faceIS;
77146c796b9SMatthew G. Knepley     const PetscInt *faces;
77246c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
773cd0c2139SMatthew G Knepley 
77446c796b9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
77546c796b9SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
77646c796b9SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
77746c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
778ca04dac2SToby Isaac       PetscInt numChildren;
779ca04dac2SToby Isaac 
780da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
781ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
782ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
783ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
78446c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
78546c796b9SMatthew G. Knepley     }
7864a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
78746c796b9SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
788cd0c2139SMatthew G Knepley   }
789cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7902582d50cSToby Isaac   ierr = PetscMalloc1(2*(depth+1), &depthShift);CHKERRQ(ierr);
7912582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
79259eef20bSToby Isaac     PetscInt dEnd;
7932582d50cSToby Isaac 
7940974a383SToby Isaac     ierr = DMPlexGetDepthStratum(dm,d,NULL,&dEnd);CHKERRQ(ierr);
79559eef20bSToby Isaac     depthShift[2*d]   = dEnd;
7962582d50cSToby Isaac     depthShift[2*d+1] = 0;
7972582d50cSToby Isaac   }
7982582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
7992582d50cSToby Isaac   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
800cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
801cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
802cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
803485ad865SMatthew G. Knepley   ierr = DMPlexSetGhostCellStratum(gdm, cEnd, PETSC_DETERMINE);CHKERRQ(ierr);
8044a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
805cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr);
806cd0c2139SMatthew G Knepley   }
807cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
808cd0c2139SMatthew G Knepley     IS              faceIS;
809cd0c2139SMatthew G Knepley     const PetscInt *faces;
810cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
811cd0c2139SMatthew G Knepley 
812cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
813cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
814cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
815cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
816ca04dac2SToby Isaac       PetscInt size, numChildren;
817cd0c2139SMatthew G Knepley 
818da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
819ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
820ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
82146c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
822cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr);
8234553fd90SToby Isaac       if (size != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
8244a6cfa73SMatthew G. Knepley       ierr = DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);CHKERRQ(ierr);
825cd0c2139SMatthew G Knepley     }
826cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
827cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
828cd0c2139SMatthew G Knepley   }
829cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
830cd0c2139SMatthew G Knepley   ierr = DMSetUp(gdm);CHKERRQ(ierr);
831cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
832cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
833cd0c2139SMatthew G Knepley   ghostCell = cEnd;
834cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
835cd0c2139SMatthew G Knepley     IS              faceIS;
836cd0c2139SMatthew G Knepley     const PetscInt *faces;
837cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
838cd0c2139SMatthew G Knepley 
839cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
840cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
841cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
84246c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
843ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
844cd0c2139SMatthew G Knepley 
845da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
846ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
847ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
84846c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
849cd0c2139SMatthew G Knepley       ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr);
850cd0c2139SMatthew G Knepley       ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr);
85146c796b9SMatthew G. Knepley       ++ghostCell;
852cd0c2139SMatthew G Knepley     }
853cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
854cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
855cd0c2139SMatthew G Knepley   }
856cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
857cd0c2139SMatthew G Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
858cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
859cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
860cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
861ca04dac2SToby Isaac   ierr = DMPlexShiftTree_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
862cd0c2139SMatthew G Knepley   ierr = PetscFree(depthShift);CHKERRQ(ierr);
863966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
86490b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
86590b157c4SStefano Zampini   ierr = DMSetPeriodicity(gdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
8664a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
867cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
868cd0c2139SMatthew G Knepley }
869cd0c2139SMatthew G Knepley 
870cd0c2139SMatthew G Knepley /*@C
871cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
872cd0c2139SMatthew G Knepley 
873cd0c2139SMatthew G Knepley   Collective on dm
874cd0c2139SMatthew G Knepley 
875cd0c2139SMatthew G Knepley   Input Parameters:
876cd0c2139SMatthew G Knepley + dm - The original DM
877cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
878cd0c2139SMatthew G Knepley 
879cd0c2139SMatthew G Knepley   Output Parameters:
880cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
881cd0c2139SMatthew G Knepley - dmGhosted - The new DM
882cd0c2139SMatthew G Knepley 
883cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
884cd0c2139SMatthew G Knepley 
885cd0c2139SMatthew G Knepley   Level: developer
886cd0c2139SMatthew G Knepley 
887cd0c2139SMatthew G Knepley .seealso: DMCreate()
88831266bc0SMatthew G. Knepley @*/
889cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
890cd0c2139SMatthew G Knepley {
891cd0c2139SMatthew G Knepley   DM             gdm;
892cd0c2139SMatthew G Knepley   DMLabel        label;
893cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
894d80ece95SMatthew G. Knepley   PetscInt       dim, Ng = 0, cMax, fMax, eMax, vMax;
895b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
896cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
897cd0c2139SMatthew G Knepley 
898cd0c2139SMatthew G Knepley   PetscFunctionBegin;
899cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9004a6cfa73SMatthew G. Knepley   if (numGhostCells) PetscValidPointer(numGhostCells, 3);
901cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
902cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr);
903cd0c2139SMatthew G Knepley   ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr);
904c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
905c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(gdm, dim);CHKERRQ(ierr);
906b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
907b0441da4SMatthew G. Knepley   ierr = DMSetBasicAdjacency(gdm, useCone,  useClosure);CHKERRQ(ierr);
908c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
909cd0c2139SMatthew G Knepley   if (!label) {
910cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
911c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
912c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
913e752be1aSMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
914cd0c2139SMatthew G Knepley   }
915d80ece95SMatthew G. Knepley   ierr = DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm);CHKERRQ(ierr);
916a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, gdm);CHKERRQ(ierr);
917b0441da4SMatthew G. Knepley   ierr = DMCopyDisc(dm, gdm);CHKERRQ(ierr);
918d80ece95SMatthew G. Knepley   /* Copy the hybrid bounds */
919d80ece95SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
920d80ece95SMatthew G. Knepley   if (fMax >= 0) fMax += Ng;
921d80ece95SMatthew G. Knepley   if (eMax >= 0) eMax += Ng;
922d80ece95SMatthew G. Knepley   if (vMax >= 0) vMax += Ng;
923d80ece95SMatthew G. Knepley   ierr = DMPlexSetHybridBounds(gdm, cMax, fMax, eMax, vMax);CHKERRQ(ierr);
924cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
925d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
926cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
927cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
928cd0c2139SMatthew G Knepley }
929cd0c2139SMatthew G Knepley 
930607ab7a9SMatthew G. Knepley /*
931faedd622SMatthew G. Knepley   We are adding three kinds of points here:
932607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
933faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
934607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
935a6ae58d1SMatthew G. Knepley 
936a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
937a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
938607ab7a9SMatthew G. Knepley */
9397db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
940cd0c2139SMatthew G Knepley {
941cd0c2139SMatthew G Knepley   MPI_Comm         comm;
942607ab7a9SMatthew G. Knepley   IS               valueIS;
943607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;       /* The number of depths for which we have replicated points */
944607ab7a9SMatthew G. Knepley   const PetscInt  *values;          /* List of depths for which we have replicated points */
94518c5995bSMatthew G. Knepley   IS              *splitIS;
94618c5995bSMatthew G. Knepley   IS              *unsplitIS;
947607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
94818c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
94936dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
95036dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
951607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
95218c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
953cd0c2139SMatthew G Knepley   PetscSection     coordSection;
954cd0c2139SMatthew G Knepley   Vec              coordinates;
955cd0c2139SMatthew G Knepley   PetscScalar     *coords;
956a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
957a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
958607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
959607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
960607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
96118c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
962cd0c2139SMatthew G Knepley   PetscErrorCode   ierr;
963cd0c2139SMatthew G Knepley 
964cd0c2139SMatthew G Knepley   PetscFunctionBegin;
965cd0c2139SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
966c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
967607ab7a9SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
968fd4b9f15SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
969cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
970607ab7a9SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
9712582d50cSToby Isaac   ierr = PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);CHKERRQ(ierr);
972dcca6d9dSJed 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);
973a6ae58d1SMatthew 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);
974607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
975607ab7a9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr);
976a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
977a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
978607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
97918c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
980607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
981a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
982607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
98318c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
98418c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
98518c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
98659eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
98759eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
98859eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
98959eef20bSToby Isaac     depthShift[2*d+1]     = 0;
990607ab7a9SMatthew G. Knepley   }
991cd0c2139SMatthew G Knepley   if (label) {
992cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
993cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr);
994cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
995cd0c2139SMatthew G Knepley   }
996cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
997cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
998cd0c2139SMatthew G Knepley 
999cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
100018c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, dep, &splitIS[dep]);CHKERRQ(ierr);
100118c5995bSMatthew G. Knepley     if (splitIS[dep]) {
100218c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr);
100318c5995bSMatthew G. Knepley       ierr = ISGetIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);
100418c5995bSMatthew G. Knepley     }
100518c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);CHKERRQ(ierr);
100618c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
100718c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);CHKERRQ(ierr);
100818c5995bSMatthew G. Knepley       ierr = ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);
1009cd0c2139SMatthew G Knepley     }
1010cd0c2139SMatthew G Knepley   }
1011607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
101218c5995bSMatthew 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   */
10132582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
10143d3eaba7SBarry Smith   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
101559eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
101659eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
101759eef20bSToby Isaac    * added points */
10182582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
1019cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1020cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1021cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1022cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1023cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10242582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
10254c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1026cd0c2139SMatthew G Knepley       const PetscInt *support;
10274c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1028cd0c2139SMatthew G Knepley 
1029cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1030cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr);
1031cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1032cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr);
1033cd0c2139SMatthew G Knepley       if (dep == depth-1) {
10344c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
10354c367dbcSMatthew G. Knepley 
1036cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
10374c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr);
1038cd0c2139SMatthew G Knepley       } else if (dep == 0) {
10394c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1040cd0c2139SMatthew G Knepley 
1041cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
10424c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1043cd0c2139SMatthew G Knepley           PetscInt val;
1044cd0c2139SMatthew G Knepley 
1045cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
10464c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
10474c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
10484c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1049cd0c2139SMatthew G Knepley         }
10504c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
10514c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
10524c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
10534c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
10544c367dbcSMatthew G. Knepley         /* Add hybrid edge */
10554c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
10564c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
1057cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
10584c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
10594c367dbcSMatthew G. Knepley 
1060cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
10614c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1062cd0c2139SMatthew G Knepley           PetscInt val;
1063cd0c2139SMatthew G Knepley 
1064cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
10654c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
10664c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
10674c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1068cd0c2139SMatthew G Knepley         }
10694c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
10704c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
10714c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
10724c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
10734c367dbcSMatthew G. Knepley         /* Add hybrid face */
10744c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
10754c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1076cd0c2139SMatthew G Knepley       }
1077cd0c2139SMatthew G Knepley     }
1078cd0c2139SMatthew G Knepley   }
107918c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
108018c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
108118c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
10822582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
108318c5995bSMatthew G. Knepley       const PetscInt *support;
1084da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
108518c5995bSMatthew G. Knepley 
108618c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
108718c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
108839254ff6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
108918c5995bSMatthew G. Knepley       if (dep == 0) {
109018c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
109118c5995bSMatthew G. Knepley 
109239254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
109339254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
109439254ff6SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
109539254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
109639254ff6SMatthew G. Knepley         }
109739254ff6SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
109818c5995bSMatthew G. Knepley         /* Add hybrid edge */
109918c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
1100e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1101e1757548SMatthew G. Knepley           PetscInt val;
1102e1757548SMatthew G. Knepley 
1103e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1104e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1105da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1106da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1107e1757548SMatthew G. Knepley         }
1108e1757548SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
110918c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
111018c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1111cd0c2139SMatthew G Knepley         PetscInt       val;
1112cd0c2139SMatthew G Knepley 
1113da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1114cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1115da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1116da1dd7e4SMatthew G. Knepley           else              ++qf;
1117cd0c2139SMatthew G Knepley         }
111818c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
1119da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
112018c5995bSMatthew G. Knepley         /* Add hybrid face */
1121da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1122da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1123da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1124da1dd7e4SMatthew G. Knepley         }
112518c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
1126da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1127cd0c2139SMatthew G Knepley       }
1128cd0c2139SMatthew G Knepley     }
1129cd0c2139SMatthew G Knepley   }
1130cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
1131cd0c2139SMatthew G Knepley   ierr = DMSetUp(sdm);CHKERRQ(ierr);
1132cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1133dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
1134dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);CHKERRQ(ierr);
1135cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1136cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1137cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1138cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
11392582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
11404c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1141cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
11424c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1143cd0c2139SMatthew G Knepley 
1144cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1145cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
1146cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
1147cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1148cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
1149cd0c2139SMatthew G Knepley       if (dep == depth-1) {
115096a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
11514c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1152cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1153cd0c2139SMatthew G Knepley 
1154cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
1155cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(sdm, newp,  &supportF);CHKERRQ(ierr);
1156cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr);
1157cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1158cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1159cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
11604c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
116118c5995bSMatthew G. Knepley           if (v < 0) {
116218c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
116318c5995bSMatthew 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);
11642582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
116596a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
116618c5995bSMatthew G. Knepley           } else {
11674c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1168163235baSMatthew G. Knepley             if (dep > 1) {
1169163235baSMatthew G. Knepley               const PetscInt *econe;
1170163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1171163235baSMatthew G. Knepley 
1172163235baSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[q], &econeSize);CHKERRQ(ierr);
1173163235baSMatthew G. Knepley               ierr = DMPlexGetCone(dm, cone[q], &econe);CHKERRQ(ierr);
1174163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
1175163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);CHKERRQ(ierr);
1176163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);CHKERRQ(ierr);
1177163235baSMatthew G. Knepley                 if (vs >= 0) continue;
1178163235baSMatthew 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);
1179163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1180163235baSMatthew G. Knepley               }
1181163235baSMatthew G. Knepley             }
1182cd0c2139SMatthew G Knepley           }
1183cd0c2139SMatthew G Knepley         }
1184cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr);
1185cd0c2139SMatthew G Knepley         ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr);
1186e537020bSMatthew G. Knepley         /* Face support */
1187cd0c2139SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
1188cd0c2139SMatthew G Knepley           PetscInt val;
1189cd0c2139SMatthew G Knepley 
1190cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr);
1191cd0c2139SMatthew G Knepley           if (val < 0) {
1192cd0c2139SMatthew G Knepley             /* Split old face:   Replace negative side cell with cohesive cell */
11934c367dbcSMatthew G. Knepley              ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr);
1194cd0c2139SMatthew G Knepley           } else {
1195cd0c2139SMatthew G Knepley             /* Split new face:   Replace positive side cell with cohesive cell */
11964c367dbcSMatthew G. Knepley             ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr);
1197e537020bSMatthew G. Knepley             /* Get orientation for cohesive face */
1198e537020bSMatthew G. Knepley             {
1199e537020bSMatthew G. Knepley               const PetscInt *ncone, *nconeO;
1200e537020bSMatthew G. Knepley               PetscInt        nconeSize, nc;
1201e537020bSMatthew G. Knepley 
1202e537020bSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr);
1203e537020bSMatthew G. Knepley               ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr);
1204e537020bSMatthew G. Knepley               ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr);
1205e537020bSMatthew G. Knepley               for (nc = 0; nc < nconeSize; ++nc) {
1206e537020bSMatthew G. Knepley                 if (ncone[nc] == oldp) {
1207e537020bSMatthew G. Knepley                   coneONew[0] = nconeO[nc];
1208e537020bSMatthew G. Knepley                   break;
1209cd0c2139SMatthew G Knepley                 }
1210cd0c2139SMatthew G Knepley               }
1211e537020bSMatthew 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]);
1212e537020bSMatthew G. Knepley             }
1213e537020bSMatthew G. Knepley           }
1214e537020bSMatthew G. Knepley         }
12154c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1216fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
12174c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
12184c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1219e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
122018c5995bSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
122118c5995bSMatthew G. Knepley           if (v < 0) {
122218c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
122318c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
122418c5995bSMatthew G. Knepley             coneONew[2+q] = 0;
122518c5995bSMatthew G. Knepley           } else {
122618c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
122718c5995bSMatthew G. Knepley           }
1228e537020bSMatthew G. Knepley           coneONew[2+q] = 0;
1229e537020bSMatthew G. Knepley         }
12304c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr);
12314c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr);
123296a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
123396a07cd0SMatthew G. Knepley         if (hasUnsplit) {ierr = DMLabelSetValue(label, -hybcell, dim);CHKERRQ(ierr);}
1234cd0c2139SMatthew G Knepley       } else if (dep == 0) {
12354c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1236cd0c2139SMatthew G Knepley 
1237cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
12384c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1239cd0c2139SMatthew G Knepley           PetscInt val;
1240cd0c2139SMatthew G Knepley 
1241cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1242cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
12432582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1244cd0c2139SMatthew G Knepley           }
1245cd0c2139SMatthew G Knepley         }
12464c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
1247cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1248cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
12494c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1250cd0c2139SMatthew G Knepley           PetscInt val, edge;
1251cd0c2139SMatthew G Knepley 
1252cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1253cd0c2139SMatthew G Knepley           if (val == 1) {
12544c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
1255cd0c2139SMatthew G Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
12564c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1257cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
12582582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1259cd0c2139SMatthew G Knepley           }
1260cd0c2139SMatthew G Knepley         }
12614c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
1262cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
12634c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1264cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1265cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
12664c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
12674c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12684c367dbcSMatthew G. Knepley           PetscInt val, edge;
12694c367dbcSMatthew G. Knepley 
12704c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12714c367dbcSMatthew G. Knepley           if (val == 1) {
12724c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
12734c367dbcSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
12744c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
12754c367dbcSMatthew G. Knepley           }
12764c367dbcSMatthew G. Knepley         }
12774c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1278cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
12794c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
12804c367dbcSMatthew G. Knepley 
1281cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1282cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1283cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
12844c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
1285e1757548SMatthew G. Knepley           if (v < 0) {
1286e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1287e1757548SMatthew 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);
12882582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1289e1757548SMatthew G. Knepley           } else {
12904c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1291cd0c2139SMatthew G Knepley           }
1292e1757548SMatthew G. Knepley         }
1293cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr);
1294cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1295cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1296cd0c2139SMatthew G Knepley           PetscInt val;
1297cd0c2139SMatthew G Knepley 
1298cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12994c367dbcSMatthew G. Knepley           if (val == dim-1) {
13002582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
13014c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
13022582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1303cd0c2139SMatthew G Knepley           }
1304cd0c2139SMatthew G Knepley         }
1305b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1306cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1307cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1308cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1309cd0c2139SMatthew G Knepley           PetscInt val, face;
1310cd0c2139SMatthew G Knepley 
1311cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1312cd0c2139SMatthew G Knepley           if (val == dim-1) {
13134c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1314cd0c2139SMatthew G Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
13154c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1316cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
13172582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1318cd0c2139SMatthew G Knepley           }
1319cd0c2139SMatthew G Knepley         }
1320b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1321cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
13224c367dbcSMatthew G. Knepley         /* Hybrid face */
13234c367dbcSMatthew G. Knepley         coneNew[0] = newp;
13244c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
13254c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
13264c367dbcSMatthew G. Knepley           PetscInt vertex;
13274c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr);
1328e1757548SMatthew G. Knepley           if (vertex < 0) {
1329e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);CHKERRQ(ierr);
1330e1757548SMatthew 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);
1331e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1332e1757548SMatthew G. Knepley           } else {
13334c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
13344c367dbcSMatthew G. Knepley           }
1335e1757548SMatthew G. Knepley         }
13364c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
13374c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13384c367dbcSMatthew G. Knepley           PetscInt val, face;
13394c367dbcSMatthew G. Knepley 
13404c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
13414c367dbcSMatthew G. Knepley           if (val == dim-1) {
13424c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
13434c367dbcSMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
13444c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
13454c367dbcSMatthew G. Knepley           }
13464c367dbcSMatthew G. Knepley         }
13474c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1348cd0c2139SMatthew G Knepley       }
1349cd0c2139SMatthew G Knepley     }
1350cd0c2139SMatthew G Knepley   }
135118c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
135218c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
135318c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
13542582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
135518c5995bSMatthew G. Knepley       const PetscInt *cone, *support, *ornt;
1356e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
135718c5995bSMatthew G. Knepley 
135818c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
135918c5995bSMatthew G. Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
136018c5995bSMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
136118c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
136218c5995bSMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
136318c5995bSMatthew G. Knepley       if (dep == 0) {
136418c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
136518c5995bSMatthew G. Knepley 
136618c5995bSMatthew G. Knepley         /* Unsplit vertex */
136718c5995bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
136818c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
13692582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
137018c5995bSMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
137118c5995bSMatthew G. Knepley           if (e >= 0) {
137218c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
137318c5995bSMatthew G. Knepley           }
137418c5995bSMatthew G. Knepley         }
137518c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
137618c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
137718c5995bSMatthew G. Knepley         if (q != supportSizeNew) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
137818c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
137918c5995bSMatthew G. Knepley         /* Hybrid edge */
138018c5995bSMatthew G. Knepley         coneNew[0] = newp;
138118c5995bSMatthew G. Knepley         coneNew[1] = newp;
138218c5995bSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
138318c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
138418c5995bSMatthew G. Knepley           PetscInt val, edge;
138518c5995bSMatthew G. Knepley 
138618c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
138718c5995bSMatthew G. Knepley           if (val == 1) {
138818c5995bSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
138918c5995bSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
139018c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1391e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
1392e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);CHKERRQ(ierr);
1393e1757548SMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1394e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
139518c5995bSMatthew G. Knepley           }
139618c5995bSMatthew G. Knepley         }
139718c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1398e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1399e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1400e1757548SMatthew G. Knepley 
1401da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1402e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1403e1757548SMatthew G. Knepley           PetscInt val, face;
1404e1757548SMatthew G. Knepley 
1405e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1406e1757548SMatthew G. Knepley           if (val == dim-1) {
1407e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1408e1757548SMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
14092582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1410e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1411e1757548SMatthew G. Knepley           } else {
14122582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1413e1757548SMatthew G. Knepley           }
1414e1757548SMatthew G. Knepley         }
1415e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1416e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1417da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
1418da1dd7e4SMatthew 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);
1419e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1420e1757548SMatthew G. Knepley         /* Add hybrid face */
1421e1757548SMatthew G. Knepley         coneNew[0] = newp;
1422212cc919SMatthew G. Knepley         coneNew[1] = newp;
1423e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1424e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1425212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1426e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1427e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1428e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1429e1757548SMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
1430da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1431da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1432da1dd7e4SMatthew G. Knepley 
1433da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1434da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
1435da1dd7e4SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1436da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1437da1dd7e4SMatthew G. Knepley           }
1438da1dd7e4SMatthew G. Knepley         }
1439da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);CHKERRQ(ierr);
1440da1dd7e4SMatthew 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);
1441e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1442cd0c2139SMatthew G Knepley       }
1443cd0c2139SMatthew G Knepley     }
1444cd0c2139SMatthew G Knepley   }
1445cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1446cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1447cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1448cd0c2139SMatthew G Knepley     IS              pIS;
1449cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1450cd0c2139SMatthew G Knepley     const PetscInt *points;
1451cd0c2139SMatthew G Knepley 
1452cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
1453cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr);
1454cd0c2139SMatthew G Knepley     if (!pIS) continue;
1455cd0c2139SMatthew G Knepley     dep  = -dep - shift;
1456cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr);
1457cd0c2139SMatthew G Knepley     ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr);
1458cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1459cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
14602582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1461cd0c2139SMatthew G Knepley       const PetscInt *cone;
1462cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
146350cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1464cd0c2139SMatthew G Knepley 
1465cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1466cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
1467cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr);
1468cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr);
1469cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1470e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1471cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1472cd0c2139SMatthew G Knepley 
1473cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr);
1474cd0c2139SMatthew G Knepley         if (val == dep-1) {
1475cd0c2139SMatthew G Knepley           ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr);
1476cd0c2139SMatthew G Knepley           if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1477cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
1478cd0c2139SMatthew G Knepley           ierr     = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr);
147950cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1480cd0c2139SMatthew G Knepley         }
1481cd0c2139SMatthew G Knepley       }
14824a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
14834a189a86SMatthew G. Knepley       /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1484cd0c2139SMatthew G Knepley     }
1485cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr);
1486cd0c2139SMatthew G Knepley     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
1487cd0c2139SMatthew G Knepley   }
1488fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
1489cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
149069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr);
1491cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr);
1492cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1493cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
14942582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1495cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1496cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1497cd0c2139SMatthew G Knepley 
1498cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr);
1499cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr);
1500cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr);
1501cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1502cd0c2139SMatthew G Knepley   }
1503cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1504fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
1505cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1506fa8e8ae5SToby Isaac   /* Step 9: Labels */
1507cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1508c58f1c22SToby Isaac   ierr = DMGetNumLabels(sdm, &numLabels);CHKERRQ(ierr);
1509cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1510cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
15112582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1512cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1513cd0c2139SMatthew G Knepley       PetscInt       l;
1514cd0c2139SMatthew G Knepley 
15157db7e0a7SMatthew G. Knepley       if (splitLabel) {
15167db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
15177db7e0a7SMatthew G. Knepley 
15187db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, newp,    val);CHKERRQ(ierr);
15197db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, splitp, -val);CHKERRQ(ierr);
15207db7e0a7SMatthew G. Knepley       }
1521cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1522cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1523cd0c2139SMatthew G Knepley         const char *lname;
1524cd0c2139SMatthew G Knepley         PetscInt    val;
15259a356370SMatthew G. Knepley         PetscBool   isDepth;
1526cd0c2139SMatthew G Knepley 
1527c58f1c22SToby Isaac         ierr = DMGetLabelName(sdm, l, &lname);CHKERRQ(ierr);
15289a356370SMatthew G. Knepley         ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
15299a356370SMatthew G. Knepley         if (isDepth) continue;
1530c58f1c22SToby Isaac         ierr = DMGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr);
1531cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr);
1532cd0c2139SMatthew G Knepley         if (val >= 0) {
1533cd0c2139SMatthew G Knepley           ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr);
1534cd0c2139SMatthew G Knepley         }
1535cd0c2139SMatthew G Knepley       }
1536cd0c2139SMatthew G Knepley     }
1537cd0c2139SMatthew G Knepley   }
1538cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1539cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1540cd0c2139SMatthew G Knepley 
1541cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
154218c5995bSMatthew G. Knepley     if (splitIS[dep]) {ierr = ISRestoreIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);}
154318c5995bSMatthew G. Knepley     ierr = ISDestroy(&splitIS[dep]);CHKERRQ(ierr);
154418c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {ierr = ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);}
154518c5995bSMatthew G. Knepley     ierr = ISDestroy(&unsplitIS[dep]);CHKERRQ(ierr);
1546cd0c2139SMatthew G Knepley   }
1547cd0c2139SMatthew G Knepley   if (label) {
1548cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
1549cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1550cd0c2139SMatthew G Knepley   }
15510d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
15520d4d4d06SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr);
155336dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
15540d4d4d06SMatthew G. Knepley   }
15550e49e2e2SMatthew 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);
1556dcbb62e8SMatthew G. Knepley   ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr);
15572582d50cSToby Isaac   ierr = PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld);CHKERRQ(ierr);
155818c5995bSMatthew G. Knepley   ierr = PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);CHKERRQ(ierr);
1559cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1560cd0c2139SMatthew G Knepley }
1561cd0c2139SMatthew G Knepley 
1562cd0c2139SMatthew G Knepley /*@C
1563cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1564cd0c2139SMatthew G Knepley 
1565cd0c2139SMatthew G Knepley   Collective on dm
1566cd0c2139SMatthew G Knepley 
1567cd0c2139SMatthew G Knepley   Input Parameters:
1568cd0c2139SMatthew G Knepley + dm - The original DM
156953156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1570cd0c2139SMatthew G Knepley 
1571cd0c2139SMatthew G Knepley   Output Parameters:
15727db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1573cd0c2139SMatthew G Knepley - dmSplit - The new DM
1574cd0c2139SMatthew G Knepley 
1575cd0c2139SMatthew G Knepley   Level: developer
1576cd0c2139SMatthew G Knepley 
15772be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1578cd0c2139SMatthew G Knepley @*/
15797db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1580cd0c2139SMatthew G Knepley {
1581cd0c2139SMatthew G Knepley   DM             sdm;
1582cd0c2139SMatthew G Knepley   PetscInt       dim;
1583cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
1584cd0c2139SMatthew G Knepley 
1585cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1586cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
158753156dfcSMatthew G. Knepley   PetscValidPointer(dmSplit, 3);
1588cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr);
1589cd0c2139SMatthew G Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
1590c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1591c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(sdm, dim);CHKERRQ(ierr);
1592cd0c2139SMatthew G Knepley   switch (dim) {
1593cd0c2139SMatthew G Knepley   case 2:
1594cd0c2139SMatthew G Knepley   case 3:
15957db7e0a7SMatthew G. Knepley     ierr = DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm);CHKERRQ(ierr);
1596cd0c2139SMatthew G Knepley     break;
1597cd0c2139SMatthew G Knepley   default:
1598cd0c2139SMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1599cd0c2139SMatthew G Knepley   }
1600cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1601cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1602cd0c2139SMatthew G Knepley }
1603cd0c2139SMatthew G Knepley 
16040f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
16050f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
16060f66a230SMatthew G. Knepley {
16070f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
16080f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
16090f66a230SMatthew G. Knepley   PetscErrorCode  ierr;
16100f66a230SMatthew G. Knepley 
16110f66a230SMatthew G. Knepley   PetscFunctionBegin;
16120f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
1613c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16140f66a230SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
16150f66a230SMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
16160f66a230SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
16170f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
16180f66a230SMatthew G. Knepley     if (cone[c] == face) {
16190f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
16200f66a230SMatthew G. Knepley 
16210f66a230SMatthew G. Knepley       if (subdm) {
16220f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
16230f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
16240f66a230SMatthew G. Knepley 
16250f66a230SMatthew G. Knepley         ierr = PetscFindInt(cell, numSubpoints, subpoints, &subpoint);CHKERRQ(ierr);
16260f66a230SMatthew G. Knepley         ierr = PetscFindInt(face, numSubpoints, subpoints, &subface);CHKERRQ(ierr);
16270f66a230SMatthew G. Knepley         ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
16280f66a230SMatthew G. Knepley         ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr);
16290f66a230SMatthew G. Knepley         ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr);
16300f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
16310f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
16320f66a230SMatthew G. Knepley             o = subornt[0];
16330f66a230SMatthew G. Knepley             break;
16340f66a230SMatthew G. Knepley           }
16350f66a230SMatthew G. Knepley         }
16360f66a230SMatthew 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);
16370f66a230SMatthew G. Knepley       }
16380f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
16390f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
16400f66a230SMatthew G. Knepley       break;
16410f66a230SMatthew G. Knepley     }
16420f66a230SMatthew G. Knepley   }
16430f66a230SMatthew 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);
16440f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
16450f66a230SMatthew G. Knepley }
16460f66a230SMatthew G. Knepley 
1647cd0c2139SMatthew G Knepley /*@
16480f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1649cd0c2139SMatthew G Knepley   to complete the surface
1650cd0c2139SMatthew G Knepley 
1651cd0c2139SMatthew G Knepley   Input Parameters:
1652cd0c2139SMatthew G Knepley + dm     - The DM
16530f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
16540f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1655bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
165647946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1657cd0c2139SMatthew G Knepley 
1658cd0c2139SMatthew G Knepley   Output Parameter:
1659cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1660cd0c2139SMatthew G Knepley 
16610f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
16620f66a230SMatthew G. Knepley 
1663cd0c2139SMatthew G Knepley   Level: developer
1664cd0c2139SMatthew G Knepley 
16652be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1666cd0c2139SMatthew G Knepley @*/
16670f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1668cd0c2139SMatthew G Knepley {
1669d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
16708630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
167147946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1672bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1673370472baSMatthew G. Knepley   PetscInt       *pMax;
1674370472baSMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, dim, depth, pSize, dep, cStart, cEnd, cMax, fStart, fEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1675cd0c2139SMatthew G Knepley   PetscErrorCode  ierr;
1676cd0c2139SMatthew G Knepley 
1677cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1678370472baSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1679370472baSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1680370472baSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
1681370472baSMatthew G. Knepley   ierr = PetscMalloc1(pSize,&pMax);CHKERRQ(ierr);
1682370472baSMatthew 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);
1683d90583fdSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1684c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
168547946fd8SMatthew G. Knepley   if (subdm) {
168647946fd8SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
168747946fd8SMatthew G. Knepley     if (subpointIS) {
168847946fd8SMatthew G. Knepley       ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
168947946fd8SMatthew G. Knepley       ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
169047946fd8SMatthew G. Knepley     }
169147946fd8SMatthew G. Knepley   }
1692d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1693cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr);
1694dc86033cSMatthew G. Knepley   if (!dimIS) {
1695dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
16968630cb1aSMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1697dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1698dc86033cSMatthew G. Knepley   }
1699cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1700cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1701d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1702cd0c2139SMatthew G Knepley     const PetscInt *support;
1703cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1704cd0c2139SMatthew G Knepley 
1705cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr);
1706c4419245SMatthew G. Knepley #if 0
1707c4419245SMatthew G. Knepley     if (supportSize != 2) {
1708c4419245SMatthew G. Knepley       const PetscInt *lp;
1709c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1710c4419245SMatthew G. Knepley 
1711c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1712c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
1713c4419245SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL);CHKERRQ(ierr);
1714c4419245SMatthew G. Knepley       ierr = PetscFindInt(points[p], Nlp, lp, &pind);CHKERRQ(ierr);
1715c4419245SMatthew G. Knepley       if (pind < 0) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports, and the face is not shared with another process", points[p], supportSize);
1716c4419245SMatthew G. Knepley     }
1717c4419245SMatthew G. Knepley #endif
1718cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr);
1719cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
17200f66a230SMatthew G. Knepley       const PetscInt *cone;
1721cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
17220f66a230SMatthew G. Knepley       PetscBool       pos;
1723cd0c2139SMatthew G Knepley 
17240f66a230SMatthew G. Knepley       ierr = GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);CHKERRQ(ierr);
17250f66a230SMatthew G. Knepley       if (pos) {ierr = DMLabelSetValue(label, support[s],  rev*(shift+dim));CHKERRQ(ierr);}
17260f66a230SMatthew G. Knepley       else     {ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr);}
17270f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
17280f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
1729cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
1730cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
1731cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1732cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1733cd0c2139SMatthew G Knepley 
1734cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1735cd0c2139SMatthew G Knepley         if (val == -1) {
1736cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1737cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1738cd0c2139SMatthew G Knepley 
1739cd0c2139SMatthew G Knepley           ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1740cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1741cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1742a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1743cd0c2139SMatthew G Knepley 
1744cd0c2139SMatthew G Knepley             ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1745a0541d8aSMatthew G. Knepley             if (blabel) {ierr = DMLabelGetValue(blabel, clp, &bval);CHKERRQ(ierr);}
1746a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1747cd0c2139SMatthew G Knepley               ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr);
1748cd0c2139SMatthew G Knepley               break;
1749cd0c2139SMatthew G Knepley             }
1750cd0c2139SMatthew G Knepley           }
1751cd0c2139SMatthew G Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1752cd0c2139SMatthew G Knepley         }
1753cd0c2139SMatthew G Knepley       }
1754cd0c2139SMatthew G Knepley     }
1755cd0c2139SMatthew G Knepley   }
17560f66a230SMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
17570f66a230SMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
175847946fd8SMatthew G. Knepley   if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);}
175947946fd8SMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1760a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
176186200784SMatthew G. Knepley   if (blabel) {
1762a0541d8aSMatthew G. Knepley     ierr = DMLabelGetStratumIS(blabel, 1, &dimIS);CHKERRQ(ierr);
1763a0541d8aSMatthew G. Knepley     ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1764a0541d8aSMatthew G. Knepley     ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1765a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1766a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1767a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1768a0541d8aSMatthew G. Knepley 
1769a0541d8aSMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1770a0541d8aSMatthew G. Knepley       if (bval >= 0) {
1771f7019248SMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1772f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1773f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1774f7019248SMatthew G. Knepley           ierr = DMLabelClearValue(blabel, point, bval);CHKERRQ(ierr);
1775f7019248SMatthew G. Knepley         }
1776f7019248SMatthew G. Knepley       }
1777f7019248SMatthew G. Knepley     }
1778f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1779f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1780f7019248SMatthew G. Knepley       PetscInt       val, bval;
1781f7019248SMatthew G. Knepley 
1782f7019248SMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1783f7019248SMatthew G. Knepley       if (bval >= 0) {
178486200784SMatthew G. Knepley         const PetscInt *cone,    *support;
178586200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
178686200784SMatthew G. Knepley 
1787a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
1788a0541d8aSMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1789a0541d8aSMatthew 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);
1790a0541d8aSMatthew G. Knepley         ierr = DMLabelClearValue(label, point, val);CHKERRQ(ierr);
1791a0541d8aSMatthew G. Knepley         ierr = DMLabelSetValue(label, point, shift2+val);CHKERRQ(ierr);
17922c06a818SMatthew G. Knepley         /* Check for cross-edge
17932c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
179486200784SMatthew G. Knepley         if (val != 0) continue;
179586200784SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
179686200784SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
179786200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
179886200784SMatthew G. Knepley           ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
179986200784SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
180086200784SMatthew G. Knepley           if (coneSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
180186200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[0], &valA);CHKERRQ(ierr);
180286200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[1], &valB);CHKERRQ(ierr);
180386200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, support[s], &valE);CHKERRQ(ierr);
180409816f77SMatthew G. Knepley           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) {ierr = DMLabelSetValue(blabel, support[s], 2);CHKERRQ(ierr);}
180586200784SMatthew G. Knepley         }
1806a0541d8aSMatthew G. Knepley       }
1807a0541d8aSMatthew G. Knepley     }
1808a0541d8aSMatthew G. Knepley     ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1809a0541d8aSMatthew G. Knepley     ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1810a0541d8aSMatthew G. Knepley   }
1811cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
1812c4d480a2SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1813cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1814d24f5ce5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1815d24f5ce5SMatthew G. Knepley   cMax = cMax < 0 ? cEnd : cMax;
18160f66a230SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1817cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr);
181886200784SMatthew G. Knepley   if (blabel) {ierr = DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);CHKERRQ(ierr);}
181986200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
182086200784SMatthew G. Knepley     IS vertIS = dimIS;
182186200784SMatthew G. Knepley 
182286200784SMatthew G. Knepley     ierr = ISExpand(vertIS, crossEdgeIS, &dimIS);CHKERRQ(ierr);
182386200784SMatthew G. Knepley     ierr = ISDestroy(&crossEdgeIS);CHKERRQ(ierr);
182486200784SMatthew G. Knepley     ierr = ISDestroy(&vertIS);CHKERRQ(ierr);
182586200784SMatthew G. Knepley   }
1826dc86033cSMatthew G. Knepley   if (!dimIS) {
1827dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
1828dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1829dc86033cSMatthew G. Knepley   }
1830cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1831cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1832d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1833cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
183486200784SMatthew G. Knepley     PetscInt  starSize, s;
1835cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1836cd0c2139SMatthew G Knepley 
1837d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1838cd0c2139SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1839cd0c2139SMatthew G Knepley     while (again) {
1840cd0c2139SMatthew G Knepley       if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1841cd0c2139SMatthew G Knepley       again = 0;
1842cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1843cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1844cd0c2139SMatthew G Knepley         const PetscInt *cone;
1845cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1846cd0c2139SMatthew G Knepley 
1847d24f5ce5SMatthew G. Knepley         if ((point < cStart) || (point >= cMax)) continue;
1848cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1849cd0c2139SMatthew G Knepley         if (val != -1) continue;
1850d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
1851cd0c2139SMatthew G Knepley         ierr  = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1852cd0c2139SMatthew G Knepley         ierr  = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
1853cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
1854cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr);
1855cd0c2139SMatthew G Knepley           if (val != -1) {
1856d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1857166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1858d7c8f101SMatthew G. Knepley 
1859db8edaafSMatthew 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);
186037a6de01SMatthew G. Knepley             if (val > 0) side =  1;
186137a6de01SMatthew G. Knepley             else         side = -1;
186237a6de01SMatthew G. Knepley             ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr);
1863d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
1864d7c8f101SMatthew G. Knepley             ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr);
1865d7c8f101SMatthew G. Knepley             ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr);
1866d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
1867d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
1868d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
1869d7c8f101SMatthew G. Knepley 
1870166d9d0cSMatthew G. Knepley               ierr = DMLabelGetValue(label, ccone[cc], &val);CHKERRQ(ierr);
1871166d9d0cSMatthew G. Knepley               if (val != -1) continue;
1872d7c8f101SMatthew G. Knepley               ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1873d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
1874d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
1875d7c8f101SMatthew G. Knepley 
1876d7c8f101SMatthew G. Knepley                 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1877d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
1878d7c8f101SMatthew G. Knepley                 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr);
187937a6de01SMatthew G. Knepley                 break;
188037a6de01SMatthew G. Knepley               }
1881d7c8f101SMatthew G. Knepley               ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1882cd0c2139SMatthew G Knepley             }
1883cd0c2139SMatthew G Knepley             again = 1;
1884cd0c2139SMatthew G Knepley             break;
1885cd0c2139SMatthew G Knepley           }
1886cd0c2139SMatthew G Knepley         }
1887cd0c2139SMatthew G Knepley       }
1888cd0c2139SMatthew G Knepley     }
1889cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
1890cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1891cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
1892cd0c2139SMatthew G Knepley 
1893cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1894cd0c2139SMatthew G Knepley       if (val == -1) {
1895cd0c2139SMatthew G Knepley         PetscInt  *sstar = NULL;
1896cd0c2139SMatthew G Knepley         PetscInt   sstarSize, ss;
1897cd0c2139SMatthew G Knepley         PetscBool  marked = PETSC_FALSE;
1898cd0c2139SMatthew G Knepley 
1899cd0c2139SMatthew G Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1900cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
1901cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
1902cd0c2139SMatthew G Knepley 
1903d24f5ce5SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cMax)) continue;
1904cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr);
1905cd0c2139SMatthew 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);
1906d90583fdSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1907cd0c2139SMatthew G Knepley           if (val > 0) {
1908cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point,   shift+dep);CHKERRQ(ierr);
1909cd0c2139SMatthew G Knepley           } else {
1910cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr);
1911cd0c2139SMatthew G Knepley           }
1912cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
1913cd0c2139SMatthew G Knepley           break;
1914cd0c2139SMatthew G Knepley         }
1915cd0c2139SMatthew G Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1916370472baSMatthew G. Knepley         ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1917370472baSMatthew G. Knepley         if (point < pMax[dep] && !marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1918cd0c2139SMatthew G Knepley       }
1919cd0c2139SMatthew G Knepley     }
1920cd0c2139SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1921cd0c2139SMatthew G Knepley   }
1922cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1923cd0c2139SMatthew G Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
192418c5995bSMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
192518c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);CHKERRQ(ierr);
192618c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);CHKERRQ(ierr);
192718c5995bSMatthew G. Knepley   ierr = ISExpand(facePosIS, faceNegIS, &dimIS);CHKERRQ(ierr);
192818c5995bSMatthew G. Knepley   ierr = ISDestroy(&facePosIS);CHKERRQ(ierr);
192918c5995bSMatthew G. Knepley   ierr = ISDestroy(&faceNegIS);CHKERRQ(ierr);
193018c5995bSMatthew G. Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
193118c5995bSMatthew G. Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
193218c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
193318c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
193418c5995bSMatthew G. Knepley     const PetscInt *support;
193518c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
193618c5995bSMatthew G. Knepley 
193718c5995bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
193818c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
193918c5995bSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
194018c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[0], &valA);CHKERRQ(ierr);
194118c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[1], &valB);CHKERRQ(ierr);
194218c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
194318c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
194418c5995bSMatthew G. Knepley     /* Split the face */
194518c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &valA);CHKERRQ(ierr);
194618c5995bSMatthew G. Knepley     ierr = DMLabelClearValue(label, point, valA);CHKERRQ(ierr);
194718c5995bSMatthew G. Knepley     ierr = DMLabelSetValue(label, point, dim-1);CHKERRQ(ierr);
1948e1757548SMatthew G. Knepley     /* Label its closure:
1949e1757548SMatthew G. Knepley       unmarked: label as unsplit
1950e1757548SMatthew G. Knepley       incident: relabel as split
1951e1757548SMatthew G. Knepley       split:    do nothing
1952e1757548SMatthew G. Knepley     */
195318c5995bSMatthew G. Knepley     {
195418c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
195518c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
195618c5995bSMatthew G. Knepley 
195718c5995bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
195818c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
195918c5995bSMatthew G. Knepley         ierr = DMLabelGetValue(label, closure[cl], &valA);CHKERRQ(ierr);
19608771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
196118c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
196218c5995bSMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], shift2+dep);CHKERRQ(ierr);
19638771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1964e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
1965e1757548SMatthew G. Knepley           ierr = DMLabelClearValue(label, closure[cl], valA);CHKERRQ(ierr);
1966e1757548SMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], dep);CHKERRQ(ierr);
1967e1757548SMatthew G. Knepley         }
196818c5995bSMatthew G. Knepley       }
196918c5995bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
197018c5995bSMatthew G. Knepley     }
197118c5995bSMatthew G. Knepley   }
197218c5995bSMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
197318c5995bSMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1974370472baSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
1975cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1976cd0c2139SMatthew G Knepley }
1977cd0c2139SMatthew G Knepley 
1978720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
1979720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
1980720e594eSMatthew G. Knepley {
1981720e594eSMatthew G. Knepley   IS              subpointIS;
1982720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
1983720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
1984720e594eSMatthew G. Knepley   PetscErrorCode  ierr;
1985720e594eSMatthew G. Knepley 
1986720e594eSMatthew G. Knepley   PetscFunctionBegin;
1987720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
1988720e594eSMatthew G. Knepley   ierr = DMLabelGetDefaultValue(label, &defaultValue);CHKERRQ(ierr);
1989720e594eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
1990720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
1991720e594eSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1992720e594eSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1993720e594eSMatthew G. Knepley   ierr = ISGetIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1994720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1995720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
1996720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
1997720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
1998720e594eSMatthew G. Knepley 
1999720e594eSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2000720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2001720e594eSMatthew G. Knepley       PetscInt value = 0;
2002720e594eSMatthew G. Knepley 
2003720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
2004720e594eSMatthew G. Knepley       ierr = DMLabelGetValue(label, closure[cl], &value);CHKERRQ(ierr);
2005720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
2006720e594eSMatthew G. Knepley     }
2007720e594eSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2008720e594eSMatthew G. Knepley     if (invalidCell) {
2009720e594eSMatthew G. Knepley       ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2010720e594eSMatthew G. Knepley       ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
2011720e594eSMatthew G. Knepley       ierr = DMDestroy(&subdm);CHKERRQ(ierr);
2012720e594eSMatthew G. Knepley       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %D has all of its vertices on the submesh.", dmpoints[c]);
2013720e594eSMatthew G. Knepley     }
2014720e594eSMatthew G. Knepley   }
2015720e594eSMatthew G. Knepley   ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2016720e594eSMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
2017720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
2018720e594eSMatthew G. Knepley }
2019720e594eSMatthew G. Knepley 
2020720e594eSMatthew G. Knepley 
2021c08575a3SMatthew G. Knepley /*@
20223cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
20233cf72582SMatthew G. Knepley 
20243cf72582SMatthew G. Knepley   Collective on dm
20253cf72582SMatthew G. Knepley 
20263cf72582SMatthew G. Knepley   Input Parameters:
20273cf72582SMatthew G. Knepley + dm - The original DM
2028720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
2029720e594eSMatthew G. Knepley - bdlabel - The optional label specifying the interface boundary vertices
20303cf72582SMatthew G. Knepley 
20313cf72582SMatthew G. Knepley   Output Parameters:
20327db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
20337db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
2034720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
2035720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
20363cf72582SMatthew G. Knepley 
20376eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
20386eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
20396eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
20406eccb800SMatthew Knepley   one vertex 3 on the surface would be 6 (101) and 3 (0) in hybridLabel. If an edge 9 from the negative side of the
20416eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
20426eccb800SMatthew Knepley 
20436eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
20446eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
20456eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
20466eccb800SMatthew Knepley 
20476eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
20486eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
20496eccb800SMatthew Knepley 
20503cf72582SMatthew G. Knepley   Level: developer
20513cf72582SMatthew G. Knepley 
20526eccb800SMatthew Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMPlexGetSubpointMap(), DMCreate()
20533cf72582SMatthew G. Knepley @*/
20547db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
20553cf72582SMatthew G. Knepley {
20563cf72582SMatthew G. Knepley   DM             idm;
20577db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
20583cf72582SMatthew G. Knepley   PetscInt       dim;
20593cf72582SMatthew G. Knepley   PetscErrorCode ierr;
20603cf72582SMatthew G. Knepley 
20613cf72582SMatthew G. Knepley   PetscFunctionBegin;
20623cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2063720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
2064720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
20657db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
20667db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
20677db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
2068c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2069158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm);CHKERRQ(ierr);
2070720e594eSMatthew G. Knepley   ierr = DMPlexCheckValidSubmesh_Private(dm, label, idm);CHKERRQ(ierr);
20713cf72582SMatthew G. Knepley   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
20723cf72582SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
20733cf72582SMatthew G. Knepley   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
20743cf72582SMatthew G. Knepley   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
20757db7e0a7SMatthew G. Knepley   if (splitLabel) {
20767db7e0a7SMatthew G. Knepley     const char *name;
20777db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
20787db7e0a7SMatthew G. Knepley 
2079d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) hlabel, &name);CHKERRQ(ierr);
20807db7e0a7SMatthew G. Knepley     ierr = PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
20817db7e0a7SMatthew G. Knepley     ierr = PetscStrcat(sname, " split");CHKERRQ(ierr);
2082d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, sname, &slabel);CHKERRQ(ierr);
20837db7e0a7SMatthew G. Knepley   }
2084720e594eSMatthew G. Knepley   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm);CHKERRQ(ierr);
2085720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
2086720e594eSMatthew G. Knepley   else             {ierr = DMDestroy(&idm);CHKERRQ(ierr);}
20877db7e0a7SMatthew G. Knepley   ierr = DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid);CHKERRQ(ierr);
20883cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
208953aa2e23SMatthew G. Knepley   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
20907db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
2091cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2092cd0c2139SMatthew G Knepley }
2093cd0c2139SMatthew G Knepley 
2094efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2095efa14ee0SMatthew G Knepley 
2096efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2097efa14ee0SMatthew G Knepley */
2098830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2099efa14ee0SMatthew G Knepley {
2100fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2101efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
210277d178adSMatthew G. Knepley   PetscInt        *pStart, *pEnd, *pMax, pSize;
2103efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2104efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2105efa14ee0SMatthew G Knepley 
2106efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2107efa14ee0SMatthew G Knepley   *numFaces = 0;
2108efa14ee0SMatthew G Knepley   *nFV      = 0;
2109efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2110c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
211177d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
2112dcca6d9dSJed Brown   ierr = PetscMalloc3(pSize,&pStart,pSize,&pEnd,pSize,&pMax);CHKERRQ(ierr);
2113d92e47f8SMatthew 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);
2114efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
2115efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2116efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2117efa14ee0SMatthew G Knepley   }
2118efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
2119fed694aaSMatthew G. Knepley   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
2120efa14ee0SMatthew G Knepley   if (subvertexIS) {
2121efa14ee0SMatthew G Knepley     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2122efa14ee0SMatthew G Knepley     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2123efa14ee0SMatthew G Knepley   }
2124efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2125efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
21260298fd71SBarry Smith     PetscInt      *star   = NULL;
2127efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2128efa14ee0SMatthew G Knepley 
2129efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2130efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2131efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2132efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2133efa14ee0SMatthew G Knepley     }
2134efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2135efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
21360298fd71SBarry Smith       PetscInt      *closure = NULL;
2137efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2138efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2139efa14ee0SMatthew G Knepley 
2140efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
214165560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
214282f516ccSBarry Smith       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
2143efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2144efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2145efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2146efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2147efa14ee0SMatthew G Knepley 
2148efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2149efa14ee0SMatthew G Knepley           ++numCorners;
2150efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2151830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2152efa14ee0SMatthew G Knepley         }
2153efa14ee0SMatthew G Knepley       }
215418ad9376SMatthew G. Knepley       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
215582f516ccSBarry 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);
2156efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2157007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2158007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2159007baee2SMatthew G. Knepley 
2160efa14ee0SMatthew G Knepley         ++(*numFaces);
2161efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2162efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
2163efa14ee0SMatthew G Knepley         }
2164007baee2SMatthew G. Knepley         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2165007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2166007baee2SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
2167007baee2SMatthew G. Knepley         }
2168007baee2SMatthew G. Knepley         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2169efa14ee0SMatthew G Knepley       }
2170efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2171efa14ee0SMatthew G Knepley     }
2172efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2173efa14ee0SMatthew G Knepley   }
2174efa14ee0SMatthew G Knepley   if (subvertexIS) {
2175efa14ee0SMatthew G Knepley     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2176efa14ee0SMatthew G Knepley   }
2177efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2178efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2179efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2180efa14ee0SMatthew G Knepley }
2181efa14ee0SMatthew G Knepley 
2182158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2183efa14ee0SMatthew G Knepley {
218434b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2185efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2186efa14ee0SMatthew G Knepley   PetscInt        *pStart, *pEnd, *pMax;
2187efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2188efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2189efa14ee0SMatthew G Knepley 
2190efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2191c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2192dcca6d9dSJed Brown   ierr = PetscMalloc3(dim+1,&pStart,dim+1,&pEnd,dim+1,&pMax);CHKERRQ(ierr);
21930298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
2194efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2195efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2196efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2197efa14ee0SMatthew G Knepley   }
2198efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
219934b4c39eSMatthew G. Knepley   if (vertexLabel) {
2200830e53efSMatthew G. Knepley     ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
2201efa14ee0SMatthew G Knepley     if (subvertexIS) {
2202efa14ee0SMatthew G Knepley       ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2203efa14ee0SMatthew G Knepley       ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2204efa14ee0SMatthew G Knepley     }
220534b4c39eSMatthew G. Knepley   }
2206efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2207efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
22080298fd71SBarry Smith     PetscInt      *star   = NULL;
2209efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2210efa14ee0SMatthew G Knepley 
2211efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2212efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2213efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2214158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2215158acfadSMatthew G. Knepley 
2216158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2217158acfadSMatthew G. Knepley         if (markedFaces) {
2218158acfadSMatthew G. Knepley           ierr = DMLabelGetValue(vertexLabel, point, &faceLoc);CHKERRQ(ierr);
2219158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2220158acfadSMatthew G. Knepley         }
2221158acfadSMatthew G. Knepley         star[numFaces++] = point;
2222158acfadSMatthew G. Knepley       }
2223efa14ee0SMatthew G Knepley     }
2224efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2225efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
22260298fd71SBarry Smith       PetscInt      *closure = NULL;
2227efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2228efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2229efa14ee0SMatthew G Knepley 
2230efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
2231efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
223282f516ccSBarry Smith       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2233efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2234efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2235efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2236efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2237efa14ee0SMatthew G Knepley 
2238efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2239efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2240830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2241efa14ee0SMatthew G Knepley         }
2242efa14ee0SMatthew G Knepley       }
2243efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2244efa14ee0SMatthew G Knepley         const PetscInt *support;
2245efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2246efa14ee0SMatthew G Knepley 
2247efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2248efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2249efa14ee0SMatthew G Knepley 
2250efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2251efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2252efa14ee0SMatthew G Knepley               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2253efa14ee0SMatthew G Knepley               break;
2254efa14ee0SMatthew G Knepley             }
2255efa14ee0SMatthew G Knepley           }
2256efa14ee0SMatthew G Knepley         }
2257efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2258efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2259efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2260efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2261efa14ee0SMatthew G Knepley         }
2262efa14ee0SMatthew G Knepley       }
2263efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2264efa14ee0SMatthew G Knepley     }
2265efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2266efa14ee0SMatthew G Knepley   }
226734b4c39eSMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);}
2268efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2269efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2270efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2271efa14ee0SMatthew G Knepley }
2272efa14ee0SMatthew G Knepley 
227327c04023SMatthew 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)
2274766ab985SMatthew G. Knepley {
227527c04023SMatthew G. Knepley   DMLabel         label = NULL;
2276766ab985SMatthew G. Knepley   const PetscInt *cone;
22779fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2278766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
2279766ab985SMatthew G. Knepley 
2280812bfc34SJed Brown   PetscFunctionBegin;
2281c0ed958bSJed Brown   *numFaces = 0;
2282c0ed958bSJed Brown   *nFV = 0;
2283c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
2284fed694aaSMatthew G. Knepley   *subCells = NULL;
2285c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2286766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2287766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2288766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
228927c04023SMatthew G. Knepley   if (label) {
229027c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
229127c04023SMatthew G. Knepley       PetscInt val;
229227c04023SMatthew G. Knepley 
229327c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
229427c04023SMatthew G. Knepley       if (val == value) {
229527c04023SMatthew G. Knepley         ++(*numFaces);
229627c04023SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
229727c04023SMatthew G. Knepley       }
229827c04023SMatthew G. Knepley     }
229927c04023SMatthew G. Knepley   } else {
2300766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
230127c04023SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
230227c04023SMatthew G. Knepley   }
2303785e854fSJed Brown   ierr = PetscMalloc1(*numFaces *2, subCells);CHKERRQ(ierr);
23049fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
23059fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2306766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2307766ab985SMatthew G. Knepley     const PetscInt *cells;
2308766ab985SMatthew G. Knepley     PetscInt        numCells;
2309766ab985SMatthew G. Knepley 
231027c04023SMatthew G. Knepley     if (label) {
231127c04023SMatthew G. Knepley       PetscInt val;
231227c04023SMatthew G. Knepley 
231327c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
231427c04023SMatthew G. Knepley       if (val != value) continue;
231527c04023SMatthew G. Knepley     }
2316766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2317766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2318766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
2319766ab985SMatthew G. Knepley     }
2320766ab985SMatthew G. Knepley     /* Negative face */
2321766ab985SMatthew G. Knepley     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
232227234c99SMatthew G. Knepley     /* Not true in parallel
232327234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2324766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2325766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
232627234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2327766ab985SMatthew G. Knepley     }
2328766ab985SMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2329766ab985SMatthew G. Knepley     /* Positive face is not included */
2330766ab985SMatthew G. Knepley   }
2331766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2332766ab985SMatthew G. Knepley }
2333766ab985SMatthew G. Knepley 
23343982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2335766ab985SMatthew G. Knepley {
2336766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2337766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2338766ab985SMatthew G. Knepley   PetscErrorCode ierr;
2339766ab985SMatthew G. Knepley 
2340812bfc34SJed Brown   PetscFunctionBegin;
2341c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2342766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2343766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2344766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2345dcca6d9dSJed Brown   ierr = PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);CHKERRQ(ierr);
2346b3154360SMatthew G. Knepley   for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);}
2347766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2348766ab985SMatthew G. Knepley     const PetscInt *cone;
2349766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2350b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2351766ab985SMatthew G. Knepley 
235227c04023SMatthew G. Knepley     if (label) {
235327c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
235427c04023SMatthew G. Knepley       if (val != value) continue;
235527c04023SMatthew G. Knepley     }
2356766ab985SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2357766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2358b3154360SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr);
2359b3154360SMatthew G. Knepley     if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2360b3154360SMatthew G. Knepley     /* Negative face */
2361766ab985SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2362766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2363766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2364766ab985SMatthew G. Knepley 
2365766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2366766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2367766ab985SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2368766ab985SMatthew G. Knepley           break;
2369766ab985SMatthew G. Knepley         }
2370766ab985SMatthew G. Knepley       }
2371766ab985SMatthew G. Knepley     }
2372766ab985SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2373766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2374766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2375766ab985SMatthew G. Knepley       const PetscInt *support;
2376766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2377766ab985SMatthew G. Knepley 
2378766ab985SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
2379711de394SMatthew G. Knepley       /* if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2380766ab985SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
2381766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2382766ab985SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2383766ab985SMatthew G. Knepley       }
2384766ab985SMatthew G. Knepley     }
2385766ab985SMatthew G. Knepley   }
2386766ab985SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2387766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2388766ab985SMatthew G. Knepley }
2389766ab985SMatthew G. Knepley 
2390c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2391e6ccafaeSMatthew G Knepley {
239282f516ccSBarry Smith   MPI_Comm       comm;
2393e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2394e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2395e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2396e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
2397e6ccafaeSMatthew G Knepley 
239882f516ccSBarry Smith   PetscFunctionBegin;
239982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2400c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
2401367003a6SStefano Zampini   if (debug) {ierr = PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
2402e6ccafaeSMatthew G Knepley 
2403ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2404ddeab2a6SMatthew G. Knepley     /* Triangle */
2405e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2406e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2407ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2408ddeab2a6SMatthew G. Knepley     /* Triangle */
2409ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2410ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2411ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2412ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2413ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2414ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2415e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2416e6ccafaeSMatthew G Knepley     /* Quadratic line */
2417e6ccafaeSMatthew G Knepley     faceSize  = 1;
2418e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2419e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2420e6ccafaeSMatthew G Knepley     /* Quads */
2421e6ccafaeSMatthew G Knepley     faceSize = 2;
2422e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2423e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2424e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2425e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2426e6ccafaeSMatthew G Knepley     } else {
2427e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2428e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2429e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2430e6ccafaeSMatthew G Knepley     }
2431e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2432e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2433e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2434e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2435e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2436e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2437e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2438e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2439e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2440e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2441e6ccafaeSMatthew G Knepley     };
2442e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2443e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2444e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2445e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2446e6ccafaeSMatthew G Knepley     };
2447e6ccafaeSMatthew G Knepley 
2448e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2449e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
2450e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2451e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2452e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2453e6ccafaeSMatthew G Knepley 
2454e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2455e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2456e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2457e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2458e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2459e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2460e6ccafaeSMatthew G Knepley               break;
2461e6ccafaeSMatthew G Knepley             }
2462e6ccafaeSMatthew G Knepley           }
2463e6ccafaeSMatthew G Knepley         }
2464e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2465e6ccafaeSMatthew G Knepley         break;
2466e6ccafaeSMatthew G Knepley       }
2467e6ccafaeSMatthew G Knepley     }
2468e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2469e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2470e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2471e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2472e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2473e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2474e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2475e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2476e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2477e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2478e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2479e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2480e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2481e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2482e6ccafaeSMatthew G Knepley     };
2483e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2484e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2485e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2486e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2487e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2488e6ccafaeSMatthew G Knepley     };
2489e6ccafaeSMatthew G Knepley 
2490e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2491e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
2492e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2493e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2494e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2495e6ccafaeSMatthew G Knepley 
2496e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2497e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2498e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2499e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2500e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2501e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2502e6ccafaeSMatthew G Knepley               break;
2503e6ccafaeSMatthew G Knepley             }
2504e6ccafaeSMatthew G Knepley           }
2505e6ccafaeSMatthew G Knepley         }
2506e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2507e6ccafaeSMatthew G Knepley         break;
2508e6ccafaeSMatthew G Knepley       }
2509e6ccafaeSMatthew G Knepley     }
2510e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2511e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2512e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2513e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2514e6ccafaeSMatthew G Knepley     /* Hexes
2515e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2516e6ccafaeSMatthew G Knepley        pointing up at the second.
2517e6ccafaeSMatthew G Knepley 
2518e6ccafaeSMatthew G Knepley           7---6
2519e6ccafaeSMatthew G Knepley          /|  /|
2520e6ccafaeSMatthew G Knepley         4---5 |
2521ddeab2a6SMatthew G. Knepley         | 1-|-2
2522e6ccafaeSMatthew G Knepley         |/  |/
2523ddeab2a6SMatthew G. Knepley         0---3
2524e6ccafaeSMatthew G Knepley 
2525e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2526e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2527e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2528e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2529e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2530e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2531e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2532ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2533ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2534ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2535ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2536e6ccafaeSMatthew G Knepley     };
2537e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2538ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2539e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2540ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2541ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2542ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2543ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2544e6ccafaeSMatthew G Knepley     };
2545e6ccafaeSMatthew G Knepley 
2546e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2547e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
2548e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2549e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2550e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2551e6ccafaeSMatthew G Knepley 
2552e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2553e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2554e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2555e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2556e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2557e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2558e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2559e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2560e6ccafaeSMatthew G Knepley               break;
2561e6ccafaeSMatthew G Knepley             }
2562e6ccafaeSMatthew G Knepley           }
2563e6ccafaeSMatthew G Knepley         }
2564e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2565e6ccafaeSMatthew G Knepley         break;
2566e6ccafaeSMatthew G Knepley       }
2567e6ccafaeSMatthew G Knepley     }
2568e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2569e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2570e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2571e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2572e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2573e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2574e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2575e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2576e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2577e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2578e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2579e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2580e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2581e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2582e6ccafaeSMatthew G Knepley     };
2583e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2584e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2585e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2586e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2587e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2588e6ccafaeSMatthew G Knepley     };
2589e6ccafaeSMatthew G Knepley 
2590e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2591e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
2592e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2593e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2594e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2595e6ccafaeSMatthew G Knepley 
2596e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2597e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2598e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2599e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2600e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2601e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2602e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2603e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2604e6ccafaeSMatthew G Knepley               break;
2605e6ccafaeSMatthew G Knepley             }
2606e6ccafaeSMatthew G Knepley           }
2607e6ccafaeSMatthew G Knepley         }
2608e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2609e6ccafaeSMatthew G Knepley         break;
2610e6ccafaeSMatthew G Knepley       }
2611e6ccafaeSMatthew G Knepley     }
2612e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2613e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2614e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2615e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2616e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2617e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2618e6ccafaeSMatthew G Knepley        pointing up at the second.
2619e6ccafaeSMatthew G Knepley 
2620e6ccafaeSMatthew G Knepley          7---6
2621e6ccafaeSMatthew G Knepley         /|  /|
2622e6ccafaeSMatthew G Knepley        4---5 |
2623e6ccafaeSMatthew G Knepley        | 3-|-2
2624e6ccafaeSMatthew G Knepley        |/  |/
2625e6ccafaeSMatthew G Knepley        0---1
2626e6ccafaeSMatthew G Knepley 
2627e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2628e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2629e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2630e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2631e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2632e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2633e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2634e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2635e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2636e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2637e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2638e6ccafaeSMatthew G Knepley     };
2639e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2640e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2641e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2642e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2643e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2644e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2645e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2646e6ccafaeSMatthew G Knepley     };
2647e6ccafaeSMatthew G Knepley 
2648e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2649e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
2650e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2651e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2652e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2653e6ccafaeSMatthew G Knepley 
2654e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2655e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2656e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2657e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2658e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2659e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2660e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2661e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2662e6ccafaeSMatthew G Knepley               break;
2663e6ccafaeSMatthew G Knepley             }
2664e6ccafaeSMatthew G Knepley           }
2665e6ccafaeSMatthew G Knepley         }
2666e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2667e6ccafaeSMatthew G Knepley         break;
2668e6ccafaeSMatthew G Knepley       }
2669e6ccafaeSMatthew G Knepley     }
2670e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2671e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2672e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2673e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2674e6ccafaeSMatthew G Knepley   if (!posOrient) {
2675e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
2676e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2677e6ccafaeSMatthew G Knepley   } else {
2678e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
2679e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2680e6ccafaeSMatthew G Knepley   }
2681e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2682e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2683e6ccafaeSMatthew G Knepley }
2684e6ccafaeSMatthew G Knepley 
2685c08575a3SMatthew G. Knepley /*@
2686c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2687c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2688c08575a3SMatthew G. Knepley 
2689c08575a3SMatthew G. Knepley   Not collective
2690c08575a3SMatthew G. Knepley 
2691c08575a3SMatthew G. Knepley   Input Parameters:
2692c08575a3SMatthew G. Knepley + dm           - The original mesh
2693c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2694c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2695c08575a3SMatthew G. Knepley . face         - The face vertices
2696c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2697c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2698c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2699c08575a3SMatthew G. Knepley 
2700c08575a3SMatthew G. Knepley   Output Parameter:
2701c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2702c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2703c08575a3SMatthew G. Knepley 
2704c08575a3SMatthew G. Knepley   Level: developer
2705c08575a3SMatthew G. Knepley 
2706c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2707c08575a3SMatthew G. Knepley @*/
2708e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2709e6ccafaeSMatthew G Knepley {
27100298fd71SBarry Smith   const PetscInt *cone = NULL;
2711e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2712e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2713e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2714e6ccafaeSMatthew G Knepley 
2715e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2716e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
2717e6ccafaeSMatthew G Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2718e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2719e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2720e6ccafaeSMatthew G Knepley 
2721e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2722e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2723e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2724e6ccafaeSMatthew G Knepley       }
2725e6ccafaeSMatthew G Knepley     }
2726e6ccafaeSMatthew G Knepley     if (found) {
2727e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2728e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2729e6ccafaeSMatthew G Knepley       ++v2;
2730e6ccafaeSMatthew G Knepley     } else {
2731e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2732e6ccafaeSMatthew G Knepley     }
2733e6ccafaeSMatthew G Knepley   }
2734e6ccafaeSMatthew G Knepley   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
2735e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2736e6ccafaeSMatthew G Knepley }
2737e6ccafaeSMatthew G Knepley 
2738e6ccafaeSMatthew G Knepley /*
2739cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2740e6ccafaeSMatthew G Knepley 
2741e6ccafaeSMatthew G Knepley   Not collective
2742e6ccafaeSMatthew G Knepley 
2743e6ccafaeSMatthew G Knepley   Input Parameters:
2744e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2745e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2746e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2747e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2748e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2749e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2750e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2751e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2752e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2753e6ccafaeSMatthew G Knepley 
2754e6ccafaeSMatthew G Knepley   Output Parameters:
2755e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2756e6ccafaeSMatthew G Knepley 
2757e6ccafaeSMatthew G Knepley   Level: developer
2758e6ccafaeSMatthew G Knepley */
2759cd0c2139SMatthew 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)
2760e6ccafaeSMatthew G Knepley {
276182f516ccSBarry Smith   MPI_Comm        comm;
2762e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2763e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2764e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2765e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2766e6ccafaeSMatthew G Knepley 
2767e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
276882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2769e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
2770e6ccafaeSMatthew G Knepley   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2771e6ccafaeSMatthew G Knepley #if 0
2772e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2773e6ccafaeSMatthew G Knepley   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2774e6ccafaeSMatthew G Knepley #else
2775e6ccafaeSMatthew G Knepley   {
2776e6ccafaeSMatthew G Knepley     PetscInt f;
2777e6ccafaeSMatthew G Knepley 
2778e6ccafaeSMatthew G Knepley     numFaces = 0;
277969291d52SBarry Smith     ierr     = DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2780e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2781e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2782e6ccafaeSMatthew G Knepley 
2783e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
2784e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
2785e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2786e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2787e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2788e6ccafaeSMatthew G Knepley         PetscInt       v;
2789e6ccafaeSMatthew G Knepley 
2790e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2791e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2792e6ccafaeSMatthew G Knepley         }
2793e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2794e6ccafaeSMatthew G Knepley       }
2795e6ccafaeSMatthew G Knepley       if (d == dof) {
2796e6ccafaeSMatthew G Knepley         numFaces               = 1;
2797e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2798e6ccafaeSMatthew G Knepley       }
2799e6ccafaeSMatthew G Knepley     }
2800e6ccafaeSMatthew G Knepley   }
2801e6ccafaeSMatthew G Knepley #endif
2802e6ccafaeSMatthew G Knepley   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2803e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2804e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2805766ab985SMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
2806e6ccafaeSMatthew G Knepley   } else {
2807e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2808e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2809e6ccafaeSMatthew G Knepley 
281069291d52SBarry Smith     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2811e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2812e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2813e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2814e6ccafaeSMatthew G Knepley     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
2815e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2816e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2817e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2818e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2819e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2820e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2821e6ccafaeSMatthew G Knepley           break;
2822e6ccafaeSMatthew G Knepley         }
2823e6ccafaeSMatthew G Knepley       }
2824e6ccafaeSMatthew G Knepley       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2825e6ccafaeSMatthew G Knepley     }
2826e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2827e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
282869291d52SBarry Smith     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2829e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2830e6ccafaeSMatthew G Knepley   }
2831ef07cca7SMatthew G. Knepley #if 0
2832e6ccafaeSMatthew G Knepley   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2833ef07cca7SMatthew G. Knepley #else
283469291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2835ef07cca7SMatthew G. Knepley #endif
2836e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2837e6ccafaeSMatthew G Knepley }
2838e6ccafaeSMatthew G Knepley 
283953156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2840e6ccafaeSMatthew G Knepley {
284182f516ccSBarry Smith   MPI_Comm        comm;
284253156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2843efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2844efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2845efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2846fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2847efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2848e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2849e6ccafaeSMatthew G Knepley 
2850e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
285182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2852efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2853d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2854efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2855efa14ee0SMatthew G Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
285653156dfcSMatthew G. Knepley   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2857efa14ee0SMatthew G Knepley   /* Setup chart */
2858efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2859efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2860efa14ee0SMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2861efa14ee0SMatthew G Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2862e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2863e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2864efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2865e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2866efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2867efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2868efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2869efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2870e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2871e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2872e6ccafaeSMatthew G Knepley   }
2873e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2874e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2875e6ccafaeSMatthew G Knepley   }
2876e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2877e6ccafaeSMatthew G Knepley   /* Create face cones */
2878efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
28790298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
288069291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2881e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2882e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2883efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
28840298fd71SBarry Smith     PetscInt      *closure = NULL;
2885efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2886e6ccafaeSMatthew G Knepley 
2887e6ccafaeSMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2888efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2889efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2890e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2891e6ccafaeSMatthew G Knepley 
2892efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2893efa14ee0SMatthew G Knepley         ++numCorners;
2894efa14ee0SMatthew G Knepley         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2895efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2896efa14ee0SMatthew G Knepley           closure[faceSize] = point;
289765560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2898e6ccafaeSMatthew G Knepley           ++faceSize;
2899e6ccafaeSMatthew G Knepley         }
2900e6ccafaeSMatthew G Knepley       }
2901e6ccafaeSMatthew G Knepley     }
2902efa14ee0SMatthew G Knepley     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2903efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2904cd0c2139SMatthew G Knepley       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2905e6ccafaeSMatthew G Knepley     }
290665560c7fSMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2907e6ccafaeSMatthew G Knepley   }
290869291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2909e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2910e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2911e6ccafaeSMatthew G Knepley   /* Build coordinates */
2912efa14ee0SMatthew G Knepley   {
2913efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2914efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2915efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2916285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
291724640c55SToby Isaac     const char  *name;
2918efa14ee0SMatthew G Knepley 
291969d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2920e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
292169d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2922285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2923285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2924285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2925efa14ee0SMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2926efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
2927efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
2928efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
2929efa14ee0SMatthew G Knepley       PetscInt       dof;
2930efa14ee0SMatthew G Knepley 
2931efa14ee0SMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2932efa14ee0SMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2933285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2934e6ccafaeSMatthew G Knepley     }
2935e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2936e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
29378b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
293824640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
293924640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
2940e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
29412eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2942830e53efSMatthew G. Knepley     if (coordSize) {
2943e6ccafaeSMatthew G Knepley       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2944e6ccafaeSMatthew G Knepley       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2945efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
2946efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
2947efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
2948efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
2949e6ccafaeSMatthew G Knepley 
2950e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2951e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2952efa14ee0SMatthew G Knepley         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2953efa14ee0SMatthew G Knepley         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2954efa14ee0SMatthew 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);
2955e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2956e6ccafaeSMatthew G Knepley       }
2957e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2958e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
29593b399e24SMatthew G. Knepley     }
2960e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2961e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2962e6ccafaeSMatthew G Knepley   }
2963efa14ee0SMatthew G Knepley   /* Cleanup */
2964efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2965efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2966efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2967efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
2968e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2969e6ccafaeSMatthew G Knepley }
2970e6ccafaeSMatthew G Knepley 
29713982b651SMatthew G. Knepley PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
29723982b651SMatthew G. Knepley {
29733982b651SMatthew G. Knepley   PetscInt       subPoint;
29743982b651SMatthew G. Knepley   PetscErrorCode ierr;
29753982b651SMatthew G. Knepley 
29763982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
29773982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
29783982b651SMatthew G. Knepley }
29793982b651SMatthew G. Knepley 
2980158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
2981e6ccafaeSMatthew G Knepley {
298282f516ccSBarry Smith   MPI_Comm         comm;
298353156dfcSMatthew G. Knepley   DMLabel          subpointMap;
2984efa14ee0SMatthew G Knepley   IS              *subpointIS;
2985efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
29863982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
29870d366550SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, cMax, cEnd, dim, p, d, v;
29880d366550SMatthew G. Knepley   PetscMPIInt      rank;
2989e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
2990e6ccafaeSMatthew G Knepley 
2991e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
299282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
29930d366550SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2994efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2995d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2996efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2997bec263e5SMatthew G. Knepley   if (cellHeight) {
29983982b651SMatthew G. Knepley     if (isCohesive) {ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
2999158acfadSMatthew G. Knepley     else            {ierr = DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm);CHKERRQ(ierr);}
3000bec263e5SMatthew G. Knepley   } else {
3001bec263e5SMatthew G. Knepley     DMLabel         depth;
3002bec263e5SMatthew G. Knepley     IS              pointIS;
3003bec263e5SMatthew G. Knepley     const PetscInt *points;
3004bec263e5SMatthew G. Knepley     PetscInt        numPoints;
3005bec263e5SMatthew G. Knepley 
3006bec263e5SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
3007bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, value, &numPoints);CHKERRQ(ierr);
3008bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, value, &pointIS);CHKERRQ(ierr);
3009bec263e5SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
3010bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3011bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3012bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3013bec263e5SMatthew G. Knepley 
3014bec263e5SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3015bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
3016bec263e5SMatthew G. Knepley         ierr = DMLabelGetValue(depth, closure[c], &pdim);CHKERRQ(ierr);
3017bec263e5SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, closure[c], pdim);CHKERRQ(ierr);
3018bec263e5SMatthew G. Knepley       }
3019bec263e5SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3020bec263e5SMatthew G. Knepley     }
3021bec263e5SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
3022bec263e5SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
3023bec263e5SMatthew G. Knepley   }
30240d366550SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
30250d366550SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
30260d366550SMatthew G. Knepley   cMax = (cMax < 0) ? cEnd : cMax;
3027efa14ee0SMatthew G Knepley   /* Setup chart */
3028c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3029dcca6d9dSJed Brown   ierr = PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);CHKERRQ(ierr);
3030e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3031e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
3032e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3033e6ccafaeSMatthew G Knepley   }
3034e6ccafaeSMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
3035bec263e5SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, cellHeight);CHKERRQ(ierr);
3036e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3037e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
3038e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
3039e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
3040e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
3041e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3042e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
30430ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3044e6ccafaeSMatthew G Knepley   }
3045e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3046e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3047e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3048e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3049e6ccafaeSMatthew G Knepley       const PetscInt *cone;
3050e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
3051e6ccafaeSMatthew G Knepley 
3052e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3053e6ccafaeSMatthew G Knepley       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
3054bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
3055e6ccafaeSMatthew G Knepley         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3056e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3057e6ccafaeSMatthew G Knepley           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
3058e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3059e6ccafaeSMatthew G Knepley         }
3060e6ccafaeSMatthew G Knepley         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
3061e6ccafaeSMatthew G Knepley       }
3062e6ccafaeSMatthew G Knepley     }
3063e6ccafaeSMatthew G Knepley   }
3064d67d17b1SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
3065e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3066e6ccafaeSMatthew G Knepley   /* Set cones */
30670298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
30683982b651SMatthew G. Knepley   ierr = PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);CHKERRQ(ierr);
3069e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3070e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3071e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3072e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
30730e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
30740d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3075e6ccafaeSMatthew G Knepley 
30760d366550SMatthew G. Knepley       if (d == dim-1) {
30770d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
30780d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
30790d366550SMatthew G. Knepley 
30800d366550SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
30810d366550SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
30820d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
30830d366550SMatthew G. Knepley           if ((support[s] < cMax) || (support[s] >= cEnd)) continue;
30840d366550SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc);CHKERRQ(ierr);
30850d366550SMatthew G. Knepley           if (subc >= 0) {
30860d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
30870d366550SMatthew G. Knepley 
30887f79407eSBarry Smith             ierr = DMPlexGetCone(dm, ccell, &cone);CHKERRQ(ierr);
30897f79407eSBarry Smith             ierr = DMPlexGetConeSize(dm, ccell, &coneSize);CHKERRQ(ierr);
30907f79407eSBarry Smith             ierr = DMPlexGetConeOrientation(dm, ccell, &ornt);CHKERRQ(ierr);
30910d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
30920d366550SMatthew G. Knepley               if (cone[c] == point) {
30930d366550SMatthew G. Knepley                 fornt = ornt[c];
30940d366550SMatthew G. Knepley                 break;
30950d366550SMatthew G. Knepley               }
30960d366550SMatthew G. Knepley             }
30970d366550SMatthew G. Knepley             break;
30980d366550SMatthew G. Knepley           }
30990d366550SMatthew G. Knepley         }
31000d366550SMatthew G. Knepley       }
3101e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3102e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
3103e6ccafaeSMatthew G Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
31040e49e2e2SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
3105e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3106e6ccafaeSMatthew G Knepley         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
310701a2673eSMatthew G. Knepley         if (subc >= 0) {
310801a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
31093982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
311001a2673eSMatthew G. Knepley           ++coneSizeNew;
311101a2673eSMatthew G. Knepley         }
3112e6ccafaeSMatthew G Knepley       }
3113e6ccafaeSMatthew G Knepley       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
31140d366550SMatthew G. Knepley       if (fornt < 0) {
31150d366550SMatthew G. Knepley         /* This should be replaced by a call to DMPlexReverseCell() */
31160d366550SMatthew G. Knepley #if 0
3117a129c400SMatthew G. Knepley         ierr = DMPlexReverseCell(subdm, subpoint);CHKERRQ(ierr);
3118a129c400SMatthew G. Knepley #else
3119c0b40891SMatthew G. Knepley         for (c = 0; c < coneSizeNew/2 + coneSizeNew%2; ++c) {
3120a129c400SMatthew G. Knepley           PetscInt faceSize, tmp;
31210d366550SMatthew G. Knepley 
31220d366550SMatthew G. Knepley           tmp        = coneNew[c];
31230d366550SMatthew G. Knepley           coneNew[c] = coneNew[coneSizeNew-1-c];
31240d366550SMatthew G. Knepley           coneNew[coneSizeNew-1-c] = tmp;
3125a129c400SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, cone[c], &faceSize);CHKERRQ(ierr);
3126a129c400SMatthew G. Knepley           tmp        = orntNew[c] >= 0 ? -(faceSize-orntNew[c]) : faceSize+orntNew[c];
3127a129c400SMatthew G. Knepley           orntNew[c] = orntNew[coneSizeNew-1-c] >= 0 ? -(faceSize-orntNew[coneSizeNew-1-c]) : faceSize+orntNew[coneSizeNew-1-c];
31280d366550SMatthew G. Knepley           orntNew[coneSizeNew-1-c] = tmp;
31290d366550SMatthew G. Knepley         }
31300d366550SMatthew G. Knepley       }
3131e6ccafaeSMatthew G Knepley       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
31323982b651SMatthew G. Knepley       ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr);
3133a129c400SMatthew G. Knepley #endif
3134e6ccafaeSMatthew G Knepley     }
3135e6ccafaeSMatthew G Knepley   }
31363982b651SMatthew G. Knepley   ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr);
3137e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3138e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3139e6ccafaeSMatthew G Knepley   /* Build coordinates */
3140e6ccafaeSMatthew G Knepley   {
3141e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3142e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3143e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3144c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
314524640c55SToby Isaac     const char  *name;
3146e6ccafaeSMatthew G Knepley 
3147c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
314869d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3149e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
315069d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3151285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3152285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3153285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3154e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
3155e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3156e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3157e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3158e6ccafaeSMatthew G Knepley       PetscInt       dof;
3159e6ccafaeSMatthew G Knepley 
3160e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3161e6ccafaeSMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3162285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3163e6ccafaeSMatthew G Knepley     }
3164e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3165e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
31668b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
316724640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
316824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3169e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3170c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
31712eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3172e6ccafaeSMatthew G Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3173e6ccafaeSMatthew G Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3174e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3175e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3176e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3177e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3178e6ccafaeSMatthew G Knepley 
3179e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3180e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3181e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3182e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3183e6ccafaeSMatthew 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);
3184efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3185e6ccafaeSMatthew G Knepley     }
3186e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3187e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3188e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3189e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3190e6ccafaeSMatthew G Knepley   }
31913982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
31923982b651SMatthew G. Knepley   {
31933982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
31943982b651SMatthew G. Knepley     IS                 subpIS;
31953982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
31963982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
31973982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
31983982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
31993982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
32003982b651SMatthew G. Knepley     PetscMPIInt        rank;
32013982b651SMatthew G. Knepley 
32023982b651SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
32033982b651SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
32043982b651SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
32053982b651SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
32063982b651SMatthew G. Knepley     ierr = DMPlexGetChart(subdm, NULL, &numSubroots);CHKERRQ(ierr);
32073982b651SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpIS);CHKERRQ(ierr);
32083982b651SMatthew G. Knepley     if (subpIS) {
32093982b651SMatthew G. Knepley       ierr = ISGetIndices(subpIS, &subpoints);CHKERRQ(ierr);
32103982b651SMatthew G. Knepley       ierr = ISGetLocalSize(subpIS, &numSubpoints);CHKERRQ(ierr);
32113982b651SMatthew G. Knepley     }
32123982b651SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
32133982b651SMatthew G. Knepley     if (numRoots >= 0) {
32143982b651SMatthew G. Knepley       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
32153982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
32163982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
32173982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
32183982b651SMatthew G. Knepley       }
32193982b651SMatthew G. Knepley       /* Set subleaves */
32203982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
32213982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
32223982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
32233982b651SMatthew G. Knepley 
32243982b651SMatthew G. Knepley         if (subpoint < 0) continue;
32253982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
32263982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
32273982b651SMatthew G. Knepley         ++numSubleaves;
32283982b651SMatthew G. Knepley       }
32293982b651SMatthew G. Knepley       /* Must put in owned subpoints */
32303982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
32313982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
32323982b651SMatthew G. Knepley 
32333982b651SMatthew G. Knepley         if (subpoint < 0) {
32343982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
32353982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
32363982b651SMatthew G. Knepley         } else {
32373982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
32383982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
32393982b651SMatthew G. Knepley         }
32403982b651SMatthew G. Knepley       }
32413982b651SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
32423982b651SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
32433982b651SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
32443982b651SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
32453982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &slocalPoints);CHKERRQ(ierr);
32463982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &sremotePoints);CHKERRQ(ierr);
32473982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
32483982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
32493982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
32503982b651SMatthew G. Knepley 
32513982b651SMatthew G. Knepley         if (subpoint < 0) continue;
32523982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
32533982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
32543982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
32553982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
32563982b651SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
32573982b651SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
32583982b651SMatthew G. Knepley         ++sl;
32593982b651SMatthew G. Knepley       }
32603982b651SMatthew G. Knepley       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
32613982b651SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
32623982b651SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
32633982b651SMatthew G. Knepley     }
32643982b651SMatthew G. Knepley     if (subpIS) {
32653982b651SMatthew G. Knepley       ierr = ISRestoreIndices(subpIS, &subpoints);CHKERRQ(ierr);
32663982b651SMatthew G. Knepley       ierr = ISDestroy(&subpIS);CHKERRQ(ierr);
32673982b651SMatthew G. Knepley     }
32683982b651SMatthew G. Knepley   }
3269efa14ee0SMatthew G Knepley   /* Cleanup */
3270e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32710ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3272e6ccafaeSMatthew G Knepley     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
3273e6ccafaeSMatthew G Knepley   }
3274efa14ee0SMatthew G Knepley   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
3275e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3276e6ccafaeSMatthew G Knepley }
3277e6ccafaeSMatthew G Knepley 
3278158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
32793982b651SMatthew G. Knepley {
32803982b651SMatthew G. Knepley   PetscErrorCode ierr;
32813982b651SMatthew G. Knepley 
32823982b651SMatthew G. Knepley   PetscFunctionBegin;
3283158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm);CHKERRQ(ierr);
32843982b651SMatthew G. Knepley   PetscFunctionReturn(0);
32853982b651SMatthew G. Knepley }
32863982b651SMatthew G. Knepley 
3287d0fa310fSMatthew G. Knepley /*@
3288e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3289e6ccafaeSMatthew G Knepley 
3290e6ccafaeSMatthew G Knepley   Input Parameters:
3291e6ccafaeSMatthew G Knepley + dm           - The original mesh
3292158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3293158acfadSMatthew G. Knepley . value        - The label value to use
3294158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3295e6ccafaeSMatthew G Knepley 
3296e6ccafaeSMatthew G Knepley   Output Parameter:
3297e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3298e6ccafaeSMatthew G Knepley 
3299e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3300e6ccafaeSMatthew G Knepley 
3301e6ccafaeSMatthew G Knepley   Level: developer
3302e6ccafaeSMatthew G Knepley 
3303c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3304830e53efSMatthew G. Knepley @*/
3305158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3306e6ccafaeSMatthew G Knepley {
3307827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3308827c4036SVaclav Hapla   PetscInt       dim, cdim;
3309e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3310e6ccafaeSMatthew G Knepley 
3311e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3312e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3313766ab985SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3314c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
331582f516ccSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3316e6ccafaeSMatthew G Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3317c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3318dd2763adSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3319dd2763adSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3320827c4036SVaclav Hapla   ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr);
3321827c4036SVaclav Hapla   if (interpolated == DMPLEX_INTERPOLATED_PARTIAL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3322827c4036SVaclav Hapla   if (interpolated) {
3323158acfadSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm);CHKERRQ(ierr);
3324e6ccafaeSMatthew G Knepley   } else {
3325830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3326e6ccafaeSMatthew G Knepley   }
3327e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3328e6ccafaeSMatthew G Knepley }
3329e6ccafaeSMatthew G Knepley 
333027c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3331766ab985SMatthew G. Knepley {
3332766ab985SMatthew G. Knepley   MPI_Comm        comm;
3333766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3334766ab985SMatthew G. Knepley   IS              subvertexIS;
3335766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3336fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3337766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
333887feddfdSMatthew G. Knepley   PetscInt        cMax, c, f;
3339766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
3340766ab985SMatthew G. Knepley 
3341766ab985SMatthew G. Knepley   PetscFunctionBegin;
3342766ab985SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
3343766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3344d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3345766ab985SMatthew G. Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3346766ab985SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
334727c04023SMatthew G. Knepley   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
3348766ab985SMatthew G. Knepley   /* Setup chart */
3349766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
3350766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
3351766ab985SMatthew G. Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
3352766ab985SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
3353766ab985SMatthew G. Knepley   /* Set cone sizes */
3354766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3355766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3356766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3357766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
3358766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3359766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3360766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
3361766ab985SMatthew G. Knepley   }
3362766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3363766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
3364766ab985SMatthew G. Knepley   }
3365766ab985SMatthew G. Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3366766ab985SMatthew G. Knepley   /* Create face cones */
3367766ab985SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
336887feddfdSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
336969291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3370766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3371766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3372766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
337387feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
337487feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3375766ab985SMatthew G. Knepley 
337687feddfdSMatthew G. Knepley     if (cell < cMax) continue;
337787feddfdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
337887feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
337987feddfdSMatthew G. Knepley       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
338087feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
338187feddfdSMatthew G. Knepley     }
338287feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
338387feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
338487feddfdSMatthew G. Knepley     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
338527234c99SMatthew G. Knepley     /* Not true in parallel
338627234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
338787feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
338887feddfdSMatthew G. Knepley       PetscInt negsubcell;
3389766ab985SMatthew G. Knepley 
339087feddfdSMatthew G. Knepley       if (cells[p] >= cMax) continue;
339187feddfdSMatthew G. Knepley       /* I know this is a crap search */
339287feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
339387feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3394766ab985SMatthew G. Knepley       }
339587feddfdSMatthew G. Knepley       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
339687feddfdSMatthew G. Knepley       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
3397766ab985SMatthew G. Knepley     }
339887feddfdSMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
339987feddfdSMatthew G. Knepley     ++newFacePoint;
3400766ab985SMatthew G. Knepley   }
340169291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3402766ab985SMatthew G. Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3403766ab985SMatthew G. Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3404766ab985SMatthew G. Knepley   /* Build coordinates */
3405766ab985SMatthew G. Knepley   {
3406766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3407766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3408766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3409c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
341024640c55SToby Isaac     const char  *name;
3411766ab985SMatthew G. Knepley 
3412c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
341369d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3414766ab985SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
341569d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3416285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3417285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3418285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3419766ab985SMatthew G. Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3420766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3421766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3422766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3423766ab985SMatthew G. Knepley       PetscInt       dof;
3424766ab985SMatthew G. Knepley 
3425766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3426766ab985SMatthew G. Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3427285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3428766ab985SMatthew G. Knepley     }
3429766ab985SMatthew G. Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3430766ab985SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
34318b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
343224640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
343324640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3434766ab985SMatthew G. Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3435c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
34362eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3437766ab985SMatthew G. Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3438766ab985SMatthew G. Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3439766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3440766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3441766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3442766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3443766ab985SMatthew G. Knepley 
3444766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3445766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3446766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3447766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3448766ab985SMatthew 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);
3449766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3450766ab985SMatthew G. Knepley     }
3451766ab985SMatthew G. Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3452766ab985SMatthew G. Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3453766ab985SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3454766ab985SMatthew G. Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3455766ab985SMatthew G. Knepley   }
3456aca35d17SMatthew G. Knepley   /* Build SF */
3457aca35d17SMatthew G. Knepley   CHKMEMQ;
3458aca35d17SMatthew G. Knepley   {
3459aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3460aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3461bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3462aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3463bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
346449c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3465bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3466aca35d17SMatthew G. Knepley 
3467bdcf2095SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3468aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
3469aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
3470aca35d17SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3471aca35d17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
3472aca35d17SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
3473aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3474aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3475dcca6d9dSJed Brown       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
3476bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3477bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3478bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3479bdcf2095SMatthew G. Knepley       }
34809e0823b2SMatthew G. Knepley       /* Set subleaves */
3481aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3482aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3483bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3484aca35d17SMatthew G. Knepley 
3485aca35d17SMatthew G. Knepley         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3486bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3487bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3488bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3489bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3490aca35d17SMatthew G. Knepley       }
34919e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
34929e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
34939e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
34949e0823b2SMatthew G. Knepley 
34959e0823b2SMatthew G. Knepley         if (subPoint < 0) {
34969e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
34979e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
34989e0823b2SMatthew G. Knepley         } else {
34999e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
35009e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
35019e0823b2SMatthew G. Knepley         }
3502bdcf2095SMatthew G. Knepley       }
3503bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3504bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3505bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3506bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3507785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves,    &slocalPoints);CHKERRQ(ierr);
3508785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves, &sremotePoints);CHKERRQ(ierr);
350949c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3510bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3511bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3512aca35d17SMatthew G. Knepley 
3513aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
351449c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3515aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3516bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3517bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
3518bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3519bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3520aca35d17SMatthew G. Knepley         ++sl;
3521aca35d17SMatthew G. Knepley       }
3522bdcf2095SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
352349c26ae4SMatthew G. Knepley       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
352449c26ae4SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3525aca35d17SMatthew G. Knepley     }
3526aca35d17SMatthew G. Knepley   }
3527aca35d17SMatthew G. Knepley   CHKMEMQ;
3528766ab985SMatthew G. Knepley   /* Cleanup */
3529766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3530766ab985SMatthew G. Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3531766ab985SMatthew G. Knepley   ierr = PetscFree(subCells);CHKERRQ(ierr);
3532766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3533766ab985SMatthew G. Knepley }
3534766ab985SMatthew G. Knepley 
35353982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3536766ab985SMatthew G. Knepley {
35373982b651SMatthew G. Knepley   DMLabel        label = NULL;
3538766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3539766ab985SMatthew G. Knepley 
3540766ab985SMatthew G. Knepley   PetscFunctionBegin;
3541c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
3542158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm);CHKERRQ(ierr);
3543766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3544766ab985SMatthew G. Knepley }
3545766ab985SMatthew G. Knepley 
3546c08575a3SMatthew G. Knepley /*@C
354727c04023SMatthew 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.
3548766ab985SMatthew G. Knepley 
3549766ab985SMatthew G. Knepley   Input Parameters:
3550766ab985SMatthew G. Knepley + dm          - The original mesh
355127c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
35527afc1a8bSJed Brown . label       - A label name, or NULL
355327c04023SMatthew G. Knepley - value  - A label value
3554766ab985SMatthew G. Knepley 
3555766ab985SMatthew G. Knepley   Output Parameter:
3556766ab985SMatthew G. Knepley . subdm - The surface mesh
3557766ab985SMatthew G. Knepley 
3558766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3559766ab985SMatthew G. Knepley 
3560766ab985SMatthew G. Knepley   Level: developer
3561766ab985SMatthew G. Knepley 
3562766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3563c08575a3SMatthew G. Knepley @*/
356427c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3565766ab985SMatthew G. Knepley {
3566c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3567766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3568766ab985SMatthew G. Knepley 
3569766ab985SMatthew G. Knepley   PetscFunctionBegin;
3570766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
357127c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3572c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3573766ab985SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3574766ab985SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3575766ab985SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3576c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3577c0e8cf5fSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3578c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3579766ab985SMatthew G. Knepley   if (depth == dim) {
3580b3154360SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr);
3581766ab985SMatthew G. Knepley   } else {
358227c04023SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
3583e6ccafaeSMatthew G Knepley   }
3584e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3585e6ccafaeSMatthew G Knepley }
3586e6ccafaeSMatthew G Knepley 
3587bec263e5SMatthew G. Knepley /*@
3588bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3589bec263e5SMatthew G. Knepley 
3590bec263e5SMatthew G. Knepley   Input Parameters:
3591bec263e5SMatthew G. Knepley + dm        - The original mesh
3592bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3593bec263e5SMatthew G. Knepley - value     - The label value to use
3594bec263e5SMatthew G. Knepley 
3595bec263e5SMatthew G. Knepley   Output Parameter:
3596bec263e5SMatthew G. Knepley . subdm - The new mesh
3597bec263e5SMatthew G. Knepley 
3598bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3599bec263e5SMatthew G. Knepley 
3600bec263e5SMatthew G. Knepley   Level: developer
3601bec263e5SMatthew G. Knepley 
3602c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3603bec263e5SMatthew G. Knepley @*/
3604bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3605bec263e5SMatthew G. Knepley {
3606bec263e5SMatthew G. Knepley   PetscInt       dim;
3607bec263e5SMatthew G. Knepley   PetscErrorCode ierr;
3608bec263e5SMatthew G. Knepley 
3609bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3610bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3611bec263e5SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3612bec263e5SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3613bec263e5SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), subdm);CHKERRQ(ierr);
3614bec263e5SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3615bec263e5SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim);CHKERRQ(ierr);
3616bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3617158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm);CHKERRQ(ierr);
3618bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3619bec263e5SMatthew G. Knepley }
3620bec263e5SMatthew G. Knepley 
362164beef6dSMatthew G. Knepley /*@
362264beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
362364beef6dSMatthew G. Knepley 
362464beef6dSMatthew G. Knepley   Input Parameter:
362564beef6dSMatthew G. Knepley . dm - The submesh DM
362664beef6dSMatthew G. Knepley 
362764beef6dSMatthew G. Knepley   Output Parameter:
362864beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
362964beef6dSMatthew G. Knepley 
363064beef6dSMatthew G. Knepley   Level: developer
363164beef6dSMatthew G. Knepley 
363264beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
363364beef6dSMatthew G. Knepley @*/
3634e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3635e6ccafaeSMatthew G Knepley {
3636e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3637e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3638e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
363965663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3640e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3641e6ccafaeSMatthew G Knepley }
3642e6ccafaeSMatthew G Knepley 
3643c08575a3SMatthew G. Knepley /*@
3644c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3645c08575a3SMatthew G. Knepley 
3646c08575a3SMatthew G. Knepley   Input Parameters:
3647c08575a3SMatthew G. Knepley + dm - The submesh DM
3648c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3649c08575a3SMatthew G. Knepley 
3650c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3651c08575a3SMatthew G. Knepley 
3652c08575a3SMatthew G. Knepley   Level: developer
3653c08575a3SMatthew G. Knepley 
3654c08575a3SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3655c08575a3SMatthew G. Knepley @*/
3656e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3657e6ccafaeSMatthew G Knepley {
3658e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3659285d324eSMatthew G. Knepley   DMLabel        tmp;
3660e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3661e6ccafaeSMatthew G Knepley 
3662e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3663e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3664285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3665e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3666d67d17b1SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) mesh->subpointMap);CHKERRQ(ierr);
3667285d324eSMatthew G. Knepley   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
3668e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3669e6ccafaeSMatthew G Knepley }
3670e6ccafaeSMatthew G Knepley 
367164beef6dSMatthew G. Knepley /*@
3672e6ccafaeSMatthew G Knepley   DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data
3673e6ccafaeSMatthew G Knepley 
3674e6ccafaeSMatthew G Knepley   Input Parameter:
3675e6ccafaeSMatthew G Knepley . dm - The submesh DM
3676e6ccafaeSMatthew G Knepley 
3677e6ccafaeSMatthew G Knepley   Output Parameter:
36780298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3679e6ccafaeSMatthew G Knepley 
36803982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
368164beef6dSMatthew G. Knepley 
368264beef6dSMatthew G. Knepley   Level: developer
368364beef6dSMatthew G. Knepley 
368464beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
368564beef6dSMatthew G. Knepley @*/
3686e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3687e6ccafaeSMatthew G Knepley {
368882f516ccSBarry Smith   MPI_Comm        comm;
3689e6ccafaeSMatthew G Knepley   DMLabel         subpointMap;
3690e6ccafaeSMatthew G Knepley   IS              is;
3691e6ccafaeSMatthew G Knepley   const PetscInt *opoints;
3692e6ccafaeSMatthew G Knepley   PetscInt       *points, *depths;
3693e6ccafaeSMatthew G Knepley   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3694e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
3695e6ccafaeSMatthew G Knepley 
3696e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3697e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3698e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
369982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
37000298fd71SBarry Smith   *subpointIS = NULL;
3701e6ccafaeSMatthew G Knepley   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
3702e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3703fed694aaSMatthew G. Knepley   if (subpointMap && depth >= 0) {
3704e6ccafaeSMatthew G Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3705e6ccafaeSMatthew G Knepley     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
370669291d52SBarry Smith     ierr = DMGetWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3707e6ccafaeSMatthew G Knepley     depths[0] = depth;
3708e6ccafaeSMatthew G Knepley     depths[1] = 0;
3709e6ccafaeSMatthew G Knepley     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3710785e854fSJed Brown     ierr = PetscMalloc1(pEnd, &points);CHKERRQ(ierr);
3711e6ccafaeSMatthew G Knepley     for(d = 0, off = 0; d <= depth; ++d) {
3712e6ccafaeSMatthew G Knepley       const PetscInt dep = depths[d];
3713e6ccafaeSMatthew G Knepley 
3714e6ccafaeSMatthew G Knepley       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
3715e6ccafaeSMatthew G Knepley       ierr = DMLabelGetStratumSize(subpointMap, dep, &n);CHKERRQ(ierr);
37161e21b6fdSMatthew G Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3717e6ccafaeSMatthew 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);
3718e6ccafaeSMatthew G Knepley       } else {
37191e21b6fdSMatthew G Knepley         if (!n) {
37201e21b6fdSMatthew G Knepley           if (d == 0) {
37211e21b6fdSMatthew G Knepley             /* Missing cells */
37221e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
37231e21b6fdSMatthew G Knepley           } else {
37241e21b6fdSMatthew G Knepley             /* Missing faces */
37251e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
37261e21b6fdSMatthew G Knepley           }
37271e21b6fdSMatthew G Knepley         }
3728e6ccafaeSMatthew G Knepley       }
3729e6ccafaeSMatthew G Knepley       if (n) {
3730e6ccafaeSMatthew G Knepley         ierr = DMLabelGetStratumIS(subpointMap, dep, &is);CHKERRQ(ierr);
3731e6ccafaeSMatthew G Knepley         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
3732e6ccafaeSMatthew G Knepley         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3733e6ccafaeSMatthew G Knepley         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
3734e6ccafaeSMatthew G Knepley         ierr = ISDestroy(&is);CHKERRQ(ierr);
3735e6ccafaeSMatthew G Knepley       }
3736e6ccafaeSMatthew G Knepley     }
373769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3738e6ccafaeSMatthew G Knepley     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3739fed694aaSMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
3740e6ccafaeSMatthew G Knepley   }
3741e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3742e6ccafaeSMatthew G Knepley }
3743559a1558SMatthew G. Knepley 
3744559a1558SMatthew G. Knepley /*@
3745*a6e0b375SMatthew G. Knepley   DMGetEnclosureRelation - Get the relationship between dmA and dmB
3746559a1558SMatthew G. Knepley 
3747559a1558SMatthew G. Knepley   Input Parameters:
3748*a6e0b375SMatthew G. Knepley + dmA - The first DM
3749*a6e0b375SMatthew G. Knepley - dmB - The second DM
3750559a1558SMatthew G. Knepley 
3751559a1558SMatthew G. Knepley   Output Parameter:
3752*a6e0b375SMatthew G. Knepley . rel - The relation of dmA to dmB
3753559a1558SMatthew G. Knepley 
3754*a6e0b375SMatthew G. Knepley   Level: intermediate
3755559a1558SMatthew G. Knepley 
3756*a6e0b375SMatthew G. Knepley .seealso: DMPlexGetEnclosurePoint()
3757559a1558SMatthew G. Knepley @*/
3758*a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
3759559a1558SMatthew G. Knepley {
3760*a6e0b375SMatthew G. Knepley   DM             plexA, plexB, sdm;
3761559a1558SMatthew G. Knepley   DMLabel        spmap;
3762*a6e0b375SMatthew G. Knepley   PetscInt       pStartA, pEndA, pStartB, pEndB, NpA, NpB;
3763559a1558SMatthew G. Knepley   PetscErrorCode ierr;
3764559a1558SMatthew G. Knepley 
376544171101SMatthew G. Knepley   PetscFunctionBegin;
3766*a6e0b375SMatthew G. Knepley   PetscValidPointer(rel, 3);
3767*a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
3768*a6e0b375SMatthew G. Knepley   if (!dmA || !dmB) PetscFunctionReturn(0);
3769*a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
3770*a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 1);
3771*a6e0b375SMatthew G. Knepley   if (dmA == dmB) {*rel = DM_ENC_EQUALITY; PetscFunctionReturn(0);}
3772*a6e0b375SMatthew G. Knepley   ierr = DMConvert(dmA, DMPLEX, &plexA);CHKERRQ(ierr);
3773*a6e0b375SMatthew G. Knepley   ierr = DMConvert(dmB, DMPLEX, &plexB);CHKERRQ(ierr);
3774*a6e0b375SMatthew G. Knepley   ierr = DMPlexGetChart(plexA, &pStartA, &pEndA);CHKERRQ(ierr);
3775*a6e0b375SMatthew G. Knepley   ierr = DMPlexGetChart(plexB, &pStartB, &pEndB);CHKERRQ(ierr);
3776*a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
3777*a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
3778*a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
3779*a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
3780*a6e0b375SMatthew G. Knepley     goto end;
3781559a1558SMatthew G. Knepley   }
3782*a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
3783*a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
3784*a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
3785*a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
3786*a6e0b375SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(sdm, &spmap);CHKERRQ(ierr);
3787*a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
3788*a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
3789*a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
3790*a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
3791*a6e0b375SMatthew G. Knepley   } else {
3792*a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
3793*a6e0b375SMatthew G. Knepley   }
3794*a6e0b375SMatthew G. Knepley   end:
3795*a6e0b375SMatthew G. Knepley   ierr = DMDestroy(&plexA);CHKERRQ(ierr);
3796*a6e0b375SMatthew G. Knepley   ierr = DMDestroy(&plexB);CHKERRQ(ierr);
3797559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3798559a1558SMatthew G. Knepley }
379944171101SMatthew G. Knepley 
380044171101SMatthew G. Knepley /*@
3801*a6e0b375SMatthew G. Knepley   DMGetEnclosurePoint - Get the point pA in dmA which corresponds to the point pB in dmB
380244171101SMatthew G. Knepley 
380344171101SMatthew G. Knepley   Input Parameters:
3804*a6e0b375SMatthew G. Knepley + dmA   - The first DM
3805*a6e0b375SMatthew G. Knepley . dmB   - The second DM
3806*a6e0b375SMatthew G. Knepley . etype - The type of enclosure relation that dmA has to dmB
3807*a6e0b375SMatthew G. Knepley - pB    - A point of dmB
380844171101SMatthew G. Knepley 
380944171101SMatthew G. Knepley   Output Parameter:
3810*a6e0b375SMatthew G. Knepley . pA    - The corresponding point of dmA
381144171101SMatthew G. Knepley 
3812*a6e0b375SMatthew G. Knepley   Level: intermediate
381344171101SMatthew G. Knepley 
3814*a6e0b375SMatthew G. Knepley .seealso: DMGetEnclosureRelation()
381544171101SMatthew G. Knepley @*/
3816*a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
381744171101SMatthew G. Knepley {
3818*a6e0b375SMatthew G. Knepley   DM              sdm;
381944171101SMatthew G. Knepley   DMLabel         spmap;
3820*a6e0b375SMatthew G. Knepley   IS              subpointIS;
3821*a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
3822*a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
382344171101SMatthew G. Knepley   PetscErrorCode  ierr;
382444171101SMatthew G. Knepley 
382544171101SMatthew G. Knepley   PetscFunctionBegin;
3826*a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
3827*a6e0b375SMatthew G. Knepley   switch (etype) {
3828*a6e0b375SMatthew G. Knepley     case DM_ENC_SUPERMESH:
3829*a6e0b375SMatthew G. Knepley     sdm  = dmB;
3830*a6e0b375SMatthew G. Knepley     ierr = DMPlexGetSubpointMap(sdm, &spmap);CHKERRQ(ierr);
3831*a6e0b375SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(sdm, &subpointIS);CHKERRQ(ierr);
3832*a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3833*a6e0b375SMatthew G. Knepley     *pA  = subpoints[pB];
3834*a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3835*a6e0b375SMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
3836*a6e0b375SMatthew G. Knepley     break;
3837*a6e0b375SMatthew G. Knepley     case DM_ENC_SUBMESH:
3838*a6e0b375SMatthew G. Knepley     sdm  = dmA;
3839*a6e0b375SMatthew G. Knepley     ierr = DMPlexGetSubpointMap(sdm, &spmap);CHKERRQ(ierr);
3840*a6e0b375SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(sdm, &subpointIS);CHKERRQ(ierr);
3841*a6e0b375SMatthew G. Knepley     ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
3842*a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3843*a6e0b375SMatthew G. Knepley     ierr = PetscFindInt(pB, numSubpoints, subpoints, pA);CHKERRQ(ierr);
3844*a6e0b375SMatthew G. Knepley     if (*pA < 0) {
3845*a6e0b375SMatthew G. Knepley       ierr = DMViewFromOptions(dmA, NULL, "-dm_enc_A_view");CHKERRQ(ierr);
3846*a6e0b375SMatthew G. Knepley       ierr = DMViewFromOptions(dmB, NULL, "-dm_enc_B_view");CHKERRQ(ierr);
3847*a6e0b375SMatthew G. Knepley       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", pB);
3848*a6e0b375SMatthew G. Knepley     }
3849*a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3850*a6e0b375SMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
3851*a6e0b375SMatthew G. Knepley     break;
3852*a6e0b375SMatthew G. Knepley     case DM_ENC_EQUALITY:
3853*a6e0b375SMatthew G. Knepley     case DM_ENC_NONE:
3854*a6e0b375SMatthew G. Knepley     *pA = pB;break;
3855*a6e0b375SMatthew G. Knepley     case DM_ENC_UNKNOWN:
3856*a6e0b375SMatthew G. Knepley     {
3857*a6e0b375SMatthew G. Knepley       DMEnclosureType enc;
385844171101SMatthew G. Knepley 
3859*a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosureRelation(dmA, dmB, &enc);CHKERRQ(ierr);
3860*a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosurePoint(dmA, dmB, enc, pB, pA);CHKERRQ(ierr);
3861*a6e0b375SMatthew G. Knepley     }
3862*a6e0b375SMatthew G. Knepley     break;
3863*a6e0b375SMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int) etype);
386444171101SMatthew G. Knepley   }
386544171101SMatthew G. Knepley   PetscFunctionReturn(0);
386644171101SMatthew G. Knepley }
3867