xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 7bd9760ac786f3efbeabeb96db6569dc808189c6)
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));
7141e3e744Sksagiyam   leafData -= pStart;
7241e3e744Sksagiyam   PetscCall(DMGetPointSF(dm, &sf));
7341e3e744Sksagiyam   if (sf) PetscCall(PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, &iremote));
7441e3e744Sksagiyam   if (sf && nroots >= 0) {
7541e3e744Sksagiyam     PetscInt        cStart, cEnd, c, i;
7641e3e744Sksagiyam     PetscInt       *rootData, *rootData1, *cellOwners, hasTwoSupportCells = -2;
7741e3e744Sksagiyam     const PetscInt *support;
7841e3e744Sksagiyam     PetscMPIInt     rank;
7930560a7bSMatthew G. Knepley 
8041e3e744Sksagiyam     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8141e3e744Sksagiyam     PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8241e3e744Sksagiyam     PetscCall(PetscCalloc3(pEnd - pStart, &rootData, pEnd - pStart, &rootData1, cEnd - cStart, &cellOwners));
8341e3e744Sksagiyam     rootData -= pStart;
8441e3e744Sksagiyam     rootData1 -= pStart;
8541e3e744Sksagiyam     for (c = cStart; c < cEnd; ++c) cellOwners[c - cStart] = (PetscInt)rank;
8641e3e744Sksagiyam     for (i = 0; i < nleaves; ++i) {
8741e3e744Sksagiyam       c = ilocal ? ilocal[i] : i;
8841e3e744Sksagiyam       if (c >= cStart && c < cEnd) cellOwners[c - cStart] = iremote[i].rank;
8941e3e744Sksagiyam     }
9041e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
92e752be1aSMatthew G. Knepley       if (supportSize == 1) {
9341e3e744Sksagiyam         PetscCall(DMPlexGetSupport(dm, f, &support));
9441e3e744Sksagiyam         leafData[f] = cellOwners[support[0] - cStart];
9541e3e744Sksagiyam       } else {
9641e3e744Sksagiyam         /* TODO: When using DMForest, we could have a parent facet (a coarse facet)     */
9741e3e744Sksagiyam         /*       supportSize of which alone does not tell us if it is an interior       */
9841e3e744Sksagiyam         /*       facet or an exterior facet. Those facets can be identified by checking */
9941e3e744Sksagiyam         /*       if they are in the parent tree. We should probably skip those parent   */
10041e3e744Sksagiyam         /*       facets here, which will allow for including the following check, and   */
10141e3e744Sksagiyam         /*       see if they are exterior facets or not afterwards by checking if the   */
10241e3e744Sksagiyam         /*       children are exterior or not.                                          */
10341e3e744Sksagiyam         /* PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", supportSize, f); */
10441e3e744Sksagiyam         leafData[f] = hasTwoSupportCells; /* some negative PetscInt */
105429fa399SMatthew G. Knepley       }
10641e3e744Sksagiyam       rootData[f]  = leafData[f];
10741e3e744Sksagiyam       rootData1[f] = leafData[f];
10841e3e744Sksagiyam     }
10941e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData, MPI_MIN));
11041e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
11141e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData, MPI_MIN));
11241e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
11341e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
11441e3e744Sksagiyam       /* Store global support size of f.                                                    */
11541e3e744Sksagiyam       /* Facet f is an interior facet if and only if one of the following two is satisfied: */
11641e3e744Sksagiyam       /* 1. supportSize is 2 on some rank.                                                  */
11741e3e744Sksagiyam       /* 2. supportSize is 1 on any rank that can see f, but f is on a partition boundary;  */
11841e3e744Sksagiyam       /*    i.e., rootData[f] < rootData1[f].                                               */
11941e3e744Sksagiyam       rootData[f] = (rootData[f] == hasTwoSupportCells || (rootData[f] < rootData1[f])) ? 2 : 1;
12041e3e744Sksagiyam       leafData[f] = rootData[f];
12141e3e744Sksagiyam     }
12241e3e744Sksagiyam     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
12341e3e744Sksagiyam     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
12441e3e744Sksagiyam     rootData += pStart;
12541e3e744Sksagiyam     rootData1 += pStart;
12641e3e744Sksagiyam     PetscCall(PetscFree3(rootData, rootData1, cellOwners));
12741e3e744Sksagiyam   } else {
12841e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
12941e3e744Sksagiyam       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
13041e3e744Sksagiyam       leafData[f] = supportSize;
13141e3e744Sksagiyam     }
13241e3e744Sksagiyam   }
13341e3e744Sksagiyam   for (f = fStart; f < fEnd; ++f) {
13441e3e744Sksagiyam     if (leafData[f] == 1) {
135e752be1aSMatthew G. Knepley       if (val < 0) {
136e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
137e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
138e752be1aSMatthew G. Knepley 
13969941377SStefano Zampini         PetscAssert(!missing_only, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented");
1409566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
141e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize * 2; cl += 2) {
1429566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, closure[cl], &cval));
143e752be1aSMatthew G. Knepley           if (cval < 0) continue;
1449566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, cval));
145e752be1aSMatthew G. Knepley           break;
146e752be1aSMatthew G. Knepley         }
1479566063dSJacob Faibussowitsch         if (cl == clSize * 2) PetscCall(DMLabelSetValue(label, f, 1));
1489566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
149e752be1aSMatthew G. Knepley       } else {
15069941377SStefano Zampini         if (missing_only) {
15169941377SStefano Zampini           PetscInt fval;
15269941377SStefano Zampini           PetscCall(DMLabelGetValue(label, f, &fval));
15369941377SStefano Zampini           if (fval != defval) PetscCall(DMLabelClearValue(label, f, fval));
15469941377SStefano Zampini           else PetscCall(DMLabelSetValue(label, f, val));
15569941377SStefano Zampini         } else {
1569566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, val));
157e752be1aSMatthew G. Knepley         }
15869941377SStefano Zampini       }
15941e3e744Sksagiyam     } else {
16041e3e744Sksagiyam       /* TODO: See the above comment on DMForest */
16141e3e744Sksagiyam       /* PetscCheck(leafData[f] == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", leafData[f], f); */
162e752be1aSMatthew G. Knepley     }
16330560a7bSMatthew G. Knepley   }
16441e3e744Sksagiyam   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 {
192827c4036SVaclav Hapla   DMPlexInterpolatedFlag flg;
193cd0c2139SMatthew G Knepley 
194cd0c2139SMatthew G Knepley   PetscFunctionBegin;
195827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1969566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &flg));
19708401ef6SPierre Jolivet   PetscCheck(flg == DMPLEX_INTERPOLATED_FULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
19869941377SStefano Zampini   PetscCall(DMPlexMarkBoundaryFaces_Internal(dm, val, 0, label, PETSC_FALSE));
1993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
200cd0c2139SMatthew G Knepley }
201cd0c2139SMatthew G Knepley 
202d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
203d71ae5a4SJacob Faibussowitsch {
204b0bf5782SToby Isaac   IS              valueIS;
205ac51f24eSSander Arens   PetscSF         sfPoint;
206b0bf5782SToby Isaac   const PetscInt *values;
207ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
208b0bf5782SToby Isaac 
209b0bf5782SToby Isaac   PetscFunctionBegin;
2109566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2119566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2139566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
214b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
215b0bf5782SToby Isaac     IS              pointIS;
216b0bf5782SToby Isaac     const PetscInt *points;
217b0bf5782SToby Isaac     PetscInt        numPoints, p;
218b0bf5782SToby Isaac 
2199566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2209566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2219566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
222b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
223b0bf5782SToby Isaac       PetscInt  q       = points[p];
224b0bf5782SToby Isaac       PetscInt *closure = NULL;
225b0bf5782SToby Isaac       PetscInt  closureSize, c;
226b0bf5782SToby Isaac 
227b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
228b0bf5782SToby Isaac         continue;
229b0bf5782SToby Isaac       }
2309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
23148a46eb9SPierre Jolivet       for (c = 0; c < closureSize * 2; c += 2) PetscCall(DMLabelSetValue(label, closure[c], values[v]));
2329566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
233b0bf5782SToby Isaac     }
2349566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2359566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
236b0bf5782SToby Isaac   }
2379566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2389566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2399566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
2409566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
241ac51f24eSSander Arens   if (nroots >= 0) {
24226279d81SSanderA     DMLabel         lblRoots, lblLeaves;
24326279d81SSanderA     IS              valueIS, pointIS;
24426279d81SSanderA     const PetscInt *values;
24526279d81SSanderA     PetscInt        numValues, v;
24626279d81SSanderA 
24726279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
2489566063dSJacob Faibussowitsch     PetscCall(DMLabelGather(label, sfPoint, &lblLeaves));
2499566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS));
2509566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2519566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
25226279d81SSanderA     for (v = 0; v < numValues; ++v) {
25326279d81SSanderA       const PetscInt value = values[v];
25426279d81SSanderA 
2559566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
2569566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2579566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
25826279d81SSanderA     }
2599566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2609566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2619566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblLeaves));
26226279d81SSanderA     /* Push point contributions from roots into remote leaves */
2639566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots));
2649566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblRoots, &valueIS));
2659566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2669566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
26726279d81SSanderA     for (v = 0; v < numValues; ++v) {
26826279d81SSanderA       const PetscInt value = values[v];
26926279d81SSanderA 
2709566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS));
2719566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2729566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
27326279d81SSanderA     }
2749566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2759566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2769566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblRoots));
27726279d81SSanderA   }
2783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
279b0bf5782SToby Isaac }
280b0bf5782SToby Isaac 
2812be2b188SMatthew G Knepley /*@
2822be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
2832be2b188SMatthew G Knepley 
2842be2b188SMatthew G Knepley   Input Parameters:
28520f4b53cSBarry Smith + dm    - The `DM`
28620f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
2872be2b188SMatthew G Knepley 
2882be2b188SMatthew G Knepley   Output Parameter:
28920f4b53cSBarry Smith . label - A `DMLabel` marking all surface points in the transitive closure
2902be2b188SMatthew G Knepley 
2912be2b188SMatthew G Knepley   Level: developer
2922be2b188SMatthew G Knepley 
2931cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelCohesiveComplete()`
2942be2b188SMatthew G Knepley @*/
295d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
296d71ae5a4SJacob Faibussowitsch {
2972be2b188SMatthew G Knepley   PetscFunctionBegin;
2989566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
2993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3002be2b188SMatthew G Knepley }
3012be2b188SMatthew G Knepley 
3026cf0e42fSMatthew G. Knepley /*@
303a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
3046cf0e42fSMatthew G. Knepley 
3056cf0e42fSMatthew G. Knepley   Input Parameters:
30620f4b53cSBarry Smith + dm    - The `DM`
30720f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
3086cf0e42fSMatthew G. Knepley 
3096cf0e42fSMatthew G. Knepley   Output Parameter:
31020f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
3116cf0e42fSMatthew G. Knepley 
3126cf0e42fSMatthew G. Knepley   Level: developer
3136cf0e42fSMatthew G. Knepley 
31420f4b53cSBarry Smith   Note:
31520f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
3166cf0e42fSMatthew G. Knepley 
3171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
3186cf0e42fSMatthew G. Knepley @*/
319d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
320d71ae5a4SJacob Faibussowitsch {
3216cf0e42fSMatthew G. Knepley   IS              valueIS;
3226cf0e42fSMatthew G. Knepley   const PetscInt *values;
3239df54392SMatthew G. Knepley   PetscInt        numValues, v, csStart, csEnd, chStart, chEnd;
3246cf0e42fSMatthew G. Knepley 
3256cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
3269df54392SMatthew G. Knepley   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &csStart, &csEnd));
3279df54392SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(dm, 0, &chStart, &chEnd));
3289566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3299566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3309566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
3316cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
3326cf0e42fSMatthew G. Knepley     IS              pointIS;
3336cf0e42fSMatthew G. Knepley     const PetscInt *points;
3346cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
3356cf0e42fSMatthew G. Knepley 
3369566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3379566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3389566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
3396cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3409df54392SMatthew G. Knepley       const PetscInt point   = points[p];
3416cf0e42fSMatthew G. Knepley       PetscInt      *closure = NULL;
342*7bd9760aSJed Brown       PetscInt       closureSize, cl, h, cStart, cEnd;
343*7bd9760aSJed Brown       DMPolytopeType ct;
3449df54392SMatthew G. Knepley 
3459df54392SMatthew G. Knepley       // If the point is a hybrid, allow hybrid cells
346*7bd9760aSJed Brown       PetscCall(DMPlexGetCellType(dm, point, &ct));
3479df54392SMatthew G. Knepley       PetscCall(DMPlexGetPointHeight(dm, point, &h));
348*7bd9760aSJed Brown       if (DMPolytopeTypeIsHybrid(ct)) {
3499df54392SMatthew G. Knepley         cStart = chStart;
3509df54392SMatthew G. Knepley         cEnd   = chEnd;
351*7bd9760aSJed Brown       } else {
352*7bd9760aSJed Brown         cStart = csStart;
353*7bd9760aSJed Brown         cEnd   = csEnd;
3549df54392SMatthew G. Knepley       }
3556cf0e42fSMatthew G. Knepley 
3569566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
35722eabd52SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
358a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
3599371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
3609371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
3619371c9d4SSatish Balay           break;
3629371c9d4SSatish Balay         }
36322eabd52SMatthew G. Knepley       }
3649566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
3656cf0e42fSMatthew G. Knepley     }
3669566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3679566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3686cf0e42fSMatthew G. Knepley   }
3699566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3709566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
3713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3726cf0e42fSMatthew G. Knepley }
3736cf0e42fSMatthew G. Knepley 
374f402d5e4SToby Isaac /*@
375a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
376a6e0b375SMatthew G. Knepley 
377a6e0b375SMatthew G. Knepley   Input Parameters:
37820f4b53cSBarry Smith + dm    - The `DM`
37920f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
380a6e0b375SMatthew G. Knepley 
381a6e0b375SMatthew G. Knepley   Output Parameter:
38220f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
383a6e0b375SMatthew G. Knepley 
384a6e0b375SMatthew G. Knepley   Level: developer
385a6e0b375SMatthew G. Knepley 
38620f4b53cSBarry Smith   Note:
38720f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
388a6e0b375SMatthew G. Knepley 
3891cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
390a6e0b375SMatthew G. Knepley @*/
391d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
392d71ae5a4SJacob Faibussowitsch {
393a6e0b375SMatthew G. Knepley   IS              valueIS;
394a6e0b375SMatthew G. Knepley   const PetscInt *values;
395a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
396a6e0b375SMatthew G. Knepley 
397a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
3989566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
3999566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
4009566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4019566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4029566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
403a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
404a6e0b375SMatthew G. Knepley     IS              pointIS;
405a6e0b375SMatthew G. Knepley     const PetscInt *points;
406a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
407a6e0b375SMatthew G. Knepley 
4089566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4099566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4109566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
411a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
412a6e0b375SMatthew G. Knepley       const PetscInt face    = points[p];
413a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
414a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
415a6e0b375SMatthew G. Knepley 
416a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
4179566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
418a6e0b375SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
419a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
4209371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
4219371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
4229371c9d4SSatish Balay           break;
4239371c9d4SSatish Balay         }
424a6e0b375SMatthew G. Knepley       }
4259566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
426a6e0b375SMatthew G. Knepley     }
4279566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4289566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
429a6e0b375SMatthew G. Knepley   }
4309566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4319566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
433a6e0b375SMatthew G. Knepley }
434a6e0b375SMatthew G. Knepley 
435a6e0b375SMatthew G. Knepley /*@
436f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
437f402d5e4SToby Isaac 
438f402d5e4SToby Isaac   Input Parameters:
43920f4b53cSBarry Smith + dm    - The `DM`
44020f4b53cSBarry Smith - label - A `DMLabel` marking surface points and their adjacent cells
441f402d5e4SToby Isaac 
442f402d5e4SToby Isaac   Output Parameter:
44320f4b53cSBarry Smith . label - A `DMLabel` without cells
444f402d5e4SToby Isaac 
445f402d5e4SToby Isaac   Level: developer
446f402d5e4SToby Isaac 
44720f4b53cSBarry Smith   Note:
44820f4b53cSBarry Smith   This undoes `DMPlexLabelAddCells()` or `DMPlexLabelAddFaceCells()`
449f402d5e4SToby Isaac 
4501cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()`
451f402d5e4SToby Isaac @*/
452d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
453d71ae5a4SJacob Faibussowitsch {
454f402d5e4SToby Isaac   IS              valueIS;
455f402d5e4SToby Isaac   const PetscInt *values;
456485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
457f402d5e4SToby Isaac 
458f402d5e4SToby Isaac   PetscFunctionBegin;
4599566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4609566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4619566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4629566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
463f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
464f402d5e4SToby Isaac     IS              pointIS;
465f402d5e4SToby Isaac     const PetscInt *points;
466f402d5e4SToby Isaac     PetscInt        numPoints, p;
467f402d5e4SToby Isaac 
4689566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4699566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4709566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
471f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
472f402d5e4SToby Isaac       PetscInt point = points[p];
473f402d5e4SToby Isaac 
47448a46eb9SPierre Jolivet       if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v]));
475f402d5e4SToby Isaac     }
4769566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4779566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
478f402d5e4SToby Isaac   }
4799566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4809566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
482f402d5e4SToby Isaac }
483f402d5e4SToby Isaac 
48459eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
48559eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
486d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
487d71ae5a4SJacob Faibussowitsch {
4882582d50cSToby Isaac   PetscInt d, off = 0;
4892582d50cSToby Isaac 
4902582d50cSToby Isaac   PetscFunctionBegin;
49159eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
4920974a383SToby Isaac   for (d = 0; d < depth; d++) {
4932582d50cSToby Isaac     PetscInt firstd     = d;
4940974a383SToby Isaac     PetscInt firstStart = depthShift[2 * d];
4952582d50cSToby Isaac     PetscInt e;
4962582d50cSToby Isaac 
4972582d50cSToby Isaac     for (e = d + 1; e <= depth; e++) {
4982582d50cSToby Isaac       if (depthShift[2 * e] < firstStart) {
4992582d50cSToby Isaac         firstd     = e;
5002582d50cSToby Isaac         firstStart = depthShift[2 * d];
5012582d50cSToby Isaac       }
5022582d50cSToby Isaac     }
5032582d50cSToby Isaac     if (firstd != d) {
5042582d50cSToby Isaac       PetscInt swap[2];
5052582d50cSToby Isaac 
5062582d50cSToby Isaac       e                     = firstd;
5072582d50cSToby Isaac       swap[0]               = depthShift[2 * d];
5082582d50cSToby Isaac       swap[1]               = depthShift[2 * d + 1];
5092582d50cSToby Isaac       depthShift[2 * d]     = depthShift[2 * e];
5102582d50cSToby Isaac       depthShift[2 * d + 1] = depthShift[2 * e + 1];
5112582d50cSToby Isaac       depthShift[2 * e]     = swap[0];
5122582d50cSToby Isaac       depthShift[2 * e + 1] = swap[1];
5132582d50cSToby Isaac     }
5142582d50cSToby Isaac   }
5152582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
5160974a383SToby Isaac   for (d = 0; d <= depth; d++) {
5172582d50cSToby Isaac     off += depthShift[2 * d + 1];
51859eef20bSToby Isaac     depthShift[2 * d + 1] = depthShift[2 * d] + off;
5192582d50cSToby Isaac   }
5203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5212582d50cSToby Isaac }
5222582d50cSToby Isaac 
5232582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
524d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
525d71ae5a4SJacob Faibussowitsch {
5262582d50cSToby Isaac   PetscInt d;
5272582d50cSToby Isaac   PetscInt newOff = 0;
5282582d50cSToby Isaac 
5292582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5302582d50cSToby Isaac     if (p < depthShift[2 * d]) return p + newOff;
5312582d50cSToby Isaac     else newOff = depthShift[2 * d + 1] - depthShift[2 * d];
5322582d50cSToby Isaac   }
5330974a383SToby Isaac   return p + newOff;
5342582d50cSToby Isaac }
5352582d50cSToby Isaac 
5362582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
537d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
538d71ae5a4SJacob Faibussowitsch {
5392582d50cSToby Isaac   PetscInt d;
5402582d50cSToby Isaac   PetscInt newOff = 0;
5412582d50cSToby Isaac 
5422582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5432582d50cSToby Isaac     if (p < depthShift[2 * d + 1]) return p + newOff;
5442582d50cSToby Isaac     else newOff = depthShift[2 * d] - depthShift[2 * d + 1];
5452582d50cSToby Isaac   }
5460974a383SToby Isaac   return p + newOff;
547cd0c2139SMatthew G Knepley }
548cd0c2139SMatthew G Knepley 
549d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
550d71ae5a4SJacob Faibussowitsch {
551cd0c2139SMatthew G Knepley   PetscInt depth = 0, d, pStart, pEnd, p;
552fa8e8ae5SToby Isaac   DMLabel  depthLabel;
553cd0c2139SMatthew G Knepley 
554cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5563ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
557cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
5589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5590974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5609566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
5619566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "depth"));
5629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel));
5639566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
564cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
565cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
566fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
567fa8e8ae5SToby Isaac     IS       pIS;
568fa8e8ae5SToby Isaac 
5699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
570fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
571fa8e8ae5SToby Isaac     pEndNew   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5729566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
5739566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
5749566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
575cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
5762582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
577cd0c2139SMatthew G Knepley       PetscInt       size;
578412e9a14SMatthew G. Knepley       DMPolytopeType ct;
579cd0c2139SMatthew G Knepley 
5809566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
5819566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
5829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
5839566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
5849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
5859566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
586cd0c2139SMatthew G Knepley     }
587cd0c2139SMatthew G Knepley   }
5883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
589cd0c2139SMatthew G Knepley }
590cd0c2139SMatthew G Knepley 
591d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
592d71ae5a4SJacob Faibussowitsch {
5932582d50cSToby Isaac   PetscInt *newpoints;
5942582d50cSToby Isaac   PetscInt  depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
595cd0c2139SMatthew G Knepley 
596cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5983ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
5999566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
6009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
6019566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints));
602cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
6039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
604cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
605cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
6062582d50cSToby Isaac     PetscInt        size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
607cd0c2139SMatthew G Knepley 
6089566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
6099566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
6109566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
611ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
6129566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
6139566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
6149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
6159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
6169566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
617ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
618dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
6199566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
620cd0c2139SMatthew G Knepley   }
6219566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
6223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
623cd0c2139SMatthew G Knepley }
624cd0c2139SMatthew G Knepley 
625d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
626d71ae5a4SJacob Faibussowitsch {
627cd0c2139SMatthew G Knepley   PetscSection coordSection, newCoordSection;
628cd0c2139SMatthew G Knepley   Vec          coordinates, newCoordinates;
629cd0c2139SMatthew G Knepley   PetscScalar *coords, *newCoords;
630f2b8cce1SMatthew G. Knepley   PetscInt     coordSize, sStart, sEnd;
631f2b8cce1SMatthew G. Knepley   PetscInt     dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
632f2b8cce1SMatthew G. Knepley   PetscBool    hasCells;
633cd0c2139SMatthew G Knepley 
634cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6359566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
6369566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
6379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
638cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
6399566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
6409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
6419566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
6429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
6439566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6449566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
6459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
6469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
6479566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
648f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
6499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
650f2b8cce1SMatthew G. Knepley   if (hasCells) {
651f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
652f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
653f2b8cce1SMatthew G. Knepley 
6549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6559566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
6569566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
657f2b8cce1SMatthew G. Knepley     }
658f2b8cce1SMatthew G. Knepley   }
659cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
6609566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
6619566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
662cd0c2139SMatthew G Knepley   }
6639566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
6649566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
6659566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
6669566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
6679566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates"));
6689566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
6699566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
6709566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates, VECSTANDARD));
6719566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
6729566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
6739566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6749566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
675f2b8cce1SMatthew G. Knepley   if (hasCells) {
676f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
677f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
678f2b8cce1SMatthew G. Knepley 
6799566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6809566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
6819566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
682f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
683f2b8cce1SMatthew G. Knepley     }
684f2b8cce1SMatthew G. Knepley   }
685cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
686cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
687cd0c2139SMatthew G Knepley 
6889566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
6899566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
6909566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
691f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
692cd0c2139SMatthew G Knepley   }
6939566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6949566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
6959566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
6969566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
6973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
698cd0c2139SMatthew G Knepley }
699cd0c2139SMatthew G Knepley 
700d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
701d71ae5a4SJacob Faibussowitsch {
702cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
703cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
704cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
705cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
7060e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
707cd0c2139SMatthew G Knepley 
708cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7099566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
7109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
7119566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
7126e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
713cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
7149566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation));
7150e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
7169566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7179566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7189566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
7199566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
720cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
7212582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
722cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
723cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
724cd0c2139SMatthew G Knepley     }
7259566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
7269566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
7270e33faafSMatthew G. Knepley   }
7283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7290e33faafSMatthew G. Knepley }
7300e33faafSMatthew G. Knepley 
731d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
732d71ae5a4SJacob Faibussowitsch {
7330e33faafSMatthew G. Knepley   PetscSF   sfPoint, sfPointNew;
7340e33faafSMatthew G. Knepley   PetscBool useNatural;
7350e33faafSMatthew G. Knepley 
7360e33faafSMatthew G. Knepley   PetscFunctionBegin;
7370e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
7389566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
7399566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
7409566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
7410e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
7429566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
7430e33faafSMatthew G. Knepley   if (useNatural) {
7440e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
7450e33faafSMatthew G. Knepley 
7469566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
7479566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
7489566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
7499566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
750cd0c2139SMatthew G Knepley   }
7513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
752cd0c2139SMatthew G Knepley }
753cd0c2139SMatthew G Knepley 
754d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
755d71ae5a4SJacob Faibussowitsch {
756d56405f8SMatthew G. Knepley   PetscInt depth = 0, numLabels, l;
757cd0c2139SMatthew G Knepley 
758cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7599566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
760cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
7619566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
762cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
763cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
764cd0c2139SMatthew G Knepley     const char     *lname;
765fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
766cd0c2139SMatthew G Knepley     IS              valueIS;
767cd0c2139SMatthew G Knepley     const PetscInt *values;
768cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
769cd0c2139SMatthew G Knepley 
7709566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
7719566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
772cd0c2139SMatthew G Knepley     if (isDepth) continue;
7739566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
774fa8e8ae5SToby Isaac     if (isDim) continue;
7759566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
7769566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
7779566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
7789566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &val));
7799566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, val));
7809566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
7819566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
7829566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
783cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
784cd0c2139SMatthew G Knepley       IS              pointIS;
785cd0c2139SMatthew G Knepley       const PetscInt *points;
786cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
787cd0c2139SMatthew G Knepley 
7889566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
7899566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
7909566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
791cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
7922582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
793cd0c2139SMatthew G Knepley 
7949566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
795cd0c2139SMatthew G Knepley       }
7969566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
7979566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
798cd0c2139SMatthew G Knepley     }
7999566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
8009566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
801cd0c2139SMatthew G Knepley   }
8023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
803d56405f8SMatthew G. Knepley }
804d56405f8SMatthew G. Knepley 
805d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
806d71ae5a4SJacob Faibussowitsch {
807d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
808d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
809d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
810d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
811d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
812d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
813d56405f8SMatthew G. Knepley 
814d56405f8SMatthew G. Knepley   PetscFunctionBegin;
815cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
8169566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8179566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
8189566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
8199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8209566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
8219566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
8229566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
823d56405f8SMatthew G. Knepley   if (createGhostLabel) {
8249566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
8259566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
826d56405f8SMatthew G. Knepley   }
827cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
82848a46eb9SPierre Jolivet     for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
829cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
830cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
8319566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
8321baa6e33SBarry Smith     } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
833cd0c2139SMatthew G Knepley   }
83448a46eb9SPierre Jolivet   for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
835d56405f8SMatthew G. Knepley   if (ghostLabel) {
8369566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
837cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
838cd0c2139SMatthew G Knepley       PetscInt numCells;
839cd0c2139SMatthew G Knepley 
8409566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
841cd0c2139SMatthew G Knepley       if (numCells < 2) {
8429566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
843cd0c2139SMatthew G Knepley       } else {
844cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
845cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
846cd0c2139SMatthew G Knepley 
8479566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
8489566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
8499566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
8509566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
851cd0c2139SMatthew G Knepley       }
852cd0c2139SMatthew G Knepley     }
853d56405f8SMatthew G. Knepley   }
8543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
855cd0c2139SMatthew G Knepley }
856cd0c2139SMatthew G Knepley 
857d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
858d71ae5a4SJacob Faibussowitsch {
859ca04dac2SToby Isaac   DM           refTree;
860ca04dac2SToby Isaac   PetscSection pSec;
861ca04dac2SToby Isaac   PetscInt    *parents, *childIDs;
862ca04dac2SToby Isaac 
863ca04dac2SToby Isaac   PetscFunctionBegin;
8649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
8659566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew, refTree));
8669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL));
867ca04dac2SToby Isaac   if (pSec) {
8682582d50cSToby Isaac     PetscInt     p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
869fb4630b5SToby Isaac     PetscInt    *childIDsShifted;
870ca04dac2SToby Isaac     PetscSection pSecShifted;
871ca04dac2SToby Isaac 
8729566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd));
8739566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
8742582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
8752582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
8769566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted));
8779566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted));
8789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted));
879ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
880fb4630b5SToby Isaac       /* start off assuming no children */
8819566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, p, 0));
882fb4630b5SToby Isaac     }
883fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
884fb4630b5SToby Isaac       PetscInt dof;
885fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
886ca04dac2SToby Isaac 
8879566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
8889566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof));
889ca04dac2SToby Isaac     }
8909566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
891fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
892fb4630b5SToby Isaac       PetscInt dof;
893fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
894fb4630b5SToby Isaac 
8959566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
896fb4630b5SToby Isaac       if (dof) {
897fb4630b5SToby Isaac         PetscInt off, offNew;
898fb4630b5SToby Isaac 
8999566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec, p, &off));
9009566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew));
901fb4630b5SToby Isaac         parentsShifted[offNew]  = DMPlexShiftPoint_Internal(parents[off], depth, depthShift);
902fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
903fb4630b5SToby Isaac       }
904fb4630b5SToby Isaac     }
9059566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted));
9069566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted, childIDsShifted));
9079566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
908ca04dac2SToby Isaac   }
9093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
910ca04dac2SToby Isaac }
911ca04dac2SToby Isaac 
912d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
913d71ae5a4SJacob Faibussowitsch {
914da97024aSMatthew G. Knepley   PetscSF          sf;
915cd0c2139SMatthew G Knepley   IS               valueIS;
916da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
917cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
9182582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
9194fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
920cd0c2139SMatthew G Knepley 
921cd0c2139SMatthew G Knepley   PetscFunctionBegin;
9229566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
9239566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
924da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
9259566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
926cd0c2139SMatthew G Knepley   /* Count ghost cells */
9279566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
9289566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
9299566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
9304a6cfa73SMatthew G. Knepley   Ng = 0;
931cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
93246c796b9SMatthew G. Knepley     IS              faceIS;
93346c796b9SMatthew G. Knepley     const PetscInt *faces;
93446c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
935cd0c2139SMatthew G Knepley 
9369566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9379566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9389566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
93946c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
940ca04dac2SToby Isaac       PetscInt numChildren;
941ca04dac2SToby Isaac 
9429566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
944ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
945ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
94646c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
94746c796b9SMatthew G. Knepley     }
9484a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
9499566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9509566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
951cd0c2139SMatthew G Knepley   }
9529566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
9539566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift));
9542582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
95559eef20bSToby Isaac     PetscInt dEnd;
9562582d50cSToby Isaac 
9579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd));
95859eef20bSToby Isaac     depthShift[2 * d]     = dEnd;
9592582d50cSToby Isaac     depthShift[2 * d + 1] = 0;
9602582d50cSToby Isaac   }
9612582d50cSToby Isaac   if (depth >= 0) depthShift[2 * depth + 1] = Ng;
9629566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
9639566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
964cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
9659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
96648a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1));
967cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
968cd0c2139SMatthew G Knepley     IS              faceIS;
969cd0c2139SMatthew G Knepley     const PetscInt *faces;
970cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
971cd0c2139SMatthew G Knepley 
9729566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9739566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9749566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
975cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
976ca04dac2SToby Isaac       PetscInt size, numChildren;
977cd0c2139SMatthew G Knepley 
9789566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9799566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
980ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
98146c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
98363a3b9bcSJacob 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);
9849566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
985cd0c2139SMatthew G Knepley     }
9869566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9879566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
988cd0c2139SMatthew G Knepley   }
989cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
9909566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
9919566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
992cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
993cd0c2139SMatthew G Knepley   ghostCell = cEnd;
994cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
995cd0c2139SMatthew G Knepley     IS              faceIS;
996cd0c2139SMatthew G Knepley     const PetscInt *faces;
997cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
998cd0c2139SMatthew G Knepley 
9999566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
10009566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
10019566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
100246c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
1003ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
1004cd0c2139SMatthew G Knepley 
10059566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
10069566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
1007ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
100846c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
10099566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
10109566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
101146c796b9SMatthew G. Knepley       ++ghostCell;
1012cd0c2139SMatthew G Knepley     }
10139566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
10149566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
1015cd0c2139SMatthew G Knepley   }
10169566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
10179566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
10189566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
10199566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
10209566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
10219566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
10229566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
10239566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
102448a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
1025966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
10264fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
10274fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L));
10284a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
10293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1030cd0c2139SMatthew G Knepley }
1031cd0c2139SMatthew G Knepley 
1032cd0c2139SMatthew G Knepley /*@C
1033cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
1034cd0c2139SMatthew G Knepley 
103520f4b53cSBarry Smith   Collective
1036cd0c2139SMatthew G Knepley 
1037cd0c2139SMatthew G Knepley   Input Parameters:
103820f4b53cSBarry Smith + dm        - The original `DM`
103920f4b53cSBarry Smith - labelName - The label specifying the boundary faces, or "Face Sets" if this is `NULL`
1040cd0c2139SMatthew G Knepley 
1041cd0c2139SMatthew G Knepley   Output Parameters:
104220f4b53cSBarry Smith + numGhostCells - The number of ghost cells added to the `DM`
104320f4b53cSBarry Smith - dmGhosted     - The new `DM`
1044cd0c2139SMatthew G Knepley 
1045cd0c2139SMatthew G Knepley   Level: developer
1046cd0c2139SMatthew G Knepley 
104720f4b53cSBarry Smith   Note:
104820f4b53cSBarry Smith   If no label exists of that name, one will be created marking all boundary faces
104920f4b53cSBarry Smith 
10501cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`
105131266bc0SMatthew G. Knepley @*/
1052d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
1053d71ae5a4SJacob Faibussowitsch {
1054cd0c2139SMatthew G Knepley   DM          gdm;
1055cd0c2139SMatthew G Knepley   DMLabel     label;
1056cd0c2139SMatthew G Knepley   const char *name = labelName ? labelName : "Face Sets";
1057412e9a14SMatthew G. Knepley   PetscInt    dim, Ng = 0;
1058b0441da4SMatthew G. Knepley   PetscBool   useCone, useClosure;
1059cd0c2139SMatthew G Knepley 
1060cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1061cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10624f572ea9SToby Isaac   if (numGhostCells) PetscAssertPointer(numGhostCells, 3);
10634f572ea9SToby Isaac   PetscAssertPointer(dmGhosted, 4);
10649566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
10659566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
10669566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
10679566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
10689566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
10699566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure));
10709566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
1071cd0c2139SMatthew G Knepley   if (!label) {
1072cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
10739566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
10749566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
10759566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
1076cd0c2139SMatthew G Knepley   }
10779566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
10789566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
10795de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
1080cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
1081d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
1082cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
10833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1084cd0c2139SMatthew G Knepley }
1085cd0c2139SMatthew G Knepley 
1086d71ae5a4SJacob Faibussowitsch static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue)
1087d71ae5a4SJacob Faibussowitsch {
1088accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
1089accc9626SMatthew G. Knepley 
1090accc9626SMatthew G. Knepley   PetscFunctionBegin;
1091accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1092accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
109348a46eb9SPierre Jolivet   for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
1094accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
1095accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
1096accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
1097accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
1098accc9626SMatthew G. Knepley 
1099accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
1100accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
1101accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1102accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
1103accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1104accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1105accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
1106accc9626SMatthew G. Knepley         PetscInt       clval;
1107accc9626SMatthew G. Knepley 
1108accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
1109accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
1110accc9626SMatthew G. Knepley         if (clval == -1) {
1111accc9626SMatthew G. Knepley           const PetscInt *cone;
1112accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
1113accc9626SMatthew G. Knepley 
1114accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
1115accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
1116accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
1117accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
1118accc9626SMatthew G. Knepley             PetscInt cpval;
1119accc9626SMatthew G. Knepley 
1120accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
1121accc9626SMatthew G. Knepley             if (cpval != -1) {
1122accc9626SMatthew G. Knepley               PetscInt dep;
1123accc9626SMatthew G. Knepley 
1124accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
1125accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift + dep) : shift + dep;
1126accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
1127accc9626SMatthew G. Knepley               break;
1128accc9626SMatthew G. Knepley             }
1129accc9626SMatthew G. Knepley           }
1130accc9626SMatthew G. Knepley         }
1131accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
1132accc9626SMatthew G. Knepley         if (d == dim - 1 && clval != -1) {
1133accc9626SMatthew G. Knepley           const PetscInt *support;
1134accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
1135accc9626SMatthew G. Knepley 
1136accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
1137accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
1138accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
1139accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
1140accc9626SMatthew G. Knepley             if (nval == -1) {
1141accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1));
1142accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1143accc9626SMatthew G. Knepley             }
1144accc9626SMatthew G. Knepley           }
1145accc9626SMatthew G. Knepley         }
1146accc9626SMatthew G. Knepley       }
1147accc9626SMatthew G. Knepley     }
1148accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1149accc9626SMatthew G. Knepley   }
1150accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
11513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1152accc9626SMatthew G. Knepley }
1153accc9626SMatthew G. Knepley 
1154accc9626SMatthew G. Knepley typedef struct {
1155accc9626SMatthew G. Knepley   DM               dm;
1156accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1157accc9626SMatthew G. Knepley } PointDivision;
1158accc9626SMatthew G. Knepley 
1159d71ae5a4SJacob Faibussowitsch static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx)
1160d71ae5a4SJacob Faibussowitsch {
1161accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *)ctx;
1162accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val - 1 : val + 1;
1163accc9626SMatthew G. Knepley   const PetscInt *support;
1164accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1165accc9626SMatthew G. Knepley 
1166accc9626SMatthew G. Knepley   PetscFunctionBegin;
1167accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1168accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1169accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1170accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1171accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1172accc9626SMatthew G. Knepley   }
11733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1174accc9626SMatthew G. Knepley }
1175accc9626SMatthew G. Knepley 
1176accc9626SMatthew G. Knepley /* Mark cells by label propagation */
1177d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel)
1178d71ae5a4SJacob Faibussowitsch {
1179accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1180accc9626SMatthew G. Knepley   PointDivision    div;
1181accc9626SMatthew G. Knepley   PetscSF          pointSF;
1182accc9626SMatthew G. Knepley   IS               pointIS;
1183accc9626SMatthew G. Knepley   const PetscInt  *points;
1184accc9626SMatthew G. Knepley   PetscBool        empty;
1185accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1186accc9626SMatthew G. Knepley 
1187accc9626SMatthew G. Knepley   PetscFunctionBegin;
1188accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1189accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1190accc9626SMatthew G. Knepley   div.dm    = dm;
1191accc9626SMatthew G. Knepley   div.queue = queue;
1192accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1193accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS));
1194accc9626SMatthew G. Knepley   if (pointIS) {
1195accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1196accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
119748a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1198accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1199accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1200accc9626SMatthew G. Knepley   }
1201accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS));
1202accc9626SMatthew G. Knepley   if (pointIS) {
1203accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1204accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
120548a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1206accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1207accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1208accc9626SMatthew G. Knepley   }
1209accc9626SMatthew G. Knepley 
1210accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1211accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1212accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1213accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1214accc9626SMatthew G. Knepley   while (!empty) {
1215accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1216accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1217accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1218accc9626SMatthew G. Knepley   }
1219accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1220accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
12213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1222accc9626SMatthew G. Knepley }
1223accc9626SMatthew G. Knepley 
1224607ab7a9SMatthew G. Knepley /*
1225faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1226607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1227faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1228b6dfa339SMatthew 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
1229607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1230a6ae58d1SMatthew G. Knepley 
1231a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1232a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1233607ab7a9SMatthew G. Knepley */
1234d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1235d71ae5a4SJacob Faibussowitsch {
1236cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1237607ab7a9SMatthew G. Knepley   IS               valueIS;
1238607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0; /* The number of depths for which we have replicated points */
1239607ab7a9SMatthew G. Knepley   const PetscInt  *values;    /* List of depths for which we have replicated points */
124018c5995bSMatthew G. Knepley   IS              *splitIS;
124118c5995bSMatthew G. Knepley   IS              *unsplitIS;
1242b6dfa339SMatthew G. Knepley   IS               ghostIS;
1243607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
124418c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
124536dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
124636dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1247b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1248607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
124918c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1250b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1251cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1252cd0c2139SMatthew G Knepley   Vec              coordinates;
1253cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1254a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;   /* The first hybrid point at each depth in the original mesh */
1255a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;   /* The point limit at each depth in the original mesh */
1256607ab7a9SMatthew G. Knepley   PetscInt        *depthShift; /* Number of replicated+hybrid points at each depth */
1257607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;    /* The first replicated point at each depth in the new mesh, hybrids come after this */
1258607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
125918c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1260cd0c2139SMatthew G Knepley 
1261cd0c2139SMatthew G Knepley   PetscFunctionBegin;
12629566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
12639566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
12649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
12659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1266412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
12679566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1268cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
12699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
12709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld));
12719566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints));
1272607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
12739566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
12749566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1275a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1276a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1277607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
127818c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1279607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1280a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1281607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
128218c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
128318c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
128418c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
128559eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
128659eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
128759eef20bSToby Isaac     depthShift[2 * d]     = depthMax[d];
128859eef20bSToby Isaac     depthShift[2 * d + 1] = 0;
1289607ab7a9SMatthew G. Knepley   }
1290b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1291b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1292cd0c2139SMatthew G Knepley   if (label) {
12939566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
12949566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
12959566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1296cd0c2139SMatthew G Knepley   }
1297cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1298cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1299cd0c2139SMatthew G Knepley 
1300cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
13019566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
130218c5995bSMatthew G. Knepley     if (splitIS[dep]) {
13039566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
13049566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
130518c5995bSMatthew G. Knepley     }
13069566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep]));
130718c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
13089566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
13099566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1310cd0c2139SMatthew G Knepley     }
1311cd0c2139SMatthew G Knepley   }
1312b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS));
1313b6dfa339SMatthew G. Knepley   if (ghostIS) {
1314b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1315b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1316b6dfa339SMatthew G. Knepley   }
1317607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
131818c5995bSMatthew 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   */
13192582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d];
13209566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
132159eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
132259eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
132359eef20bSToby Isaac    * added points */
13242582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
13259566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1326cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1327cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1328cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1329cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
13302582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
13314c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1332cd0c2139SMatthew G Knepley       const PetscInt *support;
1333394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
13344c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1335cd0c2139SMatthew G Knepley 
13369566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13379566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
13389566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13399566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
13409566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
13419566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1342cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
13434c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13444c367dbcSMatthew G. Knepley 
1345cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
13469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1347412e9a14SMatthew G. Knepley         switch (coneSize) {
1348d71ae5a4SJacob Faibussowitsch         case 2:
1349d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));
1350d71ae5a4SJacob Faibussowitsch           break;
1351d71ae5a4SJacob Faibussowitsch         case 3:
1352d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));
1353d71ae5a4SJacob Faibussowitsch           break;
1354d71ae5a4SJacob Faibussowitsch         case 4:
1355d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));
1356d71ae5a4SJacob Faibussowitsch           break;
1357412e9a14SMatthew G. Knepley         }
1358b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1359b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1360b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1361b6dfa339SMatthew G. Knepley           const PetscInt *support;
1362b6dfa339SMatthew G. Knepley           PetscInt        val;
1363b6dfa339SMatthew G. Knepley 
1364b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1365b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1366b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1367b6dfa339SMatthew G. Knepley           else PetscCall(DMPlexSetSupportSize(sdm, newp, 2));
1368b6dfa339SMatthew G. Knepley         }
1369cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13704c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1371cd0c2139SMatthew G Knepley 
13729566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13734c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1374cd0c2139SMatthew G Knepley           PetscInt val;
1375cd0c2139SMatthew G Knepley 
13769566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13774c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
13784c367dbcSMatthew G. Knepley           if ((val == 1) || (val == (shift + 1))) ++qn;
13794c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1380cd0c2139SMatthew G Knepley         }
13814c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
13829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13834c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
13849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13854c367dbcSMatthew G. Knepley         /* Add hybrid edge */
13869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
13879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1389cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
13904c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13914c367dbcSMatthew G. Knepley 
13929566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13934c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1394cd0c2139SMatthew G Knepley           PetscInt val;
1395cd0c2139SMatthew G Knepley 
13969566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13974c367dbcSMatthew G. Knepley           if (val == dim - 1) ++qf;
13984c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn;
13994c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp;
1400cd0c2139SMatthew G Knepley         }
14014c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
14029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
14034c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
14049566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
14054c367dbcSMatthew G. Knepley         /* Add hybrid face */
14069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1409cd0c2139SMatthew G Knepley       }
1410cd0c2139SMatthew G Knepley     }
1411cd0c2139SMatthew G Knepley   }
141218c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
141318c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
141418c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
14152582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
141618c5995bSMatthew G. Knepley       const PetscInt *support;
1417da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
141818c5995bSMatthew G. Knepley 
14199566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14209566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14219566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
142218c5995bSMatthew G. Knepley       if (dep == 0) {
142318c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
142418c5995bSMatthew G. Knepley 
142539254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
142639254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
14279566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
142839254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
142939254ff6SMatthew G. Knepley         }
14309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
143118c5995bSMatthew G. Knepley         /* Add hybrid edge */
14329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1433e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1434e1757548SMatthew G. Knepley           PetscInt val;
1435e1757548SMatthew G. Knepley 
14369566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1437e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1438da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1439da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1440e1757548SMatthew G. Knepley         }
14419566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
14429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
144318c5995bSMatthew G. Knepley       } else if (dep == dim - 2) {
144418c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1445cd0c2139SMatthew G Knepley         PetscInt       val;
1446cd0c2139SMatthew G Knepley 
1447da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14489566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1449da1dd7e4SMatthew G. Knepley           if (val == dim - 1) qf += 2;
1450da1dd7e4SMatthew G. Knepley           else ++qf;
1451cd0c2139SMatthew G Knepley         }
145218c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
14539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
145418c5995bSMatthew G. Knepley         /* Add hybrid face */
1455da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14569566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1457da1dd7e4SMatthew G. Knepley           if (val == dim - 1) ++qf;
1458da1dd7e4SMatthew G. Knepley         }
14599566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1462cd0c2139SMatthew G Knepley       }
1463cd0c2139SMatthew G Knepley     }
1464cd0c2139SMatthew G Knepley   }
1465cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
14669566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
14679566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
14689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
14699566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew));
1470cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1471cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1472cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1473cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
14742582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
14754c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1476cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1477b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
14784c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1479cd0c2139SMatthew G Knepley 
14809566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
14819566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
14839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
14849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1486cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
148796a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
14884c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1489cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1490cd0c2139SMatthew G Knepley 
1491b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1492cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
14939566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp, &supportF));
14949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1495cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1496cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1497cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
14989566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
149918c5995bSMatthew G. Knepley           if (v < 0) {
15009566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
150163a3b9bcSJacob 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);
15022582d50cSToby Isaac             coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
150396a07cd0SMatthew G. Knepley             hasUnsplit     = PETSC_TRUE;
150418c5995bSMatthew G. Knepley           } else {
15054c367dbcSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep - 1];
1506163235baSMatthew G. Knepley             if (dep > 1) {
1507163235baSMatthew G. Knepley               const PetscInt *econe;
1508163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1509163235baSMatthew G. Knepley 
15109566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
15119566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1512163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
15139566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs));
15149566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu));
1515163235baSMatthew G. Knepley                 if (vs >= 0) continue;
151663a3b9bcSJacob 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);
1517163235baSMatthew G. Knepley                 hasUnsplit = PETSC_TRUE;
1518163235baSMatthew G. Knepley               }
1519163235baSMatthew G. Knepley             }
1520cd0c2139SMatthew G Knepley           }
1521cd0c2139SMatthew G Knepley         }
15229566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
15239566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1524e537020bSMatthew G. Knepley         /* Face support */
1525b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1526cd0c2139SMatthew G Knepley 
1527b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1528b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1529b6dfa339SMatthew G. Knepley         else vals[1] = -vals[0];
1530b6dfa339SMatthew 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]);
1531b6dfa339SMatthew G. Knepley 
1532b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1533b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1534b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1535b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1536accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1537b6dfa339SMatthew G. Knepley             } else {
1538b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1539accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1540b6dfa339SMatthew G. Knepley             }
1541b6dfa339SMatthew G. Knepley           } else {
1542b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1543cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
15449566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1545cd0c2139SMatthew G Knepley             } else {
1546cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
15479566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1548b6dfa339SMatthew G. Knepley             }
1549b6dfa339SMatthew G. Knepley           }
1550b6dfa339SMatthew G. Knepley         }
1551b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1552e537020bSMatthew G. Knepley         {
1553e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1554b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1555accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1556e537020bSMatthew G. Knepley 
15579371c9d4SSatish Balay           if (supportSize > 1) {
15589371c9d4SSatish Balay             ocell = vals[0] < 0 ? support[1] : support[0];
15599371c9d4SSatish Balay           } else {
15609371c9d4SSatish Balay             ocell = support[0];
15619371c9d4SSatish Balay             flip  = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;
15629371c9d4SSatish Balay           }
1563b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1564b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1565e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1566e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1567e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1568b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc];
1569e537020bSMatthew G. Knepley               break;
1570cd0c2139SMatthew G Knepley             }
1571cd0c2139SMatthew G Knepley           }
1572b6dfa339SMatthew 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);
1573e537020bSMatthew G. Knepley         }
15744c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1575b6dfa339SMatthew G. Knepley         {
1576b6dfa339SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangments(ct) / 2;
1577b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1578b6dfa339SMatthew G. Knepley         }
1579b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1580b5a892a1SMatthew G. Knepley 
1581fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp; /* Extracted negative side orientation above */
15824c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
15834c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1584e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1585412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1586b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q * 2 + 0];
1587b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q * 2 + 1];
1588b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1589412e9a14SMatthew G. Knepley 
15909566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
159118c5995bSMatthew G. Knepley           if (v < 0) {
15929566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
159318c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
159418c5995bSMatthew G. Knepley           } else {
159518c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep];
159618c5995bSMatthew G. Knepley           }
1597b5a892a1SMatthew G. Knepley           coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1598e537020bSMatthew G. Knepley         }
15999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
16009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
160196a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
16029566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1603cd0c2139SMatthew G Knepley       } else if (dep == 0) {
16044c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1605cd0c2139SMatthew G Knepley 
1606cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
16074c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1608cd0c2139SMatthew G Knepley           PetscInt val;
1609cd0c2139SMatthew G Knepley 
16109566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1611ad540459SPierre Jolivet           if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1612cd0c2139SMatthew G Knepley         }
16134c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
16149566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1615cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
16164c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1617cd0c2139SMatthew G Knepley           PetscInt val, edge;
1618cd0c2139SMatthew G Knepley 
16199566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1620cd0c2139SMatthew G Knepley           if (val == 1) {
16219566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
162263a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16234c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep + 1];
1624cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
16252582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1626cd0c2139SMatthew G Knepley           }
1627cd0c2139SMatthew G Knepley         }
16284c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
16299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16304c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1631cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1632cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
16339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
16344c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
16354c367dbcSMatthew G. Knepley           PetscInt val, edge;
16364c367dbcSMatthew G. Knepley 
16379566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16384c367dbcSMatthew G. Knepley           if (val == 1) {
16399566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
164063a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16414c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
16424c367dbcSMatthew G. Knepley           }
16434c367dbcSMatthew G. Knepley         }
16449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1645cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
16464c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16474c367dbcSMatthew G. Knepley 
1648cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1649cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1650cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
16519566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
1652e1757548SMatthew G. Knepley           if (v < 0) {
16539566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
165463a3b9bcSJacob 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);
16552582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1656e1757548SMatthew G. Knepley           } else {
16574c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep - 1];
1658cd0c2139SMatthew G Knepley           }
1659e1757548SMatthew G. Knepley         }
16609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1661cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1662cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1663cd0c2139SMatthew G Knepley           PetscInt val;
1664cd0c2139SMatthew G Knepley 
16659566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16664c367dbcSMatthew G. Knepley           if (val == dim - 1) {
16672582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
16684c367dbcSMatthew G. Knepley           } else if (val == (shift + dim - 1)) {
16692582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1670cd0c2139SMatthew G Knepley           }
1671cd0c2139SMatthew G Knepley         }
1672b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1674cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1675cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1676cd0c2139SMatthew G Knepley           PetscInt val, face;
1677cd0c2139SMatthew G Knepley 
16789566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1679cd0c2139SMatthew G Knepley           if (val == dim - 1) {
16809566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
168163a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
16824c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep + 1];
1683cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim - 1)) {
16842582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1685cd0c2139SMatthew G Knepley           }
1686cd0c2139SMatthew G Knepley         }
1687b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16894c367dbcSMatthew G. Knepley         /* Hybrid face */
16904c367dbcSMatthew G. Knepley         coneNew[0] = newp;
16914c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
16924c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
16934c367dbcSMatthew G. Knepley           PetscInt vertex;
16949566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex));
1695e1757548SMatthew G. Knepley           if (vertex < 0) {
16969566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex));
169763a3b9bcSJacob 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);
1698e1757548SMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
1699e1757548SMatthew G. Knepley           } else {
17004c367dbcSMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
17014c367dbcSMatthew G. Knepley           }
1702e1757548SMatthew G. Knepley         }
17039566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
17044c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
17054c367dbcSMatthew G. Knepley           PetscInt val, face;
17064c367dbcSMatthew G. Knepley 
17079566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
17084c367dbcSMatthew G. Knepley           if (val == dim - 1) {
17099566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
171063a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
17114c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
17124c367dbcSMatthew G. Knepley           }
17134c367dbcSMatthew G. Knepley         }
17149566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1715cd0c2139SMatthew G Knepley       }
1716cd0c2139SMatthew G Knepley     }
1717cd0c2139SMatthew G Knepley   }
171818c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
171918c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
172018c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
17212582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1722b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1723e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
172418c5995bSMatthew G. Knepley 
17259566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
17269566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
17279566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
17289566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
172918c5995bSMatthew G. Knepley       if (dep == 0) {
173018c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
173118c5995bSMatthew G. Knepley 
173218c5995bSMatthew G. Knepley         /* Unsplit vertex */
17339566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
173418c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
17352582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
17369566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
1737ad540459SPierre Jolivet           if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1];
173818c5995bSMatthew G. Knepley         }
173918c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174018c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174163a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
17429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
174318c5995bSMatthew G. Knepley         /* Hybrid edge */
174418c5995bSMatthew G. Knepley         coneNew[0] = newp;
174518c5995bSMatthew G. Knepley         coneNew[1] = newp;
17469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
174718c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
174818c5995bSMatthew G. Knepley           PetscInt val, edge;
174918c5995bSMatthew G. Knepley 
17509566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
175118c5995bSMatthew G. Knepley           if (val == 1) {
17529566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
175363a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
175418c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1755e1757548SMatthew G. Knepley           } else if (val == (shift2 + 1)) {
17569566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge));
175763a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1758e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1];
175918c5995bSMatthew G. Knepley           }
176018c5995bSMatthew G. Knepley         }
17619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1762e1757548SMatthew G. Knepley       } else if (dep == dim - 2) {
1763e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1764e1757548SMatthew G. Knepley 
1765da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1766e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1767e1757548SMatthew G. Knepley           PetscInt val, face;
1768e1757548SMatthew G. Knepley 
17699566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1770e1757548SMatthew G. Knepley           if (val == dim - 1) {
17719566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
177263a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
17732582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1774e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 1];
1775e1757548SMatthew G. Knepley           } else {
17762582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1777e1757548SMatthew G. Knepley           }
1778e1757548SMatthew G. Knepley         }
1779e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1780e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
17819566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
178263a3b9bcSJacob 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);
17839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1784e1757548SMatthew G. Knepley         /* Add hybrid face */
1785e1757548SMatthew G. Knepley         coneNew[0] = newp;
1786212cc919SMatthew G. Knepley         coneNew[1] = newp;
17879566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
178863a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1789212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17909566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], 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[1]);
1792e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1794da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1795da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1796da1dd7e4SMatthew G. Knepley 
17979566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1798da1dd7e4SMatthew G. Knepley           if (val == dim - 1) {
17999566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
1800da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1801da1dd7e4SMatthew G. Knepley           }
1802da1dd7e4SMatthew G. Knepley         }
18039566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
180463a3b9bcSJacob 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);
18059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1806cd0c2139SMatthew G Knepley       }
1807cd0c2139SMatthew G Knepley     }
1808cd0c2139SMatthew G Knepley   }
1809cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1810cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1811cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1812cd0c2139SMatthew G Knepley     IS              pIS;
1813cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1814cd0c2139SMatthew G Knepley     const PetscInt *points;
1815cd0c2139SMatthew G Knepley 
1816cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
18179566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1818cd0c2139SMatthew G Knepley     if (!pIS) continue;
1819cd0c2139SMatthew G Knepley     dep = -dep - shift;
18209566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
18219566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1822cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1823cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
18242582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1825cd0c2139SMatthew G Knepley       const PetscInt *cone;
1826cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
182750cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1828cd0c2139SMatthew G Knepley 
1829cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1830cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
18319566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
18329566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1833cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1834e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift);
1835cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1836cd0c2139SMatthew G Knepley 
18379566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1838cd0c2139SMatthew G Knepley         if (val == dep - 1) {
18399566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp));
184063a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1);
1841cd0c2139SMatthew G Knepley           csplitp = pMaxNew[dep - 1] + cp;
18429566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
184350cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1844cd0c2139SMatthew G Knepley         }
1845cd0c2139SMatthew G Knepley       }
18464a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
184728b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1848cd0c2139SMatthew G Knepley     }
18499566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
18509566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1851cd0c2139SMatthew G Knepley   }
18520e18dc48SMatthew G. Knepley   PetscCall(DMPlexReorderCohesiveSupports(sdm));
1853fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
18549566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
18559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
18569566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
18579566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1858cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
18592582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1860cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1861cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1862cd0c2139SMatthew G Knepley 
18639566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
18649566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
18659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1866cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d];
1867cd0c2139SMatthew G Knepley   }
18689566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1869fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
18709566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1871b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1872fa8e8ae5SToby Isaac   /* Step 9: Labels */
18739566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
18749566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
18759566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1876cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1877cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
18782582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1879cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1880cd0c2139SMatthew G Knepley       PetscInt       l;
1881cd0c2139SMatthew G Knepley 
18827db7e0a7SMatthew G. Knepley       if (splitLabel) {
18837db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
18847db7e0a7SMatthew G. Knepley 
18859566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp, val));
18869566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
18877db7e0a7SMatthew G. Knepley       }
1888cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1889cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1890cd0c2139SMatthew G Knepley         const char *lname;
1891cd0c2139SMatthew G Knepley         PetscInt    val;
18929a356370SMatthew G. Knepley         PetscBool   isDepth;
1893cd0c2139SMatthew G Knepley 
18949566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
18959566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
18969a356370SMatthew G. Knepley         if (isDepth) continue;
18979566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
18989566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
189948a46eb9SPierre Jolivet         if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val));
1900cd0c2139SMatthew G Knepley       }
1901cd0c2139SMatthew G Knepley     }
1902cd0c2139SMatthew G Knepley   }
1903cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1904cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1905cd0c2139SMatthew G Knepley 
1906cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
19079566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
19089566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
19099566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
19109566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1911cd0c2139SMatthew G Knepley   }
1912b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1913b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1914cd0c2139SMatthew G Knepley   if (label) {
19159566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
19169566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1917cd0c2139SMatthew G Knepley   }
19180d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
19199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
192036dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
19210d4d4d06SMatthew G. Knepley   }
19229566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
19239566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
19249566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
19253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1926cd0c2139SMatthew G Knepley }
1927cd0c2139SMatthew G Knepley 
1928cd0c2139SMatthew G Knepley /*@C
1929cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1930cd0c2139SMatthew G Knepley 
193120f4b53cSBarry Smith   Collective
1932cd0c2139SMatthew G Knepley 
1933cd0c2139SMatthew G Knepley   Input Parameters:
193420f4b53cSBarry Smith + dm    - The original `DM`
193520f4b53cSBarry Smith - label - The `DMLabel` specifying the boundary faces (this could be auto-generated)
1936cd0c2139SMatthew G Knepley 
1937cd0c2139SMatthew G Knepley   Output Parameters:
193820f4b53cSBarry Smith + splitLabel - The `DMLabel` containing the split points, or `NULL` if no output is desired
193920f4b53cSBarry Smith - dmSplit    - The new `DM`
1940cd0c2139SMatthew G Knepley 
1941cd0c2139SMatthew G Knepley   Level: developer
1942cd0c2139SMatthew G Knepley 
19431cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1944cd0c2139SMatthew G Knepley @*/
1945d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1946d71ae5a4SJacob Faibussowitsch {
1947cd0c2139SMatthew G Knepley   DM       sdm;
1948cd0c2139SMatthew G Knepley   PetscInt dim;
1949cd0c2139SMatthew G Knepley 
1950cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1951cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19524f572ea9SToby Isaac   PetscAssertPointer(dmSplit, 4);
19539566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
19549566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
19559566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19569566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1957cd0c2139SMatthew G Knepley   switch (dim) {
1958cd0c2139SMatthew G Knepley   case 2:
1959d71ae5a4SJacob Faibussowitsch   case 3:
1960d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1961d71ae5a4SJacob Faibussowitsch     break;
1962d71ae5a4SJacob Faibussowitsch   default:
1963d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1964cd0c2139SMatthew G Knepley   }
1965cd0c2139SMatthew G Knepley   *dmSplit = sdm;
19663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1967cd0c2139SMatthew G Knepley }
1968cd0c2139SMatthew G Knepley 
19690f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
1970d71ae5a4SJacob Faibussowitsch static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1971d71ae5a4SJacob Faibussowitsch {
19720f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
19730f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
19740f66a230SMatthew G. Knepley 
19750f66a230SMatthew G. Knepley   PetscFunctionBegin;
19760f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
19779566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19789566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19799566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
19809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
19810f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
19820f66a230SMatthew G. Knepley     if (cone[c] == face) {
19830f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
19840f66a230SMatthew G. Knepley 
19850f66a230SMatthew G. Knepley       if (subdm) {
19860f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
19870f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
19880f66a230SMatthew G. Knepley 
19899566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
19909566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
19919566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
19929566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
19939566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
19940f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
19950f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
19960f66a230SMatthew G. Knepley             o = subornt[0];
19970f66a230SMatthew G. Knepley             break;
19980f66a230SMatthew G. Knepley           }
19990f66a230SMatthew G. Knepley         }
200063a3b9bcSJacob 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);
20010f66a230SMatthew G. Knepley       }
20020f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
20030f66a230SMatthew G. Knepley       else *pos = PETSC_FALSE;
20040f66a230SMatthew G. Knepley       break;
20050f66a230SMatthew G. Knepley     }
20060f66a230SMatthew G. Knepley   }
200763a3b9bcSJacob 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);
20083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20090f66a230SMatthew G. Knepley }
20100f66a230SMatthew G. Knepley 
2011d71ae5a4SJacob Faibussowitsch static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label)
2012d71ae5a4SJacob Faibussowitsch {
2013accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
2014accc9626SMatthew G. Knepley   const PetscInt *points;
2015accc9626SMatthew G. Knepley   PetscInt        dim, numPoints, p, shift = 100, shift2 = 200;
2016accc9626SMatthew G. Knepley 
2017accc9626SMatthew G. Knepley   PetscFunctionBegin;
2018accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
2019accc9626SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
2020accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
2021accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
2022accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
2023accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
2024accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
20253ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2026accc9626SMatthew G. Knepley   }
2027accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
2028accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
2029accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
2030accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
2031accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
2032accc9626SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
2033accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
2034accc9626SMatthew G. Knepley     const PetscInt *support;
2035accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
2036accc9626SMatthew G. Knepley 
2037accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
2038accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
2039accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
2040accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
2041accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
2042accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
2043accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
2044e1a13daeSMatthew G. Knepley     /* Check that this face is not incident on only unsplit faces, meaning has at least one split face */
2045e1a13daeSMatthew G. Knepley     {
2046e1a13daeSMatthew G. Knepley       PetscInt *closure = NULL;
2047e1a13daeSMatthew G. Knepley       PetscBool split   = PETSC_FALSE;
2048e1a13daeSMatthew G. Knepley       PetscInt  closureSize, cl;
2049e1a13daeSMatthew G. Knepley 
2050e1a13daeSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2051e1a13daeSMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2052e1a13daeSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
20539371c9d4SSatish Balay         if ((valA >= 0) && (valA <= dim)) {
20549371c9d4SSatish Balay           split = PETSC_TRUE;
20559371c9d4SSatish Balay           break;
20569371c9d4SSatish Balay         }
2057e1a13daeSMatthew G. Knepley       }
2058e1a13daeSMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2059e1a13daeSMatthew G. Knepley       if (!split) continue;
2060e1a13daeSMatthew G. Knepley     }
2061accc9626SMatthew G. Knepley     /* Split the face */
2062accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, point, &valA));
2063accc9626SMatthew G. Knepley     PetscCall(DMLabelClearValue(label, point, valA));
2064accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, dim - 1));
2065accc9626SMatthew G. Knepley     /* Label its closure:
2066accc9626SMatthew G. Knepley       unmarked: label as unsplit
2067accc9626SMatthew G. Knepley       incident: relabel as split
2068accc9626SMatthew G. Knepley       split:    do nothing
2069accc9626SMatthew G. Knepley     */
2070accc9626SMatthew G. Knepley     {
2071accc9626SMatthew G. Knepley       PetscInt *closure = NULL;
2072accc9626SMatthew G. Knepley       PetscInt  closureSize, cl, dep;
2073accc9626SMatthew G. Knepley 
2074accc9626SMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2075accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2076accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
2077accc9626SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
2078accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2079accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
2080accc9626SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2081accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2082accc9626SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], valA));
2083accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], dep));
2084accc9626SMatthew G. Knepley         }
2085accc9626SMatthew G. Knepley       }
2086accc9626SMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2087accc9626SMatthew G. Knepley     }
2088accc9626SMatthew G. Knepley   }
2089accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2090accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
20913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2092accc9626SMatthew G. Knepley }
2093accc9626SMatthew G. Knepley 
2094cd0c2139SMatthew G Knepley /*@
20950f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
2096cd0c2139SMatthew G Knepley   to complete the surface
2097cd0c2139SMatthew G Knepley 
2098cd0c2139SMatthew G Knepley   Input Parameters:
209920f4b53cSBarry Smith + dm     - The `DM`
210020f4b53cSBarry Smith . label  - A `DMLabel` marking the surface
210120f4b53cSBarry Smith . blabel - A `DMLabel` marking the vertices on the boundary which will not be duplicated, or `NULL` to find them automatically
210220f4b53cSBarry Smith . bvalue - Value of `DMLabel` marking the vertices on the boundary
2103bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
210420f4b53cSBarry Smith - subdm  - The `DM` associated with the label, or `NULL`
2105cd0c2139SMatthew G Knepley 
2106cd0c2139SMatthew G Knepley   Output Parameter:
210720f4b53cSBarry Smith . label - A `DMLabel` marking all surface points
21080f66a230SMatthew G. Knepley 
2109cd0c2139SMatthew G Knepley   Level: developer
2110cd0c2139SMatthew G Knepley 
211120f4b53cSBarry Smith   Note:
211220f4b53cSBarry Smith   The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
211320f4b53cSBarry Smith 
21141cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2115cd0c2139SMatthew G Knepley @*/
2116d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, DM subdm)
2117d71ae5a4SJacob Faibussowitsch {
2118d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2119accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
212047946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2121bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
2122accc9626SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = 300, dim, depth, numPoints, numSubpoints, p, val;
2123cd0c2139SMatthew G Knepley 
2124cd0c2139SMatthew G Knepley   PetscFunctionBegin;
21259566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
21269566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
21279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
212847946fd8SMatthew G. Knepley   if (subdm) {
21299566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
213047946fd8SMatthew G. Knepley     if (subpointIS) {
21319566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
21329566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
213347946fd8SMatthew G. Knepley     }
213447946fd8SMatthew G. Knepley   }
2135d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
21369566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2137accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
21389566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
21399566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2140d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2141cd0c2139SMatthew G Knepley     const PetscInt *support;
2142cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2143cd0c2139SMatthew G Knepley 
21449566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2145c4419245SMatthew G. Knepley #if 0
2146c4419245SMatthew G. Knepley     if (supportSize != 2) {
2147c4419245SMatthew G. Knepley       const PetscInt *lp;
2148c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2149c4419245SMatthew G. Knepley 
2150c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2151c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
21529566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
21539566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
215463a3b9bcSJacob 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);
2155c4419245SMatthew G. Knepley     }
2156c4419245SMatthew G. Knepley #endif
21579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2158cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
21590f66a230SMatthew G. Knepley       const PetscInt *cone;
2160cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
21610f66a230SMatthew G. Knepley       PetscBool       pos;
2162cd0c2139SMatthew G Knepley 
21639566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
21649566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
21659566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
21660f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
21670f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
21689566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
21699566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2170cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2171cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2172cd0c2139SMatthew G Knepley 
21739566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2174cd0c2139SMatthew G Knepley         if (val == -1) {
2175cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2176cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2177cd0c2139SMatthew G Knepley 
21789566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2179cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2180cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2181a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2182cd0c2139SMatthew G Knepley 
21839566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
21849566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2185a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim - 1) && (bval < 0)) {
21869566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2187cd0c2139SMatthew G Knepley               break;
2188cd0c2139SMatthew G Knepley             }
2189cd0c2139SMatthew G Knepley           }
21909566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2191cd0c2139SMatthew G Knepley         }
2192cd0c2139SMatthew G Knepley       }
2193cd0c2139SMatthew G Knepley     }
2194cd0c2139SMatthew G Knepley   }
2195accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2196accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2197a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
219886200784SMatthew G. Knepley   if (blabel) {
2199accc9626SMatthew G. Knepley     IS bdIS;
2200accc9626SMatthew G. Knepley 
2201caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2202accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2203accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2204a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2205a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2206a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2207a0541d8aSMatthew G. Knepley 
22089566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2209a0541d8aSMatthew G. Knepley       if (bval >= 0) {
22109566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2211f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2212f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
22139566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2214f7019248SMatthew G. Knepley         }
2215f7019248SMatthew G. Knepley       }
2216f7019248SMatthew G. Knepley     }
2217f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2218f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2219f7019248SMatthew G. Knepley       PetscInt       val, bval;
2220f7019248SMatthew G. Knepley 
22219566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2222f7019248SMatthew G. Knepley       if (bval >= 0) {
222386200784SMatthew G. Knepley         const PetscInt *cone, *support;
222486200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
222586200784SMatthew G. Knepley 
2226a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
22279566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2228e1a13daeSMatthew 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);
22299566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
22309566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
22312c06a818SMatthew G. Knepley         /* Check for cross-edge
22322c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
223386200784SMatthew G. Knepley         if (val != 0) continue;
22349566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
22359566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
223686200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
22379566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
22389566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
223963a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
22409566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
22419566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
22429566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
22439566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
224486200784SMatthew G. Knepley         }
2245a0541d8aSMatthew G. Knepley       }
2246a0541d8aSMatthew G. Knepley     }
2247accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2248accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2249a0541d8aSMatthew G. Knepley   }
2250b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2251b6dfa339SMatthew G. Knepley   {
2252b6dfa339SMatthew G. Knepley     PetscSF         sf;
2253b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2254b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2255b6dfa339SMatthew G. Knepley 
2256b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2257b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2258accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2259accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2260accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2261accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2262b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2263b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2264b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2265b6dfa339SMatthew G. Knepley         PetscInt       val;
2266b6dfa339SMatthew G. Knepley 
2267b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2268b6dfa339SMatthew G. Knepley         if (l >= 0) {
2269b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2270b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2271b6dfa339SMatthew G. Knepley 
2272b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2273b6dfa339SMatthew 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);
2274b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2275b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2276b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2277accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2278b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2279b6dfa339SMatthew G. Knepley 
2280b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2281b6dfa339SMatthew 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);
2282b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2283b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2284b6dfa339SMatthew G. Knepley           }
2285b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2286b6dfa339SMatthew G. Knepley         }
2287b6dfa339SMatthew G. Knepley       }
2288b6dfa339SMatthew G. Knepley     }
2289b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2290b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2291accc9626SMatthew G. Knepley   }
2292accc9626SMatthew G. Knepley divide:
2293b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2294accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
2295accc9626SMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label));
22963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2297cd0c2139SMatthew G Knepley }
2298cd0c2139SMatthew G Knepley 
2299720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
230066976f2fSJacob Faibussowitsch static PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2301d71ae5a4SJacob Faibussowitsch {
2302720e594eSMatthew G. Knepley   IS              subpointIS;
2303720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2304720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2305720e594eSMatthew G. Knepley 
2306720e594eSMatthew G. Knepley   PetscFunctionBegin;
23073ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
23089566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
23099566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
23103ba16761SJacob Faibussowitsch   if (!subpointIS) PetscFunctionReturn(PETSC_SUCCESS);
23119566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
23129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
23139566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2314720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2315720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2316720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2317720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2318720e594eSMatthew G. Knepley 
23199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2320720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2321720e594eSMatthew G. Knepley       PetscInt value = 0;
2322720e594eSMatthew G. Knepley 
2323720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
23249566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
23259371c9d4SSatish Balay       if (value == defaultValue) {
23269371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
23279371c9d4SSatish Balay         break;
23289371c9d4SSatish Balay       }
2329720e594eSMatthew G. Knepley     }
23309566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2331720e594eSMatthew G. Knepley     if (invalidCell) {
23329566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23339566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
23349566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
233563a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2336720e594eSMatthew G. Knepley     }
2337720e594eSMatthew G. Knepley   }
23389566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2340720e594eSMatthew G. Knepley }
2341720e594eSMatthew G. Knepley 
2342c08575a3SMatthew G. Knepley /*@
23433cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
23443cf72582SMatthew G. Knepley 
234520f4b53cSBarry Smith   Collective
23463cf72582SMatthew G. Knepley 
23473cf72582SMatthew G. Knepley   Input Parameters:
234820f4b53cSBarry Smith + dm      - The original `DM`
2349720e594eSMatthew G. Knepley . label   - The label specifying the interface vertices
2350caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2351caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
23523cf72582SMatthew G. Knepley 
23533cf72582SMatthew G. Knepley   Output Parameters:
235420f4b53cSBarry Smith + hybridLabel - The label fully marking the interface, or `NULL` if no output is desired
235520f4b53cSBarry Smith . splitLabel  - The label containing the split points, or `NULL` if no output is desired
235620f4b53cSBarry Smith . dmInterface - The new interface `DM`, or `NULL`
235720f4b53cSBarry Smith - dmHybrid    - The new `DM` with cohesive cells
23583cf72582SMatthew G. Knepley 
235920f4b53cSBarry Smith   Level: developer
236020f4b53cSBarry Smith 
236120f4b53cSBarry Smith   Note:
236220f4b53cSBarry Smith   The hybridLabel indicates what parts of the original mesh impinged on the division surface. For points
23636eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
23646eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
23656eccb800SMatthew 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
23666eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
23676eccb800SMatthew Knepley 
23686eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
236940cdb708SMatthew 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
23706eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
23716eccb800SMatthew Knepley 
237220f4b53cSBarry Smith   The dmInterface is a `DM` built from the original division surface. It has a label which can be retrieved using
237320f4b53cSBarry Smith   `DMPlexGetSubpointMap()` which maps each point back to the point in the surface of the original mesh.
23746eccb800SMatthew Knepley 
23751cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
23763cf72582SMatthew G. Knepley @*/
2377d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
2378d71ae5a4SJacob Faibussowitsch {
23793cf72582SMatthew G. Knepley   DM       idm;
23807db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
23813cf72582SMatthew G. Knepley   PetscInt dim;
23823cf72582SMatthew G. Knepley 
23833cf72582SMatthew G. Knepley   PetscFunctionBegin;
23843cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23854f572ea9SToby Isaac   if (label) PetscAssertPointer(label, 2);
23864f572ea9SToby Isaac   if (bdlabel) PetscAssertPointer(bdlabel, 3);
23874f572ea9SToby Isaac   if (hybridLabel) PetscAssertPointer(hybridLabel, 5);
23884f572ea9SToby Isaac   if (splitLabel) PetscAssertPointer(splitLabel, 6);
23894f572ea9SToby Isaac   if (dmInterface) PetscAssertPointer(dmInterface, 7);
23904f572ea9SToby Isaac   PetscAssertPointer(dmHybrid, 8);
23919566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
23929566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
23939566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
23949566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
23959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
23969566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
23979566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
23987db7e0a7SMatthew G. Knepley   if (splitLabel) {
23997db7e0a7SMatthew G. Knepley     const char *name;
24007db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
24017db7e0a7SMatthew G. Knepley 
24029566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
2403c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(sname, name, sizeof(sname)));
2404c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(sname, " split", sizeof(sname)));
24059566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
24067db7e0a7SMatthew G. Knepley   }
2407caf9e14dSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, idm));
24089371c9d4SSatish Balay   if (dmInterface) {
24099371c9d4SSatish Balay     *dmInterface = idm;
24109371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
24119566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
24125de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
24133cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
24149566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
24157db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
24164a7ee7d0SMatthew G. Knepley   {
24174a7ee7d0SMatthew G. Knepley     DM      cdm;
24184a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
24194a7ee7d0SMatthew G. Knepley 
24204a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
24219566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
24229566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
24239566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
24244a7ee7d0SMatthew G. Knepley   }
24253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2426cd0c2139SMatthew G Knepley }
2427cd0c2139SMatthew G Knepley 
2428efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2429efa14ee0SMatthew G Knepley 
2430efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2431efa14ee0SMatthew G Knepley */
2432d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2433d71ae5a4SJacob Faibussowitsch {
2434fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2435efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2436412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2437efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2438efa14ee0SMatthew G Knepley 
2439efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2440efa14ee0SMatthew G Knepley   *numFaces = 0;
2441efa14ee0SMatthew G Knepley   *nFV      = 0;
24429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24439566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
244477d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
24459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
244648a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2447efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
24489566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2449efa14ee0SMatthew G Knepley   if (subvertexIS) {
24509566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24519566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2452efa14ee0SMatthew G Knepley   }
2453efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2454efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24550298fd71SBarry Smith     PetscInt      *star   = NULL;
2456efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2457efa14ee0SMatthew G Knepley 
24589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2459efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2460efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2461efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2462efa14ee0SMatthew G Knepley     }
2463efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2464efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
24650298fd71SBarry Smith       PetscInt      *closure = NULL;
2466efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2467efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2468efa14ee0SMatthew G Knepley 
24699566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
247065560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
247163a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
24729566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2473efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2474efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2475efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2476efa14ee0SMatthew G Knepley 
2477efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2478efa14ee0SMatthew G Knepley           ++numCorners;
24799566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2480830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2481efa14ee0SMatthew G Knepley         }
2482efa14ee0SMatthew G Knepley       }
24839566063dSJacob Faibussowitsch       if (!(*nFV)) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
248463a3b9bcSJacob 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);
2485efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2486007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2487007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2488007baee2SMatthew G. Knepley 
2489efa14ee0SMatthew G Knepley         ++(*numFaces);
249048a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
24919566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
249248a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
24939566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2494efa14ee0SMatthew G Knepley       }
24959566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2496efa14ee0SMatthew G Knepley     }
24979566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2498efa14ee0SMatthew G Knepley   }
249948a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25009566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25019566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2503efa14ee0SMatthew G Knepley }
2504efa14ee0SMatthew G Knepley 
2505d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2506d71ae5a4SJacob Faibussowitsch {
250734b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2508efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2509412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2510efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2511efa14ee0SMatthew G Knepley 
2512efa14ee0SMatthew G Knepley   PetscFunctionBegin;
25139566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25149566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
251548a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2516efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
251734b4c39eSMatthew G. Knepley   if (vertexLabel) {
25189566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2519efa14ee0SMatthew G Knepley     if (subvertexIS) {
25209566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
25219566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2522efa14ee0SMatthew G Knepley     }
252334b4c39eSMatthew G. Knepley   }
2524efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2525efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
25260298fd71SBarry Smith     PetscInt      *star   = NULL;
2527efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2528efa14ee0SMatthew G Knepley 
25299566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2530efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2531efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2532158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2533158acfadSMatthew G. Knepley 
2534158acfadSMatthew G. Knepley       if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) {
2535158acfadSMatthew G. Knepley         if (markedFaces) {
25369566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2537158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2538158acfadSMatthew G. Knepley         }
2539158acfadSMatthew G. Knepley         star[numFaces++] = point;
2540158acfadSMatthew G. Knepley       }
2541efa14ee0SMatthew G Knepley     }
2542efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2543efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
25440298fd71SBarry Smith       PetscInt      *closure = NULL;
2545efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2546efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2547efa14ee0SMatthew G Knepley 
25489566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2549efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
255063a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
25519566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2552efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2553efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2554efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2555efa14ee0SMatthew G Knepley 
2556efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
25579566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2558830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2559efa14ee0SMatthew G Knepley         }
2560efa14ee0SMatthew G Knepley       }
2561efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2562efa14ee0SMatthew G Knepley         const PetscInt *support;
2563efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2564efa14ee0SMatthew G Knepley 
2565efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2566efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2567efa14ee0SMatthew G Knepley 
2568efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2569efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
25709566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2571efa14ee0SMatthew G Knepley               break;
2572efa14ee0SMatthew G Knepley             }
2573efa14ee0SMatthew G Knepley           }
2574efa14ee0SMatthew G Knepley         }
25759566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
25769566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
257748a46eb9SPierre Jolivet         for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2578efa14ee0SMatthew G Knepley       }
25799566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2580efa14ee0SMatthew G Knepley     }
25819566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2582efa14ee0SMatthew G Knepley   }
25839566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25849566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25859566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2587efa14ee0SMatthew G Knepley }
2588efa14ee0SMatthew G Knepley 
2589d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2590d71ae5a4SJacob Faibussowitsch {
259127c04023SMatthew G. Knepley   DMLabel         label = NULL;
2592766ab985SMatthew G. Knepley   const PetscInt *cone;
25939fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2594766ab985SMatthew G. Knepley 
2595812bfc34SJed Brown   PetscFunctionBegin;
2596c0ed958bSJed Brown   *numFaces = 0;
2597c0ed958bSJed Brown   *nFV      = 0;
25989566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2599fed694aaSMatthew G. Knepley   *subCells = NULL;
26009566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26023ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
260327c04023SMatthew G. Knepley   if (label) {
260427c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
260527c04023SMatthew G. Knepley       PetscInt val;
260627c04023SMatthew G. Knepley 
26079566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
260827c04023SMatthew G. Knepley       if (val == value) {
260927c04023SMatthew G. Knepley         ++(*numFaces);
26109566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
261127c04023SMatthew G. Knepley       }
261227c04023SMatthew G. Knepley     }
261327c04023SMatthew G. Knepley   } else {
2614766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
26159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
261627c04023SMatthew G. Knepley   }
26179566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
26183ba16761SJacob Faibussowitsch   if (!(*numFaces)) PetscFunctionReturn(PETSC_SUCCESS);
26199fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2620766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2621766ab985SMatthew G. Knepley     const PetscInt *cells;
2622766ab985SMatthew G. Knepley     PetscInt        numCells;
2623766ab985SMatthew G. Knepley 
262427c04023SMatthew G. Knepley     if (label) {
262527c04023SMatthew G. Knepley       PetscInt val;
262627c04023SMatthew G. Knepley 
26279566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
262827c04023SMatthew G. Knepley       if (val != value) continue;
262927c04023SMatthew G. Knepley     }
26309566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
263148a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2632766ab985SMatthew G. Knepley     /* Negative face */
26339566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
263427234c99SMatthew G. Knepley     /* Not true in parallel
263508401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2636766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
26379566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
263827234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2639766ab985SMatthew G. Knepley     }
26409566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2641766ab985SMatthew G. Knepley     /* Positive face is not included */
2642766ab985SMatthew G. Knepley   }
26433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2644766ab985SMatthew G. Knepley }
2645766ab985SMatthew G. Knepley 
2646d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2647d71ae5a4SJacob Faibussowitsch {
2648766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2649766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2650766ab985SMatthew G. Knepley 
2651812bfc34SJed Brown   PetscFunctionBegin;
26529566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26543ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
26559566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
26569566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2657766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2658766ab985SMatthew G. Knepley     const PetscInt *cone;
2659766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2660b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2661766ab985SMatthew G. Knepley 
266227c04023SMatthew G. Knepley     if (label) {
26639566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
266427c04023SMatthew G. Knepley       if (val != value) continue;
266527c04023SMatthew G. Knepley     }
26669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
26679566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
26689566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
26691dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2670b3154360SMatthew G. Knepley     /* Negative face */
26719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2672766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2673766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2674766ab985SMatthew G. Knepley 
2675766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2676766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
26779566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2678766ab985SMatthew G. Knepley           break;
2679766ab985SMatthew G. Knepley         }
2680766ab985SMatthew G. Knepley       }
2681766ab985SMatthew G. Knepley     }
26829566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2683766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2684766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2685766ab985SMatthew G. Knepley       const PetscInt *support;
2686766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2687766ab985SMatthew G. Knepley 
26889566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
268908401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
26909566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
269148a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2692766ab985SMatthew G. Knepley     }
2693766ab985SMatthew G. Knepley   }
26949566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
26953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2696766ab985SMatthew G. Knepley }
2697766ab985SMatthew G. Knepley 
2698d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2699d71ae5a4SJacob Faibussowitsch {
270082f516ccSBarry Smith   MPI_Comm       comm;
2701e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2702e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2703e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2704e6ccafaeSMatthew G Knepley 
270582f516ccSBarry Smith   PetscFunctionBegin;
27069566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
27079566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
270863a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2709e6ccafaeSMatthew G Knepley 
2710ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2711ddeab2a6SMatthew G. Knepley     /* Triangle */
2712e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2713e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2714ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2715ddeab2a6SMatthew G. Knepley     /* Triangle */
2716ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2717ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2718ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2719ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2720ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2721ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2722e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2723e6ccafaeSMatthew G Knepley     /* Quadratic line */
2724e6ccafaeSMatthew G Knepley     faceSize  = 1;
2725e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2726e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2727e6ccafaeSMatthew G Knepley     /* Quads */
2728e6ccafaeSMatthew G Knepley     faceSize = 2;
2729e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2730e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2731e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2732e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2733e6ccafaeSMatthew G Knepley     } else {
2734e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2735e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2736e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2737e6ccafaeSMatthew G Knepley     }
2738e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2739e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2740e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2741e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2742e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2743e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2744e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2745e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2746e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2747e6ccafaeSMatthew G Knepley       2, 3, 5, /* left */
2748e6ccafaeSMatthew G Knepley     };
2749e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2750e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2751e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2752e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2753e6ccafaeSMatthew G Knepley     };
2754e6ccafaeSMatthew G Knepley 
2755e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
27569566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2757e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2758e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2759e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2760e6ccafaeSMatthew G Knepley 
27619371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) {
2762e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2763e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2764e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2765e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2766e6ccafaeSMatthew G Knepley               break;
2767e6ccafaeSMatthew G Knepley             }
2768e6ccafaeSMatthew G Knepley           }
2769e6ccafaeSMatthew G Knepley         }
2770e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2771e6ccafaeSMatthew G Knepley         break;
2772e6ccafaeSMatthew G Knepley       }
2773e6ccafaeSMatthew G Knepley     }
277428b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2775e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
27763ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2777e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2778e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2779e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2780e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2781e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2782e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2783e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2784e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2785e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2786e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2787e6ccafaeSMatthew G Knepley       0, 3, 7, /* left */
2788e6ccafaeSMatthew G Knepley     };
2789e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2790e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2791e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2792e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2793e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2794e6ccafaeSMatthew G Knepley     };
2795e6ccafaeSMatthew G Knepley 
2796e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
27979566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2798e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2799e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2800e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2801e6ccafaeSMatthew G Knepley 
28029371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) {
2803e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2804e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2805e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2806e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2807e6ccafaeSMatthew G Knepley               break;
2808e6ccafaeSMatthew G Knepley             }
2809e6ccafaeSMatthew G Knepley           }
2810e6ccafaeSMatthew G Knepley         }
2811e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2812e6ccafaeSMatthew G Knepley         break;
2813e6ccafaeSMatthew G Knepley       }
2814e6ccafaeSMatthew G Knepley     }
281528b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2816e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28173ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2818e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2819e6ccafaeSMatthew G Knepley     /* Hexes
2820e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2821e6ccafaeSMatthew G Knepley        pointing up at the second.
2822e6ccafaeSMatthew G Knepley 
2823e6ccafaeSMatthew G Knepley           7---6
2824e6ccafaeSMatthew G Knepley          /|  /|
2825e6ccafaeSMatthew G Knepley         4---5 |
2826ddeab2a6SMatthew G. Knepley         | 1-|-2
2827e6ccafaeSMatthew G Knepley         |/  |/
2828ddeab2a6SMatthew G. Knepley         0---3
2829e6ccafaeSMatthew G Knepley 
2830e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2831e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2832e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2833e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2834e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2835e6ccafaeSMatthew G Knepley       0, 1, 2, 3, /* bottom */
2836e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2837ddeab2a6SMatthew G. Knepley       0, 3, 4, 5, /* front */
2838ddeab2a6SMatthew G. Knepley       2, 3, 5, 6, /* right */
2839ddeab2a6SMatthew G. Knepley       1, 2, 6, 7, /* back */
2840ddeab2a6SMatthew G. Knepley       0, 1, 4, 7, /* left */
2841e6ccafaeSMatthew G Knepley     };
2842e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2843ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2844e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2845ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2846ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2847ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2848ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2849e6ccafaeSMatthew G Knepley     };
2850e6ccafaeSMatthew G Knepley 
2851e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
28529566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2853e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2854e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2855e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2856e6ccafaeSMatthew G Knepley 
28579371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) {
2858e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2859e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2860e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2861e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2862e6ccafaeSMatthew G Knepley               break;
2863e6ccafaeSMatthew G Knepley             }
2864e6ccafaeSMatthew G Knepley           }
2865e6ccafaeSMatthew G Knepley         }
2866e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2867e6ccafaeSMatthew G Knepley         break;
2868e6ccafaeSMatthew G Knepley       }
2869e6ccafaeSMatthew G Knepley     }
287028b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2871e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28723ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2873e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2874e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2875e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2876e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2877e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2878e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2879e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2880e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2881e6ccafaeSMatthew G Knepley       0, 3, 4, 6, 7, 9, /* front */
2882e6ccafaeSMatthew G Knepley       1, 4, 5, 7, 8, 9, /* right */
2883e6ccafaeSMatthew G Knepley       2, 3, 5, 6, 8, 9, /* left */
2884e6ccafaeSMatthew G Knepley     };
2885e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2886e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2887e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2888e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2889e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2890e6ccafaeSMatthew G Knepley     };
2891e6ccafaeSMatthew G Knepley 
2892e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
28939566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2894e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2895e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2896e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2897e6ccafaeSMatthew G Knepley 
28989371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) {
2899e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2900e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2901e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2902e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2903e6ccafaeSMatthew G Knepley               break;
2904e6ccafaeSMatthew G Knepley             }
2905e6ccafaeSMatthew G Knepley           }
2906e6ccafaeSMatthew G Knepley         }
2907e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2908e6ccafaeSMatthew G Knepley         break;
2909e6ccafaeSMatthew G Knepley       }
2910e6ccafaeSMatthew G Knepley     }
291128b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2912e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29133ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2914e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2915e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2916e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2917e6ccafaeSMatthew G Knepley        pointing up at the second.
2918e6ccafaeSMatthew G Knepley 
2919e6ccafaeSMatthew G Knepley          7---6
2920e6ccafaeSMatthew G Knepley         /|  /|
2921e6ccafaeSMatthew G Knepley        4---5 |
2922e6ccafaeSMatthew G Knepley        | 3-|-2
2923e6ccafaeSMatthew G Knepley        |/  |/
2924e6ccafaeSMatthew G Knepley        0---1
2925e6ccafaeSMatthew G Knepley 
2926e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2927e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2928e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2929e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2930e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2931e6ccafaeSMatthew G Knepley       0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2932e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2933e6ccafaeSMatthew G Knepley       0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2934e6ccafaeSMatthew G Knepley       1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2935e6ccafaeSMatthew G Knepley       2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2936e6ccafaeSMatthew G Knepley       0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2937e6ccafaeSMatthew G Knepley     };
2938e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2939e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2940e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2941e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2942e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2943e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2944e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2945e6ccafaeSMatthew G Knepley     };
2946e6ccafaeSMatthew G Knepley 
2947e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
29489566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2949e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2950e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2951e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2952e6ccafaeSMatthew G Knepley 
29539371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) {
2954e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2955e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2956e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2957e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2958e6ccafaeSMatthew G Knepley               break;
2959e6ccafaeSMatthew G Knepley             }
2960e6ccafaeSMatthew G Knepley           }
2961e6ccafaeSMatthew G Knepley         }
2962e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2963e6ccafaeSMatthew G Knepley         break;
2964e6ccafaeSMatthew G Knepley       }
2965e6ccafaeSMatthew G Knepley     }
296628b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2967e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29683ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2969e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2970e6ccafaeSMatthew G Knepley   if (!posOrient) {
29719566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
2972e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
2973e6ccafaeSMatthew G Knepley   } else {
29749566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
2975e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2976e6ccafaeSMatthew G Knepley   }
2977e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
29783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2979e6ccafaeSMatthew G Knepley }
2980e6ccafaeSMatthew G Knepley 
2981c08575a3SMatthew G. Knepley /*@
2982c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2983c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2984c08575a3SMatthew G. Knepley 
298520f4b53cSBarry Smith   Not Collective
2986c08575a3SMatthew G. Knepley 
2987c08575a3SMatthew G. Knepley   Input Parameters:
2988c08575a3SMatthew G. Knepley + dm           - The original mesh
2989c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2990c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2991c08575a3SMatthew G. Knepley . face         - The face vertices
2992c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2993c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2994c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2995c08575a3SMatthew G. Knepley 
2996d8d19677SJose E. Roman   Output Parameters:
2997c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
299820f4b53cSBarry Smith - posOriented  - `PETSC_TRUE` if the face was oriented with outward normal
2999c08575a3SMatthew G. Knepley 
3000c08575a3SMatthew G. Knepley   Level: developer
3001c08575a3SMatthew G. Knepley 
30021cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
3003c08575a3SMatthew G. Knepley @*/
3004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
3005d71ae5a4SJacob Faibussowitsch {
30060298fd71SBarry Smith   const PetscInt *cone = NULL;
3007e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
3008e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
3009e6ccafaeSMatthew G Knepley 
3010e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30119566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
30129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
3013e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
3014e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
3015e6ccafaeSMatthew G Knepley 
3016e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
3017e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
30189371c9d4SSatish Balay         found = PETSC_TRUE;
30199371c9d4SSatish Balay         break;
3020e6ccafaeSMatthew G Knepley       }
3021e6ccafaeSMatthew G Knepley     }
3022e6ccafaeSMatthew G Knepley     if (found) {
3023e6ccafaeSMatthew G Knepley       indices[v2]      = v;
3024e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
3025e6ccafaeSMatthew G Knepley       ++v2;
3026e6ccafaeSMatthew G Knepley     } else {
3027e6ccafaeSMatthew G Knepley       oppositeVertex = v;
3028e6ccafaeSMatthew G Knepley     }
3029e6ccafaeSMatthew G Knepley   }
30309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
30313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3032e6ccafaeSMatthew G Knepley }
3033e6ccafaeSMatthew G Knepley 
3034e6ccafaeSMatthew G Knepley /*
3035cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
3036e6ccafaeSMatthew G Knepley 
303720f4b53cSBarry Smith   Not Collective
3038e6ccafaeSMatthew G Knepley 
3039e6ccafaeSMatthew G Knepley   Input Parameters:
304020f4b53cSBarry Smith   + dm              - The `DMPLEX`
3041e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
304220f4b53cSBarry Smith   . faceVertices    - The vertices in the face for `dm`
3043e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
304420f4b53cSBarry Smith   . numCorners      - The number of vertices in the `cell`
304520f4b53cSBarry Smith   . cell            - A cell in `dm` containing the face
3046e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
3047e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
3048e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
3049e6ccafaeSMatthew G Knepley 
30502fe279fdSBarry Smith   Output Parameter:
3051e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
3052e6ccafaeSMatthew G Knepley 
3053e6ccafaeSMatthew G Knepley   Level: developer
3054e6ccafaeSMatthew G Knepley */
3055d71ae5a4SJacob 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)
3056d71ae5a4SJacob Faibussowitsch {
305782f516ccSBarry Smith   MPI_Comm        comm;
3058e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
3059e6ccafaeSMatthew G Knepley   const PetscInt *faces;
3060e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
3061e6ccafaeSMatthew G Knepley 
3062e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30639566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
30649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
306563a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
3066e6ccafaeSMatthew G Knepley #if 0
3067e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
30689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3069e6ccafaeSMatthew G Knepley #else
3070e6ccafaeSMatthew G Knepley   {
3071e6ccafaeSMatthew G Knepley     PetscInt f;
3072e6ccafaeSMatthew G Knepley 
3073e6ccafaeSMatthew G Knepley     numFaces = 0;
30749566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3075e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
3076e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
3077e6ccafaeSMatthew G Knepley 
30789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
30799566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
3080e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
3081e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
3082e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
3083e6ccafaeSMatthew G Knepley         PetscInt       v;
3084e6ccafaeSMatthew G Knepley 
3085e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
3086e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
3087e6ccafaeSMatthew G Knepley         }
3088e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
3089e6ccafaeSMatthew G Knepley       }
3090e6ccafaeSMatthew G Knepley       if (d == dof) {
3091e6ccafaeSMatthew G Knepley         numFaces               = 1;
3092e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
3093e6ccafaeSMatthew G Knepley       }
3094e6ccafaeSMatthew G Knepley     }
3095e6ccafaeSMatthew G Knepley   }
3096e6ccafaeSMatthew G Knepley #endif
309763a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3098f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3099e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
31009566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3101e6ccafaeSMatthew G Knepley   } else {
3102e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3103e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3104e6ccafaeSMatthew G Knepley 
31059566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3106e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3107e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
3108e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
31099566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3110e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3111e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3112e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3113e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3114e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3115e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3116e6ccafaeSMatthew G Knepley           break;
3117e6ccafaeSMatthew G Knepley         }
3118e6ccafaeSMatthew G Knepley       }
311963a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3120e6ccafaeSMatthew G Knepley     }
31219566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
31229566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
31239566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3124e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3125e6ccafaeSMatthew G Knepley   }
3126ef07cca7SMatthew G. Knepley #if 0
31279566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3128ef07cca7SMatthew G. Knepley #else
31299566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3130ef07cca7SMatthew G. Knepley #endif
31313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3132e6ccafaeSMatthew G Knepley }
3133e6ccafaeSMatthew G Knepley 
3134d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3135d71ae5a4SJacob Faibussowitsch {
313682f516ccSBarry Smith   MPI_Comm        comm;
313753156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3138efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
3139efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3140efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3141fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3142efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3143e6ccafaeSMatthew G Knepley 
3144e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
31459566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3146efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
31479566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
31489566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
31499566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3150efa14ee0SMatthew G Knepley   /* Setup chart */
31519566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
31529566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
31539566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
31549566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3155e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3156e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3157efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
3158e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
31599566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
31609566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
31619566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
31629566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
316348a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
316448a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
31659566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3166d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3167e6ccafaeSMatthew G Knepley   /* Create face cones */
31689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
31699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
31709566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3171e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3172e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3173efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
31740298fd71SBarry Smith     PetscInt      *closure = NULL;
3175efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3176e6ccafaeSMatthew G Knepley 
31779566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3178efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3179efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3180e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3181e6ccafaeSMatthew G Knepley 
3182efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3183efa14ee0SMatthew G Knepley         ++numCorners;
31849566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3185efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3186efa14ee0SMatthew G Knepley           closure[faceSize] = point;
318765560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3188e6ccafaeSMatthew G Knepley           ++faceSize;
3189e6ccafaeSMatthew G Knepley         }
3190e6ccafaeSMatthew G Knepley       }
3191e6ccafaeSMatthew G Knepley     }
319263a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
319348a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
31949566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3195e6ccafaeSMatthew G Knepley   }
31969566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
31979566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
31989566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3199e6ccafaeSMatthew G Knepley   /* Build coordinates */
3200efa14ee0SMatthew G Knepley   {
3201efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3202efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3203efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3204285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
320524640c55SToby Isaac     const char  *name;
3206efa14ee0SMatthew G Knepley 
32079566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
32089566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
32099566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
32109566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
32119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
32129566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
32139566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3214efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3215efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3216efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3217efa14ee0SMatthew G Knepley       PetscInt       dof;
3218efa14ee0SMatthew G Knepley 
32199566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32209566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
32219566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3222e6ccafaeSMatthew G Knepley     }
32239566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
32249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
32259566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
32269566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
32279566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
32289566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
32299566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3230830e53efSMatthew G. Knepley     if (coordSize) {
32319566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
32329566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3233efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3234efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3235efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3236efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3237e6ccafaeSMatthew G Knepley 
32389566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32399566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
32409566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
32419566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
324263a3b9bcSJacob 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);
3243e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3244e6ccafaeSMatthew G Knepley       }
32459566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
32469566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
32473b399e24SMatthew G. Knepley     }
32489566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
32499566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3250e6ccafaeSMatthew G Knepley   }
3251efa14ee0SMatthew G Knepley   /* Cleanup */
32529566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
32539566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
32549566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
32559566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
32563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3257e6ccafaeSMatthew G Knepley }
3258e6ccafaeSMatthew G Knepley 
3259d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3260d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
3261d71ae5a4SJacob Faibussowitsch {
32623982b651SMatthew G. Knepley   PetscInt       subPoint;
32633982b651SMatthew G. Knepley   PetscErrorCode ierr;
32643982b651SMatthew G. Knepley 
32659371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
32669371c9d4SSatish Balay   if (ierr) return -1;
32673982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
32683982b651SMatthew G. Knepley }
32693982b651SMatthew G. Knepley 
3270d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3271d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[])
3272d71ae5a4SJacob Faibussowitsch {
3273dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3274dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3275dd05d810SMatthew G. Knepley 
3276dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3277dd05d810SMatthew G. Knepley   if (ierr) return -1;
3278dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3279dd05d810SMatthew G. Knepley }
3280dd05d810SMatthew G. Knepley 
3281d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3282d71ae5a4SJacob Faibussowitsch {
3283695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3284212103e5SMatthew Knepley   PetscInt Nl, l, d;
3285212103e5SMatthew Knepley 
3286212103e5SMatthew Knepley   PetscFunctionBegin;
3287695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3288695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3289695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
32909566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3291212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3292212103e5SMatthew Knepley     DMLabel         label, newlabel;
3293212103e5SMatthew Knepley     const char     *lname;
3294d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3295212103e5SMatthew Knepley     IS              valueIS;
3296212103e5SMatthew Knepley     const PetscInt *values;
3297212103e5SMatthew Knepley     PetscInt        Nv, v;
3298212103e5SMatthew Knepley 
32999566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
33009566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
33019566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
33029566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
33039566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3304d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
33059566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
33069566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
33079566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
33089566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
33099566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
33109566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
33119566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
33129566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3313212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3314212103e5SMatthew Knepley       IS              pointIS;
3315212103e5SMatthew Knepley       const PetscInt *points;
3316212103e5SMatthew Knepley       PetscInt        Np, p;
3317212103e5SMatthew Knepley 
33189566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
33199566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
33209566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3321212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3322212103e5SMatthew Knepley         const PetscInt point = points[p];
3323212103e5SMatthew Knepley         PetscInt       subp;
3324212103e5SMatthew Knepley 
33259566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3326212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
33279566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3328212103e5SMatthew Knepley       }
33299566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
33309566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3331212103e5SMatthew Knepley     }
33329566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
33339566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3334212103e5SMatthew Knepley   }
33353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3336212103e5SMatthew Knepley }
3337212103e5SMatthew Knepley 
3338d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3339d71ae5a4SJacob Faibussowitsch {
334082f516ccSBarry Smith   MPI_Comm         comm;
334153156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3342efa14ee0SMatthew G Knepley   IS              *subpointIS;
3343efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
33443982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3345dd05d810SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, v;
33460d366550SMatthew G. Knepley   PetscMPIInt      rank;
3347e6ccafaeSMatthew G Knepley 
3348e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
33499566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
33509566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3351efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
33529566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
33539566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3354bec263e5SMatthew G. Knepley   if (cellHeight) {
33559566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
33569566063dSJacob Faibussowitsch     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3357bec263e5SMatthew G. Knepley   } else {
3358bec263e5SMatthew G. Knepley     DMLabel         depth;
3359bec263e5SMatthew G. Knepley     IS              pointIS;
3360bec263e5SMatthew G. Knepley     const PetscInt *points;
3361b85c8bf9SMatthew G. Knepley     PetscInt        numPoints = 0;
3362bec263e5SMatthew G. Knepley 
33639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
33649566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3365b85c8bf9SMatthew G. Knepley     if (pointIS) {
33669566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
33679566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3368b85c8bf9SMatthew G. Knepley     }
3369bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3370bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3371bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3372bec263e5SMatthew G. Knepley 
33739566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3374bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
33759566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
33769566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3377bec263e5SMatthew G. Knepley       }
33789566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3379bec263e5SMatthew G. Knepley     }
33809566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
33819566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3382bec263e5SMatthew G. Knepley   }
3383efa14ee0SMatthew G Knepley   /* Setup chart */
33849566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3385dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
33869566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3387e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
33889566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3389e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3390e6ccafaeSMatthew G Knepley   }
3391dd05d810SMatthew G. Knepley   // Determine submesh dimension
3392dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3393dd05d810SMatthew G. Knepley   if (sdim > 0) {
3394dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3395dd05d810SMatthew G. Knepley     sdim = dim;
3396dd05d810SMatthew G. Knepley   } else {
3397dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3398dd05d810SMatthew G. Knepley     PetscInt lsdim;
3399dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3400dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3401a5a714f4SStefano Zampini     PetscCall(MPIU_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPI_MAX, comm));
3402dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3403dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3404dd05d810SMatthew G. Knepley   }
34059566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
34069566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3407e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3408dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3409dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3410dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3411dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3412dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
34139566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
34149566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3415e6ccafaeSMatthew G Knepley   }
3416412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
34179566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3418dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3419e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3420e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3421e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3422e6ccafaeSMatthew G Knepley       const PetscInt *cone;
342315100a53SVaclav Hapla       PetscInt        coneSize;
3424e6ccafaeSMatthew G Knepley 
34259566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3426dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
342715100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
342815100a53SVaclav Hapla 
34299566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3430e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34319566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3432e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3433e6ccafaeSMatthew G Knepley         }
34349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
34359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
343615100a53SVaclav Hapla       } else {
343715100a53SVaclav Hapla         DMPolytopeType ct;
343815100a53SVaclav Hapla 
343915100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
344015100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
344115100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3442e6ccafaeSMatthew G Knepley       }
3443e6ccafaeSMatthew G Knepley     }
3444e6ccafaeSMatthew G Knepley   }
34459566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
34469566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3447e6ccafaeSMatthew G Knepley   /* Set cones */
34489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
34499566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3450dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3451e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3452e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3453e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
34540e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
34550d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3456e6ccafaeSMatthew G Knepley 
3457dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
34580d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
34590d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
34600d366550SMatthew G. Knepley 
34619566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
34629566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
34630d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3464064cae4fSPierre Jolivet           PetscBool isHybrid = PETSC_FALSE;
3465412e9a14SMatthew G. Knepley 
34669566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3467412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
34689566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
34690d366550SMatthew G. Knepley           if (subc >= 0) {
34700d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
34710d366550SMatthew G. Knepley 
34729566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
34739566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
34749566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
34750d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
34760d366550SMatthew G. Knepley               if (cone[c] == point) {
34770d366550SMatthew G. Knepley                 fornt = ornt[c];
34780d366550SMatthew G. Knepley                 break;
34790d366550SMatthew G. Knepley               }
34800d366550SMatthew G. Knepley             }
34810d366550SMatthew G. Knepley             break;
34820d366550SMatthew G. Knepley           }
34830d366550SMatthew G. Knepley         }
34840d366550SMatthew G. Knepley       }
34859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
34869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
34879566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
34889566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3489e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34909566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
349101a2673eSMatthew G. Knepley         if (subc >= 0) {
349201a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
34933982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
349401a2673eSMatthew G. Knepley           ++coneSizeNew;
349501a2673eSMatthew G. Knepley         }
3496e6ccafaeSMatthew G Knepley       }
349763a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
34989566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
34999566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
35009566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3501e6ccafaeSMatthew G Knepley     }
3502e6ccafaeSMatthew G Knepley   }
35039566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
35049566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
35059566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3506e6ccafaeSMatthew G Knepley   /* Build coordinates */
3507e6ccafaeSMatthew G Knepley   {
3508e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3509e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3510e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3511c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
351224640c55SToby Isaac     const char  *name;
3513e6ccafaeSMatthew G Knepley 
35149566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
35159566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
35169566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
35179566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
35189566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
35199566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
35209566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
35219566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0] + numSubPoints[0]));
3522e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3523e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3524e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3525e6ccafaeSMatthew G Knepley       PetscInt       dof;
3526e6ccafaeSMatthew G Knepley 
35279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
35289566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
35299566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3530e6ccafaeSMatthew G Knepley     }
35319566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
35329566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
35339566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
35349566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
35359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
35369566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
35379566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
35389566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
35399566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
35409566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3541e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3542e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3543e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3544e6ccafaeSMatthew G Knepley       PetscInt       dof, off, sdof, soff, d;
3545e6ccafaeSMatthew G Knepley 
35469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
35479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
35489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
35499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
355063a3b9bcSJacob 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);
3551efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3552e6ccafaeSMatthew G Knepley     }
35539566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
35549566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
35559566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
35569566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3557e6ccafaeSMatthew G Knepley   }
35583982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
35593982b651SMatthew G. Knepley   {
35603982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
35613982b651SMatthew G. Knepley     IS                 subpIS;
35623982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
35635033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
35645033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3565dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
35665033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
35675033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
35683982b651SMatthew G. Knepley 
35699566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
35705033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
35719566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
35729566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
35739566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
35749566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
35759566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
35763982b651SMatthew G. Knepley     if (subpIS) {
3577dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3578dd05d810SMatthew G. Knepley 
35799566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
35809566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3581dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3582dd05d810SMatthew G. Knepley       if (!sorted) {
3583dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3584dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3585dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3586dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3587dd05d810SMatthew G. Knepley       }
35883982b651SMatthew G. Knepley     }
35899566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
35903982b651SMatthew G. Knepley     if (numRoots >= 0) {
35915033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
35925033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
35939566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
35943982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
35953982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
35963982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
35973982b651SMatthew G. Knepley       }
35983982b651SMatthew G. Knepley       /* Set subleaves */
35993982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
36003982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3601dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
36023982b651SMatthew G. Knepley 
36033982b651SMatthew G. Knepley         if (subpoint < 0) continue;
36043982b651SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
36053982b651SMatthew G. Knepley         newLocalPoints[point - pStart].index = subpoint;
36063982b651SMatthew G. Knepley         ++numSubleaves;
36073982b651SMatthew G. Knepley       }
36083982b651SMatthew G. Knepley       /* Must put in owned subpoints */
36093982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
36103982b651SMatthew G. Knepley         newOwners[p - pStart].rank  = -3;
36113982b651SMatthew G. Knepley         newOwners[p - pStart].index = -3;
36123982b651SMatthew G. Knepley       }
3613d9384f27SMatthew G. Knepley       for (p = 0; p < numSubpoints; ++p) {
36145033f954SMatthew G. Knepley         /* Hold on to currently owned points */
36155033f954SMatthew G. Knepley         if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
36165033f954SMatthew G. Knepley         else newOwners[subpoints[p] - pStart].rank = rank;
3617d9384f27SMatthew G. Knepley         newOwners[subpoints[p] - pStart].index = p;
36183982b651SMatthew G. Knepley       }
36199566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
36209566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
36219371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
36229371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
36239566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
36249566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
36259566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
36269566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
36275033f954SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
36283982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3629dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
36303982b651SMatthew G. Knepley 
36313982b651SMatthew G. Knepley         if (subpoint < 0) continue;
36329371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
36339371c9d4SSatish Balay           ++ll;
36349371c9d4SSatish Balay           continue;
36359371c9d4SSatish Balay         }
36363982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
36373982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
36383982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
363963a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
364063a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
36413982b651SMatthew G. Knepley         ++sl;
36423982b651SMatthew G. Knepley       }
36431dca8a05SBarry 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);
36449566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
36459566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
36463982b651SMatthew G. Knepley     }
364748a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3648dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
36493982b651SMatthew G. Knepley   }
3650212103e5SMatthew Knepley   /* Filter labels */
36519566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3652efa14ee0SMatthew G Knepley   /* Cleanup */
3653dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
36549566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
36559566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3656e6ccafaeSMatthew G Knepley   }
36579566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
36583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3659e6ccafaeSMatthew G Knepley }
3660e6ccafaeSMatthew G Knepley 
3661d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
3662d71ae5a4SJacob Faibussowitsch {
36633982b651SMatthew G. Knepley   PetscFunctionBegin;
36649566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm));
36653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36663982b651SMatthew G. Knepley }
36673982b651SMatthew G. Knepley 
3668d0fa310fSMatthew G. Knepley /*@
3669e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3670e6ccafaeSMatthew G Knepley 
3671e6ccafaeSMatthew G Knepley   Input Parameters:
3672e6ccafaeSMatthew G Knepley + dm          - The original mesh
367320f4b53cSBarry Smith . vertexLabel - The `DMLabel` marking points contained in the surface
3674158acfadSMatthew G. Knepley . value       - The label value to use
367520f4b53cSBarry Smith - markedFaces - `PETSC_TRUE` if surface faces are marked in addition to vertices, `PETSC_FALSE` if only vertices are marked
3676e6ccafaeSMatthew G Knepley 
3677e6ccafaeSMatthew G Knepley   Output Parameter:
3678e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3679e6ccafaeSMatthew G Knepley 
3680e6ccafaeSMatthew G Knepley   Level: developer
3681e6ccafaeSMatthew G Knepley 
368220f4b53cSBarry Smith   Note:
368320f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
368420f4b53cSBarry Smith 
36851cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3686830e53efSMatthew G. Knepley @*/
3687d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3688d71ae5a4SJacob Faibussowitsch {
3689827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3690827c4036SVaclav Hapla   PetscInt               dim, cdim;
3691e6ccafaeSMatthew G Knepley 
3692e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3693e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36944f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
36959566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
36969566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
36979566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
36989566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
36999566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
37009566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
37019566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
370208401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3703827c4036SVaclav Hapla   if (interpolated) {
37049566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3705e6ccafaeSMatthew G Knepley   } else {
37069566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3707e6ccafaeSMatthew G Knepley   }
37085de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
37093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3710e6ccafaeSMatthew G Knepley }
3711e6ccafaeSMatthew G Knepley 
3712d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3713d71ae5a4SJacob Faibussowitsch {
3714766ab985SMatthew G. Knepley   MPI_Comm        comm;
3715766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3716766ab985SMatthew G. Knepley   IS              subvertexIS;
3717766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3718fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3719766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3720412e9a14SMatthew G. Knepley   PetscInt        c, f;
3721766ab985SMatthew G. Knepley 
3722766ab985SMatthew G. Knepley   PetscFunctionBegin;
37239566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3724766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
37259566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
37269566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
37279566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3728766ab985SMatthew G. Knepley   /* Setup chart */
37299566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
37309566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
37319566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
37329566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3733766ab985SMatthew G. Knepley   /* Set cone sizes */
3734766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3735766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3736766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
37379566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
37389566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
373948a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
374048a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
37419566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3742d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3743766ab985SMatthew G. Knepley   /* Create face cones */
37449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
37459566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3746766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3747766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3748766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
374987feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3750064cae4fSPierre Jolivet     PetscBool       isHybrid = PETSC_FALSE;
375187feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3752766ab985SMatthew G. Knepley 
37539566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3754412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
37559566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
375687feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
37579566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
375887feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
375987feddfdSMatthew G. Knepley     }
37609566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
37619566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
37629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
376327234c99SMatthew G. Knepley     /* Not true in parallel
376408401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
376587feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
376687feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3767064cae4fSPierre Jolivet       PetscBool isHybrid = PETSC_FALSE;
3768766ab985SMatthew G. Knepley 
37699566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3770412e9a14SMatthew G. Knepley       if (isHybrid) continue;
377187feddfdSMatthew G. Knepley       /* I know this is a crap search */
377287feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
377387feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3774766ab985SMatthew G. Knepley       }
377563a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
37769566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3777766ab985SMatthew G. Knepley     }
37789566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
377987feddfdSMatthew G. Knepley     ++newFacePoint;
3780766ab985SMatthew G. Knepley   }
37819566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
37829566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
37839566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3784766ab985SMatthew G. Knepley   /* Build coordinates */
3785766ab985SMatthew G. Knepley   {
3786766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3787766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3788766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3789c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
379024640c55SToby Isaac     const char  *name;
3791766ab985SMatthew G. Knepley 
37929566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
37939566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
37949566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
37959566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
37969566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
37979566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
37989566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
37999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3800766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3801766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3802766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3803766ab985SMatthew G. Knepley       PetscInt       dof;
3804766ab985SMatthew G. Knepley 
38059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
38069566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
38079566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3808766ab985SMatthew G. Knepley     }
38099566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
38109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
38119566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
38129566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
38139566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
38149566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
38159566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
38169566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
38179566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
38189566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3819766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3820766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3821766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3822766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3823766ab985SMatthew G. Knepley 
38249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
38259566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
38269566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
38279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
382863a3b9bcSJacob 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);
3829766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3830766ab985SMatthew G. Knepley     }
38319566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
38329566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
38339566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
38349566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3835766ab985SMatthew G. Knepley   }
3836aca35d17SMatthew G. Knepley   /* Build SF */
3837aca35d17SMatthew G. Knepley   CHKMEMQ;
3838aca35d17SMatthew G. Knepley   {
3839aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3840aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3841bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3842aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3843bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
384449c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3845bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3846aca35d17SMatthew G. Knepley 
38479566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
38489566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
38499566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
38509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
38519566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
38529566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
3853aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3854aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
38559566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
3856bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
3857bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3858bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3859bdcf2095SMatthew G. Knepley       }
38609e0823b2SMatthew G. Knepley       /* Set subleaves */
3861aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3862aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3863bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3864aca35d17SMatthew G. Knepley 
386563a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
3866bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3867bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
3868bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
3869bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3870aca35d17SMatthew G. Knepley       }
38719e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
38729e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
38739e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
38749e0823b2SMatthew G. Knepley 
38759e0823b2SMatthew G. Knepley         if (subPoint < 0) {
38769e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
38779e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
38789e0823b2SMatthew G. Knepley         } else {
38799e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
38809e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
38819e0823b2SMatthew G. Knepley         }
3882bdcf2095SMatthew G. Knepley       }
38839566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
38849566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
38859566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
38869566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
38879566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
38889566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
388949c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3890bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3891bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3892aca35d17SMatthew G. Knepley 
3893aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
38949371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
38959371c9d4SSatish Balay           ++ll;
38969371c9d4SSatish Balay           continue;
38979371c9d4SSatish Balay         }
3898aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3899bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3900bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
390163a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
390263a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
3903aca35d17SMatthew G. Knepley         ++sl;
3904aca35d17SMatthew G. Knepley       }
39059566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
39061dca8a05SBarry 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);
39079566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
3908aca35d17SMatthew G. Knepley     }
3909aca35d17SMatthew G. Knepley   }
3910aca35d17SMatthew G. Knepley   CHKMEMQ;
3911766ab985SMatthew G. Knepley   /* Cleanup */
39129566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
39139566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
39149566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
39153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3916766ab985SMatthew G. Knepley }
3917766ab985SMatthew G. Knepley 
3918d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3919d71ae5a4SJacob Faibussowitsch {
39203982b651SMatthew G. Knepley   DMLabel label = NULL;
3921766ab985SMatthew G. Knepley 
3922766ab985SMatthew G. Knepley   PetscFunctionBegin;
39239566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
39249566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm));
39253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3926766ab985SMatthew G. Knepley }
3927766ab985SMatthew G. Knepley 
3928c08575a3SMatthew G. Knepley /*@C
392920f4b53cSBarry 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.
3930766ab985SMatthew G. Knepley 
3931766ab985SMatthew G. Knepley   Input Parameters:
3932766ab985SMatthew G. Knepley + dm          - The original mesh
393327c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
393420f4b53cSBarry Smith . label       - A label name, or `NULL`
393527c04023SMatthew G. Knepley - value       - A label value
3936766ab985SMatthew G. Knepley 
3937766ab985SMatthew G. Knepley   Output Parameter:
3938766ab985SMatthew G. Knepley . subdm - The surface mesh
3939766ab985SMatthew G. Knepley 
3940766ab985SMatthew G. Knepley   Level: developer
3941766ab985SMatthew G. Knepley 
394220f4b53cSBarry Smith   Note:
394320f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
394420f4b53cSBarry Smith 
39451cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
3946c08575a3SMatthew G. Knepley @*/
3947d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3948d71ae5a4SJacob Faibussowitsch {
3949c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
3950766ab985SMatthew G. Knepley 
3951766ab985SMatthew G. Knepley   PetscFunctionBegin;
3952766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39534f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
39549566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
39559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
39569566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
39579566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
39589566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
39599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
39609566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
3961766ab985SMatthew G. Knepley   if (depth == dim) {
39629566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
3963766ab985SMatthew G. Knepley   } else {
39649566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
3965e6ccafaeSMatthew G Knepley   }
39665de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
39673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3968e6ccafaeSMatthew G Knepley }
3969e6ccafaeSMatthew G Knepley 
3970bec263e5SMatthew G. Knepley /*@
39710e18dc48SMatthew G. Knepley   DMPlexReorderCohesiveSupports - Ensure that face supports for cohesive end caps are ordered
39720e18dc48SMatthew G. Knepley 
39730e18dc48SMatthew G. Knepley   Not Collective
39740e18dc48SMatthew G. Knepley 
39750e18dc48SMatthew G. Knepley   Input Parameter:
39760e18dc48SMatthew G. Knepley . dm - The `DM` containing cohesive cells
39770e18dc48SMatthew G. Knepley 
39780e18dc48SMatthew G. Knepley   Level: developer
39790e18dc48SMatthew G. Knepley 
3980a4e35b19SJacob Faibussowitsch   Note:
3981a4e35b19SJacob Faibussowitsch   For the negative size (first) face, the cohesive cell should be first in the support, and for
3982a4e35b19SJacob Faibussowitsch   the positive side (second) face, the cohesive cell should be second in the support.
39830e18dc48SMatthew G. Knepley 
39840e18dc48SMatthew G. Knepley .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexCreateCohesiveSubmesh()`
39850e18dc48SMatthew G. Knepley @*/
39860e18dc48SMatthew G. Knepley PetscErrorCode DMPlexReorderCohesiveSupports(DM dm)
39870e18dc48SMatthew G. Knepley {
39880e18dc48SMatthew G. Knepley   PetscInt dim, cStart, cEnd;
39890e18dc48SMatthew G. Knepley 
39900e18dc48SMatthew G. Knepley   PetscFunctionBegin;
39910e18dc48SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
39920e18dc48SMatthew G. Knepley   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cStart, &cEnd));
39930e18dc48SMatthew G. Knepley   for (PetscInt c = cStart; c < cEnd; ++c) {
39940e18dc48SMatthew G. Knepley     const PetscInt *cone;
39950e18dc48SMatthew G. Knepley     PetscInt        coneSize;
39960e18dc48SMatthew G. Knepley 
39970e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
39980e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetCone(dm, c, &cone));
39990e18dc48SMatthew G. Knepley     PetscCheck(coneSize >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid cell %" PetscInt_FMT " cone size %" PetscInt_FMT " must be >= 2", c, coneSize);
40000e18dc48SMatthew G. Knepley     for (PetscInt s = 0; s < 2; ++s) {
40010e18dc48SMatthew G. Knepley       const PetscInt *supp;
40020e18dc48SMatthew G. Knepley       PetscInt        suppSize, newsupp[2];
40030e18dc48SMatthew G. Knepley 
40040e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupportSize(dm, cone[s], &suppSize));
40050e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupport(dm, cone[s], &supp));
40060e18dc48SMatthew G. Knepley       if (suppSize == 2) {
40070e18dc48SMatthew G. Knepley         /* Reorder hybrid end cap support */
40080e18dc48SMatthew G. Knepley         if (supp[s] == c) {
40090e18dc48SMatthew G. Knepley           newsupp[0] = supp[1];
40100e18dc48SMatthew G. Knepley           newsupp[1] = supp[0];
40110e18dc48SMatthew G. Knepley         } else {
40120e18dc48SMatthew G. Knepley           newsupp[0] = supp[0];
40130e18dc48SMatthew G. Knepley           newsupp[1] = supp[1];
40140e18dc48SMatthew G. Knepley         }
40150e18dc48SMatthew 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);
40160e18dc48SMatthew G. Knepley         PetscCall(DMPlexSetSupport(dm, cone[s], newsupp));
40170e18dc48SMatthew G. Knepley       }
40180e18dc48SMatthew G. Knepley     }
40190e18dc48SMatthew G. Knepley   }
40200e18dc48SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
40210e18dc48SMatthew G. Knepley }
40220e18dc48SMatthew G. Knepley 
40230e18dc48SMatthew G. Knepley /*@
4024bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
4025bec263e5SMatthew G. Knepley 
4026bec263e5SMatthew G. Knepley   Input Parameters:
4027bec263e5SMatthew G. Knepley + dm        - The original mesh
402820f4b53cSBarry Smith . cellLabel - The `DMLabel` marking cells contained in the new mesh
4029bec263e5SMatthew G. Knepley - value     - The label value to use
4030bec263e5SMatthew G. Knepley 
4031bec263e5SMatthew G. Knepley   Output Parameter:
4032bec263e5SMatthew G. Knepley . subdm - The new mesh
4033bec263e5SMatthew G. Knepley 
4034bec263e5SMatthew G. Knepley   Level: developer
4035bec263e5SMatthew G. Knepley 
403620f4b53cSBarry Smith   Note:
403720f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
403820f4b53cSBarry Smith 
40391cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
4040bec263e5SMatthew G. Knepley @*/
4041d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
4042d71ae5a4SJacob Faibussowitsch {
4043b47cd10cSMatthew G. Knepley   PetscInt dim, overlap;
4044bec263e5SMatthew G. Knepley 
4045bec263e5SMatthew G. Knepley   PetscFunctionBegin;
4046bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40474f572ea9SToby Isaac   PetscAssertPointer(subdm, 4);
40489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
40499566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
40509566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
4051bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
40529566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm));
40535de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
4054b47cd10cSMatthew G. Knepley   // It is possible to obtain a surface mesh where some faces are in SF
4055b47cd10cSMatthew G. Knepley   //   We should either mark the mesh as having an overlap, or delete these from the SF
4056b47cd10cSMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
4057b47cd10cSMatthew G. Knepley   if (!overlap) {
4058b47cd10cSMatthew G. Knepley     PetscSF         sf;
4059b47cd10cSMatthew G. Knepley     const PetscInt *leaves;
4060b47cd10cSMatthew G. Knepley     PetscInt        cStart, cEnd, Nl;
4061b47cd10cSMatthew G. Knepley     PetscBool       hasSubcell = PETSC_FALSE, ghasSubcell;
4062b47cd10cSMatthew G. Knepley 
4063b47cd10cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd));
4064b47cd10cSMatthew G. Knepley     PetscCall(DMGetPointSF(*subdm, &sf));
4065b47cd10cSMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
4066b47cd10cSMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
4067b47cd10cSMatthew G. Knepley       const PetscInt point = leaves ? leaves[l] : l;
4068b47cd10cSMatthew G. Knepley 
4069b47cd10cSMatthew G. Knepley       if (point >= cStart && point < cEnd) {
4070b47cd10cSMatthew G. Knepley         hasSubcell = PETSC_TRUE;
4071b47cd10cSMatthew G. Knepley         break;
4072b47cd10cSMatthew G. Knepley       }
4073b47cd10cSMatthew G. Knepley     }
4074b47cd10cSMatthew G. Knepley     PetscCall(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
4075b47cd10cSMatthew G. Knepley     if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1));
4076b47cd10cSMatthew G. Knepley   }
40773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4078bec263e5SMatthew G. Knepley }
4079bec263e5SMatthew G. Knepley 
408064beef6dSMatthew G. Knepley /*@
408120f4b53cSBarry Smith   DMPlexGetSubpointMap - Returns a `DMLabel` with point dimension as values
408264beef6dSMatthew G. Knepley 
408364beef6dSMatthew G. Knepley   Input Parameter:
408420f4b53cSBarry Smith . dm - The submesh `DM`
408564beef6dSMatthew G. Knepley 
408664beef6dSMatthew G. Knepley   Output Parameter:
408720f4b53cSBarry Smith . subpointMap - The `DMLabel` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
408864beef6dSMatthew G. Knepley 
408964beef6dSMatthew G. Knepley   Level: developer
409064beef6dSMatthew G. Knepley 
40911cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
409264beef6dSMatthew G. Knepley @*/
4093d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
4094d71ae5a4SJacob Faibussowitsch {
4095e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4096e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40974f572ea9SToby Isaac   PetscAssertPointer(subpointMap, 2);
409865663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
40993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4100e6ccafaeSMatthew G Knepley }
4101e6ccafaeSMatthew G Knepley 
4102c08575a3SMatthew G. Knepley /*@
410320f4b53cSBarry Smith   DMPlexSetSubpointMap - Sets the `DMLabel` with point dimension as values
4104c08575a3SMatthew G. Knepley 
4105c08575a3SMatthew G. Knepley   Input Parameters:
410620f4b53cSBarry Smith + dm          - The submesh `DM`
410720f4b53cSBarry Smith - subpointMap - The `DMLabel` of all the points from the original mesh in this submesh
4108c08575a3SMatthew G. Knepley 
4109c08575a3SMatthew G. Knepley   Level: developer
4110c08575a3SMatthew G. Knepley 
411120f4b53cSBarry Smith   Note:
411220f4b53cSBarry Smith   Should normally not be called by the user, since it is set in `DMPlexCreateSubmesh()`
411320f4b53cSBarry Smith 
41141cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
4115c08575a3SMatthew G. Knepley @*/
4116d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
4117d71ae5a4SJacob Faibussowitsch {
4118e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
4119285d324eSMatthew G. Knepley   DMLabel  tmp;
4120e6ccafaeSMatthew G Knepley 
4121e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4122e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4123285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
4124e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
41259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
41269566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
41273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4128e6ccafaeSMatthew G Knepley }
4129e6ccafaeSMatthew G Knepley 
4130d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
4131d71ae5a4SJacob Faibussowitsch {
413297d8846cSMatthew Knepley   DMLabel  spmap;
413397d8846cSMatthew Knepley   PetscInt depth, d;
413497d8846cSMatthew Knepley 
413597d8846cSMatthew Knepley   PetscFunctionBegin;
41369566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
41379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
413897d8846cSMatthew Knepley   if (spmap && depth >= 0) {
413997d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
414097d8846cSMatthew Knepley     PetscInt *points, *depths;
414197d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
414297d8846cSMatthew Knepley 
41439566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
414463a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
41459566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
41469566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
414797d8846cSMatthew Knepley     depths[0] = depth;
414897d8846cSMatthew Knepley     depths[1] = 0;
4149ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
415097d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
415197d8846cSMatthew Knepley       const PetscInt dep = depths[d];
415297d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
415397d8846cSMatthew Knepley 
41549566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
41559566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
415697d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
415763a3b9bcSJacob 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);
415897d8846cSMatthew Knepley       } else {
415997d8846cSMatthew Knepley         if (!n) {
416097d8846cSMatthew Knepley           if (d == 0) {
416197d8846cSMatthew Knepley             /* Missing cells */
416297d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
416397d8846cSMatthew Knepley           } else {
416497d8846cSMatthew Knepley             /* Missing faces */
416597d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
416697d8846cSMatthew Knepley           }
416797d8846cSMatthew Knepley         }
416897d8846cSMatthew Knepley       }
416997d8846cSMatthew Knepley       if (n) {
417097d8846cSMatthew Knepley         IS              is;
417197d8846cSMatthew Knepley         const PetscInt *opoints;
417297d8846cSMatthew Knepley 
41739566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
41749566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
417597d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
41769566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
41779566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
417897d8846cSMatthew Knepley       }
417997d8846cSMatthew Knepley     }
41809566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
418163a3b9bcSJacob 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);
41829566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
41839566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
418497d8846cSMatthew Knepley   }
41853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
418697d8846cSMatthew Knepley }
418797d8846cSMatthew Knepley 
418864beef6dSMatthew G. Knepley /*@
418920f4b53cSBarry Smith   DMPlexGetSubpointIS - Returns an `IS` covering the entire subdm chart with the original points as data
4190e6ccafaeSMatthew G Knepley 
4191e6ccafaeSMatthew G Knepley   Input Parameter:
419220f4b53cSBarry Smith . dm - The submesh `DM`
4193e6ccafaeSMatthew G Knepley 
4194e6ccafaeSMatthew G Knepley   Output Parameter:
419520f4b53cSBarry Smith . subpointIS - The `IS` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
419664beef6dSMatthew G. Knepley 
419764beef6dSMatthew G. Knepley   Level: developer
419864beef6dSMatthew G. Knepley 
419920f4b53cSBarry Smith   Note:
420020f4b53cSBarry Smith   This `IS` is guaranteed to be sorted by the construction of the submesh
420120f4b53cSBarry Smith 
42021cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
420364beef6dSMatthew G. Knepley @*/
4204d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
4205d71ae5a4SJacob Faibussowitsch {
420697d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
420797d8846cSMatthew Knepley   DMLabel          spmap;
420897d8846cSMatthew Knepley   PetscObjectState state;
4209e6ccafaeSMatthew G Knepley 
4210e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4211e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42124f572ea9SToby Isaac   PetscAssertPointer(subpointIS, 2);
42139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
42149566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
42159566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
421697d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
42173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4218e6ccafaeSMatthew G Knepley }
4219559a1558SMatthew G. Knepley 
4220559a1558SMatthew G. Knepley /*@
422120f4b53cSBarry Smith   DMGetEnclosureRelation - Get the relationship between `dmA` and `dmB`
4222559a1558SMatthew G. Knepley 
4223559a1558SMatthew G. Knepley   Input Parameters:
422420f4b53cSBarry Smith + dmA - The first `DM`
422520f4b53cSBarry Smith - dmB - The second `DM`
4226559a1558SMatthew G. Knepley 
4227559a1558SMatthew G. Knepley   Output Parameter:
422820f4b53cSBarry Smith . rel - The relation of `dmA` to `dmB`
4229559a1558SMatthew G. Knepley 
4230a6e0b375SMatthew G. Knepley   Level: intermediate
4231559a1558SMatthew G. Knepley 
42321cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosurePoint()`
4233559a1558SMatthew G. Knepley @*/
4234d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4235d71ae5a4SJacob Faibussowitsch {
4236a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
4237559a1558SMatthew G. Knepley   DMLabel  spmap;
4238a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4239559a1558SMatthew G. Knepley 
424044171101SMatthew G. Knepley   PetscFunctionBegin;
42414f572ea9SToby Isaac   PetscAssertPointer(rel, 3);
4242a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
42433ba16761SJacob Faibussowitsch   if (!dmA || !dmB) PetscFunctionReturn(PETSC_SUCCESS);
4244a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4245064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
42469371c9d4SSatish Balay   if (dmA == dmB) {
42479371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
42483ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
42499371c9d4SSatish Balay   }
42509566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
42519566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
42529566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
42539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4254a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4255a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4256a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
4257a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4258a6e0b375SMatthew G. Knepley     goto end;
4259559a1558SMatthew G. Knepley   }
4260a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4261a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4262a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4263a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
42649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4265a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4266a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4267a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4268a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4269a6e0b375SMatthew G. Knepley   } else {
4270a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4271a6e0b375SMatthew G. Knepley   }
4272a6e0b375SMatthew G. Knepley end:
42739566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
42749566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
42753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4276559a1558SMatthew G. Knepley }
427744171101SMatthew G. Knepley 
427844171101SMatthew G. Knepley /*@
427920f4b53cSBarry Smith   DMGetEnclosurePoint - Get the point `pA` in `dmA` which corresponds to the point `pB` in `dmB`
428044171101SMatthew G. Knepley 
428144171101SMatthew G. Knepley   Input Parameters:
428220f4b53cSBarry Smith + dmA   - The first `DM`
428320f4b53cSBarry Smith . dmB   - The second `DM`
428420f4b53cSBarry Smith . etype - The type of enclosure relation that `dmA` has to `dmB`
428520f4b53cSBarry Smith - pB    - A point of `dmB`
428644171101SMatthew G. Knepley 
428744171101SMatthew G. Knepley   Output Parameter:
428820f4b53cSBarry Smith . pA - The corresponding point of `dmA`
428944171101SMatthew G. Knepley 
4290a6e0b375SMatthew G. Knepley   Level: intermediate
429144171101SMatthew G. Knepley 
42921cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosureRelation()`
429344171101SMatthew G. Knepley @*/
4294d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
4295d71ae5a4SJacob Faibussowitsch {
4296a6e0b375SMatthew G. Knepley   DM              sdm;
4297a6e0b375SMatthew G. Knepley   IS              subpointIS;
4298a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4299a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
430044171101SMatthew G. Knepley 
430144171101SMatthew G. Knepley   PetscFunctionBegin;
4302a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4303a6e0b375SMatthew G. Knepley   switch (etype) {
4304a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4305a6e0b375SMatthew G. Knepley     sdm = dmB;
43069566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
43079566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4308a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
43099566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4310a6e0b375SMatthew G. Knepley     break;
4311a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4312a6e0b375SMatthew G. Knepley     sdm = dmA;
43139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
43149566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
43159566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
43169566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4317a6e0b375SMatthew G. Knepley     if (*pA < 0) {
43189566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
43199566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
432063a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4321a6e0b375SMatthew G. Knepley     }
43229566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4323a6e0b375SMatthew G. Knepley     break;
4324a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
4325d71ae5a4SJacob Faibussowitsch   case DM_ENC_NONE:
4326d71ae5a4SJacob Faibussowitsch     *pA = pB;
4327d71ae5a4SJacob Faibussowitsch     break;
43289371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4329a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
433044171101SMatthew G. Knepley 
43319566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
43329566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
43339371c9d4SSatish Balay   } break;
4334d71ae5a4SJacob Faibussowitsch   default:
4335d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
433644171101SMatthew G. Knepley   }
43373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
433844171101SMatthew G. Knepley }
4339