xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision cc4c1da905d89950b196b027190941013bd3e15a)
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 {
192bbb57e68SMatthew G. Knepley   DM                     plex;
193827c4036SVaclav Hapla   DMPlexInterpolatedFlag flg;
194cd0c2139SMatthew G Knepley 
195cd0c2139SMatthew G Knepley   PetscFunctionBegin;
196827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
197bbb57e68SMatthew G. Knepley   PetscCall(DMConvert(dm, DMPLEX, &plex));
198bbb57e68SMatthew G. Knepley   PetscCall(DMPlexIsInterpolated(plex, &flg));
19908401ef6SPierre Jolivet   PetscCheck(flg == DMPLEX_INTERPOLATED_FULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
200bbb57e68SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces_Internal(plex, val, 0, label, PETSC_FALSE));
201bbb57e68SMatthew G. Knepley   PetscCall(DMDestroy(&plex));
2023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
203cd0c2139SMatthew G Knepley }
204cd0c2139SMatthew G Knepley 
205d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
206d71ae5a4SJacob Faibussowitsch {
207b0bf5782SToby Isaac   IS              valueIS;
208ac51f24eSSander Arens   PetscSF         sfPoint;
209b0bf5782SToby Isaac   const PetscInt *values;
210ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
211b0bf5782SToby Isaac 
212b0bf5782SToby Isaac   PetscFunctionBegin;
2139566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2149566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2169566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
217b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
218b0bf5782SToby Isaac     IS              pointIS;
219b0bf5782SToby Isaac     const PetscInt *points;
220b0bf5782SToby Isaac     PetscInt        numPoints, p;
221b0bf5782SToby Isaac 
2229566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2239566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2249566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
225b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
226b0bf5782SToby Isaac       PetscInt  q       = points[p];
227b0bf5782SToby Isaac       PetscInt *closure = NULL;
228b0bf5782SToby Isaac       PetscInt  closureSize, c;
229b0bf5782SToby Isaac 
230b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
231b0bf5782SToby Isaac         continue;
232b0bf5782SToby Isaac       }
2339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
23448a46eb9SPierre Jolivet       for (c = 0; c < closureSize * 2; c += 2) PetscCall(DMLabelSetValue(label, closure[c], values[v]));
2359566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
236b0bf5782SToby Isaac     }
2379566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2389566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
239b0bf5782SToby Isaac   }
2409566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2419566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2429566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
2439566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
244ac51f24eSSander Arens   if (nroots >= 0) {
24526279d81SSanderA     DMLabel         lblRoots, lblLeaves;
24626279d81SSanderA     IS              valueIS, pointIS;
24726279d81SSanderA     const PetscInt *values;
24826279d81SSanderA     PetscInt        numValues, v;
24926279d81SSanderA 
25026279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
2519566063dSJacob Faibussowitsch     PetscCall(DMLabelGather(label, sfPoint, &lblLeaves));
2529566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS));
2539566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2549566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
25526279d81SSanderA     for (v = 0; v < numValues; ++v) {
25626279d81SSanderA       const PetscInt value = values[v];
25726279d81SSanderA 
2589566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
2599566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2609566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
26126279d81SSanderA     }
2629566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2639566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2649566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblLeaves));
26526279d81SSanderA     /* Push point contributions from roots into remote leaves */
2669566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots));
2679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblRoots, &valueIS));
2689566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2699566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
27026279d81SSanderA     for (v = 0; v < numValues; ++v) {
27126279d81SSanderA       const PetscInt value = values[v];
27226279d81SSanderA 
2739566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS));
2749566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2759566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
27626279d81SSanderA     }
2779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2799566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblRoots));
28026279d81SSanderA   }
2813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
282b0bf5782SToby Isaac }
283b0bf5782SToby Isaac 
2842be2b188SMatthew G Knepley /*@
2852be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
2862be2b188SMatthew G Knepley 
2872be2b188SMatthew G Knepley   Input Parameters:
28820f4b53cSBarry Smith + dm    - The `DM`
28920f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
2902be2b188SMatthew G Knepley 
2912be2b188SMatthew G Knepley   Output Parameter:
29220f4b53cSBarry Smith . label - A `DMLabel` marking all surface points in the transitive closure
2932be2b188SMatthew G Knepley 
2942be2b188SMatthew G Knepley   Level: developer
2952be2b188SMatthew G Knepley 
2961cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelCohesiveComplete()`
2972be2b188SMatthew G Knepley @*/
298d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
299d71ae5a4SJacob Faibussowitsch {
3002be2b188SMatthew G Knepley   PetscFunctionBegin;
3019566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
3023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3032be2b188SMatthew G Knepley }
3042be2b188SMatthew G Knepley 
3056cf0e42fSMatthew G. Knepley /*@
306a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
3076cf0e42fSMatthew G. Knepley 
3086cf0e42fSMatthew G. Knepley   Input Parameters:
30920f4b53cSBarry Smith + dm    - The `DM`
31020f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
3116cf0e42fSMatthew G. Knepley 
3126cf0e42fSMatthew G. Knepley   Output Parameter:
31320f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
3146cf0e42fSMatthew G. Knepley 
3156cf0e42fSMatthew G. Knepley   Level: developer
3166cf0e42fSMatthew G. Knepley 
31720f4b53cSBarry Smith   Note:
31820f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
3196cf0e42fSMatthew G. Knepley 
3201cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
3216cf0e42fSMatthew G. Knepley @*/
322d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
323d71ae5a4SJacob Faibussowitsch {
3246cf0e42fSMatthew G. Knepley   IS              valueIS;
3256cf0e42fSMatthew G. Knepley   const PetscInt *values;
3269df54392SMatthew G. Knepley   PetscInt        numValues, v, csStart, csEnd, chStart, chEnd;
3276cf0e42fSMatthew G. Knepley 
3286cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
3299df54392SMatthew G. Knepley   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &csStart, &csEnd));
3309df54392SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(dm, 0, &chStart, &chEnd));
3319566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3329566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3339566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
3346cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
3356cf0e42fSMatthew G. Knepley     IS              pointIS;
3366cf0e42fSMatthew G. Knepley     const PetscInt *points;
3376cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
3386cf0e42fSMatthew G. Knepley 
3399566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3409566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
3426cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3439df54392SMatthew G. Knepley       const PetscInt point   = points[p];
3446cf0e42fSMatthew G. Knepley       PetscInt      *closure = NULL;
3457bd9760aSJed Brown       PetscInt       closureSize, cl, h, cStart, cEnd;
3467bd9760aSJed Brown       DMPolytopeType ct;
3479df54392SMatthew G. Knepley 
3489df54392SMatthew G. Knepley       // If the point is a hybrid, allow hybrid cells
3497bd9760aSJed Brown       PetscCall(DMPlexGetCellType(dm, point, &ct));
3509df54392SMatthew G. Knepley       PetscCall(DMPlexGetPointHeight(dm, point, &h));
3517bd9760aSJed Brown       if (DMPolytopeTypeIsHybrid(ct)) {
3529df54392SMatthew G. Knepley         cStart = chStart;
3539df54392SMatthew G. Knepley         cEnd   = chEnd;
3547bd9760aSJed Brown       } else {
3557bd9760aSJed Brown         cStart = csStart;
3567bd9760aSJed Brown         cEnd   = csEnd;
3579df54392SMatthew G. Knepley       }
3586cf0e42fSMatthew G. Knepley 
3599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
36022eabd52SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
361a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
3629371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
3639371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
3649371c9d4SSatish Balay           break;
3659371c9d4SSatish Balay         }
36622eabd52SMatthew G. Knepley       }
3679566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
3686cf0e42fSMatthew G. Knepley     }
3699566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3709566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3716cf0e42fSMatthew G. Knepley   }
3729566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3739566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
3743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3756cf0e42fSMatthew G. Knepley }
3766cf0e42fSMatthew G. Knepley 
377f402d5e4SToby Isaac /*@
378a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
379a6e0b375SMatthew G. Knepley 
380a6e0b375SMatthew G. Knepley   Input Parameters:
38120f4b53cSBarry Smith + dm    - The `DM`
38220f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
383a6e0b375SMatthew G. Knepley 
384a6e0b375SMatthew G. Knepley   Output Parameter:
38520f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
386a6e0b375SMatthew G. Knepley 
387a6e0b375SMatthew G. Knepley   Level: developer
388a6e0b375SMatthew G. Knepley 
38920f4b53cSBarry Smith   Note:
39020f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
391a6e0b375SMatthew G. Knepley 
3921cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
393a6e0b375SMatthew G. Knepley @*/
394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
395d71ae5a4SJacob Faibussowitsch {
396a6e0b375SMatthew G. Knepley   IS              valueIS;
397a6e0b375SMatthew G. Knepley   const PetscInt *values;
398a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
399a6e0b375SMatthew G. Knepley 
400a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
4019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
4039566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4049566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4059566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
406a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
407a6e0b375SMatthew G. Knepley     IS              pointIS;
408a6e0b375SMatthew G. Knepley     const PetscInt *points;
409a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
410a6e0b375SMatthew G. Knepley 
4119566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4129566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4139566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
414a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
415a6e0b375SMatthew G. Knepley       const PetscInt face    = points[p];
416a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
417a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
418a6e0b375SMatthew G. Knepley 
419a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
4209566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
421a6e0b375SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
422a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
4239371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
4249371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
4259371c9d4SSatish Balay           break;
4269371c9d4SSatish Balay         }
427a6e0b375SMatthew G. Knepley       }
4289566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
429a6e0b375SMatthew G. Knepley     }
4309566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4319566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
432a6e0b375SMatthew G. Knepley   }
4339566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4349566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
436a6e0b375SMatthew G. Knepley }
437a6e0b375SMatthew G. Knepley 
438a6e0b375SMatthew G. Knepley /*@
439f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
440f402d5e4SToby Isaac 
441f402d5e4SToby Isaac   Input Parameters:
44220f4b53cSBarry Smith + dm    - The `DM`
44320f4b53cSBarry Smith - label - A `DMLabel` marking surface points and their adjacent cells
444f402d5e4SToby Isaac 
445f402d5e4SToby Isaac   Output Parameter:
44620f4b53cSBarry Smith . label - A `DMLabel` without cells
447f402d5e4SToby Isaac 
448f402d5e4SToby Isaac   Level: developer
449f402d5e4SToby Isaac 
45020f4b53cSBarry Smith   Note:
45120f4b53cSBarry Smith   This undoes `DMPlexLabelAddCells()` or `DMPlexLabelAddFaceCells()`
452f402d5e4SToby Isaac 
4531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()`
454f402d5e4SToby Isaac @*/
455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
456d71ae5a4SJacob Faibussowitsch {
457f402d5e4SToby Isaac   IS              valueIS;
458f402d5e4SToby Isaac   const PetscInt *values;
459485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
460f402d5e4SToby Isaac 
461f402d5e4SToby Isaac   PetscFunctionBegin;
4629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4639566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4649566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4659566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
466f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
467f402d5e4SToby Isaac     IS              pointIS;
468f402d5e4SToby Isaac     const PetscInt *points;
469f402d5e4SToby Isaac     PetscInt        numPoints, p;
470f402d5e4SToby Isaac 
4719566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4729566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
474f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
475f402d5e4SToby Isaac       PetscInt point = points[p];
476f402d5e4SToby Isaac 
47748a46eb9SPierre Jolivet       if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v]));
478f402d5e4SToby Isaac     }
4799566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4809566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
481f402d5e4SToby Isaac   }
4829566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4839566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
485f402d5e4SToby Isaac }
486f402d5e4SToby Isaac 
48759eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
48859eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
489d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
490d71ae5a4SJacob Faibussowitsch {
4912582d50cSToby Isaac   PetscInt d, off = 0;
4922582d50cSToby Isaac 
4932582d50cSToby Isaac   PetscFunctionBegin;
49459eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
4950974a383SToby Isaac   for (d = 0; d < depth; d++) {
4962582d50cSToby Isaac     PetscInt firstd     = d;
4970974a383SToby Isaac     PetscInt firstStart = depthShift[2 * d];
4982582d50cSToby Isaac     PetscInt e;
4992582d50cSToby Isaac 
5002582d50cSToby Isaac     for (e = d + 1; e <= depth; e++) {
5012582d50cSToby Isaac       if (depthShift[2 * e] < firstStart) {
5022582d50cSToby Isaac         firstd     = e;
5032582d50cSToby Isaac         firstStart = depthShift[2 * d];
5042582d50cSToby Isaac       }
5052582d50cSToby Isaac     }
5062582d50cSToby Isaac     if (firstd != d) {
5072582d50cSToby Isaac       PetscInt swap[2];
5082582d50cSToby Isaac 
5092582d50cSToby Isaac       e                     = firstd;
5102582d50cSToby Isaac       swap[0]               = depthShift[2 * d];
5112582d50cSToby Isaac       swap[1]               = depthShift[2 * d + 1];
5122582d50cSToby Isaac       depthShift[2 * d]     = depthShift[2 * e];
5132582d50cSToby Isaac       depthShift[2 * d + 1] = depthShift[2 * e + 1];
5142582d50cSToby Isaac       depthShift[2 * e]     = swap[0];
5152582d50cSToby Isaac       depthShift[2 * e + 1] = swap[1];
5162582d50cSToby Isaac     }
5172582d50cSToby Isaac   }
5182582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
5190974a383SToby Isaac   for (d = 0; d <= depth; d++) {
5202582d50cSToby Isaac     off += depthShift[2 * d + 1];
52159eef20bSToby Isaac     depthShift[2 * d + 1] = depthShift[2 * d] + off;
5222582d50cSToby Isaac   }
5233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5242582d50cSToby Isaac }
5252582d50cSToby Isaac 
5262582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
527d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
528d71ae5a4SJacob Faibussowitsch {
5292582d50cSToby Isaac   PetscInt d;
5302582d50cSToby Isaac   PetscInt newOff = 0;
5312582d50cSToby Isaac 
5322582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5332582d50cSToby Isaac     if (p < depthShift[2 * d]) return p + newOff;
5342582d50cSToby Isaac     else newOff = depthShift[2 * d + 1] - depthShift[2 * d];
5352582d50cSToby Isaac   }
5360974a383SToby Isaac   return p + newOff;
5372582d50cSToby Isaac }
5382582d50cSToby Isaac 
5392582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
540d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
541d71ae5a4SJacob Faibussowitsch {
5422582d50cSToby Isaac   PetscInt d;
5432582d50cSToby Isaac   PetscInt newOff = 0;
5442582d50cSToby Isaac 
5452582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5462582d50cSToby Isaac     if (p < depthShift[2 * d + 1]) return p + newOff;
5472582d50cSToby Isaac     else newOff = depthShift[2 * d] - depthShift[2 * d + 1];
5482582d50cSToby Isaac   }
5490974a383SToby Isaac   return p + newOff;
550cd0c2139SMatthew G Knepley }
551cd0c2139SMatthew G Knepley 
552d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
553d71ae5a4SJacob Faibussowitsch {
554cd0c2139SMatthew G Knepley   PetscInt depth = 0, d, pStart, pEnd, p;
555fa8e8ae5SToby Isaac   DMLabel  depthLabel;
556cd0c2139SMatthew G Knepley 
557cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5593ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
560cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
5619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5620974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5639566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
5649566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "depth"));
5659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel));
5669566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
567cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
568cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
569fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
570fa8e8ae5SToby Isaac     IS       pIS;
571fa8e8ae5SToby Isaac 
5729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
573fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
574fa8e8ae5SToby Isaac     pEndNew   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5759566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
5769566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
5779566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
578cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
5792582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
580cd0c2139SMatthew G Knepley       PetscInt       size;
581412e9a14SMatthew G. Knepley       DMPolytopeType ct;
582cd0c2139SMatthew G Knepley 
5839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
5849566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
5859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
5869566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
5879566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
5889566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
589cd0c2139SMatthew G Knepley     }
590cd0c2139SMatthew G Knepley   }
5913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
592cd0c2139SMatthew G Knepley }
593cd0c2139SMatthew G Knepley 
594d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
595d71ae5a4SJacob Faibussowitsch {
5962582d50cSToby Isaac   PetscInt *newpoints;
5972582d50cSToby Isaac   PetscInt  depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
598cd0c2139SMatthew G Knepley 
599cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
6013ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
6029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
6039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
6049566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints));
605cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
6069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
607cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
608cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
6092582d50cSToby Isaac     PetscInt        size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
610cd0c2139SMatthew G Knepley 
6119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
6129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
6139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
614ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
6159566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
6169566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
6179566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
6189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
6199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
620ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
621dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
6229566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
623cd0c2139SMatthew G Knepley   }
6249566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
6253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
626cd0c2139SMatthew G Knepley }
627cd0c2139SMatthew G Knepley 
628d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
629d71ae5a4SJacob Faibussowitsch {
630cd0c2139SMatthew G Knepley   PetscSection coordSection, newCoordSection;
631cd0c2139SMatthew G Knepley   Vec          coordinates, newCoordinates;
632cd0c2139SMatthew G Knepley   PetscScalar *coords, *newCoords;
633f2b8cce1SMatthew G. Knepley   PetscInt     coordSize, sStart, sEnd;
634f2b8cce1SMatthew G. Knepley   PetscInt     dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
635f2b8cce1SMatthew G. Knepley   PetscBool    hasCells;
636cd0c2139SMatthew G Knepley 
637cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
6399566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
6409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
641cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
6429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
6439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
6449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
6459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
6469566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6479566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
6489566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
6499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
6509566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
651f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
6529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
653f2b8cce1SMatthew G. Knepley   if (hasCells) {
654f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
655f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
656f2b8cce1SMatthew G. Knepley 
6579566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6589566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
6599566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
660f2b8cce1SMatthew G. Knepley     }
661f2b8cce1SMatthew G. Knepley   }
662cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
6639566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
6649566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
665cd0c2139SMatthew G Knepley   }
6669566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
6679566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
6689566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
6699566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
6709566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates"));
6719566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
6729566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
6739566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates, VECSTANDARD));
6749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
6759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
6769566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6779566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
678f2b8cce1SMatthew G. Knepley   if (hasCells) {
679f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
680f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
681f2b8cce1SMatthew G. Knepley 
6829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
6849566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
685f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
686f2b8cce1SMatthew G. Knepley     }
687f2b8cce1SMatthew G. Knepley   }
688cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
689cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
690cd0c2139SMatthew G Knepley 
6919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
6929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
6939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
694f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
695cd0c2139SMatthew G Knepley   }
6969566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6979566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
6989566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
6999566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
7003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
701cd0c2139SMatthew G Knepley }
702cd0c2139SMatthew G Knepley 
703d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
704d71ae5a4SJacob Faibussowitsch {
705cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
706cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
707cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
708cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
7090e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
710cd0c2139SMatthew G Knepley 
711cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
7139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
7149566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
7156e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
716cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
7179566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation));
7180e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
7199566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7209566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7219566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
7229566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
723cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
7242582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
725cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
726cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
727cd0c2139SMatthew G Knepley     }
7289566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
7299566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
7300e33faafSMatthew G. Knepley   }
7313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7320e33faafSMatthew G. Knepley }
7330e33faafSMatthew G. Knepley 
734d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
735d71ae5a4SJacob Faibussowitsch {
7360e33faafSMatthew G. Knepley   PetscSF   sfPoint, sfPointNew;
7370e33faafSMatthew G. Knepley   PetscBool useNatural;
7380e33faafSMatthew G. Knepley 
7390e33faafSMatthew G. Knepley   PetscFunctionBegin;
7400e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
7419566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
7429566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
7439566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
7440e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
7459566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
7460e33faafSMatthew G. Knepley   if (useNatural) {
7470e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
7480e33faafSMatthew G. Knepley 
7499566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
7509566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
7519566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
7529566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
753cd0c2139SMatthew G Knepley   }
7543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
755cd0c2139SMatthew G Knepley }
756cd0c2139SMatthew G Knepley 
757d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
758d71ae5a4SJacob Faibussowitsch {
759d56405f8SMatthew G. Knepley   PetscInt depth = 0, numLabels, l;
760cd0c2139SMatthew G Knepley 
761cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
763cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
7649566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
765cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
766cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
767cd0c2139SMatthew G Knepley     const char     *lname;
768fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
769cd0c2139SMatthew G Knepley     IS              valueIS;
770cd0c2139SMatthew G Knepley     const PetscInt *values;
771cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
772cd0c2139SMatthew G Knepley 
7739566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
7749566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
775cd0c2139SMatthew G Knepley     if (isDepth) continue;
7769566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
777fa8e8ae5SToby Isaac     if (isDim) continue;
7789566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
7799566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
7809566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
7819566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &val));
7829566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, val));
7839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
7849566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
7859566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
786cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
787cd0c2139SMatthew G Knepley       IS              pointIS;
788cd0c2139SMatthew G Knepley       const PetscInt *points;
789cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
790cd0c2139SMatthew G Knepley 
7919566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
7929566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
7939566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
794cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
7952582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
796cd0c2139SMatthew G Knepley 
7979566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
798cd0c2139SMatthew G Knepley       }
7999566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
8009566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
801cd0c2139SMatthew G Knepley     }
8029566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
8039566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
804cd0c2139SMatthew G Knepley   }
8053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
806d56405f8SMatthew G. Knepley }
807d56405f8SMatthew G. Knepley 
808d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
809d71ae5a4SJacob Faibussowitsch {
810d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
811d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
812d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
813d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
814d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
815d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
816d56405f8SMatthew G. Knepley 
817d56405f8SMatthew G. Knepley   PetscFunctionBegin;
818cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
8199566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8209566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
8219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
8229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8239566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
8249566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
8259566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
826d56405f8SMatthew G. Knepley   if (createGhostLabel) {
8279566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
8289566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
829d56405f8SMatthew G. Knepley   }
830cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
83148a46eb9SPierre Jolivet     for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
832cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
833cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
8349566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
8351baa6e33SBarry Smith     } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
836cd0c2139SMatthew G Knepley   }
83748a46eb9SPierre Jolivet   for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
838d56405f8SMatthew G. Knepley   if (ghostLabel) {
8399566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
840cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
841cd0c2139SMatthew G Knepley       PetscInt numCells;
842cd0c2139SMatthew G Knepley 
8439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
844cd0c2139SMatthew G Knepley       if (numCells < 2) {
8459566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
846cd0c2139SMatthew G Knepley       } else {
847cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
848cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
849cd0c2139SMatthew G Knepley 
8509566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
8519566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
8529566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
8539566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
854cd0c2139SMatthew G Knepley       }
855cd0c2139SMatthew G Knepley     }
856d56405f8SMatthew G. Knepley   }
8573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
858cd0c2139SMatthew G Knepley }
859cd0c2139SMatthew G Knepley 
860d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
861d71ae5a4SJacob Faibussowitsch {
862ca04dac2SToby Isaac   DM           refTree;
863ca04dac2SToby Isaac   PetscSection pSec;
864ca04dac2SToby Isaac   PetscInt    *parents, *childIDs;
865ca04dac2SToby Isaac 
866ca04dac2SToby Isaac   PetscFunctionBegin;
8679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
8689566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew, refTree));
8699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL));
870ca04dac2SToby Isaac   if (pSec) {
8712582d50cSToby Isaac     PetscInt     p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
872fb4630b5SToby Isaac     PetscInt    *childIDsShifted;
873ca04dac2SToby Isaac     PetscSection pSecShifted;
874ca04dac2SToby Isaac 
8759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd));
8769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
8772582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
8782582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
8799566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted));
8809566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted));
8819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted));
882ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
883fb4630b5SToby Isaac       /* start off assuming no children */
8849566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, p, 0));
885fb4630b5SToby Isaac     }
886fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
887fb4630b5SToby Isaac       PetscInt dof;
888fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
889ca04dac2SToby Isaac 
8909566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
8919566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof));
892ca04dac2SToby Isaac     }
8939566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
894fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
895fb4630b5SToby Isaac       PetscInt dof;
896fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
897fb4630b5SToby Isaac 
8989566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
899fb4630b5SToby Isaac       if (dof) {
900fb4630b5SToby Isaac         PetscInt off, offNew;
901fb4630b5SToby Isaac 
9029566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec, p, &off));
9039566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew));
904fb4630b5SToby Isaac         parentsShifted[offNew]  = DMPlexShiftPoint_Internal(parents[off], depth, depthShift);
905fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
906fb4630b5SToby Isaac       }
907fb4630b5SToby Isaac     }
9089566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted));
9099566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted, childIDsShifted));
9109566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
911ca04dac2SToby Isaac   }
9123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
913ca04dac2SToby Isaac }
914ca04dac2SToby Isaac 
915d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
916d71ae5a4SJacob Faibussowitsch {
917da97024aSMatthew G. Knepley   PetscSF          sf;
918cd0c2139SMatthew G Knepley   IS               valueIS;
919da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
920cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
9212582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
9224fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
923cd0c2139SMatthew G Knepley 
924cd0c2139SMatthew G Knepley   PetscFunctionBegin;
9259566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
9269566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
927da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
9289566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
929cd0c2139SMatthew G Knepley   /* Count ghost cells */
9309566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
9319566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
9329566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
9334a6cfa73SMatthew G. Knepley   Ng = 0;
934cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
93546c796b9SMatthew G. Knepley     IS              faceIS;
93646c796b9SMatthew G. Knepley     const PetscInt *faces;
93746c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
938cd0c2139SMatthew G Knepley 
9399566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9409566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
94246c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
943ca04dac2SToby Isaac       PetscInt numChildren;
944ca04dac2SToby Isaac 
9459566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9469566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
947ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
948ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
94946c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
95046c796b9SMatthew G. Knepley     }
9514a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
9529566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
954cd0c2139SMatthew G Knepley   }
9559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
9569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift));
9572582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
95859eef20bSToby Isaac     PetscInt dEnd;
9592582d50cSToby Isaac 
9609566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd));
96159eef20bSToby Isaac     depthShift[2 * d]     = dEnd;
9622582d50cSToby Isaac     depthShift[2 * d + 1] = 0;
9632582d50cSToby Isaac   }
9642582d50cSToby Isaac   if (depth >= 0) depthShift[2 * depth + 1] = Ng;
9659566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
9669566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
967cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
9689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
96948a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1));
970cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
971cd0c2139SMatthew G Knepley     IS              faceIS;
972cd0c2139SMatthew G Knepley     const PetscInt *faces;
973cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
974cd0c2139SMatthew G Knepley 
9759566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9769566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9779566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
978cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
979ca04dac2SToby Isaac       PetscInt size, numChildren;
980cd0c2139SMatthew G Knepley 
9819566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
983ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
98446c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
98663a3b9bcSJacob Faibussowitsch       PetscCheck(size == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %" PetscInt_FMT " with %" PetscInt_FMT " support cells", faces[f], size);
9879566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
988cd0c2139SMatthew G Knepley     }
9899566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9909566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
991cd0c2139SMatthew G Knepley   }
992cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
9939566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
9949566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
995cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
996cd0c2139SMatthew G Knepley   ghostCell = cEnd;
997cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
998cd0c2139SMatthew G Knepley     IS              faceIS;
999cd0c2139SMatthew G Knepley     const PetscInt *faces;
1000cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
1001cd0c2139SMatthew G Knepley 
10029566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
10039566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
10049566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
100546c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
1006ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
1007cd0c2139SMatthew G Knepley 
10089566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
10099566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
1010ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
101146c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
10129566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
10139566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
101446c796b9SMatthew G. Knepley       ++ghostCell;
1015cd0c2139SMatthew G Knepley     }
10169566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
10179566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
1018cd0c2139SMatthew G Knepley   }
10199566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
10209566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
10219566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
10229566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
10239566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
10249566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
10259566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
10269566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
102748a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
1028966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
10294fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
10304fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L));
10314a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
10323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1033cd0c2139SMatthew G Knepley }
1034cd0c2139SMatthew G Knepley 
1035*cc4c1da9SBarry Smith /*@
1036cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
1037cd0c2139SMatthew G Knepley 
103820f4b53cSBarry Smith   Collective
1039cd0c2139SMatthew G Knepley 
1040cd0c2139SMatthew G Knepley   Input Parameters:
104120f4b53cSBarry Smith + dm        - The original `DM`
104220f4b53cSBarry Smith - labelName - The label specifying the boundary faces, or "Face Sets" if this is `NULL`
1043cd0c2139SMatthew G Knepley 
1044cd0c2139SMatthew G Knepley   Output Parameters:
104520f4b53cSBarry Smith + numGhostCells - The number of ghost cells added to the `DM`
104620f4b53cSBarry Smith - dmGhosted     - The new `DM`
1047cd0c2139SMatthew G Knepley 
1048cd0c2139SMatthew G Knepley   Level: developer
1049cd0c2139SMatthew G Knepley 
105020f4b53cSBarry Smith   Note:
105120f4b53cSBarry Smith   If no label exists of that name, one will be created marking all boundary faces
105220f4b53cSBarry Smith 
10531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`
105431266bc0SMatthew G. Knepley @*/
1055d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
1056d71ae5a4SJacob Faibussowitsch {
1057cd0c2139SMatthew G Knepley   DM          gdm;
1058cd0c2139SMatthew G Knepley   DMLabel     label;
1059cd0c2139SMatthew G Knepley   const char *name = labelName ? labelName : "Face Sets";
1060412e9a14SMatthew G. Knepley   PetscInt    dim, Ng = 0;
1061b0441da4SMatthew G. Knepley   PetscBool   useCone, useClosure;
1062cd0c2139SMatthew G Knepley 
1063cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1064cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10654f572ea9SToby Isaac   if (numGhostCells) PetscAssertPointer(numGhostCells, 3);
10664f572ea9SToby Isaac   PetscAssertPointer(dmGhosted, 4);
10679566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
10689566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
10699566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
10709566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
10719566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
10729566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure));
10739566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
1074cd0c2139SMatthew G Knepley   if (!label) {
1075cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
10769566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
10779566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
10789566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
1079cd0c2139SMatthew G Knepley   }
10809566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
10819566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
10825de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
1083cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
1084d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
1085cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
10863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1087cd0c2139SMatthew G Knepley }
1088cd0c2139SMatthew G Knepley 
1089d71ae5a4SJacob Faibussowitsch static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue)
1090d71ae5a4SJacob Faibussowitsch {
1091accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
1092accc9626SMatthew G. Knepley 
1093accc9626SMatthew G. Knepley   PetscFunctionBegin;
1094accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1095accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
109648a46eb9SPierre Jolivet   for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
1097accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
1098accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
1099accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
1100accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
1101accc9626SMatthew G. Knepley 
1102accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
1103accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
1104accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1105accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
1106accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1107accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1108accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
1109accc9626SMatthew G. Knepley         PetscInt       clval;
1110accc9626SMatthew G. Knepley 
1111accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
1112accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
1113accc9626SMatthew G. Knepley         if (clval == -1) {
1114accc9626SMatthew G. Knepley           const PetscInt *cone;
1115accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
1116accc9626SMatthew G. Knepley 
1117accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
1118accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
1119accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
1120accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
1121accc9626SMatthew G. Knepley             PetscInt cpval;
1122accc9626SMatthew G. Knepley 
1123accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
1124accc9626SMatthew G. Knepley             if (cpval != -1) {
1125accc9626SMatthew G. Knepley               PetscInt dep;
1126accc9626SMatthew G. Knepley 
1127accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
1128accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift + dep) : shift + dep;
1129accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
1130accc9626SMatthew G. Knepley               break;
1131accc9626SMatthew G. Knepley             }
1132accc9626SMatthew G. Knepley           }
1133accc9626SMatthew G. Knepley         }
1134accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
1135accc9626SMatthew G. Knepley         if (d == dim - 1 && clval != -1) {
1136accc9626SMatthew G. Knepley           const PetscInt *support;
1137accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
1138accc9626SMatthew G. Knepley 
1139accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
1140accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
1141accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
1142accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
1143accc9626SMatthew G. Knepley             if (nval == -1) {
1144accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1));
1145accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1146accc9626SMatthew G. Knepley             }
1147accc9626SMatthew G. Knepley           }
1148accc9626SMatthew G. Knepley         }
1149accc9626SMatthew G. Knepley       }
1150accc9626SMatthew G. Knepley     }
1151accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1152accc9626SMatthew G. Knepley   }
1153accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
11543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1155accc9626SMatthew G. Knepley }
1156accc9626SMatthew G. Knepley 
1157accc9626SMatthew G. Knepley typedef struct {
1158accc9626SMatthew G. Knepley   DM               dm;
1159accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1160accc9626SMatthew G. Knepley } PointDivision;
1161accc9626SMatthew G. Knepley 
1162d71ae5a4SJacob Faibussowitsch static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx)
1163d71ae5a4SJacob Faibussowitsch {
1164accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *)ctx;
1165accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val - 1 : val + 1;
1166accc9626SMatthew G. Knepley   const PetscInt *support;
1167accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1168accc9626SMatthew G. Knepley 
1169accc9626SMatthew G. Knepley   PetscFunctionBegin;
1170accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1171accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1172accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1173accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1174accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1175accc9626SMatthew G. Knepley   }
11763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1177accc9626SMatthew G. Knepley }
1178accc9626SMatthew G. Knepley 
1179accc9626SMatthew G. Knepley /* Mark cells by label propagation */
1180d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel)
1181d71ae5a4SJacob Faibussowitsch {
1182accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1183accc9626SMatthew G. Knepley   PointDivision    div;
1184accc9626SMatthew G. Knepley   PetscSF          pointSF;
1185accc9626SMatthew G. Knepley   IS               pointIS;
1186accc9626SMatthew G. Knepley   const PetscInt  *points;
1187accc9626SMatthew G. Knepley   PetscBool        empty;
1188accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1189accc9626SMatthew G. Knepley 
1190accc9626SMatthew G. Knepley   PetscFunctionBegin;
1191accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1192accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1193accc9626SMatthew G. Knepley   div.dm    = dm;
1194accc9626SMatthew G. Knepley   div.queue = queue;
1195accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1196accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS));
1197accc9626SMatthew G. Knepley   if (pointIS) {
1198accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1199accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
120048a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1201accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1202accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1203accc9626SMatthew G. Knepley   }
1204accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS));
1205accc9626SMatthew G. Knepley   if (pointIS) {
1206accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1207accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
120848a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1209accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1210accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1211accc9626SMatthew G. Knepley   }
1212accc9626SMatthew G. Knepley 
1213accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1214accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1215accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1216accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1217accc9626SMatthew G. Knepley   while (!empty) {
1218accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1219accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1220accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1221accc9626SMatthew G. Knepley   }
1222accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1223accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
12243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1225accc9626SMatthew G. Knepley }
1226accc9626SMatthew G. Knepley 
1227607ab7a9SMatthew G. Knepley /*
1228faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1229607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1230faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1231b6dfa339SMatthew G. Knepley     Ghost:          These are shared fault faces which are not owned by this process. These do not produce hybrid cells and do not replicate
1232607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1233a6ae58d1SMatthew G. Knepley 
1234a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1235a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1236607ab7a9SMatthew G. Knepley */
1237d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1238d71ae5a4SJacob Faibussowitsch {
1239cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1240607ab7a9SMatthew G. Knepley   IS               valueIS;
1241607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0; /* The number of depths for which we have replicated points */
1242607ab7a9SMatthew G. Knepley   const PetscInt  *values;    /* List of depths for which we have replicated points */
124318c5995bSMatthew G. Knepley   IS              *splitIS;
124418c5995bSMatthew G. Knepley   IS              *unsplitIS;
1245b6dfa339SMatthew G. Knepley   IS               ghostIS;
1246607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
124718c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
124836dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
124936dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1250b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1251607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
125218c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1253b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1254cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1255cd0c2139SMatthew G Knepley   Vec              coordinates;
1256cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1257a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;   /* The first hybrid point at each depth in the original mesh */
1258a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;   /* The point limit at each depth in the original mesh */
1259607ab7a9SMatthew G. Knepley   PetscInt        *depthShift; /* Number of replicated+hybrid points at each depth */
1260607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;    /* The first replicated point at each depth in the new mesh, hybrids come after this */
1261607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
126218c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1263cd0c2139SMatthew G Knepley 
1264cd0c2139SMatthew G Knepley   PetscFunctionBegin;
12659566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
12669566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
12679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
12689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1269412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
12709566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1271cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
12729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
12739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld));
12749566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints));
1275607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
12769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
12779566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1278a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1279a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1280607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
128118c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1282607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1283a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1284607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
128518c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
128618c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
128718c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
128859eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
128959eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
129059eef20bSToby Isaac     depthShift[2 * d]     = depthMax[d];
129159eef20bSToby Isaac     depthShift[2 * d + 1] = 0;
1292607ab7a9SMatthew G. Knepley   }
1293b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1294b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1295cd0c2139SMatthew G Knepley   if (label) {
12969566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
12979566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
12989566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1299cd0c2139SMatthew G Knepley   }
1300cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1301cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1302cd0c2139SMatthew G Knepley 
1303cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
13049566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
130518c5995bSMatthew G. Knepley     if (splitIS[dep]) {
13069566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
13079566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
130818c5995bSMatthew G. Knepley     }
13099566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep]));
131018c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
13119566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
13129566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1313cd0c2139SMatthew G Knepley     }
1314cd0c2139SMatthew G Knepley   }
1315b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS));
1316b6dfa339SMatthew G. Knepley   if (ghostIS) {
1317b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1318b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1319b6dfa339SMatthew G. Knepley   }
1320607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
132118c5995bSMatthew G. Knepley   for (d = 1; d <= depth; ++d) numHybridPoints[d] = numSplitPoints[d - 1] + numUnsplitPoints[d - 1]; /* There is a hybrid cell/face/edge for every split face/edge/vertex   */
13222582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d];
13239566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
132459eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
132559eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
132659eef20bSToby Isaac    * added points */
13272582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
13289566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1329cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1330cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1331cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1332cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
13332582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
13344c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1335cd0c2139SMatthew G Knepley       const PetscInt *support;
1336394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
13374c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1338cd0c2139SMatthew G Knepley 
13399566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13409566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
13419566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13429566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
13439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
13449566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1345cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
13464c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13474c367dbcSMatthew G. Knepley 
1348cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
13499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1350412e9a14SMatthew G. Knepley         switch (coneSize) {
1351d71ae5a4SJacob Faibussowitsch         case 2:
1352d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));
1353d71ae5a4SJacob Faibussowitsch           break;
1354d71ae5a4SJacob Faibussowitsch         case 3:
1355d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));
1356d71ae5a4SJacob Faibussowitsch           break;
1357d71ae5a4SJacob Faibussowitsch         case 4:
1358d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));
1359d71ae5a4SJacob Faibussowitsch           break;
1360412e9a14SMatthew G. Knepley         }
1361b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1362b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1363b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1364b6dfa339SMatthew G. Knepley           const PetscInt *support;
1365b6dfa339SMatthew G. Knepley           PetscInt        val;
1366b6dfa339SMatthew G. Knepley 
1367b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1368b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1369b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1370b6dfa339SMatthew G. Knepley           else PetscCall(DMPlexSetSupportSize(sdm, newp, 2));
1371b6dfa339SMatthew G. Knepley         }
1372cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13734c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1374cd0c2139SMatthew G Knepley 
13759566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13764c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1377cd0c2139SMatthew G Knepley           PetscInt val;
1378cd0c2139SMatthew G Knepley 
13799566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13804c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
13814c367dbcSMatthew G. Knepley           if ((val == 1) || (val == (shift + 1))) ++qn;
13824c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1383cd0c2139SMatthew G Knepley         }
13844c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
13859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13864c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
13879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13884c367dbcSMatthew G. Knepley         /* Add hybrid edge */
13899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
13909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1392cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
13934c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13944c367dbcSMatthew G. Knepley 
13959566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13964c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1397cd0c2139SMatthew G Knepley           PetscInt val;
1398cd0c2139SMatthew G Knepley 
13999566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
14004c367dbcSMatthew G. Knepley           if (val == dim - 1) ++qf;
14014c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn;
14024c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp;
1403cd0c2139SMatthew G Knepley         }
14044c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
14059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
14064c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
14079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
14084c367dbcSMatthew G. Knepley         /* Add hybrid face */
14099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1412cd0c2139SMatthew G Knepley       }
1413cd0c2139SMatthew G Knepley     }
1414cd0c2139SMatthew G Knepley   }
141518c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
141618c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
141718c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
14182582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
141918c5995bSMatthew G. Knepley       const PetscInt *support;
1420da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
142118c5995bSMatthew G. Knepley 
14229566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14239566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14249566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
142518c5995bSMatthew G. Knepley       if (dep == 0) {
142618c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
142718c5995bSMatthew G. Knepley 
142839254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
142939254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
14309566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
143139254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
143239254ff6SMatthew G. Knepley         }
14339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
143418c5995bSMatthew G. Knepley         /* Add hybrid edge */
14359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1436e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1437e1757548SMatthew G. Knepley           PetscInt val;
1438e1757548SMatthew G. Knepley 
14399566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1440e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1441da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1442da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1443e1757548SMatthew G. Knepley         }
14449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
14459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
144618c5995bSMatthew G. Knepley       } else if (dep == dim - 2) {
144718c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1448cd0c2139SMatthew G Knepley         PetscInt       val;
1449cd0c2139SMatthew G Knepley 
1450da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14519566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1452da1dd7e4SMatthew G. Knepley           if (val == dim - 1) qf += 2;
1453da1dd7e4SMatthew G. Knepley           else ++qf;
1454cd0c2139SMatthew G Knepley         }
145518c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
14569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
145718c5995bSMatthew G. Knepley         /* Add hybrid face */
1458da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14599566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1460da1dd7e4SMatthew G. Knepley           if (val == dim - 1) ++qf;
1461da1dd7e4SMatthew G. Knepley         }
14629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1465cd0c2139SMatthew G Knepley       }
1466cd0c2139SMatthew G Knepley     }
1467cd0c2139SMatthew G Knepley   }
1468cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
14699566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
14709566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
14719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
14729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew));
1473cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1474cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1475cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1476cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
14772582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
14784c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1479cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1480b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
14814c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1482cd0c2139SMatthew G Knepley 
14839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
14849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
14869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
14879566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14889566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1489cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
149096a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
14914c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1492cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1493cd0c2139SMatthew G Knepley 
1494b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1495cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
14969566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp, &supportF));
14979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1498cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1499cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1500cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
15019566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
150218c5995bSMatthew G. Knepley           if (v < 0) {
15039566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
150463a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1);
15052582d50cSToby Isaac             coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
150696a07cd0SMatthew G. Knepley             hasUnsplit     = PETSC_TRUE;
150718c5995bSMatthew G. Knepley           } else {
15084c367dbcSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep - 1];
1509163235baSMatthew G. Knepley             if (dep > 1) {
1510163235baSMatthew G. Knepley               const PetscInt *econe;
1511163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1512163235baSMatthew G. Knepley 
15139566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
15149566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1515163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
15169566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs));
15179566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu));
1518163235baSMatthew G. Knepley                 if (vs >= 0) continue;
151963a3b9bcSJacob Faibussowitsch                 PetscCheck(vu >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, econe[r], dep - 2);
1520163235baSMatthew G. Knepley                 hasUnsplit = PETSC_TRUE;
1521163235baSMatthew G. Knepley               }
1522163235baSMatthew G. Knepley             }
1523cd0c2139SMatthew G Knepley           }
1524cd0c2139SMatthew G Knepley         }
15259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
15269566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1527e537020bSMatthew G. Knepley         /* Face support */
1528b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1529cd0c2139SMatthew G Knepley 
1530b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1531b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1532b6dfa339SMatthew G. Knepley         else vals[1] = -vals[0];
1533b6dfa339SMatthew G. Knepley         PetscCheck(vals[0] * vals[1] < 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid support labels %" PetscInt_FMT " %" PetscInt_FMT, vals[0], vals[1]);
1534b6dfa339SMatthew G. Knepley 
1535b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1536b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1537b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1538b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1539accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1540b6dfa339SMatthew G. Knepley             } else {
1541b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1542accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1543b6dfa339SMatthew G. Knepley             }
1544b6dfa339SMatthew G. Knepley           } else {
1545b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1546cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
15479566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1548cd0c2139SMatthew G Knepley             } else {
1549cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
15509566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1551b6dfa339SMatthew G. Knepley             }
1552b6dfa339SMatthew G. Knepley           }
1553b6dfa339SMatthew G. Knepley         }
1554b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1555e537020bSMatthew G. Knepley         {
1556e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1557b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1558accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1559e537020bSMatthew G. Knepley 
15609371c9d4SSatish Balay           if (supportSize > 1) {
15619371c9d4SSatish Balay             ocell = vals[0] < 0 ? support[1] : support[0];
15629371c9d4SSatish Balay           } else {
15639371c9d4SSatish Balay             ocell = support[0];
15649371c9d4SSatish Balay             flip  = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;
15659371c9d4SSatish Balay           }
1566b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1567b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1568e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1569e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1570e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1571b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc];
1572e537020bSMatthew G. Knepley               break;
1573cd0c2139SMatthew G Knepley             }
1574cd0c2139SMatthew G Knepley           }
1575b6dfa339SMatthew G. Knepley           PetscCheck(nc < nconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %" PetscInt_FMT " in neighboring cell %" PetscInt_FMT, oldp, ocell);
1576e537020bSMatthew G. Knepley         }
15774c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1578b6dfa339SMatthew G. Knepley         {
157985036b15SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangements(ct) / 2;
1580b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1581b6dfa339SMatthew G. Knepley         }
158285036b15SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, coneONew[0]);
1583b5a892a1SMatthew G. Knepley 
1584fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp; /* Extracted negative side orientation above */
15854c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
15864c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1587e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1588412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1589b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q * 2 + 0];
1590b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q * 2 + 1];
1591b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1592412e9a14SMatthew G. Knepley 
15939566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
159418c5995bSMatthew G. Knepley           if (v < 0) {
15959566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
159618c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
159718c5995bSMatthew G. Knepley           } else {
159818c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep];
159918c5995bSMatthew G. Knepley           }
1600b5a892a1SMatthew G. Knepley           coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1601e537020bSMatthew G. Knepley         }
16029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
16039566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
160496a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
16059566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1606cd0c2139SMatthew G Knepley       } else if (dep == 0) {
16074c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1608cd0c2139SMatthew G Knepley 
1609cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
16104c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1611cd0c2139SMatthew G Knepley           PetscInt val;
1612cd0c2139SMatthew G Knepley 
16139566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1614ad540459SPierre Jolivet           if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1615cd0c2139SMatthew G Knepley         }
16164c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
16179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1618cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
16194c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1620cd0c2139SMatthew G Knepley           PetscInt val, edge;
1621cd0c2139SMatthew G Knepley 
16229566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1623cd0c2139SMatthew G Knepley           if (val == 1) {
16249566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
162563a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16264c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep + 1];
1627cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
16282582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1629cd0c2139SMatthew G Knepley           }
1630cd0c2139SMatthew G Knepley         }
16314c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
16329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16334c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1634cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1635cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
16369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
16374c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
16384c367dbcSMatthew G. Knepley           PetscInt val, edge;
16394c367dbcSMatthew G. Knepley 
16409566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16414c367dbcSMatthew G. Knepley           if (val == 1) {
16429566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
164363a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16444c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
16454c367dbcSMatthew G. Knepley           }
16464c367dbcSMatthew G. Knepley         }
16479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1648cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
16494c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16504c367dbcSMatthew G. Knepley 
1651cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1652cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1653cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
16549566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
1655e1757548SMatthew G. Knepley           if (v < 0) {
16569566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
165763a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1);
16582582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1659e1757548SMatthew G. Knepley           } else {
16604c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep - 1];
1661cd0c2139SMatthew G Knepley           }
1662e1757548SMatthew G. Knepley         }
16639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1664cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1665cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1666cd0c2139SMatthew G Knepley           PetscInt val;
1667cd0c2139SMatthew G Knepley 
16689566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16694c367dbcSMatthew G. Knepley           if (val == dim - 1) {
16702582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
16714c367dbcSMatthew G. Knepley           } else if (val == (shift + dim - 1)) {
16722582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1673cd0c2139SMatthew G Knepley           }
1674cd0c2139SMatthew G Knepley         }
1675b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1677cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1678cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1679cd0c2139SMatthew G Knepley           PetscInt val, face;
1680cd0c2139SMatthew G Knepley 
16819566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1682cd0c2139SMatthew G Knepley           if (val == dim - 1) {
16839566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
168463a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
16854c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep + 1];
1686cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim - 1)) {
16872582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1688cd0c2139SMatthew G Knepley           }
1689cd0c2139SMatthew G Knepley         }
1690b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16924c367dbcSMatthew G. Knepley         /* Hybrid face */
16934c367dbcSMatthew G. Knepley         coneNew[0] = newp;
16944c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
16954c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
16964c367dbcSMatthew G. Knepley           PetscInt vertex;
16979566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex));
1698e1757548SMatthew G. Knepley           if (vertex < 0) {
16999566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex));
170063a3b9bcSJacob Faibussowitsch             PetscCheck(vertex >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[v], dep - 1);
1701e1757548SMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
1702e1757548SMatthew G. Knepley           } else {
17034c367dbcSMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
17044c367dbcSMatthew G. Knepley           }
1705e1757548SMatthew G. Knepley         }
17069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
17074c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
17084c367dbcSMatthew G. Knepley           PetscInt val, face;
17094c367dbcSMatthew G. Knepley 
17109566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
17114c367dbcSMatthew G. Knepley           if (val == dim - 1) {
17129566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
171363a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
17144c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
17154c367dbcSMatthew G. Knepley           }
17164c367dbcSMatthew G. Knepley         }
17179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1718cd0c2139SMatthew G Knepley       }
1719cd0c2139SMatthew G Knepley     }
1720cd0c2139SMatthew G Knepley   }
172118c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
172218c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
172318c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
17242582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1725b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1726e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
172718c5995bSMatthew G. Knepley 
17289566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
17299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
17309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
17319566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
173218c5995bSMatthew G. Knepley       if (dep == 0) {
173318c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
173418c5995bSMatthew G. Knepley 
173518c5995bSMatthew G. Knepley         /* Unsplit vertex */
17369566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
173718c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
17382582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
17399566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
1740ad540459SPierre Jolivet           if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1];
174118c5995bSMatthew G. Knepley         }
174218c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174318c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174463a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
17459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
174618c5995bSMatthew G. Knepley         /* Hybrid edge */
174718c5995bSMatthew G. Knepley         coneNew[0] = newp;
174818c5995bSMatthew G. Knepley         coneNew[1] = newp;
17499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
175018c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
175118c5995bSMatthew G. Knepley           PetscInt val, edge;
175218c5995bSMatthew G. Knepley 
17539566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
175418c5995bSMatthew G. Knepley           if (val == 1) {
17559566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
175663a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
175718c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1758e1757548SMatthew G. Knepley           } else if (val == (shift2 + 1)) {
17599566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge));
176063a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1761e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1];
176218c5995bSMatthew G. Knepley           }
176318c5995bSMatthew G. Knepley         }
17649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1765e1757548SMatthew G. Knepley       } else if (dep == dim - 2) {
1766e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1767e1757548SMatthew G. Knepley 
1768da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1769e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1770e1757548SMatthew G. Knepley           PetscInt val, face;
1771e1757548SMatthew G. Knepley 
17729566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1773e1757548SMatthew G. Knepley           if (val == dim - 1) {
17749566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
177563a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
17762582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1777e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 1];
1778e1757548SMatthew G. Knepley           } else {
17792582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1780e1757548SMatthew G. Knepley           }
1781e1757548SMatthew G. Knepley         }
1782e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1783e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
17849566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
178563a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, newp, qf, supportSizeNew);
17869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1787e1757548SMatthew G. Knepley         /* Add hybrid face */
1788e1757548SMatthew G. Knepley         coneNew[0] = newp;
1789212cc919SMatthew G. Knepley         coneNew[1] = newp;
17909566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
179163a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1792212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17939566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
179463a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]);
1795e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1797da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1798da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1799da1dd7e4SMatthew G. Knepley 
18009566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1801da1dd7e4SMatthew G. Knepley           if (val == dim - 1) {
18029566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
1803da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1804da1dd7e4SMatthew G. Knepley           }
1805da1dd7e4SMatthew G. Knepley         }
18069566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
180763a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, hybface, qf, supportSizeNew);
18089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1809cd0c2139SMatthew G Knepley       }
1810cd0c2139SMatthew G Knepley     }
1811cd0c2139SMatthew G Knepley   }
1812cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1813cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1814cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1815cd0c2139SMatthew G Knepley     IS              pIS;
1816cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1817cd0c2139SMatthew G Knepley     const PetscInt *points;
1818cd0c2139SMatthew G Knepley 
1819cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
18209566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1821cd0c2139SMatthew G Knepley     if (!pIS) continue;
1822cd0c2139SMatthew G Knepley     dep = -dep - shift;
18239566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
18249566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1825cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1826cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
18272582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1828cd0c2139SMatthew G Knepley       const PetscInt *cone;
1829cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
183050cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1831cd0c2139SMatthew G Knepley 
1832cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1833cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
18349566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
18359566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1836cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1837e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift);
1838cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1839cd0c2139SMatthew G Knepley 
18409566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1841cd0c2139SMatthew G Knepley         if (val == dep - 1) {
18429566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp));
184363a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1);
1844cd0c2139SMatthew G Knepley           csplitp = pMaxNew[dep - 1] + cp;
18459566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
184650cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1847cd0c2139SMatthew G Knepley         }
1848cd0c2139SMatthew G Knepley       }
18494a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
185028b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1851cd0c2139SMatthew G Knepley     }
18529566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
18539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1854cd0c2139SMatthew G Knepley   }
18550e18dc48SMatthew G. Knepley   PetscCall(DMPlexReorderCohesiveSupports(sdm));
1856fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
18579566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
18589566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
18599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
18609566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1861cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
18622582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1863cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1864cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1865cd0c2139SMatthew G Knepley 
18669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
18679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
18689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1869cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d];
1870cd0c2139SMatthew G Knepley   }
18719566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1872fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
18739566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1874b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1875fa8e8ae5SToby Isaac   /* Step 9: Labels */
18769566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
18779566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
18789566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1879cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1880cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
18812582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1882cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1883cd0c2139SMatthew G Knepley       PetscInt       l;
1884cd0c2139SMatthew G Knepley 
18857db7e0a7SMatthew G. Knepley       if (splitLabel) {
18867db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
18877db7e0a7SMatthew G. Knepley 
18889566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp, val));
18899566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
18907db7e0a7SMatthew G. Knepley       }
1891cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1892cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1893cd0c2139SMatthew G Knepley         const char *lname;
1894cd0c2139SMatthew G Knepley         PetscInt    val;
18959a356370SMatthew G. Knepley         PetscBool   isDepth;
1896cd0c2139SMatthew G Knepley 
18979566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
18989566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
18999a356370SMatthew G. Knepley         if (isDepth) continue;
19009566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
19019566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
190248a46eb9SPierre Jolivet         if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val));
1903cd0c2139SMatthew G Knepley       }
1904cd0c2139SMatthew G Knepley     }
1905cd0c2139SMatthew G Knepley   }
1906cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1907cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1908cd0c2139SMatthew G Knepley 
1909cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
19109566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
19119566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
19129566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
19139566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1914cd0c2139SMatthew G Knepley   }
1915b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1916b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1917cd0c2139SMatthew G Knepley   if (label) {
19189566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
19199566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1920cd0c2139SMatthew G Knepley   }
19210d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
19229566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
192336dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
19240d4d4d06SMatthew G. Knepley   }
19259566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
19269566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
19279566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
19283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1929cd0c2139SMatthew G Knepley }
1930cd0c2139SMatthew G Knepley 
1931cd0c2139SMatthew G Knepley /*@C
1932cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1933cd0c2139SMatthew G Knepley 
193420f4b53cSBarry Smith   Collective
1935cd0c2139SMatthew G Knepley 
1936cd0c2139SMatthew G Knepley   Input Parameters:
193720f4b53cSBarry Smith + dm    - The original `DM`
193820f4b53cSBarry Smith - label - The `DMLabel` specifying the boundary faces (this could be auto-generated)
1939cd0c2139SMatthew G Knepley 
1940cd0c2139SMatthew G Knepley   Output Parameters:
194120f4b53cSBarry Smith + splitLabel - The `DMLabel` containing the split points, or `NULL` if no output is desired
194220f4b53cSBarry Smith - dmSplit    - The new `DM`
1943cd0c2139SMatthew G Knepley 
1944cd0c2139SMatthew G Knepley   Level: developer
1945cd0c2139SMatthew G Knepley 
19461cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1947cd0c2139SMatthew G Knepley @*/
1948d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1949d71ae5a4SJacob Faibussowitsch {
1950cd0c2139SMatthew G Knepley   DM       sdm;
1951cd0c2139SMatthew G Knepley   PetscInt dim;
1952cd0c2139SMatthew G Knepley 
1953cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1954cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19554f572ea9SToby Isaac   PetscAssertPointer(dmSplit, 4);
19569566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
19579566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
19589566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19599566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1960cd0c2139SMatthew G Knepley   switch (dim) {
1961cd0c2139SMatthew G Knepley   case 2:
1962d71ae5a4SJacob Faibussowitsch   case 3:
1963d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1964d71ae5a4SJacob Faibussowitsch     break;
1965d71ae5a4SJacob Faibussowitsch   default:
1966d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1967cd0c2139SMatthew G Knepley   }
1968cd0c2139SMatthew G Knepley   *dmSplit = sdm;
19693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1970cd0c2139SMatthew G Knepley }
1971cd0c2139SMatthew G Knepley 
19720f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
1973d71ae5a4SJacob Faibussowitsch static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1974d71ae5a4SJacob Faibussowitsch {
19750f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
19760f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
19770f66a230SMatthew G. Knepley 
19780f66a230SMatthew G. Knepley   PetscFunctionBegin;
19790f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
19809566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19829566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
19839566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
19840f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
19850f66a230SMatthew G. Knepley     if (cone[c] == face) {
19860f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
19870f66a230SMatthew G. Knepley 
19880f66a230SMatthew G. Knepley       if (subdm) {
19890f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
19900f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
19910f66a230SMatthew G. Knepley 
19929566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
19939566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
19949566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
19959566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
19969566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
19970f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
19980f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
19990f66a230SMatthew G. Knepley             o = subornt[0];
20000f66a230SMatthew G. Knepley             break;
20010f66a230SMatthew G. Knepley           }
20020f66a230SMatthew G. Knepley         }
200363a3b9bcSJacob Faibussowitsch         PetscCheck(sc < subconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %" PetscInt_FMT " (%" PetscInt_FMT ") in cone for subpoint %" PetscInt_FMT " (%" PetscInt_FMT ")", subface, face, subpoint, cell);
20040f66a230SMatthew G. Knepley       }
20050f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
20060f66a230SMatthew G. Knepley       else *pos = PETSC_FALSE;
20070f66a230SMatthew G. Knepley       break;
20080f66a230SMatthew G. Knepley     }
20090f66a230SMatthew G. Knepley   }
201063a3b9bcSJacob Faibussowitsch   PetscCheck(c != coneSize, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " in split face %" PetscInt_FMT " support does not have it in the cone", cell, face);
20113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20120f66a230SMatthew G. Knepley }
20130f66a230SMatthew G. Knepley 
20140542aa8cSMatthew G. Knepley static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label, PetscBool split)
2015d71ae5a4SJacob Faibussowitsch {
2016accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
2017accc9626SMatthew G. Knepley   const PetscInt *points;
20180542aa8cSMatthew G. Knepley   PetscInt       *closure = NULL, *inclosure = NULL;
20190542aa8cSMatthew G. Knepley   PetscInt        dim, numPoints, shift = 100, shift2 = 200, debug = 0;
2020accc9626SMatthew G. Knepley 
2021accc9626SMatthew G. Knepley   PetscFunctionBegin;
2022accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
20230542aa8cSMatthew G. Knepley   // If any faces touching the fault divide cells on either side,
20240542aa8cSMatthew G. Knepley   //   either split them, or unsplit the connection
2025accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
2026accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
2027accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
2028accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
2029accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
20303ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2031accc9626SMatthew G. Knepley   }
2032accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
2033accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
2034accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
2035accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
2036accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
20370542aa8cSMatthew G. Knepley   for (PetscInt p = 0; p < numPoints; ++p) {
2038accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
2039accc9626SMatthew G. Knepley     const PetscInt *support;
2040accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
2041accc9626SMatthew G. Knepley 
2042accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
2043accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
2044accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
2045accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
2046accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
2047accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
2048accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
20490542aa8cSMatthew G. Knepley     // Check that this face is not incident on only unsplit faces,
20500542aa8cSMatthew G. Knepley     //   meaning has at least one split face
2051e1a13daeSMatthew G. Knepley     {
2052e1a13daeSMatthew G. Knepley       PetscBool split = PETSC_FALSE;
20530542aa8cSMatthew G. Knepley       PetscInt  Ncl, val;
2054e1a13daeSMatthew G. Knepley 
20550542aa8cSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &Ncl, &closure));
20560542aa8cSMatthew G. Knepley       for (PetscInt cl = 0; cl < Ncl * 2; cl += 2) {
20570542aa8cSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &val));
20580542aa8cSMatthew G. Knepley         if ((val >= 0) && (val <= dim)) {
20599371c9d4SSatish Balay           split = PETSC_TRUE;
20609371c9d4SSatish Balay           break;
20619371c9d4SSatish Balay         }
2062e1a13daeSMatthew G. Knepley       }
2063e1a13daeSMatthew G. Knepley       if (!split) continue;
2064e1a13daeSMatthew G. Knepley     }
20650542aa8cSMatthew G. Knepley     if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Point %" PetscInt_FMT " is impinging (%" PetscInt_FMT ":%" PetscInt_FMT ", %" PetscInt_FMT ":%" PetscInt_FMT ")\n", point, support[0], valA, support[1], valB));
20660542aa8cSMatthew G. Knepley     if (split) {
20670542aa8cSMatthew G. Knepley       // Split the face
2068accc9626SMatthew G. Knepley       PetscCall(DMLabelGetValue(label, point, &valA));
2069accc9626SMatthew G. Knepley       PetscCall(DMLabelClearValue(label, point, valA));
2070accc9626SMatthew G. Knepley       PetscCall(DMLabelSetValue(label, point, dim - 1));
2071accc9626SMatthew G. Knepley       /* Label its closure:
2072accc9626SMatthew G. Knepley         unmarked: label as unsplit
2073accc9626SMatthew G. Knepley         incident: relabel as split
20740542aa8cSMatthew G. Knepley         split:    do nothing */
2075accc9626SMatthew G. Knepley       {
2076accc9626SMatthew G. Knepley         PetscInt closureSize, cl, dep;
2077accc9626SMatthew G. Knepley 
2078accc9626SMatthew G. Knepley         PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2079accc9626SMatthew G. Knepley         for (cl = 0; cl < closureSize * 2; cl += 2) {
2080accc9626SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, closure[cl], &valA));
2081accc9626SMatthew G. Knepley           if (valA == -1) { /* Mark as unsplit */
2082accc9626SMatthew G. Knepley             PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2083accc9626SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
2084accc9626SMatthew G. Knepley           } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2085accc9626SMatthew G. Knepley             PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2086accc9626SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, closure[cl], valA));
2087accc9626SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, closure[cl], dep));
2088accc9626SMatthew G. Knepley           }
2089accc9626SMatthew G. Knepley         }
20900542aa8cSMatthew G. Knepley       }
20910542aa8cSMatthew G. Knepley     } else {
20920542aa8cSMatthew G. Knepley       // Unsplit the incident faces and their closures
20930542aa8cSMatthew G. Knepley       PetscInt Ncl, dep, val;
20940542aa8cSMatthew G. Knepley 
20950542aa8cSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &Ncl, &closure));
20960542aa8cSMatthew G. Knepley       for (PetscInt cl = 0; cl < Ncl * 2; cl += 2) {
20970542aa8cSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &val));
20980542aa8cSMatthew G. Knepley         if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Point %" PetscInt_FMT ":%" PetscInt_FMT "\n", closure[cl], val));
20990542aa8cSMatthew G. Knepley         if ((val >= 0) && (val <= dim)) {
21000542aa8cSMatthew G. Knepley           PetscInt Nincl, inval, indep;
21010542aa8cSMatthew G. Knepley 
21020542aa8cSMatthew G. Knepley           if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Point %" PetscInt_FMT " is being unsplit\n", closure[cl]));
21030542aa8cSMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
21040542aa8cSMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], val));
21050542aa8cSMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
21060542aa8cSMatthew G. Knepley 
21070542aa8cSMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, closure[cl], PETSC_TRUE, &Nincl, &inclosure));
21080542aa8cSMatthew G. Knepley           for (PetscInt incl = 0; incl < Nincl * 2; incl += 2) {
21090542aa8cSMatthew G. Knepley             PetscCall(DMLabelGetValue(label, inclosure[cl], &inval));
21100542aa8cSMatthew G. Knepley             if ((inval >= 0) && (inval <= dim)) {
21110542aa8cSMatthew G. Knepley               if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "    Point %" PetscInt_FMT " is being unsplit\n", inclosure[incl]));
21120542aa8cSMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, inclosure[incl], &indep));
21130542aa8cSMatthew G. Knepley               PetscCall(DMLabelClearValue(label, inclosure[incl], inval));
21140542aa8cSMatthew G. Knepley               PetscCall(DMLabelSetValue(label, inclosure[incl], shift2 + indep));
2115accc9626SMatthew G. Knepley             }
2116accc9626SMatthew G. Knepley           }
21170542aa8cSMatthew G. Knepley         }
21180542aa8cSMatthew G. Knepley       }
21190542aa8cSMatthew G. Knepley     }
21200542aa8cSMatthew G. Knepley   }
21210542aa8cSMatthew G. Knepley   PetscCall(DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &inclosure));
21220542aa8cSMatthew G. Knepley   PetscCall(DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure));
2123accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2124accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
21253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2126accc9626SMatthew G. Knepley }
2127accc9626SMatthew G. Knepley 
2128cd0c2139SMatthew G Knepley /*@
21290f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
2130cd0c2139SMatthew G Knepley   to complete the surface
2131cd0c2139SMatthew G Knepley 
2132cd0c2139SMatthew G Knepley   Input Parameters:
213320f4b53cSBarry Smith + dm     - The `DM`
213420f4b53cSBarry Smith . label  - A `DMLabel` marking the surface
213520f4b53cSBarry Smith . blabel - A `DMLabel` marking the vertices on the boundary which will not be duplicated, or `NULL` to find them automatically
213620f4b53cSBarry Smith . bvalue - Value of `DMLabel` marking the vertices on the boundary
2137bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
21380542aa8cSMatthew G. Knepley . split  - Split faces impinging on the surface, rather than clamping the surface boundary
213920f4b53cSBarry Smith - subdm  - The `DM` associated with the label, or `NULL`
2140cd0c2139SMatthew G Knepley 
2141cd0c2139SMatthew G Knepley   Output Parameter:
214220f4b53cSBarry Smith . label - A `DMLabel` marking all surface points
21430f66a230SMatthew G. Knepley 
2144cd0c2139SMatthew G Knepley   Level: developer
2145cd0c2139SMatthew G Knepley 
214620f4b53cSBarry Smith   Note:
214720f4b53cSBarry Smith   The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
214820f4b53cSBarry Smith 
21491cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2150cd0c2139SMatthew G Knepley @*/
21510542aa8cSMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, PetscBool split, DM subdm)
2152d71ae5a4SJacob Faibussowitsch {
2153d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2154accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
215547946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2156bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
21570542aa8cSMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = split ? 300 : 0, dim, depth, numPoints, numSubpoints, p, val;
2158cd0c2139SMatthew G Knepley 
2159cd0c2139SMatthew G Knepley   PetscFunctionBegin;
21609566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
21619566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
21629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
216347946fd8SMatthew G. Knepley   if (subdm) {
21649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
216547946fd8SMatthew G. Knepley     if (subpointIS) {
21669566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
21679566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
216847946fd8SMatthew G. Knepley     }
216947946fd8SMatthew G. Knepley   }
2170d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
21719566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2172accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
21739566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
21749566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2175d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2176cd0c2139SMatthew G Knepley     const PetscInt *support;
2177cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2178cd0c2139SMatthew G Knepley 
21799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2180c4419245SMatthew G. Knepley #if 0
2181c4419245SMatthew G. Knepley     if (supportSize != 2) {
2182c4419245SMatthew G. Knepley       const PetscInt *lp;
2183c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2184c4419245SMatthew G. Knepley 
2185c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2186c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
21879566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
21889566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
218963a3b9bcSJacob Faibussowitsch       PetscCheck(pind >= 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %" PetscInt_FMT " has %" PetscInt_FMT " != 2 supports, and the face is not shared with another process", points[p], supportSize);
2190c4419245SMatthew G. Knepley     }
2191c4419245SMatthew G. Knepley #endif
21929566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2193cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
21940f66a230SMatthew G. Knepley       const PetscInt *cone;
2195cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
21960f66a230SMatthew G. Knepley       PetscBool       pos;
2197cd0c2139SMatthew G Knepley 
21989566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
21999566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
22009566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
22010f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
22020f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
22039566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
22049566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2205cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2206cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2207cd0c2139SMatthew G Knepley 
22089566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2209cd0c2139SMatthew G Knepley         if (val == -1) {
2210cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2211cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2212cd0c2139SMatthew G Knepley 
22139566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2214cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2215cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2216a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2217cd0c2139SMatthew G Knepley 
22189566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
22199566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2220a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim - 1) && (bval < 0)) {
22219566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2222cd0c2139SMatthew G Knepley               break;
2223cd0c2139SMatthew G Knepley             }
2224cd0c2139SMatthew G Knepley           }
22259566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2226cd0c2139SMatthew G Knepley         }
2227cd0c2139SMatthew G Knepley       }
2228cd0c2139SMatthew G Knepley     }
2229cd0c2139SMatthew G Knepley   }
2230accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2231accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2232a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
223386200784SMatthew G. Knepley   if (blabel) {
2234accc9626SMatthew G. Knepley     IS bdIS;
2235accc9626SMatthew G. Knepley 
2236caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2237accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2238accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2239a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2240a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2241a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2242a0541d8aSMatthew G. Knepley 
22439566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2244a0541d8aSMatthew G. Knepley       if (bval >= 0) {
22459566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2246f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2247f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
22489566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2249f7019248SMatthew G. Knepley         }
2250f7019248SMatthew G. Knepley       }
2251f7019248SMatthew G. Knepley     }
2252f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2253f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2254f7019248SMatthew G. Knepley       PetscInt       val, bval;
2255f7019248SMatthew G. Knepley 
22569566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2257f7019248SMatthew G. Knepley       if (bval >= 0) {
225886200784SMatthew G. Knepley         const PetscInt *cone, *support;
225986200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
226086200784SMatthew G. Knepley 
2261a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
22629566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2263e1a13daeSMatthew G. Knepley         PetscCheck(val >= 0 && val <= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be part of the fault", point, val);
22649566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
22659566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
22662c06a818SMatthew G. Knepley         /* Check for cross-edge
22672c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
226886200784SMatthew G. Knepley         if (val != 0) continue;
22699566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
22709566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
227186200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
22729566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
22739566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
227463a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
22759566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
22769566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
22779566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
22789566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
227986200784SMatthew G. Knepley         }
2280a0541d8aSMatthew G. Knepley       }
2281a0541d8aSMatthew G. Knepley     }
2282accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2283accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2284a0541d8aSMatthew G. Knepley   }
2285b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2286b6dfa339SMatthew G. Knepley   {
2287b6dfa339SMatthew G. Knepley     PetscSF         sf;
2288b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2289b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2290b6dfa339SMatthew G. Knepley 
2291b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2292b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2293accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2294accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2295accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2296accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2297b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2298b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2299b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2300b6dfa339SMatthew G. Knepley         PetscInt       val;
2301b6dfa339SMatthew G. Knepley 
2302b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2303b6dfa339SMatthew G. Knepley         if (l >= 0) {
2304b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2305b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2306b6dfa339SMatthew G. Knepley 
2307b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2308b6dfa339SMatthew G. Knepley           PetscCheck((val == dim - 1) || (val == shift2 + dim - 1), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be a fault face", point, val);
2309b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2310b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2311b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2312accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2313b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2314b6dfa339SMatthew G. Knepley 
2315b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2316b6dfa339SMatthew G. Knepley             PetscCheck(val != -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " is missing from label, but is in the closure of a fault face", point);
2317b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2318b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2319b6dfa339SMatthew G. Knepley           }
2320b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2321b6dfa339SMatthew G. Knepley         }
2322b6dfa339SMatthew G. Knepley       }
2323b6dfa339SMatthew G. Knepley     }
2324b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2325b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2326accc9626SMatthew G. Knepley   }
2327accc9626SMatthew G. Knepley divide:
2328b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2329accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
23300542aa8cSMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label, split));
23313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2332cd0c2139SMatthew G Knepley }
2333cd0c2139SMatthew G Knepley 
2334720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
233566976f2fSJacob Faibussowitsch static PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2336d71ae5a4SJacob Faibussowitsch {
2337720e594eSMatthew G. Knepley   IS              subpointIS;
2338720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2339720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2340720e594eSMatthew G. Knepley 
2341720e594eSMatthew G. Knepley   PetscFunctionBegin;
23423ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
23439566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
23449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
23453ba16761SJacob Faibussowitsch   if (!subpointIS) PetscFunctionReturn(PETSC_SUCCESS);
23469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
23479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
23489566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2349720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2350720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2351720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2352720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2353720e594eSMatthew G. Knepley 
23549566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2355720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2356720e594eSMatthew G. Knepley       PetscInt value = 0;
2357720e594eSMatthew G. Knepley 
2358720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
23599566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
23609371c9d4SSatish Balay       if (value == defaultValue) {
23619371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
23629371c9d4SSatish Balay         break;
23639371c9d4SSatish Balay       }
2364720e594eSMatthew G. Knepley     }
23659566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2366720e594eSMatthew G. Knepley     if (invalidCell) {
23679566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23689566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
23699566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
237063a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2371720e594eSMatthew G. Knepley     }
2372720e594eSMatthew G. Knepley   }
23739566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2375720e594eSMatthew G. Knepley }
2376720e594eSMatthew G. Knepley 
2377c08575a3SMatthew G. Knepley /*@
23783cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
23793cf72582SMatthew G. Knepley 
238020f4b53cSBarry Smith   Collective
23813cf72582SMatthew G. Knepley 
23823cf72582SMatthew G. Knepley   Input Parameters:
238320f4b53cSBarry Smith + dm      - The original `DM`
2384720e594eSMatthew G. Knepley . label   - The label specifying the interface vertices
2385caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2386caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
23873cf72582SMatthew G. Knepley 
23883cf72582SMatthew G. Knepley   Output Parameters:
238920f4b53cSBarry Smith + hybridLabel - The label fully marking the interface, or `NULL` if no output is desired
239020f4b53cSBarry Smith . splitLabel  - The label containing the split points, or `NULL` if no output is desired
239120f4b53cSBarry Smith . dmInterface - The new interface `DM`, or `NULL`
239220f4b53cSBarry Smith - dmHybrid    - The new `DM` with cohesive cells
23933cf72582SMatthew G. Knepley 
239420f4b53cSBarry Smith   Level: developer
239520f4b53cSBarry Smith 
239620f4b53cSBarry Smith   Note:
239720f4b53cSBarry Smith   The hybridLabel indicates what parts of the original mesh impinged on the division surface. For points
23986eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
23996eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
24006eccb800SMatthew Knepley   one vertex 3 on the surface would be 6 (101) and 3 (0) in hybridLabel. If an edge 9 from the negative side of the
24016eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
24026eccb800SMatthew Knepley 
24036eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
240440cdb708SMatthew Knepley   mesh. The label value is $\pm 100+dim$ for each point. For example, if two edges 10 and 14 in the hybrid resulting from
24056eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
24066eccb800SMatthew Knepley 
240720f4b53cSBarry Smith   The dmInterface is a `DM` built from the original division surface. It has a label which can be retrieved using
240820f4b53cSBarry Smith   `DMPlexGetSubpointMap()` which maps each point back to the point in the surface of the original mesh.
24096eccb800SMatthew Knepley 
24101cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
24113cf72582SMatthew G. Knepley @*/
2412d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
2413d71ae5a4SJacob Faibussowitsch {
24143cf72582SMatthew G. Knepley   DM       idm;
24157db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
24163cf72582SMatthew G. Knepley   PetscInt dim;
24173cf72582SMatthew G. Knepley 
24183cf72582SMatthew G. Knepley   PetscFunctionBegin;
24193cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24204f572ea9SToby Isaac   if (label) PetscAssertPointer(label, 2);
24214f572ea9SToby Isaac   if (bdlabel) PetscAssertPointer(bdlabel, 3);
24224f572ea9SToby Isaac   if (hybridLabel) PetscAssertPointer(hybridLabel, 5);
24234f572ea9SToby Isaac   if (splitLabel) PetscAssertPointer(splitLabel, 6);
24244f572ea9SToby Isaac   if (dmInterface) PetscAssertPointer(dmInterface, 7);
24254f572ea9SToby Isaac   PetscAssertPointer(dmHybrid, 8);
24269566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24279566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
24289566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
24299566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
24309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
24319566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
24329566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
24337db7e0a7SMatthew G. Knepley   if (splitLabel) {
24347db7e0a7SMatthew G. Knepley     const char *name;
24357db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
24367db7e0a7SMatthew G. Knepley 
24379566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
2438c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(sname, name, sizeof(sname)));
2439c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(sname, " split", sizeof(sname)));
24409566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
24417db7e0a7SMatthew G. Knepley   }
24420542aa8cSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, PETSC_TRUE, idm));
24439371c9d4SSatish Balay   if (dmInterface) {
24449371c9d4SSatish Balay     *dmInterface = idm;
24459371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
24469566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
24475de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
24483cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
24499566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
24507db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
24514a7ee7d0SMatthew G. Knepley   {
24524a7ee7d0SMatthew G. Knepley     DM      cdm;
24534a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
24544a7ee7d0SMatthew G. Knepley 
24554a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
24569566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
24579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
24589566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
24594a7ee7d0SMatthew G. Knepley   }
24603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2461cd0c2139SMatthew G Knepley }
2462cd0c2139SMatthew G Knepley 
2463efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2464efa14ee0SMatthew G Knepley 
2465efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2466efa14ee0SMatthew G Knepley */
2467d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2468d71ae5a4SJacob Faibussowitsch {
2469fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2470efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2471412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2472efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2473efa14ee0SMatthew G Knepley 
2474efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2475efa14ee0SMatthew G Knepley   *numFaces = 0;
2476efa14ee0SMatthew G Knepley   *nFV      = 0;
24779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24789566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
247977d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
24809566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
248148a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2482efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
24839566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2484efa14ee0SMatthew G Knepley   if (subvertexIS) {
24859566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24869566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2487efa14ee0SMatthew G Knepley   }
2488efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2489efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24900298fd71SBarry Smith     PetscInt      *star   = NULL;
2491efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2492efa14ee0SMatthew G Knepley 
24939566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2494efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2495efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2496efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2497efa14ee0SMatthew G Knepley     }
2498efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2499efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
25000298fd71SBarry Smith       PetscInt      *closure = NULL;
2501efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2502efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2503efa14ee0SMatthew G Knepley 
25049566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
250565560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
250663a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
25079566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2508efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2509efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2510efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2511efa14ee0SMatthew G Knepley 
2512efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2513efa14ee0SMatthew G Knepley           ++numCorners;
25149566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2515830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2516efa14ee0SMatthew G Knepley         }
2517efa14ee0SMatthew G Knepley       }
25184ad8454bSPierre Jolivet       if (!*nFV) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
251963a3b9bcSJacob Faibussowitsch       PetscCheck(faceSize <= *nFV, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
2520efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2521007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2522007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2523007baee2SMatthew G. Knepley 
2524efa14ee0SMatthew G Knepley         ++(*numFaces);
252548a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
25269566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
252748a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
25289566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2529efa14ee0SMatthew G Knepley       }
25309566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2531efa14ee0SMatthew G Knepley     }
25329566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2533efa14ee0SMatthew G Knepley   }
253448a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25359566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25369566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2538efa14ee0SMatthew G Knepley }
2539efa14ee0SMatthew G Knepley 
2540d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2541d71ae5a4SJacob Faibussowitsch {
254234b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2543efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2544412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2545efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2546efa14ee0SMatthew G Knepley 
2547efa14ee0SMatthew G Knepley   PetscFunctionBegin;
25489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25499566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
255048a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2551efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
255234b4c39eSMatthew G. Knepley   if (vertexLabel) {
25539566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2554efa14ee0SMatthew G Knepley     if (subvertexIS) {
25559566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
25569566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2557efa14ee0SMatthew G Knepley     }
255834b4c39eSMatthew G. Knepley   }
2559efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2560efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
25610298fd71SBarry Smith     PetscInt      *star   = NULL;
2562efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2563efa14ee0SMatthew G Knepley 
25649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2565efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2566efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2567158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2568158acfadSMatthew G. Knepley 
2569158acfadSMatthew G. Knepley       if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) {
2570158acfadSMatthew G. Knepley         if (markedFaces) {
25719566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2572158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2573158acfadSMatthew G. Knepley         }
2574158acfadSMatthew G. Knepley         star[numFaces++] = point;
2575158acfadSMatthew G. Knepley       }
2576efa14ee0SMatthew G Knepley     }
2577efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2578efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
25790298fd71SBarry Smith       PetscInt      *closure = NULL;
2580efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2581efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2582efa14ee0SMatthew G Knepley 
25839566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2584efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
258563a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
25869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2587efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2588efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2589efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2590efa14ee0SMatthew G Knepley 
2591efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
25929566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2593830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2594efa14ee0SMatthew G Knepley         }
2595efa14ee0SMatthew G Knepley       }
2596efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2597efa14ee0SMatthew G Knepley         const PetscInt *support;
2598efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2599efa14ee0SMatthew G Knepley 
2600efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2601efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2602efa14ee0SMatthew G Knepley 
2603efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2604efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
26059566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2606efa14ee0SMatthew G Knepley               break;
2607efa14ee0SMatthew G Knepley             }
2608efa14ee0SMatthew G Knepley           }
2609efa14ee0SMatthew G Knepley         }
26109566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
26119566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
261248a46eb9SPierre Jolivet         for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2613efa14ee0SMatthew G Knepley       }
26149566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2615efa14ee0SMatthew G Knepley     }
26169566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2617efa14ee0SMatthew G Knepley   }
26189566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
26199566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
26209566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
26213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2622efa14ee0SMatthew G Knepley }
2623efa14ee0SMatthew G Knepley 
2624d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2625d71ae5a4SJacob Faibussowitsch {
262627c04023SMatthew G. Knepley   DMLabel         label = NULL;
2627766ab985SMatthew G. Knepley   const PetscInt *cone;
26289fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2629766ab985SMatthew G. Knepley 
2630812bfc34SJed Brown   PetscFunctionBegin;
2631c0ed958bSJed Brown   *numFaces = 0;
2632c0ed958bSJed Brown   *nFV      = 0;
26339566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2634fed694aaSMatthew G. Knepley   *subCells = NULL;
26359566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26369566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26373ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
263827c04023SMatthew G. Knepley   if (label) {
263927c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
264027c04023SMatthew G. Knepley       PetscInt val;
264127c04023SMatthew G. Knepley 
26429566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
264327c04023SMatthew G. Knepley       if (val == value) {
264427c04023SMatthew G. Knepley         ++(*numFaces);
26459566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
264627c04023SMatthew G. Knepley       }
264727c04023SMatthew G. Knepley     }
264827c04023SMatthew G. Knepley   } else {
2649766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
26509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
265127c04023SMatthew G. Knepley   }
26529566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
26534ad8454bSPierre Jolivet   if (!*numFaces) PetscFunctionReturn(PETSC_SUCCESS);
26549fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2655766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2656766ab985SMatthew G. Knepley     const PetscInt *cells;
2657766ab985SMatthew G. Knepley     PetscInt        numCells;
2658766ab985SMatthew G. Knepley 
265927c04023SMatthew G. Knepley     if (label) {
266027c04023SMatthew G. Knepley       PetscInt val;
266127c04023SMatthew G. Knepley 
26629566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
266327c04023SMatthew G. Knepley       if (val != value) continue;
266427c04023SMatthew G. Knepley     }
26659566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
266648a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2667766ab985SMatthew G. Knepley     /* Negative face */
26689566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
266927234c99SMatthew G. Knepley     /* Not true in parallel
267008401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2671766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
26729566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
267327234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2674766ab985SMatthew G. Knepley     }
26759566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2676766ab985SMatthew G. Knepley     /* Positive face is not included */
2677766ab985SMatthew G. Knepley   }
26783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2679766ab985SMatthew G. Knepley }
2680766ab985SMatthew G. Knepley 
2681d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2682d71ae5a4SJacob Faibussowitsch {
2683766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2684766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2685766ab985SMatthew G. Knepley 
2686812bfc34SJed Brown   PetscFunctionBegin;
26879566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26889566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26893ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
26909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
26919566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2692766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2693766ab985SMatthew G. Knepley     const PetscInt *cone;
2694766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2695b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2696766ab985SMatthew G. Knepley 
269727c04023SMatthew G. Knepley     if (label) {
26989566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
269927c04023SMatthew G. Knepley       if (val != value) continue;
270027c04023SMatthew G. Knepley     }
27019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
27029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
27039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
27041dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2705b3154360SMatthew G. Knepley     /* Negative face */
27069566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2707766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2708766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2709766ab985SMatthew G. Knepley 
2710766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2711766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
27129566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2713766ab985SMatthew G. Knepley           break;
2714766ab985SMatthew G. Knepley         }
2715766ab985SMatthew G. Knepley       }
2716766ab985SMatthew G. Knepley     }
27179566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2718766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2719766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2720766ab985SMatthew G. Knepley       const PetscInt *support;
2721766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2722766ab985SMatthew G. Knepley 
27239566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
272408401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
27259566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
272648a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2727766ab985SMatthew G. Knepley     }
2728766ab985SMatthew G. Knepley   }
27299566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
27303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2731766ab985SMatthew G. Knepley }
2732766ab985SMatthew G. Knepley 
2733d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2734d71ae5a4SJacob Faibussowitsch {
273582f516ccSBarry Smith   MPI_Comm       comm;
2736e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2737e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2738e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2739e6ccafaeSMatthew G Knepley 
274082f516ccSBarry Smith   PetscFunctionBegin;
27419566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
27429566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
274363a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2744e6ccafaeSMatthew G Knepley 
2745ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2746ddeab2a6SMatthew G. Knepley     /* Triangle */
2747e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2748e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2749ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2750ddeab2a6SMatthew G. Knepley     /* Triangle */
2751ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2752ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2753ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2754ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2755ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2756ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2757e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2758e6ccafaeSMatthew G Knepley     /* Quadratic line */
2759e6ccafaeSMatthew G Knepley     faceSize  = 1;
2760e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2761e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2762e6ccafaeSMatthew G Knepley     /* Quads */
2763e6ccafaeSMatthew G Knepley     faceSize = 2;
2764e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2765e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2766e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2767e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2768e6ccafaeSMatthew G Knepley     } else {
2769e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2770e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2771e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2772e6ccafaeSMatthew G Knepley     }
2773e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2774e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2775e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2776e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2777e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2778e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2779e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2780e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2781e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2782e6ccafaeSMatthew G Knepley       2, 3, 5, /* left */
2783e6ccafaeSMatthew G Knepley     };
2784e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2785e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2786e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2787e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2788e6ccafaeSMatthew G Knepley     };
2789e6ccafaeSMatthew G Knepley 
2790e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
27919566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2792e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2793e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2794e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2795e6ccafaeSMatthew G Knepley 
27969371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) {
2797e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2798e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2799e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2800e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2801e6ccafaeSMatthew G Knepley               break;
2802e6ccafaeSMatthew G Knepley             }
2803e6ccafaeSMatthew G Knepley           }
2804e6ccafaeSMatthew G Knepley         }
2805e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2806e6ccafaeSMatthew G Knepley         break;
2807e6ccafaeSMatthew G Knepley       }
2808e6ccafaeSMatthew G Knepley     }
280928b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2810e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28113ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2812e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2813e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2814e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2815e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2816e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2817e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2818e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2819e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2820e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2821e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2822e6ccafaeSMatthew G Knepley       0, 3, 7, /* left */
2823e6ccafaeSMatthew G Knepley     };
2824e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2825e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2826e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2827e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2828e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2829e6ccafaeSMatthew G Knepley     };
2830e6ccafaeSMatthew G Knepley 
2831e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
28329566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2833e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2834e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2835e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2836e6ccafaeSMatthew G Knepley 
28379371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) {
2838e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2839e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2840e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2841e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2842e6ccafaeSMatthew G Knepley               break;
2843e6ccafaeSMatthew G Knepley             }
2844e6ccafaeSMatthew G Knepley           }
2845e6ccafaeSMatthew G Knepley         }
2846e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2847e6ccafaeSMatthew G Knepley         break;
2848e6ccafaeSMatthew G Knepley       }
2849e6ccafaeSMatthew G Knepley     }
285028b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2851e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28523ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2853e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2854e6ccafaeSMatthew G Knepley     /* Hexes
2855e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2856e6ccafaeSMatthew G Knepley        pointing up at the second.
2857e6ccafaeSMatthew G Knepley 
2858e6ccafaeSMatthew G Knepley           7---6
2859e6ccafaeSMatthew G Knepley          /|  /|
2860e6ccafaeSMatthew G Knepley         4---5 |
2861ddeab2a6SMatthew G. Knepley         | 1-|-2
2862e6ccafaeSMatthew G Knepley         |/  |/
2863ddeab2a6SMatthew G. Knepley         0---3
2864e6ccafaeSMatthew G Knepley 
2865e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2866e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2867e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2868e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2869e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2870e6ccafaeSMatthew G Knepley       0, 1, 2, 3, /* bottom */
2871e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2872ddeab2a6SMatthew G. Knepley       0, 3, 4, 5, /* front */
2873ddeab2a6SMatthew G. Knepley       2, 3, 5, 6, /* right */
2874ddeab2a6SMatthew G. Knepley       1, 2, 6, 7, /* back */
2875ddeab2a6SMatthew G. Knepley       0, 1, 4, 7, /* left */
2876e6ccafaeSMatthew G Knepley     };
2877e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2878ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2879e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2880ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2881ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2882ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2883ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2884e6ccafaeSMatthew G Knepley     };
2885e6ccafaeSMatthew G Knepley 
2886e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
28879566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2888e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2889e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2890e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2891e6ccafaeSMatthew G Knepley 
28929371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) {
2893e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2894e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2895e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2896e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2897e6ccafaeSMatthew G Knepley               break;
2898e6ccafaeSMatthew G Knepley             }
2899e6ccafaeSMatthew G Knepley           }
2900e6ccafaeSMatthew G Knepley         }
2901e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2902e6ccafaeSMatthew G Knepley         break;
2903e6ccafaeSMatthew G Knepley       }
2904e6ccafaeSMatthew G Knepley     }
290528b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2906e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29073ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2908e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2909e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2910e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2911e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2912e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2913e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2914e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2915e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2916e6ccafaeSMatthew G Knepley       0, 3, 4, 6, 7, 9, /* front */
2917e6ccafaeSMatthew G Knepley       1, 4, 5, 7, 8, 9, /* right */
2918e6ccafaeSMatthew G Knepley       2, 3, 5, 6, 8, 9, /* left */
2919e6ccafaeSMatthew G Knepley     };
2920e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2921e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2922e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2923e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2924e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2925e6ccafaeSMatthew G Knepley     };
2926e6ccafaeSMatthew G Knepley 
2927e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
29289566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2929e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2930e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2931e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2932e6ccafaeSMatthew G Knepley 
29339371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) {
2934e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2935e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2936e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2937e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2938e6ccafaeSMatthew G Knepley               break;
2939e6ccafaeSMatthew G Knepley             }
2940e6ccafaeSMatthew G Knepley           }
2941e6ccafaeSMatthew G Knepley         }
2942e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2943e6ccafaeSMatthew G Knepley         break;
2944e6ccafaeSMatthew G Knepley       }
2945e6ccafaeSMatthew G Knepley     }
294628b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2947e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29483ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2949e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2950e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2951e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2952e6ccafaeSMatthew G Knepley        pointing up at the second.
2953e6ccafaeSMatthew G Knepley 
2954e6ccafaeSMatthew G Knepley          7---6
2955e6ccafaeSMatthew G Knepley         /|  /|
2956e6ccafaeSMatthew G Knepley        4---5 |
2957e6ccafaeSMatthew G Knepley        | 3-|-2
2958e6ccafaeSMatthew G Knepley        |/  |/
2959e6ccafaeSMatthew G Knepley        0---1
2960e6ccafaeSMatthew G Knepley 
2961e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2962e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2963e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2964e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2965e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2966e6ccafaeSMatthew G Knepley       0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2967e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2968e6ccafaeSMatthew G Knepley       0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2969e6ccafaeSMatthew G Knepley       1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2970e6ccafaeSMatthew G Knepley       2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2971e6ccafaeSMatthew G Knepley       0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2972e6ccafaeSMatthew G Knepley     };
2973e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2974e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2975e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2976e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2977e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2978e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2979e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2980e6ccafaeSMatthew G Knepley     };
2981e6ccafaeSMatthew G Knepley 
2982e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
29839566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2984e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2985e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2986e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2987e6ccafaeSMatthew G Knepley 
29889371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) {
2989e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2990e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2991e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2992e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2993e6ccafaeSMatthew G Knepley               break;
2994e6ccafaeSMatthew G Knepley             }
2995e6ccafaeSMatthew G Knepley           }
2996e6ccafaeSMatthew G Knepley         }
2997e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2998e6ccafaeSMatthew G Knepley         break;
2999e6ccafaeSMatthew G Knepley       }
3000e6ccafaeSMatthew G Knepley     }
300128b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
3002e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
30033ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3004e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
3005e6ccafaeSMatthew G Knepley   if (!posOrient) {
30069566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
3007e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
3008e6ccafaeSMatthew G Knepley   } else {
30099566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
3010e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
3011e6ccafaeSMatthew G Knepley   }
3012e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
30133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3014e6ccafaeSMatthew G Knepley }
3015e6ccafaeSMatthew G Knepley 
3016c08575a3SMatthew G. Knepley /*@
3017c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
3018c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
3019c08575a3SMatthew G. Knepley 
302020f4b53cSBarry Smith   Not Collective
3021c08575a3SMatthew G. Knepley 
3022c08575a3SMatthew G. Knepley   Input Parameters:
3023c08575a3SMatthew G. Knepley + dm           - The original mesh
3024c08575a3SMatthew G. Knepley . cell         - The cell mesh point
3025c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
3026c08575a3SMatthew G. Knepley . face         - The face vertices
3027c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
3028c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
3029c08575a3SMatthew G. Knepley - origVertices - Original face vertices
3030c08575a3SMatthew G. Knepley 
3031d8d19677SJose E. Roman   Output Parameters:
3032c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
303320f4b53cSBarry Smith - posOriented  - `PETSC_TRUE` if the face was oriented with outward normal
3034c08575a3SMatthew G. Knepley 
3035c08575a3SMatthew G. Knepley   Level: developer
3036c08575a3SMatthew G. Knepley 
30371cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
3038c08575a3SMatthew G. Knepley @*/
3039d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
3040d71ae5a4SJacob Faibussowitsch {
30410298fd71SBarry Smith   const PetscInt *cone = NULL;
3042e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
3043e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
3044e6ccafaeSMatthew G Knepley 
3045e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
30479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
3048e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
3049e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
3050e6ccafaeSMatthew G Knepley 
3051e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
3052e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
30539371c9d4SSatish Balay         found = PETSC_TRUE;
30549371c9d4SSatish Balay         break;
3055e6ccafaeSMatthew G Knepley       }
3056e6ccafaeSMatthew G Knepley     }
3057e6ccafaeSMatthew G Knepley     if (found) {
3058e6ccafaeSMatthew G Knepley       indices[v2]      = v;
3059e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
3060e6ccafaeSMatthew G Knepley       ++v2;
3061e6ccafaeSMatthew G Knepley     } else {
3062e6ccafaeSMatthew G Knepley       oppositeVertex = v;
3063e6ccafaeSMatthew G Knepley     }
3064e6ccafaeSMatthew G Knepley   }
30659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
30663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3067e6ccafaeSMatthew G Knepley }
3068e6ccafaeSMatthew G Knepley 
3069e6ccafaeSMatthew G Knepley /*
3070cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
3071e6ccafaeSMatthew G Knepley 
307220f4b53cSBarry Smith   Not Collective
3073e6ccafaeSMatthew G Knepley 
3074e6ccafaeSMatthew G Knepley   Input Parameters:
307520f4b53cSBarry Smith   + dm              - The `DMPLEX`
3076e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
307720f4b53cSBarry Smith   . faceVertices    - The vertices in the face for `dm`
3078e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
307920f4b53cSBarry Smith   . numCorners      - The number of vertices in the `cell`
308020f4b53cSBarry Smith   . cell            - A cell in `dm` containing the face
3081e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
3082e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
3083e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
3084e6ccafaeSMatthew G Knepley 
30852fe279fdSBarry Smith   Output Parameter:
3086e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
3087e6ccafaeSMatthew G Knepley 
3088e6ccafaeSMatthew G Knepley   Level: developer
3089e6ccafaeSMatthew G Knepley */
3090d71ae5a4SJacob 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)
3091d71ae5a4SJacob Faibussowitsch {
309282f516ccSBarry Smith   MPI_Comm        comm;
3093e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
3094e6ccafaeSMatthew G Knepley   const PetscInt *faces;
3095e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
3096e6ccafaeSMatthew G Knepley 
3097e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30989566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
30999566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
310063a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
3101e6ccafaeSMatthew G Knepley #if 0
3102e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
31039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3104e6ccafaeSMatthew G Knepley #else
3105e6ccafaeSMatthew G Knepley   {
3106e6ccafaeSMatthew G Knepley     PetscInt f;
3107e6ccafaeSMatthew G Knepley 
3108e6ccafaeSMatthew G Knepley     numFaces = 0;
31099566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3110e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
3111e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
3112e6ccafaeSMatthew G Knepley 
31139566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
31149566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
3115e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
3116e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
3117e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
3118e6ccafaeSMatthew G Knepley         PetscInt       v;
3119e6ccafaeSMatthew G Knepley 
3120e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
3121e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
3122e6ccafaeSMatthew G Knepley         }
3123e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
3124e6ccafaeSMatthew G Knepley       }
3125e6ccafaeSMatthew G Knepley       if (d == dof) {
3126e6ccafaeSMatthew G Knepley         numFaces               = 1;
3127e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
3128e6ccafaeSMatthew G Knepley       }
3129e6ccafaeSMatthew G Knepley     }
3130e6ccafaeSMatthew G Knepley   }
3131e6ccafaeSMatthew G Knepley #endif
313263a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3133f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3134e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
31359566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3136e6ccafaeSMatthew G Knepley   } else {
3137e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3138e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3139e6ccafaeSMatthew G Knepley 
31409566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3141e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3142e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
3143e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
31449566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3145e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3146e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3147e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3148e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3149e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3150e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3151e6ccafaeSMatthew G Knepley           break;
3152e6ccafaeSMatthew G Knepley         }
3153e6ccafaeSMatthew G Knepley       }
315463a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3155e6ccafaeSMatthew G Knepley     }
31569566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
31579566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
31589566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3159e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3160e6ccafaeSMatthew G Knepley   }
3161ef07cca7SMatthew G. Knepley #if 0
31629566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3163ef07cca7SMatthew G. Knepley #else
31649566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3165ef07cca7SMatthew G. Knepley #endif
31663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3167e6ccafaeSMatthew G Knepley }
3168e6ccafaeSMatthew G Knepley 
3169d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3170d71ae5a4SJacob Faibussowitsch {
317182f516ccSBarry Smith   MPI_Comm        comm;
317253156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3173efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
3174efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3175efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3176fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3177efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3178e6ccafaeSMatthew G Knepley 
3179e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
31809566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3181efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
31829566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
31839566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
31849566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3185efa14ee0SMatthew G Knepley   /* Setup chart */
31869566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
31879566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
31889566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
31899566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3190e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3191e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3192efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
3193e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
31949566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
31959566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
31969566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
31979566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
319848a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
319948a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
32009566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3201d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3202e6ccafaeSMatthew G Knepley   /* Create face cones */
32039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
32049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
32059566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3206e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3207e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3208efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
32090298fd71SBarry Smith     PetscInt      *closure = NULL;
3210efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3211e6ccafaeSMatthew G Knepley 
32129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3213efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3214efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3215e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3216e6ccafaeSMatthew G Knepley 
3217efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3218efa14ee0SMatthew G Knepley         ++numCorners;
32199566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3220efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3221efa14ee0SMatthew G Knepley           closure[faceSize] = point;
322265560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3223e6ccafaeSMatthew G Knepley           ++faceSize;
3224e6ccafaeSMatthew G Knepley         }
3225e6ccafaeSMatthew G Knepley       }
3226e6ccafaeSMatthew G Knepley     }
322763a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
322848a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
32299566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3230e6ccafaeSMatthew G Knepley   }
32319566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
32329566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
32339566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3234e6ccafaeSMatthew G Knepley   /* Build coordinates */
3235efa14ee0SMatthew G Knepley   {
3236efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3237efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3238efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3239285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
324024640c55SToby Isaac     const char  *name;
3241efa14ee0SMatthew G Knepley 
32429566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
32439566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
32449566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
32459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
32469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
32479566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
32489566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3249efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3250efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3251efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3252efa14ee0SMatthew G Knepley       PetscInt       dof;
3253efa14ee0SMatthew G Knepley 
32549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32559566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
32569566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3257e6ccafaeSMatthew G Knepley     }
32589566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
32599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
32609566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
32619566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
32629566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
32639566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
32649566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3265830e53efSMatthew G. Knepley     if (coordSize) {
32669566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
32679566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3268efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3269efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3270efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3271efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3272e6ccafaeSMatthew G Knepley 
32739566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
32759566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
32769566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
327763a3b9bcSJacob 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);
3278e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3279e6ccafaeSMatthew G Knepley       }
32809566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
32819566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
32823b399e24SMatthew G. Knepley     }
32839566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
32849566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3285e6ccafaeSMatthew G Knepley   }
3286efa14ee0SMatthew G Knepley   /* Cleanup */
32879566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
32889566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
32899566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
32909566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
32913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3292e6ccafaeSMatthew G Knepley }
3293e6ccafaeSMatthew G Knepley 
3294d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3295d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
3296d71ae5a4SJacob Faibussowitsch {
32973982b651SMatthew G. Knepley   PetscInt       subPoint;
32983982b651SMatthew G. Knepley   PetscErrorCode ierr;
32993982b651SMatthew G. Knepley 
33009371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
33019371c9d4SSatish Balay   if (ierr) return -1;
33023982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
33033982b651SMatthew G. Knepley }
33043982b651SMatthew G. Knepley 
3305d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3306d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[])
3307d71ae5a4SJacob Faibussowitsch {
3308dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3309dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3310dd05d810SMatthew G. Knepley 
3311dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3312dd05d810SMatthew G. Knepley   if (ierr) return -1;
3313dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3314dd05d810SMatthew G. Knepley }
3315dd05d810SMatthew G. Knepley 
3316d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3317d71ae5a4SJacob Faibussowitsch {
3318695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3319212103e5SMatthew Knepley   PetscInt Nl, l, d;
3320212103e5SMatthew Knepley 
3321212103e5SMatthew Knepley   PetscFunctionBegin;
3322695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3323695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3324695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
33259566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3326212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3327212103e5SMatthew Knepley     DMLabel         label, newlabel;
3328212103e5SMatthew Knepley     const char     *lname;
3329d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3330212103e5SMatthew Knepley     IS              valueIS;
3331212103e5SMatthew Knepley     const PetscInt *values;
3332212103e5SMatthew Knepley     PetscInt        Nv, v;
3333212103e5SMatthew Knepley 
33349566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
33359566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
33369566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
33379566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
33389566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3339d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
33409566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
33419566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
33429566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
33439566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
33449566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
33459566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
33469566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
33479566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3348212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3349212103e5SMatthew Knepley       IS              pointIS;
3350212103e5SMatthew Knepley       const PetscInt *points;
3351212103e5SMatthew Knepley       PetscInt        Np, p;
3352212103e5SMatthew Knepley 
33539566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
33549566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
33559566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3356212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3357212103e5SMatthew Knepley         const PetscInt point = points[p];
3358212103e5SMatthew Knepley         PetscInt       subp;
3359212103e5SMatthew Knepley 
33609566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3361212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
33629566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3363212103e5SMatthew Knepley       }
33649566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
33659566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3366212103e5SMatthew Knepley     }
33679566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
33689566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3369212103e5SMatthew Knepley   }
33703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3371212103e5SMatthew Knepley }
3372212103e5SMatthew Knepley 
337330cbcd5dSksagiyam static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, PetscBool ignoreLabelHalo, PetscBool sanitizeSubmesh, PetscSF *ownershipTransferSF, DM subdm)
3374d71ae5a4SJacob Faibussowitsch {
337582f516ccSBarry Smith   MPI_Comm         comm;
337653156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3377efa14ee0SMatthew G Knepley   IS              *subpointIS;
3378efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
33793982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3380dd05d810SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, v;
33810d366550SMatthew G. Knepley   PetscMPIInt      rank;
3382e6ccafaeSMatthew G Knepley 
3383e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
33849566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
33859566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3386efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
33879566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
33889566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3389bec263e5SMatthew G. Knepley   if (cellHeight) {
33909566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
33919566063dSJacob Faibussowitsch     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3392bec263e5SMatthew G. Knepley   } else {
3393bec263e5SMatthew G. Knepley     DMLabel         depth;
3394bec263e5SMatthew G. Knepley     IS              pointIS;
3395bec263e5SMatthew G. Knepley     const PetscInt *points;
33964d00ad26Sksagiyam     PetscInt        numPoints = 0, pStart = 0;
33974d00ad26Sksagiyam     PetscBool      *isGhost = NULL;
3398bec263e5SMatthew G. Knepley 
33999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
34009566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3401b85c8bf9SMatthew G. Knepley     if (pointIS) {
34029566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
34039566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3404b85c8bf9SMatthew G. Knepley     }
34054d00ad26Sksagiyam     if (ignoreLabelHalo) {
34064d00ad26Sksagiyam       PetscSF         pointSF;
34074d00ad26Sksagiyam       PetscInt        nleaves, pEnd;
34084d00ad26Sksagiyam       const PetscInt *ilocal = NULL;
34094d00ad26Sksagiyam 
34104d00ad26Sksagiyam       PetscCall(DMGetPointSF(dm, &pointSF));
34114d00ad26Sksagiyam       PetscCall(PetscSFGetGraph(pointSF, NULL, &nleaves, &ilocal, NULL));
34124d00ad26Sksagiyam       PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
34134d00ad26Sksagiyam       PetscCall(PetscMalloc1(pEnd - pStart, &isGhost));
34144d00ad26Sksagiyam       for (p = 0; p < pEnd - pStart; ++p) isGhost[p] = PETSC_FALSE;
34154d00ad26Sksagiyam       for (p = 0; p < nleaves; ++p) isGhost[(ilocal ? ilocal[p] : p) - pStart] = PETSC_TRUE;
34164d00ad26Sksagiyam     }
3417bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3418bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3419bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3420bec263e5SMatthew G. Knepley 
34214d00ad26Sksagiyam       if (ignoreLabelHalo && isGhost[points[p] - pStart]) continue;
34229566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3423bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
34249566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
34259566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3426bec263e5SMatthew G. Knepley       }
34279566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3428bec263e5SMatthew G. Knepley     }
34294d00ad26Sksagiyam     PetscCall(PetscFree(isGhost));
34309566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
34319566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3432bec263e5SMatthew G. Knepley   }
3433efa14ee0SMatthew G Knepley   /* Setup chart */
34349566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3435dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
34369566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3437e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
34389566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3439e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3440e6ccafaeSMatthew G Knepley   }
3441dd05d810SMatthew G. Knepley   // Determine submesh dimension
3442dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3443dd05d810SMatthew G. Knepley   if (sdim > 0) {
3444dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3445dd05d810SMatthew G. Knepley     sdim = dim;
3446dd05d810SMatthew G. Knepley   } else {
3447dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3448dd05d810SMatthew G. Knepley     PetscInt lsdim;
3449dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3450dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3451a5a714f4SStefano Zampini     PetscCall(MPIU_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPI_MAX, comm));
3452dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3453dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3454dd05d810SMatthew G. Knepley   }
34559566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
34569566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3457e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3458dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3459dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3460dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3461dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3462dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
34639566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
34649566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3465e6ccafaeSMatthew G Knepley   }
3466412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
34679566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3468dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3469e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3470e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3471e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3472e6ccafaeSMatthew G Knepley       const PetscInt *cone;
347315100a53SVaclav Hapla       PetscInt        coneSize;
3474e6ccafaeSMatthew G Knepley 
34759566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3476dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
347715100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
347815100a53SVaclav Hapla 
34799566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3480e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34819566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3482e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3483e6ccafaeSMatthew G Knepley         }
34849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
34859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
348615100a53SVaclav Hapla       } else {
348715100a53SVaclav Hapla         DMPolytopeType ct;
348815100a53SVaclav Hapla 
348915100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
349015100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
349115100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3492e6ccafaeSMatthew G Knepley       }
3493e6ccafaeSMatthew G Knepley     }
3494e6ccafaeSMatthew G Knepley   }
34959566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
34969566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3497e6ccafaeSMatthew G Knepley   /* Set cones */
34989566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
34999566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3500dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3501e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3502e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3503e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
35040e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
35050d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3506e6ccafaeSMatthew G Knepley 
3507dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
35080d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
35090d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
35100d366550SMatthew G. Knepley 
35119566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
35129566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
35130d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3514064cae4fSPierre Jolivet           PetscBool isHybrid = PETSC_FALSE;
3515412e9a14SMatthew G. Knepley 
35169566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3517412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
35189566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
35190d366550SMatthew G. Knepley           if (subc >= 0) {
35200d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
35210d366550SMatthew G. Knepley 
35229566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
35239566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
35249566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
35250d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
35260d366550SMatthew G. Knepley               if (cone[c] == point) {
35270d366550SMatthew G. Knepley                 fornt = ornt[c];
35280d366550SMatthew G. Knepley                 break;
35290d366550SMatthew G. Knepley               }
35300d366550SMatthew G. Knepley             }
35310d366550SMatthew G. Knepley             break;
35320d366550SMatthew G. Knepley           }
35330d366550SMatthew G. Knepley         }
35340d366550SMatthew G. Knepley       }
35359566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
35369566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
35379566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
35389566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3539e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
35409566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
354101a2673eSMatthew G. Knepley         if (subc >= 0) {
354201a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
35433982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
354401a2673eSMatthew G. Knepley           ++coneSizeNew;
354501a2673eSMatthew G. Knepley         }
3546e6ccafaeSMatthew G Knepley       }
354763a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
35489566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
35499566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
35509566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3551e6ccafaeSMatthew G Knepley     }
3552e6ccafaeSMatthew G Knepley   }
35539566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
35549566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
35559566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3556e6ccafaeSMatthew G Knepley   /* Build coordinates */
3557e6ccafaeSMatthew G Knepley   {
3558e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3559e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3560e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3561c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
356224640c55SToby Isaac     const char  *name;
3563e6ccafaeSMatthew G Knepley 
35649566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
35659566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
35669566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
35679566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
35689566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
35699566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
35709566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
35719566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0] + numSubPoints[0]));
3572e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3573e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3574e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3575e6ccafaeSMatthew G Knepley       PetscInt       dof;
3576e6ccafaeSMatthew G Knepley 
35779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
35789566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
35799566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3580e6ccafaeSMatthew G Knepley     }
35819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
35829566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
35839566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
35849566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
35859566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
35869566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
35879566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
35889566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
35899566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
35909566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3591e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3592e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3593e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3594e6ccafaeSMatthew G Knepley       PetscInt       dof, off, sdof, soff, d;
3595e6ccafaeSMatthew G Knepley 
35969566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
35979566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
35989566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
35999566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
360063a3b9bcSJacob 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);
3601efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3602e6ccafaeSMatthew G Knepley     }
36039566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
36049566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
36059566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
36069566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3607e6ccafaeSMatthew G Knepley   }
36083982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
36093982b651SMatthew G. Knepley   {
36103982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
36113982b651SMatthew G. Knepley     IS                 subpIS;
36123982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
36135033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
36145033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3615dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
36165033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
36175033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
36183982b651SMatthew G. Knepley 
36199566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
36205033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
36219566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
36229566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
36239566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
36249566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
36259566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
36263982b651SMatthew G. Knepley     if (subpIS) {
3627dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3628dd05d810SMatthew G. Knepley 
36299566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
36309566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3631dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3632dd05d810SMatthew G. Knepley       if (!sorted) {
3633dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3634dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3635dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3636dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3637dd05d810SMatthew G. Knepley       }
36383982b651SMatthew G. Knepley     }
36399566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
36403982b651SMatthew G. Knepley     if (numRoots >= 0) {
36415033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
36425033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
36439566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
36443982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
36453982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
36463982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
36473982b651SMatthew G. Knepley       }
364830cbcd5dSksagiyam       for (p = pStart; p < pEnd; ++p) {
364930cbcd5dSksagiyam         newOwners[p - pStart].rank  = -3;
365030cbcd5dSksagiyam         newOwners[p - pStart].index = -3;
365130cbcd5dSksagiyam       }
365230cbcd5dSksagiyam       if (sanitizeSubmesh) {
365330cbcd5dSksagiyam         /* A subpoint is forced to be owned by a rank that owns */
365430cbcd5dSksagiyam         /* a subcell that contains the subpoint in its closure. */
365530cbcd5dSksagiyam         PetscInt  cStart, cEnd, c, clSize, cl;
365630cbcd5dSksagiyam         PetscInt *ownedCells, *closure = NULL;
365730cbcd5dSksagiyam 
365830cbcd5dSksagiyam         /* claim ownership */
365930cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
366030cbcd5dSksagiyam           const PetscInt point = subpoints[p];
366130cbcd5dSksagiyam 
366230cbcd5dSksagiyam           newLocalPoints[point - pStart].rank  = rank;
366330cbcd5dSksagiyam           newLocalPoints[point - pStart].index = p;
366430cbcd5dSksagiyam         }
366530cbcd5dSksagiyam         PetscCall(DMGetDimension(subdm, &sdim));
366630cbcd5dSksagiyam         PetscCall(DMPlexGetDepthStratum(dm, sdim, &cStart, &cEnd));
366730cbcd5dSksagiyam         PetscCall(PetscMalloc1(cEnd - cStart, &ownedCells));
366830cbcd5dSksagiyam         for (c = cStart; c < cEnd; ++c) ownedCells[c - cStart] = 0;
366930cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
367030cbcd5dSksagiyam           c = subpoints[p];
367130cbcd5dSksagiyam           if (c >= cStart && c < cEnd) ownedCells[c - cStart] = 1;
367230cbcd5dSksagiyam         }
367330cbcd5dSksagiyam         for (l = 0; l < numLeaves; ++l) {
367430cbcd5dSksagiyam           c = localPoints ? localPoints[l] : l;
367530cbcd5dSksagiyam           if (c >= cStart && c < cEnd) ownedCells[c - cStart] = 0;
367630cbcd5dSksagiyam         }
367730cbcd5dSksagiyam         for (c = cStart; c < cEnd; ++c) {
367830cbcd5dSksagiyam           if (ownedCells[c - cStart] == 0) continue;
367930cbcd5dSksagiyam           PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
368030cbcd5dSksagiyam           for (cl = 0; cl < clSize * 2; cl += 2) {
368130cbcd5dSksagiyam             p = closure[cl];
368230cbcd5dSksagiyam             if (newLocalPoints[p - pStart].rank < size) newLocalPoints[p - pStart].rank += size;
368330cbcd5dSksagiyam           }
368430cbcd5dSksagiyam           PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
368530cbcd5dSksagiyam         }
368630cbcd5dSksagiyam         PetscCall(PetscFree(ownedCells));
368730cbcd5dSksagiyam         for (p = 0; p < numRoots; ++p) {
368830cbcd5dSksagiyam           newOwners[p].rank  = newLocalPoints[p].rank;
368930cbcd5dSksagiyam           newOwners[p].index = newLocalPoints[p].index;
369030cbcd5dSksagiyam         }
369130cbcd5dSksagiyam       } else {
36923982b651SMatthew G. Knepley         /* Set subleaves */
36933982b651SMatthew G. Knepley         for (l = 0; l < numLeaves; ++l) {
36943982b651SMatthew G. Knepley           const PetscInt point    = localPoints[l];
3695dd05d810SMatthew G. Knepley           const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
36963982b651SMatthew G. Knepley 
36973982b651SMatthew G. Knepley           if (subpoint < 0) continue;
36983982b651SMatthew G. Knepley           newLocalPoints[point - pStart].rank  = rank;
36993982b651SMatthew G. Knepley           newLocalPoints[point - pStart].index = subpoint;
37003982b651SMatthew G. Knepley           ++numSubleaves;
37013982b651SMatthew G. Knepley         }
37023982b651SMatthew G. Knepley         /* Must put in owned subpoints */
3703d9384f27SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) {
37045033f954SMatthew G. Knepley           /* Hold on to currently owned points */
37055033f954SMatthew G. Knepley           if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
37065033f954SMatthew G. Knepley           else newOwners[subpoints[p] - pStart].rank = rank;
3707d9384f27SMatthew G. Knepley           newOwners[subpoints[p] - pStart].index = p;
37083982b651SMatthew G. Knepley         }
370930cbcd5dSksagiyam       }
37109566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
37119566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
37129371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
37139371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
3714fdb4e32bSksagiyam       if (ownershipTransferSF) {
3715fdb4e32bSksagiyam         PetscSFNode *iremote1 = NULL, *newOwners1 = NULL;
3716fdb4e32bSksagiyam         PetscInt    *ilocal1 = NULL;
3717fdb4e32bSksagiyam         PetscInt     nleaves1, point;
3718fdb4e32bSksagiyam 
3719fdb4e32bSksagiyam         for (p = 0; p < numSubpoints; ++p) {
3720fdb4e32bSksagiyam           point                                = subpoints[p];
3721fdb4e32bSksagiyam           newLocalPoints[point - pStart].index = point;
3722fdb4e32bSksagiyam         }
3723fdb4e32bSksagiyam         PetscCall(PetscMalloc1(numRoots, &newOwners1));
3724fdb4e32bSksagiyam         for (p = 0; p < numRoots; ++p) {
3725fdb4e32bSksagiyam           newOwners1[p].rank  = -1;
3726fdb4e32bSksagiyam           newOwners1[p].index = -1;
3727fdb4e32bSksagiyam         }
3728fdb4e32bSksagiyam         PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners1, MPI_MAXLOC));
3729fdb4e32bSksagiyam         PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners1, MPI_MAXLOC));
3730fdb4e32bSksagiyam         for (p = 0, nleaves1 = 0; p < numRoots; ++p) {
3731fdb4e32bSksagiyam           if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) { ++nleaves1; }
3732fdb4e32bSksagiyam         }
3733fdb4e32bSksagiyam         PetscCall(PetscMalloc1(nleaves1, &ilocal1));
3734fdb4e32bSksagiyam         PetscCall(PetscMalloc1(nleaves1, &iremote1));
3735fdb4e32bSksagiyam         for (p = 0, nleaves1 = 0; p < numRoots; ++p) {
3736fdb4e32bSksagiyam           if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) {
3737fdb4e32bSksagiyam             ilocal1[nleaves1]        = pStart + p;
3738fdb4e32bSksagiyam             iremote1[nleaves1].rank  = newOwners[p].rank;
3739fdb4e32bSksagiyam             iremote1[nleaves1].index = newOwners1[p].index;
3740fdb4e32bSksagiyam             ++nleaves1;
3741fdb4e32bSksagiyam           }
3742fdb4e32bSksagiyam         }
3743fdb4e32bSksagiyam         PetscCall(PetscFree(newOwners1));
3744fdb4e32bSksagiyam         PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)sfPoint), ownershipTransferSF));
3745fdb4e32bSksagiyam         PetscCall(PetscSFSetFromOptions(*ownershipTransferSF));
3746fdb4e32bSksagiyam         PetscCall(PetscSFSetGraph(*ownershipTransferSF, pEnd - pStart, nleaves1, ilocal1, PETSC_OWN_POINTER, iremote1, PETSC_OWN_POINTER));
3747fdb4e32bSksagiyam       }
374830cbcd5dSksagiyam       if (sanitizeSubmesh) {
374930cbcd5dSksagiyam         for (p = pStart; p < pEnd; ++p) {
375030cbcd5dSksagiyam           newLocalPoints[p - pStart].rank  = newOwners[p - pStart].rank;
375130cbcd5dSksagiyam           newLocalPoints[p - pStart].index = newOwners[p - pStart].index;
375230cbcd5dSksagiyam         }
375330cbcd5dSksagiyam       }
37549566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
37559566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
375630cbcd5dSksagiyam       if (sanitizeSubmesh) {
375730cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
375830cbcd5dSksagiyam           const PetscInt point = subpoints[p];
375930cbcd5dSksagiyam 
376030cbcd5dSksagiyam           if (newLocalPoints[point - pStart].rank >= 0 && newLocalPoints[point - pStart].rank != rank) ++sl;
376130cbcd5dSksagiyam         }
376230cbcd5dSksagiyam         PetscCall(PetscMalloc1(sl, &slocalPoints));
376330cbcd5dSksagiyam         PetscCall(PetscMalloc1(sl, &sremotePoints));
376430cbcd5dSksagiyam         for (p = 0, sl = 0; p < numSubpoints; ++p) {
376530cbcd5dSksagiyam           const PetscInt point = subpoints[p];
376630cbcd5dSksagiyam 
376730cbcd5dSksagiyam           if (newLocalPoints[point - pStart].rank >= 0 && newLocalPoints[point - pStart].rank != rank) {
376830cbcd5dSksagiyam             slocalPoints[sl]        = p;
376930cbcd5dSksagiyam             sremotePoints[sl].rank  = newLocalPoints[point - pStart].rank;
377030cbcd5dSksagiyam             sremotePoints[sl].index = newLocalPoints[point - pStart].index;
377130cbcd5dSksagiyam             PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank");
377230cbcd5dSksagiyam             PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint");
377330cbcd5dSksagiyam             ++sl;
377430cbcd5dSksagiyam           }
377530cbcd5dSksagiyam         }
377630cbcd5dSksagiyam       } else {
37779566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
37789566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
37795033f954SMatthew G. Knepley         for (l = 0; l < numLeaves; ++l) {
37803982b651SMatthew G. Knepley           const PetscInt point    = localPoints[l];
3781dd05d810SMatthew G. Knepley           const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
37823982b651SMatthew G. Knepley 
37833982b651SMatthew G. Knepley           if (subpoint < 0) continue;
37849371c9d4SSatish Balay           if (newLocalPoints[point].rank == rank) {
37859371c9d4SSatish Balay             ++ll;
37869371c9d4SSatish Balay             continue;
37879371c9d4SSatish Balay           }
37883982b651SMatthew G. Knepley           slocalPoints[sl]        = subpoint;
37893982b651SMatthew G. Knepley           sremotePoints[sl].rank  = newLocalPoints[point].rank;
37903982b651SMatthew G. Knepley           sremotePoints[sl].index = newLocalPoints[point].index;
379163a3b9bcSJacob Faibussowitsch           PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
379263a3b9bcSJacob Faibussowitsch           PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
37933982b651SMatthew G. Knepley           ++sl;
37943982b651SMatthew G. Knepley         }
37951dca8a05SBarry 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);
379630cbcd5dSksagiyam       }
37979566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
37989566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
37993982b651SMatthew G. Knepley     }
380048a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3801dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
38023982b651SMatthew G. Knepley   }
3803212103e5SMatthew Knepley   /* Filter labels */
38049566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3805efa14ee0SMatthew G Knepley   /* Cleanup */
3806dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
38079566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
38089566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3809e6ccafaeSMatthew G Knepley   }
38109566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
38113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3812e6ccafaeSMatthew G Knepley }
3813e6ccafaeSMatthew G Knepley 
3814d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
3815d71ae5a4SJacob Faibussowitsch {
38163982b651SMatthew G. Knepley   PetscFunctionBegin;
381730cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, PETSC_FALSE, PETSC_FALSE, NULL, subdm));
38183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38193982b651SMatthew G. Knepley }
38203982b651SMatthew G. Knepley 
3821d0fa310fSMatthew G. Knepley /*@
3822e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3823e6ccafaeSMatthew G Knepley 
3824e6ccafaeSMatthew G Knepley   Input Parameters:
3825e6ccafaeSMatthew G Knepley + dm          - The original mesh
382620f4b53cSBarry Smith . vertexLabel - The `DMLabel` marking points contained in the surface
3827158acfadSMatthew G. Knepley . value       - The label value to use
382820f4b53cSBarry Smith - markedFaces - `PETSC_TRUE` if surface faces are marked in addition to vertices, `PETSC_FALSE` if only vertices are marked
3829e6ccafaeSMatthew G Knepley 
3830e6ccafaeSMatthew G Knepley   Output Parameter:
3831e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3832e6ccafaeSMatthew G Knepley 
3833e6ccafaeSMatthew G Knepley   Level: developer
3834e6ccafaeSMatthew G Knepley 
383520f4b53cSBarry Smith   Note:
383620f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
383720f4b53cSBarry Smith 
38381cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3839830e53efSMatthew G. Knepley @*/
3840d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3841d71ae5a4SJacob Faibussowitsch {
3842827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3843827c4036SVaclav Hapla   PetscInt               dim, cdim;
3844e6ccafaeSMatthew G Knepley 
3845e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3846e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38474f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
38489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
38499566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
38509566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
38519566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
38529566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
38539566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
38549566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
385508401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3856827c4036SVaclav Hapla   if (interpolated) {
38579566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3858e6ccafaeSMatthew G Knepley   } else {
38599566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3860e6ccafaeSMatthew G Knepley   }
38615de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
38623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3863e6ccafaeSMatthew G Knepley }
3864e6ccafaeSMatthew G Knepley 
3865d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3866d71ae5a4SJacob Faibussowitsch {
3867766ab985SMatthew G. Knepley   MPI_Comm        comm;
3868766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3869766ab985SMatthew G. Knepley   IS              subvertexIS;
3870766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3871fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3872766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3873412e9a14SMatthew G. Knepley   PetscInt        c, f;
3874766ab985SMatthew G. Knepley 
3875766ab985SMatthew G. Knepley   PetscFunctionBegin;
38769566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3877766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
38789566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
38799566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
38809566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3881766ab985SMatthew G. Knepley   /* Setup chart */
38829566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
38839566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
38849566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
38859566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3886766ab985SMatthew G. Knepley   /* Set cone sizes */
3887766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3888766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3889766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
38909566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
38919566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
389248a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
389348a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
38949566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3895d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3896766ab985SMatthew G. Knepley   /* Create face cones */
38979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
38989566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3899766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3900766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3901766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
390287feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3903064cae4fSPierre Jolivet     PetscBool       isHybrid = PETSC_FALSE;
390487feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3905766ab985SMatthew G. Knepley 
39069566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3907412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
39089566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
390987feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
39109566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
391187feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
391287feddfdSMatthew G. Knepley     }
39139566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
39149566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
39159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
391627234c99SMatthew G. Knepley     /* Not true in parallel
391708401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
391887feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
391987feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3920064cae4fSPierre Jolivet       PetscBool isHybrid = PETSC_FALSE;
3921766ab985SMatthew G. Knepley 
39229566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3923412e9a14SMatthew G. Knepley       if (isHybrid) continue;
392487feddfdSMatthew G. Knepley       /* I know this is a crap search */
392587feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
392687feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3927766ab985SMatthew G. Knepley       }
392863a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
39299566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3930766ab985SMatthew G. Knepley     }
39319566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
393287feddfdSMatthew G. Knepley     ++newFacePoint;
3933766ab985SMatthew G. Knepley   }
39349566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
39359566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
39369566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3937766ab985SMatthew G. Knepley   /* Build coordinates */
3938766ab985SMatthew G. Knepley   {
3939766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3940766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3941766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3942c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
394324640c55SToby Isaac     const char  *name;
3944766ab985SMatthew G. Knepley 
39459566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
39469566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
39479566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
39489566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
39499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
39509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
39519566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
39529566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3953766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3954766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3955766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3956766ab985SMatthew G. Knepley       PetscInt       dof;
3957766ab985SMatthew G. Knepley 
39589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
39599566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
39609566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3961766ab985SMatthew G. Knepley     }
39629566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
39639566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
39649566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
39659566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
39669566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
39679566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
39689566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
39699566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
39709566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
39719566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3972766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3973766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3974766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3975766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3976766ab985SMatthew G. Knepley 
39779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
39789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
39799566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
39809566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
398163a3b9bcSJacob 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);
3982766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3983766ab985SMatthew G. Knepley     }
39849566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
39859566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
39869566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
39879566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3988766ab985SMatthew G. Knepley   }
3989aca35d17SMatthew G. Knepley   /* Build SF */
3990aca35d17SMatthew G. Knepley   CHKMEMQ;
3991aca35d17SMatthew G. Knepley   {
3992aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3993aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3994bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3995aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3996bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
399749c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3998bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3999aca35d17SMatthew G. Knepley 
40009566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
40019566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
40029566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
40039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
40049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
40059566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
4006aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
4007aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
40089566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
4009bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
4010bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
4011bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
4012bdcf2095SMatthew G. Knepley       }
40139e0823b2SMatthew G. Knepley       /* Set subleaves */
4014aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
4015aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
4016bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
4017aca35d17SMatthew G. Knepley 
401863a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
4019bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
4020bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
4021bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
4022bdcf2095SMatthew G. Knepley         ++numSubLeaves;
4023aca35d17SMatthew G. Knepley       }
40249e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
40259e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
40269e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
40279e0823b2SMatthew G. Knepley 
40289e0823b2SMatthew G. Knepley         if (subPoint < 0) {
40299e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
40309e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
40319e0823b2SMatthew G. Knepley         } else {
40329e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
40339e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
40349e0823b2SMatthew G. Knepley         }
4035bdcf2095SMatthew G. Knepley       }
40369566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
40379566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
40389566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
40399566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
40409566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
40419566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
404249c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
4043bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
4044bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
4045aca35d17SMatthew G. Knepley 
4046aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
40479371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
40489371c9d4SSatish Balay           ++ll;
40499371c9d4SSatish Balay           continue;
40509371c9d4SSatish Balay         }
4051aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
4052bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
4053bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
405463a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
405563a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
4056aca35d17SMatthew G. Knepley         ++sl;
4057aca35d17SMatthew G. Knepley       }
40589566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
40591dca8a05SBarry 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);
40609566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
4061aca35d17SMatthew G. Knepley     }
4062aca35d17SMatthew G. Knepley   }
4063aca35d17SMatthew G. Knepley   CHKMEMQ;
4064766ab985SMatthew G. Knepley   /* Cleanup */
40659566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
40669566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
40679566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
40683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4069766ab985SMatthew G. Knepley }
4070766ab985SMatthew G. Knepley 
4071d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
4072d71ae5a4SJacob Faibussowitsch {
40733982b651SMatthew G. Knepley   DMLabel label = NULL;
4074766ab985SMatthew G. Knepley 
4075766ab985SMatthew G. Knepley   PetscFunctionBegin;
40769566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
407730cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, PETSC_FALSE, PETSC_FALSE, NULL, subdm));
40783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4079766ab985SMatthew G. Knepley }
4080766ab985SMatthew G. Knepley 
4081*cc4c1da9SBarry Smith /*@
408220f4b53cSBarry 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.
4083766ab985SMatthew G. Knepley 
4084766ab985SMatthew G. Knepley   Input Parameters:
4085766ab985SMatthew G. Knepley + dm          - The original mesh
408627c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
408720f4b53cSBarry Smith . label       - A label name, or `NULL`
408827c04023SMatthew G. Knepley - value       - A label value
4089766ab985SMatthew G. Knepley 
4090766ab985SMatthew G. Knepley   Output Parameter:
4091766ab985SMatthew G. Knepley . subdm - The surface mesh
4092766ab985SMatthew G. Knepley 
4093766ab985SMatthew G. Knepley   Level: developer
4094766ab985SMatthew G. Knepley 
409520f4b53cSBarry Smith   Note:
409620f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
409720f4b53cSBarry Smith 
40981cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
4099c08575a3SMatthew G. Knepley @*/
4100d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
4101d71ae5a4SJacob Faibussowitsch {
4102c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
4103766ab985SMatthew G. Knepley 
4104766ab985SMatthew G. Knepley   PetscFunctionBegin;
4105766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41064f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
41079566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
41089566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
41099566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
41109566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
41119566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
41129566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
41139566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
4114766ab985SMatthew G. Knepley   if (depth == dim) {
41159566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
4116766ab985SMatthew G. Knepley   } else {
41179566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
4118e6ccafaeSMatthew G Knepley   }
41195de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
41203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4121e6ccafaeSMatthew G Knepley }
4122e6ccafaeSMatthew G Knepley 
4123bec263e5SMatthew G. Knepley /*@
41240e18dc48SMatthew G. Knepley   DMPlexReorderCohesiveSupports - Ensure that face supports for cohesive end caps are ordered
41250e18dc48SMatthew G. Knepley 
41260e18dc48SMatthew G. Knepley   Not Collective
41270e18dc48SMatthew G. Knepley 
41280e18dc48SMatthew G. Knepley   Input Parameter:
41290e18dc48SMatthew G. Knepley . dm - The `DM` containing cohesive cells
41300e18dc48SMatthew G. Knepley 
41310e18dc48SMatthew G. Knepley   Level: developer
41320e18dc48SMatthew G. Knepley 
4133a4e35b19SJacob Faibussowitsch   Note:
4134a4e35b19SJacob Faibussowitsch   For the negative size (first) face, the cohesive cell should be first in the support, and for
4135a4e35b19SJacob Faibussowitsch   the positive side (second) face, the cohesive cell should be second in the support.
41360e18dc48SMatthew G. Knepley 
41370e18dc48SMatthew G. Knepley .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexCreateCohesiveSubmesh()`
41380e18dc48SMatthew G. Knepley @*/
41390e18dc48SMatthew G. Knepley PetscErrorCode DMPlexReorderCohesiveSupports(DM dm)
41400e18dc48SMatthew G. Knepley {
41410e18dc48SMatthew G. Knepley   PetscInt dim, cStart, cEnd;
41420e18dc48SMatthew G. Knepley 
41430e18dc48SMatthew G. Knepley   PetscFunctionBegin;
41440e18dc48SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
41450e18dc48SMatthew G. Knepley   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cStart, &cEnd));
41460e18dc48SMatthew G. Knepley   for (PetscInt c = cStart; c < cEnd; ++c) {
41470e18dc48SMatthew G. Knepley     const PetscInt *cone;
41480e18dc48SMatthew G. Knepley     PetscInt        coneSize;
41490e18dc48SMatthew G. Knepley 
41500e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
41510e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetCone(dm, c, &cone));
41520e18dc48SMatthew G. Knepley     PetscCheck(coneSize >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid cell %" PetscInt_FMT " cone size %" PetscInt_FMT " must be >= 2", c, coneSize);
41530e18dc48SMatthew G. Knepley     for (PetscInt s = 0; s < 2; ++s) {
41540e18dc48SMatthew G. Knepley       const PetscInt *supp;
41550e18dc48SMatthew G. Knepley       PetscInt        suppSize, newsupp[2];
41560e18dc48SMatthew G. Knepley 
41570e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupportSize(dm, cone[s], &suppSize));
41580e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupport(dm, cone[s], &supp));
41590e18dc48SMatthew G. Knepley       if (suppSize == 2) {
41600e18dc48SMatthew G. Knepley         /* Reorder hybrid end cap support */
41610e18dc48SMatthew G. Knepley         if (supp[s] == c) {
41620e18dc48SMatthew G. Knepley           newsupp[0] = supp[1];
41630e18dc48SMatthew G. Knepley           newsupp[1] = supp[0];
41640e18dc48SMatthew G. Knepley         } else {
41650e18dc48SMatthew G. Knepley           newsupp[0] = supp[0];
41660e18dc48SMatthew G. Knepley           newsupp[1] = supp[1];
41670e18dc48SMatthew G. Knepley         }
41680e18dc48SMatthew 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);
41690e18dc48SMatthew G. Knepley         PetscCall(DMPlexSetSupport(dm, cone[s], newsupp));
41700e18dc48SMatthew G. Knepley       }
41710e18dc48SMatthew G. Knepley     }
41720e18dc48SMatthew G. Knepley   }
41730e18dc48SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41740e18dc48SMatthew G. Knepley }
41750e18dc48SMatthew G. Knepley 
41760e18dc48SMatthew G. Knepley /*@
4177bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
4178bec263e5SMatthew G. Knepley 
4179bec263e5SMatthew G. Knepley   Input Parameters:
4180bec263e5SMatthew G. Knepley + dm              - The original mesh
418120f4b53cSBarry Smith . cellLabel       - The `DMLabel` marking cells contained in the new mesh
41824d00ad26Sksagiyam . value           - The label value to use
418330cbcd5dSksagiyam . ignoreLabelHalo - The flag indicating if labeled points that are in the halo are ignored
418430cbcd5dSksagiyam - sanitizeSubmesh - The flag indicating if a subpoint is forced to be owned by a rank that owns a subcell that contains that point in its closure
4185bec263e5SMatthew G. Knepley 
41869cde84edSJose E. Roman   Output Parameters:
4187fdb4e32bSksagiyam + ownershipTransferSF - The `PetscSF` representing the ownership transfers between parent local meshes due to submeshing.
4188fdb4e32bSksagiyam - subdm               - The new mesh
4189bec263e5SMatthew G. Knepley 
4190bec263e5SMatthew G. Knepley   Level: developer
4191bec263e5SMatthew G. Knepley 
419220f4b53cSBarry Smith   Note:
419320f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
419420f4b53cSBarry Smith 
4195fdb4e32bSksagiyam   On a given rank, the leaves of the ownershipTransferSF are the points in the local mesh that this rank gives up ownership of (in the submesh), and
4196fdb4e32bSksagiyam   the remote locations for these leaves are tuples of rank and point that represent the new owners.
4197fdb4e32bSksagiyam 
41981cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
4199bec263e5SMatthew G. Knepley @*/
420030cbcd5dSksagiyam PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, PetscBool ignoreLabelHalo, PetscBool sanitizeSubmesh, PetscSF *ownershipTransferSF, DM *subdm)
4201d71ae5a4SJacob Faibussowitsch {
4202b47cd10cSMatthew G. Knepley   PetscInt dim, overlap;
4203bec263e5SMatthew G. Knepley 
4204bec263e5SMatthew G. Knepley   PetscFunctionBegin;
4205bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
420630cbcd5dSksagiyam   if (ownershipTransferSF) PetscAssertPointer(ownershipTransferSF, 6);
420730cbcd5dSksagiyam   PetscAssertPointer(subdm, 7);
42089566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
42099566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
42109566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
4211bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
421230cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, ignoreLabelHalo, sanitizeSubmesh, ownershipTransferSF, *subdm));
42135de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
4214b47cd10cSMatthew G. Knepley   // It is possible to obtain a surface mesh where some faces are in SF
4215b47cd10cSMatthew G. Knepley   //   We should either mark the mesh as having an overlap, or delete these from the SF
4216b47cd10cSMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
4217b47cd10cSMatthew G. Knepley   if (!overlap) {
4218b47cd10cSMatthew G. Knepley     PetscSF         sf;
4219b47cd10cSMatthew G. Knepley     const PetscInt *leaves;
4220b47cd10cSMatthew G. Knepley     PetscInt        cStart, cEnd, Nl;
4221b47cd10cSMatthew G. Knepley     PetscBool       hasSubcell = PETSC_FALSE, ghasSubcell;
4222b47cd10cSMatthew G. Knepley 
4223b47cd10cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd));
4224b47cd10cSMatthew G. Knepley     PetscCall(DMGetPointSF(*subdm, &sf));
4225b47cd10cSMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
4226b47cd10cSMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
4227b47cd10cSMatthew G. Knepley       const PetscInt point = leaves ? leaves[l] : l;
4228b47cd10cSMatthew G. Knepley 
4229b47cd10cSMatthew G. Knepley       if (point >= cStart && point < cEnd) {
4230b47cd10cSMatthew G. Knepley         hasSubcell = PETSC_TRUE;
4231b47cd10cSMatthew G. Knepley         break;
4232b47cd10cSMatthew G. Knepley       }
4233b47cd10cSMatthew G. Knepley     }
4234b47cd10cSMatthew G. Knepley     PetscCall(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
4235b47cd10cSMatthew G. Knepley     if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1));
4236b47cd10cSMatthew G. Knepley   }
42373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4238bec263e5SMatthew G. Knepley }
4239bec263e5SMatthew G. Knepley 
424064beef6dSMatthew G. Knepley /*@
424120f4b53cSBarry Smith   DMPlexGetSubpointMap - Returns a `DMLabel` with point dimension as values
424264beef6dSMatthew G. Knepley 
424364beef6dSMatthew G. Knepley   Input Parameter:
424420f4b53cSBarry Smith . dm - The submesh `DM`
424564beef6dSMatthew G. Knepley 
424664beef6dSMatthew G. Knepley   Output Parameter:
424720f4b53cSBarry Smith . subpointMap - The `DMLabel` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
424864beef6dSMatthew G. Knepley 
424964beef6dSMatthew G. Knepley   Level: developer
425064beef6dSMatthew G. Knepley 
42511cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
425264beef6dSMatthew G. Knepley @*/
4253d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
4254d71ae5a4SJacob Faibussowitsch {
4255e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4256e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42574f572ea9SToby Isaac   PetscAssertPointer(subpointMap, 2);
425865663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
42593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4260e6ccafaeSMatthew G Knepley }
4261e6ccafaeSMatthew G Knepley 
4262c08575a3SMatthew G. Knepley /*@
426320f4b53cSBarry Smith   DMPlexSetSubpointMap - Sets the `DMLabel` with point dimension as values
4264c08575a3SMatthew G. Knepley 
4265c08575a3SMatthew G. Knepley   Input Parameters:
426620f4b53cSBarry Smith + dm          - The submesh `DM`
426720f4b53cSBarry Smith - subpointMap - The `DMLabel` of all the points from the original mesh in this submesh
4268c08575a3SMatthew G. Knepley 
4269c08575a3SMatthew G. Knepley   Level: developer
4270c08575a3SMatthew G. Knepley 
427120f4b53cSBarry Smith   Note:
427220f4b53cSBarry Smith   Should normally not be called by the user, since it is set in `DMPlexCreateSubmesh()`
427320f4b53cSBarry Smith 
42741cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
4275c08575a3SMatthew G. Knepley @*/
4276d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
4277d71ae5a4SJacob Faibussowitsch {
4278e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
4279285d324eSMatthew G. Knepley   DMLabel  tmp;
4280e6ccafaeSMatthew G Knepley 
4281e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4282e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4283285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
4284e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
42859566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
42869566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
42873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4288e6ccafaeSMatthew G Knepley }
4289e6ccafaeSMatthew G Knepley 
4290d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
4291d71ae5a4SJacob Faibussowitsch {
429297d8846cSMatthew Knepley   DMLabel  spmap;
429397d8846cSMatthew Knepley   PetscInt depth, d;
429497d8846cSMatthew Knepley 
429597d8846cSMatthew Knepley   PetscFunctionBegin;
42969566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
42979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
429897d8846cSMatthew Knepley   if (spmap && depth >= 0) {
429997d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
430097d8846cSMatthew Knepley     PetscInt *points, *depths;
430197d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
430297d8846cSMatthew Knepley 
43039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
430463a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
43059566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
43069566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
430797d8846cSMatthew Knepley     depths[0] = depth;
430897d8846cSMatthew Knepley     depths[1] = 0;
4309ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
431097d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
431197d8846cSMatthew Knepley       const PetscInt dep = depths[d];
431297d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
431397d8846cSMatthew Knepley 
43149566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
43159566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
431697d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
431763a3b9bcSJacob 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);
431897d8846cSMatthew Knepley       } else {
431997d8846cSMatthew Knepley         if (!n) {
432097d8846cSMatthew Knepley           if (d == 0) {
432197d8846cSMatthew Knepley             /* Missing cells */
432297d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
432397d8846cSMatthew Knepley           } else {
432497d8846cSMatthew Knepley             /* Missing faces */
432597d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
432697d8846cSMatthew Knepley           }
432797d8846cSMatthew Knepley         }
432897d8846cSMatthew Knepley       }
432997d8846cSMatthew Knepley       if (n) {
433097d8846cSMatthew Knepley         IS              is;
433197d8846cSMatthew Knepley         const PetscInt *opoints;
433297d8846cSMatthew Knepley 
43339566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
43349566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
433597d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
43369566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
43379566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
433897d8846cSMatthew Knepley       }
433997d8846cSMatthew Knepley     }
43409566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
434163a3b9bcSJacob 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);
43429566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
43439566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
434497d8846cSMatthew Knepley   }
43453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
434697d8846cSMatthew Knepley }
434797d8846cSMatthew Knepley 
434864beef6dSMatthew G. Knepley /*@
434920f4b53cSBarry Smith   DMPlexGetSubpointIS - Returns an `IS` covering the entire subdm chart with the original points as data
4350e6ccafaeSMatthew G Knepley 
4351e6ccafaeSMatthew G Knepley   Input Parameter:
435220f4b53cSBarry Smith . dm - The submesh `DM`
4353e6ccafaeSMatthew G Knepley 
4354e6ccafaeSMatthew G Knepley   Output Parameter:
435520f4b53cSBarry Smith . subpointIS - The `IS` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
435664beef6dSMatthew G. Knepley 
435764beef6dSMatthew G. Knepley   Level: developer
435864beef6dSMatthew G. Knepley 
435920f4b53cSBarry Smith   Note:
436020f4b53cSBarry Smith   This `IS` is guaranteed to be sorted by the construction of the submesh
436120f4b53cSBarry Smith 
43621cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
436364beef6dSMatthew G. Knepley @*/
4364d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
4365d71ae5a4SJacob Faibussowitsch {
436697d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
436797d8846cSMatthew Knepley   DMLabel          spmap;
436897d8846cSMatthew Knepley   PetscObjectState state;
4369e6ccafaeSMatthew G Knepley 
4370e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4371e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43724f572ea9SToby Isaac   PetscAssertPointer(subpointIS, 2);
43739566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
43749566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
43759566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
437697d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
43773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4378e6ccafaeSMatthew G Knepley }
4379559a1558SMatthew G. Knepley 
4380559a1558SMatthew G. Knepley /*@
438120f4b53cSBarry Smith   DMGetEnclosureRelation - Get the relationship between `dmA` and `dmB`
4382559a1558SMatthew G. Knepley 
4383559a1558SMatthew G. Knepley   Input Parameters:
438420f4b53cSBarry Smith + dmA - The first `DM`
438520f4b53cSBarry Smith - dmB - The second `DM`
4386559a1558SMatthew G. Knepley 
4387559a1558SMatthew G. Knepley   Output Parameter:
438820f4b53cSBarry Smith . rel - The relation of `dmA` to `dmB`
4389559a1558SMatthew G. Knepley 
4390a6e0b375SMatthew G. Knepley   Level: intermediate
4391559a1558SMatthew G. Knepley 
43921cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosurePoint()`
4393559a1558SMatthew G. Knepley @*/
4394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4395d71ae5a4SJacob Faibussowitsch {
4396a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
4397559a1558SMatthew G. Knepley   DMLabel  spmap;
4398a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4399559a1558SMatthew G. Knepley 
440044171101SMatthew G. Knepley   PetscFunctionBegin;
44014f572ea9SToby Isaac   PetscAssertPointer(rel, 3);
4402a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
44033ba16761SJacob Faibussowitsch   if (!dmA || !dmB) PetscFunctionReturn(PETSC_SUCCESS);
4404a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4405064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
44069371c9d4SSatish Balay   if (dmA == dmB) {
44079371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
44083ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
44099371c9d4SSatish Balay   }
44109566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
44119566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
44129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
44139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4414a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4415a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4416a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
4417a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4418a6e0b375SMatthew G. Knepley     goto end;
4419559a1558SMatthew G. Knepley   }
4420a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4421a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4422a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4423a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
44249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4425a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4426a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4427a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4428a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4429a6e0b375SMatthew G. Knepley   } else {
4430a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4431a6e0b375SMatthew G. Knepley   }
4432a6e0b375SMatthew G. Knepley end:
44339566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
44349566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
44353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4436559a1558SMatthew G. Knepley }
443744171101SMatthew G. Knepley 
443844171101SMatthew G. Knepley /*@
443920f4b53cSBarry Smith   DMGetEnclosurePoint - Get the point `pA` in `dmA` which corresponds to the point `pB` in `dmB`
444044171101SMatthew G. Knepley 
444144171101SMatthew G. Knepley   Input Parameters:
444220f4b53cSBarry Smith + dmA   - The first `DM`
444320f4b53cSBarry Smith . dmB   - The second `DM`
444420f4b53cSBarry Smith . etype - The type of enclosure relation that `dmA` has to `dmB`
444520f4b53cSBarry Smith - pB    - A point of `dmB`
444644171101SMatthew G. Knepley 
444744171101SMatthew G. Knepley   Output Parameter:
444820f4b53cSBarry Smith . pA - The corresponding point of `dmA`
444944171101SMatthew G. Knepley 
4450a6e0b375SMatthew G. Knepley   Level: intermediate
445144171101SMatthew G. Knepley 
44521cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosureRelation()`
445344171101SMatthew G. Knepley @*/
4454d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
4455d71ae5a4SJacob Faibussowitsch {
4456a6e0b375SMatthew G. Knepley   DM              sdm;
4457a6e0b375SMatthew G. Knepley   IS              subpointIS;
4458a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4459a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
446044171101SMatthew G. Knepley 
446144171101SMatthew G. Knepley   PetscFunctionBegin;
4462a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4463a6e0b375SMatthew G. Knepley   switch (etype) {
4464a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4465a6e0b375SMatthew G. Knepley     sdm = dmB;
44669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
44679566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4468a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
44699566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4470a6e0b375SMatthew G. Knepley     break;
4471a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4472a6e0b375SMatthew G. Knepley     sdm = dmA;
44739566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
44749566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
44759566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
44769566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4477a6e0b375SMatthew G. Knepley     if (*pA < 0) {
44789566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
44799566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
448063a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4481a6e0b375SMatthew G. Knepley     }
44829566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4483a6e0b375SMatthew G. Knepley     break;
4484a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
4485d71ae5a4SJacob Faibussowitsch   case DM_ENC_NONE:
4486d71ae5a4SJacob Faibussowitsch     *pA = pB;
4487d71ae5a4SJacob Faibussowitsch     break;
44889371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4489a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
449044171101SMatthew G. Knepley 
44919566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
44929566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
44939371c9d4SSatish Balay   } break;
4494d71ae5a4SJacob Faibussowitsch   default:
4495d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
449644171101SMatthew G. Knepley   }
44973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
449844171101SMatthew G. Knepley }
4499