xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 3e98c3d082bc67816db5f10e22fe748b58ec0a8c)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>  /*I      "petscdmplex.h"    I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I      "petscdmlabel.h"   I*/
30c312b8eSJed Brown #include <petscsf.h>
4e6ccafaeSMatthew G Knepley 
5d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCellIsHybrid_Internal(DM dm, PetscInt p, PetscBool *isHybrid)
6d71ae5a4SJacob Faibussowitsch {
7412e9a14SMatthew G. Knepley   DMPolytopeType ct;
8412e9a14SMatthew G. Knepley 
9412e9a14SMatthew G. Knepley   PetscFunctionBegin;
109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, p, &ct));
11412e9a14SMatthew G. Knepley   switch (ct) {
12412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
13412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
14412e9a14SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR:
15d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
16d71ae5a4SJacob Faibussowitsch     *isHybrid = PETSC_TRUE;
17d71ae5a4SJacob Faibussowitsch     break;
18d71ae5a4SJacob Faibussowitsch   default:
19d71ae5a4SJacob Faibussowitsch     *isHybrid = PETSC_FALSE;
20d71ae5a4SJacob Faibussowitsch     break;
21412e9a14SMatthew G. Knepley   }
223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23412e9a14SMatthew G. Knepley }
24412e9a14SMatthew G. Knepley 
25d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetTensorPrismBounds_Internal(DM dm, PetscInt dim, PetscInt *cStart, PetscInt *cEnd)
26d71ae5a4SJacob Faibussowitsch {
27412e9a14SMatthew G. Knepley   DMLabel ctLabel;
28412e9a14SMatthew G. Knepley 
29412e9a14SMatthew G. Knepley   PetscFunctionBegin;
30412e9a14SMatthew G. Knepley   if (cStart) *cStart = -1;
31412e9a14SMatthew G. Knepley   if (cEnd) *cEnd = -1;
329566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
33412e9a14SMatthew G. Knepley   switch (dim) {
34d71ae5a4SJacob Faibussowitsch   case 1:
35d71ae5a4SJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_POINT_PRISM_TENSOR, cStart, cEnd));
36d71ae5a4SJacob Faibussowitsch     break;
37d71ae5a4SJacob Faibussowitsch   case 2:
38d71ae5a4SJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_SEG_PRISM_TENSOR, cStart, cEnd));
39d71ae5a4SJacob Faibussowitsch     break;
40412e9a14SMatthew G. Knepley   case 3:
419566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_TRI_PRISM_TENSOR, cStart, cEnd));
429566063dSJacob Faibussowitsch     if (*cStart < 0) PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_QUAD_PRISM_TENSOR, cStart, cEnd));
43412e9a14SMatthew G. Knepley     break;
44d71ae5a4SJacob Faibussowitsch   default:
453ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
46412e9a14SMatthew G. Knepley   }
473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48412e9a14SMatthew G. Knepley }
49412e9a14SMatthew G. Knepley 
5069941377SStefano Zampini PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label, PetscBool missing_only)
51d71ae5a4SJacob Faibussowitsch {
5269941377SStefano Zampini   PetscInt           depth, pStart, pEnd, fStart, fEnd, f, supportSize, nroots = -1, nleaves = -1, defval;
53429fa399SMatthew G. Knepley   PetscSF            sf;
5441e3e744Sksagiyam   const PetscSFNode *iremote = NULL;
5541e3e744Sksagiyam   const PetscInt    *ilocal  = NULL;
5641e3e744Sksagiyam   PetscInt          *leafData;
5730560a7bSMatthew G. Knepley 
5830560a7bSMatthew G. Knepley   PetscFunctionBegin;
5941e3e744Sksagiyam   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
6041e3e744Sksagiyam   PetscCall(DMPlexGetDepth(dm, &depth));
6141e3e744Sksagiyam   if (depth >= cellHeight + 1) {
629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, cellHeight + 1, &fStart, &fEnd));
6341e3e744Sksagiyam   } else {
6441e3e744Sksagiyam     /* Note DMPlexGetHeightStratum() returns fStart, fEnd = pStart, pEnd */
6541e3e744Sksagiyam     /* if height > depth, which is not what we want here.                */
6641e3e744Sksagiyam     fStart = 0;
6741e3e744Sksagiyam     fEnd   = 0;
6841e3e744Sksagiyam   }
6969941377SStefano Zampini   PetscCall(DMLabelGetDefaultValue(label, &defval));
7041e3e744Sksagiyam   PetscCall(PetscCalloc1(pEnd - pStart, &leafData));
718e3a54c0SPierre Jolivet   leafData = PetscSafePointerPlusOffset(leafData, -pStart);
7241e3e744Sksagiyam   PetscCall(DMGetPointSF(dm, &sf));
7341e3e744Sksagiyam   if (sf) PetscCall(PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, &iremote));
7441e3e744Sksagiyam   if (sf && nroots >= 0) {
7541e3e744Sksagiyam     PetscInt        cStart, cEnd, c, i;
7641e3e744Sksagiyam     PetscInt       *rootData, *rootData1, *cellOwners, hasTwoSupportCells = -2;
7741e3e744Sksagiyam     const PetscInt *support;
7841e3e744Sksagiyam     PetscMPIInt     rank;
7930560a7bSMatthew G. Knepley 
8041e3e744Sksagiyam     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8141e3e744Sksagiyam     PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8241e3e744Sksagiyam     PetscCall(PetscCalloc3(pEnd - pStart, &rootData, pEnd - pStart, &rootData1, cEnd - cStart, &cellOwners));
8341e3e744Sksagiyam     rootData -= pStart;
8441e3e744Sksagiyam     rootData1 -= pStart;
85835f2295SStefano Zampini     for (c = cStart; c < cEnd; ++c) cellOwners[c - cStart] = rank;
8641e3e744Sksagiyam     for (i = 0; i < nleaves; ++i) {
8741e3e744Sksagiyam       c = ilocal ? ilocal[i] : i;
8841e3e744Sksagiyam       if (c >= cStart && c < cEnd) cellOwners[c - cStart] = iremote[i].rank;
8941e3e744Sksagiyam     }
9041e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
92e752be1aSMatthew G. Knepley       if (supportSize == 1) {
9341e3e744Sksagiyam         PetscCall(DMPlexGetSupport(dm, f, &support));
9441e3e744Sksagiyam         leafData[f] = cellOwners[support[0] - cStart];
9541e3e744Sksagiyam       } else {
9641e3e744Sksagiyam         /* TODO: When using DMForest, we could have a parent facet (a coarse facet)     */
9741e3e744Sksagiyam         /*       supportSize of which alone does not tell us if it is an interior       */
9841e3e744Sksagiyam         /*       facet or an exterior facet. Those facets can be identified by checking */
9941e3e744Sksagiyam         /*       if they are in the parent tree. We should probably skip those parent   */
10041e3e744Sksagiyam         /*       facets here, which will allow for including the following check, and   */
10141e3e744Sksagiyam         /*       see if they are exterior facets or not afterwards by checking if the   */
10241e3e744Sksagiyam         /*       children are exterior or not.                                          */
10341e3e744Sksagiyam         /* PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", supportSize, f); */
10441e3e744Sksagiyam         leafData[f] = hasTwoSupportCells; /* some negative PetscInt */
105429fa399SMatthew G. Knepley       }
10641e3e744Sksagiyam       rootData[f]  = leafData[f];
10741e3e744Sksagiyam       rootData1[f] = leafData[f];
10841e3e744Sksagiyam     }
10941e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData, MPI_MIN));
11041e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
11141e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData, MPI_MIN));
11241e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
11341e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
11441e3e744Sksagiyam       /* Store global support size of f.                                                    */
11541e3e744Sksagiyam       /* Facet f is an interior facet if and only if one of the following two is satisfied: */
11641e3e744Sksagiyam       /* 1. supportSize is 2 on some rank.                                                  */
11741e3e744Sksagiyam       /* 2. supportSize is 1 on any rank that can see f, but f is on a partition boundary;  */
11841e3e744Sksagiyam       /*    i.e., rootData[f] < rootData1[f].                                               */
11941e3e744Sksagiyam       rootData[f] = (rootData[f] == hasTwoSupportCells || (rootData[f] < rootData1[f])) ? 2 : 1;
12041e3e744Sksagiyam       leafData[f] = rootData[f];
12141e3e744Sksagiyam     }
12241e3e744Sksagiyam     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
12341e3e744Sksagiyam     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
12441e3e744Sksagiyam     rootData += pStart;
12541e3e744Sksagiyam     rootData1 += pStart;
12641e3e744Sksagiyam     PetscCall(PetscFree3(rootData, rootData1, cellOwners));
12741e3e744Sksagiyam   } else {
12841e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
12941e3e744Sksagiyam       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
13041e3e744Sksagiyam       leafData[f] = supportSize;
13141e3e744Sksagiyam     }
13241e3e744Sksagiyam   }
13341e3e744Sksagiyam   for (f = fStart; f < fEnd; ++f) {
13441e3e744Sksagiyam     if (leafData[f] == 1) {
135e752be1aSMatthew G. Knepley       if (val < 0) {
136e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
137e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
138e752be1aSMatthew G. Knepley 
13969941377SStefano Zampini         PetscAssert(!missing_only, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented");
1409566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
141e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize * 2; cl += 2) {
1429566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, closure[cl], &cval));
143e752be1aSMatthew G. Knepley           if (cval < 0) continue;
1449566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, cval));
145e752be1aSMatthew G. Knepley           break;
146e752be1aSMatthew G. Knepley         }
1479566063dSJacob Faibussowitsch         if (cl == clSize * 2) PetscCall(DMLabelSetValue(label, f, 1));
1489566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
149e752be1aSMatthew G. Knepley       } else {
15069941377SStefano Zampini         if (missing_only) {
15169941377SStefano Zampini           PetscInt fval;
15269941377SStefano Zampini           PetscCall(DMLabelGetValue(label, f, &fval));
15369941377SStefano Zampini           if (fval != defval) PetscCall(DMLabelClearValue(label, f, fval));
15469941377SStefano Zampini           else PetscCall(DMLabelSetValue(label, f, val));
15569941377SStefano Zampini         } else {
1569566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, val));
157e752be1aSMatthew G. Knepley         }
15869941377SStefano Zampini       }
15941e3e744Sksagiyam     } else {
16041e3e744Sksagiyam       /* TODO: See the above comment on DMForest */
16141e3e744Sksagiyam       /* PetscCheck(leafData[f] == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", leafData[f], f); */
162e752be1aSMatthew G. Knepley     }
16330560a7bSMatthew G. Knepley   }
1648e3a54c0SPierre Jolivet   leafData = PetscSafePointerPlusOffset(leafData, pStart);
16541e3e744Sksagiyam   PetscCall(PetscFree(leafData));
1663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16730560a7bSMatthew G. Knepley }
16830560a7bSMatthew G. Knepley 
169cd0c2139SMatthew G Knepley /*@
170cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
171cd0c2139SMatthew G Knepley 
17241e3e744Sksagiyam   Collective
173cd0c2139SMatthew G Knepley 
174d8d19677SJose E. Roman   Input Parameters:
175429fa399SMatthew G. Knepley + dm  - The original `DM`
17620f4b53cSBarry Smith - val - The marker value, or `PETSC_DETERMINE` to use some value in the closure (or 1 if none are found)
177cd0c2139SMatthew G Knepley 
178cd0c2139SMatthew G Knepley   Output Parameter:
17920f4b53cSBarry Smith . label - The `DMLabel` marking boundary faces with the given value
180429fa399SMatthew G. Knepley 
181cd0c2139SMatthew G Knepley   Level: developer
182cd0c2139SMatthew G Knepley 
18320f4b53cSBarry Smith   Note:
18441e3e744Sksagiyam   This function will use the point `PetscSF` from the input `DM` and the ownership of the support cells to exclude points on the partition boundary from being marked. If you also wish to mark the partition boundary, you can use `DMSetPointSF()` to temporarily set it to `NULL`, and then reset it to the original object after the call.
18541e3e744Sksagiyam 
18641e3e744Sksagiyam   In DMForest there can be facets support sizes of which alone can not determine whether they are on the boundary. Currently, this function is not guaranteed to produce the correct result in such case.
18720f4b53cSBarry Smith 
1881cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLabelCreate()`, `DMCreateLabel()`
18909f723d9SJed Brown @*/
190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
191d71ae5a4SJacob Faibussowitsch {
192bbb57e68SMatthew G. Knepley   DM                     plex;
193827c4036SVaclav Hapla   DMPlexInterpolatedFlag flg;
194cd0c2139SMatthew G Knepley 
195cd0c2139SMatthew G Knepley   PetscFunctionBegin;
196827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
197bbb57e68SMatthew G. Knepley   PetscCall(DMConvert(dm, DMPLEX, &plex));
198bbb57e68SMatthew G. Knepley   PetscCall(DMPlexIsInterpolated(plex, &flg));
19908401ef6SPierre Jolivet   PetscCheck(flg == DMPLEX_INTERPOLATED_FULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
200bbb57e68SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces_Internal(plex, val, 0, label, PETSC_FALSE));
201bbb57e68SMatthew G. Knepley   PetscCall(DMDestroy(&plex));
2023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
203cd0c2139SMatthew G Knepley }
204cd0c2139SMatthew G Knepley 
205d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
206d71ae5a4SJacob Faibussowitsch {
207b0bf5782SToby Isaac   IS              valueIS;
208ac51f24eSSander Arens   PetscSF         sfPoint;
209b0bf5782SToby Isaac   const PetscInt *values;
210ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
211b0bf5782SToby Isaac 
212b0bf5782SToby Isaac   PetscFunctionBegin;
2139566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2149566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2169566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
217b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
218b0bf5782SToby Isaac     IS              pointIS;
219b0bf5782SToby Isaac     const PetscInt *points;
220b0bf5782SToby Isaac     PetscInt        numPoints, p;
221b0bf5782SToby Isaac 
2229566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2239566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2249566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
225b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
226b0bf5782SToby Isaac       PetscInt  q       = points[p];
227b0bf5782SToby Isaac       PetscInt *closure = NULL;
228b0bf5782SToby Isaac       PetscInt  closureSize, c;
229b0bf5782SToby Isaac 
230b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
231b0bf5782SToby Isaac         continue;
232b0bf5782SToby Isaac       }
2339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
23448a46eb9SPierre Jolivet       for (c = 0; c < closureSize * 2; c += 2) PetscCall(DMLabelSetValue(label, closure[c], values[v]));
2359566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
236b0bf5782SToby Isaac     }
2379566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2389566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
239b0bf5782SToby Isaac   }
2409566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2419566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2429566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
2439566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
244ac51f24eSSander Arens   if (nroots >= 0) {
24526279d81SSanderA     DMLabel         lblRoots, lblLeaves;
24626279d81SSanderA     IS              valueIS, pointIS;
24726279d81SSanderA     const PetscInt *values;
24826279d81SSanderA     PetscInt        numValues, v;
24926279d81SSanderA 
25026279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
2519566063dSJacob Faibussowitsch     PetscCall(DMLabelGather(label, sfPoint, &lblLeaves));
2529566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS));
2539566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2549566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
25526279d81SSanderA     for (v = 0; v < numValues; ++v) {
25626279d81SSanderA       const PetscInt value = values[v];
25726279d81SSanderA 
2589566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
2599566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2609566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
26126279d81SSanderA     }
2629566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2639566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2649566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblLeaves));
26526279d81SSanderA     /* Push point contributions from roots into remote leaves */
2669566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots));
2679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblRoots, &valueIS));
2689566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2699566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
27026279d81SSanderA     for (v = 0; v < numValues; ++v) {
27126279d81SSanderA       const PetscInt value = values[v];
27226279d81SSanderA 
2739566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS));
2749566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2759566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
27626279d81SSanderA     }
2779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2799566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblRoots));
28026279d81SSanderA   }
2813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
282b0bf5782SToby Isaac }
283b0bf5782SToby Isaac 
2842be2b188SMatthew G Knepley /*@
2852be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
2862be2b188SMatthew G Knepley 
2872be2b188SMatthew G Knepley   Input Parameters:
28820f4b53cSBarry Smith + dm    - The `DM`
28920f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
2902be2b188SMatthew G Knepley 
2912be2b188SMatthew G Knepley   Output Parameter:
29220f4b53cSBarry Smith . label - A `DMLabel` marking all surface points in the transitive closure
2932be2b188SMatthew G Knepley 
2942be2b188SMatthew G Knepley   Level: developer
2952be2b188SMatthew G Knepley 
2961cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelCohesiveComplete()`
2972be2b188SMatthew G Knepley @*/
298d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
299d71ae5a4SJacob Faibussowitsch {
3002be2b188SMatthew G Knepley   PetscFunctionBegin;
3019566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
3023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3032be2b188SMatthew G Knepley }
3042be2b188SMatthew G Knepley 
3056cf0e42fSMatthew G. Knepley /*@
306a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
3076cf0e42fSMatthew G. Knepley 
3086cf0e42fSMatthew G. Knepley   Input Parameters:
30920f4b53cSBarry Smith + dm    - The `DM`
31020f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
3116cf0e42fSMatthew G. Knepley 
3126cf0e42fSMatthew G. Knepley   Output Parameter:
31320f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
3146cf0e42fSMatthew G. Knepley 
3156cf0e42fSMatthew G. Knepley   Level: developer
3166cf0e42fSMatthew G. Knepley 
31720f4b53cSBarry Smith   Note:
31820f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
3196cf0e42fSMatthew G. Knepley 
3201cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
3216cf0e42fSMatthew G. Knepley @*/
322d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
323d71ae5a4SJacob Faibussowitsch {
3246cf0e42fSMatthew G. Knepley   IS              valueIS;
3256cf0e42fSMatthew G. Knepley   const PetscInt *values;
3269df54392SMatthew G. Knepley   PetscInt        numValues, v, csStart, csEnd, chStart, chEnd;
3276cf0e42fSMatthew G. Knepley 
3286cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
3299df54392SMatthew G. Knepley   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &csStart, &csEnd));
3309df54392SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(dm, 0, &chStart, &chEnd));
3319566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3329566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3339566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
3346cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
3356cf0e42fSMatthew G. Knepley     IS              pointIS;
3366cf0e42fSMatthew G. Knepley     const PetscInt *points;
3376cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
3386cf0e42fSMatthew G. Knepley 
3399566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3409566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
3426cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3439df54392SMatthew G. Knepley       const PetscInt point   = points[p];
3446cf0e42fSMatthew G. Knepley       PetscInt      *closure = NULL;
3457bd9760aSJed Brown       PetscInt       closureSize, cl, h, cStart, cEnd;
3467bd9760aSJed Brown       DMPolytopeType ct;
3479df54392SMatthew G. Knepley 
3489df54392SMatthew G. Knepley       // If the point is a hybrid, allow hybrid cells
3497bd9760aSJed Brown       PetscCall(DMPlexGetCellType(dm, point, &ct));
3509df54392SMatthew G. Knepley       PetscCall(DMPlexGetPointHeight(dm, point, &h));
3517bd9760aSJed Brown       if (DMPolytopeTypeIsHybrid(ct)) {
3529df54392SMatthew G. Knepley         cStart = chStart;
3539df54392SMatthew G. Knepley         cEnd   = chEnd;
3547bd9760aSJed Brown       } else {
3557bd9760aSJed Brown         cStart = csStart;
3567bd9760aSJed Brown         cEnd   = csEnd;
3579df54392SMatthew G. Knepley       }
3586cf0e42fSMatthew G. Knepley 
3599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
36022eabd52SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
361a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
3629371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
3639371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
3649371c9d4SSatish Balay           break;
3659371c9d4SSatish Balay         }
36622eabd52SMatthew G. Knepley       }
3679566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
3686cf0e42fSMatthew G. Knepley     }
3699566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3709566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3716cf0e42fSMatthew G. Knepley   }
3729566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3739566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
3743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3756cf0e42fSMatthew G. Knepley }
3766cf0e42fSMatthew G. Knepley 
377f402d5e4SToby Isaac /*@
378a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
379a6e0b375SMatthew G. Knepley 
380a6e0b375SMatthew G. Knepley   Input Parameters:
38120f4b53cSBarry Smith + dm    - The `DM`
38220f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
383a6e0b375SMatthew G. Knepley 
384a6e0b375SMatthew G. Knepley   Output Parameter:
38520f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
386a6e0b375SMatthew G. Knepley 
387a6e0b375SMatthew G. Knepley   Level: developer
388a6e0b375SMatthew G. Knepley 
38920f4b53cSBarry Smith   Note:
39020f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
391a6e0b375SMatthew G. Knepley 
3921cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
393a6e0b375SMatthew G. Knepley @*/
394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
395d71ae5a4SJacob Faibussowitsch {
396a6e0b375SMatthew G. Knepley   IS              valueIS;
397a6e0b375SMatthew G. Knepley   const PetscInt *values;
398a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
399a6e0b375SMatthew G. Knepley 
400a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
4019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
4039566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4049566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4059566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
406a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
407a6e0b375SMatthew G. Knepley     IS              pointIS;
408a6e0b375SMatthew G. Knepley     const PetscInt *points;
409a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
410a6e0b375SMatthew G. Knepley 
4119566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4129566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4139566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
414a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
415a6e0b375SMatthew G. Knepley       const PetscInt face    = points[p];
416a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
417a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
418a6e0b375SMatthew G. Knepley 
419a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
4209566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
421a6e0b375SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
422a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
4239371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
4249371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
4259371c9d4SSatish Balay           break;
4269371c9d4SSatish Balay         }
427a6e0b375SMatthew G. Knepley       }
4289566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
429a6e0b375SMatthew G. Knepley     }
4309566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4319566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
432a6e0b375SMatthew G. Knepley   }
4339566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4349566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
436a6e0b375SMatthew G. Knepley }
437a6e0b375SMatthew G. Knepley 
438a6e0b375SMatthew G. Knepley /*@
439f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
440f402d5e4SToby Isaac 
441f402d5e4SToby Isaac   Input Parameters:
44220f4b53cSBarry Smith + dm    - The `DM`
44320f4b53cSBarry Smith - label - A `DMLabel` marking surface points and their adjacent cells
444f402d5e4SToby Isaac 
445f402d5e4SToby Isaac   Output Parameter:
44620f4b53cSBarry Smith . label - A `DMLabel` without cells
447f402d5e4SToby Isaac 
448f402d5e4SToby Isaac   Level: developer
449f402d5e4SToby Isaac 
45020f4b53cSBarry Smith   Note:
45120f4b53cSBarry Smith   This undoes `DMPlexLabelAddCells()` or `DMPlexLabelAddFaceCells()`
452f402d5e4SToby Isaac 
4531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()`
454f402d5e4SToby Isaac @*/
455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
456d71ae5a4SJacob Faibussowitsch {
457f402d5e4SToby Isaac   IS              valueIS;
458f402d5e4SToby Isaac   const PetscInt *values;
459485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
460f402d5e4SToby Isaac 
461f402d5e4SToby Isaac   PetscFunctionBegin;
4629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4639566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4649566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4659566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
466f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
467f402d5e4SToby Isaac     IS              pointIS;
468f402d5e4SToby Isaac     const PetscInt *points;
469f402d5e4SToby Isaac     PetscInt        numPoints, p;
470f402d5e4SToby Isaac 
4719566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4729566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
474f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
475f402d5e4SToby Isaac       PetscInt point = points[p];
476f402d5e4SToby Isaac 
47748a46eb9SPierre Jolivet       if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v]));
478f402d5e4SToby Isaac     }
4799566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4809566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
481f402d5e4SToby Isaac   }
4829566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4839566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
485f402d5e4SToby Isaac }
486f402d5e4SToby Isaac 
48759eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
48859eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
489d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
490d71ae5a4SJacob Faibussowitsch {
4912582d50cSToby Isaac   PetscInt d, off = 0;
4922582d50cSToby Isaac 
4932582d50cSToby Isaac   PetscFunctionBegin;
49459eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
4950974a383SToby Isaac   for (d = 0; d < depth; d++) {
4962582d50cSToby Isaac     PetscInt firstd     = d;
4970974a383SToby Isaac     PetscInt firstStart = depthShift[2 * d];
4982582d50cSToby Isaac     PetscInt e;
4992582d50cSToby Isaac 
5002582d50cSToby Isaac     for (e = d + 1; e <= depth; e++) {
5012582d50cSToby Isaac       if (depthShift[2 * e] < firstStart) {
5022582d50cSToby Isaac         firstd     = e;
5032582d50cSToby Isaac         firstStart = depthShift[2 * d];
5042582d50cSToby Isaac       }
5052582d50cSToby Isaac     }
5062582d50cSToby Isaac     if (firstd != d) {
5072582d50cSToby Isaac       PetscInt swap[2];
5082582d50cSToby Isaac 
5092582d50cSToby Isaac       e                     = firstd;
5102582d50cSToby Isaac       swap[0]               = depthShift[2 * d];
5112582d50cSToby Isaac       swap[1]               = depthShift[2 * d + 1];
5122582d50cSToby Isaac       depthShift[2 * d]     = depthShift[2 * e];
5132582d50cSToby Isaac       depthShift[2 * d + 1] = depthShift[2 * e + 1];
5142582d50cSToby Isaac       depthShift[2 * e]     = swap[0];
5152582d50cSToby Isaac       depthShift[2 * e + 1] = swap[1];
5162582d50cSToby Isaac     }
5172582d50cSToby Isaac   }
5182582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
5190974a383SToby Isaac   for (d = 0; d <= depth; d++) {
5202582d50cSToby Isaac     off += depthShift[2 * d + 1];
52159eef20bSToby Isaac     depthShift[2 * d + 1] = depthShift[2 * d] + off;
5222582d50cSToby Isaac   }
5233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5242582d50cSToby Isaac }
5252582d50cSToby Isaac 
5262582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
527d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
528d71ae5a4SJacob Faibussowitsch {
5292582d50cSToby Isaac   PetscInt d;
5302582d50cSToby Isaac   PetscInt newOff = 0;
5312582d50cSToby Isaac 
5322582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5332582d50cSToby Isaac     if (p < depthShift[2 * d]) return p + newOff;
5342582d50cSToby Isaac     else newOff = depthShift[2 * d + 1] - depthShift[2 * d];
5352582d50cSToby Isaac   }
5360974a383SToby Isaac   return p + newOff;
5372582d50cSToby Isaac }
5382582d50cSToby Isaac 
5392582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
540d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
541d71ae5a4SJacob Faibussowitsch {
5422582d50cSToby Isaac   PetscInt d;
5432582d50cSToby Isaac   PetscInt newOff = 0;
5442582d50cSToby Isaac 
5452582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5462582d50cSToby Isaac     if (p < depthShift[2 * d + 1]) return p + newOff;
5472582d50cSToby Isaac     else newOff = depthShift[2 * d] - depthShift[2 * d + 1];
5482582d50cSToby Isaac   }
5490974a383SToby Isaac   return p + newOff;
550cd0c2139SMatthew G Knepley }
551cd0c2139SMatthew G Knepley 
552d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
553d71ae5a4SJacob Faibussowitsch {
554cd0c2139SMatthew G Knepley   PetscInt depth = 0, d, pStart, pEnd, p;
555fa8e8ae5SToby Isaac   DMLabel  depthLabel;
556cd0c2139SMatthew G Knepley 
557cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5593ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
560cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
5619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5620974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5639566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
5649566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "depth"));
5659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel));
5669566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
567cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
568cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
569fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
570fa8e8ae5SToby Isaac     IS       pIS;
571fa8e8ae5SToby Isaac 
5729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
573fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
574fa8e8ae5SToby Isaac     pEndNew   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5759566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
5769566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
5779566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
578cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
5792582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
580cd0c2139SMatthew G Knepley       PetscInt       size;
581412e9a14SMatthew G. Knepley       DMPolytopeType ct;
582cd0c2139SMatthew G Knepley 
5839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
5849566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
5859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
5869566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
5879566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
5889566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
589cd0c2139SMatthew G Knepley     }
590cd0c2139SMatthew G Knepley   }
5913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
592cd0c2139SMatthew G Knepley }
593cd0c2139SMatthew G Knepley 
594d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
595d71ae5a4SJacob Faibussowitsch {
5962582d50cSToby Isaac   PetscInt *newpoints;
5972582d50cSToby Isaac   PetscInt  depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
598cd0c2139SMatthew G Knepley 
599cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
6013ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
6029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
6039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
6049566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints));
605cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
6069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
607cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
608cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
6092582d50cSToby Isaac     PetscInt        size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
610cd0c2139SMatthew G Knepley 
6119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
6129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
6139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
614ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
6159566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
6169566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
6179566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
6189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
6199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
620ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
621dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
6229566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
623cd0c2139SMatthew G Knepley   }
6249566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
6253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
626cd0c2139SMatthew G Knepley }
627cd0c2139SMatthew G Knepley 
628d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
629d71ae5a4SJacob Faibussowitsch {
630cd0c2139SMatthew G Knepley   PetscSection coordSection, newCoordSection;
631cd0c2139SMatthew G Knepley   Vec          coordinates, newCoordinates;
632cd0c2139SMatthew G Knepley   PetscScalar *coords, *newCoords;
633f2b8cce1SMatthew G. Knepley   PetscInt     coordSize, sStart, sEnd;
634f2b8cce1SMatthew G. Knepley   PetscInt     dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
635f2b8cce1SMatthew G. Knepley   PetscBool    hasCells;
636cd0c2139SMatthew G Knepley 
637cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
6399566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
6409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
641cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
6429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
6439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
6449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
6459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
6469566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6479566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
6489566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
6499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
6509566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
651f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
6529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
653f2b8cce1SMatthew G. Knepley   if (hasCells) {
654f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
655f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
656f2b8cce1SMatthew G. Knepley 
6579566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6589566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
6599566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
660f2b8cce1SMatthew G. Knepley     }
661f2b8cce1SMatthew G. Knepley   }
662cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
6639566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
6649566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
665cd0c2139SMatthew G Knepley   }
6669566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
6679566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
6689566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
6699566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
6709566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates"));
6719566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
6729566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
6739566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates, VECSTANDARD));
6749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
6759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
6769566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6779566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
678f2b8cce1SMatthew G. Knepley   if (hasCells) {
679f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
680f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
681f2b8cce1SMatthew G. Knepley 
6829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
6849566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
685f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
686f2b8cce1SMatthew G. Knepley     }
687f2b8cce1SMatthew G. Knepley   }
688cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
689cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
690cd0c2139SMatthew G Knepley 
6919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
6929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
6939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
694f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
695cd0c2139SMatthew G Knepley   }
6969566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6979566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
6989566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
6999566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
7003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
701cd0c2139SMatthew G Knepley }
702cd0c2139SMatthew G Knepley 
703d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
704d71ae5a4SJacob Faibussowitsch {
705cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
706cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
707cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
708cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
7090e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
710cd0c2139SMatthew G Knepley 
711cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
7139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
7149566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
7156e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
716cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
7179566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation));
7180e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
7199566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7209566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7219566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
7229566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
723cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
7242582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
725cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
726cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
727cd0c2139SMatthew G Knepley     }
7289566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
7299566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
7300e33faafSMatthew G. Knepley   }
7313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7320e33faafSMatthew G. Knepley }
7330e33faafSMatthew G. Knepley 
734d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
735d71ae5a4SJacob Faibussowitsch {
7360e33faafSMatthew G. Knepley   PetscSF   sfPoint, sfPointNew;
7370e33faafSMatthew G. Knepley   PetscBool useNatural;
7380e33faafSMatthew G. Knepley 
7390e33faafSMatthew G. Knepley   PetscFunctionBegin;
7400e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
7419566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
7429566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
7439566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
7440e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
7459566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
7460e33faafSMatthew G. Knepley   if (useNatural) {
7470e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
7480e33faafSMatthew G. Knepley 
7499566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
7509566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
7519566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
7529566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
753cd0c2139SMatthew G Knepley   }
7543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
755cd0c2139SMatthew G Knepley }
756cd0c2139SMatthew G Knepley 
757d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
758d71ae5a4SJacob Faibussowitsch {
759d56405f8SMatthew G. Knepley   PetscInt depth = 0, numLabels, l;
760cd0c2139SMatthew G Knepley 
761cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
763cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
7649566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
765cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
766cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
767cd0c2139SMatthew G Knepley     const char     *lname;
768fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
769cd0c2139SMatthew G Knepley     IS              valueIS;
770cd0c2139SMatthew G Knepley     const PetscInt *values;
771cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
772cd0c2139SMatthew G Knepley 
7739566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
7749566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
775cd0c2139SMatthew G Knepley     if (isDepth) continue;
7769566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
777fa8e8ae5SToby Isaac     if (isDim) continue;
7789566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
7799566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
7809566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
7819566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &val));
7829566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, val));
7839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
7849566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
7859566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
786cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
787cd0c2139SMatthew G Knepley       IS              pointIS;
788cd0c2139SMatthew G Knepley       const PetscInt *points;
789cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
790cd0c2139SMatthew G Knepley 
7919566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
7929566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
7939566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
794cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
7952582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
796cd0c2139SMatthew G Knepley 
7979566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
798cd0c2139SMatthew G Knepley       }
7999566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
8009566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
801cd0c2139SMatthew G Knepley     }
8029566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
8039566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
804cd0c2139SMatthew G Knepley   }
8053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
806d56405f8SMatthew G. Knepley }
807d56405f8SMatthew G. Knepley 
808d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
809d71ae5a4SJacob Faibussowitsch {
810d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
811d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
812d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
813d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
814d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
815d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
816d56405f8SMatthew G. Knepley 
817d56405f8SMatthew G. Knepley   PetscFunctionBegin;
818cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
8199566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8209566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
8219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
8229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8239566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
8249566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
8259566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
826d56405f8SMatthew G. Knepley   if (createGhostLabel) {
8279566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
8289566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
829d56405f8SMatthew G. Knepley   }
830cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
83148a46eb9SPierre Jolivet     for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
832cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
833cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
8349566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
8351baa6e33SBarry Smith     } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
836cd0c2139SMatthew G Knepley   }
83748a46eb9SPierre Jolivet   for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
838d56405f8SMatthew G. Knepley   if (ghostLabel) {
8399566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
840cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
841cd0c2139SMatthew G Knepley       PetscInt numCells;
842cd0c2139SMatthew G Knepley 
8439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
844cd0c2139SMatthew G Knepley       if (numCells < 2) {
8459566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
846cd0c2139SMatthew G Knepley       } else {
847cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
848cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
849cd0c2139SMatthew G Knepley 
8509566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
8519566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
8529566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
8539566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
854cd0c2139SMatthew G Knepley       }
855cd0c2139SMatthew G Knepley     }
856d56405f8SMatthew G. Knepley   }
8573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
858cd0c2139SMatthew G Knepley }
859cd0c2139SMatthew G Knepley 
860d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
861d71ae5a4SJacob Faibussowitsch {
862ca04dac2SToby Isaac   DM           refTree;
863ca04dac2SToby Isaac   PetscSection pSec;
864ca04dac2SToby Isaac   PetscInt    *parents, *childIDs;
865ca04dac2SToby Isaac 
866ca04dac2SToby Isaac   PetscFunctionBegin;
8679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
8689566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew, refTree));
8699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL));
870ca04dac2SToby Isaac   if (pSec) {
8712582d50cSToby Isaac     PetscInt     p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
872fb4630b5SToby Isaac     PetscInt    *childIDsShifted;
873ca04dac2SToby Isaac     PetscSection pSecShifted;
874ca04dac2SToby Isaac 
8759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd));
8769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
8772582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
8782582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
8799566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted));
8809566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted));
8819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted));
882ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
883fb4630b5SToby Isaac       /* start off assuming no children */
8849566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, p, 0));
885fb4630b5SToby Isaac     }
886fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
887fb4630b5SToby Isaac       PetscInt dof;
888fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
889ca04dac2SToby Isaac 
8909566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
8919566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof));
892ca04dac2SToby Isaac     }
8939566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
894fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
895fb4630b5SToby Isaac       PetscInt dof;
896fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
897fb4630b5SToby Isaac 
8989566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
899fb4630b5SToby Isaac       if (dof) {
900fb4630b5SToby Isaac         PetscInt off, offNew;
901fb4630b5SToby Isaac 
9029566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec, p, &off));
9039566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew));
904fb4630b5SToby Isaac         parentsShifted[offNew]  = DMPlexShiftPoint_Internal(parents[off], depth, depthShift);
905fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
906fb4630b5SToby Isaac       }
907fb4630b5SToby Isaac     }
9089566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted));
9099566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted, childIDsShifted));
9109566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
911ca04dac2SToby Isaac   }
9123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
913ca04dac2SToby Isaac }
914ca04dac2SToby Isaac 
915d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
916d71ae5a4SJacob Faibussowitsch {
917da97024aSMatthew G. Knepley   PetscSF          sf;
918cd0c2139SMatthew G Knepley   IS               valueIS;
919da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
920cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
9212582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
9224fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
923cd0c2139SMatthew G Knepley 
924cd0c2139SMatthew G Knepley   PetscFunctionBegin;
9259566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
9269566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
927da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
9289566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
929cd0c2139SMatthew G Knepley   /* Count ghost cells */
9309566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
9319566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
9329566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
9334a6cfa73SMatthew G. Knepley   Ng = 0;
934cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
93546c796b9SMatthew G. Knepley     IS              faceIS;
93646c796b9SMatthew G. Knepley     const PetscInt *faces;
93746c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
938cd0c2139SMatthew G Knepley 
9399566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9409566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
94246c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
943ca04dac2SToby Isaac       PetscInt numChildren;
944ca04dac2SToby Isaac 
9459566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9469566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
947ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
948ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
94946c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
95046c796b9SMatthew G. Knepley     }
9514a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
9529566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
954cd0c2139SMatthew G Knepley   }
9559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
9569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift));
9572582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
95859eef20bSToby Isaac     PetscInt dEnd;
9592582d50cSToby Isaac 
9609566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd));
96159eef20bSToby Isaac     depthShift[2 * d]     = dEnd;
9622582d50cSToby Isaac     depthShift[2 * d + 1] = 0;
9632582d50cSToby Isaac   }
9642582d50cSToby Isaac   if (depth >= 0) depthShift[2 * depth + 1] = Ng;
9659566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
9669566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
967cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
9689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
96948a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1));
970cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
971cd0c2139SMatthew G Knepley     IS              faceIS;
972cd0c2139SMatthew G Knepley     const PetscInt *faces;
973cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
974cd0c2139SMatthew G Knepley 
9759566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9769566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9779566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
978cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
979ca04dac2SToby Isaac       PetscInt size, numChildren;
980cd0c2139SMatthew G Knepley 
9819566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
983ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
98446c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
98663a3b9bcSJacob Faibussowitsch       PetscCheck(size == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %" PetscInt_FMT " with %" PetscInt_FMT " support cells", faces[f], size);
9879566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
988cd0c2139SMatthew G Knepley     }
9899566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9909566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
991cd0c2139SMatthew G Knepley   }
992cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
9939566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
9949566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
995cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
996cd0c2139SMatthew G Knepley   ghostCell = cEnd;
997cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
998cd0c2139SMatthew G Knepley     IS              faceIS;
999cd0c2139SMatthew G Knepley     const PetscInt *faces;
1000cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
1001cd0c2139SMatthew G Knepley 
10029566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
10039566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
10049566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
100546c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
1006ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
1007cd0c2139SMatthew G Knepley 
10089566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
10099566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
1010ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
101146c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
10129566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
10139566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
101446c796b9SMatthew G. Knepley       ++ghostCell;
1015cd0c2139SMatthew G Knepley     }
10169566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
10179566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
1018cd0c2139SMatthew G Knepley   }
10199566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
10209566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
10219566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
10229566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
10239566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
10249566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
10259566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
10269566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
102748a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
1028966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
10294fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
10304fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L));
10314a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
10323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1033cd0c2139SMatthew G Knepley }
1034cd0c2139SMatthew G Knepley 
10355d83a8b1SBarry Smith /*@
1036cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
1037cd0c2139SMatthew G Knepley 
103820f4b53cSBarry Smith   Collective
1039cd0c2139SMatthew G Knepley 
1040cd0c2139SMatthew G Knepley   Input Parameters:
104120f4b53cSBarry Smith + dm        - The original `DM`
104220f4b53cSBarry Smith - labelName - The label specifying the boundary faces, or "Face Sets" if this is `NULL`
1043cd0c2139SMatthew G Knepley 
1044cd0c2139SMatthew G Knepley   Output Parameters:
104520f4b53cSBarry Smith + numGhostCells - The number of ghost cells added to the `DM`
104620f4b53cSBarry Smith - dmGhosted     - The new `DM`
1047cd0c2139SMatthew G Knepley 
1048cd0c2139SMatthew G Knepley   Level: developer
1049cd0c2139SMatthew G Knepley 
105020f4b53cSBarry Smith   Note:
105120f4b53cSBarry Smith   If no label exists of that name, one will be created marking all boundary faces
105220f4b53cSBarry Smith 
10531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`
105431266bc0SMatthew G. Knepley @*/
1055d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
1056d71ae5a4SJacob Faibussowitsch {
1057cd0c2139SMatthew G Knepley   DM          gdm;
1058cd0c2139SMatthew G Knepley   DMLabel     label;
1059cd0c2139SMatthew G Knepley   const char *name = labelName ? labelName : "Face Sets";
1060412e9a14SMatthew G. Knepley   PetscInt    dim, Ng = 0;
1061b0441da4SMatthew G. Knepley   PetscBool   useCone, useClosure;
1062cd0c2139SMatthew G Knepley 
1063cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1064cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10654f572ea9SToby Isaac   if (numGhostCells) PetscAssertPointer(numGhostCells, 3);
10664f572ea9SToby Isaac   PetscAssertPointer(dmGhosted, 4);
10679566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
10689566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
10699566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
10709566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
10719566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
10729566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure));
10739566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
1074cd0c2139SMatthew G Knepley   if (!label) {
1075cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
10769566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
10779566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
10789566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
1079cd0c2139SMatthew G Knepley   }
10809566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
10819566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
10825de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
1083cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
1084d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
1085cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
10863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1087cd0c2139SMatthew G Knepley }
1088cd0c2139SMatthew G Knepley 
1089d71ae5a4SJacob Faibussowitsch static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue)
1090d71ae5a4SJacob Faibussowitsch {
1091accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
1092accc9626SMatthew G. Knepley 
1093accc9626SMatthew G. Knepley   PetscFunctionBegin;
1094accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1095accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
109648a46eb9SPierre Jolivet   for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
1097accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
1098accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
1099accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
1100accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
1101accc9626SMatthew G. Knepley 
1102accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
1103accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
1104accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1105accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
1106accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1107accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1108accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
1109accc9626SMatthew G. Knepley         PetscInt       clval;
1110accc9626SMatthew G. Knepley 
1111accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
1112accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
1113accc9626SMatthew G. Knepley         if (clval == -1) {
1114accc9626SMatthew G. Knepley           const PetscInt *cone;
1115accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
1116accc9626SMatthew G. Knepley 
1117accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
1118accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
1119accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
1120accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
1121accc9626SMatthew G. Knepley             PetscInt cpval;
1122accc9626SMatthew G. Knepley 
1123accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
1124accc9626SMatthew G. Knepley             if (cpval != -1) {
1125accc9626SMatthew G. Knepley               PetscInt dep;
1126accc9626SMatthew G. Knepley 
1127accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
1128accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift + dep) : shift + dep;
1129accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
1130accc9626SMatthew G. Knepley               break;
1131accc9626SMatthew G. Knepley             }
1132accc9626SMatthew G. Knepley           }
1133accc9626SMatthew G. Knepley         }
1134accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
1135accc9626SMatthew G. Knepley         if (d == dim - 1 && clval != -1) {
1136accc9626SMatthew G. Knepley           const PetscInt *support;
1137accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
1138accc9626SMatthew G. Knepley 
1139accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
1140accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
1141accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
1142accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
1143accc9626SMatthew G. Knepley             if (nval == -1) {
1144accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1));
1145accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1146accc9626SMatthew G. Knepley             }
1147accc9626SMatthew G. Knepley           }
1148accc9626SMatthew G. Knepley         }
1149accc9626SMatthew G. Knepley       }
1150accc9626SMatthew G. Knepley     }
1151accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1152accc9626SMatthew G. Knepley   }
1153accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
11543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1155accc9626SMatthew G. Knepley }
1156accc9626SMatthew G. Knepley 
1157accc9626SMatthew G. Knepley typedef struct {
1158accc9626SMatthew G. Knepley   DM               dm;
1159accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1160accc9626SMatthew G. Knepley } PointDivision;
1161accc9626SMatthew G. Knepley 
1162d71ae5a4SJacob Faibussowitsch static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx)
1163d71ae5a4SJacob Faibussowitsch {
1164accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *)ctx;
1165accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val - 1 : val + 1;
1166accc9626SMatthew G. Knepley   const PetscInt *support;
1167accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1168accc9626SMatthew G. Knepley 
1169accc9626SMatthew G. Knepley   PetscFunctionBegin;
1170accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1171accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1172accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1173accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1174accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1175accc9626SMatthew G. Knepley   }
11763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1177accc9626SMatthew G. Knepley }
1178accc9626SMatthew G. Knepley 
1179accc9626SMatthew G. Knepley /* Mark cells by label propagation */
1180d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel)
1181d71ae5a4SJacob Faibussowitsch {
1182accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1183accc9626SMatthew G. Knepley   PointDivision    div;
1184accc9626SMatthew G. Knepley   PetscSF          pointSF;
1185accc9626SMatthew G. Knepley   IS               pointIS;
1186accc9626SMatthew G. Knepley   const PetscInt  *points;
1187accc9626SMatthew G. Knepley   PetscBool        empty;
1188accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1189accc9626SMatthew G. Knepley 
1190accc9626SMatthew G. Knepley   PetscFunctionBegin;
1191accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1192accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1193accc9626SMatthew G. Knepley   div.dm    = dm;
1194accc9626SMatthew G. Knepley   div.queue = queue;
1195accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1196accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS));
1197accc9626SMatthew G. Knepley   if (pointIS) {
1198accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1199accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
120048a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1201accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1202accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1203accc9626SMatthew G. Knepley   }
1204accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS));
1205accc9626SMatthew G. Knepley   if (pointIS) {
1206accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1207accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
120848a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1209accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1210accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1211accc9626SMatthew G. Knepley   }
1212accc9626SMatthew G. Knepley 
1213accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1214accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1215accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1216accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1217accc9626SMatthew G. Knepley   while (!empty) {
1218accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1219accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1220accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1221accc9626SMatthew G. Knepley   }
1222accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1223accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
12243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1225accc9626SMatthew G. Knepley }
1226accc9626SMatthew G. Knepley 
1227607ab7a9SMatthew G. Knepley /*
1228faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1229607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1230faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1231b6dfa339SMatthew G. Knepley     Ghost:          These are shared fault faces which are not owned by this process. These do not produce hybrid cells and do not replicate
1232607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1233a6ae58d1SMatthew G. Knepley 
1234a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1235a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1236607ab7a9SMatthew G. Knepley */
1237d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1238d71ae5a4SJacob Faibussowitsch {
1239cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1240607ab7a9SMatthew G. Knepley   IS               valueIS;
1241607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0; /* The number of depths for which we have replicated points */
1242607ab7a9SMatthew G. Knepley   const PetscInt  *values;    /* List of depths for which we have replicated points */
124318c5995bSMatthew G. Knepley   IS              *splitIS;
124418c5995bSMatthew G. Knepley   IS              *unsplitIS;
1245b6dfa339SMatthew G. Knepley   IS               ghostIS;
1246607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
124718c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
124836dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
124936dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1250b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1251607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
125218c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1253b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1254cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1255cd0c2139SMatthew G Knepley   Vec              coordinates;
1256cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1257a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;   /* The first hybrid point at each depth in the original mesh */
1258a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;   /* The point limit at each depth in the original mesh */
1259607ab7a9SMatthew G. Knepley   PetscInt        *depthShift; /* Number of replicated+hybrid points at each depth */
1260607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;    /* The first replicated point at each depth in the new mesh, hybrids come after this */
1261607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
126218c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1263cd0c2139SMatthew G Knepley 
1264cd0c2139SMatthew G Knepley   PetscFunctionBegin;
12659566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
12669566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
12679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
12689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1269412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
12709566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1271cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
12729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
12739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld));
12749566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints));
1275607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
12769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
12779566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1278a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1279a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1280607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
128118c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1282607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1283a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1284607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
128518c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
128618c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
128718c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
128859eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
128959eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
129059eef20bSToby Isaac     depthShift[2 * d]     = depthMax[d];
129159eef20bSToby Isaac     depthShift[2 * d + 1] = 0;
1292607ab7a9SMatthew G. Knepley   }
1293b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1294b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1295cd0c2139SMatthew G Knepley   if (label) {
12969566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
12979566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
12989566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1299cd0c2139SMatthew G Knepley   }
1300cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1301cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1302cd0c2139SMatthew G Knepley 
1303cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
13049566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
130518c5995bSMatthew G. Knepley     if (splitIS[dep]) {
13069566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
13079566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
130818c5995bSMatthew G. Knepley     }
13099566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep]));
131018c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
13119566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
13129566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1313cd0c2139SMatthew G Knepley     }
1314cd0c2139SMatthew G Knepley   }
1315b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS));
1316b6dfa339SMatthew G. Knepley   if (ghostIS) {
1317b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1318b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1319b6dfa339SMatthew G. Knepley   }
1320607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
132118c5995bSMatthew G. Knepley   for (d = 1; d <= depth; ++d) numHybridPoints[d] = numSplitPoints[d - 1] + numUnsplitPoints[d - 1]; /* There is a hybrid cell/face/edge for every split face/edge/vertex   */
13222582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d];
13239566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
132459eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
132559eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
132659eef20bSToby Isaac    * added points */
13272582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
13289566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1329cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1330cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1331cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1332cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
13332582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
13344c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1335cd0c2139SMatthew G Knepley       const PetscInt *support;
1336394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
13374c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1338cd0c2139SMatthew G Knepley 
13399566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13409566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
13419566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13429566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
13439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
13449566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1345cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
13464c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13474c367dbcSMatthew G. Knepley 
1348cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
13499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1350412e9a14SMatthew G. Knepley         switch (coneSize) {
1351d71ae5a4SJacob Faibussowitsch         case 2:
1352d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));
1353d71ae5a4SJacob Faibussowitsch           break;
1354d71ae5a4SJacob Faibussowitsch         case 3:
1355d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));
1356d71ae5a4SJacob Faibussowitsch           break;
1357d71ae5a4SJacob Faibussowitsch         case 4:
1358d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));
1359d71ae5a4SJacob Faibussowitsch           break;
1360412e9a14SMatthew G. Knepley         }
1361b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1362b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1363b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1364b6dfa339SMatthew G. Knepley           const PetscInt *support;
1365b6dfa339SMatthew G. Knepley           PetscInt        val;
1366b6dfa339SMatthew G. Knepley 
1367b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1368b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1369b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1370b6dfa339SMatthew G. Knepley           else PetscCall(DMPlexSetSupportSize(sdm, newp, 2));
1371b6dfa339SMatthew G. Knepley         }
1372cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13734c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1374cd0c2139SMatthew G Knepley 
13759566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13764c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1377cd0c2139SMatthew G Knepley           PetscInt val;
1378cd0c2139SMatthew G Knepley 
13799566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13804c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
13814c367dbcSMatthew G. Knepley           if ((val == 1) || (val == (shift + 1))) ++qn;
13824c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1383cd0c2139SMatthew G Knepley         }
13844c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
13859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13864c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
13879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13884c367dbcSMatthew G. Knepley         /* Add hybrid edge */
13899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
13909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1392cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
13934c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13944c367dbcSMatthew G. Knepley 
13959566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13964c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1397cd0c2139SMatthew G Knepley           PetscInt val;
1398cd0c2139SMatthew G Knepley 
13999566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
14004c367dbcSMatthew G. Knepley           if (val == dim - 1) ++qf;
14014c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn;
14024c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp;
1403cd0c2139SMatthew G Knepley         }
14044c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
14059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
14064c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
14079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
14084c367dbcSMatthew G. Knepley         /* Add hybrid face */
14099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1412cd0c2139SMatthew G Knepley       }
1413cd0c2139SMatthew G Knepley     }
1414cd0c2139SMatthew G Knepley   }
141518c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
141618c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
141718c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
14182582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
141918c5995bSMatthew G. Knepley       const PetscInt *support;
1420da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
142118c5995bSMatthew G. Knepley 
14229566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14239566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14249566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
142518c5995bSMatthew G. Knepley       if (dep == 0) {
142618c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
142718c5995bSMatthew G. Knepley 
142839254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
142939254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
14309566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
143139254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
143239254ff6SMatthew G. Knepley         }
14339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
143418c5995bSMatthew G. Knepley         /* Add hybrid edge */
14359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1436e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1437e1757548SMatthew G. Knepley           PetscInt val;
1438e1757548SMatthew G. Knepley 
14399566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1440e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1441da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1442da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1443e1757548SMatthew G. Knepley         }
14449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
14459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
144618c5995bSMatthew G. Knepley       } else if (dep == dim - 2) {
144718c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1448cd0c2139SMatthew G Knepley         PetscInt       val;
1449cd0c2139SMatthew G Knepley 
1450da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14519566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1452da1dd7e4SMatthew G. Knepley           if (val == dim - 1) qf += 2;
1453da1dd7e4SMatthew G. Knepley           else ++qf;
1454cd0c2139SMatthew G Knepley         }
145518c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
14569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
145718c5995bSMatthew G. Knepley         /* Add hybrid face */
1458da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14599566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1460da1dd7e4SMatthew G. Knepley           if (val == dim - 1) ++qf;
1461da1dd7e4SMatthew G. Knepley         }
14629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1465cd0c2139SMatthew G Knepley       }
1466cd0c2139SMatthew G Knepley     }
1467cd0c2139SMatthew G Knepley   }
1468cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
14699566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
14709566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
14719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
14729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew));
1473cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1474cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1475cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1476cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
14772582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
14784c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1479cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1480b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
14814c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1482cd0c2139SMatthew G Knepley 
14839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
14849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
14869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
14879566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14889566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1489cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
149096a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
14914c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1492cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1493cd0c2139SMatthew G Knepley 
1494b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1495cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
14969566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp, &supportF));
14979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1498cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1499cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1500cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
15019566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
150218c5995bSMatthew G. Knepley           if (v < 0) {
15039566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
150463a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1);
15052582d50cSToby Isaac             coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
150696a07cd0SMatthew G. Knepley             hasUnsplit     = PETSC_TRUE;
150718c5995bSMatthew G. Knepley           } else {
15084c367dbcSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep - 1];
1509163235baSMatthew G. Knepley             if (dep > 1) {
1510163235baSMatthew G. Knepley               const PetscInt *econe;
1511163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1512163235baSMatthew G. Knepley 
15139566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
15149566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1515163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
15169566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs));
15179566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu));
1518163235baSMatthew G. Knepley                 if (vs >= 0) continue;
151963a3b9bcSJacob Faibussowitsch                 PetscCheck(vu >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, econe[r], dep - 2);
1520163235baSMatthew G. Knepley                 hasUnsplit = PETSC_TRUE;
1521163235baSMatthew G. Knepley               }
1522163235baSMatthew G. Knepley             }
1523cd0c2139SMatthew G Knepley           }
1524cd0c2139SMatthew G Knepley         }
15259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
15269566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1527e537020bSMatthew G. Knepley         /* Face support */
1528b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1529cd0c2139SMatthew G Knepley 
1530b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1531b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1532b6dfa339SMatthew G. Knepley         else vals[1] = -vals[0];
1533b6dfa339SMatthew G. Knepley         PetscCheck(vals[0] * vals[1] < 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid support labels %" PetscInt_FMT " %" PetscInt_FMT, vals[0], vals[1]);
1534b6dfa339SMatthew G. Knepley 
1535b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1536b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1537b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1538b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1539accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1540b6dfa339SMatthew G. Knepley             } else {
1541b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1542accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1543b6dfa339SMatthew G. Knepley             }
1544b6dfa339SMatthew G. Knepley           } else {
1545b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1546cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
15479566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1548cd0c2139SMatthew G Knepley             } else {
1549cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
15509566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1551b6dfa339SMatthew G. Knepley             }
1552b6dfa339SMatthew G. Knepley           }
1553b6dfa339SMatthew G. Knepley         }
1554b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1555e537020bSMatthew G. Knepley         {
1556e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1557b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1558accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1559e537020bSMatthew G. Knepley 
15609371c9d4SSatish Balay           if (supportSize > 1) {
15619371c9d4SSatish Balay             ocell = vals[0] < 0 ? support[1] : support[0];
15629371c9d4SSatish Balay           } else {
15639371c9d4SSatish Balay             ocell = support[0];
15649371c9d4SSatish Balay             flip  = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;
15659371c9d4SSatish Balay           }
1566b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1567b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1568e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1569e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1570e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1571b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc];
1572e537020bSMatthew G. Knepley               break;
1573cd0c2139SMatthew G Knepley             }
1574cd0c2139SMatthew G Knepley           }
1575b6dfa339SMatthew G. Knepley           PetscCheck(nc < nconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %" PetscInt_FMT " in neighboring cell %" PetscInt_FMT, oldp, ocell);
1576e537020bSMatthew G. Knepley         }
15774c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1578b6dfa339SMatthew G. Knepley         {
157985036b15SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangements(ct) / 2;
1580b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1581b6dfa339SMatthew G. Knepley         }
158285036b15SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, coneONew[0]);
1583b5a892a1SMatthew G. Knepley 
1584fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp; /* Extracted negative side orientation above */
15854c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
15864c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1587e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1588412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1589b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q * 2 + 0];
1590b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q * 2 + 1];
1591b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1592412e9a14SMatthew G. Knepley 
15939566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
159418c5995bSMatthew G. Knepley           if (v < 0) {
15959566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
159618c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
159718c5995bSMatthew G. Knepley           } else {
159818c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep];
159918c5995bSMatthew G. Knepley           }
1600b5a892a1SMatthew G. Knepley           coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1601e537020bSMatthew G. Knepley         }
16029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
16039566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
160496a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
16059566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1606cd0c2139SMatthew G Knepley       } else if (dep == 0) {
16074c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1608cd0c2139SMatthew G Knepley 
1609cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
16104c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1611cd0c2139SMatthew G Knepley           PetscInt val;
1612cd0c2139SMatthew G Knepley 
16139566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1614ad540459SPierre Jolivet           if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1615cd0c2139SMatthew G Knepley         }
16164c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
16179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1618cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
16194c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1620cd0c2139SMatthew G Knepley           PetscInt val, edge;
1621cd0c2139SMatthew G Knepley 
16229566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1623cd0c2139SMatthew G Knepley           if (val == 1) {
16249566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
162563a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16264c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep + 1];
1627cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
16282582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1629cd0c2139SMatthew G Knepley           }
1630cd0c2139SMatthew G Knepley         }
16314c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
16329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16334c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1634cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1635cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
16369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
16374c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
16384c367dbcSMatthew G. Knepley           PetscInt val, edge;
16394c367dbcSMatthew G. Knepley 
16409566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16414c367dbcSMatthew G. Knepley           if (val == 1) {
16429566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
164363a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16444c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
16454c367dbcSMatthew G. Knepley           }
16464c367dbcSMatthew G. Knepley         }
16479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1648cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
16494c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16504c367dbcSMatthew G. Knepley 
1651cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1652cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1653cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
16549566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
1655e1757548SMatthew G. Knepley           if (v < 0) {
16569566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
165763a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1);
16582582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1659e1757548SMatthew G. Knepley           } else {
16604c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep - 1];
1661cd0c2139SMatthew G Knepley           }
1662e1757548SMatthew G. Knepley         }
16639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1664cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1665cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1666cd0c2139SMatthew G Knepley           PetscInt val;
1667cd0c2139SMatthew G Knepley 
16689566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16694c367dbcSMatthew G. Knepley           if (val == dim - 1) {
16702582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
16714c367dbcSMatthew G. Knepley           } else if (val == (shift + dim - 1)) {
16722582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1673cd0c2139SMatthew G Knepley           }
1674cd0c2139SMatthew G Knepley         }
1675b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1677cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1678cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1679cd0c2139SMatthew G Knepley           PetscInt val, face;
1680cd0c2139SMatthew G Knepley 
16819566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1682cd0c2139SMatthew G Knepley           if (val == dim - 1) {
16839566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
168463a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
16854c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep + 1];
1686cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim - 1)) {
16872582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1688cd0c2139SMatthew G Knepley           }
1689cd0c2139SMatthew G Knepley         }
1690b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16924c367dbcSMatthew G. Knepley         /* Hybrid face */
16934c367dbcSMatthew G. Knepley         coneNew[0] = newp;
16944c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
16954c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
16964c367dbcSMatthew G. Knepley           PetscInt vertex;
16979566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex));
1698e1757548SMatthew G. Knepley           if (vertex < 0) {
16999566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex));
170063a3b9bcSJacob Faibussowitsch             PetscCheck(vertex >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[v], dep - 1);
1701e1757548SMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
1702e1757548SMatthew G. Knepley           } else {
17034c367dbcSMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
17044c367dbcSMatthew G. Knepley           }
1705e1757548SMatthew G. Knepley         }
17069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
17074c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
17084c367dbcSMatthew G. Knepley           PetscInt val, face;
17094c367dbcSMatthew G. Knepley 
17109566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
17114c367dbcSMatthew G. Knepley           if (val == dim - 1) {
17129566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
171363a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
17144c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
17154c367dbcSMatthew G. Knepley           }
17164c367dbcSMatthew G. Knepley         }
17179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1718cd0c2139SMatthew G Knepley       }
1719cd0c2139SMatthew G Knepley     }
1720cd0c2139SMatthew G Knepley   }
172118c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
172218c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
172318c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
17242582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1725b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1726e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
172718c5995bSMatthew G. Knepley 
17289566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
17299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
17309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
17319566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
173218c5995bSMatthew G. Knepley       if (dep == 0) {
173318c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
173418c5995bSMatthew G. Knepley 
173518c5995bSMatthew G. Knepley         /* Unsplit vertex */
17369566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
173718c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
17382582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
17399566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
1740ad540459SPierre Jolivet           if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1];
174118c5995bSMatthew G. Knepley         }
174218c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174318c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174463a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
17459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
174618c5995bSMatthew G. Knepley         /* Hybrid edge */
174718c5995bSMatthew G. Knepley         coneNew[0] = newp;
174818c5995bSMatthew G. Knepley         coneNew[1] = newp;
17499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
175018c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
175118c5995bSMatthew G. Knepley           PetscInt val, edge;
175218c5995bSMatthew G. Knepley 
17539566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
175418c5995bSMatthew G. Knepley           if (val == 1) {
17559566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
175663a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
175718c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1758e1757548SMatthew G. Knepley           } else if (val == (shift2 + 1)) {
17599566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge));
176063a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1761e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1];
176218c5995bSMatthew G. Knepley           }
176318c5995bSMatthew G. Knepley         }
17649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1765e1757548SMatthew G. Knepley       } else if (dep == dim - 2) {
1766e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1767e1757548SMatthew G. Knepley 
1768da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1769e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1770e1757548SMatthew G. Knepley           PetscInt val, face;
1771e1757548SMatthew G. Knepley 
17729566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1773e1757548SMatthew G. Knepley           if (val == dim - 1) {
17749566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
177563a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
17762582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1777e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 1];
1778e1757548SMatthew G. Knepley           } else {
17792582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1780e1757548SMatthew G. Knepley           }
1781e1757548SMatthew G. Knepley         }
1782e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1783e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
17849566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
178563a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, newp, qf, supportSizeNew);
17869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1787e1757548SMatthew G. Knepley         /* Add hybrid face */
1788e1757548SMatthew G. Knepley         coneNew[0] = newp;
1789212cc919SMatthew G. Knepley         coneNew[1] = newp;
17909566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
179163a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1792212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17939566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
179463a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]);
1795e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1797da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1798da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1799da1dd7e4SMatthew G. Knepley 
18009566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1801da1dd7e4SMatthew G. Knepley           if (val == dim - 1) {
18029566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
1803da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1804da1dd7e4SMatthew G. Knepley           }
1805da1dd7e4SMatthew G. Knepley         }
18069566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
180763a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, hybface, qf, supportSizeNew);
18089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1809cd0c2139SMatthew G Knepley       }
1810cd0c2139SMatthew G Knepley     }
1811cd0c2139SMatthew G Knepley   }
1812cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1813cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1814cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1815cd0c2139SMatthew G Knepley     IS              pIS;
1816cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1817cd0c2139SMatthew G Knepley     const PetscInt *points;
1818cd0c2139SMatthew G Knepley 
1819cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
18209566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1821cd0c2139SMatthew G Knepley     if (!pIS) continue;
1822cd0c2139SMatthew G Knepley     dep = -dep - shift;
18239566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
18249566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1825cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1826cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
18272582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1828cd0c2139SMatthew G Knepley       const PetscInt *cone;
1829cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
183050cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1831cd0c2139SMatthew G Knepley 
1832cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1833cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
18349566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
18359566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1836cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1837e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift);
1838cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1839cd0c2139SMatthew G Knepley 
18409566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1841cd0c2139SMatthew G Knepley         if (val == dep - 1) {
18429566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp));
184363a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1);
1844cd0c2139SMatthew G Knepley           csplitp = pMaxNew[dep - 1] + cp;
18459566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
184650cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1847cd0c2139SMatthew G Knepley         }
1848cd0c2139SMatthew G Knepley       }
18494a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
185028b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1851cd0c2139SMatthew G Knepley     }
18529566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
18539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1854cd0c2139SMatthew G Knepley   }
18550e18dc48SMatthew G. Knepley   PetscCall(DMPlexReorderCohesiveSupports(sdm));
1856fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
18579566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
18589566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
18599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
18609566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1861cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
18622582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1863cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1864cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1865cd0c2139SMatthew G Knepley 
18669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
18679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
18689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1869cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d];
1870cd0c2139SMatthew G Knepley   }
18719566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1872fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
18739566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1874b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1875fa8e8ae5SToby Isaac   /* Step 9: Labels */
18769566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
18779566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
18789566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1879cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1880cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
18812582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1882cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1883cd0c2139SMatthew G Knepley       PetscInt       l;
1884cd0c2139SMatthew G Knepley 
18857db7e0a7SMatthew G. Knepley       if (splitLabel) {
18867db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
18877db7e0a7SMatthew G. Knepley 
18889566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp, val));
18899566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
18907db7e0a7SMatthew G. Knepley       }
1891cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1892cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1893cd0c2139SMatthew G Knepley         const char *lname;
1894cd0c2139SMatthew G Knepley         PetscInt    val;
18959a356370SMatthew G. Knepley         PetscBool   isDepth;
1896cd0c2139SMatthew G Knepley 
18979566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
18989566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
18999a356370SMatthew G. Knepley         if (isDepth) continue;
19009566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
19019566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
190248a46eb9SPierre Jolivet         if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val));
1903cd0c2139SMatthew G Knepley       }
1904cd0c2139SMatthew G Knepley     }
1905cd0c2139SMatthew G Knepley   }
1906cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1907cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1908cd0c2139SMatthew G Knepley 
1909cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
19109566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
19119566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
19129566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
19139566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1914cd0c2139SMatthew G Knepley   }
1915b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1916b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1917cd0c2139SMatthew G Knepley   if (label) {
19189566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
19199566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1920cd0c2139SMatthew G Knepley   }
19210d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
19229566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
192336dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
19240d4d4d06SMatthew G. Knepley   }
19259566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
19269566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
19279566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
19283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1929cd0c2139SMatthew G Knepley }
1930cd0c2139SMatthew G Knepley 
1931cd0c2139SMatthew G Knepley /*@C
1932cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1933cd0c2139SMatthew G Knepley 
193420f4b53cSBarry Smith   Collective
1935cd0c2139SMatthew G Knepley 
1936cd0c2139SMatthew G Knepley   Input Parameters:
193720f4b53cSBarry Smith + dm    - The original `DM`
193820f4b53cSBarry Smith - label - The `DMLabel` specifying the boundary faces (this could be auto-generated)
1939cd0c2139SMatthew G Knepley 
1940cd0c2139SMatthew G Knepley   Output Parameters:
194120f4b53cSBarry Smith + splitLabel - The `DMLabel` containing the split points, or `NULL` if no output is desired
194220f4b53cSBarry Smith - dmSplit    - The new `DM`
1943cd0c2139SMatthew G Knepley 
1944cd0c2139SMatthew G Knepley   Level: developer
1945cd0c2139SMatthew G Knepley 
19461cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1947cd0c2139SMatthew G Knepley @*/
1948d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1949d71ae5a4SJacob Faibussowitsch {
1950cd0c2139SMatthew G Knepley   DM       sdm;
1951cd0c2139SMatthew G Knepley   PetscInt dim;
1952cd0c2139SMatthew G Knepley 
1953cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1954cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19554f572ea9SToby Isaac   PetscAssertPointer(dmSplit, 4);
19569566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
19579566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
19589566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19599566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1960cd0c2139SMatthew G Knepley   switch (dim) {
1961cd0c2139SMatthew G Knepley   case 2:
1962d71ae5a4SJacob Faibussowitsch   case 3:
1963d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1964d71ae5a4SJacob Faibussowitsch     break;
1965d71ae5a4SJacob Faibussowitsch   default:
1966d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1967cd0c2139SMatthew G Knepley   }
1968cd0c2139SMatthew G Knepley   *dmSplit = sdm;
19693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1970cd0c2139SMatthew G Knepley }
1971cd0c2139SMatthew G Knepley 
19720f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
1973d71ae5a4SJacob Faibussowitsch static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1974d71ae5a4SJacob Faibussowitsch {
19750f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
19760f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
19770f66a230SMatthew G. Knepley 
19780f66a230SMatthew G. Knepley   PetscFunctionBegin;
19790f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
19809566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19829566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
19839566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
19840f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
19850f66a230SMatthew G. Knepley     if (cone[c] == face) {
19860f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
19870f66a230SMatthew G. Knepley 
19880f66a230SMatthew G. Knepley       if (subdm) {
19890f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
19900f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
19910f66a230SMatthew G. Knepley 
19929566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
19939566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
19949566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
19959566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
19969566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
19970f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
19980f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
19990f66a230SMatthew G. Knepley             o = subornt[0];
20000f66a230SMatthew G. Knepley             break;
20010f66a230SMatthew G. Knepley           }
20020f66a230SMatthew G. Knepley         }
200363a3b9bcSJacob Faibussowitsch         PetscCheck(sc < subconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %" PetscInt_FMT " (%" PetscInt_FMT ") in cone for subpoint %" PetscInt_FMT " (%" PetscInt_FMT ")", subface, face, subpoint, cell);
20040f66a230SMatthew G. Knepley       }
20050f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
20060f66a230SMatthew G. Knepley       else *pos = PETSC_FALSE;
20070f66a230SMatthew G. Knepley       break;
20080f66a230SMatthew G. Knepley     }
20090f66a230SMatthew G. Knepley   }
201063a3b9bcSJacob Faibussowitsch   PetscCheck(c != coneSize, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " in split face %" PetscInt_FMT " support does not have it in the cone", cell, face);
20113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20120f66a230SMatthew G. Knepley }
20130f66a230SMatthew G. Knepley 
20140542aa8cSMatthew G. Knepley static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label, PetscBool split)
2015d71ae5a4SJacob Faibussowitsch {
2016accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
2017accc9626SMatthew G. Knepley   const PetscInt *points;
20180542aa8cSMatthew G. Knepley   PetscInt       *closure = NULL, *inclosure = NULL;
20190542aa8cSMatthew G. Knepley   PetscInt        dim, numPoints, shift = 100, shift2 = 200, debug = 0;
2020accc9626SMatthew G. Knepley 
2021accc9626SMatthew G. Knepley   PetscFunctionBegin;
2022accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
20230542aa8cSMatthew G. Knepley   // If any faces touching the fault divide cells on either side,
20240542aa8cSMatthew G. Knepley   //   either split them, or unsplit the connection
2025accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
2026accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
2027accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
2028accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
2029accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
20303ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2031accc9626SMatthew G. Knepley   }
2032accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
2033accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
2034accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
2035accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
2036accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
20370542aa8cSMatthew G. Knepley   for (PetscInt p = 0; p < numPoints; ++p) {
2038accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
2039accc9626SMatthew G. Knepley     const PetscInt *support;
2040accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
2041accc9626SMatthew G. Knepley 
2042accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
2043accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
2044accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
2045accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
2046accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
2047accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
2048accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
20490542aa8cSMatthew G. Knepley     // Check that this face is not incident on only unsplit faces,
20500542aa8cSMatthew G. Knepley     //   meaning has at least one split face
2051e1a13daeSMatthew G. Knepley     {
2052e1a13daeSMatthew G. Knepley       PetscBool split = PETSC_FALSE;
20530542aa8cSMatthew G. Knepley       PetscInt  Ncl, val;
2054e1a13daeSMatthew G. Knepley 
20550542aa8cSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &Ncl, &closure));
20560542aa8cSMatthew G. Knepley       for (PetscInt cl = 0; cl < Ncl * 2; cl += 2) {
20570542aa8cSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &val));
20580542aa8cSMatthew G. Knepley         if ((val >= 0) && (val <= dim)) {
20599371c9d4SSatish Balay           split = PETSC_TRUE;
20609371c9d4SSatish Balay           break;
20619371c9d4SSatish Balay         }
2062e1a13daeSMatthew G. Knepley       }
2063e1a13daeSMatthew G. Knepley       if (!split) continue;
2064e1a13daeSMatthew G. Knepley     }
20650542aa8cSMatthew G. Knepley     if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Point %" PetscInt_FMT " is impinging (%" PetscInt_FMT ":%" PetscInt_FMT ", %" PetscInt_FMT ":%" PetscInt_FMT ")\n", point, support[0], valA, support[1], valB));
20660542aa8cSMatthew G. Knepley     if (split) {
20670542aa8cSMatthew G. Knepley       // Split the face
2068accc9626SMatthew G. Knepley       PetscCall(DMLabelGetValue(label, point, &valA));
2069accc9626SMatthew G. Knepley       PetscCall(DMLabelClearValue(label, point, valA));
2070accc9626SMatthew G. Knepley       PetscCall(DMLabelSetValue(label, point, dim - 1));
2071accc9626SMatthew G. Knepley       /* Label its closure:
2072accc9626SMatthew G. Knepley         unmarked: label as unsplit
2073accc9626SMatthew G. Knepley         incident: relabel as split
20740542aa8cSMatthew G. Knepley         split:    do nothing */
2075accc9626SMatthew G. Knepley       {
2076accc9626SMatthew G. Knepley         PetscInt closureSize, cl, dep;
2077accc9626SMatthew G. Knepley 
2078accc9626SMatthew G. Knepley         PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2079accc9626SMatthew G. Knepley         for (cl = 0; cl < closureSize * 2; cl += 2) {
2080accc9626SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, closure[cl], &valA));
2081accc9626SMatthew G. Knepley           if (valA == -1) { /* Mark as unsplit */
2082accc9626SMatthew G. Knepley             PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2083accc9626SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
2084accc9626SMatthew G. Knepley           } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2085accc9626SMatthew G. Knepley             PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2086accc9626SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, closure[cl], valA));
2087accc9626SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, closure[cl], dep));
2088accc9626SMatthew G. Knepley           }
2089accc9626SMatthew G. Knepley         }
20900542aa8cSMatthew G. Knepley       }
20910542aa8cSMatthew G. Knepley     } else {
20920542aa8cSMatthew G. Knepley       // Unsplit the incident faces and their closures
20930542aa8cSMatthew G. Knepley       PetscInt Ncl, dep, val;
20940542aa8cSMatthew G. Knepley 
20950542aa8cSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &Ncl, &closure));
20960542aa8cSMatthew G. Knepley       for (PetscInt cl = 0; cl < Ncl * 2; cl += 2) {
20970542aa8cSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &val));
20980542aa8cSMatthew G. Knepley         if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Point %" PetscInt_FMT ":%" PetscInt_FMT "\n", closure[cl], val));
20990542aa8cSMatthew G. Knepley         if ((val >= 0) && (val <= dim)) {
21000542aa8cSMatthew G. Knepley           PetscInt Nincl, inval, indep;
21010542aa8cSMatthew G. Knepley 
21020542aa8cSMatthew G. Knepley           if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Point %" PetscInt_FMT " is being unsplit\n", closure[cl]));
21030542aa8cSMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
21040542aa8cSMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], val));
21050542aa8cSMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
21060542aa8cSMatthew G. Knepley 
21070542aa8cSMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, closure[cl], PETSC_TRUE, &Nincl, &inclosure));
21080542aa8cSMatthew G. Knepley           for (PetscInt incl = 0; incl < Nincl * 2; incl += 2) {
21090542aa8cSMatthew G. Knepley             PetscCall(DMLabelGetValue(label, inclosure[cl], &inval));
21100542aa8cSMatthew G. Knepley             if ((inval >= 0) && (inval <= dim)) {
21110542aa8cSMatthew G. Knepley               if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "    Point %" PetscInt_FMT " is being unsplit\n", inclosure[incl]));
21120542aa8cSMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, inclosure[incl], &indep));
21130542aa8cSMatthew G. Knepley               PetscCall(DMLabelClearValue(label, inclosure[incl], inval));
21140542aa8cSMatthew G. Knepley               PetscCall(DMLabelSetValue(label, inclosure[incl], shift2 + indep));
2115accc9626SMatthew G. Knepley             }
2116accc9626SMatthew G. Knepley           }
21170542aa8cSMatthew G. Knepley         }
21180542aa8cSMatthew G. Knepley       }
21190542aa8cSMatthew G. Knepley     }
21200542aa8cSMatthew G. Knepley   }
21210542aa8cSMatthew G. Knepley   PetscCall(DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &inclosure));
21220542aa8cSMatthew G. Knepley   PetscCall(DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure));
2123accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2124accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
21253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2126accc9626SMatthew G. Knepley }
2127accc9626SMatthew G. Knepley 
2128cd0c2139SMatthew G Knepley /*@
21290f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
2130cd0c2139SMatthew G Knepley   to complete the surface
2131cd0c2139SMatthew G Knepley 
2132cd0c2139SMatthew G Knepley   Input Parameters:
213320f4b53cSBarry Smith + dm     - The `DM`
213420f4b53cSBarry Smith . label  - A `DMLabel` marking the surface
213520f4b53cSBarry Smith . blabel - A `DMLabel` marking the vertices on the boundary which will not be duplicated, or `NULL` to find them automatically
213620f4b53cSBarry Smith . bvalue - Value of `DMLabel` marking the vertices on the boundary
2137bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
21380542aa8cSMatthew G. Knepley . split  - Split faces impinging on the surface, rather than clamping the surface boundary
213920f4b53cSBarry Smith - subdm  - The `DM` associated with the label, or `NULL`
2140cd0c2139SMatthew G Knepley 
2141cd0c2139SMatthew G Knepley   Output Parameter:
214220f4b53cSBarry Smith . label - A `DMLabel` marking all surface points
21430f66a230SMatthew G. Knepley 
2144cd0c2139SMatthew G Knepley   Level: developer
2145cd0c2139SMatthew G Knepley 
214620f4b53cSBarry Smith   Note:
214720f4b53cSBarry Smith   The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
214820f4b53cSBarry Smith 
21491cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2150cd0c2139SMatthew G Knepley @*/
21510542aa8cSMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, PetscBool split, DM subdm)
2152d71ae5a4SJacob Faibussowitsch {
2153d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2154accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
215547946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2156bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
21570542aa8cSMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = split ? 300 : 0, dim, depth, numPoints, numSubpoints, p, val;
2158cd0c2139SMatthew G Knepley 
2159cd0c2139SMatthew G Knepley   PetscFunctionBegin;
21609566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
21619566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
21629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
216347946fd8SMatthew G. Knepley   if (subdm) {
21649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
216547946fd8SMatthew G. Knepley     if (subpointIS) {
21669566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
21679566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
216847946fd8SMatthew G. Knepley     }
216947946fd8SMatthew G. Knepley   }
2170d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
21719566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2172accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
21739566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
21749566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2175d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2176cd0c2139SMatthew G Knepley     const PetscInt *support;
2177cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2178cd0c2139SMatthew G Knepley 
21799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2180c4419245SMatthew G. Knepley #if 0
2181c4419245SMatthew G. Knepley     if (supportSize != 2) {
2182c4419245SMatthew G. Knepley       const PetscInt *lp;
2183c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2184c4419245SMatthew G. Knepley 
2185c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2186c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
21879566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
21889566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
218963a3b9bcSJacob Faibussowitsch       PetscCheck(pind >= 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %" PetscInt_FMT " has %" PetscInt_FMT " != 2 supports, and the face is not shared with another process", points[p], supportSize);
2190c4419245SMatthew G. Knepley     }
2191c4419245SMatthew G. Knepley #endif
21929566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2193cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
21940f66a230SMatthew G. Knepley       const PetscInt *cone;
2195cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
21960f66a230SMatthew G. Knepley       PetscBool       pos;
2197cd0c2139SMatthew G Knepley 
21989566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
21999566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
22009566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
22010f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
22020f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
22039566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
22049566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2205cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2206cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2207cd0c2139SMatthew G Knepley 
22089566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2209cd0c2139SMatthew G Knepley         if (val == -1) {
2210cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2211cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2212cd0c2139SMatthew G Knepley 
22139566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2214cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2215cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2216a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2217cd0c2139SMatthew G Knepley 
22189566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
22199566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2220a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim - 1) && (bval < 0)) {
22219566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2222cd0c2139SMatthew G Knepley               break;
2223cd0c2139SMatthew G Knepley             }
2224cd0c2139SMatthew G Knepley           }
22259566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2226cd0c2139SMatthew G Knepley         }
2227cd0c2139SMatthew G Knepley       }
2228cd0c2139SMatthew G Knepley     }
2229cd0c2139SMatthew G Knepley   }
2230accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2231accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2232a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
223386200784SMatthew G. Knepley   if (blabel) {
2234accc9626SMatthew G. Knepley     IS bdIS;
2235accc9626SMatthew G. Knepley 
2236caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2237accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2238accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2239a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2240a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2241a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2242a0541d8aSMatthew G. Knepley 
22439566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2244a0541d8aSMatthew G. Knepley       if (bval >= 0) {
22459566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2246f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2247f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
22489566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2249f7019248SMatthew G. Knepley         }
2250f7019248SMatthew G. Knepley       }
2251f7019248SMatthew G. Knepley     }
2252f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2253f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2254f7019248SMatthew G. Knepley       PetscInt       val, bval;
2255f7019248SMatthew G. Knepley 
22569566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2257f7019248SMatthew G. Knepley       if (bval >= 0) {
225886200784SMatthew G. Knepley         const PetscInt *cone, *support;
225986200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
226086200784SMatthew G. Knepley 
2261a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
22629566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2263e1a13daeSMatthew G. Knepley         PetscCheck(val >= 0 && val <= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be part of the fault", point, val);
22649566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
22659566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
22662c06a818SMatthew G. Knepley         /* Check for cross-edge
22672c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
226886200784SMatthew G. Knepley         if (val != 0) continue;
22699566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
22709566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
227186200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
22729566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
22739566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
227463a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
22759566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
22769566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
22779566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
22789566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
227986200784SMatthew G. Knepley         }
2280a0541d8aSMatthew G. Knepley       }
2281a0541d8aSMatthew G. Knepley     }
2282accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2283accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2284a0541d8aSMatthew G. Knepley   }
2285b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2286b6dfa339SMatthew G. Knepley   {
2287b6dfa339SMatthew G. Knepley     PetscSF         sf;
2288b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2289b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2290b6dfa339SMatthew G. Knepley 
2291b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2292b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2293accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2294accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2295accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2296accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2297b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2298b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2299b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2300b6dfa339SMatthew G. Knepley         PetscInt       val;
2301b6dfa339SMatthew G. Knepley 
2302b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2303b6dfa339SMatthew G. Knepley         if (l >= 0) {
2304b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2305b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2306b6dfa339SMatthew G. Knepley 
2307b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2308b6dfa339SMatthew G. Knepley           PetscCheck((val == dim - 1) || (val == shift2 + dim - 1), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be a fault face", point, val);
2309b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2310b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2311b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2312accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2313b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2314b6dfa339SMatthew G. Knepley 
2315b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2316b6dfa339SMatthew G. Knepley             PetscCheck(val != -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " is missing from label, but is in the closure of a fault face", point);
2317b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2318b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2319b6dfa339SMatthew G. Knepley           }
2320b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2321b6dfa339SMatthew G. Knepley         }
2322b6dfa339SMatthew G. Knepley       }
2323b6dfa339SMatthew G. Knepley     }
2324b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2325b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2326accc9626SMatthew G. Knepley   }
2327accc9626SMatthew G. Knepley divide:
2328b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2329accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
23300542aa8cSMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label, split));
23313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2332cd0c2139SMatthew G Knepley }
2333cd0c2139SMatthew G Knepley 
2334720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
233566976f2fSJacob Faibussowitsch static PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2336d71ae5a4SJacob Faibussowitsch {
2337720e594eSMatthew G. Knepley   IS              subpointIS;
2338720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2339720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2340720e594eSMatthew G. Knepley 
2341720e594eSMatthew G. Knepley   PetscFunctionBegin;
23423ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
23439566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
23449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
23453ba16761SJacob Faibussowitsch   if (!subpointIS) PetscFunctionReturn(PETSC_SUCCESS);
23469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
23479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
23489566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2349720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2350720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2351720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2352720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2353720e594eSMatthew G. Knepley 
23549566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2355720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2356720e594eSMatthew G. Knepley       PetscInt value = 0;
2357720e594eSMatthew G. Knepley 
2358720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
23599566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
23609371c9d4SSatish Balay       if (value == defaultValue) {
23619371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
23629371c9d4SSatish Balay         break;
23639371c9d4SSatish Balay       }
2364720e594eSMatthew G. Knepley     }
23659566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2366720e594eSMatthew G. Knepley     if (invalidCell) {
23679566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23689566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
23699566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
237063a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2371720e594eSMatthew G. Knepley     }
2372720e594eSMatthew G. Knepley   }
23739566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2375720e594eSMatthew G. Knepley }
2376720e594eSMatthew G. Knepley 
2377c08575a3SMatthew G. Knepley /*@
23783cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
23793cf72582SMatthew G. Knepley 
238020f4b53cSBarry Smith   Collective
23813cf72582SMatthew G. Knepley 
23823cf72582SMatthew G. Knepley   Input Parameters:
238320f4b53cSBarry Smith + dm      - The original `DM`
2384720e594eSMatthew G. Knepley . label   - The label specifying the interface vertices
2385caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2386caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
23873cf72582SMatthew G. Knepley 
23883cf72582SMatthew G. Knepley   Output Parameters:
238920f4b53cSBarry Smith + hybridLabel - The label fully marking the interface, or `NULL` if no output is desired
239020f4b53cSBarry Smith . splitLabel  - The label containing the split points, or `NULL` if no output is desired
239120f4b53cSBarry Smith . dmInterface - The new interface `DM`, or `NULL`
239220f4b53cSBarry Smith - dmHybrid    - The new `DM` with cohesive cells
23933cf72582SMatthew G. Knepley 
239420f4b53cSBarry Smith   Level: developer
239520f4b53cSBarry Smith 
239620f4b53cSBarry Smith   Note:
239720f4b53cSBarry Smith   The hybridLabel indicates what parts of the original mesh impinged on the division surface. For points
23986eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
23996eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
24006eccb800SMatthew Knepley   one vertex 3 on the surface would be 6 (101) and 3 (0) in hybridLabel. If an edge 9 from the negative side of the
24016eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
24026eccb800SMatthew Knepley 
24036eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
240440cdb708SMatthew Knepley   mesh. The label value is $\pm 100+dim$ for each point. For example, if two edges 10 and 14 in the hybrid resulting from
24056eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
24066eccb800SMatthew Knepley 
240720f4b53cSBarry Smith   The dmInterface is a `DM` built from the original division surface. It has a label which can be retrieved using
240820f4b53cSBarry Smith   `DMPlexGetSubpointMap()` which maps each point back to the point in the surface of the original mesh.
24096eccb800SMatthew Knepley 
24101cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
24113cf72582SMatthew G. Knepley @*/
2412d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
2413d71ae5a4SJacob Faibussowitsch {
24143cf72582SMatthew G. Knepley   DM       idm;
24157db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
24163cf72582SMatthew G. Knepley   PetscInt dim;
24173cf72582SMatthew G. Knepley 
24183cf72582SMatthew G. Knepley   PetscFunctionBegin;
24193cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24204f572ea9SToby Isaac   if (label) PetscAssertPointer(label, 2);
24214f572ea9SToby Isaac   if (bdlabel) PetscAssertPointer(bdlabel, 3);
24224f572ea9SToby Isaac   if (hybridLabel) PetscAssertPointer(hybridLabel, 5);
24234f572ea9SToby Isaac   if (splitLabel) PetscAssertPointer(splitLabel, 6);
24244f572ea9SToby Isaac   if (dmInterface) PetscAssertPointer(dmInterface, 7);
24254f572ea9SToby Isaac   PetscAssertPointer(dmHybrid, 8);
24269566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24279566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
24289566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
24299566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
24309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
24319566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
24329566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
24337db7e0a7SMatthew G. Knepley   if (splitLabel) {
24347db7e0a7SMatthew G. Knepley     const char *name;
24357db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
24367db7e0a7SMatthew G. Knepley 
24379566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
2438c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(sname, name, sizeof(sname)));
2439c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(sname, " split", sizeof(sname)));
24409566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
24417db7e0a7SMatthew G. Knepley   }
24420542aa8cSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, PETSC_TRUE, idm));
24439371c9d4SSatish Balay   if (dmInterface) {
24449371c9d4SSatish Balay     *dmInterface = idm;
24459371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
24469566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
24475de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
24483cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
24499566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
24507db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
24514a7ee7d0SMatthew G. Knepley   {
24524a7ee7d0SMatthew G. Knepley     DM      cdm;
24534a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
24544a7ee7d0SMatthew G. Knepley 
24554a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
24569566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
24579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
24589566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
24594a7ee7d0SMatthew G. Knepley   }
24603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2461cd0c2139SMatthew G Knepley }
2462cd0c2139SMatthew G Knepley 
2463efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2464efa14ee0SMatthew G Knepley 
2465efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2466efa14ee0SMatthew G Knepley */
2467d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2468d71ae5a4SJacob Faibussowitsch {
2469fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2470efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2471412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2472efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2473efa14ee0SMatthew G Knepley 
2474efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2475efa14ee0SMatthew G Knepley   *numFaces = 0;
2476efa14ee0SMatthew G Knepley   *nFV      = 0;
24779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24789566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
247977d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
24809566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
248148a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2482efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
24839566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2484efa14ee0SMatthew G Knepley   if (subvertexIS) {
24859566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24869566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2487efa14ee0SMatthew G Knepley   }
2488efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2489efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24900298fd71SBarry Smith     PetscInt      *star   = NULL;
2491efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2492efa14ee0SMatthew G Knepley 
24939566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2494efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2495efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2496efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2497efa14ee0SMatthew G Knepley     }
2498efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2499efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
25000298fd71SBarry Smith       PetscInt      *closure = NULL;
2501efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2502efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2503efa14ee0SMatthew G Knepley 
25049566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
250565560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
250663a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
25079566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2508efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2509efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2510efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2511efa14ee0SMatthew G Knepley 
2512efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2513efa14ee0SMatthew G Knepley           ++numCorners;
25149566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2515830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2516efa14ee0SMatthew G Knepley         }
2517efa14ee0SMatthew G Knepley       }
25184ad8454bSPierre Jolivet       if (!*nFV) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
251963a3b9bcSJacob Faibussowitsch       PetscCheck(faceSize <= *nFV, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
2520efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2521007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2522007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2523007baee2SMatthew G. Knepley 
2524efa14ee0SMatthew G Knepley         ++(*numFaces);
252548a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
25269566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
252748a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
25289566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2529efa14ee0SMatthew G Knepley       }
25309566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2531efa14ee0SMatthew G Knepley     }
25329566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2533efa14ee0SMatthew G Knepley   }
253448a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25359566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25369566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2538efa14ee0SMatthew G Knepley }
2539efa14ee0SMatthew G Knepley 
25405390be7dSMatthew G. Knepley PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, PetscBool addCells, DMLabel subpointMap, DM subdm)
2541d71ae5a4SJacob Faibussowitsch {
254234b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2543efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2544412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2545efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2546efa14ee0SMatthew G Knepley 
2547efa14ee0SMatthew G Knepley   PetscFunctionBegin;
25489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25499566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
255048a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2551efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
255234b4c39eSMatthew G. Knepley   if (vertexLabel) {
25539566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2554efa14ee0SMatthew G Knepley     if (subvertexIS) {
25559566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
25569566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2557efa14ee0SMatthew G Knepley     }
255834b4c39eSMatthew G. Knepley   }
2559efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2560efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
25610298fd71SBarry Smith     PetscInt      *star   = NULL;
2562efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2563efa14ee0SMatthew G Knepley 
25649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2565efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2566efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2567158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2568158acfadSMatthew G. Knepley 
2569158acfadSMatthew G. Knepley       if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) {
2570158acfadSMatthew G. Knepley         if (markedFaces) {
25719566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2572158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2573158acfadSMatthew G. Knepley         }
2574158acfadSMatthew G. Knepley         star[numFaces++] = point;
2575158acfadSMatthew G. Knepley       }
2576efa14ee0SMatthew G Knepley     }
2577efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2578efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
25790298fd71SBarry Smith       PetscInt      *closure = NULL;
2580efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2581efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2582efa14ee0SMatthew G Knepley 
25839566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2584efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
258563a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
25869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2587efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2588efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2589efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2590efa14ee0SMatthew G Knepley 
2591efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
25929566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2593830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2594efa14ee0SMatthew G Knepley         }
2595efa14ee0SMatthew G Knepley       }
2596efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2597efa14ee0SMatthew G Knepley         const PetscInt *support;
2598efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2599efa14ee0SMatthew G Knepley 
2600efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2601efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2602efa14ee0SMatthew G Knepley 
2603efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2604efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
26059566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2606efa14ee0SMatthew G Knepley               break;
2607efa14ee0SMatthew G Knepley             }
2608efa14ee0SMatthew G Knepley           }
2609efa14ee0SMatthew G Knepley         }
26109566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
26119566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
26125390be7dSMatthew G. Knepley         if (addCells)
261348a46eb9SPierre Jolivet           for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2614efa14ee0SMatthew G Knepley       }
26159566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2616efa14ee0SMatthew G Knepley     }
26179566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2618efa14ee0SMatthew G Knepley   }
26199566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
26209566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
26219566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
26223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2623efa14ee0SMatthew G Knepley }
2624efa14ee0SMatthew G Knepley 
2625d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2626d71ae5a4SJacob Faibussowitsch {
262727c04023SMatthew G. Knepley   DMLabel         label = NULL;
2628766ab985SMatthew G. Knepley   const PetscInt *cone;
26299fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2630766ab985SMatthew G. Knepley 
2631812bfc34SJed Brown   PetscFunctionBegin;
2632c0ed958bSJed Brown   *numFaces = 0;
2633c0ed958bSJed Brown   *nFV      = 0;
26349566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2635fed694aaSMatthew G. Knepley   *subCells = NULL;
26369566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26383ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
263927c04023SMatthew G. Knepley   if (label) {
264027c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
264127c04023SMatthew G. Knepley       PetscInt val;
264227c04023SMatthew G. Knepley 
26439566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
264427c04023SMatthew G. Knepley       if (val == value) {
264527c04023SMatthew G. Knepley         ++(*numFaces);
26469566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
264727c04023SMatthew G. Knepley       }
264827c04023SMatthew G. Knepley     }
264927c04023SMatthew G. Knepley   } else {
2650766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
26519566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
265227c04023SMatthew G. Knepley   }
26539566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
26544ad8454bSPierre Jolivet   if (!*numFaces) PetscFunctionReturn(PETSC_SUCCESS);
26559fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2656766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2657766ab985SMatthew G. Knepley     const PetscInt *cells;
2658766ab985SMatthew G. Knepley     PetscInt        numCells;
2659766ab985SMatthew G. Knepley 
266027c04023SMatthew G. Knepley     if (label) {
266127c04023SMatthew G. Knepley       PetscInt val;
266227c04023SMatthew G. Knepley 
26639566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
266427c04023SMatthew G. Knepley       if (val != value) continue;
266527c04023SMatthew G. Knepley     }
26669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
266748a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2668766ab985SMatthew G. Knepley     /* Negative face */
26699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
267027234c99SMatthew G. Knepley     /* Not true in parallel
267108401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2672766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
26739566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
267427234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2675766ab985SMatthew G. Knepley     }
26769566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2677766ab985SMatthew G. Knepley     /* Positive face is not included */
2678766ab985SMatthew G. Knepley   }
26793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2680766ab985SMatthew G. Knepley }
2681766ab985SMatthew G. Knepley 
2682d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2683d71ae5a4SJacob Faibussowitsch {
2684766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2685766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2686766ab985SMatthew G. Knepley 
2687812bfc34SJed Brown   PetscFunctionBegin;
26889566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26899566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26903ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
26919566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
26929566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2693766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2694766ab985SMatthew G. Knepley     const PetscInt *cone;
2695766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2696b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2697766ab985SMatthew G. Knepley 
269827c04023SMatthew G. Knepley     if (label) {
26999566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
270027c04023SMatthew G. Knepley       if (val != value) continue;
270127c04023SMatthew G. Knepley     }
27029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
27039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
27049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
27051dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2706b3154360SMatthew G. Knepley     /* Negative face */
27079566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2708766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2709766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2710766ab985SMatthew G. Knepley 
2711766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2712766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
27139566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2714766ab985SMatthew G. Knepley           break;
2715766ab985SMatthew G. Knepley         }
2716766ab985SMatthew G. Knepley       }
2717766ab985SMatthew G. Knepley     }
27189566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2719766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2720766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2721766ab985SMatthew G. Knepley       const PetscInt *support;
2722766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2723766ab985SMatthew G. Knepley 
27249566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
272508401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
27269566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
272748a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2728766ab985SMatthew G. Knepley     }
2729766ab985SMatthew G. Knepley   }
27309566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
27313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2732766ab985SMatthew G. Knepley }
2733766ab985SMatthew G. Knepley 
2734d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2735d71ae5a4SJacob Faibussowitsch {
273682f516ccSBarry Smith   MPI_Comm       comm;
2737e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2738e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2739e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2740e6ccafaeSMatthew G Knepley 
274182f516ccSBarry Smith   PetscFunctionBegin;
27429566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
27439566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
274463a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2745e6ccafaeSMatthew G Knepley 
2746ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2747ddeab2a6SMatthew G. Knepley     /* Triangle */
2748e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2749e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2750ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2751ddeab2a6SMatthew G. Knepley     /* Triangle */
2752ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2753ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2754ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2755ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2756ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2757ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2758e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2759e6ccafaeSMatthew G Knepley     /* Quadratic line */
2760e6ccafaeSMatthew G Knepley     faceSize  = 1;
2761e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2762e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2763e6ccafaeSMatthew G Knepley     /* Quads */
2764e6ccafaeSMatthew G Knepley     faceSize = 2;
2765e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2766e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2767e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2768e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2769e6ccafaeSMatthew G Knepley     } else {
2770e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2771e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2772e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2773e6ccafaeSMatthew G Knepley     }
2774e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2775e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2776e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2777e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2778e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2779e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2780e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2781e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2782e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2783e6ccafaeSMatthew G Knepley       2, 3, 5, /* left */
2784e6ccafaeSMatthew G Knepley     };
2785e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2786e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2787e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2788e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2789e6ccafaeSMatthew G Knepley     };
2790e6ccafaeSMatthew G Knepley 
2791e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
27929566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2793e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2794e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2795e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2796e6ccafaeSMatthew G Knepley 
27979371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) {
2798e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2799e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2800e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2801e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2802e6ccafaeSMatthew G Knepley               break;
2803e6ccafaeSMatthew G Knepley             }
2804e6ccafaeSMatthew G Knepley           }
2805e6ccafaeSMatthew G Knepley         }
2806e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2807e6ccafaeSMatthew G Knepley         break;
2808e6ccafaeSMatthew G Knepley       }
2809e6ccafaeSMatthew G Knepley     }
281028b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2811e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28123ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2813e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2814e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2815e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2816e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2817e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2818e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2819e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2820e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2821e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2822e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2823e6ccafaeSMatthew G Knepley       0, 3, 7, /* left */
2824e6ccafaeSMatthew G Knepley     };
2825e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2826e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2827e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2828e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2829e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2830e6ccafaeSMatthew G Knepley     };
2831e6ccafaeSMatthew G Knepley 
2832e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
28339566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2834e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2835e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2836e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2837e6ccafaeSMatthew G Knepley 
28389371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) {
2839e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2840e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2841e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2842e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2843e6ccafaeSMatthew G Knepley               break;
2844e6ccafaeSMatthew G Knepley             }
2845e6ccafaeSMatthew G Knepley           }
2846e6ccafaeSMatthew G Knepley         }
2847e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2848e6ccafaeSMatthew G Knepley         break;
2849e6ccafaeSMatthew G Knepley       }
2850e6ccafaeSMatthew G Knepley     }
285128b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2852e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28533ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2854e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2855e6ccafaeSMatthew G Knepley     /* Hexes
2856e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2857e6ccafaeSMatthew G Knepley        pointing up at the second.
2858e6ccafaeSMatthew G Knepley 
2859e6ccafaeSMatthew G Knepley           7---6
2860e6ccafaeSMatthew G Knepley          /|  /|
2861e6ccafaeSMatthew G Knepley         4---5 |
2862ddeab2a6SMatthew G. Knepley         | 1-|-2
2863e6ccafaeSMatthew G Knepley         |/  |/
2864ddeab2a6SMatthew G. Knepley         0---3
2865e6ccafaeSMatthew G Knepley 
2866e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2867e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2868e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2869e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2870e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2871e6ccafaeSMatthew G Knepley       0, 1, 2, 3, /* bottom */
2872e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2873ddeab2a6SMatthew G. Knepley       0, 3, 4, 5, /* front */
2874ddeab2a6SMatthew G. Knepley       2, 3, 5, 6, /* right */
2875ddeab2a6SMatthew G. Knepley       1, 2, 6, 7, /* back */
2876ddeab2a6SMatthew G. Knepley       0, 1, 4, 7, /* left */
2877e6ccafaeSMatthew G Knepley     };
2878e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2879ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2880e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2881ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2882ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2883ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2884ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2885e6ccafaeSMatthew G Knepley     };
2886e6ccafaeSMatthew G Knepley 
2887e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
28889566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2889e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2890e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2891e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2892e6ccafaeSMatthew G Knepley 
28939371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) {
2894e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2895e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2896e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2897e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2898e6ccafaeSMatthew G Knepley               break;
2899e6ccafaeSMatthew G Knepley             }
2900e6ccafaeSMatthew G Knepley           }
2901e6ccafaeSMatthew G Knepley         }
2902e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2903e6ccafaeSMatthew G Knepley         break;
2904e6ccafaeSMatthew G Knepley       }
2905e6ccafaeSMatthew G Knepley     }
290628b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2907e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29083ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2909e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2910e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2911e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2912e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2913e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2914e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2915e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2916e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2917e6ccafaeSMatthew G Knepley       0, 3, 4, 6, 7, 9, /* front */
2918e6ccafaeSMatthew G Knepley       1, 4, 5, 7, 8, 9, /* right */
2919e6ccafaeSMatthew G Knepley       2, 3, 5, 6, 8, 9, /* left */
2920e6ccafaeSMatthew G Knepley     };
2921e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2922e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2923e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2924e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2925e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2926e6ccafaeSMatthew G Knepley     };
2927e6ccafaeSMatthew G Knepley 
2928e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
29299566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2930e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2931e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2932e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2933e6ccafaeSMatthew G Knepley 
29349371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) {
2935e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2936e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2937e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2938e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2939e6ccafaeSMatthew G Knepley               break;
2940e6ccafaeSMatthew G Knepley             }
2941e6ccafaeSMatthew G Knepley           }
2942e6ccafaeSMatthew G Knepley         }
2943e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2944e6ccafaeSMatthew G Knepley         break;
2945e6ccafaeSMatthew G Knepley       }
2946e6ccafaeSMatthew G Knepley     }
294728b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2948e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29493ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2950e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2951e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2952e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2953e6ccafaeSMatthew G Knepley        pointing up at the second.
2954e6ccafaeSMatthew G Knepley 
2955e6ccafaeSMatthew G Knepley          7---6
2956e6ccafaeSMatthew G Knepley         /|  /|
2957e6ccafaeSMatthew G Knepley        4---5 |
2958e6ccafaeSMatthew G Knepley        | 3-|-2
2959e6ccafaeSMatthew G Knepley        |/  |/
2960e6ccafaeSMatthew G Knepley        0---1
2961e6ccafaeSMatthew G Knepley 
2962e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2963e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2964e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2965e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2966e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2967e6ccafaeSMatthew G Knepley       0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2968e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2969e6ccafaeSMatthew G Knepley       0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2970e6ccafaeSMatthew G Knepley       1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2971e6ccafaeSMatthew G Knepley       2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2972e6ccafaeSMatthew G Knepley       0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2973e6ccafaeSMatthew G Knepley     };
2974e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2975e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2976e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2977e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2978e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2979e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2980e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2981e6ccafaeSMatthew G Knepley     };
2982e6ccafaeSMatthew G Knepley 
2983e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
29849566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2985e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2986e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2987e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2988e6ccafaeSMatthew G Knepley 
29899371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) {
2990e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2991e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2992e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2993e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2994e6ccafaeSMatthew G Knepley               break;
2995e6ccafaeSMatthew G Knepley             }
2996e6ccafaeSMatthew G Knepley           }
2997e6ccafaeSMatthew G Knepley         }
2998e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2999e6ccafaeSMatthew G Knepley         break;
3000e6ccafaeSMatthew G Knepley       }
3001e6ccafaeSMatthew G Knepley     }
300228b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
3003e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
30043ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3005e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
3006e6ccafaeSMatthew G Knepley   if (!posOrient) {
30079566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
3008e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
3009e6ccafaeSMatthew G Knepley   } else {
30109566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
3011e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
3012e6ccafaeSMatthew G Knepley   }
3013e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
30143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3015e6ccafaeSMatthew G Knepley }
3016e6ccafaeSMatthew G Knepley 
3017c08575a3SMatthew G. Knepley /*@
3018c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
3019c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
3020c08575a3SMatthew G. Knepley 
302120f4b53cSBarry Smith   Not Collective
3022c08575a3SMatthew G. Knepley 
3023c08575a3SMatthew G. Knepley   Input Parameters:
3024c08575a3SMatthew G. Knepley + dm           - The original mesh
3025c08575a3SMatthew G. Knepley . cell         - The cell mesh point
3026c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
3027c08575a3SMatthew G. Knepley . face         - The face vertices
3028c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
3029c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
3030c08575a3SMatthew G. Knepley - origVertices - Original face vertices
3031c08575a3SMatthew G. Knepley 
3032d8d19677SJose E. Roman   Output Parameters:
3033c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
303420f4b53cSBarry Smith - posOriented  - `PETSC_TRUE` if the face was oriented with outward normal
3035c08575a3SMatthew G. Knepley 
3036c08575a3SMatthew G. Knepley   Level: developer
3037c08575a3SMatthew G. Knepley 
30381cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
3039c08575a3SMatthew G. Knepley @*/
3040d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
3041d71ae5a4SJacob Faibussowitsch {
30420298fd71SBarry Smith   const PetscInt *cone = NULL;
3043e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
3044e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
3045e6ccafaeSMatthew G Knepley 
3046e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
30489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
3049e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
3050e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
3051e6ccafaeSMatthew G Knepley 
3052e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
3053e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
30549371c9d4SSatish Balay         found = PETSC_TRUE;
30559371c9d4SSatish Balay         break;
3056e6ccafaeSMatthew G Knepley       }
3057e6ccafaeSMatthew G Knepley     }
3058e6ccafaeSMatthew G Knepley     if (found) {
3059e6ccafaeSMatthew G Knepley       indices[v2]      = v;
3060e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
3061e6ccafaeSMatthew G Knepley       ++v2;
3062e6ccafaeSMatthew G Knepley     } else {
3063e6ccafaeSMatthew G Knepley       oppositeVertex = v;
3064e6ccafaeSMatthew G Knepley     }
3065e6ccafaeSMatthew G Knepley   }
30669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
30673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3068e6ccafaeSMatthew G Knepley }
3069e6ccafaeSMatthew G Knepley 
3070e6ccafaeSMatthew G Knepley /*
3071cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
3072e6ccafaeSMatthew G Knepley 
307320f4b53cSBarry Smith   Not Collective
3074e6ccafaeSMatthew G Knepley 
3075e6ccafaeSMatthew G Knepley   Input Parameters:
307620f4b53cSBarry Smith   + dm              - The `DMPLEX`
3077e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
307820f4b53cSBarry Smith   . faceVertices    - The vertices in the face for `dm`
3079e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
308020f4b53cSBarry Smith   . numCorners      - The number of vertices in the `cell`
308120f4b53cSBarry Smith   . cell            - A cell in `dm` containing the face
3082e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
3083e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
3084e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
3085e6ccafaeSMatthew G Knepley 
30862fe279fdSBarry Smith   Output Parameter:
3087e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
3088e6ccafaeSMatthew G Knepley 
3089e6ccafaeSMatthew G Knepley   Level: developer
3090e6ccafaeSMatthew G Knepley */
3091d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexInsertFace_Internal(DM dm, DM subdm, PetscInt numFaceVertices, const PetscInt faceVertices[], const PetscInt subfaceVertices[], PetscInt numCorners, PetscInt cell, PetscInt subcell, PetscInt firstFace, PetscInt *newFacePoint)
3092d71ae5a4SJacob Faibussowitsch {
309382f516ccSBarry Smith   MPI_Comm        comm;
3094e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
3095e6ccafaeSMatthew G Knepley   const PetscInt *faces;
3096e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
3097e6ccafaeSMatthew G Knepley 
3098e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30999566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
31009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
310163a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
3102e6ccafaeSMatthew G Knepley #if 0
3103e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
31049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3105e6ccafaeSMatthew G Knepley #else
3106e6ccafaeSMatthew G Knepley   {
3107e6ccafaeSMatthew G Knepley     PetscInt f;
3108e6ccafaeSMatthew G Knepley 
3109e6ccafaeSMatthew G Knepley     numFaces = 0;
31109566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3111e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
3112e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
3113e6ccafaeSMatthew G Knepley 
31149566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
31159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
3116e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
3117e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
3118e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
3119e6ccafaeSMatthew G Knepley         PetscInt       v;
3120e6ccafaeSMatthew G Knepley 
3121e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
3122e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
3123e6ccafaeSMatthew G Knepley         }
3124e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
3125e6ccafaeSMatthew G Knepley       }
3126e6ccafaeSMatthew G Knepley       if (d == dof) {
3127e6ccafaeSMatthew G Knepley         numFaces               = 1;
3128e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
3129e6ccafaeSMatthew G Knepley       }
3130e6ccafaeSMatthew G Knepley     }
3131e6ccafaeSMatthew G Knepley   }
3132e6ccafaeSMatthew G Knepley #endif
313363a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3134f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3135e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
31369566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3137e6ccafaeSMatthew G Knepley   } else {
3138e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3139e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3140e6ccafaeSMatthew G Knepley 
31419566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3142e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3143e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
3144e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
31459566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3146e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3147e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3148e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3149e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3150e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3151e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3152e6ccafaeSMatthew G Knepley           break;
3153e6ccafaeSMatthew G Knepley         }
3154e6ccafaeSMatthew G Knepley       }
315563a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3156e6ccafaeSMatthew G Knepley     }
31579566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
31589566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
31599566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3160e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3161e6ccafaeSMatthew G Knepley   }
3162ef07cca7SMatthew G. Knepley #if 0
31639566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3164ef07cca7SMatthew G. Knepley #else
31659566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3166ef07cca7SMatthew G. Knepley #endif
31673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3168e6ccafaeSMatthew G Knepley }
3169e6ccafaeSMatthew G Knepley 
3170d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3171d71ae5a4SJacob Faibussowitsch {
317282f516ccSBarry Smith   MPI_Comm        comm;
317353156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3174efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
3175efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3176efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3177fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3178efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3179e6ccafaeSMatthew G Knepley 
3180e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
31819566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3182efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
31839566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
31849566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
31859566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3186efa14ee0SMatthew G Knepley   /* Setup chart */
31879566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
31889566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
31899566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
31909566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3191e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3192e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3193efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
3194e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
31959566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
31969566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
31979566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
31989566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
319948a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
320048a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
32019566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3202d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3203e6ccafaeSMatthew G Knepley   /* Create face cones */
32049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
32059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
32069566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3207e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3208e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3209efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
32100298fd71SBarry Smith     PetscInt      *closure = NULL;
3211efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3212e6ccafaeSMatthew G Knepley 
32139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3214efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3215efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3216e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3217e6ccafaeSMatthew G Knepley 
3218efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3219efa14ee0SMatthew G Knepley         ++numCorners;
32209566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3221efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3222efa14ee0SMatthew G Knepley           closure[faceSize] = point;
322365560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3224e6ccafaeSMatthew G Knepley           ++faceSize;
3225e6ccafaeSMatthew G Knepley         }
3226e6ccafaeSMatthew G Knepley       }
3227e6ccafaeSMatthew G Knepley     }
322863a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
322948a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
32309566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3231e6ccafaeSMatthew G Knepley   }
32329566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
32339566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
32349566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3235e6ccafaeSMatthew G Knepley   /* Build coordinates */
3236efa14ee0SMatthew G Knepley   {
3237efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3238efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3239efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3240285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
324124640c55SToby Isaac     const char  *name;
3242efa14ee0SMatthew G Knepley 
32439566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
32449566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
32459566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
32469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
32479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
32489566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
32499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3250efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3251efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3252efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3253efa14ee0SMatthew G Knepley       PetscInt       dof;
3254efa14ee0SMatthew G Knepley 
32559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32569566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
32579566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3258e6ccafaeSMatthew G Knepley     }
32599566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
32609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
32619566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
32629566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
32639566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
32649566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
32659566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3266830e53efSMatthew G. Knepley     if (coordSize) {
32679566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
32689566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3269efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3270efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3271efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3272efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3273e6ccafaeSMatthew G Knepley 
32749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32759566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
32769566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
32779566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
327863a3b9bcSJacob Faibussowitsch         PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3279e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3280e6ccafaeSMatthew G Knepley       }
32819566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
32829566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
32833b399e24SMatthew G. Knepley     }
32849566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
32859566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3286e6ccafaeSMatthew G Knepley   }
3287efa14ee0SMatthew G Knepley   /* Cleanup */
32889566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
32899566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
32909566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
32919566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
32923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3293e6ccafaeSMatthew G Knepley }
3294e6ccafaeSMatthew G Knepley 
3295d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3296d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
3297d71ae5a4SJacob Faibussowitsch {
32983982b651SMatthew G. Knepley   PetscInt       subPoint;
32993982b651SMatthew G. Knepley   PetscErrorCode ierr;
33003982b651SMatthew G. Knepley 
33019371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
33029371c9d4SSatish Balay   if (ierr) return -1;
33033982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
33043982b651SMatthew G. Knepley }
33053982b651SMatthew G. Knepley 
3306d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3307d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[])
3308d71ae5a4SJacob Faibussowitsch {
3309dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3310dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3311dd05d810SMatthew G. Knepley 
3312dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3313dd05d810SMatthew G. Knepley   if (ierr) return -1;
3314dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3315dd05d810SMatthew G. Knepley }
3316dd05d810SMatthew G. Knepley 
3317d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3318d71ae5a4SJacob Faibussowitsch {
3319695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3320212103e5SMatthew Knepley   PetscInt Nl, l, d;
3321212103e5SMatthew Knepley 
3322212103e5SMatthew Knepley   PetscFunctionBegin;
3323695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3324695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3325695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
33269566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3327212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3328212103e5SMatthew Knepley     DMLabel         label, newlabel;
3329212103e5SMatthew Knepley     const char     *lname;
3330d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3331212103e5SMatthew Knepley     IS              valueIS;
3332212103e5SMatthew Knepley     const PetscInt *values;
3333212103e5SMatthew Knepley     PetscInt        Nv, v;
3334212103e5SMatthew Knepley 
33359566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
33369566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
33379566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
33389566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
33399566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3340d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
33419566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
33429566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
33439566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
33449566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
33459566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
33469566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
33479566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
33489566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3349212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3350212103e5SMatthew Knepley       IS              pointIS;
3351212103e5SMatthew Knepley       const PetscInt *points;
3352212103e5SMatthew Knepley       PetscInt        Np, p;
3353212103e5SMatthew Knepley 
33549566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
33559566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
33569566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3357212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3358212103e5SMatthew Knepley         const PetscInt point = points[p];
3359f117cc94SBrad Aagaard         PetscInt       subp, subdepth;
3360212103e5SMatthew Knepley 
33619566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3362f117cc94SBrad Aagaard         PetscCall(DMPlexGetDepth(subdm, &subdepth));
3363f117cc94SBrad Aagaard         if (d > subdepth) continue;
3364212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
33659566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3366212103e5SMatthew Knepley       }
33679566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
33689566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3369212103e5SMatthew Knepley     }
33709566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
33719566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3372212103e5SMatthew Knepley   }
33733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3374212103e5SMatthew Knepley }
3375212103e5SMatthew Knepley 
337630cbcd5dSksagiyam static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, PetscBool ignoreLabelHalo, PetscBool sanitizeSubmesh, PetscSF *ownershipTransferSF, DM subdm)
3377d71ae5a4SJacob Faibussowitsch {
337882f516ccSBarry Smith   MPI_Comm         comm;
337953156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3380efa14ee0SMatthew G Knepley   IS              *subpointIS;
3381efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
33823982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3383*3e98c3d0Sksagiyam   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, coordinate_type;
33840d366550SMatthew G. Knepley   PetscMPIInt      rank;
3385e6ccafaeSMatthew G Knepley 
3386e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
33879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
33889566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3389efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
33909566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
33919566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3392bec263e5SMatthew G. Knepley   if (cellHeight) {
33939566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
33945390be7dSMatthew G. Knepley     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, PETSC_TRUE, subpointMap, subdm));
3395bec263e5SMatthew G. Knepley   } else {
3396bec263e5SMatthew G. Knepley     DMLabel         depth;
3397bec263e5SMatthew G. Knepley     IS              pointIS;
3398bec263e5SMatthew G. Knepley     const PetscInt *points;
33994d00ad26Sksagiyam     PetscInt        numPoints = 0, pStart = 0;
34004d00ad26Sksagiyam     PetscBool      *isGhost = NULL;
3401bec263e5SMatthew G. Knepley 
34029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
34039566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3404b85c8bf9SMatthew G. Knepley     if (pointIS) {
34059566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
34069566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3407b85c8bf9SMatthew G. Knepley     }
34084d00ad26Sksagiyam     if (ignoreLabelHalo) {
34094d00ad26Sksagiyam       PetscSF         pointSF;
34104d00ad26Sksagiyam       PetscInt        nleaves, pEnd;
34114d00ad26Sksagiyam       const PetscInt *ilocal = NULL;
34124d00ad26Sksagiyam 
34134d00ad26Sksagiyam       PetscCall(DMGetPointSF(dm, &pointSF));
34144d00ad26Sksagiyam       PetscCall(PetscSFGetGraph(pointSF, NULL, &nleaves, &ilocal, NULL));
34154d00ad26Sksagiyam       PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
34164d00ad26Sksagiyam       PetscCall(PetscMalloc1(pEnd - pStart, &isGhost));
34174d00ad26Sksagiyam       for (p = 0; p < pEnd - pStart; ++p) isGhost[p] = PETSC_FALSE;
34184d00ad26Sksagiyam       for (p = 0; p < nleaves; ++p) isGhost[(ilocal ? ilocal[p] : p) - pStart] = PETSC_TRUE;
34194d00ad26Sksagiyam     }
3420bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3421bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3422bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3423bec263e5SMatthew G. Knepley 
34244d00ad26Sksagiyam       if (ignoreLabelHalo && isGhost[points[p] - pStart]) continue;
34259566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3426bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
34279566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
34289566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3429bec263e5SMatthew G. Knepley       }
34309566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3431bec263e5SMatthew G. Knepley     }
34324d00ad26Sksagiyam     PetscCall(PetscFree(isGhost));
34339566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
34349566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3435bec263e5SMatthew G. Knepley   }
3436efa14ee0SMatthew G Knepley   /* Setup chart */
34379566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3438dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
34399566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3440e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
34419566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3442e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3443e6ccafaeSMatthew G Knepley   }
3444dd05d810SMatthew G. Knepley   // Determine submesh dimension
3445dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3446dd05d810SMatthew G. Knepley   if (sdim > 0) {
3447dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3448dd05d810SMatthew G. Knepley     sdim = dim;
3449dd05d810SMatthew G. Knepley   } else {
3450dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3451dd05d810SMatthew G. Knepley     PetscInt lsdim;
3452dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3453dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3454462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPI_MAX, comm));
3455dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3456dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3457dd05d810SMatthew G. Knepley   }
34589566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
34599566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3460e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3461dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3462dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3463dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3464dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3465dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
34669566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
34679566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3468e6ccafaeSMatthew G Knepley   }
3469412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
34709566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3471dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3472e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3473e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3474e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3475e6ccafaeSMatthew G Knepley       const PetscInt *cone;
347615100a53SVaclav Hapla       PetscInt        coneSize;
3477e6ccafaeSMatthew G Knepley 
34789566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3479dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
348015100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
348115100a53SVaclav Hapla 
34829566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3483e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34849566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3485e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3486e6ccafaeSMatthew G Knepley         }
34879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
34889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
348915100a53SVaclav Hapla       } else {
349015100a53SVaclav Hapla         DMPolytopeType ct;
349115100a53SVaclav Hapla 
349215100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
349315100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
349415100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3495e6ccafaeSMatthew G Knepley       }
3496e6ccafaeSMatthew G Knepley     }
3497e6ccafaeSMatthew G Knepley   }
34989566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
34999566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3500e6ccafaeSMatthew G Knepley   /* Set cones */
35019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
35029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3503dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3504e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3505e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3506e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
35070e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
35080d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3509e6ccafaeSMatthew G Knepley 
3510dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
35110d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
35120d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
35130d366550SMatthew G. Knepley 
35149566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
35159566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
35160d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3517064cae4fSPierre Jolivet           PetscBool isHybrid = PETSC_FALSE;
3518412e9a14SMatthew G. Knepley 
35199566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3520412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
35219566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
35220d366550SMatthew G. Knepley           if (subc >= 0) {
35230d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
35240d366550SMatthew G. Knepley 
35259566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
35269566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
35279566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
35280d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
35290d366550SMatthew G. Knepley               if (cone[c] == point) {
35300d366550SMatthew G. Knepley                 fornt = ornt[c];
35310d366550SMatthew G. Knepley                 break;
35320d366550SMatthew G. Knepley               }
35330d366550SMatthew G. Knepley             }
35340d366550SMatthew G. Knepley             break;
35350d366550SMatthew G. Knepley           }
35360d366550SMatthew G. Knepley         }
35370d366550SMatthew G. Knepley       }
35389566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
35399566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
35409566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
35419566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3542e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
35439566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
354401a2673eSMatthew G. Knepley         if (subc >= 0) {
354501a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
35463982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
354701a2673eSMatthew G. Knepley           ++coneSizeNew;
354801a2673eSMatthew G. Knepley         }
3549e6ccafaeSMatthew G Knepley       }
355063a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
35519566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
35529566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
35539566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3554e6ccafaeSMatthew G Knepley     }
3555e6ccafaeSMatthew G Knepley   }
35569566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
35579566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
35589566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3559e6ccafaeSMatthew G Knepley   /* Build coordinates */
3560*3e98c3d0Sksagiyam   for (coordinate_type = 0; coordinate_type < 2; ++coordinate_type) {
3561*3e98c3d0Sksagiyam     DM           coordDM = NULL, subCoordDM = NULL;
3562*3e98c3d0Sksagiyam     PetscSection coordSection = NULL, subCoordSection = NULL;
3563*3e98c3d0Sksagiyam     Vec          coordinates = NULL, subCoordinates = NULL;
3564*3e98c3d0Sksagiyam     PetscScalar *coords = NULL, *subCoords = NULL;
3565*3e98c3d0Sksagiyam     PetscInt     cdim, numComp, coordSize, firstP, lastP, firstSubP = totSubPoints, lastSubP = -1;
356624640c55SToby Isaac     const char  *name;
3567*3e98c3d0Sksagiyam     PetscBool    localized = (PetscBool)coordinate_type;
3568e6ccafaeSMatthew G Knepley 
35699566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
3570*3e98c3d0Sksagiyam     if (!dm->coordinates[coordinate_type].dm) continue;
3571*3e98c3d0Sksagiyam     if (!localized) {
3572*3e98c3d0Sksagiyam       PetscCall(DMGetCoordinateDM(dm, &coordDM));
3573*3e98c3d0Sksagiyam       PetscCall(DMGetCoordinateDM(subdm, &subCoordDM));
3574*3e98c3d0Sksagiyam     } else {
3575*3e98c3d0Sksagiyam       {
3576*3e98c3d0Sksagiyam         PetscInt  localizationHeight;
3577*3e98c3d0Sksagiyam         PetscBool sparseLocalize;
3578*3e98c3d0Sksagiyam 
3579*3e98c3d0Sksagiyam         PetscCall(DMGetSparseLocalize(dm, &sparseLocalize));
3580*3e98c3d0Sksagiyam         PetscCall(DMSetSparseLocalize(subdm, sparseLocalize));
3581*3e98c3d0Sksagiyam         PetscCall(DMGetCoordinateDM(dm, &coordDM));
3582*3e98c3d0Sksagiyam         PetscCall(DMGetCoordinateDM(subdm, &subCoordDM));
3583*3e98c3d0Sksagiyam         PetscCall(DMPlexGetMaxProjectionHeight(coordDM, &localizationHeight));
3584*3e98c3d0Sksagiyam         PetscCall(DMPlexSetMaxProjectionHeight(subCoordDM, localizationHeight - (dim - sdim) - cellHeight));
3585*3e98c3d0Sksagiyam         PetscUseTypeMethod(subdm, createcellcoordinatedm, &subCoordDM);
3586*3e98c3d0Sksagiyam         PetscCall(DMSetCellCoordinateDM(subdm, subCoordDM));
3587*3e98c3d0Sksagiyam         PetscCall(DMDestroy(&subCoordDM));
3588*3e98c3d0Sksagiyam       }
3589*3e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateDM(dm, &coordDM));
3590*3e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateDM(subdm, &subCoordDM));
3591*3e98c3d0Sksagiyam     }
3592*3e98c3d0Sksagiyam     if (!localized) {
35939566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateSection(dm, &coordSection));
35949566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
3595*3e98c3d0Sksagiyam       PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
3596*3e98c3d0Sksagiyam     } else {
3597*3e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateSection(dm, &coordSection));
3598*3e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateSection(subdm, &subCoordSection));
3599*3e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinatesLocal(dm, &coordinates));
3600*3e98c3d0Sksagiyam     }
36019566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
36029566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
36039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
3604*3e98c3d0Sksagiyam     PetscCall(PetscSectionGetChart(coordSection, &firstP, &lastP));
3605*3e98c3d0Sksagiyam     for (d = 0; d <= sdim; ++d) {
3606*3e98c3d0Sksagiyam       for (p = 0; p < numSubPoints[d]; ++p) {
3607*3e98c3d0Sksagiyam         const PetscInt point    = subpoints[d][p];
3608*3e98c3d0Sksagiyam         const PetscInt subpoint = firstSubPoint[d] + p;
3609*3e98c3d0Sksagiyam 
3610*3e98c3d0Sksagiyam         if (point >= firstP && point < lastP) {
3611*3e98c3d0Sksagiyam           firstSubP = PetscMin(firstSubP, subpoint);
3612*3e98c3d0Sksagiyam           lastSubP  = PetscMax(lastSubP, subpoint);
3613*3e98c3d0Sksagiyam         }
3614*3e98c3d0Sksagiyam       }
3615*3e98c3d0Sksagiyam     }
3616*3e98c3d0Sksagiyam     lastSubP += 1;
3617*3e98c3d0Sksagiyam     if (firstSubP == totSubPoints) {
3618*3e98c3d0Sksagiyam       /* Zero if there is no coordinate point. */
3619*3e98c3d0Sksagiyam       firstSubP = 0;
3620*3e98c3d0Sksagiyam       lastSubP  = 0;
3621*3e98c3d0Sksagiyam     }
3622*3e98c3d0Sksagiyam     PetscCall(PetscSectionSetChart(subCoordSection, firstSubP, lastSubP));
3623*3e98c3d0Sksagiyam     for (d = 0; d <= sdim; ++d) {
3624*3e98c3d0Sksagiyam       for (p = 0; p < numSubPoints[d]; ++p) {
3625*3e98c3d0Sksagiyam         const PetscInt point    = subpoints[d][p];
3626*3e98c3d0Sksagiyam         const PetscInt subpoint = firstSubPoint[d] + p;
3627e6ccafaeSMatthew G Knepley         PetscInt       dof;
3628e6ccafaeSMatthew G Knepley 
3629*3e98c3d0Sksagiyam         if (point >= firstP && point < lastP) {
3630*3e98c3d0Sksagiyam           PetscCall(PetscSectionGetDof(coordSection, point, &dof));
3631*3e98c3d0Sksagiyam           PetscCall(PetscSectionSetDof(subCoordSection, subpoint, dof));
3632*3e98c3d0Sksagiyam           PetscCall(PetscSectionSetFieldDof(subCoordSection, subpoint, 0, dof));
3633*3e98c3d0Sksagiyam         }
3634*3e98c3d0Sksagiyam       }
3635e6ccafaeSMatthew G Knepley     }
36369566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
36379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
36389566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
36399566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
36409566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
36419566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
36429566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
36439566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
36449566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
36459566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3646*3e98c3d0Sksagiyam     for (d = 0; d <= sdim; ++d) {
3647*3e98c3d0Sksagiyam       for (p = 0; p < numSubPoints[d]; ++p) {
3648*3e98c3d0Sksagiyam         const PetscInt point    = subpoints[d][p];
3649*3e98c3d0Sksagiyam         const PetscInt subpoint = firstSubPoint[d] + p;
3650e6ccafaeSMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3651e6ccafaeSMatthew G Knepley 
3652*3e98c3d0Sksagiyam         if (point >= firstP && point < lastP) {
3653*3e98c3d0Sksagiyam           PetscCall(PetscSectionGetDof(coordSection, point, &dof));
3654*3e98c3d0Sksagiyam           PetscCall(PetscSectionGetOffset(coordSection, point, &off));
3655*3e98c3d0Sksagiyam           PetscCall(PetscSectionGetDof(subCoordSection, subpoint, &sdof));
3656*3e98c3d0Sksagiyam           PetscCall(PetscSectionGetOffset(subCoordSection, subpoint, &soff));
3657*3e98c3d0Sksagiyam           PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subpoint %" PetscInt_FMT ", point %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subpoint, point, dof);
3658efa14ee0SMatthew G Knepley           for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3659e6ccafaeSMatthew G Knepley         }
3660*3e98c3d0Sksagiyam       }
3661*3e98c3d0Sksagiyam     }
36629566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
36639566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
3664*3e98c3d0Sksagiyam     switch (coordinate_type) {
3665*3e98c3d0Sksagiyam     case 0:
36669566063dSJacob Faibussowitsch       PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
3667*3e98c3d0Sksagiyam       break;
3668*3e98c3d0Sksagiyam     case 1:
3669*3e98c3d0Sksagiyam       PetscCall(DMSetCellCoordinatesLocal(subdm, subCoordinates));
3670*3e98c3d0Sksagiyam       break;
3671*3e98c3d0Sksagiyam     default:
3672*3e98c3d0Sksagiyam       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "coordinate_type must be {0, 1}");
3673*3e98c3d0Sksagiyam     }
36749566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3675e6ccafaeSMatthew G Knepley   }
36763982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
36773982b651SMatthew G. Knepley   {
36783982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
36793982b651SMatthew G. Knepley     IS                 subpIS;
36803982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
36815033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
36825033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3683dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
36845033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
36855033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
36863982b651SMatthew G. Knepley 
36879566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
36885033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
36899566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
36909566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
36919566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
36929566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
36939566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
36943982b651SMatthew G. Knepley     if (subpIS) {
3695dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3696dd05d810SMatthew G. Knepley 
36979566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
36989566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3699dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3700dd05d810SMatthew G. Knepley       if (!sorted) {
3701dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3702dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3703dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3704dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3705dd05d810SMatthew G. Knepley       }
37063982b651SMatthew G. Knepley     }
37079566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
37083982b651SMatthew G. Knepley     if (numRoots >= 0) {
37095033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
37105033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
37119566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
37123982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
37133982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
37143982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
37153982b651SMatthew G. Knepley       }
371630cbcd5dSksagiyam       for (p = pStart; p < pEnd; ++p) {
371730cbcd5dSksagiyam         newOwners[p - pStart].rank  = -3;
371830cbcd5dSksagiyam         newOwners[p - pStart].index = -3;
371930cbcd5dSksagiyam       }
372030cbcd5dSksagiyam       if (sanitizeSubmesh) {
372130cbcd5dSksagiyam         /* A subpoint is forced to be owned by a rank that owns */
372230cbcd5dSksagiyam         /* a subcell that contains the subpoint in its closure. */
372330cbcd5dSksagiyam         PetscInt  cStart, cEnd, c, clSize, cl;
372430cbcd5dSksagiyam         PetscInt *ownedCells, *closure = NULL;
372530cbcd5dSksagiyam 
372630cbcd5dSksagiyam         /* claim ownership */
372730cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
372830cbcd5dSksagiyam           const PetscInt point = subpoints[p];
372930cbcd5dSksagiyam 
373030cbcd5dSksagiyam           newLocalPoints[point - pStart].rank  = rank;
373130cbcd5dSksagiyam           newLocalPoints[point - pStart].index = p;
373230cbcd5dSksagiyam         }
373330cbcd5dSksagiyam         PetscCall(DMGetDimension(subdm, &sdim));
373430cbcd5dSksagiyam         PetscCall(DMPlexGetDepthStratum(dm, sdim, &cStart, &cEnd));
373530cbcd5dSksagiyam         PetscCall(PetscMalloc1(cEnd - cStart, &ownedCells));
373630cbcd5dSksagiyam         for (c = cStart; c < cEnd; ++c) ownedCells[c - cStart] = 0;
373730cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
373830cbcd5dSksagiyam           c = subpoints[p];
373930cbcd5dSksagiyam           if (c >= cStart && c < cEnd) ownedCells[c - cStart] = 1;
374030cbcd5dSksagiyam         }
374130cbcd5dSksagiyam         for (l = 0; l < numLeaves; ++l) {
374230cbcd5dSksagiyam           c = localPoints ? localPoints[l] : l;
374330cbcd5dSksagiyam           if (c >= cStart && c < cEnd) ownedCells[c - cStart] = 0;
374430cbcd5dSksagiyam         }
374530cbcd5dSksagiyam         for (c = cStart; c < cEnd; ++c) {
374630cbcd5dSksagiyam           if (ownedCells[c - cStart] == 0) continue;
374730cbcd5dSksagiyam           PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
374830cbcd5dSksagiyam           for (cl = 0; cl < clSize * 2; cl += 2) {
374930cbcd5dSksagiyam             p = closure[cl];
375030cbcd5dSksagiyam             if (newLocalPoints[p - pStart].rank < size) newLocalPoints[p - pStart].rank += size;
375130cbcd5dSksagiyam           }
375230cbcd5dSksagiyam           PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
375330cbcd5dSksagiyam         }
375430cbcd5dSksagiyam         PetscCall(PetscFree(ownedCells));
375530cbcd5dSksagiyam         for (p = 0; p < numRoots; ++p) {
375630cbcd5dSksagiyam           newOwners[p].rank  = newLocalPoints[p].rank;
375730cbcd5dSksagiyam           newOwners[p].index = newLocalPoints[p].index;
375830cbcd5dSksagiyam         }
375930cbcd5dSksagiyam       } else {
37603982b651SMatthew G. Knepley         /* Set subleaves */
37613982b651SMatthew G. Knepley         for (l = 0; l < numLeaves; ++l) {
37623982b651SMatthew G. Knepley           const PetscInt point    = localPoints[l];
3763dd05d810SMatthew G. Knepley           const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
37643982b651SMatthew G. Knepley 
37653982b651SMatthew G. Knepley           if (subpoint < 0) continue;
37663982b651SMatthew G. Knepley           newLocalPoints[point - pStart].rank  = rank;
37673982b651SMatthew G. Knepley           newLocalPoints[point - pStart].index = subpoint;
37683982b651SMatthew G. Knepley           ++numSubleaves;
37693982b651SMatthew G. Knepley         }
37703982b651SMatthew G. Knepley         /* Must put in owned subpoints */
3771d9384f27SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) {
37725033f954SMatthew G. Knepley           /* Hold on to currently owned points */
37735033f954SMatthew G. Knepley           if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
37745033f954SMatthew G. Knepley           else newOwners[subpoints[p] - pStart].rank = rank;
3775d9384f27SMatthew G. Knepley           newOwners[subpoints[p] - pStart].index = p;
37763982b651SMatthew G. Knepley         }
377730cbcd5dSksagiyam       }
37786497c311SBarry Smith       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
37796497c311SBarry Smith       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
37809371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
37819371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
3782fdb4e32bSksagiyam       if (ownershipTransferSF) {
3783fdb4e32bSksagiyam         PetscSFNode *iremote1 = NULL, *newOwners1 = NULL;
3784fdb4e32bSksagiyam         PetscInt    *ilocal1 = NULL;
3785fdb4e32bSksagiyam         PetscInt     nleaves1, point;
3786fdb4e32bSksagiyam 
3787fdb4e32bSksagiyam         for (p = 0; p < numSubpoints; ++p) {
3788fdb4e32bSksagiyam           point                                = subpoints[p];
3789fdb4e32bSksagiyam           newLocalPoints[point - pStart].index = point;
3790fdb4e32bSksagiyam         }
3791fdb4e32bSksagiyam         PetscCall(PetscMalloc1(numRoots, &newOwners1));
3792fdb4e32bSksagiyam         for (p = 0; p < numRoots; ++p) {
3793fdb4e32bSksagiyam           newOwners1[p].rank  = -1;
3794fdb4e32bSksagiyam           newOwners1[p].index = -1;
3795fdb4e32bSksagiyam         }
37966497c311SBarry Smith         PetscCall(PetscSFReduceBegin(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners1, MPI_MAXLOC));
37976497c311SBarry Smith         PetscCall(PetscSFReduceEnd(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners1, MPI_MAXLOC));
3798fdb4e32bSksagiyam         for (p = 0, nleaves1 = 0; p < numRoots; ++p) {
3799fdb4e32bSksagiyam           if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) { ++nleaves1; }
3800fdb4e32bSksagiyam         }
3801fdb4e32bSksagiyam         PetscCall(PetscMalloc1(nleaves1, &ilocal1));
3802fdb4e32bSksagiyam         PetscCall(PetscMalloc1(nleaves1, &iremote1));
3803fdb4e32bSksagiyam         for (p = 0, nleaves1 = 0; p < numRoots; ++p) {
3804fdb4e32bSksagiyam           if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) {
3805fdb4e32bSksagiyam             ilocal1[nleaves1]        = pStart + p;
3806fdb4e32bSksagiyam             iremote1[nleaves1].rank  = newOwners[p].rank;
3807fdb4e32bSksagiyam             iremote1[nleaves1].index = newOwners1[p].index;
3808fdb4e32bSksagiyam             ++nleaves1;
3809fdb4e32bSksagiyam           }
3810fdb4e32bSksagiyam         }
3811fdb4e32bSksagiyam         PetscCall(PetscFree(newOwners1));
3812fdb4e32bSksagiyam         PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)sfPoint), ownershipTransferSF));
3813fdb4e32bSksagiyam         PetscCall(PetscSFSetFromOptions(*ownershipTransferSF));
3814fdb4e32bSksagiyam         PetscCall(PetscSFSetGraph(*ownershipTransferSF, pEnd - pStart, nleaves1, ilocal1, PETSC_OWN_POINTER, iremote1, PETSC_OWN_POINTER));
3815fdb4e32bSksagiyam       }
381630cbcd5dSksagiyam       if (sanitizeSubmesh) {
381730cbcd5dSksagiyam         for (p = pStart; p < pEnd; ++p) {
381830cbcd5dSksagiyam           newLocalPoints[p - pStart].rank  = newOwners[p - pStart].rank;
381930cbcd5dSksagiyam           newLocalPoints[p - pStart].index = newOwners[p - pStart].index;
382030cbcd5dSksagiyam         }
382130cbcd5dSksagiyam       }
38226497c311SBarry Smith       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
38236497c311SBarry Smith       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
382430cbcd5dSksagiyam       if (sanitizeSubmesh) {
382530cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
382630cbcd5dSksagiyam           const PetscInt point = subpoints[p];
382730cbcd5dSksagiyam 
382830cbcd5dSksagiyam           if (newLocalPoints[point - pStart].rank >= 0 && newLocalPoints[point - pStart].rank != rank) ++sl;
382930cbcd5dSksagiyam         }
383030cbcd5dSksagiyam         PetscCall(PetscMalloc1(sl, &slocalPoints));
383130cbcd5dSksagiyam         PetscCall(PetscMalloc1(sl, &sremotePoints));
383230cbcd5dSksagiyam         for (p = 0, sl = 0; p < numSubpoints; ++p) {
383330cbcd5dSksagiyam           const PetscInt point = subpoints[p];
383430cbcd5dSksagiyam 
383530cbcd5dSksagiyam           if (newLocalPoints[point - pStart].rank >= 0 && newLocalPoints[point - pStart].rank != rank) {
383630cbcd5dSksagiyam             slocalPoints[sl]        = p;
383730cbcd5dSksagiyam             sremotePoints[sl].rank  = newLocalPoints[point - pStart].rank;
383830cbcd5dSksagiyam             sremotePoints[sl].index = newLocalPoints[point - pStart].index;
383930cbcd5dSksagiyam             PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank");
384030cbcd5dSksagiyam             PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint");
384130cbcd5dSksagiyam             ++sl;
384230cbcd5dSksagiyam           }
384330cbcd5dSksagiyam         }
384430cbcd5dSksagiyam       } else {
38459566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
38469566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
38475033f954SMatthew G. Knepley         for (l = 0; l < numLeaves; ++l) {
38483982b651SMatthew G. Knepley           const PetscInt point    = localPoints[l];
3849dd05d810SMatthew G. Knepley           const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
38503982b651SMatthew G. Knepley 
38513982b651SMatthew G. Knepley           if (subpoint < 0) continue;
38529371c9d4SSatish Balay           if (newLocalPoints[point].rank == rank) {
38539371c9d4SSatish Balay             ++ll;
38549371c9d4SSatish Balay             continue;
38559371c9d4SSatish Balay           }
38563982b651SMatthew G. Knepley           slocalPoints[sl]        = subpoint;
38573982b651SMatthew G. Knepley           sremotePoints[sl].rank  = newLocalPoints[point].rank;
38583982b651SMatthew G. Knepley           sremotePoints[sl].index = newLocalPoints[point].index;
385963a3b9bcSJacob Faibussowitsch           PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
386063a3b9bcSJacob Faibussowitsch           PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
38613982b651SMatthew G. Knepley           ++sl;
38623982b651SMatthew G. Knepley         }
38631dca8a05SBarry 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);
386430cbcd5dSksagiyam       }
38659566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
38669566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
38673982b651SMatthew G. Knepley     }
386848a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3869dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
38703982b651SMatthew G. Knepley   }
3871212103e5SMatthew Knepley   /* Filter labels */
38729566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3873efa14ee0SMatthew G Knepley   /* Cleanup */
3874dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
38759566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
38769566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3877e6ccafaeSMatthew G Knepley   }
38789566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
38793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3880e6ccafaeSMatthew G Knepley }
3881e6ccafaeSMatthew G Knepley 
3882d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
3883d71ae5a4SJacob Faibussowitsch {
38843982b651SMatthew G. Knepley   PetscFunctionBegin;
388530cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, PETSC_FALSE, PETSC_FALSE, NULL, subdm));
38863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38873982b651SMatthew G. Knepley }
38883982b651SMatthew G. Knepley 
3889d0fa310fSMatthew G. Knepley /*@
3890e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3891e6ccafaeSMatthew G Knepley 
3892e6ccafaeSMatthew G Knepley   Input Parameters:
3893e6ccafaeSMatthew G Knepley + dm          - The original mesh
389420f4b53cSBarry Smith . vertexLabel - The `DMLabel` marking points contained in the surface
3895158acfadSMatthew G. Knepley . value       - The label value to use
389620f4b53cSBarry Smith - markedFaces - `PETSC_TRUE` if surface faces are marked in addition to vertices, `PETSC_FALSE` if only vertices are marked
3897e6ccafaeSMatthew G Knepley 
3898e6ccafaeSMatthew G Knepley   Output Parameter:
3899e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3900e6ccafaeSMatthew G Knepley 
3901e6ccafaeSMatthew G Knepley   Level: developer
3902e6ccafaeSMatthew G Knepley 
390320f4b53cSBarry Smith   Note:
390420f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
390520f4b53cSBarry Smith 
39061cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3907830e53efSMatthew G. Knepley @*/
3908d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3909d71ae5a4SJacob Faibussowitsch {
3910827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3911827c4036SVaclav Hapla   PetscInt               dim, cdim;
3912e6ccafaeSMatthew G Knepley 
3913e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3914e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39154f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
39169566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
39179566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
39189566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
39199566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
39209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
39219566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
39229566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
392308401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3924827c4036SVaclav Hapla   if (interpolated) {
39259566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3926e6ccafaeSMatthew G Knepley   } else {
39279566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3928e6ccafaeSMatthew G Knepley   }
39295de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
39303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3931e6ccafaeSMatthew G Knepley }
3932e6ccafaeSMatthew G Knepley 
3933d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3934d71ae5a4SJacob Faibussowitsch {
3935766ab985SMatthew G. Knepley   MPI_Comm        comm;
3936766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3937766ab985SMatthew G. Knepley   IS              subvertexIS;
3938766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3939fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3940766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3941412e9a14SMatthew G. Knepley   PetscInt        c, f;
3942766ab985SMatthew G. Knepley 
3943766ab985SMatthew G. Knepley   PetscFunctionBegin;
39449566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3945766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
39469566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
39479566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
39489566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3949766ab985SMatthew G. Knepley   /* Setup chart */
39509566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
39519566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
39529566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
39539566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3954766ab985SMatthew G. Knepley   /* Set cone sizes */
3955766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3956766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3957766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
39589566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
39599566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
396048a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
396148a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
39629566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3963d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3964766ab985SMatthew G. Knepley   /* Create face cones */
39659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
39669566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3967766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3968766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3969766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
397087feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3971064cae4fSPierre Jolivet     PetscBool       isHybrid = PETSC_FALSE;
397287feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3973766ab985SMatthew G. Knepley 
39749566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3975412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
39769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
397787feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
39789566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
397987feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
398087feddfdSMatthew G. Knepley     }
39819566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
39829566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
39839566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
398427234c99SMatthew G. Knepley     /* Not true in parallel
398508401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
398687feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
398787feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3988064cae4fSPierre Jolivet       PetscBool isHybrid = PETSC_FALSE;
3989766ab985SMatthew G. Knepley 
39909566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3991412e9a14SMatthew G. Knepley       if (isHybrid) continue;
399287feddfdSMatthew G. Knepley       /* I know this is a crap search */
399387feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
399487feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3995766ab985SMatthew G. Knepley       }
399663a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
39979566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3998766ab985SMatthew G. Knepley     }
39999566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
400087feddfdSMatthew G. Knepley     ++newFacePoint;
4001766ab985SMatthew G. Knepley   }
40029566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
40039566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
40049566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
4005766ab985SMatthew G. Knepley   /* Build coordinates */
4006766ab985SMatthew G. Knepley   {
4007766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
4008766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
4009766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
4010c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
401124640c55SToby Isaac     const char  *name;
4012766ab985SMatthew G. Knepley 
40139566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
40149566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
40159566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
40169566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
40179566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
40189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
40199566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
40209566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
4021766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
4022766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
4023766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
4024766ab985SMatthew G. Knepley       PetscInt       dof;
4025766ab985SMatthew G. Knepley 
40269566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
40279566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
40289566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
4029766ab985SMatthew G. Knepley     }
40309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
40319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
40329566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
40339566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
40349566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
40359566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
40369566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
40379566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
40389566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
40399566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
4040766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
4041766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
4042766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
4043766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
4044766ab985SMatthew G. Knepley 
40459566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
40469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
40479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
40489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
404963a3b9bcSJacob 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);
4050766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
4051766ab985SMatthew G. Knepley     }
40529566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
40539566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
40549566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
40559566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
4056766ab985SMatthew G. Knepley   }
4057aca35d17SMatthew G. Knepley   /* Build SF */
4058aca35d17SMatthew G. Knepley   CHKMEMQ;
4059aca35d17SMatthew G. Knepley   {
4060aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
4061aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
4062bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
4063aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
4064bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
406549c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
4066bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
4067aca35d17SMatthew G. Knepley 
40689566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
40699566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
40709566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
40719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
40729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
40739566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
4074aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
4075aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
40769566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
4077bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
4078bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
4079bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
4080bdcf2095SMatthew G. Knepley       }
40819e0823b2SMatthew G. Knepley       /* Set subleaves */
4082aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
4083aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
4084bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
4085aca35d17SMatthew G. Knepley 
408663a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
4087bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
4088bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
4089bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
4090bdcf2095SMatthew G. Knepley         ++numSubLeaves;
4091aca35d17SMatthew G. Knepley       }
40929e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
40939e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
40949e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
40959e0823b2SMatthew G. Knepley 
40969e0823b2SMatthew G. Knepley         if (subPoint < 0) {
40979e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
40989e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
40999e0823b2SMatthew G. Knepley         } else {
41009e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
41019e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
41029e0823b2SMatthew G. Knepley         }
4103bdcf2095SMatthew G. Knepley       }
41046497c311SBarry Smith       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
41056497c311SBarry Smith       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
41066497c311SBarry Smith       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
41076497c311SBarry Smith       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
41089566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
41099566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
411049c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
4111bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
4112bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
4113aca35d17SMatthew G. Knepley 
4114aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
41159371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
41169371c9d4SSatish Balay           ++ll;
41179371c9d4SSatish Balay           continue;
41189371c9d4SSatish Balay         }
4119aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
4120bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
4121bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
412263a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
412363a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
4124aca35d17SMatthew G. Knepley         ++sl;
4125aca35d17SMatthew G. Knepley       }
41269566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
41271dca8a05SBarry 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);
41289566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
4129aca35d17SMatthew G. Knepley     }
4130aca35d17SMatthew G. Knepley   }
4131aca35d17SMatthew G. Knepley   CHKMEMQ;
4132766ab985SMatthew G. Knepley   /* Cleanup */
41339566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
41349566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
41359566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
41363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4137766ab985SMatthew G. Knepley }
4138766ab985SMatthew G. Knepley 
4139d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
4140d71ae5a4SJacob Faibussowitsch {
41413982b651SMatthew G. Knepley   DMLabel label = NULL;
4142766ab985SMatthew G. Knepley 
4143766ab985SMatthew G. Knepley   PetscFunctionBegin;
41449566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
414530cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, PETSC_FALSE, PETSC_FALSE, NULL, subdm));
41463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4147766ab985SMatthew G. Knepley }
4148766ab985SMatthew G. Knepley 
4149cc4c1da9SBarry Smith /*@
415020f4b53cSBarry 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.
4151766ab985SMatthew G. Knepley 
4152766ab985SMatthew G. Knepley   Input Parameters:
4153766ab985SMatthew G. Knepley + dm          - The original mesh
415427c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
415520f4b53cSBarry Smith . label       - A label name, or `NULL`
415627c04023SMatthew G. Knepley - value       - A label value
4157766ab985SMatthew G. Knepley 
4158766ab985SMatthew G. Knepley   Output Parameter:
4159766ab985SMatthew G. Knepley . subdm - The surface mesh
4160766ab985SMatthew G. Knepley 
4161766ab985SMatthew G. Knepley   Level: developer
4162766ab985SMatthew G. Knepley 
416320f4b53cSBarry Smith   Note:
416420f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
416520f4b53cSBarry Smith 
41661cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
4167c08575a3SMatthew G. Knepley @*/
4168d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
4169d71ae5a4SJacob Faibussowitsch {
4170c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
4171766ab985SMatthew G. Knepley 
4172766ab985SMatthew G. Knepley   PetscFunctionBegin;
4173766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41744f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
41759566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
41769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
41779566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
41789566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
41799566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
41809566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
41819566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
4182766ab985SMatthew G. Knepley   if (depth == dim) {
41839566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
4184766ab985SMatthew G. Knepley   } else {
41859566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
4186e6ccafaeSMatthew G Knepley   }
41875de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
41883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4189e6ccafaeSMatthew G Knepley }
4190e6ccafaeSMatthew G Knepley 
4191bec263e5SMatthew G. Knepley /*@
41920e18dc48SMatthew G. Knepley   DMPlexReorderCohesiveSupports - Ensure that face supports for cohesive end caps are ordered
41930e18dc48SMatthew G. Knepley 
41940e18dc48SMatthew G. Knepley   Not Collective
41950e18dc48SMatthew G. Knepley 
41960e18dc48SMatthew G. Knepley   Input Parameter:
41970e18dc48SMatthew G. Knepley . dm - The `DM` containing cohesive cells
41980e18dc48SMatthew G. Knepley 
41990e18dc48SMatthew G. Knepley   Level: developer
42000e18dc48SMatthew G. Knepley 
4201a4e35b19SJacob Faibussowitsch   Note:
4202a4e35b19SJacob Faibussowitsch   For the negative size (first) face, the cohesive cell should be first in the support, and for
4203a4e35b19SJacob Faibussowitsch   the positive side (second) face, the cohesive cell should be second in the support.
42040e18dc48SMatthew G. Knepley 
42050e18dc48SMatthew G. Knepley .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexCreateCohesiveSubmesh()`
42060e18dc48SMatthew G. Knepley @*/
42070e18dc48SMatthew G. Knepley PetscErrorCode DMPlexReorderCohesiveSupports(DM dm)
42080e18dc48SMatthew G. Knepley {
42090e18dc48SMatthew G. Knepley   PetscInt dim, cStart, cEnd;
42100e18dc48SMatthew G. Knepley 
42110e18dc48SMatthew G. Knepley   PetscFunctionBegin;
42120e18dc48SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
42130e18dc48SMatthew G. Knepley   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cStart, &cEnd));
42140e18dc48SMatthew G. Knepley   for (PetscInt c = cStart; c < cEnd; ++c) {
42150e18dc48SMatthew G. Knepley     const PetscInt *cone;
42160e18dc48SMatthew G. Knepley     PetscInt        coneSize;
42170e18dc48SMatthew G. Knepley 
42180e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
42190e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetCone(dm, c, &cone));
42200e18dc48SMatthew G. Knepley     PetscCheck(coneSize >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid cell %" PetscInt_FMT " cone size %" PetscInt_FMT " must be >= 2", c, coneSize);
42210e18dc48SMatthew G. Knepley     for (PetscInt s = 0; s < 2; ++s) {
42220e18dc48SMatthew G. Knepley       const PetscInt *supp;
42230e18dc48SMatthew G. Knepley       PetscInt        suppSize, newsupp[2];
42240e18dc48SMatthew G. Knepley 
42250e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupportSize(dm, cone[s], &suppSize));
42260e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupport(dm, cone[s], &supp));
42270e18dc48SMatthew G. Knepley       if (suppSize == 2) {
42280e18dc48SMatthew G. Knepley         /* Reorder hybrid end cap support */
42290e18dc48SMatthew G. Knepley         if (supp[s] == c) {
42300e18dc48SMatthew G. Knepley           newsupp[0] = supp[1];
42310e18dc48SMatthew G. Knepley           newsupp[1] = supp[0];
42320e18dc48SMatthew G. Knepley         } else {
42330e18dc48SMatthew G. Knepley           newsupp[0] = supp[0];
42340e18dc48SMatthew G. Knepley           newsupp[1] = supp[1];
42350e18dc48SMatthew G. Knepley         }
42360e18dc48SMatthew 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);
42370e18dc48SMatthew G. Knepley         PetscCall(DMPlexSetSupport(dm, cone[s], newsupp));
42380e18dc48SMatthew G. Knepley       }
42390e18dc48SMatthew G. Knepley     }
42400e18dc48SMatthew G. Knepley   }
42410e18dc48SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42420e18dc48SMatthew G. Knepley }
42430e18dc48SMatthew G. Knepley 
42440e18dc48SMatthew G. Knepley /*@
4245bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
4246bec263e5SMatthew G. Knepley 
4247bec263e5SMatthew G. Knepley   Input Parameters:
4248bec263e5SMatthew G. Knepley + dm              - The original mesh
424920f4b53cSBarry Smith . cellLabel       - The `DMLabel` marking cells contained in the new mesh
42504d00ad26Sksagiyam . value           - The label value to use
425130cbcd5dSksagiyam . ignoreLabelHalo - The flag indicating if labeled points that are in the halo are ignored
425230cbcd5dSksagiyam - 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
4253bec263e5SMatthew G. Knepley 
42549cde84edSJose E. Roman   Output Parameters:
4255fdb4e32bSksagiyam + ownershipTransferSF - The `PetscSF` representing the ownership transfers between parent local meshes due to submeshing.
4256fdb4e32bSksagiyam - subdm               - The new mesh
4257bec263e5SMatthew G. Knepley 
4258bec263e5SMatthew G. Knepley   Level: developer
4259bec263e5SMatthew G. Knepley 
426020f4b53cSBarry Smith   Note:
426120f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
426220f4b53cSBarry Smith 
4263fdb4e32bSksagiyam   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
4264fdb4e32bSksagiyam   the remote locations for these leaves are tuples of rank and point that represent the new owners.
4265fdb4e32bSksagiyam 
42661cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
4267bec263e5SMatthew G. Knepley @*/
426830cbcd5dSksagiyam PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, PetscBool ignoreLabelHalo, PetscBool sanitizeSubmesh, PetscSF *ownershipTransferSF, DM *subdm)
4269d71ae5a4SJacob Faibussowitsch {
4270b47cd10cSMatthew G. Knepley   PetscInt dim, overlap;
4271bec263e5SMatthew G. Knepley 
4272bec263e5SMatthew G. Knepley   PetscFunctionBegin;
4273bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
427430cbcd5dSksagiyam   if (ownershipTransferSF) PetscAssertPointer(ownershipTransferSF, 6);
427530cbcd5dSksagiyam   PetscAssertPointer(subdm, 7);
42769566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
42779566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
42789566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
4279bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
428030cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, ignoreLabelHalo, sanitizeSubmesh, ownershipTransferSF, *subdm));
42815de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
4282b47cd10cSMatthew G. Knepley   // It is possible to obtain a surface mesh where some faces are in SF
4283b47cd10cSMatthew G. Knepley   //   We should either mark the mesh as having an overlap, or delete these from the SF
4284b47cd10cSMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
4285b47cd10cSMatthew G. Knepley   if (!overlap) {
4286b47cd10cSMatthew G. Knepley     PetscSF         sf;
4287b47cd10cSMatthew G. Knepley     const PetscInt *leaves;
4288b47cd10cSMatthew G. Knepley     PetscInt        cStart, cEnd, Nl;
4289b47cd10cSMatthew G. Knepley     PetscBool       hasSubcell = PETSC_FALSE, ghasSubcell;
4290b47cd10cSMatthew G. Knepley 
4291b47cd10cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd));
4292b47cd10cSMatthew G. Knepley     PetscCall(DMGetPointSF(*subdm, &sf));
4293b47cd10cSMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
4294b47cd10cSMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
4295b47cd10cSMatthew G. Knepley       const PetscInt point = leaves ? leaves[l] : l;
4296b47cd10cSMatthew G. Knepley 
4297b47cd10cSMatthew G. Knepley       if (point >= cStart && point < cEnd) {
4298b47cd10cSMatthew G. Knepley         hasSubcell = PETSC_TRUE;
4299b47cd10cSMatthew G. Knepley         break;
4300b47cd10cSMatthew G. Knepley       }
4301b47cd10cSMatthew G. Knepley     }
4302462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
4303b47cd10cSMatthew G. Knepley     if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1));
4304b47cd10cSMatthew G. Knepley   }
43053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4306bec263e5SMatthew G. Knepley }
4307bec263e5SMatthew G. Knepley 
430864beef6dSMatthew G. Knepley /*@
430920f4b53cSBarry Smith   DMPlexGetSubpointMap - Returns a `DMLabel` with point dimension as values
431064beef6dSMatthew G. Knepley 
431164beef6dSMatthew G. Knepley   Input Parameter:
431220f4b53cSBarry Smith . dm - The submesh `DM`
431364beef6dSMatthew G. Knepley 
431464beef6dSMatthew G. Knepley   Output Parameter:
431520f4b53cSBarry Smith . subpointMap - The `DMLabel` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
431664beef6dSMatthew G. Knepley 
431764beef6dSMatthew G. Knepley   Level: developer
431864beef6dSMatthew G. Knepley 
43191cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
432064beef6dSMatthew G. Knepley @*/
4321d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
4322d71ae5a4SJacob Faibussowitsch {
4323e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4324e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43254f572ea9SToby Isaac   PetscAssertPointer(subpointMap, 2);
432665663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
43273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4328e6ccafaeSMatthew G Knepley }
4329e6ccafaeSMatthew G Knepley 
4330c08575a3SMatthew G. Knepley /*@
433120f4b53cSBarry Smith   DMPlexSetSubpointMap - Sets the `DMLabel` with point dimension as values
4332c08575a3SMatthew G. Knepley 
4333c08575a3SMatthew G. Knepley   Input Parameters:
433420f4b53cSBarry Smith + dm          - The submesh `DM`
433520f4b53cSBarry Smith - subpointMap - The `DMLabel` of all the points from the original mesh in this submesh
4336c08575a3SMatthew G. Knepley 
4337c08575a3SMatthew G. Knepley   Level: developer
4338c08575a3SMatthew G. Knepley 
433920f4b53cSBarry Smith   Note:
434020f4b53cSBarry Smith   Should normally not be called by the user, since it is set in `DMPlexCreateSubmesh()`
434120f4b53cSBarry Smith 
43421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
4343c08575a3SMatthew G. Knepley @*/
4344d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
4345d71ae5a4SJacob Faibussowitsch {
4346e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
4347285d324eSMatthew G. Knepley   DMLabel  tmp;
4348e6ccafaeSMatthew G Knepley 
4349e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4350e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4351285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
4352e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
43539566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
43549566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
43553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4356e6ccafaeSMatthew G Knepley }
4357e6ccafaeSMatthew G Knepley 
4358d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
4359d71ae5a4SJacob Faibussowitsch {
436097d8846cSMatthew Knepley   DMLabel  spmap;
436197d8846cSMatthew Knepley   PetscInt depth, d;
436297d8846cSMatthew Knepley 
436397d8846cSMatthew Knepley   PetscFunctionBegin;
43649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
43659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
436697d8846cSMatthew Knepley   if (spmap && depth >= 0) {
436797d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
436897d8846cSMatthew Knepley     PetscInt *points, *depths;
436997d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
437097d8846cSMatthew Knepley 
43719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
437263a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
43739566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
43749566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
437597d8846cSMatthew Knepley     depths[0] = depth;
437697d8846cSMatthew Knepley     depths[1] = 0;
4377ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
437897d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
437997d8846cSMatthew Knepley       const PetscInt dep = depths[d];
438097d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
438197d8846cSMatthew Knepley 
43829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
43839566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
438497d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
438563a3b9bcSJacob 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);
438697d8846cSMatthew Knepley       } else {
438797d8846cSMatthew Knepley         if (!n) {
438897d8846cSMatthew Knepley           if (d == 0) {
438997d8846cSMatthew Knepley             /* Missing cells */
439097d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
439197d8846cSMatthew Knepley           } else {
439297d8846cSMatthew Knepley             /* Missing faces */
43931690c2aeSBarry Smith             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_INT_MAX;
439497d8846cSMatthew Knepley           }
439597d8846cSMatthew Knepley         }
439697d8846cSMatthew Knepley       }
439797d8846cSMatthew Knepley       if (n) {
439897d8846cSMatthew Knepley         IS              is;
439997d8846cSMatthew Knepley         const PetscInt *opoints;
440097d8846cSMatthew Knepley 
44019566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
44029566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
440397d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
44049566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
44059566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
440697d8846cSMatthew Knepley       }
440797d8846cSMatthew Knepley     }
44089566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
440963a3b9bcSJacob 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);
44109566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
44119566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
441297d8846cSMatthew Knepley   }
44133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
441497d8846cSMatthew Knepley }
441597d8846cSMatthew Knepley 
441664beef6dSMatthew G. Knepley /*@
441720f4b53cSBarry Smith   DMPlexGetSubpointIS - Returns an `IS` covering the entire subdm chart with the original points as data
4418e6ccafaeSMatthew G Knepley 
4419e6ccafaeSMatthew G Knepley   Input Parameter:
442020f4b53cSBarry Smith . dm - The submesh `DM`
4421e6ccafaeSMatthew G Knepley 
4422e6ccafaeSMatthew G Knepley   Output Parameter:
442320f4b53cSBarry Smith . subpointIS - The `IS` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
442464beef6dSMatthew G. Knepley 
442564beef6dSMatthew G. Knepley   Level: developer
442664beef6dSMatthew G. Knepley 
442720f4b53cSBarry Smith   Note:
442829d2e75bSMatthew G. Knepley   This `IS` is guaranteed to be sorted by the construction of the submesh. However, if the filtering operation removes an entire stratum, then the strata in the submesh can be in a different order, and the `subpointIS` will only be sorted within each stratum.
442920f4b53cSBarry Smith 
44301cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
443164beef6dSMatthew G. Knepley @*/
4432d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
4433d71ae5a4SJacob Faibussowitsch {
443497d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
443597d8846cSMatthew Knepley   DMLabel          spmap;
443697d8846cSMatthew Knepley   PetscObjectState state;
4437e6ccafaeSMatthew G Knepley 
4438e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4439e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44404f572ea9SToby Isaac   PetscAssertPointer(subpointIS, 2);
44419566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
44429566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
44439566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
444497d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
44453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4446e6ccafaeSMatthew G Knepley }
4447559a1558SMatthew G. Knepley 
4448559a1558SMatthew G. Knepley /*@
444920f4b53cSBarry Smith   DMGetEnclosureRelation - Get the relationship between `dmA` and `dmB`
4450559a1558SMatthew G. Knepley 
4451559a1558SMatthew G. Knepley   Input Parameters:
445220f4b53cSBarry Smith + dmA - The first `DM`
445320f4b53cSBarry Smith - dmB - The second `DM`
4454559a1558SMatthew G. Knepley 
4455559a1558SMatthew G. Knepley   Output Parameter:
445620f4b53cSBarry Smith . rel - The relation of `dmA` to `dmB`
4457559a1558SMatthew G. Knepley 
4458a6e0b375SMatthew G. Knepley   Level: intermediate
4459559a1558SMatthew G. Knepley 
44601cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosurePoint()`
4461559a1558SMatthew G. Knepley @*/
4462d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4463d71ae5a4SJacob Faibussowitsch {
4464a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
4465559a1558SMatthew G. Knepley   DMLabel  spmap;
4466a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4467559a1558SMatthew G. Knepley 
446844171101SMatthew G. Knepley   PetscFunctionBegin;
44694f572ea9SToby Isaac   PetscAssertPointer(rel, 3);
4470a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
44713ba16761SJacob Faibussowitsch   if (!dmA || !dmB) PetscFunctionReturn(PETSC_SUCCESS);
4472a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4473064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
44749371c9d4SSatish Balay   if (dmA == dmB) {
44759371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
44763ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
44779371c9d4SSatish Balay   }
44789566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
44799566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
44809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
44819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4482a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4483a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4484a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
4485a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4486a6e0b375SMatthew G. Knepley     goto end;
4487559a1558SMatthew G. Knepley   }
4488a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4489a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4490a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4491a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
44929566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4493a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4494a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4495a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4496a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4497a6e0b375SMatthew G. Knepley   } else {
4498a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4499a6e0b375SMatthew G. Knepley   }
4500a6e0b375SMatthew G. Knepley end:
45019566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
45029566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
45033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4504559a1558SMatthew G. Knepley }
450544171101SMatthew G. Knepley 
450644171101SMatthew G. Knepley /*@
450720f4b53cSBarry Smith   DMGetEnclosurePoint - Get the point `pA` in `dmA` which corresponds to the point `pB` in `dmB`
450844171101SMatthew G. Knepley 
450944171101SMatthew G. Knepley   Input Parameters:
451020f4b53cSBarry Smith + dmA   - The first `DM`
451120f4b53cSBarry Smith . dmB   - The second `DM`
451220f4b53cSBarry Smith . etype - The type of enclosure relation that `dmA` has to `dmB`
451320f4b53cSBarry Smith - pB    - A point of `dmB`
451444171101SMatthew G. Knepley 
451544171101SMatthew G. Knepley   Output Parameter:
451620f4b53cSBarry Smith . pA - The corresponding point of `dmA`
451744171101SMatthew G. Knepley 
4518a6e0b375SMatthew G. Knepley   Level: intermediate
451944171101SMatthew G. Knepley 
45201cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosureRelation()`
452144171101SMatthew G. Knepley @*/
4522d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
4523d71ae5a4SJacob Faibussowitsch {
4524a6e0b375SMatthew G. Knepley   DM              sdm;
4525a6e0b375SMatthew G. Knepley   IS              subpointIS;
4526a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4527a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
452844171101SMatthew G. Knepley 
452944171101SMatthew G. Knepley   PetscFunctionBegin;
4530a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4531a6e0b375SMatthew G. Knepley   switch (etype) {
4532a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4533a6e0b375SMatthew G. Knepley     sdm = dmB;
45349566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
45359566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4536a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
45379566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4538a6e0b375SMatthew G. Knepley     break;
4539a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4540a6e0b375SMatthew G. Knepley     sdm = dmA;
45419566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
45429566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
45439566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
45449566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4545a6e0b375SMatthew G. Knepley     if (*pA < 0) {
45469566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
45479566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
454863a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4549a6e0b375SMatthew G. Knepley     }
45509566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4551a6e0b375SMatthew G. Knepley     break;
4552a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
4553d71ae5a4SJacob Faibussowitsch   case DM_ENC_NONE:
4554d71ae5a4SJacob Faibussowitsch     *pA = pB;
4555d71ae5a4SJacob Faibussowitsch     break;
45569371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4557a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
455844171101SMatthew G. Knepley 
45599566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
45609566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
45619371c9d4SSatish Balay   } break;
4562d71ae5a4SJacob Faibussowitsch   default:
4563d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
456444171101SMatthew G. Knepley   }
45653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
456644171101SMatthew G. Knepley }
4567