xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 605a06ccce2b6060581a2b5350eea15cf005ca7e)
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 
5069941377SStefano Zampini PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label, PetscBool missing_only)
51d71ae5a4SJacob Faibussowitsch {
5269941377SStefano Zampini   PetscInt           depth, pStart, pEnd, fStart, fEnd, f, supportSize, nroots = -1, nleaves = -1, defval;
53429fa399SMatthew G. Knepley   PetscSF            sf;
5441e3e744Sksagiyam   const PetscSFNode *iremote = NULL;
5541e3e744Sksagiyam   const PetscInt    *ilocal  = NULL;
5641e3e744Sksagiyam   PetscInt          *leafData;
5730560a7bSMatthew G. Knepley 
5830560a7bSMatthew G. Knepley   PetscFunctionBegin;
5941e3e744Sksagiyam   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
6041e3e744Sksagiyam   PetscCall(DMPlexGetDepth(dm, &depth));
6141e3e744Sksagiyam   if (depth >= cellHeight + 1) {
629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, cellHeight + 1, &fStart, &fEnd));
6341e3e744Sksagiyam   } else {
6441e3e744Sksagiyam     /* Note DMPlexGetHeightStratum() returns fStart, fEnd = pStart, pEnd */
6541e3e744Sksagiyam     /* if height > depth, which is not what we want here.                */
6641e3e744Sksagiyam     fStart = 0;
6741e3e744Sksagiyam     fEnd   = 0;
6841e3e744Sksagiyam   }
6969941377SStefano Zampini   PetscCall(DMLabelGetDefaultValue(label, &defval));
7041e3e744Sksagiyam   PetscCall(PetscCalloc1(pEnd - pStart, &leafData));
718e3a54c0SPierre Jolivet   leafData = PetscSafePointerPlusOffset(leafData, -pStart);
7241e3e744Sksagiyam   PetscCall(DMGetPointSF(dm, &sf));
7341e3e744Sksagiyam   if (sf) PetscCall(PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, &iremote));
7441e3e744Sksagiyam   if (sf && nroots >= 0) {
7541e3e744Sksagiyam     PetscInt        cStart, cEnd, c, i;
7641e3e744Sksagiyam     PetscInt       *rootData, *rootData1, *cellOwners, hasTwoSupportCells = -2;
7741e3e744Sksagiyam     const PetscInt *support;
7841e3e744Sksagiyam     PetscMPIInt     rank;
7930560a7bSMatthew G. Knepley 
8041e3e744Sksagiyam     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8141e3e744Sksagiyam     PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8241e3e744Sksagiyam     PetscCall(PetscCalloc3(pEnd - pStart, &rootData, pEnd - pStart, &rootData1, cEnd - cStart, &cellOwners));
8341e3e744Sksagiyam     rootData -= pStart;
8441e3e744Sksagiyam     rootData1 -= pStart;
85835f2295SStefano Zampini     for (c = cStart; c < cEnd; ++c) cellOwners[c - cStart] = rank;
8641e3e744Sksagiyam     for (i = 0; i < nleaves; ++i) {
8741e3e744Sksagiyam       c = ilocal ? ilocal[i] : i;
8841e3e744Sksagiyam       if (c >= cStart && c < cEnd) cellOwners[c - cStart] = iremote[i].rank;
8941e3e744Sksagiyam     }
9041e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
92e752be1aSMatthew G. Knepley       if (supportSize == 1) {
9341e3e744Sksagiyam         PetscCall(DMPlexGetSupport(dm, f, &support));
9441e3e744Sksagiyam         leafData[f] = cellOwners[support[0] - cStart];
9541e3e744Sksagiyam       } else {
9641e3e744Sksagiyam         /* TODO: When using DMForest, we could have a parent facet (a coarse facet)     */
9741e3e744Sksagiyam         /*       supportSize of which alone does not tell us if it is an interior       */
9841e3e744Sksagiyam         /*       facet or an exterior facet. Those facets can be identified by checking */
9941e3e744Sksagiyam         /*       if they are in the parent tree. We should probably skip those parent   */
10041e3e744Sksagiyam         /*       facets here, which will allow for including the following check, and   */
10141e3e744Sksagiyam         /*       see if they are exterior facets or not afterwards by checking if the   */
10241e3e744Sksagiyam         /*       children are exterior or not.                                          */
10341e3e744Sksagiyam         /* PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", supportSize, f); */
10441e3e744Sksagiyam         leafData[f] = hasTwoSupportCells; /* some negative PetscInt */
105429fa399SMatthew G. Knepley       }
10641e3e744Sksagiyam       rootData[f]  = leafData[f];
10741e3e744Sksagiyam       rootData1[f] = leafData[f];
10841e3e744Sksagiyam     }
10941e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData, MPI_MIN));
11041e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
11141e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData, MPI_MIN));
11241e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
11341e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
11441e3e744Sksagiyam       /* Store global support size of f.                                                    */
11541e3e744Sksagiyam       /* Facet f is an interior facet if and only if one of the following two is satisfied: */
11641e3e744Sksagiyam       /* 1. supportSize is 2 on some rank.                                                  */
11741e3e744Sksagiyam       /* 2. supportSize is 1 on any rank that can see f, but f is on a partition boundary;  */
11841e3e744Sksagiyam       /*    i.e., rootData[f] < rootData1[f].                                               */
11941e3e744Sksagiyam       rootData[f] = (rootData[f] == hasTwoSupportCells || (rootData[f] < rootData1[f])) ? 2 : 1;
12041e3e744Sksagiyam       leafData[f] = rootData[f];
12141e3e744Sksagiyam     }
12241e3e744Sksagiyam     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
12341e3e744Sksagiyam     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
12441e3e744Sksagiyam     rootData += pStart;
12541e3e744Sksagiyam     rootData1 += pStart;
12641e3e744Sksagiyam     PetscCall(PetscFree3(rootData, rootData1, cellOwners));
12741e3e744Sksagiyam   } else {
12841e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
12941e3e744Sksagiyam       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
13041e3e744Sksagiyam       leafData[f] = supportSize;
13141e3e744Sksagiyam     }
13241e3e744Sksagiyam   }
13341e3e744Sksagiyam   for (f = fStart; f < fEnd; ++f) {
13441e3e744Sksagiyam     if (leafData[f] == 1) {
135e752be1aSMatthew G. Knepley       if (val < 0) {
136e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
137e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
138e752be1aSMatthew G. Knepley 
13969941377SStefano Zampini         PetscAssert(!missing_only, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented");
1409566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
141e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize * 2; cl += 2) {
1429566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, closure[cl], &cval));
143e752be1aSMatthew G. Knepley           if (cval < 0) continue;
1449566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, cval));
145e752be1aSMatthew G. Knepley           break;
146e752be1aSMatthew G. Knepley         }
1479566063dSJacob Faibussowitsch         if (cl == clSize * 2) PetscCall(DMLabelSetValue(label, f, 1));
1489566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
149e752be1aSMatthew G. Knepley       } else {
15069941377SStefano Zampini         if (missing_only) {
15169941377SStefano Zampini           PetscInt fval;
15269941377SStefano Zampini           PetscCall(DMLabelGetValue(label, f, &fval));
15369941377SStefano Zampini           if (fval != defval) PetscCall(DMLabelClearValue(label, f, fval));
15469941377SStefano Zampini           else PetscCall(DMLabelSetValue(label, f, val));
15569941377SStefano Zampini         } else {
1569566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, val));
157e752be1aSMatthew G. Knepley         }
15869941377SStefano Zampini       }
15941e3e744Sksagiyam     } else {
16041e3e744Sksagiyam       /* TODO: See the above comment on DMForest */
16141e3e744Sksagiyam       /* PetscCheck(leafData[f] == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", leafData[f], f); */
162e752be1aSMatthew G. Knepley     }
16330560a7bSMatthew G. Knepley   }
1648e3a54c0SPierre Jolivet   leafData = PetscSafePointerPlusOffset(leafData, pStart);
16541e3e744Sksagiyam   PetscCall(PetscFree(leafData));
1663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16730560a7bSMatthew G. Knepley }
16830560a7bSMatthew G. Knepley 
169cd0c2139SMatthew G Knepley /*@
170cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
171cd0c2139SMatthew G Knepley 
17241e3e744Sksagiyam   Collective
173cd0c2139SMatthew G Knepley 
174d8d19677SJose E. Roman   Input Parameters:
175429fa399SMatthew G. Knepley + dm  - The original `DM`
17620f4b53cSBarry Smith - val - The marker value, or `PETSC_DETERMINE` to use some value in the closure (or 1 if none are found)
177cd0c2139SMatthew G Knepley 
178cd0c2139SMatthew G Knepley   Output Parameter:
17920f4b53cSBarry Smith . label - The `DMLabel` marking boundary faces with the given value
180429fa399SMatthew G. Knepley 
181cd0c2139SMatthew G Knepley   Level: developer
182cd0c2139SMatthew G Knepley 
18320f4b53cSBarry Smith   Note:
18441e3e744Sksagiyam   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.
18541e3e744Sksagiyam 
18641e3e744Sksagiyam   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.
18720f4b53cSBarry Smith 
1881cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLabelCreate()`, `DMCreateLabel()`
18909f723d9SJed Brown @*/
190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
191d71ae5a4SJacob Faibussowitsch {
192bbb57e68SMatthew G. Knepley   DM                     plex;
193827c4036SVaclav Hapla   DMPlexInterpolatedFlag flg;
194cd0c2139SMatthew G Knepley 
195cd0c2139SMatthew G Knepley   PetscFunctionBegin;
196827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
197bbb57e68SMatthew G. Knepley   PetscCall(DMConvert(dm, DMPLEX, &plex));
198bbb57e68SMatthew G. Knepley   PetscCall(DMPlexIsInterpolated(plex, &flg));
19908401ef6SPierre Jolivet   PetscCheck(flg == DMPLEX_INTERPOLATED_FULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
200bbb57e68SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces_Internal(plex, val, 0, label, PETSC_FALSE));
201bbb57e68SMatthew G. Knepley   PetscCall(DMDestroy(&plex));
2023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
203cd0c2139SMatthew G Knepley }
204cd0c2139SMatthew G Knepley 
205d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
206d71ae5a4SJacob Faibussowitsch {
207b0bf5782SToby Isaac   IS              valueIS;
208ac51f24eSSander Arens   PetscSF         sfPoint;
209b0bf5782SToby Isaac   const PetscInt *values;
210ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
211b0bf5782SToby Isaac 
212b0bf5782SToby Isaac   PetscFunctionBegin;
2139566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2149566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2169566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
217b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
218b0bf5782SToby Isaac     IS              pointIS;
219b0bf5782SToby Isaac     const PetscInt *points;
220b0bf5782SToby Isaac     PetscInt        numPoints, p;
221b0bf5782SToby Isaac 
2229566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2239566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2249566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
225b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
226b0bf5782SToby Isaac       PetscInt  q       = points[p];
227b0bf5782SToby Isaac       PetscInt *closure = NULL;
228b0bf5782SToby Isaac       PetscInt  closureSize, c;
229b0bf5782SToby Isaac 
230b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
231b0bf5782SToby Isaac         continue;
232b0bf5782SToby Isaac       }
2339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
23448a46eb9SPierre Jolivet       for (c = 0; c < closureSize * 2; c += 2) PetscCall(DMLabelSetValue(label, closure[c], values[v]));
2359566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
236b0bf5782SToby Isaac     }
2379566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2389566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
239b0bf5782SToby Isaac   }
2409566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2419566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2429566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
2439566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
244ac51f24eSSander Arens   if (nroots >= 0) {
24526279d81SSanderA     DMLabel         lblRoots, lblLeaves;
24626279d81SSanderA     IS              valueIS, pointIS;
24726279d81SSanderA     const PetscInt *values;
24826279d81SSanderA     PetscInt        numValues, v;
24926279d81SSanderA 
25026279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
2519566063dSJacob Faibussowitsch     PetscCall(DMLabelGather(label, sfPoint, &lblLeaves));
2529566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS));
2539566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2549566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
25526279d81SSanderA     for (v = 0; v < numValues; ++v) {
25626279d81SSanderA       const PetscInt value = values[v];
25726279d81SSanderA 
2589566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
2599566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2609566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
26126279d81SSanderA     }
2629566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2639566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2649566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblLeaves));
26526279d81SSanderA     /* Push point contributions from roots into remote leaves */
2669566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots));
2679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblRoots, &valueIS));
2689566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2699566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
27026279d81SSanderA     for (v = 0; v < numValues; ++v) {
27126279d81SSanderA       const PetscInt value = values[v];
27226279d81SSanderA 
2739566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS));
2749566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2759566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
27626279d81SSanderA     }
2779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2799566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblRoots));
28026279d81SSanderA   }
2813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
282b0bf5782SToby Isaac }
283b0bf5782SToby Isaac 
2842be2b188SMatthew G Knepley /*@
2852be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
2862be2b188SMatthew G Knepley 
2872be2b188SMatthew G Knepley   Input Parameters:
28820f4b53cSBarry Smith + dm    - The `DM`
28920f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
2902be2b188SMatthew G Knepley 
2912be2b188SMatthew G Knepley   Output Parameter:
29220f4b53cSBarry Smith . label - A `DMLabel` marking all surface points in the transitive closure
2932be2b188SMatthew G Knepley 
2942be2b188SMatthew G Knepley   Level: developer
2952be2b188SMatthew G Knepley 
2961cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelCohesiveComplete()`
2972be2b188SMatthew G Knepley @*/
298d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
299d71ae5a4SJacob Faibussowitsch {
3002be2b188SMatthew G Knepley   PetscFunctionBegin;
3019566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
3023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3032be2b188SMatthew G Knepley }
3042be2b188SMatthew G Knepley 
3056cf0e42fSMatthew G. Knepley /*@
306a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
3076cf0e42fSMatthew G. Knepley 
3086cf0e42fSMatthew G. Knepley   Input Parameters:
30920f4b53cSBarry Smith + dm    - The `DM`
31020f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
3116cf0e42fSMatthew G. Knepley 
3126cf0e42fSMatthew G. Knepley   Output Parameter:
31320f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
3146cf0e42fSMatthew G. Knepley 
3156cf0e42fSMatthew G. Knepley   Level: developer
3166cf0e42fSMatthew G. Knepley 
31720f4b53cSBarry Smith   Note:
31820f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
3196cf0e42fSMatthew G. Knepley 
3201cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
3216cf0e42fSMatthew G. Knepley @*/
322d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
323d71ae5a4SJacob Faibussowitsch {
3246cf0e42fSMatthew G. Knepley   IS              valueIS;
3256cf0e42fSMatthew G. Knepley   const PetscInt *values;
3269df54392SMatthew G. Knepley   PetscInt        numValues, v, csStart, csEnd, chStart, chEnd;
3276cf0e42fSMatthew G. Knepley 
3286cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
3299df54392SMatthew G. Knepley   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &csStart, &csEnd));
3309df54392SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(dm, 0, &chStart, &chEnd));
3319566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3329566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3339566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
3346cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
3356cf0e42fSMatthew G. Knepley     IS              pointIS;
3366cf0e42fSMatthew G. Knepley     const PetscInt *points;
3376cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
3386cf0e42fSMatthew G. Knepley 
3399566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3409566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
3426cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3439df54392SMatthew G. Knepley       const PetscInt point   = points[p];
3446cf0e42fSMatthew G. Knepley       PetscInt      *closure = NULL;
3457bd9760aSJed Brown       PetscInt       closureSize, cl, h, cStart, cEnd;
3467bd9760aSJed Brown       DMPolytopeType ct;
3479df54392SMatthew G. Knepley 
3489df54392SMatthew G. Knepley       // If the point is a hybrid, allow hybrid cells
3497bd9760aSJed Brown       PetscCall(DMPlexGetCellType(dm, point, &ct));
3509df54392SMatthew G. Knepley       PetscCall(DMPlexGetPointHeight(dm, point, &h));
3517bd9760aSJed Brown       if (DMPolytopeTypeIsHybrid(ct)) {
3529df54392SMatthew G. Knepley         cStart = chStart;
3539df54392SMatthew G. Knepley         cEnd   = chEnd;
3547bd9760aSJed Brown       } else {
3557bd9760aSJed Brown         cStart = csStart;
3567bd9760aSJed Brown         cEnd   = csEnd;
3579df54392SMatthew G. Knepley       }
3586cf0e42fSMatthew G. Knepley 
3599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
36022eabd52SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
361a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
3629371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
3639371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
3649371c9d4SSatish Balay           break;
3659371c9d4SSatish Balay         }
36622eabd52SMatthew G. Knepley       }
3679566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
3686cf0e42fSMatthew G. Knepley     }
3699566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3709566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3716cf0e42fSMatthew G. Knepley   }
3729566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3739566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
3743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3756cf0e42fSMatthew G. Knepley }
3766cf0e42fSMatthew G. Knepley 
377f402d5e4SToby Isaac /*@
378a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
379a6e0b375SMatthew G. Knepley 
380a6e0b375SMatthew G. Knepley   Input Parameters:
38120f4b53cSBarry Smith + dm    - The `DM`
38220f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
383a6e0b375SMatthew G. Knepley 
384a6e0b375SMatthew G. Knepley   Output Parameter:
38520f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
386a6e0b375SMatthew G. Knepley 
387a6e0b375SMatthew G. Knepley   Level: developer
388a6e0b375SMatthew G. Knepley 
38920f4b53cSBarry Smith   Note:
39020f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
391a6e0b375SMatthew G. Knepley 
3921cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
393a6e0b375SMatthew G. Knepley @*/
394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
395d71ae5a4SJacob Faibussowitsch {
396a6e0b375SMatthew G. Knepley   IS              valueIS;
397a6e0b375SMatthew G. Knepley   const PetscInt *values;
398a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
399a6e0b375SMatthew G. Knepley 
400a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
4019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
4039566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4049566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4059566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
406a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
407a6e0b375SMatthew G. Knepley     IS              pointIS;
408a6e0b375SMatthew G. Knepley     const PetscInt *points;
409a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
410a6e0b375SMatthew G. Knepley 
4119566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4129566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4139566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
414a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
415a6e0b375SMatthew G. Knepley       const PetscInt face    = points[p];
416a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
417a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
418a6e0b375SMatthew G. Knepley 
419a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
4209566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
421a6e0b375SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
422a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
4239371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
4249371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
4259371c9d4SSatish Balay           break;
4269371c9d4SSatish Balay         }
427a6e0b375SMatthew G. Knepley       }
4289566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
429a6e0b375SMatthew G. Knepley     }
4309566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4319566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
432a6e0b375SMatthew G. Knepley   }
4339566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4349566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
436a6e0b375SMatthew G. Knepley }
437a6e0b375SMatthew G. Knepley 
438a6e0b375SMatthew G. Knepley /*@
439f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
440f402d5e4SToby Isaac 
441f402d5e4SToby Isaac   Input Parameters:
44220f4b53cSBarry Smith + dm    - The `DM`
44320f4b53cSBarry Smith - label - A `DMLabel` marking surface points and their adjacent cells
444f402d5e4SToby Isaac 
445f402d5e4SToby Isaac   Output Parameter:
44620f4b53cSBarry Smith . label - A `DMLabel` without cells
447f402d5e4SToby Isaac 
448f402d5e4SToby Isaac   Level: developer
449f402d5e4SToby Isaac 
45020f4b53cSBarry Smith   Note:
45120f4b53cSBarry Smith   This undoes `DMPlexLabelAddCells()` or `DMPlexLabelAddFaceCells()`
452f402d5e4SToby Isaac 
4531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()`
454f402d5e4SToby Isaac @*/
455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
456d71ae5a4SJacob Faibussowitsch {
457f402d5e4SToby Isaac   IS              valueIS;
458f402d5e4SToby Isaac   const PetscInt *values;
459485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
460f402d5e4SToby Isaac 
461f402d5e4SToby Isaac   PetscFunctionBegin;
4629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4639566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4649566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4659566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
466f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
467f402d5e4SToby Isaac     IS              pointIS;
468f402d5e4SToby Isaac     const PetscInt *points;
469f402d5e4SToby Isaac     PetscInt        numPoints, p;
470f402d5e4SToby Isaac 
4719566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4729566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
474f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
475f402d5e4SToby Isaac       PetscInt point = points[p];
476f402d5e4SToby Isaac 
47748a46eb9SPierre Jolivet       if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v]));
478f402d5e4SToby Isaac     }
4799566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4809566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
481f402d5e4SToby Isaac   }
4829566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4839566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
485f402d5e4SToby Isaac }
486f402d5e4SToby Isaac 
48759eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
48859eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
489d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
490d71ae5a4SJacob Faibussowitsch {
4912582d50cSToby Isaac   PetscInt d, off = 0;
4922582d50cSToby Isaac 
4932582d50cSToby Isaac   PetscFunctionBegin;
49459eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
4950974a383SToby Isaac   for (d = 0; d < depth; d++) {
4962582d50cSToby Isaac     PetscInt firstd     = d;
4970974a383SToby Isaac     PetscInt firstStart = depthShift[2 * d];
4982582d50cSToby Isaac     PetscInt e;
4992582d50cSToby Isaac 
5002582d50cSToby Isaac     for (e = d + 1; e <= depth; e++) {
5012582d50cSToby Isaac       if (depthShift[2 * e] < firstStart) {
5022582d50cSToby Isaac         firstd     = e;
5032582d50cSToby Isaac         firstStart = depthShift[2 * d];
5042582d50cSToby Isaac       }
5052582d50cSToby Isaac     }
5062582d50cSToby Isaac     if (firstd != d) {
5072582d50cSToby Isaac       PetscInt swap[2];
5082582d50cSToby Isaac 
5092582d50cSToby Isaac       e                     = firstd;
5102582d50cSToby Isaac       swap[0]               = depthShift[2 * d];
5112582d50cSToby Isaac       swap[1]               = depthShift[2 * d + 1];
5122582d50cSToby Isaac       depthShift[2 * d]     = depthShift[2 * e];
5132582d50cSToby Isaac       depthShift[2 * d + 1] = depthShift[2 * e + 1];
5142582d50cSToby Isaac       depthShift[2 * e]     = swap[0];
5152582d50cSToby Isaac       depthShift[2 * e + 1] = swap[1];
5162582d50cSToby Isaac     }
5172582d50cSToby Isaac   }
5182582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
5190974a383SToby Isaac   for (d = 0; d <= depth; d++) {
5202582d50cSToby Isaac     off += depthShift[2 * d + 1];
52159eef20bSToby Isaac     depthShift[2 * d + 1] = depthShift[2 * d] + off;
5222582d50cSToby Isaac   }
5233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5242582d50cSToby Isaac }
5252582d50cSToby Isaac 
5262582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
527d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_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]) return p + newOff;
5342582d50cSToby Isaac     else newOff = depthShift[2 * d + 1] - depthShift[2 * d];
5352582d50cSToby Isaac   }
5360974a383SToby Isaac   return p + newOff;
5372582d50cSToby Isaac }
5382582d50cSToby Isaac 
5392582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
540d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
541d71ae5a4SJacob Faibussowitsch {
5422582d50cSToby Isaac   PetscInt d;
5432582d50cSToby Isaac   PetscInt newOff = 0;
5442582d50cSToby Isaac 
5452582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5462582d50cSToby Isaac     if (p < depthShift[2 * d + 1]) return p + newOff;
5472582d50cSToby Isaac     else newOff = depthShift[2 * d] - depthShift[2 * d + 1];
5482582d50cSToby Isaac   }
5490974a383SToby Isaac   return p + newOff;
550cd0c2139SMatthew G Knepley }
551cd0c2139SMatthew G Knepley 
552d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
553d71ae5a4SJacob Faibussowitsch {
554cd0c2139SMatthew G Knepley   PetscInt depth = 0, d, pStart, pEnd, p;
555fa8e8ae5SToby Isaac   DMLabel  depthLabel;
556cd0c2139SMatthew G Knepley 
557cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5593ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
560cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
5619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5620974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5639566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
5649566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "depth"));
5659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel));
5669566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
567cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
568cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
569fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
570fa8e8ae5SToby Isaac     IS       pIS;
571fa8e8ae5SToby Isaac 
5729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
573fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
574fa8e8ae5SToby Isaac     pEndNew   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5759566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
5769566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
5779566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
578cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
5792582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
580cd0c2139SMatthew G Knepley       PetscInt       size;
581412e9a14SMatthew G. Knepley       DMPolytopeType ct;
582cd0c2139SMatthew G Knepley 
5839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
5849566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
5859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
5869566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
5879566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
5889566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
589cd0c2139SMatthew G Knepley     }
590cd0c2139SMatthew G Knepley   }
5913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
592cd0c2139SMatthew G Knepley }
593cd0c2139SMatthew G Knepley 
594d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
595d71ae5a4SJacob Faibussowitsch {
5962582d50cSToby Isaac   PetscInt *newpoints;
5972582d50cSToby Isaac   PetscInt  depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
598cd0c2139SMatthew G Knepley 
599cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
6013ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
6029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
6039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
6049566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints));
605cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
6069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
607cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
608cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
6092582d50cSToby Isaac     PetscInt        size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
610cd0c2139SMatthew G Knepley 
6119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
6129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
6139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
614ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
6159566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
6169566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
6179566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
6189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
6199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
620ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
621dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
6229566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
623cd0c2139SMatthew G Knepley   }
6249566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
6253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
626cd0c2139SMatthew G Knepley }
627cd0c2139SMatthew G Knepley 
628d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
629d71ae5a4SJacob Faibussowitsch {
630cd0c2139SMatthew G Knepley   PetscSection coordSection, newCoordSection;
631cd0c2139SMatthew G Knepley   Vec          coordinates, newCoordinates;
632cd0c2139SMatthew G Knepley   PetscScalar *coords, *newCoords;
633f2b8cce1SMatthew G. Knepley   PetscInt     coordSize, sStart, sEnd;
634f2b8cce1SMatthew G. Knepley   PetscInt     dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
635f2b8cce1SMatthew G. Knepley   PetscBool    hasCells;
636cd0c2139SMatthew G Knepley 
637cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
6399566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
6409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
641cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
6429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
6439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
6449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
6459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
6469566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6479566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
6489566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
6499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
6509566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
651f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
6529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
653f2b8cce1SMatthew G. Knepley   if (hasCells) {
654f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
655f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
656f2b8cce1SMatthew G. Knepley 
6579566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6589566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
6599566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
660f2b8cce1SMatthew G. Knepley     }
661f2b8cce1SMatthew G. Knepley   }
662cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
6639566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
6649566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
665cd0c2139SMatthew G Knepley   }
6669566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
6679566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
6689566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
6699566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
6709566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates"));
6719566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
6729566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
6739566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates, VECSTANDARD));
6749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
6759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
6769566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6779566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
678f2b8cce1SMatthew G. Knepley   if (hasCells) {
679f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
680f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
681f2b8cce1SMatthew G. Knepley 
6829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
6849566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
685f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
686f2b8cce1SMatthew G. Knepley     }
687f2b8cce1SMatthew G. Knepley   }
688cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
689cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
690cd0c2139SMatthew G Knepley 
6919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
6929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
6939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
694f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
695cd0c2139SMatthew G Knepley   }
6969566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6979566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
6989566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
6999566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
7003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
701cd0c2139SMatthew G Knepley }
702cd0c2139SMatthew G Knepley 
703d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
704d71ae5a4SJacob Faibussowitsch {
705cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
706cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
707cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
708cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
7090e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
710cd0c2139SMatthew G Knepley 
711cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
7139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
7149566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
7156e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
716cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
7179566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation));
7180e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
7199566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7209566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7219566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
7229566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
723cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
7242582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
725cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
726cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
727cd0c2139SMatthew G Knepley     }
7289566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
7299566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
7300e33faafSMatthew G. Knepley   }
7313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7320e33faafSMatthew G. Knepley }
7330e33faafSMatthew G. Knepley 
734d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
735d71ae5a4SJacob Faibussowitsch {
7360e33faafSMatthew G. Knepley   PetscSF   sfPoint, sfPointNew;
7370e33faafSMatthew G. Knepley   PetscBool useNatural;
7380e33faafSMatthew G. Knepley 
7390e33faafSMatthew G. Knepley   PetscFunctionBegin;
7400e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
7419566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
7429566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
7439566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
7440e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
7459566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
7460e33faafSMatthew G. Knepley   if (useNatural) {
7470e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
7480e33faafSMatthew G. Knepley 
7499566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
7509566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
7519566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
7529566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
753cd0c2139SMatthew G Knepley   }
7543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
755cd0c2139SMatthew G Knepley }
756cd0c2139SMatthew G Knepley 
757d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
758d71ae5a4SJacob Faibussowitsch {
759d56405f8SMatthew G. Knepley   PetscInt depth = 0, numLabels, l;
760cd0c2139SMatthew G Knepley 
761cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
763cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
7649566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
765cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
766cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
767cd0c2139SMatthew G Knepley     const char     *lname;
768fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
769cd0c2139SMatthew G Knepley     IS              valueIS;
770cd0c2139SMatthew G Knepley     const PetscInt *values;
771cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
772cd0c2139SMatthew G Knepley 
7739566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
7749566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
775cd0c2139SMatthew G Knepley     if (isDepth) continue;
7769566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
777fa8e8ae5SToby Isaac     if (isDim) continue;
7789566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
7799566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
7809566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
7819566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &val));
7829566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, val));
7839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
7849566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
7859566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
786cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
787cd0c2139SMatthew G Knepley       IS              pointIS;
788cd0c2139SMatthew G Knepley       const PetscInt *points;
789cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
790cd0c2139SMatthew G Knepley 
7919566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
7929566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
7939566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
794cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
7952582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
796cd0c2139SMatthew G Knepley 
7979566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
798cd0c2139SMatthew G Knepley       }
7999566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
8009566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
801cd0c2139SMatthew G Knepley     }
8029566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
8039566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
804cd0c2139SMatthew G Knepley   }
8053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
806d56405f8SMatthew G. Knepley }
807d56405f8SMatthew G. Knepley 
808d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
809d71ae5a4SJacob Faibussowitsch {
810d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
811d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
812d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
813d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
814d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
815d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
816d56405f8SMatthew G. Knepley 
817d56405f8SMatthew G. Knepley   PetscFunctionBegin;
818cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
8199566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8209566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
8219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
8229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8239566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
8249566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
8259566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
826d56405f8SMatthew G. Knepley   if (createGhostLabel) {
8279566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
8289566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
829d56405f8SMatthew G. Knepley   }
830cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
83148a46eb9SPierre Jolivet     for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
832cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
833cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
8349566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
8351baa6e33SBarry Smith     } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
836cd0c2139SMatthew G Knepley   }
83748a46eb9SPierre Jolivet   for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
838d56405f8SMatthew G. Knepley   if (ghostLabel) {
8399566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
840cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
841cd0c2139SMatthew G Knepley       PetscInt numCells;
842cd0c2139SMatthew G Knepley 
8439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
844cd0c2139SMatthew G Knepley       if (numCells < 2) {
8459566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
846cd0c2139SMatthew G Knepley       } else {
847cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
848cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
849cd0c2139SMatthew G Knepley 
8509566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
8519566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
8529566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
8539566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
854cd0c2139SMatthew G Knepley       }
855cd0c2139SMatthew G Knepley     }
856d56405f8SMatthew G. Knepley   }
8573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
858cd0c2139SMatthew G Knepley }
859cd0c2139SMatthew G Knepley 
860d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
861d71ae5a4SJacob Faibussowitsch {
862ca04dac2SToby Isaac   DM           refTree;
863ca04dac2SToby Isaac   PetscSection pSec;
864ca04dac2SToby Isaac   PetscInt    *parents, *childIDs;
865ca04dac2SToby Isaac 
866ca04dac2SToby Isaac   PetscFunctionBegin;
8679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
8689566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew, refTree));
8699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL));
870ca04dac2SToby Isaac   if (pSec) {
8712582d50cSToby Isaac     PetscInt     p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
872fb4630b5SToby Isaac     PetscInt    *childIDsShifted;
873ca04dac2SToby Isaac     PetscSection pSecShifted;
874ca04dac2SToby Isaac 
8759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd));
8769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
8772582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
8782582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
8799566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted));
8809566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted));
8819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted));
882ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
883fb4630b5SToby Isaac       /* start off assuming no children */
8849566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, p, 0));
885fb4630b5SToby Isaac     }
886fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
887fb4630b5SToby Isaac       PetscInt dof;
888fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
889ca04dac2SToby Isaac 
8909566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
8919566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof));
892ca04dac2SToby Isaac     }
8939566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
894fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
895fb4630b5SToby Isaac       PetscInt dof;
896fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
897fb4630b5SToby Isaac 
8989566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
899fb4630b5SToby Isaac       if (dof) {
900fb4630b5SToby Isaac         PetscInt off, offNew;
901fb4630b5SToby Isaac 
9029566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec, p, &off));
9039566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew));
904fb4630b5SToby Isaac         parentsShifted[offNew]  = DMPlexShiftPoint_Internal(parents[off], depth, depthShift);
905fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
906fb4630b5SToby Isaac       }
907fb4630b5SToby Isaac     }
9089566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted));
9099566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted, childIDsShifted));
9109566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
911ca04dac2SToby Isaac   }
9123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
913ca04dac2SToby Isaac }
914ca04dac2SToby Isaac 
915d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
916d71ae5a4SJacob Faibussowitsch {
917da97024aSMatthew G. Knepley   PetscSF          sf;
918cd0c2139SMatthew G Knepley   IS               valueIS;
919da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
920cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
9212582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
9224fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
923cd0c2139SMatthew G Knepley 
924cd0c2139SMatthew G Knepley   PetscFunctionBegin;
9259566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
9269566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
927da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
9289566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
929cd0c2139SMatthew G Knepley   /* Count ghost cells */
9309566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
9319566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
9329566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
9334a6cfa73SMatthew G. Knepley   Ng = 0;
934cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
93546c796b9SMatthew G. Knepley     IS              faceIS;
93646c796b9SMatthew G. Knepley     const PetscInt *faces;
93746c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
938cd0c2139SMatthew G Knepley 
9399566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9409566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
94246c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
943ca04dac2SToby Isaac       PetscInt numChildren;
944ca04dac2SToby Isaac 
9459566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9469566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
947ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
948ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
94946c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
95046c796b9SMatthew G. Knepley     }
9514a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
9529566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
954cd0c2139SMatthew G Knepley   }
9559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
9569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift));
9572582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
95859eef20bSToby Isaac     PetscInt dEnd;
9592582d50cSToby Isaac 
9609566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd));
96159eef20bSToby Isaac     depthShift[2 * d]     = dEnd;
9622582d50cSToby Isaac     depthShift[2 * d + 1] = 0;
9632582d50cSToby Isaac   }
9642582d50cSToby Isaac   if (depth >= 0) depthShift[2 * depth + 1] = Ng;
9659566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
9669566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
967cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
9689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
96948a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1));
970cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
971cd0c2139SMatthew G Knepley     IS              faceIS;
972cd0c2139SMatthew G Knepley     const PetscInt *faces;
973cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
974cd0c2139SMatthew G Knepley 
9759566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9769566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9779566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
978cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
979ca04dac2SToby Isaac       PetscInt size, numChildren;
980cd0c2139SMatthew G Knepley 
9819566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
983ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
98446c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
98663a3b9bcSJacob 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);
9879566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
988cd0c2139SMatthew G Knepley     }
9899566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9909566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
991cd0c2139SMatthew G Knepley   }
992cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
9939566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
9949566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
995cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
996cd0c2139SMatthew G Knepley   ghostCell = cEnd;
997cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
998cd0c2139SMatthew G Knepley     IS              faceIS;
999cd0c2139SMatthew G Knepley     const PetscInt *faces;
1000cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
1001cd0c2139SMatthew G Knepley 
10029566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
10039566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
10049566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
100546c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
1006ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
1007cd0c2139SMatthew G Knepley 
10089566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
10099566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
1010ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
101146c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
10129566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
10139566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
101446c796b9SMatthew G. Knepley       ++ghostCell;
1015cd0c2139SMatthew G Knepley     }
10169566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
10179566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
1018cd0c2139SMatthew G Knepley   }
10199566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
10209566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
10219566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
10229566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
10239566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
10249566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
10259566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
10269566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
102748a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
1028966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
10294fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
10304fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L));
10314a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
10323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1033cd0c2139SMatthew G Knepley }
1034cd0c2139SMatthew G Knepley 
10355d83a8b1SBarry Smith /*@
1036cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
1037cd0c2139SMatthew G Knepley 
103820f4b53cSBarry Smith   Collective
1039cd0c2139SMatthew G Knepley 
1040cd0c2139SMatthew G Knepley   Input Parameters:
104120f4b53cSBarry Smith + dm        - The original `DM`
104220f4b53cSBarry Smith - labelName - The label specifying the boundary faces, or "Face Sets" if this is `NULL`
1043cd0c2139SMatthew G Knepley 
1044cd0c2139SMatthew G Knepley   Output Parameters:
104520f4b53cSBarry Smith + numGhostCells - The number of ghost cells added to the `DM`
104620f4b53cSBarry Smith - dmGhosted     - The new `DM`
1047cd0c2139SMatthew G Knepley 
1048cd0c2139SMatthew G Knepley   Level: developer
1049cd0c2139SMatthew G Knepley 
105020f4b53cSBarry Smith   Note:
105120f4b53cSBarry Smith   If no label exists of that name, one will be created marking all boundary faces
105220f4b53cSBarry Smith 
10531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`
105431266bc0SMatthew G. Knepley @*/
1055d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
1056d71ae5a4SJacob Faibussowitsch {
1057cd0c2139SMatthew G Knepley   DM          gdm;
1058cd0c2139SMatthew G Knepley   DMLabel     label;
1059cd0c2139SMatthew G Knepley   const char *name = labelName ? labelName : "Face Sets";
1060412e9a14SMatthew G. Knepley   PetscInt    dim, Ng = 0;
1061b0441da4SMatthew G. Knepley   PetscBool   useCone, useClosure;
1062cd0c2139SMatthew G Knepley 
1063cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1064cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10654f572ea9SToby Isaac   if (numGhostCells) PetscAssertPointer(numGhostCells, 3);
10664f572ea9SToby Isaac   PetscAssertPointer(dmGhosted, 4);
10679566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
10689566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
10699566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
10709566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
10719566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
10729566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure));
10739566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
1074cd0c2139SMatthew G Knepley   if (!label) {
1075cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
10769566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
10779566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
10789566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
1079cd0c2139SMatthew G Knepley   }
10809566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
10819566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
10825de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
1083cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
1084d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
1085cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
10863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1087cd0c2139SMatthew G Knepley }
1088cd0c2139SMatthew G Knepley 
1089d71ae5a4SJacob Faibussowitsch static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue)
1090d71ae5a4SJacob Faibussowitsch {
1091accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
1092accc9626SMatthew G. Knepley 
1093accc9626SMatthew G. Knepley   PetscFunctionBegin;
1094accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1095accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
109648a46eb9SPierre Jolivet   for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
1097accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
1098accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
1099accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
1100accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
1101accc9626SMatthew G. Knepley 
1102accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
1103accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
1104accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1105accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
1106accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1107accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1108accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
1109accc9626SMatthew G. Knepley         PetscInt       clval;
1110accc9626SMatthew G. Knepley 
1111accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
1112accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
1113accc9626SMatthew G. Knepley         if (clval == -1) {
1114accc9626SMatthew G. Knepley           const PetscInt *cone;
1115accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
1116accc9626SMatthew G. Knepley 
1117accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
1118accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
1119accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
1120accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
1121accc9626SMatthew G. Knepley             PetscInt cpval;
1122accc9626SMatthew G. Knepley 
1123accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
1124accc9626SMatthew G. Knepley             if (cpval != -1) {
1125accc9626SMatthew G. Knepley               PetscInt dep;
1126accc9626SMatthew G. Knepley 
1127accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
1128accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift + dep) : shift + dep;
1129accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
1130accc9626SMatthew G. Knepley               break;
1131accc9626SMatthew G. Knepley             }
1132accc9626SMatthew G. Knepley           }
1133accc9626SMatthew G. Knepley         }
1134accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
1135accc9626SMatthew G. Knepley         if (d == dim - 1 && clval != -1) {
1136accc9626SMatthew G. Knepley           const PetscInt *support;
1137accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
1138accc9626SMatthew G. Knepley 
1139accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
1140accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
1141accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
1142accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
1143accc9626SMatthew G. Knepley             if (nval == -1) {
1144accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1));
1145accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1146accc9626SMatthew G. Knepley             }
1147accc9626SMatthew G. Knepley           }
1148accc9626SMatthew G. Knepley         }
1149accc9626SMatthew G. Knepley       }
1150accc9626SMatthew G. Knepley     }
1151accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1152accc9626SMatthew G. Knepley   }
1153accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
11543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1155accc9626SMatthew G. Knepley }
1156accc9626SMatthew G. Knepley 
1157accc9626SMatthew G. Knepley typedef struct {
1158accc9626SMatthew G. Knepley   DM               dm;
1159accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1160accc9626SMatthew G. Knepley } PointDivision;
1161accc9626SMatthew G. Knepley 
1162d71ae5a4SJacob Faibussowitsch static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx)
1163d71ae5a4SJacob Faibussowitsch {
1164accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *)ctx;
1165accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val - 1 : val + 1;
1166accc9626SMatthew G. Knepley   const PetscInt *support;
1167accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1168accc9626SMatthew G. Knepley 
1169accc9626SMatthew G. Knepley   PetscFunctionBegin;
1170accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1171accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1172accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1173accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1174accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1175accc9626SMatthew G. Knepley   }
11763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1177accc9626SMatthew G. Knepley }
1178accc9626SMatthew G. Knepley 
1179accc9626SMatthew G. Knepley /* Mark cells by label propagation */
1180d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel)
1181d71ae5a4SJacob Faibussowitsch {
1182accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1183accc9626SMatthew G. Knepley   PointDivision    div;
1184accc9626SMatthew G. Knepley   PetscSF          pointSF;
1185accc9626SMatthew G. Knepley   IS               pointIS;
1186accc9626SMatthew G. Knepley   const PetscInt  *points;
1187accc9626SMatthew G. Knepley   PetscBool        empty;
1188accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1189accc9626SMatthew G. Knepley 
1190accc9626SMatthew G. Knepley   PetscFunctionBegin;
1191accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1192accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1193accc9626SMatthew G. Knepley   div.dm    = dm;
1194accc9626SMatthew G. Knepley   div.queue = queue;
1195accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1196accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS));
1197accc9626SMatthew G. Knepley   if (pointIS) {
1198accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1199accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
120048a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1201accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1202accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1203accc9626SMatthew G. Knepley   }
1204accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS));
1205accc9626SMatthew G. Knepley   if (pointIS) {
1206accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1207accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
120848a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1209accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1210accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1211accc9626SMatthew G. Knepley   }
1212accc9626SMatthew G. Knepley 
1213accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1214accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1215accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1216accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1217accc9626SMatthew G. Knepley   while (!empty) {
1218accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1219accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1220accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1221accc9626SMatthew G. Knepley   }
1222accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1223accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
12243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1225accc9626SMatthew G. Knepley }
1226accc9626SMatthew G. Knepley 
1227607ab7a9SMatthew G. Knepley /*
1228faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1229607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1230faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1231b6dfa339SMatthew 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
1232607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1233a6ae58d1SMatthew G. Knepley 
1234a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1235a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1236607ab7a9SMatthew G. Knepley */
1237d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1238d71ae5a4SJacob Faibussowitsch {
1239cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1240607ab7a9SMatthew G. Knepley   IS               valueIS;
1241607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0; /* The number of depths for which we have replicated points */
1242607ab7a9SMatthew G. Knepley   const PetscInt  *values;    /* List of depths for which we have replicated points */
124318c5995bSMatthew G. Knepley   IS              *splitIS;
124418c5995bSMatthew G. Knepley   IS              *unsplitIS;
1245b6dfa339SMatthew G. Knepley   IS               ghostIS;
1246607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
124718c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
124836dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
124936dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1250b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1251607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
125218c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1253b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1254cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1255cd0c2139SMatthew G Knepley   Vec              coordinates;
1256cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1257a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;   /* The first hybrid point at each depth in the original mesh */
1258a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;   /* The point limit at each depth in the original mesh */
1259607ab7a9SMatthew G. Knepley   PetscInt        *depthShift; /* Number of replicated+hybrid points at each depth */
1260607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;    /* The first replicated point at each depth in the new mesh, hybrids come after this */
1261607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
126218c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1263cd0c2139SMatthew G Knepley 
1264cd0c2139SMatthew G Knepley   PetscFunctionBegin;
12659566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
12669566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
12679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
12689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1269412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
12709566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1271cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
12729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
12739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld));
12749566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints));
1275607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
12769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
12779566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1278a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1279a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1280607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
128118c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1282607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1283a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1284607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
128518c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
128618c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
128718c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
128859eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
128959eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
129059eef20bSToby Isaac     depthShift[2 * d]     = depthMax[d];
129159eef20bSToby Isaac     depthShift[2 * d + 1] = 0;
1292607ab7a9SMatthew G. Knepley   }
1293b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1294b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1295cd0c2139SMatthew G Knepley   if (label) {
12969566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
12979566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
12989566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1299cd0c2139SMatthew G Knepley   }
1300cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1301cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1302cd0c2139SMatthew G Knepley 
1303cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
13049566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
130518c5995bSMatthew G. Knepley     if (splitIS[dep]) {
13069566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
13079566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
130818c5995bSMatthew G. Knepley     }
13099566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep]));
131018c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
13119566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
13129566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1313cd0c2139SMatthew G Knepley     }
1314cd0c2139SMatthew G Knepley   }
1315b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS));
1316b6dfa339SMatthew G. Knepley   if (ghostIS) {
1317b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1318b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1319b6dfa339SMatthew G. Knepley   }
1320607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
132118c5995bSMatthew 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   */
13222582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d];
13239566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
132459eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
132559eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
132659eef20bSToby Isaac    * added points */
13272582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
13289566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1329cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1330cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1331cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1332cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
13332582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
13344c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1335cd0c2139SMatthew G Knepley       const PetscInt *support;
1336394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
13374c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1338cd0c2139SMatthew G Knepley 
13399566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13409566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
13419566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13429566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
13439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
13449566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1345cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
13464c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13474c367dbcSMatthew G. Knepley 
1348cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
13499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1350412e9a14SMatthew G. Knepley         switch (coneSize) {
1351d71ae5a4SJacob Faibussowitsch         case 2:
1352d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));
1353d71ae5a4SJacob Faibussowitsch           break;
1354d71ae5a4SJacob Faibussowitsch         case 3:
1355d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));
1356d71ae5a4SJacob Faibussowitsch           break;
1357d71ae5a4SJacob Faibussowitsch         case 4:
1358d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));
1359d71ae5a4SJacob Faibussowitsch           break;
1360412e9a14SMatthew G. Knepley         }
1361b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1362b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1363b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1364b6dfa339SMatthew G. Knepley           const PetscInt *support;
1365b6dfa339SMatthew G. Knepley           PetscInt        val;
1366b6dfa339SMatthew G. Knepley 
1367b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1368b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1369b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1370b6dfa339SMatthew G. Knepley           else PetscCall(DMPlexSetSupportSize(sdm, newp, 2));
1371b6dfa339SMatthew G. Knepley         }
1372cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13734c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1374cd0c2139SMatthew G Knepley 
13759566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13764c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1377cd0c2139SMatthew G Knepley           PetscInt val;
1378cd0c2139SMatthew G Knepley 
13799566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13804c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
13814c367dbcSMatthew G. Knepley           if ((val == 1) || (val == (shift + 1))) ++qn;
13824c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1383cd0c2139SMatthew G Knepley         }
13844c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
13859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13864c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
13879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13884c367dbcSMatthew G. Knepley         /* Add hybrid edge */
13899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
13909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1392cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
13934c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13944c367dbcSMatthew G. Knepley 
13959566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13964c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1397cd0c2139SMatthew G Knepley           PetscInt val;
1398cd0c2139SMatthew G Knepley 
13999566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
14004c367dbcSMatthew G. Knepley           if (val == dim - 1) ++qf;
14014c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn;
14024c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp;
1403cd0c2139SMatthew G Knepley         }
14044c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
14059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
14064c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
14079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
14084c367dbcSMatthew G. Knepley         /* Add hybrid face */
14099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1412cd0c2139SMatthew G Knepley       }
1413cd0c2139SMatthew G Knepley     }
1414cd0c2139SMatthew G Knepley   }
141518c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
141618c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
141718c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
14182582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
141918c5995bSMatthew G. Knepley       const PetscInt *support;
1420da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
142118c5995bSMatthew G. Knepley 
14229566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14239566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14249566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
142518c5995bSMatthew G. Knepley       if (dep == 0) {
142618c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
142718c5995bSMatthew G. Knepley 
142839254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
142939254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
14309566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
143139254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
143239254ff6SMatthew G. Knepley         }
14339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
143418c5995bSMatthew G. Knepley         /* Add hybrid edge */
14359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1436e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1437e1757548SMatthew G. Knepley           PetscInt val;
1438e1757548SMatthew G. Knepley 
14399566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1440e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1441da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1442da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1443e1757548SMatthew G. Knepley         }
14449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
14459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
144618c5995bSMatthew G. Knepley       } else if (dep == dim - 2) {
144718c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1448cd0c2139SMatthew G Knepley         PetscInt       val;
1449cd0c2139SMatthew G Knepley 
1450da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14519566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1452da1dd7e4SMatthew G. Knepley           if (val == dim - 1) qf += 2;
1453da1dd7e4SMatthew G. Knepley           else ++qf;
1454cd0c2139SMatthew G Knepley         }
145518c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
14569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
145718c5995bSMatthew G. Knepley         /* Add hybrid face */
1458da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14599566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1460da1dd7e4SMatthew G. Knepley           if (val == dim - 1) ++qf;
1461da1dd7e4SMatthew G. Knepley         }
14629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1465cd0c2139SMatthew G Knepley       }
1466cd0c2139SMatthew G Knepley     }
1467cd0c2139SMatthew G Knepley   }
1468cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
14699566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
14709566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
14719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
14729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew));
1473cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1474cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1475cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1476cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
14772582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
14784c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1479cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1480b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
14814c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1482cd0c2139SMatthew G Knepley 
14839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
14849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
14869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
14879566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14889566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1489cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
149096a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
14914c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1492cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1493cd0c2139SMatthew G Knepley 
1494b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1495cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
14969566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp, &supportF));
14979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1498cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1499cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1500cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
15019566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
150218c5995bSMatthew G. Knepley           if (v < 0) {
15039566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
150463a3b9bcSJacob 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);
15052582d50cSToby Isaac             coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
150696a07cd0SMatthew G. Knepley             hasUnsplit     = PETSC_TRUE;
150718c5995bSMatthew G. Knepley           } else {
15084c367dbcSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep - 1];
1509163235baSMatthew G. Knepley             if (dep > 1) {
1510163235baSMatthew G. Knepley               const PetscInt *econe;
1511163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1512163235baSMatthew G. Knepley 
15139566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
15149566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1515163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
15169566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs));
15179566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu));
1518163235baSMatthew G. Knepley                 if (vs >= 0) continue;
151963a3b9bcSJacob 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);
1520163235baSMatthew G. Knepley                 hasUnsplit = PETSC_TRUE;
1521163235baSMatthew G. Knepley               }
1522163235baSMatthew G. Knepley             }
1523cd0c2139SMatthew G Knepley           }
1524cd0c2139SMatthew G Knepley         }
15259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
15269566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1527e537020bSMatthew G. Knepley         /* Face support */
1528b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1529cd0c2139SMatthew G Knepley 
1530b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1531b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1532b6dfa339SMatthew G. Knepley         else vals[1] = -vals[0];
1533b6dfa339SMatthew 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]);
1534b6dfa339SMatthew G. Knepley 
1535b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1536b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1537b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1538b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1539accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1540b6dfa339SMatthew G. Knepley             } else {
1541b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1542accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1543b6dfa339SMatthew G. Knepley             }
1544b6dfa339SMatthew G. Knepley           } else {
1545b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1546cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
15479566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1548cd0c2139SMatthew G Knepley             } else {
1549cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
15509566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1551b6dfa339SMatthew G. Knepley             }
1552b6dfa339SMatthew G. Knepley           }
1553b6dfa339SMatthew G. Knepley         }
1554b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1555e537020bSMatthew G. Knepley         {
1556e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1557b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1558accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1559e537020bSMatthew G. Knepley 
15609371c9d4SSatish Balay           if (supportSize > 1) {
15619371c9d4SSatish Balay             ocell = vals[0] < 0 ? support[1] : support[0];
15629371c9d4SSatish Balay           } else {
15639371c9d4SSatish Balay             ocell = support[0];
15649371c9d4SSatish Balay             flip  = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;
15659371c9d4SSatish Balay           }
1566b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1567b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1568e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1569e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1570e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1571b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc];
1572e537020bSMatthew G. Knepley               break;
1573cd0c2139SMatthew G Knepley             }
1574cd0c2139SMatthew G Knepley           }
1575b6dfa339SMatthew 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);
1576e537020bSMatthew G. Knepley         }
15774c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1578b6dfa339SMatthew G. Knepley         {
157985036b15SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangements(ct) / 2;
1580b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1581b6dfa339SMatthew G. Knepley         }
158285036b15SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, coneONew[0]);
1583b5a892a1SMatthew G. Knepley 
1584fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp; /* Extracted negative side orientation above */
15854c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
15864c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1587e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1588412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1589b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q * 2 + 0];
1590b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q * 2 + 1];
1591b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1592412e9a14SMatthew G. Knepley 
15939566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
159418c5995bSMatthew G. Knepley           if (v < 0) {
15959566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
159618c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
159718c5995bSMatthew G. Knepley           } else {
159818c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep];
159918c5995bSMatthew G. Knepley           }
1600b5a892a1SMatthew G. Knepley           coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1601e537020bSMatthew G. Knepley         }
16029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
16039566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
160496a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
16059566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1606cd0c2139SMatthew G Knepley       } else if (dep == 0) {
16074c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1608cd0c2139SMatthew G Knepley 
1609cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
16104c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1611cd0c2139SMatthew G Knepley           PetscInt val;
1612cd0c2139SMatthew G Knepley 
16139566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1614ad540459SPierre Jolivet           if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1615cd0c2139SMatthew G Knepley         }
16164c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
16179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1618cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
16194c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1620cd0c2139SMatthew G Knepley           PetscInt val, edge;
1621cd0c2139SMatthew G Knepley 
16229566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1623cd0c2139SMatthew G Knepley           if (val == 1) {
16249566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
162563a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16264c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep + 1];
1627cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
16282582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1629cd0c2139SMatthew G Knepley           }
1630cd0c2139SMatthew G Knepley         }
16314c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
16329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16334c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1634cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1635cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
16369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
16374c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
16384c367dbcSMatthew G. Knepley           PetscInt val, edge;
16394c367dbcSMatthew G. Knepley 
16409566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16414c367dbcSMatthew G. Knepley           if (val == 1) {
16429566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
164363a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16444c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
16454c367dbcSMatthew G. Knepley           }
16464c367dbcSMatthew G. Knepley         }
16479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1648cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
16494c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16504c367dbcSMatthew G. Knepley 
1651cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1652cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1653cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
16549566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
1655e1757548SMatthew G. Knepley           if (v < 0) {
16569566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
165763a3b9bcSJacob 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);
16582582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1659e1757548SMatthew G. Knepley           } else {
16604c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep - 1];
1661cd0c2139SMatthew G Knepley           }
1662e1757548SMatthew G. Knepley         }
16639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1664cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1665cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1666cd0c2139SMatthew G Knepley           PetscInt val;
1667cd0c2139SMatthew G Knepley 
16689566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16694c367dbcSMatthew G. Knepley           if (val == dim - 1) {
16702582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
16714c367dbcSMatthew G. Knepley           } else if (val == (shift + dim - 1)) {
16722582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1673cd0c2139SMatthew G Knepley           }
1674cd0c2139SMatthew G Knepley         }
1675b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1677cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1678cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1679cd0c2139SMatthew G Knepley           PetscInt val, face;
1680cd0c2139SMatthew G Knepley 
16819566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1682cd0c2139SMatthew G Knepley           if (val == dim - 1) {
16839566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
168463a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
16854c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep + 1];
1686cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim - 1)) {
16872582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1688cd0c2139SMatthew G Knepley           }
1689cd0c2139SMatthew G Knepley         }
1690b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16924c367dbcSMatthew G. Knepley         /* Hybrid face */
16934c367dbcSMatthew G. Knepley         coneNew[0] = newp;
16944c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
16954c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
16964c367dbcSMatthew G. Knepley           PetscInt vertex;
16979566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex));
1698e1757548SMatthew G. Knepley           if (vertex < 0) {
16999566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex));
170063a3b9bcSJacob 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);
1701e1757548SMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
1702e1757548SMatthew G. Knepley           } else {
17034c367dbcSMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
17044c367dbcSMatthew G. Knepley           }
1705e1757548SMatthew G. Knepley         }
17069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
17074c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
17084c367dbcSMatthew G. Knepley           PetscInt val, face;
17094c367dbcSMatthew G. Knepley 
17109566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
17114c367dbcSMatthew G. Knepley           if (val == dim - 1) {
17129566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
171363a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
17144c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
17154c367dbcSMatthew G. Knepley           }
17164c367dbcSMatthew G. Knepley         }
17179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1718cd0c2139SMatthew G Knepley       }
1719cd0c2139SMatthew G Knepley     }
1720cd0c2139SMatthew G Knepley   }
172118c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
172218c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
172318c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
17242582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1725b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1726e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
172718c5995bSMatthew G. Knepley 
17289566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
17299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
17309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
17319566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
173218c5995bSMatthew G. Knepley       if (dep == 0) {
173318c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
173418c5995bSMatthew G. Knepley 
173518c5995bSMatthew G. Knepley         /* Unsplit vertex */
17369566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
173718c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
17382582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
17399566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
1740ad540459SPierre Jolivet           if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1];
174118c5995bSMatthew G. Knepley         }
174218c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174318c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174463a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
17459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
174618c5995bSMatthew G. Knepley         /* Hybrid edge */
174718c5995bSMatthew G. Knepley         coneNew[0] = newp;
174818c5995bSMatthew G. Knepley         coneNew[1] = newp;
17499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
175018c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
175118c5995bSMatthew G. Knepley           PetscInt val, edge;
175218c5995bSMatthew G. Knepley 
17539566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
175418c5995bSMatthew G. Knepley           if (val == 1) {
17559566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
175663a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
175718c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1758e1757548SMatthew G. Knepley           } else if (val == (shift2 + 1)) {
17599566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge));
176063a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1761e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1];
176218c5995bSMatthew G. Knepley           }
176318c5995bSMatthew G. Knepley         }
17649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1765e1757548SMatthew G. Knepley       } else if (dep == dim - 2) {
1766e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1767e1757548SMatthew G. Knepley 
1768da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1769e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1770e1757548SMatthew G. Knepley           PetscInt val, face;
1771e1757548SMatthew G. Knepley 
17729566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1773e1757548SMatthew G. Knepley           if (val == dim - 1) {
17749566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
177563a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
17762582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1777e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 1];
1778e1757548SMatthew G. Knepley           } else {
17792582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1780e1757548SMatthew G. Knepley           }
1781e1757548SMatthew G. Knepley         }
1782e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1783e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
17849566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
178563a3b9bcSJacob 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);
17869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1787e1757548SMatthew G. Knepley         /* Add hybrid face */
1788e1757548SMatthew G. Knepley         coneNew[0] = newp;
1789212cc919SMatthew G. Knepley         coneNew[1] = newp;
17909566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
179163a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1792212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17939566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
179463a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]);
1795e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1797da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1798da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1799da1dd7e4SMatthew G. Knepley 
18009566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1801da1dd7e4SMatthew G. Knepley           if (val == dim - 1) {
18029566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
1803da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1804da1dd7e4SMatthew G. Knepley           }
1805da1dd7e4SMatthew G. Knepley         }
18069566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
180763a3b9bcSJacob 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);
18089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1809cd0c2139SMatthew G Knepley       }
1810cd0c2139SMatthew G Knepley     }
1811cd0c2139SMatthew G Knepley   }
1812cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1813cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1814cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1815cd0c2139SMatthew G Knepley     IS              pIS;
1816cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1817cd0c2139SMatthew G Knepley     const PetscInt *points;
1818cd0c2139SMatthew G Knepley 
1819cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
18209566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1821cd0c2139SMatthew G Knepley     if (!pIS) continue;
1822cd0c2139SMatthew G Knepley     dep = -dep - shift;
18239566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
18249566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1825cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1826cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
18272582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1828cd0c2139SMatthew G Knepley       const PetscInt *cone;
1829cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
183050cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1831cd0c2139SMatthew G Knepley 
1832cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1833cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
18349566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
18359566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1836cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1837e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift);
1838cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1839cd0c2139SMatthew G Knepley 
18409566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1841cd0c2139SMatthew G Knepley         if (val == dep - 1) {
18429566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp));
184363a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1);
1844cd0c2139SMatthew G Knepley           csplitp = pMaxNew[dep - 1] + cp;
18459566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
184650cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1847cd0c2139SMatthew G Knepley         }
1848cd0c2139SMatthew G Knepley       }
18494a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
185028b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1851cd0c2139SMatthew G Knepley     }
18529566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
18539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1854cd0c2139SMatthew G Knepley   }
18550e18dc48SMatthew G. Knepley   PetscCall(DMPlexReorderCohesiveSupports(sdm));
1856fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
18579566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
18589566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
18599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
18609566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1861cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
18622582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1863cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1864cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1865cd0c2139SMatthew G Knepley 
18669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
18679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
18689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1869cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d];
1870cd0c2139SMatthew G Knepley   }
18719566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1872fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
18739566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1874b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1875fa8e8ae5SToby Isaac   /* Step 9: Labels */
18769566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
18779566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
18789566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1879cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1880cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
18812582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1882cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1883cd0c2139SMatthew G Knepley       PetscInt       l;
1884cd0c2139SMatthew G Knepley 
18857db7e0a7SMatthew G. Knepley       if (splitLabel) {
18867db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
18877db7e0a7SMatthew G. Knepley 
18889566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp, val));
18899566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
18907db7e0a7SMatthew G. Knepley       }
1891cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1892cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1893cd0c2139SMatthew G Knepley         const char *lname;
1894cd0c2139SMatthew G Knepley         PetscInt    val;
18959a356370SMatthew G. Knepley         PetscBool   isDepth;
1896cd0c2139SMatthew G Knepley 
18979566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
18989566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
18999a356370SMatthew G. Knepley         if (isDepth) continue;
19009566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
19019566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
190248a46eb9SPierre Jolivet         if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val));
1903cd0c2139SMatthew G Knepley       }
1904cd0c2139SMatthew G Knepley     }
1905cd0c2139SMatthew G Knepley   }
1906cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1907cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1908cd0c2139SMatthew G Knepley 
1909cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
19109566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
19119566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
19129566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
19139566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1914cd0c2139SMatthew G Knepley   }
1915b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1916b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1917cd0c2139SMatthew G Knepley   if (label) {
19189566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
19199566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1920cd0c2139SMatthew G Knepley   }
19210d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
19229566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
192336dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
19240d4d4d06SMatthew G. Knepley   }
19259566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
19269566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
19279566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
19283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1929cd0c2139SMatthew G Knepley }
1930cd0c2139SMatthew G Knepley 
1931cd0c2139SMatthew G Knepley /*@C
1932cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1933cd0c2139SMatthew G Knepley 
193420f4b53cSBarry Smith   Collective
1935cd0c2139SMatthew G Knepley 
1936cd0c2139SMatthew G Knepley   Input Parameters:
193720f4b53cSBarry Smith + dm    - The original `DM`
193820f4b53cSBarry Smith - label - The `DMLabel` specifying the boundary faces (this could be auto-generated)
1939cd0c2139SMatthew G Knepley 
1940cd0c2139SMatthew G Knepley   Output Parameters:
194120f4b53cSBarry Smith + splitLabel - The `DMLabel` containing the split points, or `NULL` if no output is desired
194220f4b53cSBarry Smith - dmSplit    - The new `DM`
1943cd0c2139SMatthew G Knepley 
1944cd0c2139SMatthew G Knepley   Level: developer
1945cd0c2139SMatthew G Knepley 
19461cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1947cd0c2139SMatthew G Knepley @*/
1948d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1949d71ae5a4SJacob Faibussowitsch {
1950cd0c2139SMatthew G Knepley   DM       sdm;
1951cd0c2139SMatthew G Knepley   PetscInt dim;
1952cd0c2139SMatthew G Knepley 
1953cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1954cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19554f572ea9SToby Isaac   PetscAssertPointer(dmSplit, 4);
19569566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
19579566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
19589566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19599566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1960cd0c2139SMatthew G Knepley   switch (dim) {
1961cd0c2139SMatthew G Knepley   case 2:
1962d71ae5a4SJacob Faibussowitsch   case 3:
1963d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1964d71ae5a4SJacob Faibussowitsch     break;
1965d71ae5a4SJacob Faibussowitsch   default:
1966d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1967cd0c2139SMatthew G Knepley   }
1968cd0c2139SMatthew G Knepley   *dmSplit = sdm;
19693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1970cd0c2139SMatthew G Knepley }
1971cd0c2139SMatthew G Knepley 
19720f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
1973d71ae5a4SJacob Faibussowitsch static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1974d71ae5a4SJacob Faibussowitsch {
19750f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
19760f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
19770f66a230SMatthew G. Knepley 
19780f66a230SMatthew G. Knepley   PetscFunctionBegin;
19790f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
19809566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19829566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
19839566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
19840f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
19850f66a230SMatthew G. Knepley     if (cone[c] == face) {
19860f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
19870f66a230SMatthew G. Knepley 
19880f66a230SMatthew G. Knepley       if (subdm) {
19890f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
19900f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
19910f66a230SMatthew G. Knepley 
19929566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
19939566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
19949566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
19959566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
19969566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
19970f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
19980f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
19990f66a230SMatthew G. Knepley             o = subornt[0];
20000f66a230SMatthew G. Knepley             break;
20010f66a230SMatthew G. Knepley           }
20020f66a230SMatthew G. Knepley         }
200363a3b9bcSJacob 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);
20040f66a230SMatthew G. Knepley       }
20050f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
20060f66a230SMatthew G. Knepley       else *pos = PETSC_FALSE;
20070f66a230SMatthew G. Knepley       break;
20080f66a230SMatthew G. Knepley     }
20090f66a230SMatthew G. Knepley   }
201063a3b9bcSJacob 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);
20113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20120f66a230SMatthew G. Knepley }
20130f66a230SMatthew G. Knepley 
20140542aa8cSMatthew G. Knepley static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label, PetscBool split)
2015d71ae5a4SJacob Faibussowitsch {
2016accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
2017accc9626SMatthew G. Knepley   const PetscInt *points;
20180542aa8cSMatthew G. Knepley   PetscInt       *closure = NULL, *inclosure = NULL;
20190542aa8cSMatthew G. Knepley   PetscInt        dim, numPoints, shift = 100, shift2 = 200, debug = 0;
2020accc9626SMatthew G. Knepley 
2021accc9626SMatthew G. Knepley   PetscFunctionBegin;
2022accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
20230542aa8cSMatthew G. Knepley   // If any faces touching the fault divide cells on either side,
20240542aa8cSMatthew G. Knepley   //   either split them, or unsplit the connection
2025accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
2026accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
2027accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
2028accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
2029accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
20303ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2031accc9626SMatthew G. Knepley   }
2032accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
2033accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
2034accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
2035accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
2036accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
20370542aa8cSMatthew G. Knepley   for (PetscInt p = 0; p < numPoints; ++p) {
2038accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
2039accc9626SMatthew G. Knepley     const PetscInt *support;
2040accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
2041accc9626SMatthew G. Knepley 
2042accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
2043accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
2044accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
2045accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
2046accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
2047accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
2048accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
20490542aa8cSMatthew G. Knepley     // Check that this face is not incident on only unsplit faces,
20500542aa8cSMatthew G. Knepley     //   meaning has at least one split face
2051e1a13daeSMatthew G. Knepley     {
2052e1a13daeSMatthew G. Knepley       PetscBool split = PETSC_FALSE;
20530542aa8cSMatthew G. Knepley       PetscInt  Ncl, val;
2054e1a13daeSMatthew G. Knepley 
20550542aa8cSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &Ncl, &closure));
20560542aa8cSMatthew G. Knepley       for (PetscInt cl = 0; cl < Ncl * 2; cl += 2) {
20570542aa8cSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &val));
20580542aa8cSMatthew G. Knepley         if ((val >= 0) && (val <= dim)) {
20599371c9d4SSatish Balay           split = PETSC_TRUE;
20609371c9d4SSatish Balay           break;
20619371c9d4SSatish Balay         }
2062e1a13daeSMatthew G. Knepley       }
2063e1a13daeSMatthew G. Knepley       if (!split) continue;
2064e1a13daeSMatthew G. Knepley     }
20650542aa8cSMatthew G. Knepley     if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Point %" PetscInt_FMT " is impinging (%" PetscInt_FMT ":%" PetscInt_FMT ", %" PetscInt_FMT ":%" PetscInt_FMT ")\n", point, support[0], valA, support[1], valB));
20660542aa8cSMatthew G. Knepley     if (split) {
20670542aa8cSMatthew G. Knepley       // Split the face
2068accc9626SMatthew G. Knepley       PetscCall(DMLabelGetValue(label, point, &valA));
2069accc9626SMatthew G. Knepley       PetscCall(DMLabelClearValue(label, point, valA));
2070accc9626SMatthew G. Knepley       PetscCall(DMLabelSetValue(label, point, dim - 1));
2071accc9626SMatthew G. Knepley       /* Label its closure:
2072accc9626SMatthew G. Knepley         unmarked: label as unsplit
2073accc9626SMatthew G. Knepley         incident: relabel as split
20740542aa8cSMatthew G. Knepley         split:    do nothing */
2075accc9626SMatthew G. Knepley       {
2076accc9626SMatthew G. Knepley         PetscInt closureSize, cl, dep;
2077accc9626SMatthew G. Knepley 
2078accc9626SMatthew G. Knepley         PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2079accc9626SMatthew G. Knepley         for (cl = 0; cl < closureSize * 2; cl += 2) {
2080accc9626SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, closure[cl], &valA));
2081accc9626SMatthew G. Knepley           if (valA == -1) { /* Mark as unsplit */
2082accc9626SMatthew G. Knepley             PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2083accc9626SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
2084accc9626SMatthew G. Knepley           } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2085accc9626SMatthew G. Knepley             PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2086accc9626SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, closure[cl], valA));
2087accc9626SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, closure[cl], dep));
2088accc9626SMatthew G. Knepley           }
2089accc9626SMatthew G. Knepley         }
20900542aa8cSMatthew G. Knepley       }
20910542aa8cSMatthew G. Knepley     } else {
20920542aa8cSMatthew G. Knepley       // Unsplit the incident faces and their closures
20930542aa8cSMatthew G. Knepley       PetscInt Ncl, dep, val;
20940542aa8cSMatthew G. Knepley 
20950542aa8cSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &Ncl, &closure));
20960542aa8cSMatthew G. Knepley       for (PetscInt cl = 0; cl < Ncl * 2; cl += 2) {
20970542aa8cSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &val));
20980542aa8cSMatthew G. Knepley         if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Point %" PetscInt_FMT ":%" PetscInt_FMT "\n", closure[cl], val));
20990542aa8cSMatthew G. Knepley         if ((val >= 0) && (val <= dim)) {
21000542aa8cSMatthew G. Knepley           PetscInt Nincl, inval, indep;
21010542aa8cSMatthew G. Knepley 
21020542aa8cSMatthew G. Knepley           if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Point %" PetscInt_FMT " is being unsplit\n", closure[cl]));
21030542aa8cSMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
21040542aa8cSMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], val));
21050542aa8cSMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
21060542aa8cSMatthew G. Knepley 
21070542aa8cSMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, closure[cl], PETSC_TRUE, &Nincl, &inclosure));
21080542aa8cSMatthew G. Knepley           for (PetscInt incl = 0; incl < Nincl * 2; incl += 2) {
21090542aa8cSMatthew G. Knepley             PetscCall(DMLabelGetValue(label, inclosure[cl], &inval));
21100542aa8cSMatthew G. Knepley             if ((inval >= 0) && (inval <= dim)) {
21110542aa8cSMatthew G. Knepley               if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "    Point %" PetscInt_FMT " is being unsplit\n", inclosure[incl]));
21120542aa8cSMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, inclosure[incl], &indep));
21130542aa8cSMatthew G. Knepley               PetscCall(DMLabelClearValue(label, inclosure[incl], inval));
21140542aa8cSMatthew G. Knepley               PetscCall(DMLabelSetValue(label, inclosure[incl], shift2 + indep));
2115accc9626SMatthew G. Knepley             }
2116accc9626SMatthew G. Knepley           }
21170542aa8cSMatthew G. Knepley         }
21180542aa8cSMatthew G. Knepley       }
21190542aa8cSMatthew G. Knepley     }
21200542aa8cSMatthew G. Knepley   }
21210542aa8cSMatthew G. Knepley   PetscCall(DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &inclosure));
21220542aa8cSMatthew G. Knepley   PetscCall(DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure));
2123accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2124accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
21253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2126accc9626SMatthew G. Knepley }
2127accc9626SMatthew G. Knepley 
2128cd0c2139SMatthew G Knepley /*@
21290f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
2130cd0c2139SMatthew G Knepley   to complete the surface
2131cd0c2139SMatthew G Knepley 
2132cd0c2139SMatthew G Knepley   Input Parameters:
213320f4b53cSBarry Smith + dm     - The `DM`
213420f4b53cSBarry Smith . label  - A `DMLabel` marking the surface
213520f4b53cSBarry Smith . blabel - A `DMLabel` marking the vertices on the boundary which will not be duplicated, or `NULL` to find them automatically
213620f4b53cSBarry Smith . bvalue - Value of `DMLabel` marking the vertices on the boundary
2137bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
21380542aa8cSMatthew G. Knepley . split  - Split faces impinging on the surface, rather than clamping the surface boundary
213920f4b53cSBarry Smith - subdm  - The `DM` associated with the label, or `NULL`
2140cd0c2139SMatthew G Knepley 
2141cd0c2139SMatthew G Knepley   Output Parameter:
214220f4b53cSBarry Smith . label - A `DMLabel` marking all surface points
21430f66a230SMatthew G. Knepley 
2144cd0c2139SMatthew G Knepley   Level: developer
2145cd0c2139SMatthew G Knepley 
214620f4b53cSBarry Smith   Note:
214720f4b53cSBarry Smith   The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
214820f4b53cSBarry Smith 
21491cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2150cd0c2139SMatthew G Knepley @*/
21510542aa8cSMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, PetscBool split, DM subdm)
2152d71ae5a4SJacob Faibussowitsch {
2153d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2154accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
215547946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2156bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
21570542aa8cSMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = split ? 300 : 0, dim, depth, numPoints, numSubpoints, p, val;
2158cd0c2139SMatthew G Knepley 
2159cd0c2139SMatthew G Knepley   PetscFunctionBegin;
21609566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
21619566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
21629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
216347946fd8SMatthew G. Knepley   if (subdm) {
21649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
216547946fd8SMatthew G. Knepley     if (subpointIS) {
21669566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
21679566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
216847946fd8SMatthew G. Knepley     }
216947946fd8SMatthew G. Knepley   }
2170d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
21719566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2172accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
21739566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
21749566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2175d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2176cd0c2139SMatthew G Knepley     const PetscInt *support;
2177cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2178cd0c2139SMatthew G Knepley 
21799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2180c4419245SMatthew G. Knepley #if 0
2181c4419245SMatthew G. Knepley     if (supportSize != 2) {
2182c4419245SMatthew G. Knepley       const PetscInt *lp;
2183c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2184c4419245SMatthew G. Knepley 
2185c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2186c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
21879566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
21889566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
218963a3b9bcSJacob 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);
2190c4419245SMatthew G. Knepley     }
2191c4419245SMatthew G. Knepley #endif
21929566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2193cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
21940f66a230SMatthew G. Knepley       const PetscInt *cone;
2195cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
21960f66a230SMatthew G. Knepley       PetscBool       pos;
2197cd0c2139SMatthew G Knepley 
21989566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
21999566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
22009566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
22010f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
22020f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
22039566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
22049566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2205cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2206cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2207cd0c2139SMatthew G Knepley 
22089566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2209cd0c2139SMatthew G Knepley         if (val == -1) {
2210cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2211cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2212cd0c2139SMatthew G Knepley 
22139566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2214cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2215cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2216a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2217cd0c2139SMatthew G Knepley 
22189566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
22199566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2220a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim - 1) && (bval < 0)) {
22219566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2222cd0c2139SMatthew G Knepley               break;
2223cd0c2139SMatthew G Knepley             }
2224cd0c2139SMatthew G Knepley           }
22259566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2226cd0c2139SMatthew G Knepley         }
2227cd0c2139SMatthew G Knepley       }
2228cd0c2139SMatthew G Knepley     }
2229cd0c2139SMatthew G Knepley   }
2230accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2231accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2232a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
223386200784SMatthew G. Knepley   if (blabel) {
2234accc9626SMatthew G. Knepley     IS bdIS;
2235accc9626SMatthew G. Knepley 
2236caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2237accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2238accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2239a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2240a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2241a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2242a0541d8aSMatthew G. Knepley 
22439566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2244a0541d8aSMatthew G. Knepley       if (bval >= 0) {
22459566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2246f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2247f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
22489566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2249f7019248SMatthew G. Knepley         }
2250f7019248SMatthew G. Knepley       }
2251f7019248SMatthew G. Knepley     }
2252f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2253f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2254f7019248SMatthew G. Knepley       PetscInt       val, bval;
2255f7019248SMatthew G. Knepley 
22569566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2257f7019248SMatthew G. Knepley       if (bval >= 0) {
225886200784SMatthew G. Knepley         const PetscInt *cone, *support;
225986200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
226086200784SMatthew G. Knepley 
2261a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
22629566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2263e1a13daeSMatthew 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);
22649566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
22659566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
22662c06a818SMatthew G. Knepley         /* Check for cross-edge
22672c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
226886200784SMatthew G. Knepley         if (val != 0) continue;
22699566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
22709566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
227186200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
22729566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
22739566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
227463a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
22759566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
22769566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
22779566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
22789566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
227986200784SMatthew G. Knepley         }
2280a0541d8aSMatthew G. Knepley       }
2281a0541d8aSMatthew G. Knepley     }
2282accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2283accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2284a0541d8aSMatthew G. Knepley   }
2285b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2286b6dfa339SMatthew G. Knepley   {
2287b6dfa339SMatthew G. Knepley     PetscSF         sf;
2288b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2289b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2290b6dfa339SMatthew G. Knepley 
2291b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2292b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2293accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2294accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2295accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2296accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2297b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2298b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2299b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2300b6dfa339SMatthew G. Knepley         PetscInt       val;
2301b6dfa339SMatthew G. Knepley 
2302b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2303b6dfa339SMatthew G. Knepley         if (l >= 0) {
2304b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2305b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2306b6dfa339SMatthew G. Knepley 
2307b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2308b6dfa339SMatthew 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);
2309b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2310b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2311b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2312accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2313b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2314b6dfa339SMatthew G. Knepley 
2315b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2316b6dfa339SMatthew 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);
2317b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2318b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2319b6dfa339SMatthew G. Knepley           }
2320b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2321b6dfa339SMatthew G. Knepley         }
2322b6dfa339SMatthew G. Knepley       }
2323b6dfa339SMatthew G. Knepley     }
2324b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2325b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2326accc9626SMatthew G. Knepley   }
2327accc9626SMatthew G. Knepley divide:
2328b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2329accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
23300542aa8cSMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label, split));
23313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2332cd0c2139SMatthew G Knepley }
2333cd0c2139SMatthew G Knepley 
2334720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
233566976f2fSJacob Faibussowitsch static PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2336d71ae5a4SJacob Faibussowitsch {
2337720e594eSMatthew G. Knepley   IS              subpointIS;
2338720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2339720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2340720e594eSMatthew G. Knepley 
2341720e594eSMatthew G. Knepley   PetscFunctionBegin;
23423ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
23439566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
23449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
23453ba16761SJacob Faibussowitsch   if (!subpointIS) PetscFunctionReturn(PETSC_SUCCESS);
23469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
23479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
23489566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2349720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2350720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2351720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2352720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2353720e594eSMatthew G. Knepley 
23549566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2355720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2356720e594eSMatthew G. Knepley       PetscInt value = 0;
2357720e594eSMatthew G. Knepley 
2358720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
23599566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
23609371c9d4SSatish Balay       if (value == defaultValue) {
23619371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
23629371c9d4SSatish Balay         break;
23639371c9d4SSatish Balay       }
2364720e594eSMatthew G. Knepley     }
23659566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2366720e594eSMatthew G. Knepley     if (invalidCell) {
23679566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23689566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
23699566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
237063a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2371720e594eSMatthew G. Knepley     }
2372720e594eSMatthew G. Knepley   }
23739566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2375720e594eSMatthew G. Knepley }
2376720e594eSMatthew G. Knepley 
2377c08575a3SMatthew G. Knepley /*@
23783cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
23793cf72582SMatthew G. Knepley 
238020f4b53cSBarry Smith   Collective
23813cf72582SMatthew G. Knepley 
23823cf72582SMatthew G. Knepley   Input Parameters:
238320f4b53cSBarry Smith + dm      - The original `DM`
2384720e594eSMatthew G. Knepley . label   - The label specifying the interface vertices
2385caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2386caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
23873cf72582SMatthew G. Knepley 
23883cf72582SMatthew G. Knepley   Output Parameters:
238920f4b53cSBarry Smith + hybridLabel - The label fully marking the interface, or `NULL` if no output is desired
239020f4b53cSBarry Smith . splitLabel  - The label containing the split points, or `NULL` if no output is desired
239120f4b53cSBarry Smith . dmInterface - The new interface `DM`, or `NULL`
239220f4b53cSBarry Smith - dmHybrid    - The new `DM` with cohesive cells
23933cf72582SMatthew G. Knepley 
239420f4b53cSBarry Smith   Level: developer
239520f4b53cSBarry Smith 
239620f4b53cSBarry Smith   Note:
239720f4b53cSBarry Smith   The hybridLabel indicates what parts of the original mesh impinged on the division surface. For points
23986eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
23996eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
24006eccb800SMatthew 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
24016eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
24026eccb800SMatthew Knepley 
24036eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
240440cdb708SMatthew 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
24056eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
24066eccb800SMatthew Knepley 
240720f4b53cSBarry Smith   The dmInterface is a `DM` built from the original division surface. It has a label which can be retrieved using
240820f4b53cSBarry Smith   `DMPlexGetSubpointMap()` which maps each point back to the point in the surface of the original mesh.
24096eccb800SMatthew Knepley 
24101cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
24113cf72582SMatthew G. Knepley @*/
2412d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
2413d71ae5a4SJacob Faibussowitsch {
24143cf72582SMatthew G. Knepley   DM       idm;
24157db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
24163cf72582SMatthew G. Knepley   PetscInt dim;
24173cf72582SMatthew G. Knepley 
24183cf72582SMatthew G. Knepley   PetscFunctionBegin;
24193cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24204f572ea9SToby Isaac   if (label) PetscAssertPointer(label, 2);
24214f572ea9SToby Isaac   if (bdlabel) PetscAssertPointer(bdlabel, 3);
24224f572ea9SToby Isaac   if (hybridLabel) PetscAssertPointer(hybridLabel, 5);
24234f572ea9SToby Isaac   if (splitLabel) PetscAssertPointer(splitLabel, 6);
24244f572ea9SToby Isaac   if (dmInterface) PetscAssertPointer(dmInterface, 7);
24254f572ea9SToby Isaac   PetscAssertPointer(dmHybrid, 8);
24269566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24279566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
24289566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
24299566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
24309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
24319566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
24329566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
24337db7e0a7SMatthew G. Knepley   if (splitLabel) {
24347db7e0a7SMatthew G. Knepley     const char *name;
24357db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
24367db7e0a7SMatthew G. Knepley 
24379566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
2438c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(sname, name, sizeof(sname)));
2439c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(sname, " split", sizeof(sname)));
24409566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
24417db7e0a7SMatthew G. Knepley   }
24420542aa8cSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, PETSC_TRUE, idm));
24439371c9d4SSatish Balay   if (dmInterface) {
24449371c9d4SSatish Balay     *dmInterface = idm;
24459371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
24469566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
24475de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
24483cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
24499566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
24507db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
24514a7ee7d0SMatthew G. Knepley   {
24524a7ee7d0SMatthew G. Knepley     DM      cdm;
24534a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
24544a7ee7d0SMatthew G. Knepley 
24554a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
24569566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
24579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
24589566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
24594a7ee7d0SMatthew G. Knepley   }
24603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2461cd0c2139SMatthew G Knepley }
2462cd0c2139SMatthew G Knepley 
2463efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2464efa14ee0SMatthew G Knepley 
2465efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2466efa14ee0SMatthew G Knepley */
2467d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2468d71ae5a4SJacob Faibussowitsch {
2469fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2470efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2471412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2472efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2473efa14ee0SMatthew G Knepley 
2474efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2475efa14ee0SMatthew G Knepley   *numFaces = 0;
2476efa14ee0SMatthew G Knepley   *nFV      = 0;
24779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24789566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
247977d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
24809566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
248148a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2482efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
24839566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2484efa14ee0SMatthew G Knepley   if (subvertexIS) {
24859566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24869566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2487efa14ee0SMatthew G Knepley   }
2488efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2489efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24900298fd71SBarry Smith     PetscInt      *star   = NULL;
2491efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2492efa14ee0SMatthew G Knepley 
24939566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2494efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2495efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2496efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2497efa14ee0SMatthew G Knepley     }
2498efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2499efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
25000298fd71SBarry Smith       PetscInt      *closure = NULL;
2501efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2502efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2503efa14ee0SMatthew G Knepley 
25049566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
250565560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
250663a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
25079566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2508efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2509efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2510efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2511efa14ee0SMatthew G Knepley 
2512efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2513efa14ee0SMatthew G Knepley           ++numCorners;
25149566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2515830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2516efa14ee0SMatthew G Knepley         }
2517efa14ee0SMatthew G Knepley       }
25184ad8454bSPierre Jolivet       if (!*nFV) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
251963a3b9bcSJacob 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);
2520efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2521007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2522007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2523007baee2SMatthew G. Knepley 
2524efa14ee0SMatthew G Knepley         ++(*numFaces);
252548a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
25269566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
252748a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
25289566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2529efa14ee0SMatthew G Knepley       }
25309566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2531efa14ee0SMatthew G Knepley     }
25329566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2533efa14ee0SMatthew G Knepley   }
253448a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25359566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25369566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2538efa14ee0SMatthew G Knepley }
2539efa14ee0SMatthew G Knepley 
25405390be7dSMatthew G. Knepley PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, PetscBool addCells, DMLabel subpointMap, DM subdm)
2541d71ae5a4SJacob Faibussowitsch {
254234b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2543efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2544412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2545efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2546efa14ee0SMatthew G Knepley 
2547efa14ee0SMatthew G Knepley   PetscFunctionBegin;
25489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25499566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
255048a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2551efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
255234b4c39eSMatthew G. Knepley   if (vertexLabel) {
25539566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2554efa14ee0SMatthew G Knepley     if (subvertexIS) {
25559566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
25569566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2557efa14ee0SMatthew G Knepley     }
255834b4c39eSMatthew G. Knepley   }
2559efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2560efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
25610298fd71SBarry Smith     PetscInt      *star   = NULL;
2562efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2563efa14ee0SMatthew G Knepley 
25649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2565efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2566efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2567158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2568158acfadSMatthew G. Knepley 
2569158acfadSMatthew G. Knepley       if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) {
2570158acfadSMatthew G. Knepley         if (markedFaces) {
25719566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2572158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2573158acfadSMatthew G. Knepley         }
2574158acfadSMatthew G. Knepley         star[numFaces++] = point;
2575158acfadSMatthew G. Knepley       }
2576efa14ee0SMatthew G Knepley     }
2577efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2578efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
25790298fd71SBarry Smith       PetscInt      *closure = NULL;
2580efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2581efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2582efa14ee0SMatthew G Knepley 
25839566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2584efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
258563a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
25869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2587efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2588efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2589efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2590efa14ee0SMatthew G Knepley 
2591efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
25929566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2593830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2594efa14ee0SMatthew G Knepley         }
2595efa14ee0SMatthew G Knepley       }
2596efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2597efa14ee0SMatthew G Knepley         const PetscInt *support;
2598efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2599efa14ee0SMatthew G Knepley 
2600efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2601efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2602efa14ee0SMatthew G Knepley 
2603efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2604efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
26059566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2606efa14ee0SMatthew G Knepley               break;
2607efa14ee0SMatthew G Knepley             }
2608efa14ee0SMatthew G Knepley           }
2609efa14ee0SMatthew G Knepley         }
26109566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
26119566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
26125390be7dSMatthew G. Knepley         if (addCells)
261348a46eb9SPierre Jolivet           for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2614efa14ee0SMatthew G Knepley       }
26159566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2616efa14ee0SMatthew G Knepley     }
26179566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2618efa14ee0SMatthew G Knepley   }
26199566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
26209566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
26219566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
26223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2623efa14ee0SMatthew G Knepley }
2624efa14ee0SMatthew G Knepley 
2625d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2626d71ae5a4SJacob Faibussowitsch {
262727c04023SMatthew G. Knepley   DMLabel         label = NULL;
2628766ab985SMatthew G. Knepley   const PetscInt *cone;
26299fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2630766ab985SMatthew G. Knepley 
2631812bfc34SJed Brown   PetscFunctionBegin;
2632c0ed958bSJed Brown   *numFaces = 0;
2633c0ed958bSJed Brown   *nFV      = 0;
26349566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2635fed694aaSMatthew G. Knepley   *subCells = NULL;
26369566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26383ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
263927c04023SMatthew G. Knepley   if (label) {
264027c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
264127c04023SMatthew G. Knepley       PetscInt val;
264227c04023SMatthew G. Knepley 
26439566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
264427c04023SMatthew G. Knepley       if (val == value) {
264527c04023SMatthew G. Knepley         ++(*numFaces);
26469566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
264727c04023SMatthew G. Knepley       }
264827c04023SMatthew G. Knepley     }
264927c04023SMatthew G. Knepley   } else {
2650766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
26519566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
265227c04023SMatthew G. Knepley   }
26539566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
26544ad8454bSPierre Jolivet   if (!*numFaces) PetscFunctionReturn(PETSC_SUCCESS);
26559fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2656766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2657766ab985SMatthew G. Knepley     const PetscInt *cells;
2658766ab985SMatthew G. Knepley     PetscInt        numCells;
2659766ab985SMatthew G. Knepley 
266027c04023SMatthew G. Knepley     if (label) {
266127c04023SMatthew G. Knepley       PetscInt val;
266227c04023SMatthew G. Knepley 
26639566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
266427c04023SMatthew G. Knepley       if (val != value) continue;
266527c04023SMatthew G. Knepley     }
26669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
266748a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2668766ab985SMatthew G. Knepley     /* Negative face */
26699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
267027234c99SMatthew G. Knepley     /* Not true in parallel
267108401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2672766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
26739566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
267427234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2675766ab985SMatthew G. Knepley     }
26769566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2677766ab985SMatthew G. Knepley     /* Positive face is not included */
2678766ab985SMatthew G. Knepley   }
26793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2680766ab985SMatthew G. Knepley }
2681766ab985SMatthew G. Knepley 
2682d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2683d71ae5a4SJacob Faibussowitsch {
2684766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2685766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2686766ab985SMatthew G. Knepley 
2687812bfc34SJed Brown   PetscFunctionBegin;
26889566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26899566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26903ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
26919566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
26929566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2693766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2694766ab985SMatthew G. Knepley     const PetscInt *cone;
2695766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2696b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2697766ab985SMatthew G. Knepley 
269827c04023SMatthew G. Knepley     if (label) {
26999566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
270027c04023SMatthew G. Knepley       if (val != value) continue;
270127c04023SMatthew G. Knepley     }
27029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
27039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
27049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
27051dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2706b3154360SMatthew G. Knepley     /* Negative face */
27079566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2708766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2709766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2710766ab985SMatthew G. Knepley 
2711766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2712766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
27139566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2714766ab985SMatthew G. Knepley           break;
2715766ab985SMatthew G. Knepley         }
2716766ab985SMatthew G. Knepley       }
2717766ab985SMatthew G. Knepley     }
27189566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2719766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2720766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2721766ab985SMatthew G. Knepley       const PetscInt *support;
2722766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2723766ab985SMatthew G. Knepley 
27249566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
272508401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
27269566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
272748a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2728766ab985SMatthew G. Knepley     }
2729766ab985SMatthew G. Knepley   }
27309566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
27313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2732766ab985SMatthew G. Knepley }
2733766ab985SMatthew G. Knepley 
2734d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2735d71ae5a4SJacob Faibussowitsch {
273682f516ccSBarry Smith   MPI_Comm       comm;
2737e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2738e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2739e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2740e6ccafaeSMatthew G Knepley 
274182f516ccSBarry Smith   PetscFunctionBegin;
27429566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
27439566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
274463a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2745e6ccafaeSMatthew G Knepley 
2746ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2747ddeab2a6SMatthew G. Knepley     /* Triangle */
2748e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2749e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2750ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2751ddeab2a6SMatthew G. Knepley     /* Triangle */
2752ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2753ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2754ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2755ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2756ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2757ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2758e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2759e6ccafaeSMatthew G Knepley     /* Quadratic line */
2760e6ccafaeSMatthew G Knepley     faceSize  = 1;
2761e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2762e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2763e6ccafaeSMatthew G Knepley     /* Quads */
2764e6ccafaeSMatthew G Knepley     faceSize = 2;
2765e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2766e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2767e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2768e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2769e6ccafaeSMatthew G Knepley     } else {
2770e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2771e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2772e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2773e6ccafaeSMatthew G Knepley     }
2774e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2775e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2776e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2777e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2778e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2779e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2780e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2781e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2782e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2783e6ccafaeSMatthew G Knepley       2, 3, 5, /* left */
2784e6ccafaeSMatthew G Knepley     };
2785e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2786e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2787e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2788e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2789e6ccafaeSMatthew G Knepley     };
2790e6ccafaeSMatthew G Knepley 
2791e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
27929566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2793e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2794e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2795e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2796e6ccafaeSMatthew G Knepley 
27979371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) {
2798e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2799e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2800e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2801e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2802e6ccafaeSMatthew G Knepley               break;
2803e6ccafaeSMatthew G Knepley             }
2804e6ccafaeSMatthew G Knepley           }
2805e6ccafaeSMatthew G Knepley         }
2806e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2807e6ccafaeSMatthew G Knepley         break;
2808e6ccafaeSMatthew G Knepley       }
2809e6ccafaeSMatthew G Knepley     }
281028b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2811e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28123ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2813e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2814e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2815e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2816e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2817e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2818e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2819e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2820e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2821e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2822e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2823e6ccafaeSMatthew G Knepley       0, 3, 7, /* left */
2824e6ccafaeSMatthew G Knepley     };
2825e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2826e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2827e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2828e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2829e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2830e6ccafaeSMatthew G Knepley     };
2831e6ccafaeSMatthew G Knepley 
2832e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
28339566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2834e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2835e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2836e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2837e6ccafaeSMatthew G Knepley 
28389371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) {
2839e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2840e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2841e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2842e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2843e6ccafaeSMatthew G Knepley               break;
2844e6ccafaeSMatthew G Knepley             }
2845e6ccafaeSMatthew G Knepley           }
2846e6ccafaeSMatthew G Knepley         }
2847e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2848e6ccafaeSMatthew G Knepley         break;
2849e6ccafaeSMatthew G Knepley       }
2850e6ccafaeSMatthew G Knepley     }
285128b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2852e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28533ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2854e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2855e6ccafaeSMatthew G Knepley     /* Hexes
2856e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2857e6ccafaeSMatthew G Knepley        pointing up at the second.
2858e6ccafaeSMatthew G Knepley 
2859e6ccafaeSMatthew G Knepley           7---6
2860e6ccafaeSMatthew G Knepley          /|  /|
2861e6ccafaeSMatthew G Knepley         4---5 |
2862ddeab2a6SMatthew G. Knepley         | 1-|-2
2863e6ccafaeSMatthew G Knepley         |/  |/
2864ddeab2a6SMatthew G. Knepley         0---3
2865e6ccafaeSMatthew G Knepley 
2866e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2867e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2868e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2869e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2870e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2871e6ccafaeSMatthew G Knepley       0, 1, 2, 3, /* bottom */
2872e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2873ddeab2a6SMatthew G. Knepley       0, 3, 4, 5, /* front */
2874ddeab2a6SMatthew G. Knepley       2, 3, 5, 6, /* right */
2875ddeab2a6SMatthew G. Knepley       1, 2, 6, 7, /* back */
2876ddeab2a6SMatthew G. Knepley       0, 1, 4, 7, /* left */
2877e6ccafaeSMatthew G Knepley     };
2878e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2879ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2880e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2881ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2882ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2883ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2884ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2885e6ccafaeSMatthew G Knepley     };
2886e6ccafaeSMatthew G Knepley 
2887e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
28889566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2889e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2890e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2891e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2892e6ccafaeSMatthew G Knepley 
28939371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) {
2894e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2895e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2896e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2897e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2898e6ccafaeSMatthew G Knepley               break;
2899e6ccafaeSMatthew G Knepley             }
2900e6ccafaeSMatthew G Knepley           }
2901e6ccafaeSMatthew G Knepley         }
2902e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2903e6ccafaeSMatthew G Knepley         break;
2904e6ccafaeSMatthew G Knepley       }
2905e6ccafaeSMatthew G Knepley     }
290628b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2907e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29083ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2909e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2910e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2911e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2912e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2913e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2914e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2915e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2916e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2917e6ccafaeSMatthew G Knepley       0, 3, 4, 6, 7, 9, /* front */
2918e6ccafaeSMatthew G Knepley       1, 4, 5, 7, 8, 9, /* right */
2919e6ccafaeSMatthew G Knepley       2, 3, 5, 6, 8, 9, /* left */
2920e6ccafaeSMatthew G Knepley     };
2921e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2922e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2923e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2924e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2925e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2926e6ccafaeSMatthew G Knepley     };
2927e6ccafaeSMatthew G Knepley 
2928e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
29299566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2930e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2931e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2932e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2933e6ccafaeSMatthew G Knepley 
29349371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) {
2935e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2936e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2937e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2938e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2939e6ccafaeSMatthew G Knepley               break;
2940e6ccafaeSMatthew G Knepley             }
2941e6ccafaeSMatthew G Knepley           }
2942e6ccafaeSMatthew G Knepley         }
2943e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2944e6ccafaeSMatthew G Knepley         break;
2945e6ccafaeSMatthew G Knepley       }
2946e6ccafaeSMatthew G Knepley     }
294728b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2948e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29493ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2950e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2951e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2952e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2953e6ccafaeSMatthew G Knepley        pointing up at the second.
2954e6ccafaeSMatthew G Knepley 
2955e6ccafaeSMatthew G Knepley          7---6
2956e6ccafaeSMatthew G Knepley         /|  /|
2957e6ccafaeSMatthew G Knepley        4---5 |
2958e6ccafaeSMatthew G Knepley        | 3-|-2
2959e6ccafaeSMatthew G Knepley        |/  |/
2960e6ccafaeSMatthew G Knepley        0---1
2961e6ccafaeSMatthew G Knepley 
2962e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2963e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2964e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2965e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2966e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2967e6ccafaeSMatthew G Knepley       0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2968e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2969e6ccafaeSMatthew G Knepley       0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2970e6ccafaeSMatthew G Knepley       1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2971e6ccafaeSMatthew G Knepley       2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2972e6ccafaeSMatthew G Knepley       0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2973e6ccafaeSMatthew G Knepley     };
2974e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2975e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2976e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2977e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2978e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2979e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2980e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2981e6ccafaeSMatthew G Knepley     };
2982e6ccafaeSMatthew G Knepley 
2983e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
29849566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2985e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2986e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2987e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2988e6ccafaeSMatthew G Knepley 
29899371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) {
2990e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2991e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2992e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2993e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2994e6ccafaeSMatthew G Knepley               break;
2995e6ccafaeSMatthew G Knepley             }
2996e6ccafaeSMatthew G Knepley           }
2997e6ccafaeSMatthew G Knepley         }
2998e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2999e6ccafaeSMatthew G Knepley         break;
3000e6ccafaeSMatthew G Knepley       }
3001e6ccafaeSMatthew G Knepley     }
300228b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
3003e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
30043ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3005e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
3006e6ccafaeSMatthew G Knepley   if (!posOrient) {
30079566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
3008e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
3009e6ccafaeSMatthew G Knepley   } else {
30109566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
3011e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
3012e6ccafaeSMatthew G Knepley   }
3013e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
30143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3015e6ccafaeSMatthew G Knepley }
3016e6ccafaeSMatthew G Knepley 
3017c08575a3SMatthew G. Knepley /*@
3018c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
3019c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
3020c08575a3SMatthew G. Knepley 
302120f4b53cSBarry Smith   Not Collective
3022c08575a3SMatthew G. Knepley 
3023c08575a3SMatthew G. Knepley   Input Parameters:
3024c08575a3SMatthew G. Knepley + dm           - The original mesh
3025c08575a3SMatthew G. Knepley . cell         - The cell mesh point
3026c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
3027c08575a3SMatthew G. Knepley . face         - The face vertices
3028c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
3029c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
3030c08575a3SMatthew G. Knepley - origVertices - Original face vertices
3031c08575a3SMatthew G. Knepley 
3032d8d19677SJose E. Roman   Output Parameters:
3033c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
303420f4b53cSBarry Smith - posOriented  - `PETSC_TRUE` if the face was oriented with outward normal
3035c08575a3SMatthew G. Knepley 
3036c08575a3SMatthew G. Knepley   Level: developer
3037c08575a3SMatthew G. Knepley 
30381cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
3039c08575a3SMatthew G. Knepley @*/
3040d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
3041d71ae5a4SJacob Faibussowitsch {
30420298fd71SBarry Smith   const PetscInt *cone = NULL;
3043e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
3044e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
3045e6ccafaeSMatthew G Knepley 
3046e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
30489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
3049e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
3050e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
3051e6ccafaeSMatthew G Knepley 
3052e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
3053e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
30549371c9d4SSatish Balay         found = PETSC_TRUE;
30559371c9d4SSatish Balay         break;
3056e6ccafaeSMatthew G Knepley       }
3057e6ccafaeSMatthew G Knepley     }
3058e6ccafaeSMatthew G Knepley     if (found) {
3059e6ccafaeSMatthew G Knepley       indices[v2]      = v;
3060e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
3061e6ccafaeSMatthew G Knepley       ++v2;
3062e6ccafaeSMatthew G Knepley     } else {
3063e6ccafaeSMatthew G Knepley       oppositeVertex = v;
3064e6ccafaeSMatthew G Knepley     }
3065e6ccafaeSMatthew G Knepley   }
30669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
30673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3068e6ccafaeSMatthew G Knepley }
3069e6ccafaeSMatthew G Knepley 
3070e6ccafaeSMatthew G Knepley /*
3071cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
3072e6ccafaeSMatthew G Knepley 
307320f4b53cSBarry Smith   Not Collective
3074e6ccafaeSMatthew G Knepley 
3075e6ccafaeSMatthew G Knepley   Input Parameters:
307620f4b53cSBarry Smith   + dm              - The `DMPLEX`
3077e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
307820f4b53cSBarry Smith   . faceVertices    - The vertices in the face for `dm`
3079e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
308020f4b53cSBarry Smith   . numCorners      - The number of vertices in the `cell`
308120f4b53cSBarry Smith   . cell            - A cell in `dm` containing the face
3082e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
3083e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
3084e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
3085e6ccafaeSMatthew G Knepley 
30862fe279fdSBarry Smith   Output Parameter:
3087e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
3088e6ccafaeSMatthew G Knepley 
3089e6ccafaeSMatthew G Knepley   Level: developer
3090e6ccafaeSMatthew G Knepley */
3091d71ae5a4SJacob 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)
3092d71ae5a4SJacob Faibussowitsch {
309382f516ccSBarry Smith   MPI_Comm        comm;
3094e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
3095e6ccafaeSMatthew G Knepley   const PetscInt *faces;
3096e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
3097e6ccafaeSMatthew G Knepley 
3098e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30999566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
31009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
310163a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
3102e6ccafaeSMatthew G Knepley #if 0
3103e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
31049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3105e6ccafaeSMatthew G Knepley #else
3106e6ccafaeSMatthew G Knepley   {
3107e6ccafaeSMatthew G Knepley     PetscInt f;
3108e6ccafaeSMatthew G Knepley 
3109e6ccafaeSMatthew G Knepley     numFaces = 0;
31109566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3111e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
3112e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
3113e6ccafaeSMatthew G Knepley 
31149566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
31159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
3116e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
3117e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
3118e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
3119e6ccafaeSMatthew G Knepley         PetscInt       v;
3120e6ccafaeSMatthew G Knepley 
3121e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
3122e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
3123e6ccafaeSMatthew G Knepley         }
3124e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
3125e6ccafaeSMatthew G Knepley       }
3126e6ccafaeSMatthew G Knepley       if (d == dof) {
3127e6ccafaeSMatthew G Knepley         numFaces               = 1;
3128e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
3129e6ccafaeSMatthew G Knepley       }
3130e6ccafaeSMatthew G Knepley     }
3131e6ccafaeSMatthew G Knepley   }
3132e6ccafaeSMatthew G Knepley #endif
313363a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3134f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3135e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
31369566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3137e6ccafaeSMatthew G Knepley   } else {
3138e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3139e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3140e6ccafaeSMatthew G Knepley 
31419566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3142e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3143e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
3144e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
31459566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3146e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3147e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3148e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3149e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3150e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3151e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3152e6ccafaeSMatthew G Knepley           break;
3153e6ccafaeSMatthew G Knepley         }
3154e6ccafaeSMatthew G Knepley       }
315563a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3156e6ccafaeSMatthew G Knepley     }
31579566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
31589566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
31599566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3160e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3161e6ccafaeSMatthew G Knepley   }
3162ef07cca7SMatthew G. Knepley #if 0
31639566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3164ef07cca7SMatthew G. Knepley #else
31659566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3166ef07cca7SMatthew G. Knepley #endif
31673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3168e6ccafaeSMatthew G Knepley }
3169e6ccafaeSMatthew G Knepley 
3170d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3171d71ae5a4SJacob Faibussowitsch {
317282f516ccSBarry Smith   MPI_Comm        comm;
317353156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3174efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
3175efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3176efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3177fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3178efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3179e6ccafaeSMatthew G Knepley 
3180e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
31819566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3182efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
31839566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
31849566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
31859566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3186efa14ee0SMatthew G Knepley   /* Setup chart */
31879566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
31889566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
31899566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
31909566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3191e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3192e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3193efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
3194e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
31959566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
31969566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
31979566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
31989566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
319948a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
320048a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
32019566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3202d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3203e6ccafaeSMatthew G Knepley   /* Create face cones */
32049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
32059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
32069566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3207e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3208e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3209efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
32100298fd71SBarry Smith     PetscInt      *closure = NULL;
3211efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3212e6ccafaeSMatthew G Knepley 
32139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3214efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3215efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3216e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3217e6ccafaeSMatthew G Knepley 
3218efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3219efa14ee0SMatthew G Knepley         ++numCorners;
32209566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3221efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3222efa14ee0SMatthew G Knepley           closure[faceSize] = point;
322365560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3224e6ccafaeSMatthew G Knepley           ++faceSize;
3225e6ccafaeSMatthew G Knepley         }
3226e6ccafaeSMatthew G Knepley       }
3227e6ccafaeSMatthew G Knepley     }
322863a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
322948a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
32309566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3231e6ccafaeSMatthew G Knepley   }
32329566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
32339566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
32349566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3235e6ccafaeSMatthew G Knepley   /* Build coordinates */
3236efa14ee0SMatthew G Knepley   {
3237efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3238efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3239efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3240285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
324124640c55SToby Isaac     const char  *name;
3242efa14ee0SMatthew G Knepley 
32439566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
32449566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
32459566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
32469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
32479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
32489566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
32499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3250efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3251efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3252efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3253efa14ee0SMatthew G Knepley       PetscInt       dof;
3254efa14ee0SMatthew G Knepley 
32559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32569566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
32579566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3258e6ccafaeSMatthew G Knepley     }
32599566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
32609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
32619566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
32629566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
32639566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
32649566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
32659566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3266830e53efSMatthew G. Knepley     if (coordSize) {
32679566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
32689566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3269efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3270efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3271efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3272efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3273e6ccafaeSMatthew G Knepley 
32749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32759566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
32769566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
32779566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
327863a3b9bcSJacob 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);
3279e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3280e6ccafaeSMatthew G Knepley       }
32819566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
32829566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
32833b399e24SMatthew G. Knepley     }
32849566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
32859566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3286e6ccafaeSMatthew G Knepley   }
3287efa14ee0SMatthew G Knepley   /* Cleanup */
32889566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
32899566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
32909566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
32919566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
32923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3293e6ccafaeSMatthew G Knepley }
3294e6ccafaeSMatthew G Knepley 
3295d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3296d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
3297d71ae5a4SJacob Faibussowitsch {
32983982b651SMatthew G. Knepley   PetscInt       subPoint;
32993982b651SMatthew G. Knepley   PetscErrorCode ierr;
33003982b651SMatthew G. Knepley 
33019371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
33029371c9d4SSatish Balay   if (ierr) return -1;
33033982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
33043982b651SMatthew G. Knepley }
33053982b651SMatthew G. Knepley 
3306d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3307d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[])
3308d71ae5a4SJacob Faibussowitsch {
3309dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3310dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3311dd05d810SMatthew G. Knepley 
3312dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3313dd05d810SMatthew G. Knepley   if (ierr) return -1;
3314dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3315dd05d810SMatthew G. Knepley }
3316dd05d810SMatthew G. Knepley 
3317d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3318d71ae5a4SJacob Faibussowitsch {
3319695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3320212103e5SMatthew Knepley   PetscInt Nl, l, d;
3321212103e5SMatthew Knepley 
3322212103e5SMatthew Knepley   PetscFunctionBegin;
3323695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3324695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3325695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
33269566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3327212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3328212103e5SMatthew Knepley     DMLabel         label, newlabel;
3329212103e5SMatthew Knepley     const char     *lname;
3330d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3331212103e5SMatthew Knepley     IS              valueIS;
3332212103e5SMatthew Knepley     const PetscInt *values;
3333212103e5SMatthew Knepley     PetscInt        Nv, v;
3334212103e5SMatthew Knepley 
33359566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
33369566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
33379566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
33389566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
33399566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3340d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
33419566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
33429566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
33439566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
33449566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
33459566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
33469566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
33479566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
33489566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3349212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3350212103e5SMatthew Knepley       IS              pointIS;
3351212103e5SMatthew Knepley       const PetscInt *points;
3352212103e5SMatthew Knepley       PetscInt        Np, p;
3353212103e5SMatthew Knepley 
33549566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
33559566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
33569566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3357212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3358212103e5SMatthew Knepley         const PetscInt point = points[p];
3359f117cc94SBrad Aagaard         PetscInt       subp, subdepth;
3360212103e5SMatthew Knepley 
33619566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3362f117cc94SBrad Aagaard         PetscCall(DMPlexGetDepth(subdm, &subdepth));
3363f117cc94SBrad Aagaard         if (d > subdepth) continue;
3364212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
33659566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3366212103e5SMatthew Knepley       }
33679566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
33689566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3369212103e5SMatthew Knepley     }
33709566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
33719566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3372212103e5SMatthew Knepley   }
33733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3374212103e5SMatthew Knepley }
3375212103e5SMatthew Knepley 
337630cbcd5dSksagiyam static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, PetscBool ignoreLabelHalo, PetscBool sanitizeSubmesh, PetscSF *ownershipTransferSF, DM subdm)
3377d71ae5a4SJacob Faibussowitsch {
337882f516ccSBarry Smith   MPI_Comm         comm;
337953156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3380efa14ee0SMatthew G Knepley   IS              *subpointIS;
3381efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
33823982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
33833e98c3d0Sksagiyam   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, coordinate_type;
33840d366550SMatthew G. Knepley   PetscMPIInt      rank;
3385e6ccafaeSMatthew G Knepley 
3386e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
33879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
33889566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3389efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
33909566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
33919566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3392bec263e5SMatthew G. Knepley   if (cellHeight) {
33939566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
33945390be7dSMatthew G. Knepley     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, PETSC_TRUE, subpointMap, subdm));
3395bec263e5SMatthew G. Knepley   } else {
3396bec263e5SMatthew G. Knepley     DMLabel         depth;
3397bec263e5SMatthew G. Knepley     IS              pointIS;
3398bec263e5SMatthew G. Knepley     const PetscInt *points;
33994d00ad26Sksagiyam     PetscInt        numPoints = 0, pStart = 0;
34004d00ad26Sksagiyam     PetscBool      *isGhost = NULL;
3401bec263e5SMatthew G. Knepley 
34029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
34039566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3404b85c8bf9SMatthew G. Knepley     if (pointIS) {
34059566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
34069566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3407b85c8bf9SMatthew G. Knepley     }
34084d00ad26Sksagiyam     if (ignoreLabelHalo) {
34094d00ad26Sksagiyam       PetscSF         pointSF;
34104d00ad26Sksagiyam       PetscInt        nleaves, pEnd;
34114d00ad26Sksagiyam       const PetscInt *ilocal = NULL;
34124d00ad26Sksagiyam 
34134d00ad26Sksagiyam       PetscCall(DMGetPointSF(dm, &pointSF));
34144d00ad26Sksagiyam       PetscCall(PetscSFGetGraph(pointSF, NULL, &nleaves, &ilocal, NULL));
34154d00ad26Sksagiyam       PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
34164d00ad26Sksagiyam       PetscCall(PetscMalloc1(pEnd - pStart, &isGhost));
34174d00ad26Sksagiyam       for (p = 0; p < pEnd - pStart; ++p) isGhost[p] = PETSC_FALSE;
34184d00ad26Sksagiyam       for (p = 0; p < nleaves; ++p) isGhost[(ilocal ? ilocal[p] : p) - pStart] = PETSC_TRUE;
34194d00ad26Sksagiyam     }
3420bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3421bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3422bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3423bec263e5SMatthew G. Knepley 
34244d00ad26Sksagiyam       if (ignoreLabelHalo && isGhost[points[p] - pStart]) continue;
34259566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3426bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
34279566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
34289566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3429bec263e5SMatthew G. Knepley       }
34309566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3431bec263e5SMatthew G. Knepley     }
34324d00ad26Sksagiyam     PetscCall(PetscFree(isGhost));
34339566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
34349566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3435bec263e5SMatthew G. Knepley   }
3436efa14ee0SMatthew G Knepley   /* Setup chart */
34379566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3438dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
34399566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3440e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
34419566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3442e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3443e6ccafaeSMatthew G Knepley   }
3444dd05d810SMatthew G. Knepley   // Determine submesh dimension
3445dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3446dd05d810SMatthew G. Knepley   if (sdim > 0) {
3447dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3448dd05d810SMatthew G. Knepley     sdim = dim;
3449dd05d810SMatthew G. Knepley   } else {
3450dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3451dd05d810SMatthew G. Knepley     PetscInt lsdim;
3452dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3453dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3454462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPI_MAX, comm));
3455dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3456dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3457dd05d810SMatthew G. Knepley   }
34589566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
34599566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3460e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3461dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3462dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3463dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3464dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3465dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
34669566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
34679566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3468e6ccafaeSMatthew G Knepley   }
3469412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
34709566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3471dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3472e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3473e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3474e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3475e6ccafaeSMatthew G Knepley       const PetscInt *cone;
347615100a53SVaclav Hapla       PetscInt        coneSize;
3477e6ccafaeSMatthew G Knepley 
34789566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3479dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
348015100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
348115100a53SVaclav Hapla 
34829566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3483e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34849566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3485e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3486e6ccafaeSMatthew G Knepley         }
34879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
34889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
348915100a53SVaclav Hapla       } else {
349015100a53SVaclav Hapla         DMPolytopeType ct;
349115100a53SVaclav Hapla 
349215100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
349315100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
349415100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3495e6ccafaeSMatthew G Knepley       }
3496e6ccafaeSMatthew G Knepley     }
3497e6ccafaeSMatthew G Knepley   }
34989566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
34999566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3500e6ccafaeSMatthew G Knepley   /* Set cones */
35019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
35029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3503dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3504e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3505e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3506e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
35070e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
35080d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3509e6ccafaeSMatthew G Knepley 
3510dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
35110d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
35120d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
35130d366550SMatthew G. Knepley 
35149566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
35159566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
35160d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3517064cae4fSPierre Jolivet           PetscBool isHybrid = PETSC_FALSE;
3518412e9a14SMatthew G. Knepley 
35199566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3520412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
35219566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
35220d366550SMatthew G. Knepley           if (subc >= 0) {
35230d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
35240d366550SMatthew G. Knepley 
35259566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
35269566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
35279566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
35280d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
35290d366550SMatthew G. Knepley               if (cone[c] == point) {
35300d366550SMatthew G. Knepley                 fornt = ornt[c];
35310d366550SMatthew G. Knepley                 break;
35320d366550SMatthew G. Knepley               }
35330d366550SMatthew G. Knepley             }
35340d366550SMatthew G. Knepley             break;
35350d366550SMatthew G. Knepley           }
35360d366550SMatthew G. Knepley         }
35370d366550SMatthew G. Knepley       }
35389566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
35399566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
35409566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
35419566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3542e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
35439566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
354401a2673eSMatthew G. Knepley         if (subc >= 0) {
354501a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
35463982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
354701a2673eSMatthew G. Knepley           ++coneSizeNew;
354801a2673eSMatthew G. Knepley         }
3549e6ccafaeSMatthew G Knepley       }
355063a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
35519566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
35529566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
35539566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3554e6ccafaeSMatthew G Knepley     }
3555e6ccafaeSMatthew G Knepley   }
35569566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
35579566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
35589566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3559e6ccafaeSMatthew G Knepley   /* Build coordinates */
35603e98c3d0Sksagiyam   for (coordinate_type = 0; coordinate_type < 2; ++coordinate_type) {
35613e98c3d0Sksagiyam     DM           coordDM = NULL, subCoordDM = NULL;
35623e98c3d0Sksagiyam     PetscSection coordSection = NULL, subCoordSection = NULL;
35633e98c3d0Sksagiyam     Vec          coordinates = NULL, subCoordinates = NULL;
35643e98c3d0Sksagiyam     PetscScalar *coords = NULL, *subCoords = NULL;
3565*605a06ccSksagiyam     PetscInt     bs, numComp, coordSize, firstP, lastP, firstSubP = totSubPoints, lastSubP = -1, numFields;
356624640c55SToby Isaac     const char  *name;
35673e98c3d0Sksagiyam     PetscBool    localized = (PetscBool)coordinate_type;
3568e6ccafaeSMatthew G Knepley 
35693e98c3d0Sksagiyam     if (!dm->coordinates[coordinate_type].dm) continue;
35703e98c3d0Sksagiyam     if (!localized) {
35713e98c3d0Sksagiyam       PetscCall(DMGetCoordinateDM(dm, &coordDM));
35723e98c3d0Sksagiyam       PetscCall(DMGetCoordinateDM(subdm, &subCoordDM));
35733e98c3d0Sksagiyam     } else {
35743e98c3d0Sksagiyam       {
35753e98c3d0Sksagiyam         PetscInt  localizationHeight;
35763e98c3d0Sksagiyam         PetscBool sparseLocalize;
35773e98c3d0Sksagiyam 
35783e98c3d0Sksagiyam         PetscCall(DMGetSparseLocalize(dm, &sparseLocalize));
35793e98c3d0Sksagiyam         PetscCall(DMSetSparseLocalize(subdm, sparseLocalize));
35803e98c3d0Sksagiyam         PetscCall(DMGetCoordinateDM(dm, &coordDM));
35813e98c3d0Sksagiyam         PetscCall(DMGetCoordinateDM(subdm, &subCoordDM));
35823e98c3d0Sksagiyam         PetscCall(DMPlexGetMaxProjectionHeight(coordDM, &localizationHeight));
35833e98c3d0Sksagiyam         PetscCall(DMPlexSetMaxProjectionHeight(subCoordDM, localizationHeight - (dim - sdim) - cellHeight));
35843e98c3d0Sksagiyam         PetscUseTypeMethod(subdm, createcellcoordinatedm, &subCoordDM);
35853e98c3d0Sksagiyam         PetscCall(DMSetCellCoordinateDM(subdm, subCoordDM));
35863e98c3d0Sksagiyam         PetscCall(DMDestroy(&subCoordDM));
35873e98c3d0Sksagiyam       }
35883e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateDM(dm, &coordDM));
35893e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateDM(subdm, &subCoordDM));
35903e98c3d0Sksagiyam     }
35914c712d99Sksagiyam     PetscCall(DMGetNumFields(coordDM, &numFields));
35924c712d99Sksagiyam     if (numFields > 0) {
35934c712d99Sksagiyam       PetscFE      fe = NULL;
35944c712d99Sksagiyam       PetscSpace   P  = NULL;
35954c712d99Sksagiyam       PetscClassId id;
35964c712d99Sksagiyam       PetscInt     degree;
35974c712d99Sksagiyam 
35984c712d99Sksagiyam       PetscCall(DMGetField(coordDM, 0, NULL, (PetscObject *)&fe));
35994c712d99Sksagiyam       PetscCall(PetscObjectGetClassId((PetscObject)fe, &id));
36004c712d99Sksagiyam       if (id == PETSCFE_CLASSID) {
36014c712d99Sksagiyam         if (sdim == dim && cellHeight == 0) {
36024c712d99Sksagiyam           /* TODO: Handle Field labels correctly */
36034c712d99Sksagiyam           PetscCall(DMSetField(subCoordDM, 0, NULL, (PetscObject)fe));
36044c712d99Sksagiyam           PetscCall(DMCreateDS(subCoordDM));
36054c712d99Sksagiyam         } else {
36064c712d99Sksagiyam           /* TODO: Reconstruct the lower-dimensional FE more robustly */
36074c712d99Sksagiyam           PetscCall(PetscFEGetBasisSpace(fe, &P));
36084c712d99Sksagiyam           PetscCall(PetscSpaceGetDegree(P, &degree, NULL));
3609e65c294aSksagiyam           PetscCall(DMPlexCreateCoordinateSpace(subdm, degree, localized, PETSC_FALSE));
36104c712d99Sksagiyam         }
36114c712d99Sksagiyam       }
36124c712d99Sksagiyam     }
36133e98c3d0Sksagiyam     if (!localized) {
36149566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateSection(dm, &coordSection));
36159566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
36163e98c3d0Sksagiyam       PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
36173e98c3d0Sksagiyam     } else {
36183e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateSection(dm, &coordSection));
36193e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateSection(subdm, &subCoordSection));
36203e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinatesLocal(dm, &coordinates));
36213e98c3d0Sksagiyam     }
36229566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
36239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
36249566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
36253e98c3d0Sksagiyam     PetscCall(PetscSectionGetChart(coordSection, &firstP, &lastP));
36263e98c3d0Sksagiyam     for (d = 0; d <= sdim; ++d) {
36273e98c3d0Sksagiyam       for (p = 0; p < numSubPoints[d]; ++p) {
36283e98c3d0Sksagiyam         const PetscInt point    = subpoints[d][p];
36293e98c3d0Sksagiyam         const PetscInt subpoint = firstSubPoint[d] + p;
36303e98c3d0Sksagiyam 
36313e98c3d0Sksagiyam         if (point >= firstP && point < lastP) {
36323e98c3d0Sksagiyam           firstSubP = PetscMin(firstSubP, subpoint);
36333e98c3d0Sksagiyam           lastSubP  = PetscMax(lastSubP, subpoint);
36343e98c3d0Sksagiyam         }
36353e98c3d0Sksagiyam       }
36363e98c3d0Sksagiyam     }
36373e98c3d0Sksagiyam     lastSubP += 1;
36383e98c3d0Sksagiyam     if (firstSubP == totSubPoints) {
36393e98c3d0Sksagiyam       /* Zero if there is no coordinate point. */
36403e98c3d0Sksagiyam       firstSubP = 0;
36413e98c3d0Sksagiyam       lastSubP  = 0;
36423e98c3d0Sksagiyam     }
36433e98c3d0Sksagiyam     PetscCall(PetscSectionSetChart(subCoordSection, firstSubP, lastSubP));
36443e98c3d0Sksagiyam     for (d = 0; d <= sdim; ++d) {
36453e98c3d0Sksagiyam       for (p = 0; p < numSubPoints[d]; ++p) {
36463e98c3d0Sksagiyam         const PetscInt point    = subpoints[d][p];
36473e98c3d0Sksagiyam         const PetscInt subpoint = firstSubPoint[d] + p;
3648e6ccafaeSMatthew G Knepley         PetscInt       dof;
3649e6ccafaeSMatthew G Knepley 
36503e98c3d0Sksagiyam         if (point >= firstP && point < lastP) {
36513e98c3d0Sksagiyam           PetscCall(PetscSectionGetDof(coordSection, point, &dof));
36523e98c3d0Sksagiyam           PetscCall(PetscSectionSetDof(subCoordSection, subpoint, dof));
36533e98c3d0Sksagiyam           PetscCall(PetscSectionSetFieldDof(subCoordSection, subpoint, 0, dof));
36543e98c3d0Sksagiyam         }
36553e98c3d0Sksagiyam       }
3656e6ccafaeSMatthew G Knepley     }
36579566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
36589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
36599566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
36609566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
36619566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
36629566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
3663*605a06ccSksagiyam     PetscCall(VecGetBlockSize(coordinates, &bs));
3664*605a06ccSksagiyam     PetscCall(VecSetBlockSize(subCoordinates, bs));
36659566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
36669566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
36679566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
36683e98c3d0Sksagiyam     for (d = 0; d <= sdim; ++d) {
36693e98c3d0Sksagiyam       for (p = 0; p < numSubPoints[d]; ++p) {
36703e98c3d0Sksagiyam         const PetscInt point    = subpoints[d][p];
36713e98c3d0Sksagiyam         const PetscInt subpoint = firstSubPoint[d] + p;
3672e6ccafaeSMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3673e6ccafaeSMatthew G Knepley 
36743e98c3d0Sksagiyam         if (point >= firstP && point < lastP) {
36753e98c3d0Sksagiyam           PetscCall(PetscSectionGetDof(coordSection, point, &dof));
36763e98c3d0Sksagiyam           PetscCall(PetscSectionGetOffset(coordSection, point, &off));
36773e98c3d0Sksagiyam           PetscCall(PetscSectionGetDof(subCoordSection, subpoint, &sdof));
36783e98c3d0Sksagiyam           PetscCall(PetscSectionGetOffset(subCoordSection, subpoint, &soff));
36793e98c3d0Sksagiyam           PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subpoint %" PetscInt_FMT ", point %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subpoint, point, dof);
3680efa14ee0SMatthew G Knepley           for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3681e6ccafaeSMatthew G Knepley         }
36823e98c3d0Sksagiyam       }
36833e98c3d0Sksagiyam     }
36849566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
36859566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
36863e98c3d0Sksagiyam     switch (coordinate_type) {
36873e98c3d0Sksagiyam     case 0:
36889566063dSJacob Faibussowitsch       PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
36893e98c3d0Sksagiyam       break;
36903e98c3d0Sksagiyam     case 1:
36913e98c3d0Sksagiyam       PetscCall(DMSetCellCoordinatesLocal(subdm, subCoordinates));
36923e98c3d0Sksagiyam       break;
36933e98c3d0Sksagiyam     default:
36943e98c3d0Sksagiyam       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "coordinate_type must be {0, 1}");
36953e98c3d0Sksagiyam     }
36969566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3697e6ccafaeSMatthew G Knepley   }
36983982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
36993982b651SMatthew G. Knepley   {
37003982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
37013982b651SMatthew G. Knepley     IS                 subpIS;
37023982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
37035033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
37045033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3705dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
37065033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
37075033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
37083982b651SMatthew G. Knepley 
37099566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
37105033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
37119566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
37129566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
37139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
37149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
37159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
37163982b651SMatthew G. Knepley     if (subpIS) {
3717dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3718dd05d810SMatthew G. Knepley 
37199566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
37209566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3721dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3722dd05d810SMatthew G. Knepley       if (!sorted) {
3723dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3724dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3725dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3726dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3727dd05d810SMatthew G. Knepley       }
37283982b651SMatthew G. Knepley     }
37299566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
37303982b651SMatthew G. Knepley     if (numRoots >= 0) {
37315033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
37325033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
37339566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
37343982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
37353982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
37363982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
37373982b651SMatthew G. Knepley       }
373830cbcd5dSksagiyam       for (p = pStart; p < pEnd; ++p) {
373930cbcd5dSksagiyam         newOwners[p - pStart].rank  = -3;
374030cbcd5dSksagiyam         newOwners[p - pStart].index = -3;
374130cbcd5dSksagiyam       }
374230cbcd5dSksagiyam       if (sanitizeSubmesh) {
374330cbcd5dSksagiyam         /* A subpoint is forced to be owned by a rank that owns */
374430cbcd5dSksagiyam         /* a subcell that contains the subpoint in its closure. */
374530cbcd5dSksagiyam         PetscInt  cStart, cEnd, c, clSize, cl;
374630cbcd5dSksagiyam         PetscInt *ownedCells, *closure = NULL;
374730cbcd5dSksagiyam 
374830cbcd5dSksagiyam         /* claim ownership */
374930cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
375030cbcd5dSksagiyam           const PetscInt point = subpoints[p];
375130cbcd5dSksagiyam 
375230cbcd5dSksagiyam           newLocalPoints[point - pStart].rank  = rank;
375330cbcd5dSksagiyam           newLocalPoints[point - pStart].index = p;
375430cbcd5dSksagiyam         }
375530cbcd5dSksagiyam         PetscCall(DMGetDimension(subdm, &sdim));
375630cbcd5dSksagiyam         PetscCall(DMPlexGetDepthStratum(dm, sdim, &cStart, &cEnd));
375730cbcd5dSksagiyam         PetscCall(PetscMalloc1(cEnd - cStart, &ownedCells));
375830cbcd5dSksagiyam         for (c = cStart; c < cEnd; ++c) ownedCells[c - cStart] = 0;
375930cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
376030cbcd5dSksagiyam           c = subpoints[p];
376130cbcd5dSksagiyam           if (c >= cStart && c < cEnd) ownedCells[c - cStart] = 1;
376230cbcd5dSksagiyam         }
376330cbcd5dSksagiyam         for (l = 0; l < numLeaves; ++l) {
376430cbcd5dSksagiyam           c = localPoints ? localPoints[l] : l;
376530cbcd5dSksagiyam           if (c >= cStart && c < cEnd) ownedCells[c - cStart] = 0;
376630cbcd5dSksagiyam         }
376730cbcd5dSksagiyam         for (c = cStart; c < cEnd; ++c) {
376830cbcd5dSksagiyam           if (ownedCells[c - cStart] == 0) continue;
376930cbcd5dSksagiyam           PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
377030cbcd5dSksagiyam           for (cl = 0; cl < clSize * 2; cl += 2) {
377130cbcd5dSksagiyam             p = closure[cl];
377230cbcd5dSksagiyam             if (newLocalPoints[p - pStart].rank < size) newLocalPoints[p - pStart].rank += size;
377330cbcd5dSksagiyam           }
377430cbcd5dSksagiyam           PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
377530cbcd5dSksagiyam         }
377630cbcd5dSksagiyam         PetscCall(PetscFree(ownedCells));
377730cbcd5dSksagiyam         for (p = 0; p < numRoots; ++p) {
377830cbcd5dSksagiyam           newOwners[p].rank  = newLocalPoints[p].rank;
377930cbcd5dSksagiyam           newOwners[p].index = newLocalPoints[p].index;
378030cbcd5dSksagiyam         }
378130cbcd5dSksagiyam       } else {
37823982b651SMatthew G. Knepley         /* Set subleaves */
37833982b651SMatthew G. Knepley         for (l = 0; l < numLeaves; ++l) {
37843982b651SMatthew G. Knepley           const PetscInt point    = localPoints[l];
3785dd05d810SMatthew G. Knepley           const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
37863982b651SMatthew G. Knepley 
37873982b651SMatthew G. Knepley           if (subpoint < 0) continue;
37883982b651SMatthew G. Knepley           newLocalPoints[point - pStart].rank  = rank;
37893982b651SMatthew G. Knepley           newLocalPoints[point - pStart].index = subpoint;
37903982b651SMatthew G. Knepley           ++numSubleaves;
37913982b651SMatthew G. Knepley         }
37923982b651SMatthew G. Knepley         /* Must put in owned subpoints */
3793d9384f27SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) {
37945033f954SMatthew G. Knepley           /* Hold on to currently owned points */
37955033f954SMatthew G. Knepley           if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
37965033f954SMatthew G. Knepley           else newOwners[subpoints[p] - pStart].rank = rank;
3797d9384f27SMatthew G. Knepley           newOwners[subpoints[p] - pStart].index = p;
37983982b651SMatthew G. Knepley         }
379930cbcd5dSksagiyam       }
38006497c311SBarry Smith       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
38016497c311SBarry Smith       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
38029371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
38039371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
3804fdb4e32bSksagiyam       if (ownershipTransferSF) {
3805fdb4e32bSksagiyam         PetscSFNode *iremote1 = NULL, *newOwners1 = NULL;
3806fdb4e32bSksagiyam         PetscInt    *ilocal1 = NULL;
3807fdb4e32bSksagiyam         PetscInt     nleaves1, point;
3808fdb4e32bSksagiyam 
3809fdb4e32bSksagiyam         for (p = 0; p < numSubpoints; ++p) {
3810fdb4e32bSksagiyam           point                                = subpoints[p];
3811fdb4e32bSksagiyam           newLocalPoints[point - pStart].index = point;
3812fdb4e32bSksagiyam         }
3813fdb4e32bSksagiyam         PetscCall(PetscMalloc1(numRoots, &newOwners1));
3814fdb4e32bSksagiyam         for (p = 0; p < numRoots; ++p) {
3815fdb4e32bSksagiyam           newOwners1[p].rank  = -1;
3816fdb4e32bSksagiyam           newOwners1[p].index = -1;
3817fdb4e32bSksagiyam         }
38186497c311SBarry Smith         PetscCall(PetscSFReduceBegin(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners1, MPI_MAXLOC));
38196497c311SBarry Smith         PetscCall(PetscSFReduceEnd(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners1, MPI_MAXLOC));
3820fdb4e32bSksagiyam         for (p = 0, nleaves1 = 0; p < numRoots; ++p) {
3821fdb4e32bSksagiyam           if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) { ++nleaves1; }
3822fdb4e32bSksagiyam         }
3823fdb4e32bSksagiyam         PetscCall(PetscMalloc1(nleaves1, &ilocal1));
3824fdb4e32bSksagiyam         PetscCall(PetscMalloc1(nleaves1, &iremote1));
3825fdb4e32bSksagiyam         for (p = 0, nleaves1 = 0; p < numRoots; ++p) {
3826fdb4e32bSksagiyam           if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) {
3827fdb4e32bSksagiyam             ilocal1[nleaves1]        = pStart + p;
3828fdb4e32bSksagiyam             iremote1[nleaves1].rank  = newOwners[p].rank;
3829fdb4e32bSksagiyam             iremote1[nleaves1].index = newOwners1[p].index;
3830fdb4e32bSksagiyam             ++nleaves1;
3831fdb4e32bSksagiyam           }
3832fdb4e32bSksagiyam         }
3833fdb4e32bSksagiyam         PetscCall(PetscFree(newOwners1));
3834fdb4e32bSksagiyam         PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)sfPoint), ownershipTransferSF));
3835fdb4e32bSksagiyam         PetscCall(PetscSFSetFromOptions(*ownershipTransferSF));
3836fdb4e32bSksagiyam         PetscCall(PetscSFSetGraph(*ownershipTransferSF, pEnd - pStart, nleaves1, ilocal1, PETSC_OWN_POINTER, iremote1, PETSC_OWN_POINTER));
3837fdb4e32bSksagiyam       }
383830cbcd5dSksagiyam       if (sanitizeSubmesh) {
383930cbcd5dSksagiyam         for (p = pStart; p < pEnd; ++p) {
384030cbcd5dSksagiyam           newLocalPoints[p - pStart].rank  = newOwners[p - pStart].rank;
384130cbcd5dSksagiyam           newLocalPoints[p - pStart].index = newOwners[p - pStart].index;
384230cbcd5dSksagiyam         }
384330cbcd5dSksagiyam       }
38446497c311SBarry Smith       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
38456497c311SBarry Smith       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
384630cbcd5dSksagiyam       if (sanitizeSubmesh) {
384730cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
384830cbcd5dSksagiyam           const PetscInt point = subpoints[p];
384930cbcd5dSksagiyam 
385030cbcd5dSksagiyam           if (newLocalPoints[point - pStart].rank >= 0 && newLocalPoints[point - pStart].rank != rank) ++sl;
385130cbcd5dSksagiyam         }
385230cbcd5dSksagiyam         PetscCall(PetscMalloc1(sl, &slocalPoints));
385330cbcd5dSksagiyam         PetscCall(PetscMalloc1(sl, &sremotePoints));
385430cbcd5dSksagiyam         for (p = 0, sl = 0; p < numSubpoints; ++p) {
385530cbcd5dSksagiyam           const PetscInt point = subpoints[p];
385630cbcd5dSksagiyam 
385730cbcd5dSksagiyam           if (newLocalPoints[point - pStart].rank >= 0 && newLocalPoints[point - pStart].rank != rank) {
385830cbcd5dSksagiyam             slocalPoints[sl]        = p;
385930cbcd5dSksagiyam             sremotePoints[sl].rank  = newLocalPoints[point - pStart].rank;
386030cbcd5dSksagiyam             sremotePoints[sl].index = newLocalPoints[point - pStart].index;
386130cbcd5dSksagiyam             PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank");
386230cbcd5dSksagiyam             PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint");
386330cbcd5dSksagiyam             ++sl;
386430cbcd5dSksagiyam           }
386530cbcd5dSksagiyam         }
386630cbcd5dSksagiyam       } else {
38679566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
38689566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
38695033f954SMatthew G. Knepley         for (l = 0; l < numLeaves; ++l) {
38703982b651SMatthew G. Knepley           const PetscInt point    = localPoints[l];
3871dd05d810SMatthew G. Knepley           const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
38723982b651SMatthew G. Knepley 
38733982b651SMatthew G. Knepley           if (subpoint < 0) continue;
38749371c9d4SSatish Balay           if (newLocalPoints[point].rank == rank) {
38759371c9d4SSatish Balay             ++ll;
38769371c9d4SSatish Balay             continue;
38779371c9d4SSatish Balay           }
38783982b651SMatthew G. Knepley           slocalPoints[sl]        = subpoint;
38793982b651SMatthew G. Knepley           sremotePoints[sl].rank  = newLocalPoints[point].rank;
38803982b651SMatthew G. Knepley           sremotePoints[sl].index = newLocalPoints[point].index;
388163a3b9bcSJacob Faibussowitsch           PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
388263a3b9bcSJacob Faibussowitsch           PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
38833982b651SMatthew G. Knepley           ++sl;
38843982b651SMatthew G. Knepley         }
38851dca8a05SBarry 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);
388630cbcd5dSksagiyam       }
38879566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
38889566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
38893982b651SMatthew G. Knepley     }
389048a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3891dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
38923982b651SMatthew G. Knepley   }
3893212103e5SMatthew Knepley   /* Filter labels */
38949566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3895efa14ee0SMatthew G Knepley   /* Cleanup */
3896dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
38979566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
38989566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3899e6ccafaeSMatthew G Knepley   }
39009566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
39013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3902e6ccafaeSMatthew G Knepley }
3903e6ccafaeSMatthew G Knepley 
3904d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
3905d71ae5a4SJacob Faibussowitsch {
39063982b651SMatthew G. Knepley   PetscFunctionBegin;
390730cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, PETSC_FALSE, PETSC_FALSE, NULL, subdm));
39083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39093982b651SMatthew G. Knepley }
39103982b651SMatthew G. Knepley 
3911d0fa310fSMatthew G. Knepley /*@
3912e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3913e6ccafaeSMatthew G Knepley 
3914e6ccafaeSMatthew G Knepley   Input Parameters:
3915e6ccafaeSMatthew G Knepley + dm          - The original mesh
391620f4b53cSBarry Smith . vertexLabel - The `DMLabel` marking points contained in the surface
3917158acfadSMatthew G. Knepley . value       - The label value to use
391820f4b53cSBarry Smith - markedFaces - `PETSC_TRUE` if surface faces are marked in addition to vertices, `PETSC_FALSE` if only vertices are marked
3919e6ccafaeSMatthew G Knepley 
3920e6ccafaeSMatthew G Knepley   Output Parameter:
3921e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3922e6ccafaeSMatthew G Knepley 
3923e6ccafaeSMatthew G Knepley   Level: developer
3924e6ccafaeSMatthew G Knepley 
392520f4b53cSBarry Smith   Note:
392620f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
392720f4b53cSBarry Smith 
39281cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3929830e53efSMatthew G. Knepley @*/
3930d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3931d71ae5a4SJacob Faibussowitsch {
3932827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3933827c4036SVaclav Hapla   PetscInt               dim, cdim;
3934e6ccafaeSMatthew G Knepley 
3935e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3936e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39374f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
39389566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
39399566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
39409566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
39419566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
39429566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
39439566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
39449566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
394508401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3946827c4036SVaclav Hapla   if (interpolated) {
39479566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3948e6ccafaeSMatthew G Knepley   } else {
39499566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3950e6ccafaeSMatthew G Knepley   }
39515de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
39523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3953e6ccafaeSMatthew G Knepley }
3954e6ccafaeSMatthew G Knepley 
3955d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3956d71ae5a4SJacob Faibussowitsch {
3957766ab985SMatthew G. Knepley   MPI_Comm        comm;
3958766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3959766ab985SMatthew G. Knepley   IS              subvertexIS;
3960766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3961fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3962766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3963412e9a14SMatthew G. Knepley   PetscInt        c, f;
3964766ab985SMatthew G. Knepley 
3965766ab985SMatthew G. Knepley   PetscFunctionBegin;
39669566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3967766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
39689566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
39699566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
39709566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3971766ab985SMatthew G. Knepley   /* Setup chart */
39729566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
39739566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
39749566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
39759566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3976766ab985SMatthew G. Knepley   /* Set cone sizes */
3977766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3978766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3979766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
39809566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
39819566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
398248a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
398348a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
39849566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3985d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3986766ab985SMatthew G. Knepley   /* Create face cones */
39879566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
39889566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3989766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3990766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3991766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
399287feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3993064cae4fSPierre Jolivet     PetscBool       isHybrid = PETSC_FALSE;
399487feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3995766ab985SMatthew G. Knepley 
39969566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3997412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
39989566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
399987feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
40009566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
400187feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
400287feddfdSMatthew G. Knepley     }
40039566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
40049566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
40059566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
400627234c99SMatthew G. Knepley     /* Not true in parallel
400708401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
400887feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
400987feddfdSMatthew G. Knepley       PetscInt  negsubcell;
4010064cae4fSPierre Jolivet       PetscBool isHybrid = PETSC_FALSE;
4011766ab985SMatthew G. Knepley 
40129566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
4013412e9a14SMatthew G. Knepley       if (isHybrid) continue;
401487feddfdSMatthew G. Knepley       /* I know this is a crap search */
401587feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
401687feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
4017766ab985SMatthew G. Knepley       }
401863a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
40199566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
4020766ab985SMatthew G. Knepley     }
40219566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
402287feddfdSMatthew G. Knepley     ++newFacePoint;
4023766ab985SMatthew G. Knepley   }
40249566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
40259566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
40269566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
4027766ab985SMatthew G. Knepley   /* Build coordinates */
4028766ab985SMatthew G. Knepley   {
4029766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
4030766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
4031766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
4032c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
403324640c55SToby Isaac     const char  *name;
4034766ab985SMatthew G. Knepley 
40359566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
40369566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
40379566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
40389566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
40399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
40409566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
40419566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
40429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
4043766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
4044766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
4045766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
4046766ab985SMatthew G. Knepley       PetscInt       dof;
4047766ab985SMatthew G. Knepley 
40489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
40499566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
40509566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
4051766ab985SMatthew G. Knepley     }
40529566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
40539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
40549566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
40559566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
40569566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
40579566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
40589566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
40599566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
40609566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
40619566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
4062766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
4063766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
4064766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
4065766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
4066766ab985SMatthew G. Knepley 
40679566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
40689566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
40699566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
40709566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
407163a3b9bcSJacob 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);
4072766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
4073766ab985SMatthew G. Knepley     }
40749566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
40759566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
40769566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
40779566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
4078766ab985SMatthew G. Knepley   }
4079aca35d17SMatthew G. Knepley   /* Build SF */
4080aca35d17SMatthew G. Knepley   CHKMEMQ;
4081aca35d17SMatthew G. Knepley   {
4082aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
4083aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
4084bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
4085aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
4086bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
408749c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
4088bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
4089aca35d17SMatthew G. Knepley 
40909566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
40919566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
40929566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
40939566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
40949566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
40959566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
4096aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
4097aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
40989566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
4099bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
4100bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
4101bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
4102bdcf2095SMatthew G. Knepley       }
41039e0823b2SMatthew G. Knepley       /* Set subleaves */
4104aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
4105aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
4106bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
4107aca35d17SMatthew G. Knepley 
410863a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
4109bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
4110bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
4111bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
4112bdcf2095SMatthew G. Knepley         ++numSubLeaves;
4113aca35d17SMatthew G. Knepley       }
41149e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
41159e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
41169e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
41179e0823b2SMatthew G. Knepley 
41189e0823b2SMatthew G. Knepley         if (subPoint < 0) {
41199e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
41209e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
41219e0823b2SMatthew G. Knepley         } else {
41229e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
41239e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
41249e0823b2SMatthew G. Knepley         }
4125bdcf2095SMatthew G. Knepley       }
41266497c311SBarry Smith       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
41276497c311SBarry Smith       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
41286497c311SBarry Smith       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
41296497c311SBarry Smith       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
41309566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
41319566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
413249c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
4133bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
4134bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
4135aca35d17SMatthew G. Knepley 
4136aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
41379371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
41389371c9d4SSatish Balay           ++ll;
41399371c9d4SSatish Balay           continue;
41409371c9d4SSatish Balay         }
4141aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
4142bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
4143bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
414463a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
414563a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
4146aca35d17SMatthew G. Knepley         ++sl;
4147aca35d17SMatthew G. Knepley       }
41489566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
41491dca8a05SBarry 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);
41509566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
4151aca35d17SMatthew G. Knepley     }
4152aca35d17SMatthew G. Knepley   }
4153aca35d17SMatthew G. Knepley   CHKMEMQ;
4154766ab985SMatthew G. Knepley   /* Cleanup */
41559566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
41569566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
41579566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
41583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4159766ab985SMatthew G. Knepley }
4160766ab985SMatthew G. Knepley 
4161d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
4162d71ae5a4SJacob Faibussowitsch {
41633982b651SMatthew G. Knepley   DMLabel label = NULL;
4164766ab985SMatthew G. Knepley 
4165766ab985SMatthew G. Knepley   PetscFunctionBegin;
41669566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
416730cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, PETSC_FALSE, PETSC_FALSE, NULL, subdm));
41683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4169766ab985SMatthew G. Knepley }
4170766ab985SMatthew G. Knepley 
4171cc4c1da9SBarry Smith /*@
417220f4b53cSBarry 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.
4173766ab985SMatthew G. Knepley 
4174766ab985SMatthew G. Knepley   Input Parameters:
4175766ab985SMatthew G. Knepley + dm          - The original mesh
417627c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
417720f4b53cSBarry Smith . label       - A label name, or `NULL`
417827c04023SMatthew G. Knepley - value       - A label value
4179766ab985SMatthew G. Knepley 
4180766ab985SMatthew G. Knepley   Output Parameter:
4181766ab985SMatthew G. Knepley . subdm - The surface mesh
4182766ab985SMatthew G. Knepley 
4183766ab985SMatthew G. Knepley   Level: developer
4184766ab985SMatthew G. Knepley 
418520f4b53cSBarry Smith   Note:
418620f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
418720f4b53cSBarry Smith 
41881cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
4189c08575a3SMatthew G. Knepley @*/
4190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
4191d71ae5a4SJacob Faibussowitsch {
4192c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
4193766ab985SMatthew G. Knepley 
4194766ab985SMatthew G. Knepley   PetscFunctionBegin;
4195766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41964f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
41979566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
41989566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
41999566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
42009566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
42019566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
42029566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
42039566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
4204766ab985SMatthew G. Knepley   if (depth == dim) {
42059566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
4206766ab985SMatthew G. Knepley   } else {
42079566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
4208e6ccafaeSMatthew G Knepley   }
42095de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
42103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4211e6ccafaeSMatthew G Knepley }
4212e6ccafaeSMatthew G Knepley 
4213bec263e5SMatthew G. Knepley /*@
42140e18dc48SMatthew G. Knepley   DMPlexReorderCohesiveSupports - Ensure that face supports for cohesive end caps are ordered
42150e18dc48SMatthew G. Knepley 
42160e18dc48SMatthew G. Knepley   Not Collective
42170e18dc48SMatthew G. Knepley 
42180e18dc48SMatthew G. Knepley   Input Parameter:
42190e18dc48SMatthew G. Knepley . dm - The `DM` containing cohesive cells
42200e18dc48SMatthew G. Knepley 
42210e18dc48SMatthew G. Knepley   Level: developer
42220e18dc48SMatthew G. Knepley 
4223a4e35b19SJacob Faibussowitsch   Note:
4224a4e35b19SJacob Faibussowitsch   For the negative size (first) face, the cohesive cell should be first in the support, and for
4225a4e35b19SJacob Faibussowitsch   the positive side (second) face, the cohesive cell should be second in the support.
42260e18dc48SMatthew G. Knepley 
42270e18dc48SMatthew G. Knepley .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexCreateCohesiveSubmesh()`
42280e18dc48SMatthew G. Knepley @*/
42290e18dc48SMatthew G. Knepley PetscErrorCode DMPlexReorderCohesiveSupports(DM dm)
42300e18dc48SMatthew G. Knepley {
42310e18dc48SMatthew G. Knepley   PetscInt dim, cStart, cEnd;
42320e18dc48SMatthew G. Knepley 
42330e18dc48SMatthew G. Knepley   PetscFunctionBegin;
42340e18dc48SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
42350e18dc48SMatthew G. Knepley   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cStart, &cEnd));
42360e18dc48SMatthew G. Knepley   for (PetscInt c = cStart; c < cEnd; ++c) {
42370e18dc48SMatthew G. Knepley     const PetscInt *cone;
42380e18dc48SMatthew G. Knepley     PetscInt        coneSize;
42390e18dc48SMatthew G. Knepley 
42400e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
42410e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetCone(dm, c, &cone));
42420e18dc48SMatthew G. Knepley     PetscCheck(coneSize >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid cell %" PetscInt_FMT " cone size %" PetscInt_FMT " must be >= 2", c, coneSize);
42430e18dc48SMatthew G. Knepley     for (PetscInt s = 0; s < 2; ++s) {
42440e18dc48SMatthew G. Knepley       const PetscInt *supp;
42450e18dc48SMatthew G. Knepley       PetscInt        suppSize, newsupp[2];
42460e18dc48SMatthew G. Knepley 
42470e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupportSize(dm, cone[s], &suppSize));
42480e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupport(dm, cone[s], &supp));
42490e18dc48SMatthew G. Knepley       if (suppSize == 2) {
42500e18dc48SMatthew G. Knepley         /* Reorder hybrid end cap support */
42510e18dc48SMatthew G. Knepley         if (supp[s] == c) {
42520e18dc48SMatthew G. Knepley           newsupp[0] = supp[1];
42530e18dc48SMatthew G. Knepley           newsupp[1] = supp[0];
42540e18dc48SMatthew G. Knepley         } else {
42550e18dc48SMatthew G. Knepley           newsupp[0] = supp[0];
42560e18dc48SMatthew G. Knepley           newsupp[1] = supp[1];
42570e18dc48SMatthew G. Knepley         }
42580e18dc48SMatthew 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);
42590e18dc48SMatthew G. Knepley         PetscCall(DMPlexSetSupport(dm, cone[s], newsupp));
42600e18dc48SMatthew G. Knepley       }
42610e18dc48SMatthew G. Knepley     }
42620e18dc48SMatthew G. Knepley   }
42630e18dc48SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42640e18dc48SMatthew G. Knepley }
42650e18dc48SMatthew G. Knepley 
42660e18dc48SMatthew G. Knepley /*@
4267bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
4268bec263e5SMatthew G. Knepley 
4269bec263e5SMatthew G. Knepley   Input Parameters:
4270bec263e5SMatthew G. Knepley + dm              - The original mesh
427120f4b53cSBarry Smith . cellLabel       - The `DMLabel` marking cells contained in the new mesh
42724d00ad26Sksagiyam . value           - The label value to use
427330cbcd5dSksagiyam . ignoreLabelHalo - The flag indicating if labeled points that are in the halo are ignored
427430cbcd5dSksagiyam - sanitizeSubmesh - The flag indicating if a subpoint is forced to be owned by a rank that owns a subcell that contains that point in its closure
4275bec263e5SMatthew G. Knepley 
42769cde84edSJose E. Roman   Output Parameters:
4277fdb4e32bSksagiyam + ownershipTransferSF - The `PetscSF` representing the ownership transfers between parent local meshes due to submeshing.
4278fdb4e32bSksagiyam - subdm               - The new mesh
4279bec263e5SMatthew G. Knepley 
4280bec263e5SMatthew G. Knepley   Level: developer
4281bec263e5SMatthew G. Knepley 
428220f4b53cSBarry Smith   Note:
428320f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
428420f4b53cSBarry Smith 
4285fdb4e32bSksagiyam   On a given rank, the leaves of the ownershipTransferSF are the points in the local mesh that this rank gives up ownership of (in the submesh), and
4286fdb4e32bSksagiyam   the remote locations for these leaves are tuples of rank and point that represent the new owners.
4287fdb4e32bSksagiyam 
42881cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
4289bec263e5SMatthew G. Knepley @*/
429030cbcd5dSksagiyam PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, PetscBool ignoreLabelHalo, PetscBool sanitizeSubmesh, PetscSF *ownershipTransferSF, DM *subdm)
4291d71ae5a4SJacob Faibussowitsch {
4292b47cd10cSMatthew G. Knepley   PetscInt dim, overlap;
4293bec263e5SMatthew G. Knepley 
4294bec263e5SMatthew G. Knepley   PetscFunctionBegin;
4295bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
429630cbcd5dSksagiyam   if (ownershipTransferSF) PetscAssertPointer(ownershipTransferSF, 6);
429730cbcd5dSksagiyam   PetscAssertPointer(subdm, 7);
42989566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
42999566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
43009566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
4301bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
430230cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, ignoreLabelHalo, sanitizeSubmesh, ownershipTransferSF, *subdm));
43035de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
4304b47cd10cSMatthew G. Knepley   // It is possible to obtain a surface mesh where some faces are in SF
4305b47cd10cSMatthew G. Knepley   //   We should either mark the mesh as having an overlap, or delete these from the SF
4306b47cd10cSMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
4307b47cd10cSMatthew G. Knepley   if (!overlap) {
4308b47cd10cSMatthew G. Knepley     PetscSF         sf;
4309b47cd10cSMatthew G. Knepley     const PetscInt *leaves;
4310b47cd10cSMatthew G. Knepley     PetscInt        cStart, cEnd, Nl;
4311b47cd10cSMatthew G. Knepley     PetscBool       hasSubcell = PETSC_FALSE, ghasSubcell;
4312b47cd10cSMatthew G. Knepley 
4313b47cd10cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd));
4314b47cd10cSMatthew G. Knepley     PetscCall(DMGetPointSF(*subdm, &sf));
4315b47cd10cSMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
4316b47cd10cSMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
4317b47cd10cSMatthew G. Knepley       const PetscInt point = leaves ? leaves[l] : l;
4318b47cd10cSMatthew G. Knepley 
4319b47cd10cSMatthew G. Knepley       if (point >= cStart && point < cEnd) {
4320b47cd10cSMatthew G. Knepley         hasSubcell = PETSC_TRUE;
4321b47cd10cSMatthew G. Knepley         break;
4322b47cd10cSMatthew G. Knepley       }
4323b47cd10cSMatthew G. Knepley     }
4324462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
4325b47cd10cSMatthew G. Knepley     if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1));
4326b47cd10cSMatthew G. Knepley   }
43273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4328bec263e5SMatthew G. Knepley }
4329bec263e5SMatthew G. Knepley 
433064beef6dSMatthew G. Knepley /*@
433120f4b53cSBarry Smith   DMPlexGetSubpointMap - Returns a `DMLabel` with point dimension as values
433264beef6dSMatthew G. Knepley 
433364beef6dSMatthew G. Knepley   Input Parameter:
433420f4b53cSBarry Smith . dm - The submesh `DM`
433564beef6dSMatthew G. Knepley 
433664beef6dSMatthew G. Knepley   Output Parameter:
433720f4b53cSBarry Smith . subpointMap - The `DMLabel` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
433864beef6dSMatthew G. Knepley 
433964beef6dSMatthew G. Knepley   Level: developer
434064beef6dSMatthew G. Knepley 
43411cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
434264beef6dSMatthew G. Knepley @*/
4343d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
4344d71ae5a4SJacob Faibussowitsch {
4345e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4346e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43474f572ea9SToby Isaac   PetscAssertPointer(subpointMap, 2);
434865663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
43493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4350e6ccafaeSMatthew G Knepley }
4351e6ccafaeSMatthew G Knepley 
4352c08575a3SMatthew G. Knepley /*@
435320f4b53cSBarry Smith   DMPlexSetSubpointMap - Sets the `DMLabel` with point dimension as values
4354c08575a3SMatthew G. Knepley 
4355c08575a3SMatthew G. Knepley   Input Parameters:
435620f4b53cSBarry Smith + dm          - The submesh `DM`
435720f4b53cSBarry Smith - subpointMap - The `DMLabel` of all the points from the original mesh in this submesh
4358c08575a3SMatthew G. Knepley 
4359c08575a3SMatthew G. Knepley   Level: developer
4360c08575a3SMatthew G. Knepley 
436120f4b53cSBarry Smith   Note:
436220f4b53cSBarry Smith   Should normally not be called by the user, since it is set in `DMPlexCreateSubmesh()`
436320f4b53cSBarry Smith 
43641cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
4365c08575a3SMatthew G. Knepley @*/
4366d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
4367d71ae5a4SJacob Faibussowitsch {
4368e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
4369285d324eSMatthew G. Knepley   DMLabel  tmp;
4370e6ccafaeSMatthew G Knepley 
4371e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4372e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4373285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
4374e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
43759566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
43769566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
43773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4378e6ccafaeSMatthew G Knepley }
4379e6ccafaeSMatthew G Knepley 
4380d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
4381d71ae5a4SJacob Faibussowitsch {
438297d8846cSMatthew Knepley   DMLabel  spmap;
438397d8846cSMatthew Knepley   PetscInt depth, d;
438497d8846cSMatthew Knepley 
438597d8846cSMatthew Knepley   PetscFunctionBegin;
43869566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
43879566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
438897d8846cSMatthew Knepley   if (spmap && depth >= 0) {
438997d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
439097d8846cSMatthew Knepley     PetscInt *points, *depths;
439197d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
439297d8846cSMatthew Knepley 
43939566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
439463a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
43959566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
43969566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
439797d8846cSMatthew Knepley     depths[0] = depth;
439897d8846cSMatthew Knepley     depths[1] = 0;
4399ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
440097d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
440197d8846cSMatthew Knepley       const PetscInt dep = depths[d];
440297d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
440397d8846cSMatthew Knepley 
44049566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
44059566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
440697d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
440763a3b9bcSJacob 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);
440897d8846cSMatthew Knepley       } else {
440997d8846cSMatthew Knepley         if (!n) {
441097d8846cSMatthew Knepley           if (d == 0) {
441197d8846cSMatthew Knepley             /* Missing cells */
441297d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
441397d8846cSMatthew Knepley           } else {
441497d8846cSMatthew Knepley             /* Missing faces */
44151690c2aeSBarry Smith             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_INT_MAX;
441697d8846cSMatthew Knepley           }
441797d8846cSMatthew Knepley         }
441897d8846cSMatthew Knepley       }
441997d8846cSMatthew Knepley       if (n) {
442097d8846cSMatthew Knepley         IS              is;
442197d8846cSMatthew Knepley         const PetscInt *opoints;
442297d8846cSMatthew Knepley 
44239566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
44249566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
442597d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
44269566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
44279566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
442897d8846cSMatthew Knepley       }
442997d8846cSMatthew Knepley     }
44309566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
443163a3b9bcSJacob 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);
44329566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
44339566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
443497d8846cSMatthew Knepley   }
44353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
443697d8846cSMatthew Knepley }
443797d8846cSMatthew Knepley 
443864beef6dSMatthew G. Knepley /*@
443920f4b53cSBarry Smith   DMPlexGetSubpointIS - Returns an `IS` covering the entire subdm chart with the original points as data
4440e6ccafaeSMatthew G Knepley 
4441e6ccafaeSMatthew G Knepley   Input Parameter:
444220f4b53cSBarry Smith . dm - The submesh `DM`
4443e6ccafaeSMatthew G Knepley 
4444e6ccafaeSMatthew G Knepley   Output Parameter:
444520f4b53cSBarry Smith . subpointIS - The `IS` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
444664beef6dSMatthew G. Knepley 
444764beef6dSMatthew G. Knepley   Level: developer
444864beef6dSMatthew G. Knepley 
444920f4b53cSBarry Smith   Note:
445029d2e75bSMatthew G. Knepley   This `IS` is guaranteed to be sorted by the construction of the submesh. However, if the filtering operation removes an entire stratum, then the strata in the submesh can be in a different order, and the `subpointIS` will only be sorted within each stratum.
445120f4b53cSBarry Smith 
44521cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
445364beef6dSMatthew G. Knepley @*/
4454d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
4455d71ae5a4SJacob Faibussowitsch {
445697d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
445797d8846cSMatthew Knepley   DMLabel          spmap;
445897d8846cSMatthew Knepley   PetscObjectState state;
4459e6ccafaeSMatthew G Knepley 
4460e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4461e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44624f572ea9SToby Isaac   PetscAssertPointer(subpointIS, 2);
44639566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
44649566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
44659566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
446697d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
44673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4468e6ccafaeSMatthew G Knepley }
4469559a1558SMatthew G. Knepley 
4470559a1558SMatthew G. Knepley /*@
447120f4b53cSBarry Smith   DMGetEnclosureRelation - Get the relationship between `dmA` and `dmB`
4472559a1558SMatthew G. Knepley 
4473559a1558SMatthew G. Knepley   Input Parameters:
447420f4b53cSBarry Smith + dmA - The first `DM`
447520f4b53cSBarry Smith - dmB - The second `DM`
4476559a1558SMatthew G. Knepley 
4477559a1558SMatthew G. Knepley   Output Parameter:
447820f4b53cSBarry Smith . rel - The relation of `dmA` to `dmB`
4479559a1558SMatthew G. Knepley 
4480a6e0b375SMatthew G. Knepley   Level: intermediate
4481559a1558SMatthew G. Knepley 
44821cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosurePoint()`
4483559a1558SMatthew G. Knepley @*/
4484d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4485d71ae5a4SJacob Faibussowitsch {
4486a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
4487559a1558SMatthew G. Knepley   DMLabel  spmap;
4488a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4489559a1558SMatthew G. Knepley 
449044171101SMatthew G. Knepley   PetscFunctionBegin;
44914f572ea9SToby Isaac   PetscAssertPointer(rel, 3);
4492a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
44933ba16761SJacob Faibussowitsch   if (!dmA || !dmB) PetscFunctionReturn(PETSC_SUCCESS);
4494a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4495064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
44969371c9d4SSatish Balay   if (dmA == dmB) {
44979371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
44983ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
44999371c9d4SSatish Balay   }
45009566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
45019566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
45029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
45039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4504a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4505a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4506a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
4507a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4508a6e0b375SMatthew G. Knepley     goto end;
4509559a1558SMatthew G. Knepley   }
4510a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4511a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4512a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4513a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
45149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4515a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4516a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4517a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4518a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4519a6e0b375SMatthew G. Knepley   } else {
4520a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4521a6e0b375SMatthew G. Knepley   }
4522a6e0b375SMatthew G. Knepley end:
45239566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
45249566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
45253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4526559a1558SMatthew G. Knepley }
452744171101SMatthew G. Knepley 
452844171101SMatthew G. Knepley /*@
452920f4b53cSBarry Smith   DMGetEnclosurePoint - Get the point `pA` in `dmA` which corresponds to the point `pB` in `dmB`
453044171101SMatthew G. Knepley 
453144171101SMatthew G. Knepley   Input Parameters:
453220f4b53cSBarry Smith + dmA   - The first `DM`
453320f4b53cSBarry Smith . dmB   - The second `DM`
453420f4b53cSBarry Smith . etype - The type of enclosure relation that `dmA` has to `dmB`
453520f4b53cSBarry Smith - pB    - A point of `dmB`
453644171101SMatthew G. Knepley 
453744171101SMatthew G. Knepley   Output Parameter:
453820f4b53cSBarry Smith . pA - The corresponding point of `dmA`
453944171101SMatthew G. Knepley 
4540a6e0b375SMatthew G. Knepley   Level: intermediate
454144171101SMatthew G. Knepley 
45421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosureRelation()`
454344171101SMatthew G. Knepley @*/
4544d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
4545d71ae5a4SJacob Faibussowitsch {
4546a6e0b375SMatthew G. Knepley   DM              sdm;
4547a6e0b375SMatthew G. Knepley   IS              subpointIS;
4548a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4549a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
455044171101SMatthew G. Knepley 
455144171101SMatthew G. Knepley   PetscFunctionBegin;
4552a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4553a6e0b375SMatthew G. Knepley   switch (etype) {
4554a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4555a6e0b375SMatthew G. Knepley     sdm = dmB;
45569566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
45579566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4558a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
45599566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4560a6e0b375SMatthew G. Knepley     break;
4561a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4562a6e0b375SMatthew G. Knepley     sdm = dmA;
45639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
45649566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
45659566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
45669566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4567a6e0b375SMatthew G. Knepley     if (*pA < 0) {
45689566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
45699566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
457063a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4571a6e0b375SMatthew G. Knepley     }
45729566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4573a6e0b375SMatthew G. Knepley     break;
4574a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
4575d71ae5a4SJacob Faibussowitsch   case DM_ENC_NONE:
4576d71ae5a4SJacob Faibussowitsch     *pA = pB;
4577d71ae5a4SJacob Faibussowitsch     break;
45789371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4579a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
458044171101SMatthew G. Knepley 
45819566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
45829566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
45839371c9d4SSatish Balay   } break;
4584d71ae5a4SJacob Faibussowitsch   default:
4585d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
458644171101SMatthew G. Knepley   }
45873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
458844171101SMatthew G. Knepley }
4589