xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision bbb57e682000431fc78c3bae656318d784c00124)
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;
8541e3e744Sksagiyam     for (c = cStart; c < cEnd; ++c) cellOwners[c - cStart] = (PetscInt)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 {
192*bbb57e68SMatthew G. Knepley   DM                     plex;
193827c4036SVaclav Hapla   DMPlexInterpolatedFlag flg;
194cd0c2139SMatthew G Knepley 
195cd0c2139SMatthew G Knepley   PetscFunctionBegin;
196827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
197*bbb57e68SMatthew G. Knepley   PetscCall(DMConvert(dm, DMPLEX, &plex));
198*bbb57e68SMatthew 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");
200*bbb57e68SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces_Internal(plex, val, 0, label, PETSC_FALSE));
201*bbb57e68SMatthew 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 
1035cd0c2139SMatthew G Knepley /*@C
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 
2014d71ae5a4SJacob Faibussowitsch static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label)
2015d71ae5a4SJacob Faibussowitsch {
2016accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
2017accc9626SMatthew G. Knepley   const PetscInt *points;
2018accc9626SMatthew G. Knepley   PetscInt        dim, numPoints, p, shift = 100, shift2 = 200;
2019accc9626SMatthew G. Knepley 
2020accc9626SMatthew G. Knepley   PetscFunctionBegin;
2021accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
2022accc9626SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
2023accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
2024accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
2025accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
2026accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
2027accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
20283ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2029accc9626SMatthew G. Knepley   }
2030accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
2031accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
2032accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
2033accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
2034accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
2035accc9626SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
2036accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
2037accc9626SMatthew G. Knepley     const PetscInt *support;
2038accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
2039accc9626SMatthew G. Knepley 
2040accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
2041accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
2042accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
2043accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
2044accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
2045accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
2046accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
2047e1a13daeSMatthew G. Knepley     /* Check that this face is not incident on only unsplit faces, meaning has at least one split face */
2048e1a13daeSMatthew G. Knepley     {
2049e1a13daeSMatthew G. Knepley       PetscInt *closure = NULL;
2050e1a13daeSMatthew G. Knepley       PetscBool split   = PETSC_FALSE;
2051e1a13daeSMatthew G. Knepley       PetscInt  closureSize, cl;
2052e1a13daeSMatthew G. Knepley 
2053e1a13daeSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2054e1a13daeSMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2055e1a13daeSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
20569371c9d4SSatish Balay         if ((valA >= 0) && (valA <= dim)) {
20579371c9d4SSatish Balay           split = PETSC_TRUE;
20589371c9d4SSatish Balay           break;
20599371c9d4SSatish Balay         }
2060e1a13daeSMatthew G. Knepley       }
2061e1a13daeSMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2062e1a13daeSMatthew G. Knepley       if (!split) continue;
2063e1a13daeSMatthew G. Knepley     }
2064accc9626SMatthew G. Knepley     /* Split the face */
2065accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, point, &valA));
2066accc9626SMatthew G. Knepley     PetscCall(DMLabelClearValue(label, point, valA));
2067accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, dim - 1));
2068accc9626SMatthew G. Knepley     /* Label its closure:
2069accc9626SMatthew G. Knepley       unmarked: label as unsplit
2070accc9626SMatthew G. Knepley       incident: relabel as split
2071accc9626SMatthew G. Knepley       split:    do nothing
2072accc9626SMatthew G. Knepley     */
2073accc9626SMatthew G. Knepley     {
2074accc9626SMatthew G. Knepley       PetscInt *closure = NULL;
2075accc9626SMatthew G. Knepley       PetscInt  closureSize, cl, dep;
2076accc9626SMatthew G. Knepley 
2077accc9626SMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2078accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2079accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
2080accc9626SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
2081accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2082accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
2083accc9626SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2084accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2085accc9626SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], valA));
2086accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], dep));
2087accc9626SMatthew G. Knepley         }
2088accc9626SMatthew G. Knepley       }
2089accc9626SMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2090accc9626SMatthew G. Knepley     }
2091accc9626SMatthew G. Knepley   }
2092accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2093accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
20943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2095accc9626SMatthew G. Knepley }
2096accc9626SMatthew G. Knepley 
2097cd0c2139SMatthew G Knepley /*@
20980f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
2099cd0c2139SMatthew G Knepley   to complete the surface
2100cd0c2139SMatthew G Knepley 
2101cd0c2139SMatthew G Knepley   Input Parameters:
210220f4b53cSBarry Smith + dm     - The `DM`
210320f4b53cSBarry Smith . label  - A `DMLabel` marking the surface
210420f4b53cSBarry Smith . blabel - A `DMLabel` marking the vertices on the boundary which will not be duplicated, or `NULL` to find them automatically
210520f4b53cSBarry Smith . bvalue - Value of `DMLabel` marking the vertices on the boundary
2106bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
210720f4b53cSBarry Smith - subdm  - The `DM` associated with the label, or `NULL`
2108cd0c2139SMatthew G Knepley 
2109cd0c2139SMatthew G Knepley   Output Parameter:
211020f4b53cSBarry Smith . label - A `DMLabel` marking all surface points
21110f66a230SMatthew G. Knepley 
2112cd0c2139SMatthew G Knepley   Level: developer
2113cd0c2139SMatthew G Knepley 
211420f4b53cSBarry Smith   Note:
211520f4b53cSBarry Smith   The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
211620f4b53cSBarry Smith 
21171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2118cd0c2139SMatthew G Knepley @*/
2119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, DM subdm)
2120d71ae5a4SJacob Faibussowitsch {
2121d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2122accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
212347946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2124bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
2125accc9626SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = 300, dim, depth, numPoints, numSubpoints, p, val;
2126cd0c2139SMatthew G Knepley 
2127cd0c2139SMatthew G Knepley   PetscFunctionBegin;
21289566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
21299566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
21309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
213147946fd8SMatthew G. Knepley   if (subdm) {
21329566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
213347946fd8SMatthew G. Knepley     if (subpointIS) {
21349566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
21359566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
213647946fd8SMatthew G. Knepley     }
213747946fd8SMatthew G. Knepley   }
2138d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
21399566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2140accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
21419566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
21429566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2143d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2144cd0c2139SMatthew G Knepley     const PetscInt *support;
2145cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2146cd0c2139SMatthew G Knepley 
21479566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2148c4419245SMatthew G. Knepley #if 0
2149c4419245SMatthew G. Knepley     if (supportSize != 2) {
2150c4419245SMatthew G. Knepley       const PetscInt *lp;
2151c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2152c4419245SMatthew G. Knepley 
2153c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2154c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
21559566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
21569566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
215763a3b9bcSJacob 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);
2158c4419245SMatthew G. Knepley     }
2159c4419245SMatthew G. Knepley #endif
21609566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2161cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
21620f66a230SMatthew G. Knepley       const PetscInt *cone;
2163cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
21640f66a230SMatthew G. Knepley       PetscBool       pos;
2165cd0c2139SMatthew G Knepley 
21669566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
21679566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
21689566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
21690f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
21700f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
21719566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
21729566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2173cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2174cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2175cd0c2139SMatthew G Knepley 
21769566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2177cd0c2139SMatthew G Knepley         if (val == -1) {
2178cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2179cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2180cd0c2139SMatthew G Knepley 
21819566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2182cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2183cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2184a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2185cd0c2139SMatthew G Knepley 
21869566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
21879566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2188a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim - 1) && (bval < 0)) {
21899566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2190cd0c2139SMatthew G Knepley               break;
2191cd0c2139SMatthew G Knepley             }
2192cd0c2139SMatthew G Knepley           }
21939566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2194cd0c2139SMatthew G Knepley         }
2195cd0c2139SMatthew G Knepley       }
2196cd0c2139SMatthew G Knepley     }
2197cd0c2139SMatthew G Knepley   }
2198accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2199accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2200a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
220186200784SMatthew G. Knepley   if (blabel) {
2202accc9626SMatthew G. Knepley     IS bdIS;
2203accc9626SMatthew G. Knepley 
2204caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2205accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2206accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2207a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2208a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2209a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2210a0541d8aSMatthew G. Knepley 
22119566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2212a0541d8aSMatthew G. Knepley       if (bval >= 0) {
22139566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2214f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2215f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
22169566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2217f7019248SMatthew G. Knepley         }
2218f7019248SMatthew G. Knepley       }
2219f7019248SMatthew G. Knepley     }
2220f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2221f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2222f7019248SMatthew G. Knepley       PetscInt       val, bval;
2223f7019248SMatthew G. Knepley 
22249566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2225f7019248SMatthew G. Knepley       if (bval >= 0) {
222686200784SMatthew G. Knepley         const PetscInt *cone, *support;
222786200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
222886200784SMatthew G. Knepley 
2229a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
22309566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2231e1a13daeSMatthew 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);
22329566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
22339566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
22342c06a818SMatthew G. Knepley         /* Check for cross-edge
22352c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
223686200784SMatthew G. Knepley         if (val != 0) continue;
22379566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
22389566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
223986200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
22409566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
22419566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
224263a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
22439566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
22449566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
22459566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
22469566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
224786200784SMatthew G. Knepley         }
2248a0541d8aSMatthew G. Knepley       }
2249a0541d8aSMatthew G. Knepley     }
2250accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2251accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2252a0541d8aSMatthew G. Knepley   }
2253b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2254b6dfa339SMatthew G. Knepley   {
2255b6dfa339SMatthew G. Knepley     PetscSF         sf;
2256b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2257b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2258b6dfa339SMatthew G. Knepley 
2259b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2260b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2261accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2262accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2263accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2264accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2265b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2266b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2267b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2268b6dfa339SMatthew G. Knepley         PetscInt       val;
2269b6dfa339SMatthew G. Knepley 
2270b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2271b6dfa339SMatthew G. Knepley         if (l >= 0) {
2272b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2273b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2274b6dfa339SMatthew G. Knepley 
2275b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2276b6dfa339SMatthew 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);
2277b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2278b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2279b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2280accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2281b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2282b6dfa339SMatthew G. Knepley 
2283b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2284b6dfa339SMatthew 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);
2285b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2286b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2287b6dfa339SMatthew G. Knepley           }
2288b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2289b6dfa339SMatthew G. Knepley         }
2290b6dfa339SMatthew G. Knepley       }
2291b6dfa339SMatthew G. Knepley     }
2292b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2293b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2294accc9626SMatthew G. Knepley   }
2295accc9626SMatthew G. Knepley divide:
2296b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2297accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
2298accc9626SMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label));
22993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2300cd0c2139SMatthew G Knepley }
2301cd0c2139SMatthew G Knepley 
2302720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
230366976f2fSJacob Faibussowitsch static PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2304d71ae5a4SJacob Faibussowitsch {
2305720e594eSMatthew G. Knepley   IS              subpointIS;
2306720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2307720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2308720e594eSMatthew G. Knepley 
2309720e594eSMatthew G. Knepley   PetscFunctionBegin;
23103ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
23119566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
23129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
23133ba16761SJacob Faibussowitsch   if (!subpointIS) PetscFunctionReturn(PETSC_SUCCESS);
23149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
23159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
23169566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2317720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2318720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2319720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2320720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2321720e594eSMatthew G. Knepley 
23229566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2323720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2324720e594eSMatthew G. Knepley       PetscInt value = 0;
2325720e594eSMatthew G. Knepley 
2326720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
23279566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
23289371c9d4SSatish Balay       if (value == defaultValue) {
23299371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
23309371c9d4SSatish Balay         break;
23319371c9d4SSatish Balay       }
2332720e594eSMatthew G. Knepley     }
23339566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2334720e594eSMatthew G. Knepley     if (invalidCell) {
23359566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23369566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
23379566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
233863a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2339720e594eSMatthew G. Knepley     }
2340720e594eSMatthew G. Knepley   }
23419566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2343720e594eSMatthew G. Knepley }
2344720e594eSMatthew G. Knepley 
2345c08575a3SMatthew G. Knepley /*@
23463cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
23473cf72582SMatthew G. Knepley 
234820f4b53cSBarry Smith   Collective
23493cf72582SMatthew G. Knepley 
23503cf72582SMatthew G. Knepley   Input Parameters:
235120f4b53cSBarry Smith + dm      - The original `DM`
2352720e594eSMatthew G. Knepley . label   - The label specifying the interface vertices
2353caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2354caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
23553cf72582SMatthew G. Knepley 
23563cf72582SMatthew G. Knepley   Output Parameters:
235720f4b53cSBarry Smith + hybridLabel - The label fully marking the interface, or `NULL` if no output is desired
235820f4b53cSBarry Smith . splitLabel  - The label containing the split points, or `NULL` if no output is desired
235920f4b53cSBarry Smith . dmInterface - The new interface `DM`, or `NULL`
236020f4b53cSBarry Smith - dmHybrid    - The new `DM` with cohesive cells
23613cf72582SMatthew G. Knepley 
236220f4b53cSBarry Smith   Level: developer
236320f4b53cSBarry Smith 
236420f4b53cSBarry Smith   Note:
236520f4b53cSBarry Smith   The hybridLabel indicates what parts of the original mesh impinged on the division surface. For points
23666eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
23676eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
23686eccb800SMatthew 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
23696eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
23706eccb800SMatthew Knepley 
23716eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
237240cdb708SMatthew 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
23736eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
23746eccb800SMatthew Knepley 
237520f4b53cSBarry Smith   The dmInterface is a `DM` built from the original division surface. It has a label which can be retrieved using
237620f4b53cSBarry Smith   `DMPlexGetSubpointMap()` which maps each point back to the point in the surface of the original mesh.
23776eccb800SMatthew Knepley 
23781cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
23793cf72582SMatthew G. Knepley @*/
2380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
2381d71ae5a4SJacob Faibussowitsch {
23823cf72582SMatthew G. Knepley   DM       idm;
23837db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
23843cf72582SMatthew G. Knepley   PetscInt dim;
23853cf72582SMatthew G. Knepley 
23863cf72582SMatthew G. Knepley   PetscFunctionBegin;
23873cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23884f572ea9SToby Isaac   if (label) PetscAssertPointer(label, 2);
23894f572ea9SToby Isaac   if (bdlabel) PetscAssertPointer(bdlabel, 3);
23904f572ea9SToby Isaac   if (hybridLabel) PetscAssertPointer(hybridLabel, 5);
23914f572ea9SToby Isaac   if (splitLabel) PetscAssertPointer(splitLabel, 6);
23924f572ea9SToby Isaac   if (dmInterface) PetscAssertPointer(dmInterface, 7);
23934f572ea9SToby Isaac   PetscAssertPointer(dmHybrid, 8);
23949566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
23959566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
23969566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
23979566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
23989566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
23999566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
24009566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
24017db7e0a7SMatthew G. Knepley   if (splitLabel) {
24027db7e0a7SMatthew G. Knepley     const char *name;
24037db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
24047db7e0a7SMatthew G. Knepley 
24059566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
2406c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(sname, name, sizeof(sname)));
2407c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(sname, " split", sizeof(sname)));
24089566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
24097db7e0a7SMatthew G. Knepley   }
2410caf9e14dSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, idm));
24119371c9d4SSatish Balay   if (dmInterface) {
24129371c9d4SSatish Balay     *dmInterface = idm;
24139371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
24149566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
24155de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
24163cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
24179566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
24187db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
24194a7ee7d0SMatthew G. Knepley   {
24204a7ee7d0SMatthew G. Knepley     DM      cdm;
24214a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
24224a7ee7d0SMatthew G. Knepley 
24234a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
24249566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
24259566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
24269566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
24274a7ee7d0SMatthew G. Knepley   }
24283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2429cd0c2139SMatthew G Knepley }
2430cd0c2139SMatthew G Knepley 
2431efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2432efa14ee0SMatthew G Knepley 
2433efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2434efa14ee0SMatthew G Knepley */
2435d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2436d71ae5a4SJacob Faibussowitsch {
2437fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2438efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2439412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2440efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2441efa14ee0SMatthew G Knepley 
2442efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2443efa14ee0SMatthew G Knepley   *numFaces = 0;
2444efa14ee0SMatthew G Knepley   *nFV      = 0;
24459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24469566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
244777d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
24489566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
244948a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2450efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
24519566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2452efa14ee0SMatthew G Knepley   if (subvertexIS) {
24539566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24549566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2455efa14ee0SMatthew G Knepley   }
2456efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2457efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24580298fd71SBarry Smith     PetscInt      *star   = NULL;
2459efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2460efa14ee0SMatthew G Knepley 
24619566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2462efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2463efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2464efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2465efa14ee0SMatthew G Knepley     }
2466efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2467efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
24680298fd71SBarry Smith       PetscInt      *closure = NULL;
2469efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2470efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2471efa14ee0SMatthew G Knepley 
24729566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
247365560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
247463a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
24759566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2476efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2477efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2478efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2479efa14ee0SMatthew G Knepley 
2480efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2481efa14ee0SMatthew G Knepley           ++numCorners;
24829566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2483830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2484efa14ee0SMatthew G Knepley         }
2485efa14ee0SMatthew G Knepley       }
24869566063dSJacob Faibussowitsch       if (!(*nFV)) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
248763a3b9bcSJacob 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);
2488efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2489007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2490007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2491007baee2SMatthew G. Knepley 
2492efa14ee0SMatthew G Knepley         ++(*numFaces);
249348a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
24949566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
249548a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
24969566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2497efa14ee0SMatthew G Knepley       }
24989566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2499efa14ee0SMatthew G Knepley     }
25009566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2501efa14ee0SMatthew G Knepley   }
250248a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25039566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25049566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2506efa14ee0SMatthew G Knepley }
2507efa14ee0SMatthew G Knepley 
2508d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2509d71ae5a4SJacob Faibussowitsch {
251034b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2511efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2512412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2513efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2514efa14ee0SMatthew G Knepley 
2515efa14ee0SMatthew G Knepley   PetscFunctionBegin;
25169566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25179566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
251848a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2519efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
252034b4c39eSMatthew G. Knepley   if (vertexLabel) {
25219566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2522efa14ee0SMatthew G Knepley     if (subvertexIS) {
25239566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
25249566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2525efa14ee0SMatthew G Knepley     }
252634b4c39eSMatthew G. Knepley   }
2527efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2528efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
25290298fd71SBarry Smith     PetscInt      *star   = NULL;
2530efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2531efa14ee0SMatthew G Knepley 
25329566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2533efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2534efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2535158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2536158acfadSMatthew G. Knepley 
2537158acfadSMatthew G. Knepley       if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) {
2538158acfadSMatthew G. Knepley         if (markedFaces) {
25399566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2540158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2541158acfadSMatthew G. Knepley         }
2542158acfadSMatthew G. Knepley         star[numFaces++] = point;
2543158acfadSMatthew G. Knepley       }
2544efa14ee0SMatthew G Knepley     }
2545efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2546efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
25470298fd71SBarry Smith       PetscInt      *closure = NULL;
2548efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2549efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2550efa14ee0SMatthew G Knepley 
25519566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2552efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
255363a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
25549566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2555efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2556efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2557efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2558efa14ee0SMatthew G Knepley 
2559efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
25609566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2561830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2562efa14ee0SMatthew G Knepley         }
2563efa14ee0SMatthew G Knepley       }
2564efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2565efa14ee0SMatthew G Knepley         const PetscInt *support;
2566efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2567efa14ee0SMatthew G Knepley 
2568efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2569efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2570efa14ee0SMatthew G Knepley 
2571efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2572efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
25739566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2574efa14ee0SMatthew G Knepley               break;
2575efa14ee0SMatthew G Knepley             }
2576efa14ee0SMatthew G Knepley           }
2577efa14ee0SMatthew G Knepley         }
25789566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
25799566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
258048a46eb9SPierre Jolivet         for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2581efa14ee0SMatthew G Knepley       }
25829566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2583efa14ee0SMatthew G Knepley     }
25849566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2585efa14ee0SMatthew G Knepley   }
25869566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25879566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25889566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2590efa14ee0SMatthew G Knepley }
2591efa14ee0SMatthew G Knepley 
2592d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2593d71ae5a4SJacob Faibussowitsch {
259427c04023SMatthew G. Knepley   DMLabel         label = NULL;
2595766ab985SMatthew G. Knepley   const PetscInt *cone;
25969fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2597766ab985SMatthew G. Knepley 
2598812bfc34SJed Brown   PetscFunctionBegin;
2599c0ed958bSJed Brown   *numFaces = 0;
2600c0ed958bSJed Brown   *nFV      = 0;
26019566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2602fed694aaSMatthew G. Knepley   *subCells = NULL;
26039566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26053ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
260627c04023SMatthew G. Knepley   if (label) {
260727c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
260827c04023SMatthew G. Knepley       PetscInt val;
260927c04023SMatthew G. Knepley 
26109566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
261127c04023SMatthew G. Knepley       if (val == value) {
261227c04023SMatthew G. Knepley         ++(*numFaces);
26139566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
261427c04023SMatthew G. Knepley       }
261527c04023SMatthew G. Knepley     }
261627c04023SMatthew G. Knepley   } else {
2617766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
26189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
261927c04023SMatthew G. Knepley   }
26209566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
26213ba16761SJacob Faibussowitsch   if (!(*numFaces)) PetscFunctionReturn(PETSC_SUCCESS);
26229fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2623766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2624766ab985SMatthew G. Knepley     const PetscInt *cells;
2625766ab985SMatthew G. Knepley     PetscInt        numCells;
2626766ab985SMatthew G. Knepley 
262727c04023SMatthew G. Knepley     if (label) {
262827c04023SMatthew G. Knepley       PetscInt val;
262927c04023SMatthew G. Knepley 
26309566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
263127c04023SMatthew G. Knepley       if (val != value) continue;
263227c04023SMatthew G. Knepley     }
26339566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
263448a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2635766ab985SMatthew G. Knepley     /* Negative face */
26369566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
263727234c99SMatthew G. Knepley     /* Not true in parallel
263808401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2639766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
26409566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
264127234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2642766ab985SMatthew G. Knepley     }
26439566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2644766ab985SMatthew G. Knepley     /* Positive face is not included */
2645766ab985SMatthew G. Knepley   }
26463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2647766ab985SMatthew G. Knepley }
2648766ab985SMatthew G. Knepley 
2649d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2650d71ae5a4SJacob Faibussowitsch {
2651766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2652766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2653766ab985SMatthew G. Knepley 
2654812bfc34SJed Brown   PetscFunctionBegin;
26559566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26573ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
26589566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
26599566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2660766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2661766ab985SMatthew G. Knepley     const PetscInt *cone;
2662766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2663b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2664766ab985SMatthew G. Knepley 
266527c04023SMatthew G. Knepley     if (label) {
26669566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
266727c04023SMatthew G. Knepley       if (val != value) continue;
266827c04023SMatthew G. Knepley     }
26699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
26709566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
26719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
26721dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2673b3154360SMatthew G. Knepley     /* Negative face */
26749566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2675766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2676766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2677766ab985SMatthew G. Knepley 
2678766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2679766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
26809566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2681766ab985SMatthew G. Knepley           break;
2682766ab985SMatthew G. Knepley         }
2683766ab985SMatthew G. Knepley       }
2684766ab985SMatthew G. Knepley     }
26859566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2686766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2687766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2688766ab985SMatthew G. Knepley       const PetscInt *support;
2689766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2690766ab985SMatthew G. Knepley 
26919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
269208401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
26939566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
269448a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2695766ab985SMatthew G. Knepley     }
2696766ab985SMatthew G. Knepley   }
26979566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
26983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2699766ab985SMatthew G. Knepley }
2700766ab985SMatthew G. Knepley 
2701d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2702d71ae5a4SJacob Faibussowitsch {
270382f516ccSBarry Smith   MPI_Comm       comm;
2704e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2705e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2706e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2707e6ccafaeSMatthew G Knepley 
270882f516ccSBarry Smith   PetscFunctionBegin;
27099566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
27109566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
271163a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2712e6ccafaeSMatthew G Knepley 
2713ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2714ddeab2a6SMatthew G. Knepley     /* Triangle */
2715e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2716e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2717ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2718ddeab2a6SMatthew G. Knepley     /* Triangle */
2719ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2720ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2721ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2722ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2723ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2724ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2725e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2726e6ccafaeSMatthew G Knepley     /* Quadratic line */
2727e6ccafaeSMatthew G Knepley     faceSize  = 1;
2728e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2729e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2730e6ccafaeSMatthew G Knepley     /* Quads */
2731e6ccafaeSMatthew G Knepley     faceSize = 2;
2732e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2733e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2734e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2735e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2736e6ccafaeSMatthew G Knepley     } else {
2737e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2738e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2739e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2740e6ccafaeSMatthew G Knepley     }
2741e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2742e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2743e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2744e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2745e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2746e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2747e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2748e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2749e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2750e6ccafaeSMatthew G Knepley       2, 3, 5, /* left */
2751e6ccafaeSMatthew G Knepley     };
2752e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2753e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2754e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2755e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2756e6ccafaeSMatthew G Knepley     };
2757e6ccafaeSMatthew G Knepley 
2758e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
27599566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2760e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2761e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2762e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2763e6ccafaeSMatthew G Knepley 
27649371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) {
2765e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2766e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2767e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2768e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2769e6ccafaeSMatthew G Knepley               break;
2770e6ccafaeSMatthew G Knepley             }
2771e6ccafaeSMatthew G Knepley           }
2772e6ccafaeSMatthew G Knepley         }
2773e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2774e6ccafaeSMatthew G Knepley         break;
2775e6ccafaeSMatthew G Knepley       }
2776e6ccafaeSMatthew G Knepley     }
277728b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2778e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
27793ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2780e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2781e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2782e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2783e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2784e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2785e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2786e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2787e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2788e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2789e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2790e6ccafaeSMatthew G Knepley       0, 3, 7, /* left */
2791e6ccafaeSMatthew G Knepley     };
2792e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2793e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2794e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2795e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2796e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2797e6ccafaeSMatthew G Knepley     };
2798e6ccafaeSMatthew G Knepley 
2799e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
28009566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2801e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2802e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2803e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2804e6ccafaeSMatthew G Knepley 
28059371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) {
2806e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2807e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2808e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2809e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2810e6ccafaeSMatthew G Knepley               break;
2811e6ccafaeSMatthew G Knepley             }
2812e6ccafaeSMatthew G Knepley           }
2813e6ccafaeSMatthew G Knepley         }
2814e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2815e6ccafaeSMatthew G Knepley         break;
2816e6ccafaeSMatthew G Knepley       }
2817e6ccafaeSMatthew G Knepley     }
281828b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2819e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28203ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2821e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2822e6ccafaeSMatthew G Knepley     /* Hexes
2823e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2824e6ccafaeSMatthew G Knepley        pointing up at the second.
2825e6ccafaeSMatthew G Knepley 
2826e6ccafaeSMatthew G Knepley           7---6
2827e6ccafaeSMatthew G Knepley          /|  /|
2828e6ccafaeSMatthew G Knepley         4---5 |
2829ddeab2a6SMatthew G. Knepley         | 1-|-2
2830e6ccafaeSMatthew G Knepley         |/  |/
2831ddeab2a6SMatthew G. Knepley         0---3
2832e6ccafaeSMatthew G Knepley 
2833e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2834e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2835e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2836e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2837e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2838e6ccafaeSMatthew G Knepley       0, 1, 2, 3, /* bottom */
2839e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2840ddeab2a6SMatthew G. Knepley       0, 3, 4, 5, /* front */
2841ddeab2a6SMatthew G. Knepley       2, 3, 5, 6, /* right */
2842ddeab2a6SMatthew G. Knepley       1, 2, 6, 7, /* back */
2843ddeab2a6SMatthew G. Knepley       0, 1, 4, 7, /* left */
2844e6ccafaeSMatthew G Knepley     };
2845e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2846ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2847e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2848ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2849ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2850ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2851ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2852e6ccafaeSMatthew G Knepley     };
2853e6ccafaeSMatthew G Knepley 
2854e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
28559566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2856e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2857e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2858e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2859e6ccafaeSMatthew G Knepley 
28609371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) {
2861e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2862e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2863e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2864e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2865e6ccafaeSMatthew G Knepley               break;
2866e6ccafaeSMatthew G Knepley             }
2867e6ccafaeSMatthew G Knepley           }
2868e6ccafaeSMatthew G Knepley         }
2869e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2870e6ccafaeSMatthew G Knepley         break;
2871e6ccafaeSMatthew G Knepley       }
2872e6ccafaeSMatthew G Knepley     }
287328b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2874e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28753ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2876e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2877e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2878e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2879e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2880e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2881e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2882e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2883e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2884e6ccafaeSMatthew G Knepley       0, 3, 4, 6, 7, 9, /* front */
2885e6ccafaeSMatthew G Knepley       1, 4, 5, 7, 8, 9, /* right */
2886e6ccafaeSMatthew G Knepley       2, 3, 5, 6, 8, 9, /* left */
2887e6ccafaeSMatthew G Knepley     };
2888e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2889e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2890e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2891e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2892e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2893e6ccafaeSMatthew G Knepley     };
2894e6ccafaeSMatthew G Knepley 
2895e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
28969566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2897e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2898e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2899e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2900e6ccafaeSMatthew G Knepley 
29019371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) {
2902e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2903e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2904e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2905e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2906e6ccafaeSMatthew G Knepley               break;
2907e6ccafaeSMatthew G Knepley             }
2908e6ccafaeSMatthew G Knepley           }
2909e6ccafaeSMatthew G Knepley         }
2910e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2911e6ccafaeSMatthew G Knepley         break;
2912e6ccafaeSMatthew G Knepley       }
2913e6ccafaeSMatthew G Knepley     }
291428b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2915e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29163ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2917e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2918e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2919e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2920e6ccafaeSMatthew G Knepley        pointing up at the second.
2921e6ccafaeSMatthew G Knepley 
2922e6ccafaeSMatthew G Knepley          7---6
2923e6ccafaeSMatthew G Knepley         /|  /|
2924e6ccafaeSMatthew G Knepley        4---5 |
2925e6ccafaeSMatthew G Knepley        | 3-|-2
2926e6ccafaeSMatthew G Knepley        |/  |/
2927e6ccafaeSMatthew G Knepley        0---1
2928e6ccafaeSMatthew G Knepley 
2929e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2930e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2931e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2932e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2933e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2934e6ccafaeSMatthew G Knepley       0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2935e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2936e6ccafaeSMatthew G Knepley       0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2937e6ccafaeSMatthew G Knepley       1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2938e6ccafaeSMatthew G Knepley       2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2939e6ccafaeSMatthew G Knepley       0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2940e6ccafaeSMatthew G Knepley     };
2941e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2942e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2943e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2944e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2945e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2946e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2947e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2948e6ccafaeSMatthew G Knepley     };
2949e6ccafaeSMatthew G Knepley 
2950e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
29519566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2952e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2953e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2954e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2955e6ccafaeSMatthew G Knepley 
29569371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) {
2957e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2958e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2959e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2960e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2961e6ccafaeSMatthew G Knepley               break;
2962e6ccafaeSMatthew G Knepley             }
2963e6ccafaeSMatthew G Knepley           }
2964e6ccafaeSMatthew G Knepley         }
2965e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2966e6ccafaeSMatthew G Knepley         break;
2967e6ccafaeSMatthew G Knepley       }
2968e6ccafaeSMatthew G Knepley     }
296928b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2970e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29713ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2972e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2973e6ccafaeSMatthew G Knepley   if (!posOrient) {
29749566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
2975e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
2976e6ccafaeSMatthew G Knepley   } else {
29779566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
2978e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2979e6ccafaeSMatthew G Knepley   }
2980e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
29813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2982e6ccafaeSMatthew G Knepley }
2983e6ccafaeSMatthew G Knepley 
2984c08575a3SMatthew G. Knepley /*@
2985c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2986c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2987c08575a3SMatthew G. Knepley 
298820f4b53cSBarry Smith   Not Collective
2989c08575a3SMatthew G. Knepley 
2990c08575a3SMatthew G. Knepley   Input Parameters:
2991c08575a3SMatthew G. Knepley + dm           - The original mesh
2992c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2993c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2994c08575a3SMatthew G. Knepley . face         - The face vertices
2995c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2996c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2997c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2998c08575a3SMatthew G. Knepley 
2999d8d19677SJose E. Roman   Output Parameters:
3000c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
300120f4b53cSBarry Smith - posOriented  - `PETSC_TRUE` if the face was oriented with outward normal
3002c08575a3SMatthew G. Knepley 
3003c08575a3SMatthew G. Knepley   Level: developer
3004c08575a3SMatthew G. Knepley 
30051cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
3006c08575a3SMatthew G. Knepley @*/
3007d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
3008d71ae5a4SJacob Faibussowitsch {
30090298fd71SBarry Smith   const PetscInt *cone = NULL;
3010e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
3011e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
3012e6ccafaeSMatthew G Knepley 
3013e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
30159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
3016e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
3017e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
3018e6ccafaeSMatthew G Knepley 
3019e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
3020e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
30219371c9d4SSatish Balay         found = PETSC_TRUE;
30229371c9d4SSatish Balay         break;
3023e6ccafaeSMatthew G Knepley       }
3024e6ccafaeSMatthew G Knepley     }
3025e6ccafaeSMatthew G Knepley     if (found) {
3026e6ccafaeSMatthew G Knepley       indices[v2]      = v;
3027e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
3028e6ccafaeSMatthew G Knepley       ++v2;
3029e6ccafaeSMatthew G Knepley     } else {
3030e6ccafaeSMatthew G Knepley       oppositeVertex = v;
3031e6ccafaeSMatthew G Knepley     }
3032e6ccafaeSMatthew G Knepley   }
30339566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
30343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3035e6ccafaeSMatthew G Knepley }
3036e6ccafaeSMatthew G Knepley 
3037e6ccafaeSMatthew G Knepley /*
3038cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
3039e6ccafaeSMatthew G Knepley 
304020f4b53cSBarry Smith   Not Collective
3041e6ccafaeSMatthew G Knepley 
3042e6ccafaeSMatthew G Knepley   Input Parameters:
304320f4b53cSBarry Smith   + dm              - The `DMPLEX`
3044e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
304520f4b53cSBarry Smith   . faceVertices    - The vertices in the face for `dm`
3046e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
304720f4b53cSBarry Smith   . numCorners      - The number of vertices in the `cell`
304820f4b53cSBarry Smith   . cell            - A cell in `dm` containing the face
3049e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
3050e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
3051e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
3052e6ccafaeSMatthew G Knepley 
30532fe279fdSBarry Smith   Output Parameter:
3054e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
3055e6ccafaeSMatthew G Knepley 
3056e6ccafaeSMatthew G Knepley   Level: developer
3057e6ccafaeSMatthew G Knepley */
3058d71ae5a4SJacob 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)
3059d71ae5a4SJacob Faibussowitsch {
306082f516ccSBarry Smith   MPI_Comm        comm;
3061e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
3062e6ccafaeSMatthew G Knepley   const PetscInt *faces;
3063e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
3064e6ccafaeSMatthew G Knepley 
3065e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30669566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
30679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
306863a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
3069e6ccafaeSMatthew G Knepley #if 0
3070e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
30719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3072e6ccafaeSMatthew G Knepley #else
3073e6ccafaeSMatthew G Knepley   {
3074e6ccafaeSMatthew G Knepley     PetscInt f;
3075e6ccafaeSMatthew G Knepley 
3076e6ccafaeSMatthew G Knepley     numFaces = 0;
30779566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3078e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
3079e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
3080e6ccafaeSMatthew G Knepley 
30819566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
30829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
3083e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
3084e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
3085e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
3086e6ccafaeSMatthew G Knepley         PetscInt       v;
3087e6ccafaeSMatthew G Knepley 
3088e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
3089e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
3090e6ccafaeSMatthew G Knepley         }
3091e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
3092e6ccafaeSMatthew G Knepley       }
3093e6ccafaeSMatthew G Knepley       if (d == dof) {
3094e6ccafaeSMatthew G Knepley         numFaces               = 1;
3095e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
3096e6ccafaeSMatthew G Knepley       }
3097e6ccafaeSMatthew G Knepley     }
3098e6ccafaeSMatthew G Knepley   }
3099e6ccafaeSMatthew G Knepley #endif
310063a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3101f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3102e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
31039566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3104e6ccafaeSMatthew G Knepley   } else {
3105e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3106e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3107e6ccafaeSMatthew G Knepley 
31089566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3109e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3110e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
3111e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
31129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3113e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3114e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3115e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3116e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3117e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3118e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3119e6ccafaeSMatthew G Knepley           break;
3120e6ccafaeSMatthew G Knepley         }
3121e6ccafaeSMatthew G Knepley       }
312263a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3123e6ccafaeSMatthew G Knepley     }
31249566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
31259566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
31269566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3127e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3128e6ccafaeSMatthew G Knepley   }
3129ef07cca7SMatthew G. Knepley #if 0
31309566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3131ef07cca7SMatthew G. Knepley #else
31329566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3133ef07cca7SMatthew G. Knepley #endif
31343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3135e6ccafaeSMatthew G Knepley }
3136e6ccafaeSMatthew G Knepley 
3137d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3138d71ae5a4SJacob Faibussowitsch {
313982f516ccSBarry Smith   MPI_Comm        comm;
314053156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3141efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
3142efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3143efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3144fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3145efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3146e6ccafaeSMatthew G Knepley 
3147e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
31489566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3149efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
31509566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
31519566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
31529566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3153efa14ee0SMatthew G Knepley   /* Setup chart */
31549566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
31559566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
31569566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
31579566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3158e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3159e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3160efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
3161e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
31629566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
31639566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
31649566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
31659566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
316648a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
316748a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
31689566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3169d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3170e6ccafaeSMatthew G Knepley   /* Create face cones */
31719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
31729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
31739566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3174e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3175e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3176efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
31770298fd71SBarry Smith     PetscInt      *closure = NULL;
3178efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3179e6ccafaeSMatthew G Knepley 
31809566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3181efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3182efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3183e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3184e6ccafaeSMatthew G Knepley 
3185efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3186efa14ee0SMatthew G Knepley         ++numCorners;
31879566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3188efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3189efa14ee0SMatthew G Knepley           closure[faceSize] = point;
319065560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3191e6ccafaeSMatthew G Knepley           ++faceSize;
3192e6ccafaeSMatthew G Knepley         }
3193e6ccafaeSMatthew G Knepley       }
3194e6ccafaeSMatthew G Knepley     }
319563a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
319648a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
31979566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3198e6ccafaeSMatthew G Knepley   }
31999566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
32009566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
32019566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3202e6ccafaeSMatthew G Knepley   /* Build coordinates */
3203efa14ee0SMatthew G Knepley   {
3204efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3205efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3206efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3207285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
320824640c55SToby Isaac     const char  *name;
3209efa14ee0SMatthew G Knepley 
32109566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
32119566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
32129566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
32139566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
32149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
32159566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
32169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3217efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3218efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3219efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3220efa14ee0SMatthew G Knepley       PetscInt       dof;
3221efa14ee0SMatthew G Knepley 
32229566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32239566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
32249566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3225e6ccafaeSMatthew G Knepley     }
32269566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
32279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
32289566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
32299566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
32309566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
32319566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
32329566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3233830e53efSMatthew G. Knepley     if (coordSize) {
32349566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
32359566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3236efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3237efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3238efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3239efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3240e6ccafaeSMatthew G Knepley 
32419566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32429566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
32439566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
32449566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
324563a3b9bcSJacob 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);
3246e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3247e6ccafaeSMatthew G Knepley       }
32489566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
32499566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
32503b399e24SMatthew G. Knepley     }
32519566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
32529566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3253e6ccafaeSMatthew G Knepley   }
3254efa14ee0SMatthew G Knepley   /* Cleanup */
32559566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
32569566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
32579566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
32589566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
32593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3260e6ccafaeSMatthew G Knepley }
3261e6ccafaeSMatthew G Knepley 
3262d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3263d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
3264d71ae5a4SJacob Faibussowitsch {
32653982b651SMatthew G. Knepley   PetscInt       subPoint;
32663982b651SMatthew G. Knepley   PetscErrorCode ierr;
32673982b651SMatthew G. Knepley 
32689371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
32699371c9d4SSatish Balay   if (ierr) return -1;
32703982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
32713982b651SMatthew G. Knepley }
32723982b651SMatthew G. Knepley 
3273d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3274d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[])
3275d71ae5a4SJacob Faibussowitsch {
3276dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3277dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3278dd05d810SMatthew G. Knepley 
3279dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3280dd05d810SMatthew G. Knepley   if (ierr) return -1;
3281dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3282dd05d810SMatthew G. Knepley }
3283dd05d810SMatthew G. Knepley 
3284d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3285d71ae5a4SJacob Faibussowitsch {
3286695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3287212103e5SMatthew Knepley   PetscInt Nl, l, d;
3288212103e5SMatthew Knepley 
3289212103e5SMatthew Knepley   PetscFunctionBegin;
3290695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3291695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3292695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
32939566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3294212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3295212103e5SMatthew Knepley     DMLabel         label, newlabel;
3296212103e5SMatthew Knepley     const char     *lname;
3297d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3298212103e5SMatthew Knepley     IS              valueIS;
3299212103e5SMatthew Knepley     const PetscInt *values;
3300212103e5SMatthew Knepley     PetscInt        Nv, v;
3301212103e5SMatthew Knepley 
33029566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
33039566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
33049566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
33059566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
33069566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3307d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
33089566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
33099566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
33109566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
33119566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
33129566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
33139566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
33149566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
33159566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3316212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3317212103e5SMatthew Knepley       IS              pointIS;
3318212103e5SMatthew Knepley       const PetscInt *points;
3319212103e5SMatthew Knepley       PetscInt        Np, p;
3320212103e5SMatthew Knepley 
33219566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
33229566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
33239566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3324212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3325212103e5SMatthew Knepley         const PetscInt point = points[p];
3326212103e5SMatthew Knepley         PetscInt       subp;
3327212103e5SMatthew Knepley 
33289566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3329212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
33309566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3331212103e5SMatthew Knepley       }
33329566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
33339566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3334212103e5SMatthew Knepley     }
33359566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
33369566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3337212103e5SMatthew Knepley   }
33383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3339212103e5SMatthew Knepley }
3340212103e5SMatthew Knepley 
3341d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3342d71ae5a4SJacob Faibussowitsch {
334382f516ccSBarry Smith   MPI_Comm         comm;
334453156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3345efa14ee0SMatthew G Knepley   IS              *subpointIS;
3346efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
33473982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3348dd05d810SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, v;
33490d366550SMatthew G. Knepley   PetscMPIInt      rank;
3350e6ccafaeSMatthew G Knepley 
3351e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
33529566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
33539566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3354efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
33559566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
33569566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3357bec263e5SMatthew G. Knepley   if (cellHeight) {
33589566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
33599566063dSJacob Faibussowitsch     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3360bec263e5SMatthew G. Knepley   } else {
3361bec263e5SMatthew G. Knepley     DMLabel         depth;
3362bec263e5SMatthew G. Knepley     IS              pointIS;
3363bec263e5SMatthew G. Knepley     const PetscInt *points;
3364b85c8bf9SMatthew G. Knepley     PetscInt        numPoints = 0;
3365bec263e5SMatthew G. Knepley 
33669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
33679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3368b85c8bf9SMatthew G. Knepley     if (pointIS) {
33699566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
33709566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3371b85c8bf9SMatthew G. Knepley     }
3372bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3373bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3374bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3375bec263e5SMatthew G. Knepley 
33769566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3377bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
33789566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
33799566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3380bec263e5SMatthew G. Knepley       }
33819566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3382bec263e5SMatthew G. Knepley     }
33839566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
33849566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3385bec263e5SMatthew G. Knepley   }
3386efa14ee0SMatthew G Knepley   /* Setup chart */
33879566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3388dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
33899566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3390e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
33919566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3392e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3393e6ccafaeSMatthew G Knepley   }
3394dd05d810SMatthew G. Knepley   // Determine submesh dimension
3395dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3396dd05d810SMatthew G. Knepley   if (sdim > 0) {
3397dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3398dd05d810SMatthew G. Knepley     sdim = dim;
3399dd05d810SMatthew G. Knepley   } else {
3400dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3401dd05d810SMatthew G. Knepley     PetscInt lsdim;
3402dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3403dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3404a5a714f4SStefano Zampini     PetscCall(MPIU_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPI_MAX, comm));
3405dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3406dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3407dd05d810SMatthew G. Knepley   }
34089566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
34099566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3410e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3411dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3412dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3413dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3414dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3415dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
34169566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
34179566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3418e6ccafaeSMatthew G Knepley   }
3419412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
34209566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3421dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3422e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3423e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3424e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3425e6ccafaeSMatthew G Knepley       const PetscInt *cone;
342615100a53SVaclav Hapla       PetscInt        coneSize;
3427e6ccafaeSMatthew G Knepley 
34289566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3429dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
343015100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
343115100a53SVaclav Hapla 
34329566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3433e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34349566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3435e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3436e6ccafaeSMatthew G Knepley         }
34379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
34389566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
343915100a53SVaclav Hapla       } else {
344015100a53SVaclav Hapla         DMPolytopeType ct;
344115100a53SVaclav Hapla 
344215100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
344315100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
344415100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3445e6ccafaeSMatthew G Knepley       }
3446e6ccafaeSMatthew G Knepley     }
3447e6ccafaeSMatthew G Knepley   }
34489566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
34499566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3450e6ccafaeSMatthew G Knepley   /* Set cones */
34519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
34529566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3453dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3454e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3455e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3456e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
34570e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
34580d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3459e6ccafaeSMatthew G Knepley 
3460dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
34610d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
34620d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
34630d366550SMatthew G. Knepley 
34649566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
34659566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
34660d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3467064cae4fSPierre Jolivet           PetscBool isHybrid = PETSC_FALSE;
3468412e9a14SMatthew G. Knepley 
34699566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3470412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
34719566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
34720d366550SMatthew G. Knepley           if (subc >= 0) {
34730d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
34740d366550SMatthew G. Knepley 
34759566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
34769566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
34779566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
34780d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
34790d366550SMatthew G. Knepley               if (cone[c] == point) {
34800d366550SMatthew G. Knepley                 fornt = ornt[c];
34810d366550SMatthew G. Knepley                 break;
34820d366550SMatthew G. Knepley               }
34830d366550SMatthew G. Knepley             }
34840d366550SMatthew G. Knepley             break;
34850d366550SMatthew G. Knepley           }
34860d366550SMatthew G. Knepley         }
34870d366550SMatthew G. Knepley       }
34889566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
34899566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
34909566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
34919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3492e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34939566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
349401a2673eSMatthew G. Knepley         if (subc >= 0) {
349501a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
34963982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
349701a2673eSMatthew G. Knepley           ++coneSizeNew;
349801a2673eSMatthew G. Knepley         }
3499e6ccafaeSMatthew G Knepley       }
350063a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
35019566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
35029566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
35039566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3504e6ccafaeSMatthew G Knepley     }
3505e6ccafaeSMatthew G Knepley   }
35069566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
35079566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
35089566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3509e6ccafaeSMatthew G Knepley   /* Build coordinates */
3510e6ccafaeSMatthew G Knepley   {
3511e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3512e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3513e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3514c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
351524640c55SToby Isaac     const char  *name;
3516e6ccafaeSMatthew G Knepley 
35179566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
35189566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
35199566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
35209566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
35219566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
35229566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
35239566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
35249566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0] + numSubPoints[0]));
3525e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3526e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3527e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3528e6ccafaeSMatthew G Knepley       PetscInt       dof;
3529e6ccafaeSMatthew G Knepley 
35309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
35319566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
35329566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3533e6ccafaeSMatthew G Knepley     }
35349566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
35359566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
35369566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
35379566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
35389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
35399566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
35409566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
35419566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
35429566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
35439566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3544e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3545e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3546e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3547e6ccafaeSMatthew G Knepley       PetscInt       dof, off, sdof, soff, d;
3548e6ccafaeSMatthew G Knepley 
35499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
35509566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
35519566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
35529566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
355363a3b9bcSJacob 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);
3554efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3555e6ccafaeSMatthew G Knepley     }
35569566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
35579566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
35589566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
35599566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3560e6ccafaeSMatthew G Knepley   }
35613982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
35623982b651SMatthew G. Knepley   {
35633982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
35643982b651SMatthew G. Knepley     IS                 subpIS;
35653982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
35665033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
35675033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3568dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
35695033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
35705033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
35713982b651SMatthew G. Knepley 
35729566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
35735033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
35749566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
35759566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
35769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
35779566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
35789566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
35793982b651SMatthew G. Knepley     if (subpIS) {
3580dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3581dd05d810SMatthew G. Knepley 
35829566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
35839566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3584dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3585dd05d810SMatthew G. Knepley       if (!sorted) {
3586dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3587dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3588dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3589dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3590dd05d810SMatthew G. Knepley       }
35913982b651SMatthew G. Knepley     }
35929566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
35933982b651SMatthew G. Knepley     if (numRoots >= 0) {
35945033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
35955033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
35969566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
35973982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
35983982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
35993982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
36003982b651SMatthew G. Knepley       }
36013982b651SMatthew G. Knepley       /* Set subleaves */
36023982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
36033982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3604dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
36053982b651SMatthew G. Knepley 
36063982b651SMatthew G. Knepley         if (subpoint < 0) continue;
36073982b651SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
36083982b651SMatthew G. Knepley         newLocalPoints[point - pStart].index = subpoint;
36093982b651SMatthew G. Knepley         ++numSubleaves;
36103982b651SMatthew G. Knepley       }
36113982b651SMatthew G. Knepley       /* Must put in owned subpoints */
36123982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
36133982b651SMatthew G. Knepley         newOwners[p - pStart].rank  = -3;
36143982b651SMatthew G. Knepley         newOwners[p - pStart].index = -3;
36153982b651SMatthew G. Knepley       }
3616d9384f27SMatthew G. Knepley       for (p = 0; p < numSubpoints; ++p) {
36175033f954SMatthew G. Knepley         /* Hold on to currently owned points */
36185033f954SMatthew G. Knepley         if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
36195033f954SMatthew G. Knepley         else newOwners[subpoints[p] - pStart].rank = rank;
3620d9384f27SMatthew G. Knepley         newOwners[subpoints[p] - pStart].index = p;
36213982b651SMatthew G. Knepley       }
36229566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
36239566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
36249371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
36259371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
36269566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
36279566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
36289566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
36299566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
36305033f954SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
36313982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3632dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
36333982b651SMatthew G. Knepley 
36343982b651SMatthew G. Knepley         if (subpoint < 0) continue;
36359371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
36369371c9d4SSatish Balay           ++ll;
36379371c9d4SSatish Balay           continue;
36389371c9d4SSatish Balay         }
36393982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
36403982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
36413982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
364263a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
364363a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
36443982b651SMatthew G. Knepley         ++sl;
36453982b651SMatthew G. Knepley       }
36461dca8a05SBarry 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);
36479566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
36489566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
36493982b651SMatthew G. Knepley     }
365048a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3651dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
36523982b651SMatthew G. Knepley   }
3653212103e5SMatthew Knepley   /* Filter labels */
36549566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3655efa14ee0SMatthew G Knepley   /* Cleanup */
3656dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
36579566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
36589566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3659e6ccafaeSMatthew G Knepley   }
36609566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
36613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3662e6ccafaeSMatthew G Knepley }
3663e6ccafaeSMatthew G Knepley 
3664d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
3665d71ae5a4SJacob Faibussowitsch {
36663982b651SMatthew G. Knepley   PetscFunctionBegin;
36679566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm));
36683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36693982b651SMatthew G. Knepley }
36703982b651SMatthew G. Knepley 
3671d0fa310fSMatthew G. Knepley /*@
3672e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3673e6ccafaeSMatthew G Knepley 
3674e6ccafaeSMatthew G Knepley   Input Parameters:
3675e6ccafaeSMatthew G Knepley + dm          - The original mesh
367620f4b53cSBarry Smith . vertexLabel - The `DMLabel` marking points contained in the surface
3677158acfadSMatthew G. Knepley . value       - The label value to use
367820f4b53cSBarry Smith - markedFaces - `PETSC_TRUE` if surface faces are marked in addition to vertices, `PETSC_FALSE` if only vertices are marked
3679e6ccafaeSMatthew G Knepley 
3680e6ccafaeSMatthew G Knepley   Output Parameter:
3681e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3682e6ccafaeSMatthew G Knepley 
3683e6ccafaeSMatthew G Knepley   Level: developer
3684e6ccafaeSMatthew G Knepley 
368520f4b53cSBarry Smith   Note:
368620f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
368720f4b53cSBarry Smith 
36881cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3689830e53efSMatthew G. Knepley @*/
3690d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3691d71ae5a4SJacob Faibussowitsch {
3692827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3693827c4036SVaclav Hapla   PetscInt               dim, cdim;
3694e6ccafaeSMatthew G Knepley 
3695e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3696e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36974f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
36989566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
36999566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
37009566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
37019566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
37029566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
37039566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
37049566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
370508401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3706827c4036SVaclav Hapla   if (interpolated) {
37079566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3708e6ccafaeSMatthew G Knepley   } else {
37099566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3710e6ccafaeSMatthew G Knepley   }
37115de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
37123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3713e6ccafaeSMatthew G Knepley }
3714e6ccafaeSMatthew G Knepley 
3715d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3716d71ae5a4SJacob Faibussowitsch {
3717766ab985SMatthew G. Knepley   MPI_Comm        comm;
3718766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3719766ab985SMatthew G. Knepley   IS              subvertexIS;
3720766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3721fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3722766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3723412e9a14SMatthew G. Knepley   PetscInt        c, f;
3724766ab985SMatthew G. Knepley 
3725766ab985SMatthew G. Knepley   PetscFunctionBegin;
37269566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3727766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
37289566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
37299566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
37309566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3731766ab985SMatthew G. Knepley   /* Setup chart */
37329566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
37339566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
37349566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
37359566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3736766ab985SMatthew G. Knepley   /* Set cone sizes */
3737766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3738766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3739766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
37409566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
37419566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
374248a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
374348a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
37449566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3745d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3746766ab985SMatthew G. Knepley   /* Create face cones */
37479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
37489566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3749766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3750766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3751766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
375287feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3753064cae4fSPierre Jolivet     PetscBool       isHybrid = PETSC_FALSE;
375487feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3755766ab985SMatthew G. Knepley 
37569566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3757412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
37589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
375987feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
37609566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
376187feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
376287feddfdSMatthew G. Knepley     }
37639566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
37649566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
37659566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
376627234c99SMatthew G. Knepley     /* Not true in parallel
376708401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
376887feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
376987feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3770064cae4fSPierre Jolivet       PetscBool isHybrid = PETSC_FALSE;
3771766ab985SMatthew G. Knepley 
37729566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3773412e9a14SMatthew G. Knepley       if (isHybrid) continue;
377487feddfdSMatthew G. Knepley       /* I know this is a crap search */
377587feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
377687feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3777766ab985SMatthew G. Knepley       }
377863a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
37799566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3780766ab985SMatthew G. Knepley     }
37819566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
378287feddfdSMatthew G. Knepley     ++newFacePoint;
3783766ab985SMatthew G. Knepley   }
37849566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
37859566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
37869566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3787766ab985SMatthew G. Knepley   /* Build coordinates */
3788766ab985SMatthew G. Knepley   {
3789766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3790766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3791766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3792c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
379324640c55SToby Isaac     const char  *name;
3794766ab985SMatthew G. Knepley 
37959566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
37969566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
37979566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
37989566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
37999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
38009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
38019566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
38029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3803766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3804766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3805766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3806766ab985SMatthew G. Knepley       PetscInt       dof;
3807766ab985SMatthew G. Knepley 
38089566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
38099566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
38109566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3811766ab985SMatthew G. Knepley     }
38129566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
38139566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
38149566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
38159566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
38169566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
38179566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
38189566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
38199566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
38209566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
38219566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3822766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3823766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3824766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3825766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3826766ab985SMatthew G. Knepley 
38279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
38289566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
38299566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
38309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
383163a3b9bcSJacob 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);
3832766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3833766ab985SMatthew G. Knepley     }
38349566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
38359566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
38369566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
38379566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3838766ab985SMatthew G. Knepley   }
3839aca35d17SMatthew G. Knepley   /* Build SF */
3840aca35d17SMatthew G. Knepley   CHKMEMQ;
3841aca35d17SMatthew G. Knepley   {
3842aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3843aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3844bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3845aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3846bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
384749c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3848bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3849aca35d17SMatthew G. Knepley 
38509566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
38519566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
38529566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
38539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
38549566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
38559566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
3856aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3857aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
38589566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
3859bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
3860bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3861bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3862bdcf2095SMatthew G. Knepley       }
38639e0823b2SMatthew G. Knepley       /* Set subleaves */
3864aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3865aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3866bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3867aca35d17SMatthew G. Knepley 
386863a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
3869bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3870bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
3871bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
3872bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3873aca35d17SMatthew G. Knepley       }
38749e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
38759e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
38769e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
38779e0823b2SMatthew G. Knepley 
38789e0823b2SMatthew G. Knepley         if (subPoint < 0) {
38799e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
38809e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
38819e0823b2SMatthew G. Knepley         } else {
38829e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
38839e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
38849e0823b2SMatthew G. Knepley         }
3885bdcf2095SMatthew G. Knepley       }
38869566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
38879566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
38889566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
38899566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
38909566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
38919566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
389249c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3893bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3894bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3895aca35d17SMatthew G. Knepley 
3896aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
38979371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
38989371c9d4SSatish Balay           ++ll;
38999371c9d4SSatish Balay           continue;
39009371c9d4SSatish Balay         }
3901aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3902bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3903bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
390463a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
390563a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
3906aca35d17SMatthew G. Knepley         ++sl;
3907aca35d17SMatthew G. Knepley       }
39089566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
39091dca8a05SBarry 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);
39109566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
3911aca35d17SMatthew G. Knepley     }
3912aca35d17SMatthew G. Knepley   }
3913aca35d17SMatthew G. Knepley   CHKMEMQ;
3914766ab985SMatthew G. Knepley   /* Cleanup */
39159566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
39169566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
39179566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
39183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3919766ab985SMatthew G. Knepley }
3920766ab985SMatthew G. Knepley 
3921d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3922d71ae5a4SJacob Faibussowitsch {
39233982b651SMatthew G. Knepley   DMLabel label = NULL;
3924766ab985SMatthew G. Knepley 
3925766ab985SMatthew G. Knepley   PetscFunctionBegin;
39269566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
39279566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm));
39283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3929766ab985SMatthew G. Knepley }
3930766ab985SMatthew G. Knepley 
3931c08575a3SMatthew G. Knepley /*@C
393220f4b53cSBarry 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.
3933766ab985SMatthew G. Knepley 
3934766ab985SMatthew G. Knepley   Input Parameters:
3935766ab985SMatthew G. Knepley + dm          - The original mesh
393627c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
393720f4b53cSBarry Smith . label       - A label name, or `NULL`
393827c04023SMatthew G. Knepley - value       - A label value
3939766ab985SMatthew G. Knepley 
3940766ab985SMatthew G. Knepley   Output Parameter:
3941766ab985SMatthew G. Knepley . subdm - The surface mesh
3942766ab985SMatthew G. Knepley 
3943766ab985SMatthew G. Knepley   Level: developer
3944766ab985SMatthew G. Knepley 
394520f4b53cSBarry Smith   Note:
394620f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
394720f4b53cSBarry Smith 
39481cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
3949c08575a3SMatthew G. Knepley @*/
3950d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3951d71ae5a4SJacob Faibussowitsch {
3952c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
3953766ab985SMatthew G. Knepley 
3954766ab985SMatthew G. Knepley   PetscFunctionBegin;
3955766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39564f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
39579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
39589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
39599566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
39609566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
39619566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
39629566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
39639566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
3964766ab985SMatthew G. Knepley   if (depth == dim) {
39659566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
3966766ab985SMatthew G. Knepley   } else {
39679566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
3968e6ccafaeSMatthew G Knepley   }
39695de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
39703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3971e6ccafaeSMatthew G Knepley }
3972e6ccafaeSMatthew G Knepley 
3973bec263e5SMatthew G. Knepley /*@
39740e18dc48SMatthew G. Knepley   DMPlexReorderCohesiveSupports - Ensure that face supports for cohesive end caps are ordered
39750e18dc48SMatthew G. Knepley 
39760e18dc48SMatthew G. Knepley   Not Collective
39770e18dc48SMatthew G. Knepley 
39780e18dc48SMatthew G. Knepley   Input Parameter:
39790e18dc48SMatthew G. Knepley . dm - The `DM` containing cohesive cells
39800e18dc48SMatthew G. Knepley 
39810e18dc48SMatthew G. Knepley   Level: developer
39820e18dc48SMatthew G. Knepley 
3983a4e35b19SJacob Faibussowitsch   Note:
3984a4e35b19SJacob Faibussowitsch   For the negative size (first) face, the cohesive cell should be first in the support, and for
3985a4e35b19SJacob Faibussowitsch   the positive side (second) face, the cohesive cell should be second in the support.
39860e18dc48SMatthew G. Knepley 
39870e18dc48SMatthew G. Knepley .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexCreateCohesiveSubmesh()`
39880e18dc48SMatthew G. Knepley @*/
39890e18dc48SMatthew G. Knepley PetscErrorCode DMPlexReorderCohesiveSupports(DM dm)
39900e18dc48SMatthew G. Knepley {
39910e18dc48SMatthew G. Knepley   PetscInt dim, cStart, cEnd;
39920e18dc48SMatthew G. Knepley 
39930e18dc48SMatthew G. Knepley   PetscFunctionBegin;
39940e18dc48SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
39950e18dc48SMatthew G. Knepley   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cStart, &cEnd));
39960e18dc48SMatthew G. Knepley   for (PetscInt c = cStart; c < cEnd; ++c) {
39970e18dc48SMatthew G. Knepley     const PetscInt *cone;
39980e18dc48SMatthew G. Knepley     PetscInt        coneSize;
39990e18dc48SMatthew G. Knepley 
40000e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
40010e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetCone(dm, c, &cone));
40020e18dc48SMatthew G. Knepley     PetscCheck(coneSize >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid cell %" PetscInt_FMT " cone size %" PetscInt_FMT " must be >= 2", c, coneSize);
40030e18dc48SMatthew G. Knepley     for (PetscInt s = 0; s < 2; ++s) {
40040e18dc48SMatthew G. Knepley       const PetscInt *supp;
40050e18dc48SMatthew G. Knepley       PetscInt        suppSize, newsupp[2];
40060e18dc48SMatthew G. Knepley 
40070e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupportSize(dm, cone[s], &suppSize));
40080e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupport(dm, cone[s], &supp));
40090e18dc48SMatthew G. Knepley       if (suppSize == 2) {
40100e18dc48SMatthew G. Knepley         /* Reorder hybrid end cap support */
40110e18dc48SMatthew G. Knepley         if (supp[s] == c) {
40120e18dc48SMatthew G. Knepley           newsupp[0] = supp[1];
40130e18dc48SMatthew G. Knepley           newsupp[1] = supp[0];
40140e18dc48SMatthew G. Knepley         } else {
40150e18dc48SMatthew G. Knepley           newsupp[0] = supp[0];
40160e18dc48SMatthew G. Knepley           newsupp[1] = supp[1];
40170e18dc48SMatthew G. Knepley         }
40180e18dc48SMatthew 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);
40190e18dc48SMatthew G. Knepley         PetscCall(DMPlexSetSupport(dm, cone[s], newsupp));
40200e18dc48SMatthew G. Knepley       }
40210e18dc48SMatthew G. Knepley     }
40220e18dc48SMatthew G. Knepley   }
40230e18dc48SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
40240e18dc48SMatthew G. Knepley }
40250e18dc48SMatthew G. Knepley 
40260e18dc48SMatthew G. Knepley /*@
4027bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
4028bec263e5SMatthew G. Knepley 
4029bec263e5SMatthew G. Knepley   Input Parameters:
4030bec263e5SMatthew G. Knepley + dm        - The original mesh
403120f4b53cSBarry Smith . cellLabel - The `DMLabel` marking cells contained in the new mesh
4032bec263e5SMatthew G. Knepley - value     - The label value to use
4033bec263e5SMatthew G. Knepley 
4034bec263e5SMatthew G. Knepley   Output Parameter:
4035bec263e5SMatthew G. Knepley . subdm - The new mesh
4036bec263e5SMatthew G. Knepley 
4037bec263e5SMatthew G. Knepley   Level: developer
4038bec263e5SMatthew G. Knepley 
403920f4b53cSBarry Smith   Note:
404020f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
404120f4b53cSBarry Smith 
40421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
4043bec263e5SMatthew G. Knepley @*/
4044d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
4045d71ae5a4SJacob Faibussowitsch {
4046b47cd10cSMatthew G. Knepley   PetscInt dim, overlap;
4047bec263e5SMatthew G. Knepley 
4048bec263e5SMatthew G. Knepley   PetscFunctionBegin;
4049bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40504f572ea9SToby Isaac   PetscAssertPointer(subdm, 4);
40519566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
40529566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
40539566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
4054bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
40559566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm));
40565de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
4057b47cd10cSMatthew G. Knepley   // It is possible to obtain a surface mesh where some faces are in SF
4058b47cd10cSMatthew G. Knepley   //   We should either mark the mesh as having an overlap, or delete these from the SF
4059b47cd10cSMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
4060b47cd10cSMatthew G. Knepley   if (!overlap) {
4061b47cd10cSMatthew G. Knepley     PetscSF         sf;
4062b47cd10cSMatthew G. Knepley     const PetscInt *leaves;
4063b47cd10cSMatthew G. Knepley     PetscInt        cStart, cEnd, Nl;
4064b47cd10cSMatthew G. Knepley     PetscBool       hasSubcell = PETSC_FALSE, ghasSubcell;
4065b47cd10cSMatthew G. Knepley 
4066b47cd10cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd));
4067b47cd10cSMatthew G. Knepley     PetscCall(DMGetPointSF(*subdm, &sf));
4068b47cd10cSMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
4069b47cd10cSMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
4070b47cd10cSMatthew G. Knepley       const PetscInt point = leaves ? leaves[l] : l;
4071b47cd10cSMatthew G. Knepley 
4072b47cd10cSMatthew G. Knepley       if (point >= cStart && point < cEnd) {
4073b47cd10cSMatthew G. Knepley         hasSubcell = PETSC_TRUE;
4074b47cd10cSMatthew G. Knepley         break;
4075b47cd10cSMatthew G. Knepley       }
4076b47cd10cSMatthew G. Knepley     }
4077b47cd10cSMatthew G. Knepley     PetscCall(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
4078b47cd10cSMatthew G. Knepley     if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1));
4079b47cd10cSMatthew G. Knepley   }
40803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4081bec263e5SMatthew G. Knepley }
4082bec263e5SMatthew G. Knepley 
408364beef6dSMatthew G. Knepley /*@
408420f4b53cSBarry Smith   DMPlexGetSubpointMap - Returns a `DMLabel` with point dimension as values
408564beef6dSMatthew G. Knepley 
408664beef6dSMatthew G. Knepley   Input Parameter:
408720f4b53cSBarry Smith . dm - The submesh `DM`
408864beef6dSMatthew G. Knepley 
408964beef6dSMatthew G. Knepley   Output Parameter:
409020f4b53cSBarry Smith . subpointMap - The `DMLabel` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
409164beef6dSMatthew G. Knepley 
409264beef6dSMatthew G. Knepley   Level: developer
409364beef6dSMatthew G. Knepley 
40941cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
409564beef6dSMatthew G. Knepley @*/
4096d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
4097d71ae5a4SJacob Faibussowitsch {
4098e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4099e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41004f572ea9SToby Isaac   PetscAssertPointer(subpointMap, 2);
410165663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
41023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4103e6ccafaeSMatthew G Knepley }
4104e6ccafaeSMatthew G Knepley 
4105c08575a3SMatthew G. Knepley /*@
410620f4b53cSBarry Smith   DMPlexSetSubpointMap - Sets the `DMLabel` with point dimension as values
4107c08575a3SMatthew G. Knepley 
4108c08575a3SMatthew G. Knepley   Input Parameters:
410920f4b53cSBarry Smith + dm          - The submesh `DM`
411020f4b53cSBarry Smith - subpointMap - The `DMLabel` of all the points from the original mesh in this submesh
4111c08575a3SMatthew G. Knepley 
4112c08575a3SMatthew G. Knepley   Level: developer
4113c08575a3SMatthew G. Knepley 
411420f4b53cSBarry Smith   Note:
411520f4b53cSBarry Smith   Should normally not be called by the user, since it is set in `DMPlexCreateSubmesh()`
411620f4b53cSBarry Smith 
41171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
4118c08575a3SMatthew G. Knepley @*/
4119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
4120d71ae5a4SJacob Faibussowitsch {
4121e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
4122285d324eSMatthew G. Knepley   DMLabel  tmp;
4123e6ccafaeSMatthew G Knepley 
4124e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4125e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4126285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
4127e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
41289566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
41299566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
41303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4131e6ccafaeSMatthew G Knepley }
4132e6ccafaeSMatthew G Knepley 
4133d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
4134d71ae5a4SJacob Faibussowitsch {
413597d8846cSMatthew Knepley   DMLabel  spmap;
413697d8846cSMatthew Knepley   PetscInt depth, d;
413797d8846cSMatthew Knepley 
413897d8846cSMatthew Knepley   PetscFunctionBegin;
41399566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
41409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
414197d8846cSMatthew Knepley   if (spmap && depth >= 0) {
414297d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
414397d8846cSMatthew Knepley     PetscInt *points, *depths;
414497d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
414597d8846cSMatthew Knepley 
41469566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
414763a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
41489566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
41499566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
415097d8846cSMatthew Knepley     depths[0] = depth;
415197d8846cSMatthew Knepley     depths[1] = 0;
4152ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
415397d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
415497d8846cSMatthew Knepley       const PetscInt dep = depths[d];
415597d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
415697d8846cSMatthew Knepley 
41579566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
41589566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
415997d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
416063a3b9bcSJacob 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);
416197d8846cSMatthew Knepley       } else {
416297d8846cSMatthew Knepley         if (!n) {
416397d8846cSMatthew Knepley           if (d == 0) {
416497d8846cSMatthew Knepley             /* Missing cells */
416597d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
416697d8846cSMatthew Knepley           } else {
416797d8846cSMatthew Knepley             /* Missing faces */
416897d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
416997d8846cSMatthew Knepley           }
417097d8846cSMatthew Knepley         }
417197d8846cSMatthew Knepley       }
417297d8846cSMatthew Knepley       if (n) {
417397d8846cSMatthew Knepley         IS              is;
417497d8846cSMatthew Knepley         const PetscInt *opoints;
417597d8846cSMatthew Knepley 
41769566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
41779566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
417897d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
41799566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
41809566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
418197d8846cSMatthew Knepley       }
418297d8846cSMatthew Knepley     }
41839566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
418463a3b9bcSJacob 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);
41859566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
41869566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
418797d8846cSMatthew Knepley   }
41883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
418997d8846cSMatthew Knepley }
419097d8846cSMatthew Knepley 
419164beef6dSMatthew G. Knepley /*@
419220f4b53cSBarry Smith   DMPlexGetSubpointIS - Returns an `IS` covering the entire subdm chart with the original points as data
4193e6ccafaeSMatthew G Knepley 
4194e6ccafaeSMatthew G Knepley   Input Parameter:
419520f4b53cSBarry Smith . dm - The submesh `DM`
4196e6ccafaeSMatthew G Knepley 
4197e6ccafaeSMatthew G Knepley   Output Parameter:
419820f4b53cSBarry Smith . subpointIS - The `IS` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
419964beef6dSMatthew G. Knepley 
420064beef6dSMatthew G. Knepley   Level: developer
420164beef6dSMatthew G. Knepley 
420220f4b53cSBarry Smith   Note:
420320f4b53cSBarry Smith   This `IS` is guaranteed to be sorted by the construction of the submesh
420420f4b53cSBarry Smith 
42051cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
420664beef6dSMatthew G. Knepley @*/
4207d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
4208d71ae5a4SJacob Faibussowitsch {
420997d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
421097d8846cSMatthew Knepley   DMLabel          spmap;
421197d8846cSMatthew Knepley   PetscObjectState state;
4212e6ccafaeSMatthew G Knepley 
4213e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4214e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42154f572ea9SToby Isaac   PetscAssertPointer(subpointIS, 2);
42169566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
42179566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
42189566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
421997d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
42203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4221e6ccafaeSMatthew G Knepley }
4222559a1558SMatthew G. Knepley 
4223559a1558SMatthew G. Knepley /*@
422420f4b53cSBarry Smith   DMGetEnclosureRelation - Get the relationship between `dmA` and `dmB`
4225559a1558SMatthew G. Knepley 
4226559a1558SMatthew G. Knepley   Input Parameters:
422720f4b53cSBarry Smith + dmA - The first `DM`
422820f4b53cSBarry Smith - dmB - The second `DM`
4229559a1558SMatthew G. Knepley 
4230559a1558SMatthew G. Knepley   Output Parameter:
423120f4b53cSBarry Smith . rel - The relation of `dmA` to `dmB`
4232559a1558SMatthew G. Knepley 
4233a6e0b375SMatthew G. Knepley   Level: intermediate
4234559a1558SMatthew G. Knepley 
42351cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosurePoint()`
4236559a1558SMatthew G. Knepley @*/
4237d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4238d71ae5a4SJacob Faibussowitsch {
4239a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
4240559a1558SMatthew G. Knepley   DMLabel  spmap;
4241a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4242559a1558SMatthew G. Knepley 
424344171101SMatthew G. Knepley   PetscFunctionBegin;
42444f572ea9SToby Isaac   PetscAssertPointer(rel, 3);
4245a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
42463ba16761SJacob Faibussowitsch   if (!dmA || !dmB) PetscFunctionReturn(PETSC_SUCCESS);
4247a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4248064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
42499371c9d4SSatish Balay   if (dmA == dmB) {
42509371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
42513ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
42529371c9d4SSatish Balay   }
42539566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
42549566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
42559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
42569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4257a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4258a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4259a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
4260a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4261a6e0b375SMatthew G. Knepley     goto end;
4262559a1558SMatthew G. Knepley   }
4263a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4264a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4265a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4266a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
42679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4268a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4269a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4270a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4271a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4272a6e0b375SMatthew G. Knepley   } else {
4273a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4274a6e0b375SMatthew G. Knepley   }
4275a6e0b375SMatthew G. Knepley end:
42769566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
42779566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
42783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4279559a1558SMatthew G. Knepley }
428044171101SMatthew G. Knepley 
428144171101SMatthew G. Knepley /*@
428220f4b53cSBarry Smith   DMGetEnclosurePoint - Get the point `pA` in `dmA` which corresponds to the point `pB` in `dmB`
428344171101SMatthew G. Knepley 
428444171101SMatthew G. Knepley   Input Parameters:
428520f4b53cSBarry Smith + dmA   - The first `DM`
428620f4b53cSBarry Smith . dmB   - The second `DM`
428720f4b53cSBarry Smith . etype - The type of enclosure relation that `dmA` has to `dmB`
428820f4b53cSBarry Smith - pB    - A point of `dmB`
428944171101SMatthew G. Knepley 
429044171101SMatthew G. Knepley   Output Parameter:
429120f4b53cSBarry Smith . pA - The corresponding point of `dmA`
429244171101SMatthew G. Knepley 
4293a6e0b375SMatthew G. Knepley   Level: intermediate
429444171101SMatthew G. Knepley 
42951cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosureRelation()`
429644171101SMatthew G. Knepley @*/
4297d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
4298d71ae5a4SJacob Faibussowitsch {
4299a6e0b375SMatthew G. Knepley   DM              sdm;
4300a6e0b375SMatthew G. Knepley   IS              subpointIS;
4301a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4302a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
430344171101SMatthew G. Knepley 
430444171101SMatthew G. Knepley   PetscFunctionBegin;
4305a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4306a6e0b375SMatthew G. Knepley   switch (etype) {
4307a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4308a6e0b375SMatthew G. Knepley     sdm = dmB;
43099566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
43109566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4311a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
43129566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4313a6e0b375SMatthew G. Knepley     break;
4314a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4315a6e0b375SMatthew G. Knepley     sdm = dmA;
43169566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
43179566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
43189566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
43199566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4320a6e0b375SMatthew G. Knepley     if (*pA < 0) {
43219566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
43229566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
432363a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4324a6e0b375SMatthew G. Knepley     }
43259566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4326a6e0b375SMatthew G. Knepley     break;
4327a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
4328d71ae5a4SJacob Faibussowitsch   case DM_ENC_NONE:
4329d71ae5a4SJacob Faibussowitsch     *pA = pB;
4330d71ae5a4SJacob Faibussowitsch     break;
43319371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4332a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
433344171101SMatthew G. Knepley 
43349566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
43359566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
43369371c9d4SSatish Balay   } break;
4337d71ae5a4SJacob Faibussowitsch   default:
4338d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
433944171101SMatthew G. Knepley   }
43403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
434144171101SMatthew G. Knepley }
4342