xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 41e3e744d3c75c62dbad5fa29ebc2acfd1565676)
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 
5d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCellIsHybrid_Internal(DM dm, PetscInt p, PetscBool *isHybrid)
6d71ae5a4SJacob Faibussowitsch {
7412e9a14SMatthew G. Knepley   DMPolytopeType ct;
8412e9a14SMatthew G. Knepley 
9412e9a14SMatthew G. Knepley   PetscFunctionBegin;
109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, p, &ct));
11412e9a14SMatthew G. Knepley   switch (ct) {
12412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
13412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
14412e9a14SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR:
15d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
16d71ae5a4SJacob Faibussowitsch     *isHybrid = PETSC_TRUE;
17d71ae5a4SJacob Faibussowitsch     break;
18d71ae5a4SJacob Faibussowitsch   default:
19d71ae5a4SJacob Faibussowitsch     *isHybrid = PETSC_FALSE;
20d71ae5a4SJacob Faibussowitsch     break;
21412e9a14SMatthew G. Knepley   }
223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23412e9a14SMatthew G. Knepley }
24412e9a14SMatthew G. Knepley 
25d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetTensorPrismBounds_Internal(DM dm, PetscInt dim, PetscInt *cStart, PetscInt *cEnd)
26d71ae5a4SJacob Faibussowitsch {
27412e9a14SMatthew G. Knepley   DMLabel ctLabel;
28412e9a14SMatthew G. Knepley 
29412e9a14SMatthew G. Knepley   PetscFunctionBegin;
30412e9a14SMatthew G. Knepley   if (cStart) *cStart = -1;
31412e9a14SMatthew G. Knepley   if (cEnd) *cEnd = -1;
329566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
33412e9a14SMatthew G. Knepley   switch (dim) {
34d71ae5a4SJacob Faibussowitsch   case 1:
35d71ae5a4SJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_POINT_PRISM_TENSOR, cStart, cEnd));
36d71ae5a4SJacob Faibussowitsch     break;
37d71ae5a4SJacob Faibussowitsch   case 2:
38d71ae5a4SJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_SEG_PRISM_TENSOR, cStart, cEnd));
39d71ae5a4SJacob Faibussowitsch     break;
40412e9a14SMatthew G. Knepley   case 3:
419566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_TRI_PRISM_TENSOR, cStart, cEnd));
429566063dSJacob Faibussowitsch     if (*cStart < 0) PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_QUAD_PRISM_TENSOR, cStart, cEnd));
43412e9a14SMatthew G. Knepley     break;
44d71ae5a4SJacob Faibussowitsch   default:
453ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
46412e9a14SMatthew G. Knepley   }
473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48412e9a14SMatthew G. Knepley }
49412e9a14SMatthew G. Knepley 
50d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label)
51d71ae5a4SJacob Faibussowitsch {
52*41e3e744Sksagiyam   PetscInt           depth, pStart, pEnd, fStart, fEnd, f, supportSize, nroots = -1, nleaves = -1;
53429fa399SMatthew G. Knepley   PetscSF            sf;
54*41e3e744Sksagiyam   const PetscSFNode *iremote = NULL;
55*41e3e744Sksagiyam   const PetscInt    *ilocal  = NULL;
56*41e3e744Sksagiyam   PetscInt          *leafData;
5730560a7bSMatthew G. Knepley 
5830560a7bSMatthew G. Knepley   PetscFunctionBegin;
59*41e3e744Sksagiyam   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
60*41e3e744Sksagiyam   PetscCall(DMPlexGetDepth(dm, &depth));
61*41e3e744Sksagiyam   if (depth >= cellHeight + 1) {
629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, cellHeight + 1, &fStart, &fEnd));
63*41e3e744Sksagiyam   } else {
64*41e3e744Sksagiyam     /* Note DMPlexGetHeightStratum() returns fStart, fEnd = pStart, pEnd */
65*41e3e744Sksagiyam     /* if height > depth, which is not what we want here.                */
66*41e3e744Sksagiyam     fStart = 0;
67*41e3e744Sksagiyam     fEnd   = 0;
68*41e3e744Sksagiyam   }
69*41e3e744Sksagiyam   PetscCall(PetscCalloc1(pEnd - pStart, &leafData));
70*41e3e744Sksagiyam   leafData -= pStart;
71*41e3e744Sksagiyam   PetscCall(DMGetPointSF(dm, &sf));
72*41e3e744Sksagiyam   if (sf) PetscCall(PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, &iremote));
73*41e3e744Sksagiyam   if (sf && nroots >= 0) {
74*41e3e744Sksagiyam     PetscInt        cStart, cEnd, c, i;
75*41e3e744Sksagiyam     PetscInt       *rootData, *rootData1, *cellOwners, hasTwoSupportCells = -2;
76*41e3e744Sksagiyam     const PetscInt *support;
77*41e3e744Sksagiyam     PetscMPIInt     rank;
7830560a7bSMatthew G. Knepley 
79*41e3e744Sksagiyam     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
80*41e3e744Sksagiyam     PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
81*41e3e744Sksagiyam     PetscCall(PetscCalloc3(pEnd - pStart, &rootData, pEnd - pStart, &rootData1, cEnd - cStart, &cellOwners));
82*41e3e744Sksagiyam     rootData -= pStart;
83*41e3e744Sksagiyam     rootData1 -= pStart;
84*41e3e744Sksagiyam     for (c = cStart; c < cEnd; ++c) cellOwners[c - cStart] = (PetscInt)rank;
85*41e3e744Sksagiyam     for (i = 0; i < nleaves; ++i) {
86*41e3e744Sksagiyam       c = ilocal ? ilocal[i] : i;
87*41e3e744Sksagiyam       if (c >= cStart && c < cEnd) cellOwners[c - cStart] = iremote[i].rank;
88*41e3e744Sksagiyam     }
89*41e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
909566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
91e752be1aSMatthew G. Knepley       if (supportSize == 1) {
92*41e3e744Sksagiyam         PetscCall(DMPlexGetSupport(dm, f, &support));
93*41e3e744Sksagiyam         leafData[f] = cellOwners[support[0] - cStart];
94*41e3e744Sksagiyam       } else {
95*41e3e744Sksagiyam         /* TODO: When using DMForest, we could have a parent facet (a coarse facet)     */
96*41e3e744Sksagiyam         /*       supportSize of which alone does not tell us if it is an interior       */
97*41e3e744Sksagiyam         /*       facet or an exterior facet. Those facets can be identified by checking */
98*41e3e744Sksagiyam         /*       if they are in the parent tree. We should probably skip those parent   */
99*41e3e744Sksagiyam         /*       facets here, which will allow for including the following check, and   */
100*41e3e744Sksagiyam         /*       see if they are exterior facets or not afterwards by checking if the   */
101*41e3e744Sksagiyam         /*       children are exterior or not.                                          */
102*41e3e744Sksagiyam         /* PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", supportSize, f); */
103*41e3e744Sksagiyam         leafData[f] = hasTwoSupportCells; /* some negative PetscInt */
104429fa399SMatthew G. Knepley       }
105*41e3e744Sksagiyam       rootData[f]  = leafData[f];
106*41e3e744Sksagiyam       rootData1[f] = leafData[f];
107*41e3e744Sksagiyam     }
108*41e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData, MPI_MIN));
109*41e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
110*41e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData, MPI_MIN));
111*41e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
112*41e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
113*41e3e744Sksagiyam       /* Store global support size of f.                                                    */
114*41e3e744Sksagiyam       /* Facet f is an interior facet if and only if one of the following two is satisfied: */
115*41e3e744Sksagiyam       /* 1. supportSize is 2 on some rank.                                                  */
116*41e3e744Sksagiyam       /* 2. supportSize is 1 on any rank that can see f, but f is on a partition boundary;  */
117*41e3e744Sksagiyam       /*    i.e., rootData[f] < rootData1[f].                                               */
118*41e3e744Sksagiyam       rootData[f] = (rootData[f] == hasTwoSupportCells || (rootData[f] < rootData1[f])) ? 2 : 1;
119*41e3e744Sksagiyam       leafData[f] = rootData[f];
120*41e3e744Sksagiyam     }
121*41e3e744Sksagiyam     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
122*41e3e744Sksagiyam     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
123*41e3e744Sksagiyam     rootData += pStart;
124*41e3e744Sksagiyam     rootData1 += pStart;
125*41e3e744Sksagiyam     PetscCall(PetscFree3(rootData, rootData1, cellOwners));
126*41e3e744Sksagiyam   } else {
127*41e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
128*41e3e744Sksagiyam       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
129*41e3e744Sksagiyam       leafData[f] = supportSize;
130*41e3e744Sksagiyam     }
131*41e3e744Sksagiyam   }
132*41e3e744Sksagiyam   for (f = fStart; f < fEnd; ++f) {
133*41e3e744Sksagiyam     if (leafData[f] == 1) {
134e752be1aSMatthew G. Knepley       if (val < 0) {
135e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
136e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
137e752be1aSMatthew G. Knepley 
1389566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
139e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize * 2; cl += 2) {
1409566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, closure[cl], &cval));
141e752be1aSMatthew G. Knepley           if (cval < 0) continue;
1429566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, cval));
143e752be1aSMatthew G. Knepley           break;
144e752be1aSMatthew G. Knepley         }
1459566063dSJacob Faibussowitsch         if (cl == clSize * 2) PetscCall(DMLabelSetValue(label, f, 1));
1469566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
147e752be1aSMatthew G. Knepley       } else {
1489566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, f, val));
149e752be1aSMatthew G. Knepley       }
150*41e3e744Sksagiyam     } else {
151*41e3e744Sksagiyam       /* TODO: See the above comment on DMForest */
152*41e3e744Sksagiyam       /* PetscCheck(leafData[f] == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", leafData[f], f); */
153e752be1aSMatthew G. Knepley     }
15430560a7bSMatthew G. Knepley   }
155*41e3e744Sksagiyam   leafData += pStart;
156*41e3e744Sksagiyam   PetscCall(PetscFree(leafData));
1573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15830560a7bSMatthew G. Knepley }
15930560a7bSMatthew G. Knepley 
160cd0c2139SMatthew G Knepley /*@
161cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
162cd0c2139SMatthew G Knepley 
163*41e3e744Sksagiyam   Collective
164cd0c2139SMatthew G Knepley 
165d8d19677SJose E. Roman   Input Parameters:
166429fa399SMatthew G. Knepley + dm  - The original `DM`
16720f4b53cSBarry Smith - val - The marker value, or `PETSC_DETERMINE` to use some value in the closure (or 1 if none are found)
168cd0c2139SMatthew G Knepley 
169cd0c2139SMatthew G Knepley   Output Parameter:
17020f4b53cSBarry Smith . label - The `DMLabel` marking boundary faces with the given value
171429fa399SMatthew G. Knepley 
172cd0c2139SMatthew G Knepley   Level: developer
173cd0c2139SMatthew G Knepley 
17420f4b53cSBarry Smith   Note:
175*41e3e744Sksagiyam   This function will use the point `PetscSF` from the input `DM` and the ownership of the support cells to exclude points on the partition boundary from being marked. If you also wish to mark the partition boundary, you can use `DMSetPointSF()` to temporarily set it to `NULL`, and then reset it to the original object after the call.
176*41e3e744Sksagiyam 
177*41e3e744Sksagiyam   In DMForest there can be facets support sizes of which alone can not determine whether they are on the boundary. Currently, this function is not guaranteed to produce the correct result in such case.
17820f4b53cSBarry Smith 
1791cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLabelCreate()`, `DMCreateLabel()`
18009f723d9SJed Brown @*/
181d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
182d71ae5a4SJacob Faibussowitsch {
183827c4036SVaclav Hapla   DMPlexInterpolatedFlag flg;
184cd0c2139SMatthew G Knepley 
185cd0c2139SMatthew G Knepley   PetscFunctionBegin;
186827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1879566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &flg));
18808401ef6SPierre Jolivet   PetscCheck(flg == DMPLEX_INTERPOLATED_FULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
1899566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces_Internal(dm, val, 0, label));
1903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
191cd0c2139SMatthew G Knepley }
192cd0c2139SMatthew G Knepley 
193d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
194d71ae5a4SJacob Faibussowitsch {
195b0bf5782SToby Isaac   IS              valueIS;
196ac51f24eSSander Arens   PetscSF         sfPoint;
197b0bf5782SToby Isaac   const PetscInt *values;
198ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
199b0bf5782SToby Isaac 
200b0bf5782SToby Isaac   PetscFunctionBegin;
2019566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2029566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2049566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
205b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
206b0bf5782SToby Isaac     IS              pointIS;
207b0bf5782SToby Isaac     const PetscInt *points;
208b0bf5782SToby Isaac     PetscInt        numPoints, p;
209b0bf5782SToby Isaac 
2109566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2119566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2129566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
213b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
214b0bf5782SToby Isaac       PetscInt  q       = points[p];
215b0bf5782SToby Isaac       PetscInt *closure = NULL;
216b0bf5782SToby Isaac       PetscInt  closureSize, c;
217b0bf5782SToby Isaac 
218b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
219b0bf5782SToby Isaac         continue;
220b0bf5782SToby Isaac       }
2219566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
22248a46eb9SPierre Jolivet       for (c = 0; c < closureSize * 2; c += 2) PetscCall(DMLabelSetValue(label, closure[c], values[v]));
2239566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
224b0bf5782SToby Isaac     }
2259566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2269566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
227b0bf5782SToby Isaac   }
2289566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2299566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2309566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
2319566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
232ac51f24eSSander Arens   if (nroots >= 0) {
23326279d81SSanderA     DMLabel         lblRoots, lblLeaves;
23426279d81SSanderA     IS              valueIS, pointIS;
23526279d81SSanderA     const PetscInt *values;
23626279d81SSanderA     PetscInt        numValues, v;
23726279d81SSanderA 
23826279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
2399566063dSJacob Faibussowitsch     PetscCall(DMLabelGather(label, sfPoint, &lblLeaves));
2409566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS));
2419566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2429566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
24326279d81SSanderA     for (v = 0; v < numValues; ++v) {
24426279d81SSanderA       const PetscInt value = values[v];
24526279d81SSanderA 
2469566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
2479566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2489566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
24926279d81SSanderA     }
2509566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2519566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2529566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblLeaves));
25326279d81SSanderA     /* Push point contributions from roots into remote leaves */
2549566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots));
2559566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblRoots, &valueIS));
2569566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2579566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
25826279d81SSanderA     for (v = 0; v < numValues; ++v) {
25926279d81SSanderA       const PetscInt value = values[v];
26026279d81SSanderA 
2619566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS));
2629566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2639566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
26426279d81SSanderA     }
2659566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2669566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2679566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblRoots));
26826279d81SSanderA   }
2693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
270b0bf5782SToby Isaac }
271b0bf5782SToby Isaac 
2722be2b188SMatthew G Knepley /*@
2732be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
2742be2b188SMatthew G Knepley 
2752be2b188SMatthew G Knepley   Input Parameters:
27620f4b53cSBarry Smith + dm    - The `DM`
27720f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
2782be2b188SMatthew G Knepley 
2792be2b188SMatthew G Knepley   Output Parameter:
28020f4b53cSBarry Smith . label - A `DMLabel` marking all surface points in the transitive closure
2812be2b188SMatthew G Knepley 
2822be2b188SMatthew G Knepley   Level: developer
2832be2b188SMatthew G Knepley 
2841cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelCohesiveComplete()`
2852be2b188SMatthew G Knepley @*/
286d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
287d71ae5a4SJacob Faibussowitsch {
2882be2b188SMatthew G Knepley   PetscFunctionBegin;
2899566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
2903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2912be2b188SMatthew G Knepley }
2922be2b188SMatthew G Knepley 
2936cf0e42fSMatthew G. Knepley /*@
294a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
2956cf0e42fSMatthew G. Knepley 
2966cf0e42fSMatthew G. Knepley   Input Parameters:
29720f4b53cSBarry Smith + dm    - The `DM`
29820f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
2996cf0e42fSMatthew G. Knepley 
3006cf0e42fSMatthew G. Knepley   Output Parameter:
30120f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
3026cf0e42fSMatthew G. Knepley 
3036cf0e42fSMatthew G. Knepley   Level: developer
3046cf0e42fSMatthew G. Knepley 
30520f4b53cSBarry Smith   Note:
30620f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
3076cf0e42fSMatthew G. Knepley 
3081cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
3096cf0e42fSMatthew G. Knepley @*/
310d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
311d71ae5a4SJacob Faibussowitsch {
3126cf0e42fSMatthew G. Knepley   IS              valueIS;
3136cf0e42fSMatthew G. Knepley   const PetscInt *values;
3149df54392SMatthew G. Knepley   PetscInt        numValues, v, csStart, csEnd, chStart, chEnd;
3156cf0e42fSMatthew G. Knepley 
3166cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
3179df54392SMatthew G. Knepley   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &csStart, &csEnd));
3189df54392SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(dm, 0, &chStart, &chEnd));
3199566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3209566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3219566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
3226cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
3236cf0e42fSMatthew G. Knepley     IS              pointIS;
3246cf0e42fSMatthew G. Knepley     const PetscInt *points;
3256cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
3266cf0e42fSMatthew G. Knepley 
3279566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3289566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3299566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
3306cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3319df54392SMatthew G. Knepley       const PetscInt point   = points[p];
3326cf0e42fSMatthew G. Knepley       PetscInt      *closure = NULL;
3339df54392SMatthew G. Knepley       PetscInt       closureSize, cl, h, pStart, pEnd, cStart, cEnd;
3349df54392SMatthew G. Knepley 
3359df54392SMatthew G. Knepley       // If the point is a hybrid, allow hybrid cells
3369df54392SMatthew G. Knepley       PetscCall(DMPlexGetPointHeight(dm, point, &h));
3379df54392SMatthew G. Knepley       PetscCall(DMPlexGetSimplexOrBoxCells(dm, h, &pStart, &pEnd));
3389df54392SMatthew G. Knepley       if (point >= pStart && point < pEnd) {
3399df54392SMatthew G. Knepley         cStart = csStart;
3409df54392SMatthew G. Knepley         cEnd   = csEnd;
3419df54392SMatthew G. Knepley       } else {
3429df54392SMatthew G. Knepley         cStart = chStart;
3439df54392SMatthew G. Knepley         cEnd   = chEnd;
3449df54392SMatthew G. Knepley       }
3456cf0e42fSMatthew G. Knepley 
3469566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
34722eabd52SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
348a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
3499371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
3509371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
3519371c9d4SSatish Balay           break;
3529371c9d4SSatish Balay         }
35322eabd52SMatthew G. Knepley       }
3549566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
3556cf0e42fSMatthew G. Knepley     }
3569566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3579566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3586cf0e42fSMatthew G. Knepley   }
3599566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3609566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
3613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3626cf0e42fSMatthew G. Knepley }
3636cf0e42fSMatthew G. Knepley 
364f402d5e4SToby Isaac /*@
365a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
366a6e0b375SMatthew G. Knepley 
367a6e0b375SMatthew G. Knepley   Input Parameters:
36820f4b53cSBarry Smith + dm    - The `DM`
36920f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
370a6e0b375SMatthew G. Knepley 
371a6e0b375SMatthew G. Knepley   Output Parameter:
37220f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
373a6e0b375SMatthew G. Knepley 
374a6e0b375SMatthew G. Knepley   Level: developer
375a6e0b375SMatthew G. Knepley 
37620f4b53cSBarry Smith   Note:
37720f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
378a6e0b375SMatthew G. Knepley 
3791cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
380a6e0b375SMatthew G. Knepley @*/
381d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
382d71ae5a4SJacob Faibussowitsch {
383a6e0b375SMatthew G. Knepley   IS              valueIS;
384a6e0b375SMatthew G. Knepley   const PetscInt *values;
385a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
386a6e0b375SMatthew G. Knepley 
387a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
3889566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
3899566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
3909566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3919566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3929566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
393a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
394a6e0b375SMatthew G. Knepley     IS              pointIS;
395a6e0b375SMatthew G. Knepley     const PetscInt *points;
396a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
397a6e0b375SMatthew G. Knepley 
3989566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3999566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4009566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
401a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
402a6e0b375SMatthew G. Knepley       const PetscInt face    = points[p];
403a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
404a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
405a6e0b375SMatthew G. Knepley 
406a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
4079566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
408a6e0b375SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
409a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
4109371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
4119371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
4129371c9d4SSatish Balay           break;
4139371c9d4SSatish Balay         }
414a6e0b375SMatthew G. Knepley       }
4159566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
416a6e0b375SMatthew G. Knepley     }
4179566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4189566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
419a6e0b375SMatthew G. Knepley   }
4209566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4219566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
423a6e0b375SMatthew G. Knepley }
424a6e0b375SMatthew G. Knepley 
425a6e0b375SMatthew G. Knepley /*@
426f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
427f402d5e4SToby Isaac 
428f402d5e4SToby Isaac   Input Parameters:
42920f4b53cSBarry Smith + dm    - The `DM`
43020f4b53cSBarry Smith - label - A `DMLabel` marking surface points and their adjacent cells
431f402d5e4SToby Isaac 
432f402d5e4SToby Isaac   Output Parameter:
43320f4b53cSBarry Smith . label - A `DMLabel` without cells
434f402d5e4SToby Isaac 
435f402d5e4SToby Isaac   Level: developer
436f402d5e4SToby Isaac 
43720f4b53cSBarry Smith   Note:
43820f4b53cSBarry Smith   This undoes `DMPlexLabelAddCells()` or `DMPlexLabelAddFaceCells()`
439f402d5e4SToby Isaac 
4401cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()`
441f402d5e4SToby Isaac @*/
442d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
443d71ae5a4SJacob Faibussowitsch {
444f402d5e4SToby Isaac   IS              valueIS;
445f402d5e4SToby Isaac   const PetscInt *values;
446485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
447f402d5e4SToby Isaac 
448f402d5e4SToby Isaac   PetscFunctionBegin;
4499566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4509566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4519566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4529566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
453f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
454f402d5e4SToby Isaac     IS              pointIS;
455f402d5e4SToby Isaac     const PetscInt *points;
456f402d5e4SToby Isaac     PetscInt        numPoints, p;
457f402d5e4SToby Isaac 
4589566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4599566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4609566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
461f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
462f402d5e4SToby Isaac       PetscInt point = points[p];
463f402d5e4SToby Isaac 
46448a46eb9SPierre Jolivet       if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v]));
465f402d5e4SToby Isaac     }
4669566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4679566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
468f402d5e4SToby Isaac   }
4699566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4709566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
472f402d5e4SToby Isaac }
473f402d5e4SToby Isaac 
47459eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
47559eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
476d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
477d71ae5a4SJacob Faibussowitsch {
4782582d50cSToby Isaac   PetscInt d, off = 0;
4792582d50cSToby Isaac 
4802582d50cSToby Isaac   PetscFunctionBegin;
48159eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
4820974a383SToby Isaac   for (d = 0; d < depth; d++) {
4832582d50cSToby Isaac     PetscInt firstd     = d;
4840974a383SToby Isaac     PetscInt firstStart = depthShift[2 * d];
4852582d50cSToby Isaac     PetscInt e;
4862582d50cSToby Isaac 
4872582d50cSToby Isaac     for (e = d + 1; e <= depth; e++) {
4882582d50cSToby Isaac       if (depthShift[2 * e] < firstStart) {
4892582d50cSToby Isaac         firstd     = e;
4902582d50cSToby Isaac         firstStart = depthShift[2 * d];
4912582d50cSToby Isaac       }
4922582d50cSToby Isaac     }
4932582d50cSToby Isaac     if (firstd != d) {
4942582d50cSToby Isaac       PetscInt swap[2];
4952582d50cSToby Isaac 
4962582d50cSToby Isaac       e                     = firstd;
4972582d50cSToby Isaac       swap[0]               = depthShift[2 * d];
4982582d50cSToby Isaac       swap[1]               = depthShift[2 * d + 1];
4992582d50cSToby Isaac       depthShift[2 * d]     = depthShift[2 * e];
5002582d50cSToby Isaac       depthShift[2 * d + 1] = depthShift[2 * e + 1];
5012582d50cSToby Isaac       depthShift[2 * e]     = swap[0];
5022582d50cSToby Isaac       depthShift[2 * e + 1] = swap[1];
5032582d50cSToby Isaac     }
5042582d50cSToby Isaac   }
5052582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
5060974a383SToby Isaac   for (d = 0; d <= depth; d++) {
5072582d50cSToby Isaac     off += depthShift[2 * d + 1];
50859eef20bSToby Isaac     depthShift[2 * d + 1] = depthShift[2 * d] + off;
5092582d50cSToby Isaac   }
5103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5112582d50cSToby Isaac }
5122582d50cSToby Isaac 
5132582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
514d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
515d71ae5a4SJacob Faibussowitsch {
5162582d50cSToby Isaac   PetscInt d;
5172582d50cSToby Isaac   PetscInt newOff = 0;
5182582d50cSToby Isaac 
5192582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5202582d50cSToby Isaac     if (p < depthShift[2 * d]) return p + newOff;
5212582d50cSToby Isaac     else newOff = depthShift[2 * d + 1] - depthShift[2 * d];
5222582d50cSToby Isaac   }
5230974a383SToby Isaac   return p + newOff;
5242582d50cSToby Isaac }
5252582d50cSToby Isaac 
5262582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
527d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
528d71ae5a4SJacob Faibussowitsch {
5292582d50cSToby Isaac   PetscInt d;
5302582d50cSToby Isaac   PetscInt newOff = 0;
5312582d50cSToby Isaac 
5322582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5332582d50cSToby Isaac     if (p < depthShift[2 * d + 1]) return p + newOff;
5342582d50cSToby Isaac     else newOff = depthShift[2 * d] - depthShift[2 * d + 1];
5352582d50cSToby Isaac   }
5360974a383SToby Isaac   return p + newOff;
537cd0c2139SMatthew G Knepley }
538cd0c2139SMatthew G Knepley 
539d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
540d71ae5a4SJacob Faibussowitsch {
541cd0c2139SMatthew G Knepley   PetscInt depth = 0, d, pStart, pEnd, p;
542fa8e8ae5SToby Isaac   DMLabel  depthLabel;
543cd0c2139SMatthew G Knepley 
544cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5463ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
547cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
5489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5490974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5509566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
5519566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "depth"));
5529566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel));
5539566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
554cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
555cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
556fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
557fa8e8ae5SToby Isaac     IS       pIS;
558fa8e8ae5SToby Isaac 
5599566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
560fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
561fa8e8ae5SToby Isaac     pEndNew   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5629566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
5639566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
5649566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
565cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
5662582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
567cd0c2139SMatthew G Knepley       PetscInt       size;
568412e9a14SMatthew G. Knepley       DMPolytopeType ct;
569cd0c2139SMatthew G Knepley 
5709566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
5719566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
5729566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
5739566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
5749566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
5759566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
576cd0c2139SMatthew G Knepley     }
577cd0c2139SMatthew G Knepley   }
5783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
579cd0c2139SMatthew G Knepley }
580cd0c2139SMatthew G Knepley 
581d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
582d71ae5a4SJacob Faibussowitsch {
5832582d50cSToby Isaac   PetscInt *newpoints;
5842582d50cSToby Isaac   PetscInt  depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
585cd0c2139SMatthew G Knepley 
586cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5879566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5883ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
5899566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
5909566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
5919566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints));
592cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
5939566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
594cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
595cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
5962582d50cSToby Isaac     PetscInt        size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
597cd0c2139SMatthew G Knepley 
5989566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
5999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
6009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
601ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
6029566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
6039566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
6049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
6059566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
6069566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
607ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
608dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
6099566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
610cd0c2139SMatthew G Knepley   }
6119566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
6123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
613cd0c2139SMatthew G Knepley }
614cd0c2139SMatthew G Knepley 
615d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
616d71ae5a4SJacob Faibussowitsch {
617cd0c2139SMatthew G Knepley   PetscSection coordSection, newCoordSection;
618cd0c2139SMatthew G Knepley   Vec          coordinates, newCoordinates;
619cd0c2139SMatthew G Knepley   PetscScalar *coords, *newCoords;
620f2b8cce1SMatthew G. Knepley   PetscInt     coordSize, sStart, sEnd;
621f2b8cce1SMatthew G. Knepley   PetscInt     dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
622f2b8cce1SMatthew G. Knepley   PetscBool    hasCells;
623cd0c2139SMatthew G Knepley 
624cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6259566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
6269566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
6279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
628cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
6299566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
6309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
6319566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
6329566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
6339566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6349566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
6359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
6369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
6379566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
638f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
6399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
640f2b8cce1SMatthew G. Knepley   if (hasCells) {
641f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
642f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
643f2b8cce1SMatthew G. Knepley 
6449566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6459566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
6469566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
647f2b8cce1SMatthew G. Knepley     }
648f2b8cce1SMatthew G. Knepley   }
649cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
6509566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
6519566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
652cd0c2139SMatthew G Knepley   }
6539566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
6549566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
6559566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
6569566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
6579566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates"));
6589566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
6599566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
6609566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates, VECSTANDARD));
6619566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
6629566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
6639566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6649566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
665f2b8cce1SMatthew G. Knepley   if (hasCells) {
666f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
667f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
668f2b8cce1SMatthew G. Knepley 
6699566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6709566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
6719566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
672f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
673f2b8cce1SMatthew G. Knepley     }
674f2b8cce1SMatthew G. Knepley   }
675cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
676cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
677cd0c2139SMatthew G Knepley 
6789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
6799566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
6809566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
681f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
682cd0c2139SMatthew G Knepley   }
6839566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6849566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
6859566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
6869566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
6873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
688cd0c2139SMatthew G Knepley }
689cd0c2139SMatthew G Knepley 
690d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
691d71ae5a4SJacob Faibussowitsch {
692cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
693cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
694cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
695cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
6960e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
697cd0c2139SMatthew G Knepley 
698cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6999566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
7009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
7019566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
7026e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
703cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
7049566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation));
7050e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
7069566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7079566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7089566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
7099566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
710cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
7112582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
712cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
713cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
714cd0c2139SMatthew G Knepley     }
7159566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
7169566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
7170e33faafSMatthew G. Knepley   }
7183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7190e33faafSMatthew G. Knepley }
7200e33faafSMatthew G. Knepley 
721d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
722d71ae5a4SJacob Faibussowitsch {
7230e33faafSMatthew G. Knepley   PetscSF   sfPoint, sfPointNew;
7240e33faafSMatthew G. Knepley   PetscBool useNatural;
7250e33faafSMatthew G. Knepley 
7260e33faafSMatthew G. Knepley   PetscFunctionBegin;
7270e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
7289566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
7299566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
7309566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
7310e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
7329566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
7330e33faafSMatthew G. Knepley   if (useNatural) {
7340e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
7350e33faafSMatthew G. Knepley 
7369566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
7379566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
7389566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
7399566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
740cd0c2139SMatthew G Knepley   }
7413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
742cd0c2139SMatthew G Knepley }
743cd0c2139SMatthew G Knepley 
744d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
745d71ae5a4SJacob Faibussowitsch {
746d56405f8SMatthew G. Knepley   PetscInt depth = 0, numLabels, l;
747cd0c2139SMatthew G Knepley 
748cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7499566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
750cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
7519566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
752cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
753cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
754cd0c2139SMatthew G Knepley     const char     *lname;
755fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
756cd0c2139SMatthew G Knepley     IS              valueIS;
757cd0c2139SMatthew G Knepley     const PetscInt *values;
758cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
759cd0c2139SMatthew G Knepley 
7609566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
7619566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
762cd0c2139SMatthew G Knepley     if (isDepth) continue;
7639566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
764fa8e8ae5SToby Isaac     if (isDim) continue;
7659566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
7669566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
7679566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
7689566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &val));
7699566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, val));
7709566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
7719566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
7729566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
773cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
774cd0c2139SMatthew G Knepley       IS              pointIS;
775cd0c2139SMatthew G Knepley       const PetscInt *points;
776cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
777cd0c2139SMatthew G Knepley 
7789566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
7799566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
7809566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
781cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
7822582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
783cd0c2139SMatthew G Knepley 
7849566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
785cd0c2139SMatthew G Knepley       }
7869566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
7879566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
788cd0c2139SMatthew G Knepley     }
7899566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
7909566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
791cd0c2139SMatthew G Knepley   }
7923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
793d56405f8SMatthew G. Knepley }
794d56405f8SMatthew G. Knepley 
795d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
796d71ae5a4SJacob Faibussowitsch {
797d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
798d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
799d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
800d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
801d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
802d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
803d56405f8SMatthew G. Knepley 
804d56405f8SMatthew G. Knepley   PetscFunctionBegin;
805cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
8069566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8079566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
8089566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
8099566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8109566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
8119566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
8129566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
813d56405f8SMatthew G. Knepley   if (createGhostLabel) {
8149566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
8159566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
816d56405f8SMatthew G. Knepley   }
817cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
81848a46eb9SPierre Jolivet     for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
819cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
820cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
8219566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
8221baa6e33SBarry Smith     } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
823cd0c2139SMatthew G Knepley   }
82448a46eb9SPierre Jolivet   for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
825d56405f8SMatthew G. Knepley   if (ghostLabel) {
8269566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
827cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
828cd0c2139SMatthew G Knepley       PetscInt numCells;
829cd0c2139SMatthew G Knepley 
8309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
831cd0c2139SMatthew G Knepley       if (numCells < 2) {
8329566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
833cd0c2139SMatthew G Knepley       } else {
834cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
835cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
836cd0c2139SMatthew G Knepley 
8379566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
8389566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
8399566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
8409566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
841cd0c2139SMatthew G Knepley       }
842cd0c2139SMatthew G Knepley     }
843d56405f8SMatthew G. Knepley   }
8443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
845cd0c2139SMatthew G Knepley }
846cd0c2139SMatthew G Knepley 
847d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
848d71ae5a4SJacob Faibussowitsch {
849ca04dac2SToby Isaac   DM           refTree;
850ca04dac2SToby Isaac   PetscSection pSec;
851ca04dac2SToby Isaac   PetscInt    *parents, *childIDs;
852ca04dac2SToby Isaac 
853ca04dac2SToby Isaac   PetscFunctionBegin;
8549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
8559566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew, refTree));
8569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL));
857ca04dac2SToby Isaac   if (pSec) {
8582582d50cSToby Isaac     PetscInt     p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
859fb4630b5SToby Isaac     PetscInt    *childIDsShifted;
860ca04dac2SToby Isaac     PetscSection pSecShifted;
861ca04dac2SToby Isaac 
8629566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd));
8639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
8642582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
8652582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
8669566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted));
8679566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted));
8689566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted));
869ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
870fb4630b5SToby Isaac       /* start off assuming no children */
8719566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, p, 0));
872fb4630b5SToby Isaac     }
873fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
874fb4630b5SToby Isaac       PetscInt dof;
875fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
876ca04dac2SToby Isaac 
8779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
8789566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof));
879ca04dac2SToby Isaac     }
8809566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
881fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
882fb4630b5SToby Isaac       PetscInt dof;
883fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
884fb4630b5SToby Isaac 
8859566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
886fb4630b5SToby Isaac       if (dof) {
887fb4630b5SToby Isaac         PetscInt off, offNew;
888fb4630b5SToby Isaac 
8899566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec, p, &off));
8909566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew));
891fb4630b5SToby Isaac         parentsShifted[offNew]  = DMPlexShiftPoint_Internal(parents[off], depth, depthShift);
892fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
893fb4630b5SToby Isaac       }
894fb4630b5SToby Isaac     }
8959566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted));
8969566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted, childIDsShifted));
8979566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
898ca04dac2SToby Isaac   }
8993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
900ca04dac2SToby Isaac }
901ca04dac2SToby Isaac 
902d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
903d71ae5a4SJacob Faibussowitsch {
904da97024aSMatthew G. Knepley   PetscSF          sf;
905cd0c2139SMatthew G Knepley   IS               valueIS;
906da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
907cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
9082582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
9094fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
910cd0c2139SMatthew G Knepley 
911cd0c2139SMatthew G Knepley   PetscFunctionBegin;
9129566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
9139566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
914da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
9159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
916cd0c2139SMatthew G Knepley   /* Count ghost cells */
9179566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
9189566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
9199566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
9204a6cfa73SMatthew G. Knepley   Ng = 0;
921cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
92246c796b9SMatthew G. Knepley     IS              faceIS;
92346c796b9SMatthew G. Knepley     const PetscInt *faces;
92446c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
925cd0c2139SMatthew G Knepley 
9269566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9279566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9289566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
92946c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
930ca04dac2SToby Isaac       PetscInt numChildren;
931ca04dac2SToby Isaac 
9329566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
934ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
935ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
93646c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
93746c796b9SMatthew G. Knepley     }
9384a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
9399566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9409566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
941cd0c2139SMatthew G Knepley   }
9429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
9439566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift));
9442582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
94559eef20bSToby Isaac     PetscInt dEnd;
9462582d50cSToby Isaac 
9479566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd));
94859eef20bSToby Isaac     depthShift[2 * d]     = dEnd;
9492582d50cSToby Isaac     depthShift[2 * d + 1] = 0;
9502582d50cSToby Isaac   }
9512582d50cSToby Isaac   if (depth >= 0) depthShift[2 * depth + 1] = Ng;
9529566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
9539566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
954cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
9559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
95648a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1));
957cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
958cd0c2139SMatthew G Knepley     IS              faceIS;
959cd0c2139SMatthew G Knepley     const PetscInt *faces;
960cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
961cd0c2139SMatthew G Knepley 
9629566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9639566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9649566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
965cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
966ca04dac2SToby Isaac       PetscInt size, numChildren;
967cd0c2139SMatthew G Knepley 
9689566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9699566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
970ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
97146c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9729566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
97363a3b9bcSJacob Faibussowitsch       PetscCheck(size == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %" PetscInt_FMT " with %" PetscInt_FMT " support cells", faces[f], size);
9749566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
975cd0c2139SMatthew G Knepley     }
9769566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9779566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
978cd0c2139SMatthew G Knepley   }
979cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
9809566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
9819566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
982cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
983cd0c2139SMatthew G Knepley   ghostCell = cEnd;
984cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
985cd0c2139SMatthew G Knepley     IS              faceIS;
986cd0c2139SMatthew G Knepley     const PetscInt *faces;
987cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
988cd0c2139SMatthew G Knepley 
9899566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9909566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9919566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
99246c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
993ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
994cd0c2139SMatthew G Knepley 
9959566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9969566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
997ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
99846c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9999566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
10009566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
100146c796b9SMatthew G. Knepley       ++ghostCell;
1002cd0c2139SMatthew G Knepley     }
10039566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
10049566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
1005cd0c2139SMatthew G Knepley   }
10069566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
10079566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
10089566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
10099566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
10109566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
10119566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
10129566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
10139566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
101448a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
1015966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
10164fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
10174fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L));
10184a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
10193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1020cd0c2139SMatthew G Knepley }
1021cd0c2139SMatthew G Knepley 
1022cd0c2139SMatthew G Knepley /*@C
1023cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
1024cd0c2139SMatthew G Knepley 
102520f4b53cSBarry Smith   Collective
1026cd0c2139SMatthew G Knepley 
1027cd0c2139SMatthew G Knepley   Input Parameters:
102820f4b53cSBarry Smith + dm        - The original `DM`
102920f4b53cSBarry Smith - labelName - The label specifying the boundary faces, or "Face Sets" if this is `NULL`
1030cd0c2139SMatthew G Knepley 
1031cd0c2139SMatthew G Knepley   Output Parameters:
103220f4b53cSBarry Smith + numGhostCells - The number of ghost cells added to the `DM`
103320f4b53cSBarry Smith - dmGhosted     - The new `DM`
1034cd0c2139SMatthew G Knepley 
1035cd0c2139SMatthew G Knepley   Level: developer
1036cd0c2139SMatthew G Knepley 
103720f4b53cSBarry Smith   Note:
103820f4b53cSBarry Smith   If no label exists of that name, one will be created marking all boundary faces
103920f4b53cSBarry Smith 
10401cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`
104131266bc0SMatthew G. Knepley @*/
1042d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
1043d71ae5a4SJacob Faibussowitsch {
1044cd0c2139SMatthew G Knepley   DM          gdm;
1045cd0c2139SMatthew G Knepley   DMLabel     label;
1046cd0c2139SMatthew G Knepley   const char *name = labelName ? labelName : "Face Sets";
1047412e9a14SMatthew G. Knepley   PetscInt    dim, Ng = 0;
1048b0441da4SMatthew G. Knepley   PetscBool   useCone, useClosure;
1049cd0c2139SMatthew G Knepley 
1050cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1051cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10524f572ea9SToby Isaac   if (numGhostCells) PetscAssertPointer(numGhostCells, 3);
10534f572ea9SToby Isaac   PetscAssertPointer(dmGhosted, 4);
10549566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
10559566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
10569566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
10579566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
10589566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
10599566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure));
10609566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
1061cd0c2139SMatthew G Knepley   if (!label) {
1062cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
10639566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
10649566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
10659566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
1066cd0c2139SMatthew G Knepley   }
10679566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
10689566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
10695de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
1070cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
1071d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
1072cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
10733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1074cd0c2139SMatthew G Knepley }
1075cd0c2139SMatthew G Knepley 
1076d71ae5a4SJacob Faibussowitsch static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue)
1077d71ae5a4SJacob Faibussowitsch {
1078accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
1079accc9626SMatthew G. Knepley 
1080accc9626SMatthew G. Knepley   PetscFunctionBegin;
1081accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1082accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
108348a46eb9SPierre Jolivet   for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
1084accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
1085accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
1086accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
1087accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
1088accc9626SMatthew G. Knepley 
1089accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
1090accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
1091accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1092accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
1093accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1094accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1095accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
1096accc9626SMatthew G. Knepley         PetscInt       clval;
1097accc9626SMatthew G. Knepley 
1098accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
1099accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
1100accc9626SMatthew G. Knepley         if (clval == -1) {
1101accc9626SMatthew G. Knepley           const PetscInt *cone;
1102accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
1103accc9626SMatthew G. Knepley 
1104accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
1105accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
1106accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
1107accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
1108accc9626SMatthew G. Knepley             PetscInt cpval;
1109accc9626SMatthew G. Knepley 
1110accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
1111accc9626SMatthew G. Knepley             if (cpval != -1) {
1112accc9626SMatthew G. Knepley               PetscInt dep;
1113accc9626SMatthew G. Knepley 
1114accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
1115accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift + dep) : shift + dep;
1116accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
1117accc9626SMatthew G. Knepley               break;
1118accc9626SMatthew G. Knepley             }
1119accc9626SMatthew G. Knepley           }
1120accc9626SMatthew G. Knepley         }
1121accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
1122accc9626SMatthew G. Knepley         if (d == dim - 1 && clval != -1) {
1123accc9626SMatthew G. Knepley           const PetscInt *support;
1124accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
1125accc9626SMatthew G. Knepley 
1126accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
1127accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
1128accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
1129accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
1130accc9626SMatthew G. Knepley             if (nval == -1) {
1131accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1));
1132accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1133accc9626SMatthew G. Knepley             }
1134accc9626SMatthew G. Knepley           }
1135accc9626SMatthew G. Knepley         }
1136accc9626SMatthew G. Knepley       }
1137accc9626SMatthew G. Knepley     }
1138accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1139accc9626SMatthew G. Knepley   }
1140accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
11413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1142accc9626SMatthew G. Knepley }
1143accc9626SMatthew G. Knepley 
1144accc9626SMatthew G. Knepley typedef struct {
1145accc9626SMatthew G. Knepley   DM               dm;
1146accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1147accc9626SMatthew G. Knepley } PointDivision;
1148accc9626SMatthew G. Knepley 
1149d71ae5a4SJacob Faibussowitsch static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx)
1150d71ae5a4SJacob Faibussowitsch {
1151accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *)ctx;
1152accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val - 1 : val + 1;
1153accc9626SMatthew G. Knepley   const PetscInt *support;
1154accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1155accc9626SMatthew G. Knepley 
1156accc9626SMatthew G. Knepley   PetscFunctionBegin;
1157accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1158accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1159accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1160accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1161accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1162accc9626SMatthew G. Knepley   }
11633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1164accc9626SMatthew G. Knepley }
1165accc9626SMatthew G. Knepley 
1166accc9626SMatthew G. Knepley /* Mark cells by label propagation */
1167d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel)
1168d71ae5a4SJacob Faibussowitsch {
1169accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1170accc9626SMatthew G. Knepley   PointDivision    div;
1171accc9626SMatthew G. Knepley   PetscSF          pointSF;
1172accc9626SMatthew G. Knepley   IS               pointIS;
1173accc9626SMatthew G. Knepley   const PetscInt  *points;
1174accc9626SMatthew G. Knepley   PetscBool        empty;
1175accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1176accc9626SMatthew G. Knepley 
1177accc9626SMatthew G. Knepley   PetscFunctionBegin;
1178accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1179accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1180accc9626SMatthew G. Knepley   div.dm    = dm;
1181accc9626SMatthew G. Knepley   div.queue = queue;
1182accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1183accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS));
1184accc9626SMatthew G. Knepley   if (pointIS) {
1185accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1186accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
118748a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1188accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1189accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1190accc9626SMatthew G. Knepley   }
1191accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS));
1192accc9626SMatthew G. Knepley   if (pointIS) {
1193accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1194accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
119548a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1196accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1197accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1198accc9626SMatthew G. Knepley   }
1199accc9626SMatthew G. Knepley 
1200accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1201accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1202accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1203accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1204accc9626SMatthew G. Knepley   while (!empty) {
1205accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1206accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1207accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1208accc9626SMatthew G. Knepley   }
1209accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1210accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
12113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1212accc9626SMatthew G. Knepley }
1213accc9626SMatthew G. Knepley 
1214607ab7a9SMatthew G. Knepley /*
1215faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1216607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1217faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1218b6dfa339SMatthew G. Knepley     Ghost:          These are shared fault faces which are not owned by this process. These do not produce hybrid cells and do not replicate
1219607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1220a6ae58d1SMatthew G. Knepley 
1221a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1222a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1223607ab7a9SMatthew G. Knepley */
1224d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1225d71ae5a4SJacob Faibussowitsch {
1226cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1227607ab7a9SMatthew G. Knepley   IS               valueIS;
1228607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0; /* The number of depths for which we have replicated points */
1229607ab7a9SMatthew G. Knepley   const PetscInt  *values;    /* List of depths for which we have replicated points */
123018c5995bSMatthew G. Knepley   IS              *splitIS;
123118c5995bSMatthew G. Knepley   IS              *unsplitIS;
1232b6dfa339SMatthew G. Knepley   IS               ghostIS;
1233607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
123418c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
123536dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
123636dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1237b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1238607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
123918c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1240b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1241cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1242cd0c2139SMatthew G Knepley   Vec              coordinates;
1243cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1244a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;   /* The first hybrid point at each depth in the original mesh */
1245a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;   /* The point limit at each depth in the original mesh */
1246607ab7a9SMatthew G. Knepley   PetscInt        *depthShift; /* Number of replicated+hybrid points at each depth */
1247607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;    /* The first replicated point at each depth in the new mesh, hybrids come after this */
1248607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
124918c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1250cd0c2139SMatthew G Knepley 
1251cd0c2139SMatthew G Knepley   PetscFunctionBegin;
12529566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
12539566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
12549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
12559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1256412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
12579566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1258cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
12599566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
12609566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld));
12619566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints));
1262607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
12639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
12649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1265a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1266a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1267607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
126818c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1269607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1270a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1271607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
127218c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
127318c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
127418c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
127559eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
127659eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
127759eef20bSToby Isaac     depthShift[2 * d]     = depthMax[d];
127859eef20bSToby Isaac     depthShift[2 * d + 1] = 0;
1279607ab7a9SMatthew G. Knepley   }
1280b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1281b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1282cd0c2139SMatthew G Knepley   if (label) {
12839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
12849566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
12859566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1286cd0c2139SMatthew G Knepley   }
1287cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1288cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1289cd0c2139SMatthew G Knepley 
1290cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
12919566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
129218c5995bSMatthew G. Knepley     if (splitIS[dep]) {
12939566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
12949566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
129518c5995bSMatthew G. Knepley     }
12969566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep]));
129718c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
12989566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
12999566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1300cd0c2139SMatthew G Knepley     }
1301cd0c2139SMatthew G Knepley   }
1302b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS));
1303b6dfa339SMatthew G. Knepley   if (ghostIS) {
1304b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1305b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1306b6dfa339SMatthew G. Knepley   }
1307607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
130818c5995bSMatthew 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   */
13092582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d];
13109566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
131159eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
131259eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
131359eef20bSToby Isaac    * added points */
13142582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
13159566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1316cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1317cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1318cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1319cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
13202582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
13214c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1322cd0c2139SMatthew G Knepley       const PetscInt *support;
1323394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
13244c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1325cd0c2139SMatthew G Knepley 
13269566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13279566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
13289566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13299566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
13309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
13319566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1332cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
13334c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13344c367dbcSMatthew G. Knepley 
1335cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
13369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1337412e9a14SMatthew G. Knepley         switch (coneSize) {
1338d71ae5a4SJacob Faibussowitsch         case 2:
1339d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));
1340d71ae5a4SJacob Faibussowitsch           break;
1341d71ae5a4SJacob Faibussowitsch         case 3:
1342d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));
1343d71ae5a4SJacob Faibussowitsch           break;
1344d71ae5a4SJacob Faibussowitsch         case 4:
1345d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));
1346d71ae5a4SJacob Faibussowitsch           break;
1347412e9a14SMatthew G. Knepley         }
1348b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1349b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1350b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1351b6dfa339SMatthew G. Knepley           const PetscInt *support;
1352b6dfa339SMatthew G. Knepley           PetscInt        val;
1353b6dfa339SMatthew G. Knepley 
1354b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1355b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1356b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1357b6dfa339SMatthew G. Knepley           else PetscCall(DMPlexSetSupportSize(sdm, newp, 2));
1358b6dfa339SMatthew G. Knepley         }
1359cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13604c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1361cd0c2139SMatthew G Knepley 
13629566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13634c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1364cd0c2139SMatthew G Knepley           PetscInt val;
1365cd0c2139SMatthew G Knepley 
13669566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13674c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
13684c367dbcSMatthew G. Knepley           if ((val == 1) || (val == (shift + 1))) ++qn;
13694c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1370cd0c2139SMatthew G Knepley         }
13714c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
13729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13734c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
13749566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13754c367dbcSMatthew G. Knepley         /* Add hybrid edge */
13769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
13779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13789566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1379cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
13804c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13814c367dbcSMatthew G. Knepley 
13829566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13834c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1384cd0c2139SMatthew G Knepley           PetscInt val;
1385cd0c2139SMatthew G Knepley 
13869566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13874c367dbcSMatthew G. Knepley           if (val == dim - 1) ++qf;
13884c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn;
13894c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp;
1390cd0c2139SMatthew G Knepley         }
13914c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
13929566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13934c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
13949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13954c367dbcSMatthew G. Knepley         /* Add hybrid face */
13969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
13979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
13989566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1399cd0c2139SMatthew G Knepley       }
1400cd0c2139SMatthew G Knepley     }
1401cd0c2139SMatthew G Knepley   }
140218c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
140318c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
140418c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
14052582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
140618c5995bSMatthew G. Knepley       const PetscInt *support;
1407da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
140818c5995bSMatthew G. Knepley 
14099566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14109566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14119566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
141218c5995bSMatthew G. Knepley       if (dep == 0) {
141318c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
141418c5995bSMatthew G. Knepley 
141539254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
141639254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
14179566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
141839254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
141939254ff6SMatthew G. Knepley         }
14209566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
142118c5995bSMatthew G. Knepley         /* Add hybrid edge */
14229566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1423e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1424e1757548SMatthew G. Knepley           PetscInt val;
1425e1757548SMatthew G. Knepley 
14269566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1427e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1428da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1429da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1430e1757548SMatthew G. Knepley         }
14319566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
14329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
143318c5995bSMatthew G. Knepley       } else if (dep == dim - 2) {
143418c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1435cd0c2139SMatthew G Knepley         PetscInt       val;
1436cd0c2139SMatthew G Knepley 
1437da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14389566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1439da1dd7e4SMatthew G. Knepley           if (val == dim - 1) qf += 2;
1440da1dd7e4SMatthew G. Knepley           else ++qf;
1441cd0c2139SMatthew G Knepley         }
144218c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
14439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
144418c5995bSMatthew G. Knepley         /* Add hybrid face */
1445da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14469566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1447da1dd7e4SMatthew G. Knepley           if (val == dim - 1) ++qf;
1448da1dd7e4SMatthew G. Knepley         }
14499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14509566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1452cd0c2139SMatthew G Knepley       }
1453cd0c2139SMatthew G Knepley     }
1454cd0c2139SMatthew G Knepley   }
1455cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
14569566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
14579566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
14589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
14599566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew));
1460cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1461cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1462cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1463cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
14642582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
14654c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1466cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1467b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
14684c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1469cd0c2139SMatthew G Knepley 
14709566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
14719566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14729566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
14739566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
14749566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14759566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1476cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
147796a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
14784c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1479cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1480cd0c2139SMatthew G Knepley 
1481b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1482cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
14839566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp, &supportF));
14849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1485cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1486cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1487cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
14889566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
148918c5995bSMatthew G. Knepley           if (v < 0) {
14909566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
149163a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1);
14922582d50cSToby Isaac             coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
149396a07cd0SMatthew G. Knepley             hasUnsplit     = PETSC_TRUE;
149418c5995bSMatthew G. Knepley           } else {
14954c367dbcSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep - 1];
1496163235baSMatthew G. Knepley             if (dep > 1) {
1497163235baSMatthew G. Knepley               const PetscInt *econe;
1498163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1499163235baSMatthew G. Knepley 
15009566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
15019566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1502163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
15039566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs));
15049566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu));
1505163235baSMatthew G. Knepley                 if (vs >= 0) continue;
150663a3b9bcSJacob Faibussowitsch                 PetscCheck(vu >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, econe[r], dep - 2);
1507163235baSMatthew G. Knepley                 hasUnsplit = PETSC_TRUE;
1508163235baSMatthew G. Knepley               }
1509163235baSMatthew G. Knepley             }
1510cd0c2139SMatthew G Knepley           }
1511cd0c2139SMatthew G Knepley         }
15129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
15139566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1514e537020bSMatthew G. Knepley         /* Face support */
1515b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1516cd0c2139SMatthew G Knepley 
1517b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1518b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1519b6dfa339SMatthew G. Knepley         else vals[1] = -vals[0];
1520b6dfa339SMatthew G. Knepley         PetscCheck(vals[0] * vals[1] < 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid support labels %" PetscInt_FMT " %" PetscInt_FMT, vals[0], vals[1]);
1521b6dfa339SMatthew G. Knepley 
1522b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1523b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1524b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1525b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1526accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1527b6dfa339SMatthew G. Knepley             } else {
1528b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1529accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1530b6dfa339SMatthew G. Knepley             }
1531b6dfa339SMatthew G. Knepley           } else {
1532b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1533cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
15349566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1535cd0c2139SMatthew G Knepley             } else {
1536cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
15379566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1538b6dfa339SMatthew G. Knepley             }
1539b6dfa339SMatthew G. Knepley           }
1540b6dfa339SMatthew G. Knepley         }
1541b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1542e537020bSMatthew G. Knepley         {
1543e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1544b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1545accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1546e537020bSMatthew G. Knepley 
15479371c9d4SSatish Balay           if (supportSize > 1) {
15489371c9d4SSatish Balay             ocell = vals[0] < 0 ? support[1] : support[0];
15499371c9d4SSatish Balay           } else {
15509371c9d4SSatish Balay             ocell = support[0];
15519371c9d4SSatish Balay             flip  = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;
15529371c9d4SSatish Balay           }
1553b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1554b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1555e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1556e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1557e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1558b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc];
1559e537020bSMatthew G. Knepley               break;
1560cd0c2139SMatthew G Knepley             }
1561cd0c2139SMatthew G Knepley           }
1562b6dfa339SMatthew G. Knepley           PetscCheck(nc < nconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %" PetscInt_FMT " in neighboring cell %" PetscInt_FMT, oldp, ocell);
1563e537020bSMatthew G. Knepley         }
15644c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1565b6dfa339SMatthew G. Knepley         {
1566b6dfa339SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangments(ct) / 2;
1567b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1568b6dfa339SMatthew G. Knepley         }
1569b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1570b5a892a1SMatthew G. Knepley 
1571fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp; /* Extracted negative side orientation above */
15724c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
15734c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1574e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1575412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1576b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q * 2 + 0];
1577b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q * 2 + 1];
1578b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1579412e9a14SMatthew G. Knepley 
15809566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
158118c5995bSMatthew G. Knepley           if (v < 0) {
15829566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
158318c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
158418c5995bSMatthew G. Knepley           } else {
158518c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep];
158618c5995bSMatthew G. Knepley           }
1587b5a892a1SMatthew G. Knepley           coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1588e537020bSMatthew G. Knepley         }
15899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
15909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
159196a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
15929566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1593cd0c2139SMatthew G Knepley       } else if (dep == 0) {
15944c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1595cd0c2139SMatthew G Knepley 
1596cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
15974c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1598cd0c2139SMatthew G Knepley           PetscInt val;
1599cd0c2139SMatthew G Knepley 
16009566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1601ad540459SPierre Jolivet           if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1602cd0c2139SMatthew G Knepley         }
16034c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
16049566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1605cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
16064c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1607cd0c2139SMatthew G Knepley           PetscInt val, edge;
1608cd0c2139SMatthew G Knepley 
16099566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1610cd0c2139SMatthew G Knepley           if (val == 1) {
16119566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
161263a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16134c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep + 1];
1614cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
16152582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1616cd0c2139SMatthew G Knepley           }
1617cd0c2139SMatthew G Knepley         }
16184c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
16199566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16204c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1621cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1622cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
16239566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
16244c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
16254c367dbcSMatthew G. Knepley           PetscInt val, edge;
16264c367dbcSMatthew G. Knepley 
16279566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16284c367dbcSMatthew G. Knepley           if (val == 1) {
16299566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
163063a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16314c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
16324c367dbcSMatthew G. Knepley           }
16334c367dbcSMatthew G. Knepley         }
16349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1635cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
16364c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16374c367dbcSMatthew G. Knepley 
1638cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1639cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1640cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
16419566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
1642e1757548SMatthew G. Knepley           if (v < 0) {
16439566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
164463a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1);
16452582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1646e1757548SMatthew G. Knepley           } else {
16474c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep - 1];
1648cd0c2139SMatthew G Knepley           }
1649e1757548SMatthew G. Knepley         }
16509566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1651cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1652cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1653cd0c2139SMatthew G Knepley           PetscInt val;
1654cd0c2139SMatthew G Knepley 
16559566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16564c367dbcSMatthew G. Knepley           if (val == dim - 1) {
16572582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
16584c367dbcSMatthew G. Knepley           } else if (val == (shift + dim - 1)) {
16592582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1660cd0c2139SMatthew G Knepley           }
1661cd0c2139SMatthew G Knepley         }
1662b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1664cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1665cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1666cd0c2139SMatthew G Knepley           PetscInt val, face;
1667cd0c2139SMatthew G Knepley 
16689566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1669cd0c2139SMatthew G Knepley           if (val == dim - 1) {
16709566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
167163a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
16724c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep + 1];
1673cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim - 1)) {
16742582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1675cd0c2139SMatthew G Knepley           }
1676cd0c2139SMatthew G Knepley         }
1677b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16789566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16794c367dbcSMatthew G. Knepley         /* Hybrid face */
16804c367dbcSMatthew G. Knepley         coneNew[0] = newp;
16814c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
16824c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
16834c367dbcSMatthew G. Knepley           PetscInt vertex;
16849566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex));
1685e1757548SMatthew G. Knepley           if (vertex < 0) {
16869566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex));
168763a3b9bcSJacob Faibussowitsch             PetscCheck(vertex >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[v], dep - 1);
1688e1757548SMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
1689e1757548SMatthew G. Knepley           } else {
16904c367dbcSMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
16914c367dbcSMatthew G. Knepley           }
1692e1757548SMatthew G. Knepley         }
16939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
16944c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
16954c367dbcSMatthew G. Knepley           PetscInt val, face;
16964c367dbcSMatthew G. Knepley 
16979566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16984c367dbcSMatthew G. Knepley           if (val == dim - 1) {
16999566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
170063a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
17014c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
17024c367dbcSMatthew G. Knepley           }
17034c367dbcSMatthew G. Knepley         }
17049566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1705cd0c2139SMatthew G Knepley       }
1706cd0c2139SMatthew G Knepley     }
1707cd0c2139SMatthew G Knepley   }
170818c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
170918c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
171018c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
17112582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1712b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1713e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
171418c5995bSMatthew G. Knepley 
17159566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
17169566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
17179566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
17189566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
171918c5995bSMatthew G. Knepley       if (dep == 0) {
172018c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
172118c5995bSMatthew G. Knepley 
172218c5995bSMatthew G. Knepley         /* Unsplit vertex */
17239566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
172418c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
17252582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
17269566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
1727ad540459SPierre Jolivet           if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1];
172818c5995bSMatthew G. Knepley         }
172918c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
173018c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
173163a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
17329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
173318c5995bSMatthew G. Knepley         /* Hybrid edge */
173418c5995bSMatthew G. Knepley         coneNew[0] = newp;
173518c5995bSMatthew G. Knepley         coneNew[1] = newp;
17369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
173718c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
173818c5995bSMatthew G. Knepley           PetscInt val, edge;
173918c5995bSMatthew G. Knepley 
17409566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
174118c5995bSMatthew G. Knepley           if (val == 1) {
17429566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
174363a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
174418c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1745e1757548SMatthew G. Knepley           } else if (val == (shift2 + 1)) {
17469566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge));
174763a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1748e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1];
174918c5995bSMatthew G. Knepley           }
175018c5995bSMatthew G. Knepley         }
17519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1752e1757548SMatthew G. Knepley       } else if (dep == dim - 2) {
1753e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1754e1757548SMatthew G. Knepley 
1755da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1756e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1757e1757548SMatthew G. Knepley           PetscInt val, face;
1758e1757548SMatthew G. Knepley 
17599566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1760e1757548SMatthew G. Knepley           if (val == dim - 1) {
17619566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
176263a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
17632582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1764e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 1];
1765e1757548SMatthew G. Knepley           } else {
17662582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1767e1757548SMatthew G. Knepley           }
1768e1757548SMatthew G. Knepley         }
1769e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1770e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
17719566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
177263a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, newp, qf, supportSizeNew);
17739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1774e1757548SMatthew G. Knepley         /* Add hybrid face */
1775e1757548SMatthew G. Knepley         coneNew[0] = newp;
1776212cc919SMatthew G. Knepley         coneNew[1] = newp;
17779566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
177863a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1779212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17809566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
178163a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]);
1782e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1784da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1785da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1786da1dd7e4SMatthew G. Knepley 
17879566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1788da1dd7e4SMatthew G. Knepley           if (val == dim - 1) {
17899566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
1790da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1791da1dd7e4SMatthew G. Knepley           }
1792da1dd7e4SMatthew G. Knepley         }
17939566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
179463a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, hybface, qf, supportSizeNew);
17959566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1796cd0c2139SMatthew G Knepley       }
1797cd0c2139SMatthew G Knepley     }
1798cd0c2139SMatthew G Knepley   }
1799cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1800cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1801cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1802cd0c2139SMatthew G Knepley     IS              pIS;
1803cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1804cd0c2139SMatthew G Knepley     const PetscInt *points;
1805cd0c2139SMatthew G Knepley 
1806cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
18079566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1808cd0c2139SMatthew G Knepley     if (!pIS) continue;
1809cd0c2139SMatthew G Knepley     dep = -dep - shift;
18109566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
18119566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1812cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1813cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
18142582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1815cd0c2139SMatthew G Knepley       const PetscInt *cone;
1816cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
181750cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1818cd0c2139SMatthew G Knepley 
1819cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1820cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
18219566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
18229566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1823cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1824e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift);
1825cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1826cd0c2139SMatthew G Knepley 
18279566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1828cd0c2139SMatthew G Knepley         if (val == dep - 1) {
18299566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp));
183063a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1);
1831cd0c2139SMatthew G Knepley           csplitp = pMaxNew[dep - 1] + cp;
18329566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
183350cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1834cd0c2139SMatthew G Knepley         }
1835cd0c2139SMatthew G Knepley       }
18364a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
183728b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1838cd0c2139SMatthew G Knepley     }
18399566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
18409566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1841cd0c2139SMatthew G Knepley   }
18420e18dc48SMatthew G. Knepley   PetscCall(DMPlexReorderCohesiveSupports(sdm));
1843fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
18449566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
18459566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
18469566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
18479566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1848cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
18492582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1850cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1851cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1852cd0c2139SMatthew G Knepley 
18539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
18549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
18559566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1856cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d];
1857cd0c2139SMatthew G Knepley   }
18589566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1859fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
18609566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1861b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1862fa8e8ae5SToby Isaac   /* Step 9: Labels */
18639566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
18649566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
18659566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1866cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1867cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
18682582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1869cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1870cd0c2139SMatthew G Knepley       PetscInt       l;
1871cd0c2139SMatthew G Knepley 
18727db7e0a7SMatthew G. Knepley       if (splitLabel) {
18737db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
18747db7e0a7SMatthew G. Knepley 
18759566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp, val));
18769566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
18777db7e0a7SMatthew G. Knepley       }
1878cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1879cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1880cd0c2139SMatthew G Knepley         const char *lname;
1881cd0c2139SMatthew G Knepley         PetscInt    val;
18829a356370SMatthew G. Knepley         PetscBool   isDepth;
1883cd0c2139SMatthew G Knepley 
18849566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
18859566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
18869a356370SMatthew G. Knepley         if (isDepth) continue;
18879566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
18889566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
188948a46eb9SPierre Jolivet         if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val));
1890cd0c2139SMatthew G Knepley       }
1891cd0c2139SMatthew G Knepley     }
1892cd0c2139SMatthew G Knepley   }
1893cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1894cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1895cd0c2139SMatthew G Knepley 
1896cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
18979566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
18989566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
18999566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
19009566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1901cd0c2139SMatthew G Knepley   }
1902b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1903b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1904cd0c2139SMatthew G Knepley   if (label) {
19059566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
19069566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1907cd0c2139SMatthew G Knepley   }
19080d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
19099566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
191036dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
19110d4d4d06SMatthew G. Knepley   }
19129566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
19139566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
19149566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
19153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1916cd0c2139SMatthew G Knepley }
1917cd0c2139SMatthew G Knepley 
1918cd0c2139SMatthew G Knepley /*@C
1919cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1920cd0c2139SMatthew G Knepley 
192120f4b53cSBarry Smith   Collective
1922cd0c2139SMatthew G Knepley 
1923cd0c2139SMatthew G Knepley   Input Parameters:
192420f4b53cSBarry Smith + dm    - The original `DM`
192520f4b53cSBarry Smith - label - The `DMLabel` specifying the boundary faces (this could be auto-generated)
1926cd0c2139SMatthew G Knepley 
1927cd0c2139SMatthew G Knepley   Output Parameters:
192820f4b53cSBarry Smith + splitLabel - The `DMLabel` containing the split points, or `NULL` if no output is desired
192920f4b53cSBarry Smith - dmSplit    - The new `DM`
1930cd0c2139SMatthew G Knepley 
1931cd0c2139SMatthew G Knepley   Level: developer
1932cd0c2139SMatthew G Knepley 
19331cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1934cd0c2139SMatthew G Knepley @*/
1935d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1936d71ae5a4SJacob Faibussowitsch {
1937cd0c2139SMatthew G Knepley   DM       sdm;
1938cd0c2139SMatthew G Knepley   PetscInt dim;
1939cd0c2139SMatthew G Knepley 
1940cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1941cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19424f572ea9SToby Isaac   PetscAssertPointer(dmSplit, 4);
19439566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
19449566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
19459566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19469566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1947cd0c2139SMatthew G Knepley   switch (dim) {
1948cd0c2139SMatthew G Knepley   case 2:
1949d71ae5a4SJacob Faibussowitsch   case 3:
1950d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1951d71ae5a4SJacob Faibussowitsch     break;
1952d71ae5a4SJacob Faibussowitsch   default:
1953d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1954cd0c2139SMatthew G Knepley   }
1955cd0c2139SMatthew G Knepley   *dmSplit = sdm;
19563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1957cd0c2139SMatthew G Knepley }
1958cd0c2139SMatthew G Knepley 
19590f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
1960d71ae5a4SJacob Faibussowitsch static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1961d71ae5a4SJacob Faibussowitsch {
19620f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
19630f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
19640f66a230SMatthew G. Knepley 
19650f66a230SMatthew G. Knepley   PetscFunctionBegin;
19660f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
19679566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
19709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
19710f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
19720f66a230SMatthew G. Knepley     if (cone[c] == face) {
19730f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
19740f66a230SMatthew G. Knepley 
19750f66a230SMatthew G. Knepley       if (subdm) {
19760f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
19770f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
19780f66a230SMatthew G. Knepley 
19799566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
19809566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
19819566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
19829566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
19839566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
19840f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
19850f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
19860f66a230SMatthew G. Knepley             o = subornt[0];
19870f66a230SMatthew G. Knepley             break;
19880f66a230SMatthew G. Knepley           }
19890f66a230SMatthew G. Knepley         }
199063a3b9bcSJacob Faibussowitsch         PetscCheck(sc < subconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %" PetscInt_FMT " (%" PetscInt_FMT ") in cone for subpoint %" PetscInt_FMT " (%" PetscInt_FMT ")", subface, face, subpoint, cell);
19910f66a230SMatthew G. Knepley       }
19920f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
19930f66a230SMatthew G. Knepley       else *pos = PETSC_FALSE;
19940f66a230SMatthew G. Knepley       break;
19950f66a230SMatthew G. Knepley     }
19960f66a230SMatthew G. Knepley   }
199763a3b9bcSJacob Faibussowitsch   PetscCheck(c != coneSize, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " in split face %" PetscInt_FMT " support does not have it in the cone", cell, face);
19983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19990f66a230SMatthew G. Knepley }
20000f66a230SMatthew G. Knepley 
2001d71ae5a4SJacob Faibussowitsch static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label)
2002d71ae5a4SJacob Faibussowitsch {
2003accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
2004accc9626SMatthew G. Knepley   const PetscInt *points;
2005accc9626SMatthew G. Knepley   PetscInt        dim, numPoints, p, shift = 100, shift2 = 200;
2006accc9626SMatthew G. Knepley 
2007accc9626SMatthew G. Knepley   PetscFunctionBegin;
2008accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
2009accc9626SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
2010accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
2011accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
2012accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
2013accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
2014accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
20153ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2016accc9626SMatthew G. Knepley   }
2017accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
2018accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
2019accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
2020accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
2021accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
2022accc9626SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
2023accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
2024accc9626SMatthew G. Knepley     const PetscInt *support;
2025accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
2026accc9626SMatthew G. Knepley 
2027accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
2028accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
2029accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
2030accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
2031accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
2032accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
2033accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
2034e1a13daeSMatthew G. Knepley     /* Check that this face is not incident on only unsplit faces, meaning has at least one split face */
2035e1a13daeSMatthew G. Knepley     {
2036e1a13daeSMatthew G. Knepley       PetscInt *closure = NULL;
2037e1a13daeSMatthew G. Knepley       PetscBool split   = PETSC_FALSE;
2038e1a13daeSMatthew G. Knepley       PetscInt  closureSize, cl;
2039e1a13daeSMatthew G. Knepley 
2040e1a13daeSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2041e1a13daeSMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2042e1a13daeSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
20439371c9d4SSatish Balay         if ((valA >= 0) && (valA <= dim)) {
20449371c9d4SSatish Balay           split = PETSC_TRUE;
20459371c9d4SSatish Balay           break;
20469371c9d4SSatish Balay         }
2047e1a13daeSMatthew G. Knepley       }
2048e1a13daeSMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2049e1a13daeSMatthew G. Knepley       if (!split) continue;
2050e1a13daeSMatthew G. Knepley     }
2051accc9626SMatthew G. Knepley     /* Split the face */
2052accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, point, &valA));
2053accc9626SMatthew G. Knepley     PetscCall(DMLabelClearValue(label, point, valA));
2054accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, dim - 1));
2055accc9626SMatthew G. Knepley     /* Label its closure:
2056accc9626SMatthew G. Knepley       unmarked: label as unsplit
2057accc9626SMatthew G. Knepley       incident: relabel as split
2058accc9626SMatthew G. Knepley       split:    do nothing
2059accc9626SMatthew G. Knepley     */
2060accc9626SMatthew G. Knepley     {
2061accc9626SMatthew G. Knepley       PetscInt *closure = NULL;
2062accc9626SMatthew G. Knepley       PetscInt  closureSize, cl, dep;
2063accc9626SMatthew G. Knepley 
2064accc9626SMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2065accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2066accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
2067accc9626SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
2068accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2069accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
2070accc9626SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2071accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2072accc9626SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], valA));
2073accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], dep));
2074accc9626SMatthew G. Knepley         }
2075accc9626SMatthew G. Knepley       }
2076accc9626SMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2077accc9626SMatthew G. Knepley     }
2078accc9626SMatthew G. Knepley   }
2079accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2080accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
20813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2082accc9626SMatthew G. Knepley }
2083accc9626SMatthew G. Knepley 
2084cd0c2139SMatthew G Knepley /*@
20850f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
2086cd0c2139SMatthew G Knepley   to complete the surface
2087cd0c2139SMatthew G Knepley 
2088cd0c2139SMatthew G Knepley   Input Parameters:
208920f4b53cSBarry Smith + dm     - The `DM`
209020f4b53cSBarry Smith . label  - A `DMLabel` marking the surface
209120f4b53cSBarry Smith . blabel - A `DMLabel` marking the vertices on the boundary which will not be duplicated, or `NULL` to find them automatically
209220f4b53cSBarry Smith . bvalue - Value of `DMLabel` marking the vertices on the boundary
2093bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
209420f4b53cSBarry Smith - subdm  - The `DM` associated with the label, or `NULL`
2095cd0c2139SMatthew G Knepley 
2096cd0c2139SMatthew G Knepley   Output Parameter:
209720f4b53cSBarry Smith . label - A `DMLabel` marking all surface points
20980f66a230SMatthew G. Knepley 
2099cd0c2139SMatthew G Knepley   Level: developer
2100cd0c2139SMatthew G Knepley 
210120f4b53cSBarry Smith   Note:
210220f4b53cSBarry Smith   The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
210320f4b53cSBarry Smith 
21041cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2105cd0c2139SMatthew G Knepley @*/
2106d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, DM subdm)
2107d71ae5a4SJacob Faibussowitsch {
2108d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2109accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
211047946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2111bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
2112accc9626SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = 300, dim, depth, numPoints, numSubpoints, p, val;
2113cd0c2139SMatthew G Knepley 
2114cd0c2139SMatthew G Knepley   PetscFunctionBegin;
21159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
21169566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
21179566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
211847946fd8SMatthew G. Knepley   if (subdm) {
21199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
212047946fd8SMatthew G. Knepley     if (subpointIS) {
21219566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
21229566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
212347946fd8SMatthew G. Knepley     }
212447946fd8SMatthew G. Knepley   }
2125d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
21269566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2127accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
21289566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
21299566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2130d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2131cd0c2139SMatthew G Knepley     const PetscInt *support;
2132cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2133cd0c2139SMatthew G Knepley 
21349566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2135c4419245SMatthew G. Knepley #if 0
2136c4419245SMatthew G. Knepley     if (supportSize != 2) {
2137c4419245SMatthew G. Knepley       const PetscInt *lp;
2138c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2139c4419245SMatthew G. Knepley 
2140c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2141c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
21429566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
21439566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
214463a3b9bcSJacob Faibussowitsch       PetscCheck(pind >= 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %" PetscInt_FMT " has %" PetscInt_FMT " != 2 supports, and the face is not shared with another process", points[p], supportSize);
2145c4419245SMatthew G. Knepley     }
2146c4419245SMatthew G. Knepley #endif
21479566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2148cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
21490f66a230SMatthew G. Knepley       const PetscInt *cone;
2150cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
21510f66a230SMatthew G. Knepley       PetscBool       pos;
2152cd0c2139SMatthew G Knepley 
21539566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
21549566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
21559566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
21560f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
21570f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
21589566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
21599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2160cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2161cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2162cd0c2139SMatthew G Knepley 
21639566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2164cd0c2139SMatthew G Knepley         if (val == -1) {
2165cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2166cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2167cd0c2139SMatthew G Knepley 
21689566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2169cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2170cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2171a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2172cd0c2139SMatthew G Knepley 
21739566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
21749566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2175a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim - 1) && (bval < 0)) {
21769566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2177cd0c2139SMatthew G Knepley               break;
2178cd0c2139SMatthew G Knepley             }
2179cd0c2139SMatthew G Knepley           }
21809566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2181cd0c2139SMatthew G Knepley         }
2182cd0c2139SMatthew G Knepley       }
2183cd0c2139SMatthew G Knepley     }
2184cd0c2139SMatthew G Knepley   }
2185accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2186accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2187a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
218886200784SMatthew G. Knepley   if (blabel) {
2189accc9626SMatthew G. Knepley     IS bdIS;
2190accc9626SMatthew G. Knepley 
2191caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2192accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2193accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2194a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2195a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2196a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2197a0541d8aSMatthew G. Knepley 
21989566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2199a0541d8aSMatthew G. Knepley       if (bval >= 0) {
22009566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2201f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2202f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
22039566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2204f7019248SMatthew G. Knepley         }
2205f7019248SMatthew G. Knepley       }
2206f7019248SMatthew G. Knepley     }
2207f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2208f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2209f7019248SMatthew G. Knepley       PetscInt       val, bval;
2210f7019248SMatthew G. Knepley 
22119566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2212f7019248SMatthew G. Knepley       if (bval >= 0) {
221386200784SMatthew G. Knepley         const PetscInt *cone, *support;
221486200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
221586200784SMatthew G. Knepley 
2216a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
22179566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2218e1a13daeSMatthew G. Knepley         PetscCheck(val >= 0 && val <= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be part of the fault", point, val);
22199566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
22209566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
22212c06a818SMatthew G. Knepley         /* Check for cross-edge
22222c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
222386200784SMatthew G. Knepley         if (val != 0) continue;
22249566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
22259566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
222686200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
22279566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
22289566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
222963a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
22309566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
22319566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
22329566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
22339566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
223486200784SMatthew G. Knepley         }
2235a0541d8aSMatthew G. Knepley       }
2236a0541d8aSMatthew G. Knepley     }
2237accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2238accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2239a0541d8aSMatthew G. Knepley   }
2240b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2241b6dfa339SMatthew G. Knepley   {
2242b6dfa339SMatthew G. Knepley     PetscSF         sf;
2243b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2244b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2245b6dfa339SMatthew G. Knepley 
2246b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2247b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2248accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2249accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2250accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2251accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2252b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2253b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2254b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2255b6dfa339SMatthew G. Knepley         PetscInt       val;
2256b6dfa339SMatthew G. Knepley 
2257b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2258b6dfa339SMatthew G. Knepley         if (l >= 0) {
2259b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2260b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2261b6dfa339SMatthew G. Knepley 
2262b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2263b6dfa339SMatthew G. Knepley           PetscCheck((val == dim - 1) || (val == shift2 + dim - 1), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be a fault face", point, val);
2264b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2265b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2266b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2267accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2268b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2269b6dfa339SMatthew G. Knepley 
2270b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2271b6dfa339SMatthew G. Knepley             PetscCheck(val != -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " is missing from label, but is in the closure of a fault face", point);
2272b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2273b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2274b6dfa339SMatthew G. Knepley           }
2275b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2276b6dfa339SMatthew G. Knepley         }
2277b6dfa339SMatthew G. Knepley       }
2278b6dfa339SMatthew G. Knepley     }
2279b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2280b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2281accc9626SMatthew G. Knepley   }
2282accc9626SMatthew G. Knepley divide:
2283b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2284accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
2285accc9626SMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label));
22863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2287cd0c2139SMatthew G Knepley }
2288cd0c2139SMatthew G Knepley 
2289720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
229066976f2fSJacob Faibussowitsch static PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2291d71ae5a4SJacob Faibussowitsch {
2292720e594eSMatthew G. Knepley   IS              subpointIS;
2293720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2294720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2295720e594eSMatthew G. Knepley 
2296720e594eSMatthew G. Knepley   PetscFunctionBegin;
22973ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
22989566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
22999566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
23003ba16761SJacob Faibussowitsch   if (!subpointIS) PetscFunctionReturn(PETSC_SUCCESS);
23019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
23029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
23039566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2304720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2305720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2306720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2307720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2308720e594eSMatthew G. Knepley 
23099566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2310720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2311720e594eSMatthew G. Knepley       PetscInt value = 0;
2312720e594eSMatthew G. Knepley 
2313720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
23149566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
23159371c9d4SSatish Balay       if (value == defaultValue) {
23169371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
23179371c9d4SSatish Balay         break;
23189371c9d4SSatish Balay       }
2319720e594eSMatthew G. Knepley     }
23209566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2321720e594eSMatthew G. Knepley     if (invalidCell) {
23229566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23239566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
23249566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
232563a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2326720e594eSMatthew G. Knepley     }
2327720e594eSMatthew G. Knepley   }
23289566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2330720e594eSMatthew G. Knepley }
2331720e594eSMatthew G. Knepley 
2332c08575a3SMatthew G. Knepley /*@
23333cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
23343cf72582SMatthew G. Knepley 
233520f4b53cSBarry Smith   Collective
23363cf72582SMatthew G. Knepley 
23373cf72582SMatthew G. Knepley   Input Parameters:
233820f4b53cSBarry Smith + dm      - The original `DM`
2339720e594eSMatthew G. Knepley . label   - The label specifying the interface vertices
2340caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2341caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
23423cf72582SMatthew G. Knepley 
23433cf72582SMatthew G. Knepley   Output Parameters:
234420f4b53cSBarry Smith + hybridLabel - The label fully marking the interface, or `NULL` if no output is desired
234520f4b53cSBarry Smith . splitLabel  - The label containing the split points, or `NULL` if no output is desired
234620f4b53cSBarry Smith . dmInterface - The new interface `DM`, or `NULL`
234720f4b53cSBarry Smith - dmHybrid    - The new `DM` with cohesive cells
23483cf72582SMatthew G. Knepley 
234920f4b53cSBarry Smith   Level: developer
235020f4b53cSBarry Smith 
235120f4b53cSBarry Smith   Note:
235220f4b53cSBarry Smith   The hybridLabel indicates what parts of the original mesh impinged on the division surface. For points
23536eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
23546eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
23556eccb800SMatthew 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
23566eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
23576eccb800SMatthew Knepley 
23586eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
235940cdb708SMatthew Knepley   mesh. The label value is $\pm 100+dim$ for each point. For example, if two edges 10 and 14 in the hybrid resulting from
23606eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
23616eccb800SMatthew Knepley 
236220f4b53cSBarry Smith   The dmInterface is a `DM` built from the original division surface. It has a label which can be retrieved using
236320f4b53cSBarry Smith   `DMPlexGetSubpointMap()` which maps each point back to the point in the surface of the original mesh.
23646eccb800SMatthew Knepley 
23651cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
23663cf72582SMatthew G. Knepley @*/
2367d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
2368d71ae5a4SJacob Faibussowitsch {
23693cf72582SMatthew G. Knepley   DM       idm;
23707db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
23713cf72582SMatthew G. Knepley   PetscInt dim;
23723cf72582SMatthew G. Knepley 
23733cf72582SMatthew G. Knepley   PetscFunctionBegin;
23743cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23754f572ea9SToby Isaac   if (label) PetscAssertPointer(label, 2);
23764f572ea9SToby Isaac   if (bdlabel) PetscAssertPointer(bdlabel, 3);
23774f572ea9SToby Isaac   if (hybridLabel) PetscAssertPointer(hybridLabel, 5);
23784f572ea9SToby Isaac   if (splitLabel) PetscAssertPointer(splitLabel, 6);
23794f572ea9SToby Isaac   if (dmInterface) PetscAssertPointer(dmInterface, 7);
23804f572ea9SToby Isaac   PetscAssertPointer(dmHybrid, 8);
23819566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
23829566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
23839566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
23849566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
23859566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
23869566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
23879566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
23887db7e0a7SMatthew G. Knepley   if (splitLabel) {
23897db7e0a7SMatthew G. Knepley     const char *name;
23907db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
23917db7e0a7SMatthew G. Knepley 
23929566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
2393c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(sname, name, sizeof(sname)));
2394c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(sname, " split", sizeof(sname)));
23959566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
23967db7e0a7SMatthew G. Knepley   }
2397caf9e14dSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, idm));
23989371c9d4SSatish Balay   if (dmInterface) {
23999371c9d4SSatish Balay     *dmInterface = idm;
24009371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
24019566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
24025de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
24033cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
24049566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
24057db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
24064a7ee7d0SMatthew G. Knepley   {
24074a7ee7d0SMatthew G. Knepley     DM      cdm;
24084a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
24094a7ee7d0SMatthew G. Knepley 
24104a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
24119566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
24129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
24139566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
24144a7ee7d0SMatthew G. Knepley   }
24153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2416cd0c2139SMatthew G Knepley }
2417cd0c2139SMatthew G Knepley 
2418efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2419efa14ee0SMatthew G Knepley 
2420efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2421efa14ee0SMatthew G Knepley */
2422d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2423d71ae5a4SJacob Faibussowitsch {
2424fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2425efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2426412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2427efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2428efa14ee0SMatthew G Knepley 
2429efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2430efa14ee0SMatthew G Knepley   *numFaces = 0;
2431efa14ee0SMatthew G Knepley   *nFV      = 0;
24329566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24339566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
243477d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
24359566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
243648a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2437efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
24389566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2439efa14ee0SMatthew G Knepley   if (subvertexIS) {
24409566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2442efa14ee0SMatthew G Knepley   }
2443efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2444efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24450298fd71SBarry Smith     PetscInt      *star   = NULL;
2446efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2447efa14ee0SMatthew G Knepley 
24489566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2449efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2450efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2451efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2452efa14ee0SMatthew G Knepley     }
2453efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2454efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
24550298fd71SBarry Smith       PetscInt      *closure = NULL;
2456efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2457efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2458efa14ee0SMatthew G Knepley 
24599566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
246065560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
246163a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
24629566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2463efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2464efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2465efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2466efa14ee0SMatthew G Knepley 
2467efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2468efa14ee0SMatthew G Knepley           ++numCorners;
24699566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2470830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2471efa14ee0SMatthew G Knepley         }
2472efa14ee0SMatthew G Knepley       }
24739566063dSJacob Faibussowitsch       if (!(*nFV)) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
247463a3b9bcSJacob Faibussowitsch       PetscCheck(faceSize <= *nFV, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
2475efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2476007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2477007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2478007baee2SMatthew G. Knepley 
2479efa14ee0SMatthew G Knepley         ++(*numFaces);
248048a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
24819566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
248248a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
24839566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2484efa14ee0SMatthew G Knepley       }
24859566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2486efa14ee0SMatthew G Knepley     }
24879566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2488efa14ee0SMatthew G Knepley   }
248948a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
24909566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
24919566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
24923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2493efa14ee0SMatthew G Knepley }
2494efa14ee0SMatthew G Knepley 
2495d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2496d71ae5a4SJacob Faibussowitsch {
249734b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2498efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2499412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2500efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2501efa14ee0SMatthew G Knepley 
2502efa14ee0SMatthew G Knepley   PetscFunctionBegin;
25039566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25049566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
250548a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2506efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
250734b4c39eSMatthew G. Knepley   if (vertexLabel) {
25089566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2509efa14ee0SMatthew G Knepley     if (subvertexIS) {
25109566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
25119566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2512efa14ee0SMatthew G Knepley     }
251334b4c39eSMatthew G. Knepley   }
2514efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2515efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
25160298fd71SBarry Smith     PetscInt      *star   = NULL;
2517efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2518efa14ee0SMatthew G Knepley 
25199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2520efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2521efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2522158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2523158acfadSMatthew G. Knepley 
2524158acfadSMatthew G. Knepley       if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) {
2525158acfadSMatthew G. Knepley         if (markedFaces) {
25269566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2527158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2528158acfadSMatthew G. Knepley         }
2529158acfadSMatthew G. Knepley         star[numFaces++] = point;
2530158acfadSMatthew G. Knepley       }
2531efa14ee0SMatthew G Knepley     }
2532efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2533efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
25340298fd71SBarry Smith       PetscInt      *closure = NULL;
2535efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2536efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2537efa14ee0SMatthew G Knepley 
25389566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2539efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
254063a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
25419566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2542efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2543efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2544efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2545efa14ee0SMatthew G Knepley 
2546efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
25479566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2548830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2549efa14ee0SMatthew G Knepley         }
2550efa14ee0SMatthew G Knepley       }
2551efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2552efa14ee0SMatthew G Knepley         const PetscInt *support;
2553efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2554efa14ee0SMatthew G Knepley 
2555efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2556efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2557efa14ee0SMatthew G Knepley 
2558efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2559efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
25609566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2561efa14ee0SMatthew G Knepley               break;
2562efa14ee0SMatthew G Knepley             }
2563efa14ee0SMatthew G Knepley           }
2564efa14ee0SMatthew G Knepley         }
25659566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
25669566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
256748a46eb9SPierre Jolivet         for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2568efa14ee0SMatthew G Knepley       }
25699566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2570efa14ee0SMatthew G Knepley     }
25719566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2572efa14ee0SMatthew G Knepley   }
25739566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25749566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25759566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2577efa14ee0SMatthew G Knepley }
2578efa14ee0SMatthew G Knepley 
2579d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2580d71ae5a4SJacob Faibussowitsch {
258127c04023SMatthew G. Knepley   DMLabel         label = NULL;
2582766ab985SMatthew G. Knepley   const PetscInt *cone;
25839fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2584766ab985SMatthew G. Knepley 
2585812bfc34SJed Brown   PetscFunctionBegin;
2586c0ed958bSJed Brown   *numFaces = 0;
2587c0ed958bSJed Brown   *nFV      = 0;
25889566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2589fed694aaSMatthew G. Knepley   *subCells = NULL;
25909566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25919566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
25923ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
259327c04023SMatthew G. Knepley   if (label) {
259427c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
259527c04023SMatthew G. Knepley       PetscInt val;
259627c04023SMatthew G. Knepley 
25979566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
259827c04023SMatthew G. Knepley       if (val == value) {
259927c04023SMatthew G. Knepley         ++(*numFaces);
26009566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
260127c04023SMatthew G. Knepley       }
260227c04023SMatthew G. Knepley     }
260327c04023SMatthew G. Knepley   } else {
2604766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
26059566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
260627c04023SMatthew G. Knepley   }
26079566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
26083ba16761SJacob Faibussowitsch   if (!(*numFaces)) PetscFunctionReturn(PETSC_SUCCESS);
26099fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2610766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2611766ab985SMatthew G. Knepley     const PetscInt *cells;
2612766ab985SMatthew G. Knepley     PetscInt        numCells;
2613766ab985SMatthew G. Knepley 
261427c04023SMatthew G. Knepley     if (label) {
261527c04023SMatthew G. Knepley       PetscInt val;
261627c04023SMatthew G. Knepley 
26179566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
261827c04023SMatthew G. Knepley       if (val != value) continue;
261927c04023SMatthew G. Knepley     }
26209566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
262148a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2622766ab985SMatthew G. Knepley     /* Negative face */
26239566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
262427234c99SMatthew G. Knepley     /* Not true in parallel
262508401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2626766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
26279566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
262827234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2629766ab985SMatthew G. Knepley     }
26309566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2631766ab985SMatthew G. Knepley     /* Positive face is not included */
2632766ab985SMatthew G. Knepley   }
26333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2634766ab985SMatthew G. Knepley }
2635766ab985SMatthew G. Knepley 
2636d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2637d71ae5a4SJacob Faibussowitsch {
2638766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2639766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2640766ab985SMatthew G. Knepley 
2641812bfc34SJed Brown   PetscFunctionBegin;
26429566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26443ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
26459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
26469566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2647766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2648766ab985SMatthew G. Knepley     const PetscInt *cone;
2649766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2650b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2651766ab985SMatthew G. Knepley 
265227c04023SMatthew G. Knepley     if (label) {
26539566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
265427c04023SMatthew G. Knepley       if (val != value) continue;
265527c04023SMatthew G. Knepley     }
26569566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
26579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
26589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
26591dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2660b3154360SMatthew G. Knepley     /* Negative face */
26619566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2662766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2663766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2664766ab985SMatthew G. Knepley 
2665766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2666766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
26679566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2668766ab985SMatthew G. Knepley           break;
2669766ab985SMatthew G. Knepley         }
2670766ab985SMatthew G. Knepley       }
2671766ab985SMatthew G. Knepley     }
26729566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2673766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2674766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2675766ab985SMatthew G. Knepley       const PetscInt *support;
2676766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2677766ab985SMatthew G. Knepley 
26789566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
267908401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
26809566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
268148a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2682766ab985SMatthew G. Knepley     }
2683766ab985SMatthew G. Knepley   }
26849566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
26853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2686766ab985SMatthew G. Knepley }
2687766ab985SMatthew G. Knepley 
2688d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2689d71ae5a4SJacob Faibussowitsch {
269082f516ccSBarry Smith   MPI_Comm       comm;
2691e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2692e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2693e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2694e6ccafaeSMatthew G Knepley 
269582f516ccSBarry Smith   PetscFunctionBegin;
26969566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
26979566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
269863a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2699e6ccafaeSMatthew G Knepley 
2700ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2701ddeab2a6SMatthew G. Knepley     /* Triangle */
2702e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2703e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2704ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2705ddeab2a6SMatthew G. Knepley     /* Triangle */
2706ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2707ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2708ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2709ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2710ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2711ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2712e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2713e6ccafaeSMatthew G Knepley     /* Quadratic line */
2714e6ccafaeSMatthew G Knepley     faceSize  = 1;
2715e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2716e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2717e6ccafaeSMatthew G Knepley     /* Quads */
2718e6ccafaeSMatthew G Knepley     faceSize = 2;
2719e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2720e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2721e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2722e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2723e6ccafaeSMatthew G Knepley     } else {
2724e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2725e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2726e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2727e6ccafaeSMatthew G Knepley     }
2728e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2729e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2730e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2731e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2732e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2733e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2734e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2735e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2736e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2737e6ccafaeSMatthew G Knepley       2, 3, 5, /* left */
2738e6ccafaeSMatthew G Knepley     };
2739e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2740e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2741e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2742e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2743e6ccafaeSMatthew G Knepley     };
2744e6ccafaeSMatthew G Knepley 
2745e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
27469566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2747e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2748e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2749e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2750e6ccafaeSMatthew G Knepley 
27519371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) {
2752e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2753e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2754e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2755e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2756e6ccafaeSMatthew G Knepley               break;
2757e6ccafaeSMatthew G Knepley             }
2758e6ccafaeSMatthew G Knepley           }
2759e6ccafaeSMatthew G Knepley         }
2760e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2761e6ccafaeSMatthew G Knepley         break;
2762e6ccafaeSMatthew G Knepley       }
2763e6ccafaeSMatthew G Knepley     }
276428b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2765e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
27663ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2767e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2768e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2769e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2770e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2771e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2772e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2773e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2774e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2775e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2776e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2777e6ccafaeSMatthew G Knepley       0, 3, 7, /* left */
2778e6ccafaeSMatthew G Knepley     };
2779e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2780e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2781e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2782e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2783e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2784e6ccafaeSMatthew G Knepley     };
2785e6ccafaeSMatthew G Knepley 
2786e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
27879566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2788e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2789e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2790e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2791e6ccafaeSMatthew G Knepley 
27929371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) {
2793e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2794e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2795e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2796e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2797e6ccafaeSMatthew G Knepley               break;
2798e6ccafaeSMatthew G Knepley             }
2799e6ccafaeSMatthew G Knepley           }
2800e6ccafaeSMatthew G Knepley         }
2801e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2802e6ccafaeSMatthew G Knepley         break;
2803e6ccafaeSMatthew G Knepley       }
2804e6ccafaeSMatthew G Knepley     }
280528b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2806e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28073ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2808e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2809e6ccafaeSMatthew G Knepley     /* Hexes
2810e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2811e6ccafaeSMatthew G Knepley        pointing up at the second.
2812e6ccafaeSMatthew G Knepley 
2813e6ccafaeSMatthew G Knepley           7---6
2814e6ccafaeSMatthew G Knepley          /|  /|
2815e6ccafaeSMatthew G Knepley         4---5 |
2816ddeab2a6SMatthew G. Knepley         | 1-|-2
2817e6ccafaeSMatthew G Knepley         |/  |/
2818ddeab2a6SMatthew G. Knepley         0---3
2819e6ccafaeSMatthew G Knepley 
2820e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2821e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2822e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2823e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2824e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2825e6ccafaeSMatthew G Knepley       0, 1, 2, 3, /* bottom */
2826e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2827ddeab2a6SMatthew G. Knepley       0, 3, 4, 5, /* front */
2828ddeab2a6SMatthew G. Knepley       2, 3, 5, 6, /* right */
2829ddeab2a6SMatthew G. Knepley       1, 2, 6, 7, /* back */
2830ddeab2a6SMatthew G. Knepley       0, 1, 4, 7, /* left */
2831e6ccafaeSMatthew G Knepley     };
2832e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2833ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2834e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2835ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2836ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2837ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2838ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2839e6ccafaeSMatthew G Knepley     };
2840e6ccafaeSMatthew G Knepley 
2841e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
28429566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2843e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2844e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2845e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2846e6ccafaeSMatthew G Knepley 
28479371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) {
2848e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2849e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2850e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2851e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2852e6ccafaeSMatthew G Knepley               break;
2853e6ccafaeSMatthew G Knepley             }
2854e6ccafaeSMatthew G Knepley           }
2855e6ccafaeSMatthew G Knepley         }
2856e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2857e6ccafaeSMatthew G Knepley         break;
2858e6ccafaeSMatthew G Knepley       }
2859e6ccafaeSMatthew G Knepley     }
286028b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2861e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28623ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2863e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2864e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2865e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2866e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2867e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2868e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2869e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2870e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2871e6ccafaeSMatthew G Knepley       0, 3, 4, 6, 7, 9, /* front */
2872e6ccafaeSMatthew G Knepley       1, 4, 5, 7, 8, 9, /* right */
2873e6ccafaeSMatthew G Knepley       2, 3, 5, 6, 8, 9, /* left */
2874e6ccafaeSMatthew G Knepley     };
2875e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2876e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2877e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2878e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2879e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2880e6ccafaeSMatthew G Knepley     };
2881e6ccafaeSMatthew G Knepley 
2882e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
28839566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2884e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2885e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2886e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2887e6ccafaeSMatthew G Knepley 
28889371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) {
2889e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2890e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2891e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2892e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2893e6ccafaeSMatthew G Knepley               break;
2894e6ccafaeSMatthew G Knepley             }
2895e6ccafaeSMatthew G Knepley           }
2896e6ccafaeSMatthew G Knepley         }
2897e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2898e6ccafaeSMatthew G Knepley         break;
2899e6ccafaeSMatthew G Knepley       }
2900e6ccafaeSMatthew G Knepley     }
290128b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2902e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29033ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2904e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2905e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2906e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2907e6ccafaeSMatthew G Knepley        pointing up at the second.
2908e6ccafaeSMatthew G Knepley 
2909e6ccafaeSMatthew G Knepley          7---6
2910e6ccafaeSMatthew G Knepley         /|  /|
2911e6ccafaeSMatthew G Knepley        4---5 |
2912e6ccafaeSMatthew G Knepley        | 3-|-2
2913e6ccafaeSMatthew G Knepley        |/  |/
2914e6ccafaeSMatthew G Knepley        0---1
2915e6ccafaeSMatthew G Knepley 
2916e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2917e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2918e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2919e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2920e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2921e6ccafaeSMatthew G Knepley       0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2922e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2923e6ccafaeSMatthew G Knepley       0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2924e6ccafaeSMatthew G Knepley       1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2925e6ccafaeSMatthew G Knepley       2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2926e6ccafaeSMatthew G Knepley       0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2927e6ccafaeSMatthew G Knepley     };
2928e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2929e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2930e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2931e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2932e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2933e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2934e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2935e6ccafaeSMatthew G Knepley     };
2936e6ccafaeSMatthew G Knepley 
2937e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
29389566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2939e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2940e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2941e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2942e6ccafaeSMatthew G Knepley 
29439371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) {
2944e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2945e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2946e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2947e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2948e6ccafaeSMatthew G Knepley               break;
2949e6ccafaeSMatthew G Knepley             }
2950e6ccafaeSMatthew G Knepley           }
2951e6ccafaeSMatthew G Knepley         }
2952e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2953e6ccafaeSMatthew G Knepley         break;
2954e6ccafaeSMatthew G Knepley       }
2955e6ccafaeSMatthew G Knepley     }
295628b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2957e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29583ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2959e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2960e6ccafaeSMatthew G Knepley   if (!posOrient) {
29619566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
2962e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
2963e6ccafaeSMatthew G Knepley   } else {
29649566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
2965e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2966e6ccafaeSMatthew G Knepley   }
2967e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
29683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2969e6ccafaeSMatthew G Knepley }
2970e6ccafaeSMatthew G Knepley 
2971c08575a3SMatthew G. Knepley /*@
2972c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2973c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2974c08575a3SMatthew G. Knepley 
297520f4b53cSBarry Smith   Not Collective
2976c08575a3SMatthew G. Knepley 
2977c08575a3SMatthew G. Knepley   Input Parameters:
2978c08575a3SMatthew G. Knepley + dm           - The original mesh
2979c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2980c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2981c08575a3SMatthew G. Knepley . face         - The face vertices
2982c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2983c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2984c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2985c08575a3SMatthew G. Knepley 
2986d8d19677SJose E. Roman   Output Parameters:
2987c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
298820f4b53cSBarry Smith - posOriented  - `PETSC_TRUE` if the face was oriented with outward normal
2989c08575a3SMatthew G. Knepley 
2990c08575a3SMatthew G. Knepley   Level: developer
2991c08575a3SMatthew G. Knepley 
29921cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
2993c08575a3SMatthew G. Knepley @*/
2994d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2995d71ae5a4SJacob Faibussowitsch {
29960298fd71SBarry Smith   const PetscInt *cone = NULL;
2997e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2998e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2999e6ccafaeSMatthew G Knepley 
3000e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
30029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
3003e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
3004e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
3005e6ccafaeSMatthew G Knepley 
3006e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
3007e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
30089371c9d4SSatish Balay         found = PETSC_TRUE;
30099371c9d4SSatish Balay         break;
3010e6ccafaeSMatthew G Knepley       }
3011e6ccafaeSMatthew G Knepley     }
3012e6ccafaeSMatthew G Knepley     if (found) {
3013e6ccafaeSMatthew G Knepley       indices[v2]      = v;
3014e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
3015e6ccafaeSMatthew G Knepley       ++v2;
3016e6ccafaeSMatthew G Knepley     } else {
3017e6ccafaeSMatthew G Knepley       oppositeVertex = v;
3018e6ccafaeSMatthew G Knepley     }
3019e6ccafaeSMatthew G Knepley   }
30209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
30213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3022e6ccafaeSMatthew G Knepley }
3023e6ccafaeSMatthew G Knepley 
3024e6ccafaeSMatthew G Knepley /*
3025cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
3026e6ccafaeSMatthew G Knepley 
302720f4b53cSBarry Smith   Not Collective
3028e6ccafaeSMatthew G Knepley 
3029e6ccafaeSMatthew G Knepley   Input Parameters:
303020f4b53cSBarry Smith   + dm              - The `DMPLEX`
3031e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
303220f4b53cSBarry Smith   . faceVertices    - The vertices in the face for `dm`
3033e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
303420f4b53cSBarry Smith   . numCorners      - The number of vertices in the `cell`
303520f4b53cSBarry Smith   . cell            - A cell in `dm` containing the face
3036e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
3037e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
3038e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
3039e6ccafaeSMatthew G Knepley 
30402fe279fdSBarry Smith   Output Parameter:
3041e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
3042e6ccafaeSMatthew G Knepley 
3043e6ccafaeSMatthew G Knepley   Level: developer
3044e6ccafaeSMatthew G Knepley */
3045d71ae5a4SJacob Faibussowitsch 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)
3046d71ae5a4SJacob Faibussowitsch {
304782f516ccSBarry Smith   MPI_Comm        comm;
3048e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
3049e6ccafaeSMatthew G Knepley   const PetscInt *faces;
3050e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
3051e6ccafaeSMatthew G Knepley 
3052e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30539566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
30549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
305563a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
3056e6ccafaeSMatthew G Knepley #if 0
3057e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
30589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3059e6ccafaeSMatthew G Knepley #else
3060e6ccafaeSMatthew G Knepley   {
3061e6ccafaeSMatthew G Knepley     PetscInt f;
3062e6ccafaeSMatthew G Knepley 
3063e6ccafaeSMatthew G Knepley     numFaces = 0;
30649566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3065e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
3066e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
3067e6ccafaeSMatthew G Knepley 
30689566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
30699566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
3070e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
3071e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
3072e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
3073e6ccafaeSMatthew G Knepley         PetscInt       v;
3074e6ccafaeSMatthew G Knepley 
3075e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
3076e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
3077e6ccafaeSMatthew G Knepley         }
3078e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
3079e6ccafaeSMatthew G Knepley       }
3080e6ccafaeSMatthew G Knepley       if (d == dof) {
3081e6ccafaeSMatthew G Knepley         numFaces               = 1;
3082e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
3083e6ccafaeSMatthew G Knepley       }
3084e6ccafaeSMatthew G Knepley     }
3085e6ccafaeSMatthew G Knepley   }
3086e6ccafaeSMatthew G Knepley #endif
308763a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3088f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3089e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
30909566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3091e6ccafaeSMatthew G Knepley   } else {
3092e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3093e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3094e6ccafaeSMatthew G Knepley 
30959566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3096e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3097e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
3098e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
30999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3100e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3101e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3102e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3103e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3104e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3105e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3106e6ccafaeSMatthew G Knepley           break;
3107e6ccafaeSMatthew G Knepley         }
3108e6ccafaeSMatthew G Knepley       }
310963a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3110e6ccafaeSMatthew G Knepley     }
31119566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
31129566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
31139566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3114e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3115e6ccafaeSMatthew G Knepley   }
3116ef07cca7SMatthew G. Knepley #if 0
31179566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3118ef07cca7SMatthew G. Knepley #else
31199566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3120ef07cca7SMatthew G. Knepley #endif
31213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3122e6ccafaeSMatthew G Knepley }
3123e6ccafaeSMatthew G Knepley 
3124d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3125d71ae5a4SJacob Faibussowitsch {
312682f516ccSBarry Smith   MPI_Comm        comm;
312753156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3128efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
3129efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3130efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3131fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3132efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3133e6ccafaeSMatthew G Knepley 
3134e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
31359566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3136efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
31379566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
31389566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
31399566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
31409566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3141efa14ee0SMatthew G Knepley   /* Setup chart */
31429566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
31439566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
31449566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
31459566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3146e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3147e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3148efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
3149e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
31509566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
31519566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
31529566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
31539566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
315448a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
315548a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
31569566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3157e6ccafaeSMatthew G Knepley   /* Create face cones */
31589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
31599566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
31609566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3161e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3162e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3163efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
31640298fd71SBarry Smith     PetscInt      *closure = NULL;
3165efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3166e6ccafaeSMatthew G Knepley 
31679566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3168efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3169efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3170e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3171e6ccafaeSMatthew G Knepley 
3172efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3173efa14ee0SMatthew G Knepley         ++numCorners;
31749566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3175efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3176efa14ee0SMatthew G Knepley           closure[faceSize] = point;
317765560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3178e6ccafaeSMatthew G Knepley           ++faceSize;
3179e6ccafaeSMatthew G Knepley         }
3180e6ccafaeSMatthew G Knepley       }
3181e6ccafaeSMatthew G Knepley     }
318263a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
318348a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
31849566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3185e6ccafaeSMatthew G Knepley   }
31869566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
31879566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
31889566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3189e6ccafaeSMatthew G Knepley   /* Build coordinates */
3190efa14ee0SMatthew G Knepley   {
3191efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3192efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3193efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3194285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
319524640c55SToby Isaac     const char  *name;
3196efa14ee0SMatthew G Knepley 
31979566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
31989566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
31999566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
32009566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
32019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
32029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
32039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3204efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3205efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3206efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3207efa14ee0SMatthew G Knepley       PetscInt       dof;
3208efa14ee0SMatthew G Knepley 
32099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32109566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
32119566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3212e6ccafaeSMatthew G Knepley     }
32139566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
32149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
32159566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
32169566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
32179566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
32189566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
32199566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3220830e53efSMatthew G. Knepley     if (coordSize) {
32219566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
32229566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3223efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3224efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3225efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3226efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3227e6ccafaeSMatthew G Knepley 
32289566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32299566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
32309566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
32319566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
323263a3b9bcSJacob Faibussowitsch         PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3233e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3234e6ccafaeSMatthew G Knepley       }
32359566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
32369566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
32373b399e24SMatthew G. Knepley     }
32389566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
32399566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3240e6ccafaeSMatthew G Knepley   }
3241efa14ee0SMatthew G Knepley   /* Cleanup */
32429566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
32439566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
32449566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
32459566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
32463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3247e6ccafaeSMatthew G Knepley }
3248e6ccafaeSMatthew G Knepley 
3249d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3250d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
3251d71ae5a4SJacob Faibussowitsch {
32523982b651SMatthew G. Knepley   PetscInt       subPoint;
32533982b651SMatthew G. Knepley   PetscErrorCode ierr;
32543982b651SMatthew G. Knepley 
32559371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
32569371c9d4SSatish Balay   if (ierr) return -1;
32573982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
32583982b651SMatthew G. Knepley }
32593982b651SMatthew G. Knepley 
3260d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3261d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[])
3262d71ae5a4SJacob Faibussowitsch {
3263dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3264dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3265dd05d810SMatthew G. Knepley 
3266dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3267dd05d810SMatthew G. Knepley   if (ierr) return -1;
3268dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3269dd05d810SMatthew G. Knepley }
3270dd05d810SMatthew G. Knepley 
3271d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3272d71ae5a4SJacob Faibussowitsch {
3273695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3274212103e5SMatthew Knepley   PetscInt Nl, l, d;
3275212103e5SMatthew Knepley 
3276212103e5SMatthew Knepley   PetscFunctionBegin;
3277695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3278695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3279695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
32809566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3281212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3282212103e5SMatthew Knepley     DMLabel         label, newlabel;
3283212103e5SMatthew Knepley     const char     *lname;
3284d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3285212103e5SMatthew Knepley     IS              valueIS;
3286212103e5SMatthew Knepley     const PetscInt *values;
3287212103e5SMatthew Knepley     PetscInt        Nv, v;
3288212103e5SMatthew Knepley 
32899566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
32909566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
32919566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
32929566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
32939566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3294d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
32959566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
32969566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
32979566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
32989566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
32999566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
33009566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
33019566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
33029566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3303212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3304212103e5SMatthew Knepley       IS              pointIS;
3305212103e5SMatthew Knepley       const PetscInt *points;
3306212103e5SMatthew Knepley       PetscInt        Np, p;
3307212103e5SMatthew Knepley 
33089566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
33099566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
33109566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3311212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3312212103e5SMatthew Knepley         const PetscInt point = points[p];
3313212103e5SMatthew Knepley         PetscInt       subp;
3314212103e5SMatthew Knepley 
33159566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3316212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
33179566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3318212103e5SMatthew Knepley       }
33199566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
33209566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3321212103e5SMatthew Knepley     }
33229566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
33239566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3324212103e5SMatthew Knepley   }
33253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3326212103e5SMatthew Knepley }
3327212103e5SMatthew Knepley 
3328d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3329d71ae5a4SJacob Faibussowitsch {
333082f516ccSBarry Smith   MPI_Comm         comm;
333153156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3332efa14ee0SMatthew G Knepley   IS              *subpointIS;
3333efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
33343982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3335dd05d810SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, v;
33360d366550SMatthew G. Knepley   PetscMPIInt      rank;
3337e6ccafaeSMatthew G Knepley 
3338e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
33399566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
33409566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3341efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
33429566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
33439566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3344bec263e5SMatthew G. Knepley   if (cellHeight) {
33459566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
33469566063dSJacob Faibussowitsch     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3347bec263e5SMatthew G. Knepley   } else {
3348bec263e5SMatthew G. Knepley     DMLabel         depth;
3349bec263e5SMatthew G. Knepley     IS              pointIS;
3350bec263e5SMatthew G. Knepley     const PetscInt *points;
3351b85c8bf9SMatthew G. Knepley     PetscInt        numPoints = 0;
3352bec263e5SMatthew G. Knepley 
33539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
33549566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3355b85c8bf9SMatthew G. Knepley     if (pointIS) {
33569566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
33579566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3358b85c8bf9SMatthew G. Knepley     }
3359bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3360bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3361bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3362bec263e5SMatthew G. Knepley 
33639566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3364bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
33659566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
33669566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3367bec263e5SMatthew G. Knepley       }
33689566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3369bec263e5SMatthew G. Knepley     }
33709566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
33719566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3372bec263e5SMatthew G. Knepley   }
3373efa14ee0SMatthew G Knepley   /* Setup chart */
33749566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3375dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
33769566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3377e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
33789566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3379e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3380e6ccafaeSMatthew G Knepley   }
3381dd05d810SMatthew G. Knepley   // Determine submesh dimension
3382dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3383dd05d810SMatthew G. Knepley   if (sdim > 0) {
3384dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3385dd05d810SMatthew G. Knepley     sdim = dim;
3386dd05d810SMatthew G. Knepley   } else {
3387dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3388dd05d810SMatthew G. Knepley     PetscInt lsdim;
3389dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3390dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3391a5a714f4SStefano Zampini     PetscCall(MPIU_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPI_MAX, comm));
3392dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3393dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3394dd05d810SMatthew G. Knepley   }
33959566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
33969566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3397e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3398dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3399dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3400dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3401dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3402dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
34039566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
34049566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3405e6ccafaeSMatthew G Knepley   }
3406412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
34079566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3408dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3409e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3410e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3411e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3412e6ccafaeSMatthew G Knepley       const PetscInt *cone;
341315100a53SVaclav Hapla       PetscInt        coneSize;
3414e6ccafaeSMatthew G Knepley 
34159566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3416dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
341715100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
341815100a53SVaclav Hapla 
34199566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3420e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34219566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3422e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3423e6ccafaeSMatthew G Knepley         }
34249566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
34259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
342615100a53SVaclav Hapla       } else {
342715100a53SVaclav Hapla         DMPolytopeType ct;
342815100a53SVaclav Hapla 
342915100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
343015100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
343115100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3432e6ccafaeSMatthew G Knepley       }
3433e6ccafaeSMatthew G Knepley     }
3434e6ccafaeSMatthew G Knepley   }
34359566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
34369566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3437e6ccafaeSMatthew G Knepley   /* Set cones */
34389566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
34399566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3440dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3441e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3442e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3443e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
34440e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
34450d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3446e6ccafaeSMatthew G Knepley 
3447dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
34480d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
34490d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
34500d366550SMatthew G. Knepley 
34519566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
34529566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
34530d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3454064cae4fSPierre Jolivet           PetscBool isHybrid = PETSC_FALSE;
3455412e9a14SMatthew G. Knepley 
34569566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3457412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
34589566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
34590d366550SMatthew G. Knepley           if (subc >= 0) {
34600d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
34610d366550SMatthew G. Knepley 
34629566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
34639566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
34649566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
34650d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
34660d366550SMatthew G. Knepley               if (cone[c] == point) {
34670d366550SMatthew G. Knepley                 fornt = ornt[c];
34680d366550SMatthew G. Knepley                 break;
34690d366550SMatthew G. Knepley               }
34700d366550SMatthew G. Knepley             }
34710d366550SMatthew G. Knepley             break;
34720d366550SMatthew G. Knepley           }
34730d366550SMatthew G. Knepley         }
34740d366550SMatthew G. Knepley       }
34759566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
34769566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
34779566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
34789566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3479e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34809566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
348101a2673eSMatthew G. Knepley         if (subc >= 0) {
348201a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
34833982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
348401a2673eSMatthew G. Knepley           ++coneSizeNew;
348501a2673eSMatthew G. Knepley         }
3486e6ccafaeSMatthew G Knepley       }
348763a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
34889566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
34899566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
34909566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3491e6ccafaeSMatthew G Knepley     }
3492e6ccafaeSMatthew G Knepley   }
34939566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
34949566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
34959566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3496e6ccafaeSMatthew G Knepley   /* Build coordinates */
3497e6ccafaeSMatthew G Knepley   {
3498e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3499e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3500e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3501c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
350224640c55SToby Isaac     const char  *name;
3503e6ccafaeSMatthew G Knepley 
35049566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
35059566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
35069566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
35079566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
35089566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
35099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
35109566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
35119566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0] + numSubPoints[0]));
3512e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3513e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3514e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3515e6ccafaeSMatthew G Knepley       PetscInt       dof;
3516e6ccafaeSMatthew G Knepley 
35179566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
35189566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
35199566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3520e6ccafaeSMatthew G Knepley     }
35219566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
35229566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
35239566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
35249566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
35259566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
35269566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
35279566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
35289566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
35299566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
35309566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3531e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3532e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3533e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3534e6ccafaeSMatthew G Knepley       PetscInt       dof, off, sdof, soff, d;
3535e6ccafaeSMatthew G Knepley 
35369566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
35379566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
35389566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
35399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
354063a3b9bcSJacob Faibussowitsch       PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3541efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3542e6ccafaeSMatthew G Knepley     }
35439566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
35449566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
35459566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
35469566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3547e6ccafaeSMatthew G Knepley   }
35483982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
35493982b651SMatthew G. Knepley   {
35503982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
35513982b651SMatthew G. Knepley     IS                 subpIS;
35523982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
35535033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
35545033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3555dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
35565033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
35575033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
35583982b651SMatthew G. Knepley 
35599566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
35605033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
35619566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
35629566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
35639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
35649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
35659566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
35663982b651SMatthew G. Knepley     if (subpIS) {
3567dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3568dd05d810SMatthew G. Knepley 
35699566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
35709566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3571dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3572dd05d810SMatthew G. Knepley       if (!sorted) {
3573dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3574dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3575dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3576dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3577dd05d810SMatthew G. Knepley       }
35783982b651SMatthew G. Knepley     }
35799566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
35803982b651SMatthew G. Knepley     if (numRoots >= 0) {
35815033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
35825033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
35839566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
35843982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
35853982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
35863982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
35873982b651SMatthew G. Knepley       }
35883982b651SMatthew G. Knepley       /* Set subleaves */
35893982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
35903982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3591dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
35923982b651SMatthew G. Knepley 
35933982b651SMatthew G. Knepley         if (subpoint < 0) continue;
35943982b651SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
35953982b651SMatthew G. Knepley         newLocalPoints[point - pStart].index = subpoint;
35963982b651SMatthew G. Knepley         ++numSubleaves;
35973982b651SMatthew G. Knepley       }
35983982b651SMatthew G. Knepley       /* Must put in owned subpoints */
35993982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
36003982b651SMatthew G. Knepley         newOwners[p - pStart].rank  = -3;
36013982b651SMatthew G. Knepley         newOwners[p - pStart].index = -3;
36023982b651SMatthew G. Knepley       }
3603d9384f27SMatthew G. Knepley       for (p = 0; p < numSubpoints; ++p) {
36045033f954SMatthew G. Knepley         /* Hold on to currently owned points */
36055033f954SMatthew G. Knepley         if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
36065033f954SMatthew G. Knepley         else newOwners[subpoints[p] - pStart].rank = rank;
3607d9384f27SMatthew G. Knepley         newOwners[subpoints[p] - pStart].index = p;
36083982b651SMatthew G. Knepley       }
36099566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
36109566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
36119371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
36129371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
36139566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
36149566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
36159566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
36169566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
36175033f954SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
36183982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3619dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
36203982b651SMatthew G. Knepley 
36213982b651SMatthew G. Knepley         if (subpoint < 0) continue;
36229371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
36239371c9d4SSatish Balay           ++ll;
36249371c9d4SSatish Balay           continue;
36259371c9d4SSatish Balay         }
36263982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
36273982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
36283982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
362963a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
363063a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
36313982b651SMatthew G. Knepley         ++sl;
36323982b651SMatthew G. Knepley       }
36331dca8a05SBarry Smith       PetscCheck(sl + ll == numSubleaves, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %" PetscInt_FMT " + %" PetscInt_FMT " != %" PetscInt_FMT, sl, ll, numSubleaves);
36349566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
36359566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
36363982b651SMatthew G. Knepley     }
363748a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3638dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
36393982b651SMatthew G. Knepley   }
3640212103e5SMatthew Knepley   /* Filter labels */
36419566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3642efa14ee0SMatthew G Knepley   /* Cleanup */
3643dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
36449566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
36459566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3646e6ccafaeSMatthew G Knepley   }
36479566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
36483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3649e6ccafaeSMatthew G Knepley }
3650e6ccafaeSMatthew G Knepley 
3651d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
3652d71ae5a4SJacob Faibussowitsch {
36533982b651SMatthew G. Knepley   PetscFunctionBegin;
36549566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm));
36553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36563982b651SMatthew G. Knepley }
36573982b651SMatthew G. Knepley 
3658d0fa310fSMatthew G. Knepley /*@
3659e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3660e6ccafaeSMatthew G Knepley 
3661e6ccafaeSMatthew G Knepley   Input Parameters:
3662e6ccafaeSMatthew G Knepley + dm          - The original mesh
366320f4b53cSBarry Smith . vertexLabel - The `DMLabel` marking points contained in the surface
3664158acfadSMatthew G. Knepley . value       - The label value to use
366520f4b53cSBarry Smith - markedFaces - `PETSC_TRUE` if surface faces are marked in addition to vertices, `PETSC_FALSE` if only vertices are marked
3666e6ccafaeSMatthew G Knepley 
3667e6ccafaeSMatthew G Knepley   Output Parameter:
3668e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3669e6ccafaeSMatthew G Knepley 
3670e6ccafaeSMatthew G Knepley   Level: developer
3671e6ccafaeSMatthew G Knepley 
367220f4b53cSBarry Smith   Note:
367320f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
367420f4b53cSBarry Smith 
36751cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3676830e53efSMatthew G. Knepley @*/
3677d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3678d71ae5a4SJacob Faibussowitsch {
3679827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3680827c4036SVaclav Hapla   PetscInt               dim, cdim;
3681e6ccafaeSMatthew G Knepley 
3682e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3683e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36844f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
36859566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
36869566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
36879566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
36889566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
36899566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
36909566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
36919566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
369208401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3693827c4036SVaclav Hapla   if (interpolated) {
36949566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3695e6ccafaeSMatthew G Knepley   } else {
36969566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3697e6ccafaeSMatthew G Knepley   }
36985de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
36993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3700e6ccafaeSMatthew G Knepley }
3701e6ccafaeSMatthew G Knepley 
3702d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3703d71ae5a4SJacob Faibussowitsch {
3704766ab985SMatthew G. Knepley   MPI_Comm        comm;
3705766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3706766ab985SMatthew G. Knepley   IS              subvertexIS;
3707766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3708fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3709766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3710412e9a14SMatthew G. Knepley   PetscInt        c, f;
3711766ab985SMatthew G. Knepley 
3712766ab985SMatthew G. Knepley   PetscFunctionBegin;
37139566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3714766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
37159566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
37169566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
37179566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
37189566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3719766ab985SMatthew G. Knepley   /* Setup chart */
37209566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
37219566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
37229566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
37239566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3724766ab985SMatthew G. Knepley   /* Set cone sizes */
3725766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3726766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3727766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
37289566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
37299566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
373048a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
373148a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
37329566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3733766ab985SMatthew G. Knepley   /* Create face cones */
37349566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
37359566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3736766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3737766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3738766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
373987feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3740064cae4fSPierre Jolivet     PetscBool       isHybrid = PETSC_FALSE;
374187feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3742766ab985SMatthew G. Knepley 
37439566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3744412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
37459566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
374687feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
37479566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
374887feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
374987feddfdSMatthew G. Knepley     }
37509566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
37519566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
37529566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
375327234c99SMatthew G. Knepley     /* Not true in parallel
375408401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
375587feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
375687feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3757064cae4fSPierre Jolivet       PetscBool isHybrid = PETSC_FALSE;
3758766ab985SMatthew G. Knepley 
37599566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3760412e9a14SMatthew G. Knepley       if (isHybrid) continue;
376187feddfdSMatthew G. Knepley       /* I know this is a crap search */
376287feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
376387feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3764766ab985SMatthew G. Knepley       }
376563a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
37669566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3767766ab985SMatthew G. Knepley     }
37689566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
376987feddfdSMatthew G. Knepley     ++newFacePoint;
3770766ab985SMatthew G. Knepley   }
37719566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
37729566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
37739566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3774766ab985SMatthew G. Knepley   /* Build coordinates */
3775766ab985SMatthew G. Knepley   {
3776766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3777766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3778766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3779c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
378024640c55SToby Isaac     const char  *name;
3781766ab985SMatthew G. Knepley 
37829566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
37839566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
37849566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
37859566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
37869566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
37879566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
37889566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
37899566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3790766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3791766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3792766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3793766ab985SMatthew G. Knepley       PetscInt       dof;
3794766ab985SMatthew G. Knepley 
37959566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
37969566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
37979566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3798766ab985SMatthew G. Knepley     }
37999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
38009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
38019566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
38029566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
38039566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
38049566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
38059566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
38069566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
38079566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
38089566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3809766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3810766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3811766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3812766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3813766ab985SMatthew G. Knepley 
38149566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
38159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
38169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
38179566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
381863a3b9bcSJacob Faibussowitsch       PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3819766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3820766ab985SMatthew G. Knepley     }
38219566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
38229566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
38239566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
38249566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3825766ab985SMatthew G. Knepley   }
3826aca35d17SMatthew G. Knepley   /* Build SF */
3827aca35d17SMatthew G. Knepley   CHKMEMQ;
3828aca35d17SMatthew G. Knepley   {
3829aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3830aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3831bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3832aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3833bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
383449c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3835bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3836aca35d17SMatthew G. Knepley 
38379566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
38389566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
38399566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
38409566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
38419566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
38429566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
3843aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3844aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
38459566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
3846bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
3847bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3848bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3849bdcf2095SMatthew G. Knepley       }
38509e0823b2SMatthew G. Knepley       /* Set subleaves */
3851aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3852aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3853bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3854aca35d17SMatthew G. Knepley 
385563a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
3856bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3857bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
3858bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
3859bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3860aca35d17SMatthew G. Knepley       }
38619e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
38629e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
38639e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
38649e0823b2SMatthew G. Knepley 
38659e0823b2SMatthew G. Knepley         if (subPoint < 0) {
38669e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
38679e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
38689e0823b2SMatthew G. Knepley         } else {
38699e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
38709e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
38719e0823b2SMatthew G. Knepley         }
3872bdcf2095SMatthew G. Knepley       }
38739566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
38749566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
38759566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
38769566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
38779566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
38789566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
387949c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3880bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3881bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3882aca35d17SMatthew G. Knepley 
3883aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
38849371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
38859371c9d4SSatish Balay           ++ll;
38869371c9d4SSatish Balay           continue;
38879371c9d4SSatish Balay         }
3888aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3889bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3890bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
389163a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
389263a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
3893aca35d17SMatthew G. Knepley         ++sl;
3894aca35d17SMatthew G. Knepley       }
38959566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
38961dca8a05SBarry Smith       PetscCheck(sl + ll == numSubLeaves, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %" PetscInt_FMT " + %" PetscInt_FMT " != %" PetscInt_FMT, sl, ll, numSubLeaves);
38979566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
3898aca35d17SMatthew G. Knepley     }
3899aca35d17SMatthew G. Knepley   }
3900aca35d17SMatthew G. Knepley   CHKMEMQ;
3901766ab985SMatthew G. Knepley   /* Cleanup */
39029566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
39039566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
39049566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
39053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3906766ab985SMatthew G. Knepley }
3907766ab985SMatthew G. Knepley 
3908d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3909d71ae5a4SJacob Faibussowitsch {
39103982b651SMatthew G. Knepley   DMLabel label = NULL;
3911766ab985SMatthew G. Knepley 
3912766ab985SMatthew G. Knepley   PetscFunctionBegin;
39139566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
39149566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm));
39153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3916766ab985SMatthew G. Knepley }
3917766ab985SMatthew G. Knepley 
3918c08575a3SMatthew G. Knepley /*@C
391920f4b53cSBarry Smith   DMPlexCreateCohesiveSubmesh - Extract from a mesh with cohesive cells the hypersurface defined by one face of the cells. Optionally, a label can be given to restrict the cells.
3920766ab985SMatthew G. Knepley 
3921766ab985SMatthew G. Knepley   Input Parameters:
3922766ab985SMatthew G. Knepley + dm          - The original mesh
392327c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
392420f4b53cSBarry Smith . label       - A label name, or `NULL`
392527c04023SMatthew G. Knepley - value       - A label value
3926766ab985SMatthew G. Knepley 
3927766ab985SMatthew G. Knepley   Output Parameter:
3928766ab985SMatthew G. Knepley . subdm - The surface mesh
3929766ab985SMatthew G. Knepley 
3930766ab985SMatthew G. Knepley   Level: developer
3931766ab985SMatthew G. Knepley 
393220f4b53cSBarry Smith   Note:
393320f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
393420f4b53cSBarry Smith 
39351cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
3936c08575a3SMatthew G. Knepley @*/
3937d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3938d71ae5a4SJacob Faibussowitsch {
3939c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
3940766ab985SMatthew G. Knepley 
3941766ab985SMatthew G. Knepley   PetscFunctionBegin;
3942766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39434f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
39449566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
39459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
39469566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
39479566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
39489566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
39499566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
39509566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
3951766ab985SMatthew G. Knepley   if (depth == dim) {
39529566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
3953766ab985SMatthew G. Knepley   } else {
39549566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
3955e6ccafaeSMatthew G Knepley   }
39565de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
39573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3958e6ccafaeSMatthew G Knepley }
3959e6ccafaeSMatthew G Knepley 
3960bec263e5SMatthew G. Knepley /*@
39610e18dc48SMatthew G. Knepley   DMPlexReorderCohesiveSupports - Ensure that face supports for cohesive end caps are ordered
39620e18dc48SMatthew G. Knepley 
39630e18dc48SMatthew G. Knepley   Not Collective
39640e18dc48SMatthew G. Knepley 
39650e18dc48SMatthew G. Knepley   Input Parameter:
39660e18dc48SMatthew G. Knepley . dm - The `DM` containing cohesive cells
39670e18dc48SMatthew G. Knepley 
39680e18dc48SMatthew G. Knepley   Level: developer
39690e18dc48SMatthew G. Knepley 
3970a4e35b19SJacob Faibussowitsch   Note:
3971a4e35b19SJacob Faibussowitsch   For the negative size (first) face, the cohesive cell should be first in the support, and for
3972a4e35b19SJacob Faibussowitsch   the positive side (second) face, the cohesive cell should be second in the support.
39730e18dc48SMatthew G. Knepley 
39740e18dc48SMatthew G. Knepley .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexCreateCohesiveSubmesh()`
39750e18dc48SMatthew G. Knepley @*/
39760e18dc48SMatthew G. Knepley PetscErrorCode DMPlexReorderCohesiveSupports(DM dm)
39770e18dc48SMatthew G. Knepley {
39780e18dc48SMatthew G. Knepley   PetscInt dim, cStart, cEnd;
39790e18dc48SMatthew G. Knepley 
39800e18dc48SMatthew G. Knepley   PetscFunctionBegin;
39810e18dc48SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
39820e18dc48SMatthew G. Knepley   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cStart, &cEnd));
39830e18dc48SMatthew G. Knepley   for (PetscInt c = cStart; c < cEnd; ++c) {
39840e18dc48SMatthew G. Knepley     const PetscInt *cone;
39850e18dc48SMatthew G. Knepley     PetscInt        coneSize;
39860e18dc48SMatthew G. Knepley 
39870e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
39880e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetCone(dm, c, &cone));
39890e18dc48SMatthew G. Knepley     PetscCheck(coneSize >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid cell %" PetscInt_FMT " cone size %" PetscInt_FMT " must be >= 2", c, coneSize);
39900e18dc48SMatthew G. Knepley     for (PetscInt s = 0; s < 2; ++s) {
39910e18dc48SMatthew G. Knepley       const PetscInt *supp;
39920e18dc48SMatthew G. Knepley       PetscInt        suppSize, newsupp[2];
39930e18dc48SMatthew G. Knepley 
39940e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupportSize(dm, cone[s], &suppSize));
39950e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupport(dm, cone[s], &supp));
39960e18dc48SMatthew G. Knepley       if (suppSize == 2) {
39970e18dc48SMatthew G. Knepley         /* Reorder hybrid end cap support */
39980e18dc48SMatthew G. Knepley         if (supp[s] == c) {
39990e18dc48SMatthew G. Knepley           newsupp[0] = supp[1];
40000e18dc48SMatthew G. Knepley           newsupp[1] = supp[0];
40010e18dc48SMatthew G. Knepley         } else {
40020e18dc48SMatthew G. Knepley           newsupp[0] = supp[0];
40030e18dc48SMatthew G. Knepley           newsupp[1] = supp[1];
40040e18dc48SMatthew G. Knepley         }
40050e18dc48SMatthew G. Knepley         PetscCheck(newsupp[1 - s] == c, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid end cap %" PetscInt_FMT " support entry %" PetscInt_FMT " != %" PetscInt_FMT " hybrid cell", cone[s], newsupp[1 - s], c);
40060e18dc48SMatthew G. Knepley         PetscCall(DMPlexSetSupport(dm, cone[s], newsupp));
40070e18dc48SMatthew G. Knepley       }
40080e18dc48SMatthew G. Knepley     }
40090e18dc48SMatthew G. Knepley   }
40100e18dc48SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
40110e18dc48SMatthew G. Knepley }
40120e18dc48SMatthew G. Knepley 
40130e18dc48SMatthew G. Knepley /*@
4014bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
4015bec263e5SMatthew G. Knepley 
4016bec263e5SMatthew G. Knepley   Input Parameters:
4017bec263e5SMatthew G. Knepley + dm        - The original mesh
401820f4b53cSBarry Smith . cellLabel - The `DMLabel` marking cells contained in the new mesh
4019bec263e5SMatthew G. Knepley - value     - The label value to use
4020bec263e5SMatthew G. Knepley 
4021bec263e5SMatthew G. Knepley   Output Parameter:
4022bec263e5SMatthew G. Knepley . subdm - The new mesh
4023bec263e5SMatthew G. Knepley 
4024bec263e5SMatthew G. Knepley   Level: developer
4025bec263e5SMatthew G. Knepley 
402620f4b53cSBarry Smith   Note:
402720f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
402820f4b53cSBarry Smith 
40291cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
4030bec263e5SMatthew G. Knepley @*/
4031d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
4032d71ae5a4SJacob Faibussowitsch {
4033b47cd10cSMatthew G. Knepley   PetscInt dim, overlap;
4034bec263e5SMatthew G. Knepley 
4035bec263e5SMatthew G. Knepley   PetscFunctionBegin;
4036bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40374f572ea9SToby Isaac   PetscAssertPointer(subdm, 4);
40389566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
40399566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
40409566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
4041bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
40429566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm));
40435de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
4044b47cd10cSMatthew G. Knepley   // It is possible to obtain a surface mesh where some faces are in SF
4045b47cd10cSMatthew G. Knepley   //   We should either mark the mesh as having an overlap, or delete these from the SF
4046b47cd10cSMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
4047b47cd10cSMatthew G. Knepley   if (!overlap) {
4048b47cd10cSMatthew G. Knepley     PetscSF         sf;
4049b47cd10cSMatthew G. Knepley     const PetscInt *leaves;
4050b47cd10cSMatthew G. Knepley     PetscInt        cStart, cEnd, Nl;
4051b47cd10cSMatthew G. Knepley     PetscBool       hasSubcell = PETSC_FALSE, ghasSubcell;
4052b47cd10cSMatthew G. Knepley 
4053b47cd10cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd));
4054b47cd10cSMatthew G. Knepley     PetscCall(DMGetPointSF(*subdm, &sf));
4055b47cd10cSMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
4056b47cd10cSMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
4057b47cd10cSMatthew G. Knepley       const PetscInt point = leaves ? leaves[l] : l;
4058b47cd10cSMatthew G. Knepley 
4059b47cd10cSMatthew G. Knepley       if (point >= cStart && point < cEnd) {
4060b47cd10cSMatthew G. Knepley         hasSubcell = PETSC_TRUE;
4061b47cd10cSMatthew G. Knepley         break;
4062b47cd10cSMatthew G. Knepley       }
4063b47cd10cSMatthew G. Knepley     }
4064b47cd10cSMatthew G. Knepley     PetscCall(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
4065b47cd10cSMatthew G. Knepley     if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1));
4066b47cd10cSMatthew G. Knepley   }
40673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4068bec263e5SMatthew G. Knepley }
4069bec263e5SMatthew G. Knepley 
407064beef6dSMatthew G. Knepley /*@
407120f4b53cSBarry Smith   DMPlexGetSubpointMap - Returns a `DMLabel` with point dimension as values
407264beef6dSMatthew G. Knepley 
407364beef6dSMatthew G. Knepley   Input Parameter:
407420f4b53cSBarry Smith . dm - The submesh `DM`
407564beef6dSMatthew G. Knepley 
407664beef6dSMatthew G. Knepley   Output Parameter:
407720f4b53cSBarry Smith . subpointMap - The `DMLabel` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
407864beef6dSMatthew G. Knepley 
407964beef6dSMatthew G. Knepley   Level: developer
408064beef6dSMatthew G. Knepley 
40811cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
408264beef6dSMatthew G. Knepley @*/
4083d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
4084d71ae5a4SJacob Faibussowitsch {
4085e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4086e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40874f572ea9SToby Isaac   PetscAssertPointer(subpointMap, 2);
408865663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
40893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4090e6ccafaeSMatthew G Knepley }
4091e6ccafaeSMatthew G Knepley 
4092c08575a3SMatthew G. Knepley /*@
409320f4b53cSBarry Smith   DMPlexSetSubpointMap - Sets the `DMLabel` with point dimension as values
4094c08575a3SMatthew G. Knepley 
4095c08575a3SMatthew G. Knepley   Input Parameters:
409620f4b53cSBarry Smith + dm          - The submesh `DM`
409720f4b53cSBarry Smith - subpointMap - The `DMLabel` of all the points from the original mesh in this submesh
4098c08575a3SMatthew G. Knepley 
4099c08575a3SMatthew G. Knepley   Level: developer
4100c08575a3SMatthew G. Knepley 
410120f4b53cSBarry Smith   Note:
410220f4b53cSBarry Smith   Should normally not be called by the user, since it is set in `DMPlexCreateSubmesh()`
410320f4b53cSBarry Smith 
41041cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
4105c08575a3SMatthew G. Knepley @*/
4106d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
4107d71ae5a4SJacob Faibussowitsch {
4108e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
4109285d324eSMatthew G. Knepley   DMLabel  tmp;
4110e6ccafaeSMatthew G Knepley 
4111e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4112e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4113285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
4114e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
41159566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
41169566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
41173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4118e6ccafaeSMatthew G Knepley }
4119e6ccafaeSMatthew G Knepley 
4120d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
4121d71ae5a4SJacob Faibussowitsch {
412297d8846cSMatthew Knepley   DMLabel  spmap;
412397d8846cSMatthew Knepley   PetscInt depth, d;
412497d8846cSMatthew Knepley 
412597d8846cSMatthew Knepley   PetscFunctionBegin;
41269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
41279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
412897d8846cSMatthew Knepley   if (spmap && depth >= 0) {
412997d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
413097d8846cSMatthew Knepley     PetscInt *points, *depths;
413197d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
413297d8846cSMatthew Knepley 
41339566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
413463a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
41359566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
41369566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
413797d8846cSMatthew Knepley     depths[0] = depth;
413897d8846cSMatthew Knepley     depths[1] = 0;
4139ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
414097d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
414197d8846cSMatthew Knepley       const PetscInt dep = depths[d];
414297d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
414397d8846cSMatthew Knepley 
41449566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
41459566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
414697d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
414763a3b9bcSJacob Faibussowitsch         PetscCheck(n == depEnd - depStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %" PetscInt_FMT " at depth %" PetscInt_FMT " should be %" PetscInt_FMT, n, dep, depEnd - depStart);
414897d8846cSMatthew Knepley       } else {
414997d8846cSMatthew Knepley         if (!n) {
415097d8846cSMatthew Knepley           if (d == 0) {
415197d8846cSMatthew Knepley             /* Missing cells */
415297d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
415397d8846cSMatthew Knepley           } else {
415497d8846cSMatthew Knepley             /* Missing faces */
415597d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
415697d8846cSMatthew Knepley           }
415797d8846cSMatthew Knepley         }
415897d8846cSMatthew Knepley       }
415997d8846cSMatthew Knepley       if (n) {
416097d8846cSMatthew Knepley         IS              is;
416197d8846cSMatthew Knepley         const PetscInt *opoints;
416297d8846cSMatthew Knepley 
41639566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
41649566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
416597d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
41669566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
41679566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
416897d8846cSMatthew Knepley       }
416997d8846cSMatthew Knepley     }
41709566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
417163a3b9bcSJacob Faibussowitsch     PetscCheck(off == pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %" PetscInt_FMT " should be %" PetscInt_FMT, off, pEnd);
41729566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
41739566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
417497d8846cSMatthew Knepley   }
41753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
417697d8846cSMatthew Knepley }
417797d8846cSMatthew Knepley 
417864beef6dSMatthew G. Knepley /*@
417920f4b53cSBarry Smith   DMPlexGetSubpointIS - Returns an `IS` covering the entire subdm chart with the original points as data
4180e6ccafaeSMatthew G Knepley 
4181e6ccafaeSMatthew G Knepley   Input Parameter:
418220f4b53cSBarry Smith . dm - The submesh `DM`
4183e6ccafaeSMatthew G Knepley 
4184e6ccafaeSMatthew G Knepley   Output Parameter:
418520f4b53cSBarry Smith . subpointIS - The `IS` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
418664beef6dSMatthew G. Knepley 
418764beef6dSMatthew G. Knepley   Level: developer
418864beef6dSMatthew G. Knepley 
418920f4b53cSBarry Smith   Note:
419020f4b53cSBarry Smith   This `IS` is guaranteed to be sorted by the construction of the submesh
419120f4b53cSBarry Smith 
41921cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
419364beef6dSMatthew G. Knepley @*/
4194d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
4195d71ae5a4SJacob Faibussowitsch {
419697d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
419797d8846cSMatthew Knepley   DMLabel          spmap;
419897d8846cSMatthew Knepley   PetscObjectState state;
4199e6ccafaeSMatthew G Knepley 
4200e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4201e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42024f572ea9SToby Isaac   PetscAssertPointer(subpointIS, 2);
42039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
42049566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
42059566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
420697d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
42073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4208e6ccafaeSMatthew G Knepley }
4209559a1558SMatthew G. Knepley 
4210559a1558SMatthew G. Knepley /*@
421120f4b53cSBarry Smith   DMGetEnclosureRelation - Get the relationship between `dmA` and `dmB`
4212559a1558SMatthew G. Knepley 
4213559a1558SMatthew G. Knepley   Input Parameters:
421420f4b53cSBarry Smith + dmA - The first `DM`
421520f4b53cSBarry Smith - dmB - The second `DM`
4216559a1558SMatthew G. Knepley 
4217559a1558SMatthew G. Knepley   Output Parameter:
421820f4b53cSBarry Smith . rel - The relation of `dmA` to `dmB`
4219559a1558SMatthew G. Knepley 
4220a6e0b375SMatthew G. Knepley   Level: intermediate
4221559a1558SMatthew G. Knepley 
42221cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosurePoint()`
4223559a1558SMatthew G. Knepley @*/
4224d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4225d71ae5a4SJacob Faibussowitsch {
4226a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
4227559a1558SMatthew G. Knepley   DMLabel  spmap;
4228a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4229559a1558SMatthew G. Knepley 
423044171101SMatthew G. Knepley   PetscFunctionBegin;
42314f572ea9SToby Isaac   PetscAssertPointer(rel, 3);
4232a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
42333ba16761SJacob Faibussowitsch   if (!dmA || !dmB) PetscFunctionReturn(PETSC_SUCCESS);
4234a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4235064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
42369371c9d4SSatish Balay   if (dmA == dmB) {
42379371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
42383ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
42399371c9d4SSatish Balay   }
42409566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
42419566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
42429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
42439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4244a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4245a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4246a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
4247a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4248a6e0b375SMatthew G. Knepley     goto end;
4249559a1558SMatthew G. Knepley   }
4250a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4251a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4252a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4253a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
42549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4255a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4256a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4257a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4258a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4259a6e0b375SMatthew G. Knepley   } else {
4260a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4261a6e0b375SMatthew G. Knepley   }
4262a6e0b375SMatthew G. Knepley end:
42639566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
42649566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
42653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4266559a1558SMatthew G. Knepley }
426744171101SMatthew G. Knepley 
426844171101SMatthew G. Knepley /*@
426920f4b53cSBarry Smith   DMGetEnclosurePoint - Get the point `pA` in `dmA` which corresponds to the point `pB` in `dmB`
427044171101SMatthew G. Knepley 
427144171101SMatthew G. Knepley   Input Parameters:
427220f4b53cSBarry Smith + dmA   - The first `DM`
427320f4b53cSBarry Smith . dmB   - The second `DM`
427420f4b53cSBarry Smith . etype - The type of enclosure relation that `dmA` has to `dmB`
427520f4b53cSBarry Smith - pB    - A point of `dmB`
427644171101SMatthew G. Knepley 
427744171101SMatthew G. Knepley   Output Parameter:
427820f4b53cSBarry Smith . pA - The corresponding point of `dmA`
427944171101SMatthew G. Knepley 
4280a6e0b375SMatthew G. Knepley   Level: intermediate
428144171101SMatthew G. Knepley 
42821cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosureRelation()`
428344171101SMatthew G. Knepley @*/
4284d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
4285d71ae5a4SJacob Faibussowitsch {
4286a6e0b375SMatthew G. Knepley   DM              sdm;
4287a6e0b375SMatthew G. Knepley   IS              subpointIS;
4288a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4289a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
429044171101SMatthew G. Knepley 
429144171101SMatthew G. Knepley   PetscFunctionBegin;
4292a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4293a6e0b375SMatthew G. Knepley   switch (etype) {
4294a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4295a6e0b375SMatthew G. Knepley     sdm = dmB;
42969566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
42979566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4298a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
42999566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4300a6e0b375SMatthew G. Knepley     break;
4301a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4302a6e0b375SMatthew G. Knepley     sdm = dmA;
43039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
43049566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
43059566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
43069566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4307a6e0b375SMatthew G. Knepley     if (*pA < 0) {
43089566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
43099566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
431063a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4311a6e0b375SMatthew G. Knepley     }
43129566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4313a6e0b375SMatthew G. Knepley     break;
4314a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
4315d71ae5a4SJacob Faibussowitsch   case DM_ENC_NONE:
4316d71ae5a4SJacob Faibussowitsch     *pA = pB;
4317d71ae5a4SJacob Faibussowitsch     break;
43189371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4319a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
432044171101SMatthew G. Knepley 
43219566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
43229566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
43239371c9d4SSatish Balay   } break;
4324d71ae5a4SJacob Faibussowitsch   default:
4325d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
432644171101SMatthew G. Knepley   }
43273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
432844171101SMatthew G. Knepley }
4329