xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 4e8208cbcbc709572b8abe32f33c78b69c819375)
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 
DMPlexCellIsHybrid_Internal(DM dm,PetscInt p,PetscBool * isHybrid)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 
DMPlexGetTensorPrismBounds_Internal(DM dm,PetscInt dim,PetscInt * cStart,PetscInt * cEnd)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 
DMPlexMarkBoundaryFaces_Internal(DM dm,PetscInt val,PetscInt cellHeight,DMLabel label,PetscBool missing_only)5069941377SStefano Zampini PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label, PetscBool missing_only)
51d71ae5a4SJacob Faibussowitsch {
5269941377SStefano Zampini   PetscInt           depth, pStart, pEnd, fStart, fEnd, f, supportSize, nroots = -1, nleaves = -1, defval;
53429fa399SMatthew G. Knepley   PetscSF            sf;
5441e3e744Sksagiyam   const PetscSFNode *iremote = NULL;
5541e3e744Sksagiyam   const PetscInt    *ilocal  = NULL;
5641e3e744Sksagiyam   PetscInt          *leafData;
5730560a7bSMatthew G. Knepley 
5830560a7bSMatthew G. Knepley   PetscFunctionBegin;
5941e3e744Sksagiyam   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
6041e3e744Sksagiyam   PetscCall(DMPlexGetDepth(dm, &depth));
6141e3e744Sksagiyam   if (depth >= cellHeight + 1) {
629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, cellHeight + 1, &fStart, &fEnd));
6341e3e744Sksagiyam   } else {
6441e3e744Sksagiyam     /* Note DMPlexGetHeightStratum() returns fStart, fEnd = pStart, pEnd */
6541e3e744Sksagiyam     /* if height > depth, which is not what we want here.                */
6641e3e744Sksagiyam     fStart = 0;
6741e3e744Sksagiyam     fEnd   = 0;
6841e3e744Sksagiyam   }
6969941377SStefano Zampini   PetscCall(DMLabelGetDefaultValue(label, &defval));
7041e3e744Sksagiyam   PetscCall(PetscCalloc1(pEnd - pStart, &leafData));
718e3a54c0SPierre Jolivet   leafData = PetscSafePointerPlusOffset(leafData, -pStart);
7241e3e744Sksagiyam   PetscCall(DMGetPointSF(dm, &sf));
7341e3e744Sksagiyam   if (sf) PetscCall(PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, &iremote));
7441e3e744Sksagiyam   if (sf && nroots >= 0) {
7541e3e744Sksagiyam     PetscInt        cStart, cEnd, c, i;
7641e3e744Sksagiyam     PetscInt       *rootData, *rootData1, *cellOwners, hasTwoSupportCells = -2;
7741e3e744Sksagiyam     const PetscInt *support;
7841e3e744Sksagiyam     PetscMPIInt     rank;
7930560a7bSMatthew G. Knepley 
8041e3e744Sksagiyam     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8141e3e744Sksagiyam     PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8241e3e744Sksagiyam     PetscCall(PetscCalloc3(pEnd - pStart, &rootData, pEnd - pStart, &rootData1, cEnd - cStart, &cellOwners));
8341e3e744Sksagiyam     rootData -= pStart;
8441e3e744Sksagiyam     rootData1 -= pStart;
85835f2295SStefano Zampini     for (c = cStart; c < cEnd; ++c) cellOwners[c - cStart] = rank;
8641e3e744Sksagiyam     for (i = 0; i < nleaves; ++i) {
8741e3e744Sksagiyam       c = ilocal ? ilocal[i] : i;
8841e3e744Sksagiyam       if (c >= cStart && c < cEnd) cellOwners[c - cStart] = iremote[i].rank;
8941e3e744Sksagiyam     }
9041e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
92e752be1aSMatthew G. Knepley       if (supportSize == 1) {
9341e3e744Sksagiyam         PetscCall(DMPlexGetSupport(dm, f, &support));
9441e3e744Sksagiyam         leafData[f] = cellOwners[support[0] - cStart];
9541e3e744Sksagiyam       } else {
9641e3e744Sksagiyam         /* TODO: When using DMForest, we could have a parent facet (a coarse facet)     */
9741e3e744Sksagiyam         /*       supportSize of which alone does not tell us if it is an interior       */
9841e3e744Sksagiyam         /*       facet or an exterior facet. Those facets can be identified by checking */
9941e3e744Sksagiyam         /*       if they are in the parent tree. We should probably skip those parent   */
10041e3e744Sksagiyam         /*       facets here, which will allow for including the following check, and   */
10141e3e744Sksagiyam         /*       see if they are exterior facets or not afterwards by checking if the   */
10241e3e744Sksagiyam         /*       children are exterior or not.                                          */
10341e3e744Sksagiyam         /* PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", supportSize, f); */
10441e3e744Sksagiyam         leafData[f] = hasTwoSupportCells; /* some negative PetscInt */
105429fa399SMatthew G. Knepley       }
10641e3e744Sksagiyam       rootData[f]  = leafData[f];
10741e3e744Sksagiyam       rootData1[f] = leafData[f];
10841e3e744Sksagiyam     }
10941e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData, MPI_MIN));
11041e3e744Sksagiyam     PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
11141e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData, MPI_MIN));
11241e3e744Sksagiyam     PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData1, MPI_MAX));
11341e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
11441e3e744Sksagiyam       /* Store global support size of f.                                                    */
11541e3e744Sksagiyam       /* Facet f is an interior facet if and only if one of the following two is satisfied: */
11641e3e744Sksagiyam       /* 1. supportSize is 2 on some rank.                                                  */
11741e3e744Sksagiyam       /* 2. supportSize is 1 on any rank that can see f, but f is on a partition boundary;  */
11841e3e744Sksagiyam       /*    i.e., rootData[f] < rootData1[f].                                               */
11941e3e744Sksagiyam       rootData[f] = (rootData[f] == hasTwoSupportCells || (rootData[f] < rootData1[f])) ? 2 : 1;
12041e3e744Sksagiyam       leafData[f] = rootData[f];
12141e3e744Sksagiyam     }
12241e3e744Sksagiyam     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
12341e3e744Sksagiyam     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, rootData, leafData, MPI_REPLACE));
12441e3e744Sksagiyam     rootData += pStart;
12541e3e744Sksagiyam     rootData1 += pStart;
12641e3e744Sksagiyam     PetscCall(PetscFree3(rootData, rootData1, cellOwners));
12741e3e744Sksagiyam   } else {
12841e3e744Sksagiyam     for (f = fStart; f < fEnd; ++f) {
12941e3e744Sksagiyam       PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
13041e3e744Sksagiyam       leafData[f] = supportSize;
13141e3e744Sksagiyam     }
13241e3e744Sksagiyam   }
13341e3e744Sksagiyam   for (f = fStart; f < fEnd; ++f) {
13441e3e744Sksagiyam     if (leafData[f] == 1) {
135e752be1aSMatthew G. Knepley       if (val < 0) {
136e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
137e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
138e752be1aSMatthew G. Knepley 
13969941377SStefano Zampini         PetscAssert(!missing_only, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented");
1409566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
141e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize * 2; cl += 2) {
1429566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, closure[cl], &cval));
143e752be1aSMatthew G. Knepley           if (cval < 0) continue;
1449566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, cval));
145e752be1aSMatthew G. Knepley           break;
146e752be1aSMatthew G. Knepley         }
1479566063dSJacob Faibussowitsch         if (cl == clSize * 2) PetscCall(DMLabelSetValue(label, f, 1));
1489566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
149e752be1aSMatthew G. Knepley       } else {
15069941377SStefano Zampini         if (missing_only) {
15169941377SStefano Zampini           PetscInt fval;
15269941377SStefano Zampini           PetscCall(DMLabelGetValue(label, f, &fval));
15369941377SStefano Zampini           if (fval != defval) PetscCall(DMLabelClearValue(label, f, fval));
15469941377SStefano Zampini           else PetscCall(DMLabelSetValue(label, f, val));
15569941377SStefano Zampini         } else {
1569566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, val));
157e752be1aSMatthew G. Knepley         }
15869941377SStefano Zampini       }
15941e3e744Sksagiyam     } else {
16041e3e744Sksagiyam       /* TODO: See the above comment on DMForest */
16141e3e744Sksagiyam       /* PetscCheck(leafData[f] == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", leafData[f], f); */
162e752be1aSMatthew G. Knepley     }
16330560a7bSMatthew G. Knepley   }
1648e3a54c0SPierre Jolivet   leafData = PetscSafePointerPlusOffset(leafData, pStart);
16541e3e744Sksagiyam   PetscCall(PetscFree(leafData));
1663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16730560a7bSMatthew G. Knepley }
16830560a7bSMatthew G. Knepley 
169cd0c2139SMatthew G Knepley /*@
170cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
171cd0c2139SMatthew G Knepley 
17241e3e744Sksagiyam   Collective
173cd0c2139SMatthew G Knepley 
174d8d19677SJose E. Roman   Input Parameters:
175429fa399SMatthew G. Knepley + dm  - The original `DM`
17620f4b53cSBarry Smith - val - The marker value, or `PETSC_DETERMINE` to use some value in the closure (or 1 if none are found)
177cd0c2139SMatthew G Knepley 
178cd0c2139SMatthew G Knepley   Output Parameter:
17920f4b53cSBarry Smith . label - The `DMLabel` marking boundary faces with the given value
180429fa399SMatthew G. Knepley 
181cd0c2139SMatthew G Knepley   Level: developer
182cd0c2139SMatthew G Knepley 
18320f4b53cSBarry Smith   Note:
18441e3e744Sksagiyam   This function will use the point `PetscSF` from the input `DM` and the ownership of the support cells to exclude points on the partition boundary from being marked. If you also wish to mark the partition boundary, you can use `DMSetPointSF()` to temporarily set it to `NULL`, and then reset it to the original object after the call.
18541e3e744Sksagiyam 
18641e3e744Sksagiyam   In DMForest there can be facets support sizes of which alone can not determine whether they are on the boundary. Currently, this function is not guaranteed to produce the correct result in such case.
18720f4b53cSBarry Smith 
1881cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLabelCreate()`, `DMCreateLabel()`
18909f723d9SJed Brown @*/
DMPlexMarkBoundaryFaces(DM dm,PetscInt val,DMLabel label)190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
191d71ae5a4SJacob Faibussowitsch {
192bbb57e68SMatthew G. Knepley   DM                     plex;
193827c4036SVaclav Hapla   DMPlexInterpolatedFlag flg;
194cd0c2139SMatthew G Knepley 
195cd0c2139SMatthew G Knepley   PetscFunctionBegin;
196827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
197bbb57e68SMatthew G. Knepley   PetscCall(DMConvert(dm, DMPLEX, &plex));
198bbb57e68SMatthew G. Knepley   PetscCall(DMPlexIsInterpolated(plex, &flg));
19908401ef6SPierre Jolivet   PetscCheck(flg == DMPLEX_INTERPOLATED_FULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
200bbb57e68SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces_Internal(plex, val, 0, label, PETSC_FALSE));
201bbb57e68SMatthew G. Knepley   PetscCall(DMDestroy(&plex));
2023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
203cd0c2139SMatthew G Knepley }
204cd0c2139SMatthew G Knepley 
DMPlexLabelComplete_Internal(DM dm,DMLabel label,PetscBool completeCells)205d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
206d71ae5a4SJacob Faibussowitsch {
207b0bf5782SToby Isaac   IS              valueIS;
208ac51f24eSSander Arens   PetscSF         sfPoint;
209b0bf5782SToby Isaac   const PetscInt *values;
210ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
211b0bf5782SToby Isaac 
212b0bf5782SToby Isaac   PetscFunctionBegin;
2139566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2149566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2169566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
217b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
218b0bf5782SToby Isaac     IS              pointIS;
219b0bf5782SToby Isaac     const PetscInt *points;
220b0bf5782SToby Isaac     PetscInt        numPoints, p;
221b0bf5782SToby Isaac 
2229566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2239566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2249566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
225b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
226b0bf5782SToby Isaac       PetscInt  q       = points[p];
227b0bf5782SToby Isaac       PetscInt *closure = NULL;
228b0bf5782SToby Isaac       PetscInt  closureSize, c;
229b0bf5782SToby Isaac 
230b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
231b0bf5782SToby Isaac         continue;
232b0bf5782SToby Isaac       }
2339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
23448a46eb9SPierre Jolivet       for (c = 0; c < closureSize * 2; c += 2) PetscCall(DMLabelSetValue(label, closure[c], values[v]));
2359566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
236b0bf5782SToby Isaac     }
2379566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2389566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
239b0bf5782SToby Isaac   }
2409566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2419566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2429566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
2439566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
244ac51f24eSSander Arens   if (nroots >= 0) {
24526279d81SSanderA     DMLabel         lblRoots, lblLeaves;
24626279d81SSanderA     IS              valueIS, pointIS;
24726279d81SSanderA     const PetscInt *values;
24826279d81SSanderA     PetscInt        numValues, v;
24926279d81SSanderA 
25026279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
2519566063dSJacob Faibussowitsch     PetscCall(DMLabelGather(label, sfPoint, &lblLeaves));
2529566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS));
2539566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2549566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
25526279d81SSanderA     for (v = 0; v < numValues; ++v) {
25626279d81SSanderA       const PetscInt value = values[v];
25726279d81SSanderA 
2589566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
2599566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2609566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
26126279d81SSanderA     }
2629566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2639566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2649566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblLeaves));
26526279d81SSanderA     /* Push point contributions from roots into remote leaves */
2669566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots));
2679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblRoots, &valueIS));
2689566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
2699566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
27026279d81SSanderA     for (v = 0; v < numValues; ++v) {
27126279d81SSanderA       const PetscInt value = values[v];
27226279d81SSanderA 
2739566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS));
2749566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2759566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
27626279d81SSanderA     }
2779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2799566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblRoots));
28026279d81SSanderA   }
2813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
282b0bf5782SToby Isaac }
283b0bf5782SToby Isaac 
2842be2b188SMatthew G Knepley /*@
28571f1c950SStefano Zampini   DMPlexLabelComplete - Starting with a label marking points, we add their transitive closure
2862be2b188SMatthew G Knepley 
2872be2b188SMatthew G Knepley   Input Parameters:
28820f4b53cSBarry Smith + dm    - The `DM`
28971f1c950SStefano Zampini - label - A `DMLabel` marking the points
2902be2b188SMatthew G Knepley 
2912be2b188SMatthew G Knepley   Output Parameter:
29271f1c950SStefano Zampini . label - A `DMLabel` marking all points in the transitive closure
2932be2b188SMatthew G Knepley 
2942be2b188SMatthew G Knepley   Level: developer
2952be2b188SMatthew G Knepley 
2961cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelCohesiveComplete()`
2972be2b188SMatthew G Knepley @*/
DMPlexLabelComplete(DM dm,DMLabel label)298d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
299d71ae5a4SJacob Faibussowitsch {
3002be2b188SMatthew G Knepley   PetscFunctionBegin;
3019566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
3023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3032be2b188SMatthew G Knepley }
3042be2b188SMatthew G Knepley 
3056cf0e42fSMatthew G. Knepley /*@
306a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
3076cf0e42fSMatthew G. Knepley 
3086cf0e42fSMatthew G. Knepley   Input Parameters:
30920f4b53cSBarry Smith + dm    - The `DM`
31020f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
3116cf0e42fSMatthew G. Knepley 
3126cf0e42fSMatthew G. Knepley   Output Parameter:
31320f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
3146cf0e42fSMatthew G. Knepley 
3156cf0e42fSMatthew G. Knepley   Level: developer
3166cf0e42fSMatthew G. Knepley 
31720f4b53cSBarry Smith   Note:
31820f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
3196cf0e42fSMatthew G. Knepley 
3201cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
3216cf0e42fSMatthew G. Knepley @*/
DMPlexLabelAddCells(DM dm,DMLabel label)322d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
323d71ae5a4SJacob Faibussowitsch {
3246cf0e42fSMatthew G. Knepley   IS              valueIS;
3256cf0e42fSMatthew G. Knepley   const PetscInt *values;
3269df54392SMatthew G. Knepley   PetscInt        numValues, v, csStart, csEnd, chStart, chEnd;
3276cf0e42fSMatthew G. Knepley 
3286cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
3299df54392SMatthew G. Knepley   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &csStart, &csEnd));
3309df54392SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(dm, 0, &chStart, &chEnd));
3319566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3329566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3339566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
3346cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
3356cf0e42fSMatthew G. Knepley     IS              pointIS;
3366cf0e42fSMatthew G. Knepley     const PetscInt *points;
3376cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
3386cf0e42fSMatthew G. Knepley 
3399566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3409566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
3426cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3439df54392SMatthew G. Knepley       const PetscInt point   = points[p];
3446cf0e42fSMatthew G. Knepley       PetscInt      *closure = NULL;
3457bd9760aSJed Brown       PetscInt       closureSize, cl, h, cStart, cEnd;
3467bd9760aSJed Brown       DMPolytopeType ct;
3479df54392SMatthew G. Knepley 
3489df54392SMatthew G. Knepley       // If the point is a hybrid, allow hybrid cells
3497bd9760aSJed Brown       PetscCall(DMPlexGetCellType(dm, point, &ct));
3509df54392SMatthew G. Knepley       PetscCall(DMPlexGetPointHeight(dm, point, &h));
3517bd9760aSJed Brown       if (DMPolytopeTypeIsHybrid(ct)) {
3529df54392SMatthew G. Knepley         cStart = chStart;
3539df54392SMatthew G. Knepley         cEnd   = chEnd;
3547bd9760aSJed Brown       } else {
3557bd9760aSJed Brown         cStart = csStart;
3567bd9760aSJed Brown         cEnd   = csEnd;
3579df54392SMatthew G. Knepley       }
3586cf0e42fSMatthew G. Knepley 
3599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
36022eabd52SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
361a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
362b6555650SPierre Jolivet         if (cell >= cStart && cell < cEnd) {
3639371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
3649371c9d4SSatish Balay           break;
3659371c9d4SSatish Balay         }
36622eabd52SMatthew G. Knepley       }
3679566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
3686cf0e42fSMatthew G. Knepley     }
3699566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3709566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3716cf0e42fSMatthew G. Knepley   }
3729566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3739566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
3743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3756cf0e42fSMatthew G. Knepley }
3766cf0e42fSMatthew G. Knepley 
377f402d5e4SToby Isaac /*@
378a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
379a6e0b375SMatthew G. Knepley 
380a6e0b375SMatthew G. Knepley   Input Parameters:
38120f4b53cSBarry Smith + dm    - The `DM`
38220f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
383a6e0b375SMatthew G. Knepley 
384a6e0b375SMatthew G. Knepley   Output Parameter:
38520f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
386a6e0b375SMatthew G. Knepley 
387a6e0b375SMatthew G. Knepley   Level: developer
388a6e0b375SMatthew G. Knepley 
38920f4b53cSBarry Smith   Note:
39020f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
391a6e0b375SMatthew G. Knepley 
3921cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
393a6e0b375SMatthew G. Knepley @*/
DMPlexLabelAddFaceCells(DM dm,DMLabel label)394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
395d71ae5a4SJacob Faibussowitsch {
396a6e0b375SMatthew G. Knepley   IS              valueIS;
397a6e0b375SMatthew G. Knepley   const PetscInt *values;
398a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
399a6e0b375SMatthew G. Knepley 
400a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
4019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
4039566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4049566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4059566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
406a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
407a6e0b375SMatthew G. Knepley     IS              pointIS;
408a6e0b375SMatthew G. Knepley     const PetscInt *points;
409a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
410a6e0b375SMatthew G. Knepley 
4119566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4129566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4139566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
414a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
415a6e0b375SMatthew G. Knepley       const PetscInt face    = points[p];
416a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
417a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
418a6e0b375SMatthew G. Knepley 
419a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
4209566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
421a6e0b375SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
422a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
423b6555650SPierre Jolivet         if (cell >= cStart && cell < cEnd) {
4249371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
4259371c9d4SSatish Balay           break;
4269371c9d4SSatish Balay         }
427a6e0b375SMatthew G. Knepley       }
4289566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
429a6e0b375SMatthew G. Knepley     }
4309566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4319566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
432a6e0b375SMatthew G. Knepley   }
4339566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4349566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
436a6e0b375SMatthew G. Knepley }
437a6e0b375SMatthew G. Knepley 
438a6e0b375SMatthew G. Knepley /*@
439f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
440f402d5e4SToby Isaac 
441f402d5e4SToby Isaac   Input Parameters:
44220f4b53cSBarry Smith + dm    - The `DM`
44320f4b53cSBarry Smith - label - A `DMLabel` marking surface points and their adjacent cells
444f402d5e4SToby Isaac 
445f402d5e4SToby Isaac   Output Parameter:
44620f4b53cSBarry Smith . label - A `DMLabel` without cells
447f402d5e4SToby Isaac 
448f402d5e4SToby Isaac   Level: developer
449f402d5e4SToby Isaac 
45020f4b53cSBarry Smith   Note:
45120f4b53cSBarry Smith   This undoes `DMPlexLabelAddCells()` or `DMPlexLabelAddFaceCells()`
452f402d5e4SToby Isaac 
4531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()`
454f402d5e4SToby Isaac @*/
DMPlexLabelClearCells(DM dm,DMLabel label)455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
456d71ae5a4SJacob Faibussowitsch {
457f402d5e4SToby Isaac   IS              valueIS;
458f402d5e4SToby Isaac   const PetscInt *values;
459485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
460f402d5e4SToby Isaac 
461f402d5e4SToby Isaac   PetscFunctionBegin;
4629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
4639566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
4649566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
4659566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
466f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
467f402d5e4SToby Isaac     IS              pointIS;
468f402d5e4SToby Isaac     const PetscInt *points;
469f402d5e4SToby Isaac     PetscInt        numPoints, p;
470f402d5e4SToby Isaac 
4719566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
4729566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
4739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
474f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
475f402d5e4SToby Isaac       PetscInt point = points[p];
476f402d5e4SToby Isaac 
47748a46eb9SPierre Jolivet       if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v]));
478f402d5e4SToby Isaac     }
4799566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4809566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
481f402d5e4SToby Isaac   }
4829566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4839566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
485f402d5e4SToby Isaac }
486f402d5e4SToby Isaac 
48759eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
48859eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
DMPlexShiftPointSetUp_Internal(PetscInt depth,PetscInt depthShift[])489d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
490d71ae5a4SJacob Faibussowitsch {
4912582d50cSToby Isaac   PetscInt d, off = 0;
4922582d50cSToby Isaac 
4932582d50cSToby Isaac   PetscFunctionBegin;
49459eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
4950974a383SToby Isaac   for (d = 0; d < depth; d++) {
4962582d50cSToby Isaac     PetscInt firstd     = d;
4970974a383SToby Isaac     PetscInt firstStart = depthShift[2 * d];
4982582d50cSToby Isaac     PetscInt e;
4992582d50cSToby Isaac 
5002582d50cSToby Isaac     for (e = d + 1; e <= depth; e++) {
5012582d50cSToby Isaac       if (depthShift[2 * e] < firstStart) {
5022582d50cSToby Isaac         firstd     = e;
5032582d50cSToby Isaac         firstStart = depthShift[2 * d];
5042582d50cSToby Isaac       }
5052582d50cSToby Isaac     }
5062582d50cSToby Isaac     if (firstd != d) {
5072582d50cSToby Isaac       PetscInt swap[2];
5082582d50cSToby Isaac 
5092582d50cSToby Isaac       e                     = firstd;
5102582d50cSToby Isaac       swap[0]               = depthShift[2 * d];
5112582d50cSToby Isaac       swap[1]               = depthShift[2 * d + 1];
5122582d50cSToby Isaac       depthShift[2 * d]     = depthShift[2 * e];
5132582d50cSToby Isaac       depthShift[2 * d + 1] = depthShift[2 * e + 1];
5142582d50cSToby Isaac       depthShift[2 * e]     = swap[0];
5152582d50cSToby Isaac       depthShift[2 * e + 1] = swap[1];
5162582d50cSToby Isaac     }
5172582d50cSToby Isaac   }
5182582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
5190974a383SToby Isaac   for (d = 0; d <= depth; d++) {
5202582d50cSToby Isaac     off += depthShift[2 * d + 1];
52159eef20bSToby Isaac     depthShift[2 * d + 1] = depthShift[2 * d] + off;
5222582d50cSToby Isaac   }
5233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5242582d50cSToby Isaac }
5252582d50cSToby Isaac 
5262582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
DMPlexShiftPoint_Internal(PetscInt p,PetscInt depth,PetscInt depthShift[])527d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
528d71ae5a4SJacob Faibussowitsch {
5292582d50cSToby Isaac   PetscInt d;
5302582d50cSToby Isaac   PetscInt newOff = 0;
5312582d50cSToby Isaac 
5322582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5332582d50cSToby Isaac     if (p < depthShift[2 * d]) return p + newOff;
5342582d50cSToby Isaac     else newOff = depthShift[2 * d + 1] - depthShift[2 * d];
5352582d50cSToby Isaac   }
5360974a383SToby Isaac   return p + newOff;
5372582d50cSToby Isaac }
5382582d50cSToby Isaac 
5392582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
DMPlexShiftPointInverse_Internal(PetscInt p,PetscInt depth,PetscInt depthShift[])540d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
541d71ae5a4SJacob Faibussowitsch {
5422582d50cSToby Isaac   PetscInt d;
5432582d50cSToby Isaac   PetscInt newOff = 0;
5442582d50cSToby Isaac 
5452582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
5462582d50cSToby Isaac     if (p < depthShift[2 * d + 1]) return p + newOff;
5472582d50cSToby Isaac     else newOff = depthShift[2 * d] - depthShift[2 * d + 1];
5482582d50cSToby Isaac   }
5490974a383SToby Isaac   return p + newOff;
550cd0c2139SMatthew G Knepley }
551cd0c2139SMatthew G Knepley 
DMPlexShiftSizes_Internal(DM dm,PetscInt depthShift[],DM dmNew)552d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
553d71ae5a4SJacob Faibussowitsch {
554cd0c2139SMatthew G Knepley   PetscInt depth = 0, d, pStart, pEnd, p;
555fa8e8ae5SToby Isaac   DMLabel  depthLabel;
556cd0c2139SMatthew G Knepley 
557cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5593ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
560cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
5619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5620974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5639566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
5649566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "depth"));
5659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel));
5669566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
567cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
568cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
569fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
570fa8e8ae5SToby Isaac     IS       pIS;
571fa8e8ae5SToby Isaac 
5729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
573fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
574fa8e8ae5SToby Isaac     pEndNew   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5759566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
5769566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
5779566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
578cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
5792582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
580cd0c2139SMatthew G Knepley       PetscInt       size;
581412e9a14SMatthew G. Knepley       DMPolytopeType ct;
582cd0c2139SMatthew G Knepley 
5839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
5849566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
5859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
5869566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
5879566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
5889566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
589cd0c2139SMatthew G Knepley     }
590cd0c2139SMatthew G Knepley   }
5913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
592cd0c2139SMatthew G Knepley }
593cd0c2139SMatthew G Knepley 
DMPlexShiftPoints_Internal(DM dm,PetscInt depthShift[],DM dmNew)594d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
595d71ae5a4SJacob Faibussowitsch {
5962582d50cSToby Isaac   PetscInt *newpoints;
5972582d50cSToby Isaac   PetscInt  depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
598cd0c2139SMatthew G Knepley 
599cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
6013ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
6029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
6039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
6049566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints));
605cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
6069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
607cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
608cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
6092582d50cSToby Isaac     PetscInt        size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
610cd0c2139SMatthew G Knepley 
6119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
6129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
6139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
614ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
6159566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
6169566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
6179566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
6189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
6199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
620ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
621dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
6229566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
623cd0c2139SMatthew G Knepley   }
6249566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
6253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
626cd0c2139SMatthew G Knepley }
627cd0c2139SMatthew G Knepley 
DMPlexShiftCoordinates_Internal(DM dm,PetscInt depthShift[],DM dmNew)628d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
629d71ae5a4SJacob Faibussowitsch {
630cd0c2139SMatthew G Knepley   PetscSection coordSection, newCoordSection;
631cd0c2139SMatthew G Knepley   Vec          coordinates, newCoordinates;
632cd0c2139SMatthew G Knepley   PetscScalar *coords, *newCoords;
633f2b8cce1SMatthew G. Knepley   PetscInt     coordSize, sStart, sEnd;
634f2b8cce1SMatthew G. Knepley   PetscInt     dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
635f2b8cce1SMatthew G. Knepley   PetscBool    hasCells;
636cd0c2139SMatthew G Knepley 
637cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
6399566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
6409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
641cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
6429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
6439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
6449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
6459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
6469566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6479566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
6489566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
6499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
6509566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
651f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
6529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
653f2b8cce1SMatthew G. Knepley   if (hasCells) {
654f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
655f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
656f2b8cce1SMatthew G. Knepley 
6579566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6589566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
6599566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
660f2b8cce1SMatthew G. Knepley     }
661f2b8cce1SMatthew G. Knepley   }
662cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
6639566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
6649566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
665cd0c2139SMatthew G Knepley   }
6669566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
6679566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
6689566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
6699566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
6709566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates"));
6719566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
6729566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
6739566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates, VECSTANDARD));
6749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
6759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
6769566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6779566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
678f2b8cce1SMatthew G. Knepley   if (hasCells) {
679f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
680f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
681f2b8cce1SMatthew G. Knepley 
6829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
6849566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
685f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
686f2b8cce1SMatthew G. Knepley     }
687f2b8cce1SMatthew G. Knepley   }
688cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
689cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
690cd0c2139SMatthew G Knepley 
6919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
6929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
6939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
694f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
695cd0c2139SMatthew G Knepley   }
6969566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6979566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
6989566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
6999566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
7003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
701cd0c2139SMatthew G Knepley }
702cd0c2139SMatthew G Knepley 
DMPlexShiftSF_Single(DM dm,PetscInt depthShift[],PetscSF sf,PetscSF sfNew)703d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
704d71ae5a4SJacob Faibussowitsch {
705cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
706cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
707cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
708cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
7090e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
710cd0c2139SMatthew G Knepley 
711cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
7139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
7149566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
7156e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
716cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
7179566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation));
7180e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
7199566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7209566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
7219566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
7229566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
723cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
7242582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
725cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
726cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
727cd0c2139SMatthew G Knepley     }
7289566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
7299566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
7300e33faafSMatthew G. Knepley   }
7313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7320e33faafSMatthew G. Knepley }
7330e33faafSMatthew G. Knepley 
DMPlexShiftSF_Internal(DM dm,PetscInt depthShift[],DM dmNew)734d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
735d71ae5a4SJacob Faibussowitsch {
7360e33faafSMatthew G. Knepley   PetscSF   sfPoint, sfPointNew;
7370e33faafSMatthew G. Knepley   PetscBool useNatural;
7380e33faafSMatthew G. Knepley 
7390e33faafSMatthew G. Knepley   PetscFunctionBegin;
7400e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
7419566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
7429566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
7439566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
7440e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
7459566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
7460e33faafSMatthew G. Knepley   if (useNatural) {
7470e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
7480e33faafSMatthew G. Knepley 
7499566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
7509566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
7519566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
7529566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
753cd0c2139SMatthew G Knepley   }
7543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
755cd0c2139SMatthew G Knepley }
756cd0c2139SMatthew G Knepley 
DMPlexShiftLabels_Internal(DM dm,PetscInt depthShift[],DM dmNew)757d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
758d71ae5a4SJacob Faibussowitsch {
759d56405f8SMatthew G. Knepley   PetscInt depth = 0, numLabels, l;
760cd0c2139SMatthew G Knepley 
761cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
763cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
7649566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
765cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
766cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
767cd0c2139SMatthew G Knepley     const char     *lname;
768fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
769cd0c2139SMatthew G Knepley     IS              valueIS;
770cd0c2139SMatthew G Knepley     const PetscInt *values;
771cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
772cd0c2139SMatthew G Knepley 
7739566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
7749566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
775cd0c2139SMatthew G Knepley     if (isDepth) continue;
7769566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
777fa8e8ae5SToby Isaac     if (isDim) continue;
7789566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
7799566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
7809566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
7819566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &val));
7829566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, val));
7839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
7849566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
7859566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
786cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
787cd0c2139SMatthew G Knepley       IS              pointIS;
788cd0c2139SMatthew G Knepley       const PetscInt *points;
789cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
790cd0c2139SMatthew G Knepley 
7919566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
7929566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
7939566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
794cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
7952582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
796cd0c2139SMatthew G Knepley 
7979566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
798cd0c2139SMatthew G Knepley       }
7999566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
8009566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
801cd0c2139SMatthew G Knepley     }
8029566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
8039566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
804cd0c2139SMatthew G Knepley   }
8053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
806d56405f8SMatthew G. Knepley }
807d56405f8SMatthew G. Knepley 
DMPlexCreateVTKLabel_Internal(DM dm,PetscBool createGhostLabel,DM dmNew)808d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
809d71ae5a4SJacob Faibussowitsch {
810d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
811d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
812d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
813d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
814d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
815d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
816d56405f8SMatthew G. Knepley 
817d56405f8SMatthew G. Knepley   PetscFunctionBegin;
818cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
8199566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8209566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
8219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
8229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
8239566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
8249566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
8259566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
826d56405f8SMatthew G. Knepley   if (createGhostLabel) {
8279566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
8289566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
829d56405f8SMatthew G. Knepley   }
830cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
83148a46eb9SPierre Jolivet     for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
832cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
833ac530a7eSPierre Jolivet     if (leafRemote[l].rank == rank) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
834ac530a7eSPierre Jolivet     else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
835cd0c2139SMatthew G Knepley   }
83648a46eb9SPierre Jolivet   for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
837d56405f8SMatthew G. Knepley   if (ghostLabel) {
8389566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
839cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
840cd0c2139SMatthew G Knepley       PetscInt numCells;
841cd0c2139SMatthew G Knepley 
8429566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
843cd0c2139SMatthew G Knepley       if (numCells < 2) {
8449566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
845cd0c2139SMatthew G Knepley       } else {
846cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
847cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
848cd0c2139SMatthew G Knepley 
8499566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
8509566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
8519566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
8529566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
853cd0c2139SMatthew G Knepley       }
854cd0c2139SMatthew G Knepley     }
855d56405f8SMatthew G. Knepley   }
8563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
857cd0c2139SMatthew G Knepley }
858cd0c2139SMatthew G Knepley 
DMPlexShiftTree_Internal(DM dm,PetscInt depthShift[],DM dmNew)859d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
860d71ae5a4SJacob Faibussowitsch {
861ca04dac2SToby Isaac   DM           refTree;
862ca04dac2SToby Isaac   PetscSection pSec;
863ca04dac2SToby Isaac   PetscInt    *parents, *childIDs;
864ca04dac2SToby Isaac 
865ca04dac2SToby Isaac   PetscFunctionBegin;
8669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
8679566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew, refTree));
8689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL));
869ca04dac2SToby Isaac   if (pSec) {
8702582d50cSToby Isaac     PetscInt     p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
871fb4630b5SToby Isaac     PetscInt    *childIDsShifted;
872ca04dac2SToby Isaac     PetscSection pSecShifted;
873ca04dac2SToby Isaac 
8749566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd));
8759566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
8762582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
8772582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
8789566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted));
8799566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted));
8809566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted));
881ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
882fb4630b5SToby Isaac       /* start off assuming no children */
8839566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, p, 0));
884fb4630b5SToby Isaac     }
885fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
886fb4630b5SToby Isaac       PetscInt dof;
887fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
888ca04dac2SToby Isaac 
8899566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
8909566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof));
891ca04dac2SToby Isaac     }
8929566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
893fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
894fb4630b5SToby Isaac       PetscInt dof;
895fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
896fb4630b5SToby Isaac 
8979566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
898fb4630b5SToby Isaac       if (dof) {
899fb4630b5SToby Isaac         PetscInt off, offNew;
900fb4630b5SToby Isaac 
9019566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec, p, &off));
9029566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew));
903fb4630b5SToby Isaac         parentsShifted[offNew]  = DMPlexShiftPoint_Internal(parents[off], depth, depthShift);
904fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
905fb4630b5SToby Isaac       }
906fb4630b5SToby Isaac     }
9079566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted));
9089566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted, childIDsShifted));
9099566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
910ca04dac2SToby Isaac   }
9113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
912ca04dac2SToby Isaac }
913ca04dac2SToby Isaac 
DMPlexConstructGhostCells_Internal(DM dm,DMLabel label,PetscInt * numGhostCells,DM gdm)914d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
915d71ae5a4SJacob Faibussowitsch {
916da97024aSMatthew G. Knepley   PetscSF          sf;
917cd0c2139SMatthew G Knepley   IS               valueIS;
918da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
919cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
9202582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
9214fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
922cd0c2139SMatthew G Knepley 
923cd0c2139SMatthew G Knepley   PetscFunctionBegin;
9249566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
9259566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
926da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
9279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
928cd0c2139SMatthew G Knepley   /* Count ghost cells */
9299566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
9309566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
9319566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
9324a6cfa73SMatthew G. Knepley   Ng = 0;
933cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
93446c796b9SMatthew G. Knepley     IS              faceIS;
93546c796b9SMatthew G. Knepley     const PetscInt *faces;
93646c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
937cd0c2139SMatthew G Knepley 
9389566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9399566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9409566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
94146c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
942ca04dac2SToby Isaac       PetscInt numChildren;
943ca04dac2SToby Isaac 
9449566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9459566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
946ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
947ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
948b6555650SPierre Jolivet       if (faces[f] >= fStart && faces[f] < fEnd) ++numBdFaces;
94946c796b9SMatthew G. Knepley     }
9504a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
9519566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9529566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
953cd0c2139SMatthew G Knepley   }
9549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
9559566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift));
9562582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
95759eef20bSToby Isaac     PetscInt dEnd;
9582582d50cSToby Isaac 
9599566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd));
96059eef20bSToby Isaac     depthShift[2 * d]     = dEnd;
9612582d50cSToby Isaac     depthShift[2 * d + 1] = 0;
9622582d50cSToby Isaac   }
9632582d50cSToby Isaac   if (depth >= 0) depthShift[2 * depth + 1] = Ng;
9649566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
9659566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
966cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
9679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
96848a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1));
969cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
970cd0c2139SMatthew G Knepley     IS              faceIS;
971cd0c2139SMatthew G Knepley     const PetscInt *faces;
972cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
973cd0c2139SMatthew G Knepley 
9749566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9759566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9769566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
977cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
978ca04dac2SToby Isaac       PetscInt size, numChildren;
979cd0c2139SMatthew G Knepley 
9809566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9819566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
982ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
98346c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
98563a3b9bcSJacob 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);
9869566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
987cd0c2139SMatthew G Knepley     }
9889566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9899566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
990cd0c2139SMatthew G Knepley   }
991cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
9929566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
9939566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
994cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
995cd0c2139SMatthew G Knepley   ghostCell = cEnd;
996cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
997cd0c2139SMatthew G Knepley     IS              faceIS;
998cd0c2139SMatthew G Knepley     const PetscInt *faces;
999cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
1000cd0c2139SMatthew G Knepley 
10019566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
10029566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
10039566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
100446c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
1005ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
1006cd0c2139SMatthew G Knepley 
10079566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
10089566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
1009ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
101046c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
10119566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
10129566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
101346c796b9SMatthew G. Knepley       ++ghostCell;
1014cd0c2139SMatthew G Knepley     }
10159566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
10169566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
1017cd0c2139SMatthew G Knepley   }
10189566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
10199566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
10209566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
10219566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
10229566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
10239566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
10249566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
10259566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
102648a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
1027966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
10284fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
10294fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L));
10304a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
10313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1032cd0c2139SMatthew G Knepley }
1033cd0c2139SMatthew G Knepley 
10345d83a8b1SBarry Smith /*@
1035cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
1036cd0c2139SMatthew G Knepley 
103720f4b53cSBarry Smith   Collective
1038cd0c2139SMatthew G Knepley 
1039cd0c2139SMatthew G Knepley   Input Parameters:
104020f4b53cSBarry Smith + dm        - The original `DM`
104120f4b53cSBarry Smith - labelName - The label specifying the boundary faces, or "Face Sets" if this is `NULL`
1042cd0c2139SMatthew G Knepley 
1043cd0c2139SMatthew G Knepley   Output Parameters:
104420f4b53cSBarry Smith + numGhostCells - The number of ghost cells added to the `DM`
104520f4b53cSBarry Smith - dmGhosted     - The new `DM`
1046cd0c2139SMatthew G Knepley 
1047cd0c2139SMatthew G Knepley   Level: developer
1048cd0c2139SMatthew G Knepley 
104920f4b53cSBarry Smith   Note:
105020f4b53cSBarry Smith   If no label exists of that name, one will be created marking all boundary faces
105120f4b53cSBarry Smith 
10521cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`
105331266bc0SMatthew G. Knepley @*/
DMPlexConstructGhostCells(DM dm,const char labelName[],PetscInt * numGhostCells,DM * dmGhosted)1054d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
1055d71ae5a4SJacob Faibussowitsch {
1056cd0c2139SMatthew G Knepley   DM          gdm;
1057cd0c2139SMatthew G Knepley   DMLabel     label;
1058cd0c2139SMatthew G Knepley   const char *name = labelName ? labelName : "Face Sets";
1059412e9a14SMatthew G. Knepley   PetscInt    dim, Ng = 0;
1060b0441da4SMatthew G. Knepley   PetscBool   useCone, useClosure;
1061cd0c2139SMatthew G Knepley 
1062cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1063cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10644f572ea9SToby Isaac   if (numGhostCells) PetscAssertPointer(numGhostCells, 3);
10654f572ea9SToby Isaac   PetscAssertPointer(dmGhosted, 4);
10669566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
10679566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
10689566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
10699566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
10709566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
10719566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure));
10729566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
1073cd0c2139SMatthew G Knepley   if (!label) {
1074cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
10759566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
10769566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
10779566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
1078cd0c2139SMatthew G Knepley   }
10799566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
10809566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
10815de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
1082cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
1083d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
1084cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
10853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1086cd0c2139SMatthew G Knepley }
1087cd0c2139SMatthew G Knepley 
DivideCells_Private(DM dm,DMLabel label,DMPlexPointQueue queue)1088d71ae5a4SJacob Faibussowitsch static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue)
1089d71ae5a4SJacob Faibussowitsch {
1090accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
1091accc9626SMatthew G. Knepley 
1092accc9626SMatthew G. Knepley   PetscFunctionBegin;
1093accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1094accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
109548a46eb9SPierre Jolivet   for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
1096accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
1097accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
1098accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
1099accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
1100accc9626SMatthew G. Knepley 
1101accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
1102accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
1103accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1104accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
1105accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1106accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1107accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
1108accc9626SMatthew G. Knepley         PetscInt       clval;
1109accc9626SMatthew G. Knepley 
1110accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
1111accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
1112accc9626SMatthew G. Knepley         if (clval == -1) {
1113accc9626SMatthew G. Knepley           const PetscInt *cone;
1114accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
1115accc9626SMatthew G. Knepley 
1116accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
1117accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
1118accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
1119accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
1120accc9626SMatthew G. Knepley             PetscInt cpval;
1121accc9626SMatthew G. Knepley 
1122accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
1123accc9626SMatthew G. Knepley             if (cpval != -1) {
1124accc9626SMatthew G. Knepley               PetscInt dep;
1125accc9626SMatthew G. Knepley 
1126accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
1127accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift + dep) : shift + dep;
1128accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
1129accc9626SMatthew G. Knepley               break;
1130accc9626SMatthew G. Knepley             }
1131accc9626SMatthew G. Knepley           }
1132accc9626SMatthew G. Knepley         }
1133accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
1134accc9626SMatthew G. Knepley         if (d == dim - 1 && clval != -1) {
1135accc9626SMatthew G. Knepley           const PetscInt *support;
1136accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
1137accc9626SMatthew G. Knepley 
1138accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
1139accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
1140accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
1141accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
1142accc9626SMatthew G. Knepley             if (nval == -1) {
1143accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1));
1144accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1145accc9626SMatthew G. Knepley             }
1146accc9626SMatthew G. Knepley           }
1147accc9626SMatthew G. Knepley         }
1148accc9626SMatthew G. Knepley       }
1149accc9626SMatthew G. Knepley     }
1150accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1151accc9626SMatthew G. Knepley   }
1152accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
11533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1154accc9626SMatthew G. Knepley }
1155accc9626SMatthew G. Knepley 
1156accc9626SMatthew G. Knepley typedef struct {
1157accc9626SMatthew G. Knepley   DM               dm;
1158accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1159accc9626SMatthew G. Knepley } PointDivision;
1160accc9626SMatthew G. Knepley 
divideCell(DMLabel label,PetscInt p,PetscInt val,PetscCtx ctx)1161*2a8381b2SBarry Smith static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, PetscCtx ctx)
1162d71ae5a4SJacob Faibussowitsch {
1163accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *)ctx;
1164accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val - 1 : val + 1;
1165accc9626SMatthew G. Knepley   const PetscInt *support;
1166accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1167accc9626SMatthew G. Knepley 
1168accc9626SMatthew G. Knepley   PetscFunctionBegin;
1169accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1170accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1171accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1172accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1173accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1174accc9626SMatthew G. Knepley   }
11753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1176accc9626SMatthew G. Knepley }
1177accc9626SMatthew G. Knepley 
1178accc9626SMatthew G. Knepley /* Mark cells by label propagation */
DMPlexLabelFaultHalo(DM dm,DMLabel faultLabel)1179d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel)
1180d71ae5a4SJacob Faibussowitsch {
1181accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1182accc9626SMatthew G. Knepley   PointDivision    div;
1183accc9626SMatthew G. Knepley   PetscSF          pointSF;
1184accc9626SMatthew G. Knepley   IS               pointIS;
1185accc9626SMatthew G. Knepley   const PetscInt  *points;
1186accc9626SMatthew G. Knepley   PetscBool        empty;
1187accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1188accc9626SMatthew G. Knepley 
1189accc9626SMatthew G. Knepley   PetscFunctionBegin;
1190accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1191accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1192accc9626SMatthew G. Knepley   div.dm    = dm;
1193accc9626SMatthew G. Knepley   div.queue = queue;
1194accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1195accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS));
1196accc9626SMatthew G. Knepley   if (pointIS) {
1197accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1198accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
119948a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1200accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1201accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1202accc9626SMatthew G. Knepley   }
1203accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS));
1204accc9626SMatthew G. Knepley   if (pointIS) {
1205accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1206accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
120748a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1208accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1209accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1210accc9626SMatthew G. Knepley   }
1211accc9626SMatthew G. Knepley 
1212accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1213accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1214accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1215accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1216accc9626SMatthew G. Knepley   while (!empty) {
1217accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1218accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1219accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1220accc9626SMatthew G. Knepley   }
1221accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1222accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
12233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1224accc9626SMatthew G. Knepley }
1225accc9626SMatthew G. Knepley 
1226607ab7a9SMatthew G. Knepley /*
1227faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1228607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1229faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1230b6dfa339SMatthew 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
1231607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1232a6ae58d1SMatthew G. Knepley 
1233a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1234a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1235607ab7a9SMatthew G. Knepley */
DMPlexConstructCohesiveCells_Internal(DM dm,DMLabel label,DMLabel splitLabel,DM sdm)1236d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1237d71ae5a4SJacob Faibussowitsch {
1238cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1239607ab7a9SMatthew G. Knepley   IS               valueIS;
1240607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0; /* The number of depths for which we have replicated points */
1241607ab7a9SMatthew G. Knepley   const PetscInt  *values;    /* List of depths for which we have replicated points */
124218c5995bSMatthew G. Knepley   IS              *splitIS;
124318c5995bSMatthew G. Knepley   IS              *unsplitIS;
1244b6dfa339SMatthew G. Knepley   IS               ghostIS;
1245607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
124618c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
124736dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
124836dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1249b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1250607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
125118c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1252b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1253cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1254cd0c2139SMatthew G Knepley   Vec              coordinates;
1255cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1256a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;   /* The first hybrid point at each depth in the original mesh */
1257a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;   /* The point limit at each depth in the original mesh */
1258607ab7a9SMatthew G. Knepley   PetscInt        *depthShift; /* Number of replicated+hybrid points at each depth */
1259607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;    /* The first replicated point at each depth in the new mesh, hybrids come after this */
1260607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
126118c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1262cd0c2139SMatthew G Knepley 
1263cd0c2139SMatthew G Knepley   PetscFunctionBegin;
12649566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
12659566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
12669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
12679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1268412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
12699566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1270cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
12719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
12729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld));
12739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints));
1274607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
12759566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
12769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1277a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1278a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1279607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
128018c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1281607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1282a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1283607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
128418c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
128518c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
128618c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
128759eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
128859eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
128959eef20bSToby Isaac     depthShift[2 * d]     = depthMax[d];
129059eef20bSToby Isaac     depthShift[2 * d + 1] = 0;
1291607ab7a9SMatthew G. Knepley   }
1292b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1293b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1294cd0c2139SMatthew G Knepley   if (label) {
12959566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
12969566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
12979566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1298cd0c2139SMatthew G Knepley   }
1299cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1300cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1301cd0c2139SMatthew G Knepley 
1302cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
13039566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
130418c5995bSMatthew G. Knepley     if (splitIS[dep]) {
13059566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
13069566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
130718c5995bSMatthew G. Knepley     }
13089566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep]));
130918c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
13109566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
13119566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1312cd0c2139SMatthew G Knepley     }
1313cd0c2139SMatthew G Knepley   }
1314b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS));
1315b6dfa339SMatthew G. Knepley   if (ghostIS) {
1316b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1317b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1318b6dfa339SMatthew G. Knepley   }
1319607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
132018c5995bSMatthew 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   */
13212582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d];
13229566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
132359eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
132459eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
132559eef20bSToby Isaac    * added points */
13262582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
13279566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1328cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1329cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1330cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1331cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
13322582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
13334c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1334cd0c2139SMatthew G Knepley       const PetscInt *support;
1335394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
13364c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1337cd0c2139SMatthew G Knepley 
13389566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13399566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
13409566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13419566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
13429566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
13439566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1344cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
13454c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13464c367dbcSMatthew G. Knepley 
1347cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
13489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1349412e9a14SMatthew G. Knepley         switch (coneSize) {
1350d71ae5a4SJacob Faibussowitsch         case 2:
1351d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));
1352d71ae5a4SJacob Faibussowitsch           break;
1353d71ae5a4SJacob Faibussowitsch         case 3:
1354d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));
1355d71ae5a4SJacob Faibussowitsch           break;
1356d71ae5a4SJacob Faibussowitsch         case 4:
1357d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));
1358d71ae5a4SJacob Faibussowitsch           break;
1359412e9a14SMatthew G. Knepley         }
1360b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1361b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1362b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1363b6dfa339SMatthew G. Knepley           const PetscInt *support;
1364b6dfa339SMatthew G. Knepley           PetscInt        val;
1365b6dfa339SMatthew G. Knepley 
1366b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1367b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1368b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1369b6dfa339SMatthew G. Knepley           else PetscCall(DMPlexSetSupportSize(sdm, newp, 2));
1370b6dfa339SMatthew G. Knepley         }
1371cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13724c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1373cd0c2139SMatthew G Knepley 
13749566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13754c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1376cd0c2139SMatthew G Knepley           PetscInt val;
1377cd0c2139SMatthew G Knepley 
13789566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13794c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
13804c367dbcSMatthew G. Knepley           if ((val == 1) || (val == (shift + 1))) ++qn;
13814c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1382cd0c2139SMatthew G Knepley         }
13834c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
13849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13854c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
13869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13874c367dbcSMatthew G. Knepley         /* Add hybrid edge */
13889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
13899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1391cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
13924c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13934c367dbcSMatthew G. Knepley 
13949566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13954c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1396cd0c2139SMatthew G Knepley           PetscInt val;
1397cd0c2139SMatthew G Knepley 
13989566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13994c367dbcSMatthew G. Knepley           if (val == dim - 1) ++qf;
14004c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn;
14014c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp;
1402cd0c2139SMatthew G Knepley         }
14034c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
14049566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
14054c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
14069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
14074c367dbcSMatthew G. Knepley         /* Add hybrid face */
14089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1411cd0c2139SMatthew G Knepley       }
1412cd0c2139SMatthew G Knepley     }
1413cd0c2139SMatthew G Knepley   }
141418c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
141518c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
141618c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
14172582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
141818c5995bSMatthew G. Knepley       const PetscInt *support;
1419da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
142018c5995bSMatthew G. Knepley 
14219566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14229566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14239566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
142418c5995bSMatthew G. Knepley       if (dep == 0) {
142518c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
142618c5995bSMatthew G. Knepley 
142739254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
142839254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
14299566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
143039254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
143139254ff6SMatthew G. Knepley         }
14329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
143318c5995bSMatthew G. Knepley         /* Add hybrid edge */
14349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1435e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1436e1757548SMatthew G. Knepley           PetscInt val;
1437e1757548SMatthew G. Knepley 
14389566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1439e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1440da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1441da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1442e1757548SMatthew G. Knepley         }
14439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
14449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
144518c5995bSMatthew G. Knepley       } else if (dep == dim - 2) {
144618c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1447cd0c2139SMatthew G Knepley         PetscInt       val;
1448cd0c2139SMatthew G Knepley 
1449da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14509566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1451da1dd7e4SMatthew G. Knepley           if (val == dim - 1) qf += 2;
1452da1dd7e4SMatthew G. Knepley           else ++qf;
1453cd0c2139SMatthew G Knepley         }
145418c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
14559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
145618c5995bSMatthew G. Knepley         /* Add hybrid face */
1457da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14589566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1459da1dd7e4SMatthew G. Knepley           if (val == dim - 1) ++qf;
1460da1dd7e4SMatthew G. Knepley         }
14619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
14629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
14639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1464cd0c2139SMatthew G Knepley       }
1465cd0c2139SMatthew G Knepley     }
1466cd0c2139SMatthew G Knepley   }
1467cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
14689566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
14699566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
14709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
14719566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew));
1472cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1473cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1474cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1475cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
14762582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
14774c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1478cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1479b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
14804c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1481cd0c2139SMatthew G Knepley 
14829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
14839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
14859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
14869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14879566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1488cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
148996a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
14904c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1491cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1492cd0c2139SMatthew G Knepley 
1493b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1494cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
14959566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp, &supportF));
14969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1497cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1498cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1499cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
15009566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
150118c5995bSMatthew G. Knepley           if (v < 0) {
15029566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
150363a3b9bcSJacob 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);
15042582d50cSToby Isaac             coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
150596a07cd0SMatthew G. Knepley             hasUnsplit     = PETSC_TRUE;
150618c5995bSMatthew G. Knepley           } else {
15074c367dbcSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep - 1];
1508163235baSMatthew G. Knepley             if (dep > 1) {
1509163235baSMatthew G. Knepley               const PetscInt *econe;
1510163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1511163235baSMatthew G. Knepley 
15129566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
15139566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1514163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
15159566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs));
15169566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu));
1517163235baSMatthew G. Knepley                 if (vs >= 0) continue;
151863a3b9bcSJacob 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);
1519163235baSMatthew G. Knepley                 hasUnsplit = PETSC_TRUE;
1520163235baSMatthew G. Knepley               }
1521163235baSMatthew G. Knepley             }
1522cd0c2139SMatthew G Knepley           }
1523cd0c2139SMatthew G Knepley         }
15249566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
15259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1526e537020bSMatthew G. Knepley         /* Face support */
1527b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1528cd0c2139SMatthew G Knepley 
1529b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1530b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1531b6dfa339SMatthew G. Knepley         else vals[1] = -vals[0];
1532b6dfa339SMatthew 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]);
1533b6dfa339SMatthew G. Knepley 
1534b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1535b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1536b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1537b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1538accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1539b6dfa339SMatthew G. Knepley             } else {
1540b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1541accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1542b6dfa339SMatthew G. Knepley             }
1543b6dfa339SMatthew G. Knepley           } else {
1544b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1545cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
15469566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1547cd0c2139SMatthew G Knepley             } else {
1548cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
15499566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1550b6dfa339SMatthew G. Knepley             }
1551b6dfa339SMatthew G. Knepley           }
1552b6dfa339SMatthew G. Knepley         }
1553b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1554e537020bSMatthew G. Knepley         {
1555e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1556b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1557accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1558e537020bSMatthew G. Knepley 
15599371c9d4SSatish Balay           if (supportSize > 1) {
15609371c9d4SSatish Balay             ocell = vals[0] < 0 ? support[1] : support[0];
15619371c9d4SSatish Balay           } else {
15629371c9d4SSatish Balay             ocell = support[0];
15639371c9d4SSatish Balay             flip  = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;
15649371c9d4SSatish Balay           }
1565b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1566b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1567e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1568e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1569e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1570b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc];
1571e537020bSMatthew G. Knepley               break;
1572cd0c2139SMatthew G Knepley             }
1573cd0c2139SMatthew G Knepley           }
1574b6dfa339SMatthew 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);
1575e537020bSMatthew G. Knepley         }
15764c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1577b6dfa339SMatthew G. Knepley         {
157885036b15SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangements(ct) / 2;
1579b6555650SPierre Jolivet           PetscCheck(coneONew[0] >= -No && coneONew[0] < No, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1580b6dfa339SMatthew G. Knepley         }
158185036b15SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, coneONew[0]);
1582b5a892a1SMatthew G. Knepley 
1583fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp; /* Extracted negative side orientation above */
15844c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
15854c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1586e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1587412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1588b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q * 2 + 0];
1589b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q * 2 + 1];
1590b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1591412e9a14SMatthew G. Knepley 
15929566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
159318c5995bSMatthew G. Knepley           if (v < 0) {
15949566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
159518c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
159618c5995bSMatthew G. Knepley           } else {
159718c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep];
159818c5995bSMatthew G. Knepley           }
1599b5a892a1SMatthew G. Knepley           coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1600e537020bSMatthew G. Knepley         }
16019566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
16029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
160396a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
16049566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1605cd0c2139SMatthew G Knepley       } else if (dep == 0) {
16064c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1607cd0c2139SMatthew G Knepley 
1608cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
16094c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1610cd0c2139SMatthew G Knepley           PetscInt val;
1611cd0c2139SMatthew G Knepley 
16129566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1613ad540459SPierre Jolivet           if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1614cd0c2139SMatthew G Knepley         }
16154c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
16169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1617cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
16184c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1619cd0c2139SMatthew G Knepley           PetscInt val, edge;
1620cd0c2139SMatthew G Knepley 
16219566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1622cd0c2139SMatthew G Knepley           if (val == 1) {
16239566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
162463a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16254c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep + 1];
1626cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
16272582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1628cd0c2139SMatthew G Knepley           }
1629cd0c2139SMatthew G Knepley         }
16304c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
16319566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16324c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1633cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1634cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
16359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
16364c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
16374c367dbcSMatthew G. Knepley           PetscInt val, edge;
16384c367dbcSMatthew G. Knepley 
16399566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16404c367dbcSMatthew G. Knepley           if (val == 1) {
16419566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
164263a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
16434c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
16444c367dbcSMatthew G. Knepley           }
16454c367dbcSMatthew G. Knepley         }
16469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1647cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
16484c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16494c367dbcSMatthew G. Knepley 
1650cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1651cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1652cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
16539566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
1654e1757548SMatthew G. Knepley           if (v < 0) {
16559566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
165663a3b9bcSJacob 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);
16572582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1658e1757548SMatthew G. Knepley           } else {
16594c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep - 1];
1660cd0c2139SMatthew G Knepley           }
1661e1757548SMatthew G. Knepley         }
16629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1663cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1664cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1665cd0c2139SMatthew G Knepley           PetscInt val;
1666cd0c2139SMatthew G Knepley 
16679566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16684c367dbcSMatthew G. Knepley           if (val == dim - 1) {
16692582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
16704c367dbcSMatthew G. Knepley           } else if (val == (shift + dim - 1)) {
16712582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1672cd0c2139SMatthew G Knepley           }
1673cd0c2139SMatthew G Knepley         }
1674b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1676cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1677cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1678cd0c2139SMatthew G Knepley           PetscInt val, face;
1679cd0c2139SMatthew G Knepley 
16809566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1681cd0c2139SMatthew G Knepley           if (val == dim - 1) {
16829566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
168363a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
16844c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep + 1];
1685cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim - 1)) {
16862582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1687cd0c2139SMatthew G Knepley           }
1688cd0c2139SMatthew G Knepley         }
1689b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16914c367dbcSMatthew G. Knepley         /* Hybrid face */
16924c367dbcSMatthew G. Knepley         coneNew[0] = newp;
16934c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
16944c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
16954c367dbcSMatthew G. Knepley           PetscInt vertex;
16969566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex));
1697e1757548SMatthew G. Knepley           if (vertex < 0) {
16989566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex));
169963a3b9bcSJacob 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);
1700e1757548SMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
1701e1757548SMatthew G. Knepley           } else {
17024c367dbcSMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
17034c367dbcSMatthew G. Knepley           }
1704e1757548SMatthew G. Knepley         }
17059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
17064c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
17074c367dbcSMatthew G. Knepley           PetscInt val, face;
17084c367dbcSMatthew G. Knepley 
17099566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
17104c367dbcSMatthew G. Knepley           if (val == dim - 1) {
17119566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
171263a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
17134c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
17144c367dbcSMatthew G. Knepley           }
17154c367dbcSMatthew G. Knepley         }
17169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1717cd0c2139SMatthew G Knepley       }
1718cd0c2139SMatthew G Knepley     }
1719cd0c2139SMatthew G Knepley   }
172018c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
172118c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
172218c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
17232582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1724b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1725e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
172618c5995bSMatthew G. Knepley 
17279566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
17289566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
17299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
17309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
173118c5995bSMatthew G. Knepley       if (dep == 0) {
173218c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
173318c5995bSMatthew G. Knepley 
173418c5995bSMatthew G. Knepley         /* Unsplit vertex */
17359566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
173618c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
17372582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
17389566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
1739ad540459SPierre Jolivet           if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1];
174018c5995bSMatthew G. Knepley         }
174118c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174218c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
174363a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
17449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
174518c5995bSMatthew G. Knepley         /* Hybrid edge */
174618c5995bSMatthew G. Knepley         coneNew[0] = newp;
174718c5995bSMatthew G. Knepley         coneNew[1] = newp;
17489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
174918c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
175018c5995bSMatthew G. Knepley           PetscInt val, edge;
175118c5995bSMatthew G. Knepley 
17529566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
175318c5995bSMatthew G. Knepley           if (val == 1) {
17549566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
175563a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
175618c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1757e1757548SMatthew G. Knepley           } else if (val == (shift2 + 1)) {
17589566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge));
175963a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1760e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1];
176118c5995bSMatthew G. Knepley           }
176218c5995bSMatthew G. Knepley         }
17639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1764e1757548SMatthew G. Knepley       } else if (dep == dim - 2) {
1765e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1766e1757548SMatthew G. Knepley 
1767da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1768e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1769e1757548SMatthew G. Knepley           PetscInt val, face;
1770e1757548SMatthew G. Knepley 
17719566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1772e1757548SMatthew G. Knepley           if (val == dim - 1) {
17739566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
177463a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
17752582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1776e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 1];
1777e1757548SMatthew G. Knepley           } else {
17782582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1779e1757548SMatthew G. Knepley           }
1780e1757548SMatthew G. Knepley         }
1781e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1782e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
17839566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
178463a3b9bcSJacob 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);
17859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1786e1757548SMatthew G. Knepley         /* Add hybrid face */
1787e1757548SMatthew G. Knepley         coneNew[0] = newp;
1788212cc919SMatthew G. Knepley         coneNew[1] = newp;
17899566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
179063a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1791212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17929566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
179363a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]);
1794e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17959566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1796da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1797da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1798da1dd7e4SMatthew G. Knepley 
17999566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1800da1dd7e4SMatthew G. Knepley           if (val == dim - 1) {
18019566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
1802da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1803da1dd7e4SMatthew G. Knepley           }
1804da1dd7e4SMatthew G. Knepley         }
18059566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
180663a3b9bcSJacob 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);
18079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1808cd0c2139SMatthew G Knepley       }
1809cd0c2139SMatthew G Knepley     }
1810cd0c2139SMatthew G Knepley   }
1811cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1812cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1813cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1814cd0c2139SMatthew G Knepley     IS              pIS;
1815cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1816cd0c2139SMatthew G Knepley     const PetscInt *points;
1817cd0c2139SMatthew G Knepley 
1818cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
18199566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1820cd0c2139SMatthew G Knepley     if (!pIS) continue;
1821cd0c2139SMatthew G Knepley     dep = -dep - shift;
18229566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
18239566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1824cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1825cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
18262582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1827cd0c2139SMatthew G Knepley       const PetscInt *cone;
1828cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
182950cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1830cd0c2139SMatthew G Knepley 
1831cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1832cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
18339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
18349566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1835cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1836e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift);
1837cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1838cd0c2139SMatthew G Knepley 
18399566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1840cd0c2139SMatthew G Knepley         if (val == dep - 1) {
18419566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp));
184263a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1);
1843cd0c2139SMatthew G Knepley           csplitp = pMaxNew[dep - 1] + cp;
18449566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
184550cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1846cd0c2139SMatthew G Knepley         }
1847cd0c2139SMatthew G Knepley       }
18484a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
184928b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1850cd0c2139SMatthew G Knepley     }
18519566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
18529566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1853cd0c2139SMatthew G Knepley   }
18540e18dc48SMatthew G. Knepley   PetscCall(DMPlexReorderCohesiveSupports(sdm));
1855fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
18569566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
18579566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
18589566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
18599566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1860cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
18612582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1862cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1863cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1864cd0c2139SMatthew G Knepley 
18659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
18669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
18679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1868cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d];
1869cd0c2139SMatthew G Knepley   }
18709566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1871fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
18729566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1873b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1874fa8e8ae5SToby Isaac   /* Step 9: Labels */
18759566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
18769566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
18779566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1878cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1879cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
18802582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1881cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1882cd0c2139SMatthew G Knepley       PetscInt       l;
1883cd0c2139SMatthew G Knepley 
18847db7e0a7SMatthew G. Knepley       if (splitLabel) {
18857db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
18867db7e0a7SMatthew G. Knepley 
18879566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp, val));
18889566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
18897db7e0a7SMatthew G. Knepley       }
1890cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1891cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1892cd0c2139SMatthew G Knepley         const char *lname;
1893cd0c2139SMatthew G Knepley         PetscInt    val;
18949a356370SMatthew G. Knepley         PetscBool   isDepth;
1895cd0c2139SMatthew G Knepley 
18969566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
18979566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
18989a356370SMatthew G. Knepley         if (isDepth) continue;
18999566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
19009566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
190148a46eb9SPierre Jolivet         if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val));
1902cd0c2139SMatthew G Knepley       }
1903cd0c2139SMatthew G Knepley     }
1904cd0c2139SMatthew G Knepley   }
1905cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1906cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1907cd0c2139SMatthew G Knepley 
1908cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
19099566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
19109566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
19119566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
19129566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1913cd0c2139SMatthew G Knepley   }
1914b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1915b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1916cd0c2139SMatthew G Knepley   if (label) {
19179566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
19189566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1919cd0c2139SMatthew G Knepley   }
19200d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
19219566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
192236dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
19230d4d4d06SMatthew G. Knepley   }
19249566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
19259566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
19269566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
19273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1928cd0c2139SMatthew G Knepley }
1929cd0c2139SMatthew G Knepley 
1930cd0c2139SMatthew G Knepley /*@C
1931cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1932cd0c2139SMatthew G Knepley 
193320f4b53cSBarry Smith   Collective
1934cd0c2139SMatthew G Knepley 
1935cd0c2139SMatthew G Knepley   Input Parameters:
193620f4b53cSBarry Smith + dm    - The original `DM`
193720f4b53cSBarry Smith - label - The `DMLabel` specifying the boundary faces (this could be auto-generated)
1938cd0c2139SMatthew G Knepley 
1939cd0c2139SMatthew G Knepley   Output Parameters:
194020f4b53cSBarry Smith + splitLabel - The `DMLabel` containing the split points, or `NULL` if no output is desired
194120f4b53cSBarry Smith - dmSplit    - The new `DM`
1942cd0c2139SMatthew G Knepley 
1943cd0c2139SMatthew G Knepley   Level: developer
1944cd0c2139SMatthew G Knepley 
19451cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1946cd0c2139SMatthew G Knepley @*/
DMPlexConstructCohesiveCells(DM dm,DMLabel label,DMLabel splitLabel,DM * dmSplit)1947d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1948d71ae5a4SJacob Faibussowitsch {
1949cd0c2139SMatthew G Knepley   DM       sdm;
1950cd0c2139SMatthew G Knepley   PetscInt dim;
1951cd0c2139SMatthew G Knepley 
1952cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1953cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19544f572ea9SToby Isaac   PetscAssertPointer(dmSplit, 4);
19559566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
19569566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
19579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19589566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1959cd0c2139SMatthew G Knepley   switch (dim) {
1960cd0c2139SMatthew G Knepley   case 2:
1961d71ae5a4SJacob Faibussowitsch   case 3:
1962d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1963d71ae5a4SJacob Faibussowitsch     break;
1964d71ae5a4SJacob Faibussowitsch   default:
1965d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1966cd0c2139SMatthew G Knepley   }
1967cd0c2139SMatthew G Knepley   *dmSplit = sdm;
19683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1969cd0c2139SMatthew G Knepley }
1970cd0c2139SMatthew G Knepley 
19710f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
GetSurfaceSide_Static(DM dm,DM subdm,PetscInt numSubpoints,const PetscInt * subpoints,PetscInt cell,PetscInt face,PetscBool * pos)1972d71ae5a4SJacob Faibussowitsch static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1973d71ae5a4SJacob Faibussowitsch {
19740f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
19750f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
19760f66a230SMatthew G. Knepley 
19770f66a230SMatthew G. Knepley   PetscFunctionBegin;
19780f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
19799566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
19829566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
19830f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
19840f66a230SMatthew G. Knepley     if (cone[c] == face) {
19850f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
19860f66a230SMatthew G. Knepley 
19870f66a230SMatthew G. Knepley       if (subdm) {
19880f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
19890f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
19900f66a230SMatthew G. Knepley 
19919566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
19929566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
19939566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
19949566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
19959566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
19960f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
19970f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
19980f66a230SMatthew G. Knepley             o = subornt[0];
19990f66a230SMatthew G. Knepley             break;
20000f66a230SMatthew G. Knepley           }
20010f66a230SMatthew G. Knepley         }
200263a3b9bcSJacob 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);
20030f66a230SMatthew G. Knepley       }
20040f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
20050f66a230SMatthew G. Knepley       else *pos = PETSC_FALSE;
20060f66a230SMatthew G. Knepley       break;
20070f66a230SMatthew G. Knepley     }
20080f66a230SMatthew G. Knepley   }
200963a3b9bcSJacob 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);
20103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20110f66a230SMatthew G. Knepley }
20120f66a230SMatthew G. Knepley 
CheckFaultEdge_Private(DM dm,DMLabel label,PetscBool split)20130542aa8cSMatthew G. Knepley static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label, PetscBool split)
2014d71ae5a4SJacob Faibussowitsch {
2015accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
2016accc9626SMatthew G. Knepley   const PetscInt *points;
20170542aa8cSMatthew G. Knepley   PetscInt       *closure = NULL, *inclosure = NULL;
20180542aa8cSMatthew G. Knepley   PetscInt        dim, numPoints, shift = 100, shift2 = 200, debug = 0;
2019accc9626SMatthew G. Knepley 
2020accc9626SMatthew G. Knepley   PetscFunctionBegin;
2021accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
20220542aa8cSMatthew G. Knepley   // If any faces touching the fault divide cells on either side,
20230542aa8cSMatthew G. Knepley   //   either split them, or unsplit the connection
2024accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
2025accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
2026accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
2027accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
2028accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
20293ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2030accc9626SMatthew G. Knepley   }
2031accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
2032accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
2033accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
2034accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
2035accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
20360542aa8cSMatthew G. Knepley   for (PetscInt p = 0; p < numPoints; ++p) {
2037accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
2038accc9626SMatthew G. Knepley     const PetscInt *support;
2039accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
2040accc9626SMatthew G. Knepley 
2041accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
2042accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
2043accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
2044accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
2045accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
2046accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
2047accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
20480542aa8cSMatthew G. Knepley     // Check that this face is not incident on only unsplit faces,
20490542aa8cSMatthew G. Knepley     //   meaning has at least one split face
2050e1a13daeSMatthew G. Knepley     {
2051e1a13daeSMatthew G. Knepley       PetscBool split = PETSC_FALSE;
20520542aa8cSMatthew G. Knepley       PetscInt  Ncl, val;
2053e1a13daeSMatthew G. Knepley 
20540542aa8cSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &Ncl, &closure));
20550542aa8cSMatthew G. Knepley       for (PetscInt cl = 0; cl < Ncl * 2; cl += 2) {
20560542aa8cSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &val));
2057b6555650SPierre Jolivet         if (val >= 0 && val <= dim) {
20589371c9d4SSatish Balay           split = PETSC_TRUE;
20599371c9d4SSatish Balay           break;
20609371c9d4SSatish Balay         }
2061e1a13daeSMatthew G. Knepley       }
2062e1a13daeSMatthew G. Knepley       if (!split) continue;
2063e1a13daeSMatthew G. Knepley     }
20640542aa8cSMatthew G. Knepley     if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Point %" PetscInt_FMT " is impinging (%" PetscInt_FMT ":%" PetscInt_FMT ", %" PetscInt_FMT ":%" PetscInt_FMT ")\n", point, support[0], valA, support[1], valB));
20650542aa8cSMatthew G. Knepley     if (split) {
20660542aa8cSMatthew G. Knepley       // Split the face
2067accc9626SMatthew G. Knepley       PetscCall(DMLabelGetValue(label, point, &valA));
2068accc9626SMatthew G. Knepley       PetscCall(DMLabelClearValue(label, point, valA));
2069accc9626SMatthew G. Knepley       PetscCall(DMLabelSetValue(label, point, dim - 1));
2070accc9626SMatthew G. Knepley       /* Label its closure:
2071accc9626SMatthew G. Knepley         unmarked: label as unsplit
2072accc9626SMatthew G. Knepley         incident: relabel as split
20730542aa8cSMatthew G. Knepley         split:    do nothing */
2074accc9626SMatthew G. Knepley       {
2075accc9626SMatthew G. Knepley         PetscInt closureSize, cl, dep;
2076accc9626SMatthew G. Knepley 
2077accc9626SMatthew G. Knepley         PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2078accc9626SMatthew G. Knepley         for (cl = 0; cl < closureSize * 2; cl += 2) {
2079accc9626SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, closure[cl], &valA));
2080accc9626SMatthew G. Knepley           if (valA == -1) { /* Mark as unsplit */
2081accc9626SMatthew G. Knepley             PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2082accc9626SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
2083b6555650SPierre Jolivet           } else if ((valA >= shift && valA < shift2) || (valA <= -shift && valA > -shift2)) {
2084accc9626SMatthew G. Knepley             PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2085accc9626SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, closure[cl], valA));
2086accc9626SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, closure[cl], dep));
2087accc9626SMatthew G. Knepley           }
2088accc9626SMatthew G. Knepley         }
20890542aa8cSMatthew G. Knepley       }
20900542aa8cSMatthew G. Knepley     } else {
20910542aa8cSMatthew G. Knepley       // Unsplit the incident faces and their closures
20920542aa8cSMatthew G. Knepley       PetscInt Ncl, dep, val;
20930542aa8cSMatthew G. Knepley 
20940542aa8cSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &Ncl, &closure));
20950542aa8cSMatthew G. Knepley       for (PetscInt cl = 0; cl < Ncl * 2; cl += 2) {
20960542aa8cSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &val));
20970542aa8cSMatthew G. Knepley         if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Point %" PetscInt_FMT ":%" PetscInt_FMT "\n", closure[cl], val));
2098b6555650SPierre Jolivet         if (val >= 0 && val <= dim) {
20990542aa8cSMatthew G. Knepley           PetscInt Nincl, inval, indep;
21000542aa8cSMatthew G. Knepley 
21010542aa8cSMatthew G. Knepley           if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Point %" PetscInt_FMT " is being unsplit\n", closure[cl]));
21020542aa8cSMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
21030542aa8cSMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], val));
21040542aa8cSMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
21050542aa8cSMatthew G. Knepley 
21060542aa8cSMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, closure[cl], PETSC_TRUE, &Nincl, &inclosure));
21070542aa8cSMatthew G. Knepley           for (PetscInt incl = 0; incl < Nincl * 2; incl += 2) {
21080542aa8cSMatthew G. Knepley             PetscCall(DMLabelGetValue(label, inclosure[cl], &inval));
2109b6555650SPierre Jolivet             if (inval >= 0 && inval <= dim) {
21100542aa8cSMatthew G. Knepley               if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "    Point %" PetscInt_FMT " is being unsplit\n", inclosure[incl]));
21110542aa8cSMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, inclosure[incl], &indep));
21120542aa8cSMatthew G. Knepley               PetscCall(DMLabelClearValue(label, inclosure[incl], inval));
21130542aa8cSMatthew G. Knepley               PetscCall(DMLabelSetValue(label, inclosure[incl], shift2 + indep));
2114accc9626SMatthew G. Knepley             }
2115accc9626SMatthew G. Knepley           }
21160542aa8cSMatthew G. Knepley         }
21170542aa8cSMatthew G. Knepley       }
21180542aa8cSMatthew G. Knepley     }
21190542aa8cSMatthew G. Knepley   }
21200542aa8cSMatthew G. Knepley   PetscCall(DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &inclosure));
21210542aa8cSMatthew G. Knepley   PetscCall(DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure));
2122accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2123accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
21243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2125accc9626SMatthew G. Knepley }
2126accc9626SMatthew G. Knepley 
2127cd0c2139SMatthew G Knepley /*@
21280f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
2129cd0c2139SMatthew G Knepley   to complete the surface
2130cd0c2139SMatthew G Knepley 
2131cd0c2139SMatthew G Knepley   Input Parameters:
213220f4b53cSBarry Smith + dm     - The `DM`
213320f4b53cSBarry Smith . label  - A `DMLabel` marking the surface
213420f4b53cSBarry Smith . blabel - A `DMLabel` marking the vertices on the boundary which will not be duplicated, or `NULL` to find them automatically
213520f4b53cSBarry Smith . bvalue - Value of `DMLabel` marking the vertices on the boundary
2136bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
21370542aa8cSMatthew G. Knepley . split  - Split faces impinging on the surface, rather than clamping the surface boundary
213820f4b53cSBarry Smith - subdm  - The `DM` associated with the label, or `NULL`
2139cd0c2139SMatthew G Knepley 
2140cd0c2139SMatthew G Knepley   Output Parameter:
214120f4b53cSBarry Smith . label - A `DMLabel` marking all surface points
21420f66a230SMatthew G. Knepley 
2143cd0c2139SMatthew G Knepley   Level: developer
2144cd0c2139SMatthew G Knepley 
214520f4b53cSBarry Smith   Note:
214620f4b53cSBarry Smith   The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
214720f4b53cSBarry Smith 
21481cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2149cd0c2139SMatthew G Knepley @*/
DMPlexLabelCohesiveComplete(DM dm,DMLabel label,DMLabel blabel,PetscInt bvalue,PetscBool flip,PetscBool split,DM subdm)21500542aa8cSMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, PetscBool split, DM subdm)
2151d71ae5a4SJacob Faibussowitsch {
2152d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2153accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
215447946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2155bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
21560542aa8cSMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = split ? 300 : 0, dim, depth, numPoints, numSubpoints, p, val;
2157cd0c2139SMatthew G Knepley 
2158cd0c2139SMatthew G Knepley   PetscFunctionBegin;
21599566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
21609566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
21619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
216247946fd8SMatthew G. Knepley   if (subdm) {
21639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
216447946fd8SMatthew G. Knepley     if (subpointIS) {
21659566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
21669566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
216747946fd8SMatthew G. Knepley     }
216847946fd8SMatthew G. Knepley   }
2169d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
21709566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2171accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
21729566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
21739566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2174d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2175cd0c2139SMatthew G Knepley     const PetscInt *support;
2176cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2177cd0c2139SMatthew G Knepley 
21789566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2179c4419245SMatthew G. Knepley #if 0
2180c4419245SMatthew G. Knepley     if (supportSize != 2) {
2181c4419245SMatthew G. Knepley       const PetscInt *lp;
2182c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2183c4419245SMatthew G. Knepley 
2184c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2185c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
21869566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
21879566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
218863a3b9bcSJacob 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);
2189c4419245SMatthew G. Knepley     }
2190c4419245SMatthew G. Knepley #endif
21919566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2192cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
21930f66a230SMatthew G. Knepley       const PetscInt *cone;
2194cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
21950f66a230SMatthew G. Knepley       PetscBool       pos;
2196cd0c2139SMatthew G Knepley 
21979566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
21989566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
21999566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
22000f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
22010f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
22029566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
22039566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2204cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2205cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2206cd0c2139SMatthew G Knepley 
22079566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2208cd0c2139SMatthew G Knepley         if (val == -1) {
2209cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2210cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2211cd0c2139SMatthew G Knepley 
22129566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2213cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2214cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2215a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2216cd0c2139SMatthew G Knepley 
22179566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
22189566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2219b6555650SPierre Jolivet             if (val >= 0 && val < dim - 1 && bval < 0) {
22209566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2221cd0c2139SMatthew G Knepley               break;
2222cd0c2139SMatthew G Knepley             }
2223cd0c2139SMatthew G Knepley           }
22249566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2225cd0c2139SMatthew G Knepley         }
2226cd0c2139SMatthew G Knepley       }
2227cd0c2139SMatthew G Knepley     }
2228cd0c2139SMatthew G Knepley   }
2229accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2230accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2231a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
223286200784SMatthew G. Knepley   if (blabel) {
2233accc9626SMatthew G. Knepley     IS bdIS;
2234accc9626SMatthew G. Knepley 
2235caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2236accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2237accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2238a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2239a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2240a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2241a0541d8aSMatthew G. Knepley 
22429566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2243a0541d8aSMatthew G. Knepley       if (bval >= 0) {
22449566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2245f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2246f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
22479566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2248f7019248SMatthew G. Knepley         }
2249f7019248SMatthew G. Knepley       }
2250f7019248SMatthew G. Knepley     }
2251f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2252f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2253f7019248SMatthew G. Knepley       PetscInt       val, bval;
2254f7019248SMatthew G. Knepley 
22559566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2256f7019248SMatthew G. Knepley       if (bval >= 0) {
225786200784SMatthew G. Knepley         const PetscInt *cone, *support;
225886200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
225986200784SMatthew G. Knepley 
2260a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
22619566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2262e1a13daeSMatthew 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);
22639566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
22649566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
22652c06a818SMatthew G. Knepley         /* Check for cross-edge
22662c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
226786200784SMatthew G. Knepley         if (val != 0) continue;
22689566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
22699566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
227086200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
22719566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
22729566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
227363a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
22749566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
22759566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
22769566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
2277b6555650SPierre Jolivet           if (valE < 0 && valA >= 0 && valB >= 0 && cone[0] != cone[1]) PetscCall(DMLabelSetValue(blabel, support[s], 2));
227886200784SMatthew G. Knepley         }
2279a0541d8aSMatthew G. Knepley       }
2280a0541d8aSMatthew G. Knepley     }
2281accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2282accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2283a0541d8aSMatthew G. Knepley   }
2284b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2285b6dfa339SMatthew G. Knepley   {
2286b6dfa339SMatthew G. Knepley     PetscSF         sf;
2287b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2288b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2289b6dfa339SMatthew G. Knepley 
2290b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2291b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2292accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2293accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2294accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2295accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2296b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2297b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2298b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2299b6dfa339SMatthew G. Knepley         PetscInt       val;
2300b6dfa339SMatthew G. Knepley 
2301b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2302b6dfa339SMatthew G. Knepley         if (l >= 0) {
2303b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2304b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2305b6dfa339SMatthew G. Knepley 
2306b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2307b6dfa339SMatthew 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);
2308b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2309b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2310b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2311accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2312b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2313b6dfa339SMatthew G. Knepley 
2314b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2315b6dfa339SMatthew 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);
2316b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2317b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2318b6dfa339SMatthew G. Knepley           }
2319b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2320b6dfa339SMatthew G. Knepley         }
2321b6dfa339SMatthew G. Knepley       }
2322b6dfa339SMatthew G. Knepley     }
2323b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2324b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2325accc9626SMatthew G. Knepley   }
2326accc9626SMatthew G. Knepley divide:
2327b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2328accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
23290542aa8cSMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label, split));
23303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2331cd0c2139SMatthew G Knepley }
2332cd0c2139SMatthew G Knepley 
2333720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
DMPlexCheckValidSubmesh_Private(DM dm,DMLabel label,DM subdm)233466976f2fSJacob Faibussowitsch static PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2335d71ae5a4SJacob Faibussowitsch {
2336720e594eSMatthew G. Knepley   IS              subpointIS;
2337720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2338720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2339720e594eSMatthew G. Knepley 
2340720e594eSMatthew G. Knepley   PetscFunctionBegin;
23413ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
23429566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
23439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
23443ba16761SJacob Faibussowitsch   if (!subpointIS) PetscFunctionReturn(PETSC_SUCCESS);
23459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
23469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
23479566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2348720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2349720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2350720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2351720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2352720e594eSMatthew G. Knepley 
23539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2354720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2355720e594eSMatthew G. Knepley       PetscInt value = 0;
2356720e594eSMatthew G. Knepley 
2357720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
23589566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
23599371c9d4SSatish Balay       if (value == defaultValue) {
23609371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
23619371c9d4SSatish Balay         break;
23629371c9d4SSatish Balay       }
2363720e594eSMatthew G. Knepley     }
23649566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2365720e594eSMatthew G. Knepley     if (invalidCell) {
23669566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23679566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
23689566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
236963a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2370720e594eSMatthew G. Knepley     }
2371720e594eSMatthew G. Knepley   }
23729566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
23733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2374720e594eSMatthew G. Knepley }
2375720e594eSMatthew G. Knepley 
2376c08575a3SMatthew G. Knepley /*@
23773cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
23783cf72582SMatthew G. Knepley 
237920f4b53cSBarry Smith   Collective
23803cf72582SMatthew G. Knepley 
23813cf72582SMatthew G. Knepley   Input Parameters:
238220f4b53cSBarry Smith + dm      - The original `DM`
2383720e594eSMatthew G. Knepley . label   - The label specifying the interface vertices
2384caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2385caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
23863cf72582SMatthew G. Knepley 
23873cf72582SMatthew G. Knepley   Output Parameters:
238820f4b53cSBarry Smith + hybridLabel - The label fully marking the interface, or `NULL` if no output is desired
238920f4b53cSBarry Smith . splitLabel  - The label containing the split points, or `NULL` if no output is desired
239020f4b53cSBarry Smith . dmInterface - The new interface `DM`, or `NULL`
239120f4b53cSBarry Smith - dmHybrid    - The new `DM` with cohesive cells
23923cf72582SMatthew G. Knepley 
239320f4b53cSBarry Smith   Level: developer
239420f4b53cSBarry Smith 
239520f4b53cSBarry Smith   Note:
239620f4b53cSBarry Smith   The hybridLabel indicates what parts of the original mesh impinged on the division surface. For points
23976eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
23986eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
23996eccb800SMatthew 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
24006eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
24016eccb800SMatthew Knepley 
24026eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
240340cdb708SMatthew 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
24046eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
24056eccb800SMatthew Knepley 
240620f4b53cSBarry Smith   The dmInterface is a `DM` built from the original division surface. It has a label which can be retrieved using
240720f4b53cSBarry Smith   `DMPlexGetSubpointMap()` which maps each point back to the point in the surface of the original mesh.
24086eccb800SMatthew Knepley 
24091cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
24103cf72582SMatthew G. Knepley @*/
DMPlexCreateHybridMesh(DM dm,DMLabel label,DMLabel bdlabel,PetscInt bdvalue,DMLabel * hybridLabel,DMLabel * splitLabel,DM * dmInterface,DM * dmHybrid)2411d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
2412d71ae5a4SJacob Faibussowitsch {
24133cf72582SMatthew G. Knepley   DM       idm;
24147db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
24153cf72582SMatthew G. Knepley   PetscInt dim;
24163cf72582SMatthew G. Knepley 
24173cf72582SMatthew G. Knepley   PetscFunctionBegin;
24183cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24194f572ea9SToby Isaac   if (label) PetscAssertPointer(label, 2);
24204f572ea9SToby Isaac   if (bdlabel) PetscAssertPointer(bdlabel, 3);
24214f572ea9SToby Isaac   if (hybridLabel) PetscAssertPointer(hybridLabel, 5);
24224f572ea9SToby Isaac   if (splitLabel) PetscAssertPointer(splitLabel, 6);
24234f572ea9SToby Isaac   if (dmInterface) PetscAssertPointer(dmInterface, 7);
24244f572ea9SToby Isaac   PetscAssertPointer(dmHybrid, 8);
24259566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24269566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
24279566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
24289566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
24299566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
24309566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
24319566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
24327db7e0a7SMatthew G. Knepley   if (splitLabel) {
24337db7e0a7SMatthew G. Knepley     const char *name;
24347db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
24357db7e0a7SMatthew G. Knepley 
24369566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
2437c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(sname, name, sizeof(sname)));
2438c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(sname, " split", sizeof(sname)));
24399566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
24407db7e0a7SMatthew G. Knepley   }
24410542aa8cSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, PETSC_TRUE, idm));
24429371c9d4SSatish Balay   if (dmInterface) {
24439371c9d4SSatish Balay     *dmInterface = idm;
24449371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
24459566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
24465de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
24473cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
24489566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
24497db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
24504a7ee7d0SMatthew G. Knepley   {
24514a7ee7d0SMatthew G. Knepley     DM      cdm;
24524a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
24534a7ee7d0SMatthew G. Knepley 
24544a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
24559566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
24569566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
24579566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
24584a7ee7d0SMatthew G. Knepley   }
24593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2460cd0c2139SMatthew G Knepley }
2461cd0c2139SMatthew G Knepley 
2462efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2463efa14ee0SMatthew G Knepley 
2464efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2465efa14ee0SMatthew G Knepley */
DMPlexMarkSubmesh_Uninterpolated(DM dm,DMLabel vertexLabel,PetscInt value,DMLabel subpointMap,PetscInt * numFaces,PetscInt * nFV,DM subdm)2466d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2467d71ae5a4SJacob Faibussowitsch {
2468fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2469efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2470412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2471efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2472efa14ee0SMatthew G Knepley 
2473efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2474efa14ee0SMatthew G Knepley   *numFaces = 0;
2475efa14ee0SMatthew G Knepley   *nFV      = 0;
24769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24779566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
247877d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
24799566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
248048a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2481efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
24829566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2483efa14ee0SMatthew G Knepley   if (subvertexIS) {
24849566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24859566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2486efa14ee0SMatthew G Knepley   }
2487efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2488efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24890298fd71SBarry Smith     PetscInt      *star   = NULL;
2490efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2491efa14ee0SMatthew G Knepley 
24929566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2493efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2494efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2495b6555650SPierre Jolivet       if (point >= pStart[depth] && point < pEnd[depth]) star[numCells++] = point;
2496efa14ee0SMatthew G Knepley     }
2497efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2498efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
24990298fd71SBarry Smith       PetscInt      *closure = NULL;
2500efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2501efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2502efa14ee0SMatthew G Knepley 
25039566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
250465560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
250563a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
25069566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2507efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2508efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2509efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2510efa14ee0SMatthew G Knepley 
2511b6555650SPierre Jolivet         if (point >= pStart[0] && point < pEnd[0]) {
2512efa14ee0SMatthew G Knepley           ++numCorners;
25139566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2514830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2515efa14ee0SMatthew G Knepley         }
2516efa14ee0SMatthew G Knepley       }
25174ad8454bSPierre Jolivet       if (!*nFV) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
251863a3b9bcSJacob 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);
2519efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2520007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2521007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2522007baee2SMatthew G. Knepley 
2523efa14ee0SMatthew G Knepley         ++(*numFaces);
252448a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
25259566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
252648a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
25279566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2528efa14ee0SMatthew G Knepley       }
25299566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2530efa14ee0SMatthew G Knepley     }
25319566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2532efa14ee0SMatthew G Knepley   }
253348a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25349566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25359566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2537efa14ee0SMatthew G Knepley }
2538efa14ee0SMatthew G Knepley 
DMPlexMarkSubmesh_Interpolated(DM dm,DMLabel vertexLabel,PetscInt value,PetscBool markedFaces,PetscBool addCells,DMLabel subpointMap,DM subdm)25395390be7dSMatthew G. Knepley PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, PetscBool addCells, DMLabel subpointMap, DM subdm)
2540d71ae5a4SJacob Faibussowitsch {
254134b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2542efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2543412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2544efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2545efa14ee0SMatthew G Knepley 
2546efa14ee0SMatthew G Knepley   PetscFunctionBegin;
25479566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25489566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
254948a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2550efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
255134b4c39eSMatthew G. Knepley   if (vertexLabel) {
25529566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2553efa14ee0SMatthew G Knepley     if (subvertexIS) {
25549566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
25559566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2556efa14ee0SMatthew G Knepley     }
255734b4c39eSMatthew G. Knepley   }
2558efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2559efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
25600298fd71SBarry Smith     PetscInt      *star   = NULL;
2561efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2562efa14ee0SMatthew G Knepley 
25639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2564efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2565efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2566158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2567158acfadSMatthew G. Knepley 
2568b6555650SPierre Jolivet       if (point >= pStart[dim - 1] && point < pEnd[dim - 1]) {
2569158acfadSMatthew G. Knepley         if (markedFaces) {
25709566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2571158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2572158acfadSMatthew G. Knepley         }
2573158acfadSMatthew G. Knepley         star[numFaces++] = point;
2574158acfadSMatthew G. Knepley       }
2575efa14ee0SMatthew G Knepley     }
2576efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2577efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
25780298fd71SBarry Smith       PetscInt      *closure = NULL;
2579efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2580efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2581efa14ee0SMatthew G Knepley 
25829566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2583efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
258463a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
25859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2586efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2587efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2588efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2589efa14ee0SMatthew G Knepley 
2590b6555650SPierre Jolivet         if (point >= pStart[0] && point < pEnd[0]) {
25919566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2592830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2593efa14ee0SMatthew G Knepley         }
2594efa14ee0SMatthew G Knepley       }
2595efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2596efa14ee0SMatthew G Knepley         const PetscInt *support;
2597efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2598efa14ee0SMatthew G Knepley 
2599efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2600efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2601efa14ee0SMatthew G Knepley 
2602efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2603b6555650SPierre Jolivet             if (point >= pStart[d] && point < pEnd[d]) {
26049566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2605efa14ee0SMatthew G Knepley               break;
2606efa14ee0SMatthew G Knepley             }
2607efa14ee0SMatthew G Knepley           }
2608efa14ee0SMatthew G Knepley         }
26099566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
26109566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
26115390be7dSMatthew G. Knepley         if (addCells)
261248a46eb9SPierre Jolivet           for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2613efa14ee0SMatthew G Knepley       }
26149566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2615efa14ee0SMatthew G Knepley     }
26169566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2617efa14ee0SMatthew G Knepley   }
26189566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
26199566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
26209566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
26213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2622efa14ee0SMatthew G Knepley }
2623efa14ee0SMatthew G Knepley 
DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm,PetscBool hasLagrange,const char labelname[],PetscInt value,DMLabel subpointMap,PetscInt * numFaces,PetscInt * nFV,PetscInt * subCells[],DM subdm)2624d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2625d71ae5a4SJacob Faibussowitsch {
262627c04023SMatthew G. Knepley   DMLabel         label = NULL;
2627766ab985SMatthew G. Knepley   const PetscInt *cone;
26289fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2629766ab985SMatthew G. Knepley 
2630812bfc34SJed Brown   PetscFunctionBegin;
2631c0ed958bSJed Brown   *numFaces = 0;
2632c0ed958bSJed Brown   *nFV      = 0;
26339566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2634fed694aaSMatthew G. Knepley   *subCells = NULL;
26359566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26369566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26373ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
263827c04023SMatthew G. Knepley   if (label) {
263927c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
264027c04023SMatthew G. Knepley       PetscInt val;
264127c04023SMatthew G. Knepley 
26429566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
264327c04023SMatthew G. Knepley       if (val == value) {
264427c04023SMatthew G. Knepley         ++(*numFaces);
26459566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
264627c04023SMatthew G. Knepley       }
264727c04023SMatthew G. Knepley     }
264827c04023SMatthew G. Knepley   } else {
2649766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
26509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
265127c04023SMatthew G. Knepley   }
26529566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
26534ad8454bSPierre Jolivet   if (!*numFaces) PetscFunctionReturn(PETSC_SUCCESS);
26549fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2655766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2656766ab985SMatthew G. Knepley     const PetscInt *cells;
2657766ab985SMatthew G. Knepley     PetscInt        numCells;
2658766ab985SMatthew G. Knepley 
265927c04023SMatthew G. Knepley     if (label) {
266027c04023SMatthew G. Knepley       PetscInt val;
266127c04023SMatthew G. Knepley 
26629566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
266327c04023SMatthew G. Knepley       if (val != value) continue;
266427c04023SMatthew G. Knepley     }
26659566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
266648a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2667766ab985SMatthew G. Knepley     /* Negative face */
26689566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
266927234c99SMatthew G. Knepley     /* Not true in parallel
267008401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2671766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
26729566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
267327234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2674766ab985SMatthew G. Knepley     }
26759566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2676766ab985SMatthew G. Knepley     /* Positive face is not included */
2677766ab985SMatthew G. Knepley   }
26783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2679766ab985SMatthew G. Knepley }
2680766ab985SMatthew G. Knepley 
DMPlexMarkCohesiveSubmesh_Interpolated(DM dm,DMLabel label,PetscInt value,DMLabel subpointMap,DM subdm)2681d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2682d71ae5a4SJacob Faibussowitsch {
2683766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2684766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2685766ab985SMatthew G. Knepley 
2686812bfc34SJed Brown   PetscFunctionBegin;
26879566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26889566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
26893ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
26909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
26919566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2692766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2693766ab985SMatthew G. Knepley     const PetscInt *cone;
2694766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2695b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2696766ab985SMatthew G. Knepley 
269727c04023SMatthew G. Knepley     if (label) {
26989566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
269927c04023SMatthew G. Knepley       if (val != value) continue;
270027c04023SMatthew G. Knepley     }
27019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
27029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
27039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
27041dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2705b3154360SMatthew G. Knepley     /* Negative face */
27069566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2707766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2708766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2709766ab985SMatthew G. Knepley 
2710766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2711b6555650SPierre Jolivet         if (point >= pStart[d] && point < pEnd[d]) {
27129566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2713766ab985SMatthew G. Knepley           break;
2714766ab985SMatthew G. Knepley         }
2715766ab985SMatthew G. Knepley       }
2716766ab985SMatthew G. Knepley     }
27179566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2718766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2719766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2720766ab985SMatthew G. Knepley       const PetscInt *support;
2721766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2722766ab985SMatthew G. Knepley 
27239566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
272408401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
27259566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
272648a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2727766ab985SMatthew G. Knepley     }
2728766ab985SMatthew G. Knepley   }
27299566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
27303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2731766ab985SMatthew G. Knepley }
2732766ab985SMatthew G. Knepley 
DMPlexGetFaceOrientation(DM dm,PetscInt cell,PetscInt numCorners,PetscInt indices[],PetscInt oppositeVertex,PetscInt origVertices[],PetscInt faceVertices[],PetscBool * posOriented)2733d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2734d71ae5a4SJacob Faibussowitsch {
273582f516ccSBarry Smith   MPI_Comm       comm;
2736e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2737e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2738e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2739e6ccafaeSMatthew G Knepley 
274082f516ccSBarry Smith   PetscFunctionBegin;
27419566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
27429566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
274363a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2744e6ccafaeSMatthew G Knepley 
2745ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2746ddeab2a6SMatthew G. Knepley     /* Triangle */
2747e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2748e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2749ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2750ddeab2a6SMatthew G. Knepley     /* Triangle */
2751ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2752ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2753ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2754ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2755ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2756ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2757e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2758e6ccafaeSMatthew G Knepley     /* Quadratic line */
2759e6ccafaeSMatthew G Knepley     faceSize  = 1;
2760e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2761e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2762e6ccafaeSMatthew G Knepley     /* Quads */
2763e6ccafaeSMatthew G Knepley     faceSize = 2;
2764b6555650SPierre Jolivet     if (indices[1] > indices[0] && indices[1] - indices[0] == 1) {
2765e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2766b6555650SPierre Jolivet     } else if (indices[0] == 3 && indices[1] == 0) {
2767e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2768e6ccafaeSMatthew G Knepley     } else {
2769966bd95aSPierre Jolivet       PetscCheck((indices[0] > indices[1] && indices[0] - indices[1] == 1) || (indices[0] == 0 && indices[1] == 3), comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2770e6ccafaeSMatthew G Knepley       posOrient = PETSC_FALSE;
2771e6ccafaeSMatthew G Knepley     }
2772e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2773e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2774e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2775e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2776e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2777e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2778e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2779e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2780e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2781e6ccafaeSMatthew G Knepley       2, 3, 5, /* left */
2782e6ccafaeSMatthew G Knepley     };
2783e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2784e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2785e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2786e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2787e6ccafaeSMatthew G Knepley     };
2788e6ccafaeSMatthew G Knepley 
2789e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
27909566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2791e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2792e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2793e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2794e6ccafaeSMatthew G Knepley 
2795b6555650SPierre Jolivet       if (sortedIndices[0] == faceVerticesTriSorted[ii + 0] && sortedIndices[1] == faceVerticesTriSorted[ii + 1]) {
2796e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2797e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2798e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2799e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2800e6ccafaeSMatthew G Knepley               break;
2801e6ccafaeSMatthew G Knepley             }
2802e6ccafaeSMatthew G Knepley           }
2803e6ccafaeSMatthew G Knepley         }
2804e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2805e6ccafaeSMatthew G Knepley         break;
2806e6ccafaeSMatthew G Knepley       }
2807e6ccafaeSMatthew G Knepley     }
280828b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2809e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28103ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2811e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2812e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2813e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2814e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2815e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2816e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2817e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2818e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2819e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2820e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2821e6ccafaeSMatthew G Knepley       0, 3, 7, /* left */
2822e6ccafaeSMatthew G Knepley     };
2823e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2824e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2825e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2826e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2827e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2828e6ccafaeSMatthew G Knepley     };
2829e6ccafaeSMatthew G Knepley 
2830e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
28319566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2832e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2833e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2834e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2835e6ccafaeSMatthew G Knepley 
2836b6555650SPierre Jolivet       if (sortedIndices[0] == faceVerticesQuadSorted[ii + 0] && sortedIndices[1] == faceVerticesQuadSorted[ii + 1]) {
2837e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2838e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2839e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2840e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2841e6ccafaeSMatthew G Knepley               break;
2842e6ccafaeSMatthew G Knepley             }
2843e6ccafaeSMatthew G Knepley           }
2844e6ccafaeSMatthew G Knepley         }
2845e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2846e6ccafaeSMatthew G Knepley         break;
2847e6ccafaeSMatthew G Knepley       }
2848e6ccafaeSMatthew G Knepley     }
284928b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2850e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28513ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2852e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2853e6ccafaeSMatthew G Knepley     /* Hexes
2854e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2855e6ccafaeSMatthew G Knepley        pointing up at the second.
2856e6ccafaeSMatthew G Knepley 
2857e6ccafaeSMatthew G Knepley           7---6
2858e6ccafaeSMatthew G Knepley          /|  /|
2859e6ccafaeSMatthew G Knepley         4---5 |
2860ddeab2a6SMatthew G. Knepley         | 1-|-2
2861e6ccafaeSMatthew G Knepley         |/  |/
2862ddeab2a6SMatthew G. Knepley         0---3
2863e6ccafaeSMatthew G Knepley 
2864e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2865e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2866e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2867e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2868e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2869e6ccafaeSMatthew G Knepley       0, 1, 2, 3, /* bottom */
2870e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2871ddeab2a6SMatthew G. Knepley       0, 3, 4, 5, /* front */
2872ddeab2a6SMatthew G. Knepley       2, 3, 5, 6, /* right */
2873ddeab2a6SMatthew G. Knepley       1, 2, 6, 7, /* back */
2874ddeab2a6SMatthew G. Knepley       0, 1, 4, 7, /* left */
2875e6ccafaeSMatthew G Knepley     };
2876e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2877ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2878e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2879ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2880ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2881ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2882ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2883e6ccafaeSMatthew G Knepley     };
2884e6ccafaeSMatthew G Knepley 
2885e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
28869566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2887e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2888e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2889e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2890e6ccafaeSMatthew G Knepley 
2891b6555650SPierre Jolivet       if (sortedIndices[0] == faceVerticesHexSorted[ii + 0] && sortedIndices[1] == faceVerticesHexSorted[ii + 1] && sortedIndices[2] == faceVerticesHexSorted[ii + 2] && sortedIndices[3] == faceVerticesHexSorted[ii + 3]) {
2892e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2893e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2894e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2895e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2896e6ccafaeSMatthew G Knepley               break;
2897e6ccafaeSMatthew G Knepley             }
2898e6ccafaeSMatthew G Knepley           }
2899e6ccafaeSMatthew G Knepley         }
2900e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2901e6ccafaeSMatthew G Knepley         break;
2902e6ccafaeSMatthew G Knepley       }
2903e6ccafaeSMatthew G Knepley     }
290428b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2905e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29063ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2907e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2908e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2909e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2910e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2911e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2912e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2913e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2914e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2915e6ccafaeSMatthew G Knepley       0, 3, 4, 6, 7, 9, /* front */
2916e6ccafaeSMatthew G Knepley       1, 4, 5, 7, 8, 9, /* right */
2917e6ccafaeSMatthew G Knepley       2, 3, 5, 6, 8, 9, /* left */
2918e6ccafaeSMatthew G Knepley     };
2919e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2920e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2921e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2922e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2923e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2924e6ccafaeSMatthew G Knepley     };
2925e6ccafaeSMatthew G Knepley 
2926e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
29279566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2928e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2929e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2930e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2931e6ccafaeSMatthew G Knepley 
2932b6555650SPierre Jolivet       if (sortedIndices[0] == faceVerticesTetSorted[ii + 0] && sortedIndices[1] == faceVerticesTetSorted[ii + 1] && sortedIndices[2] == faceVerticesTetSorted[ii + 2] && sortedIndices[3] == faceVerticesTetSorted[ii + 3]) {
2933e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2934e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2935e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2936e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2937e6ccafaeSMatthew G Knepley               break;
2938e6ccafaeSMatthew G Knepley             }
2939e6ccafaeSMatthew G Knepley           }
2940e6ccafaeSMatthew G Knepley         }
2941e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2942e6ccafaeSMatthew G Knepley         break;
2943e6ccafaeSMatthew G Knepley       }
2944e6ccafaeSMatthew G Knepley     }
294528b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2946e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
29473ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2948e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2949e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2950e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2951e6ccafaeSMatthew G Knepley        pointing up at the second.
2952e6ccafaeSMatthew G Knepley 
2953e6ccafaeSMatthew G Knepley          7---6
2954e6ccafaeSMatthew G Knepley         /|  /|
2955e6ccafaeSMatthew G Knepley        4---5 |
2956e6ccafaeSMatthew G Knepley        | 3-|-2
2957e6ccafaeSMatthew G Knepley        |/  |/
2958e6ccafaeSMatthew G Knepley        0---1
2959e6ccafaeSMatthew G Knepley 
2960e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2961e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2962e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2963e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2964e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2965e6ccafaeSMatthew G Knepley       0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2966e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2967e6ccafaeSMatthew G Knepley       0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2968e6ccafaeSMatthew G Knepley       1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2969e6ccafaeSMatthew G Knepley       2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2970e6ccafaeSMatthew G Knepley       0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2971e6ccafaeSMatthew G Knepley     };
2972e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2973e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2974e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2975e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2976e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2977e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2978e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2979e6ccafaeSMatthew G Knepley     };
2980e6ccafaeSMatthew G Knepley 
2981e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
29829566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2983e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2984e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2985e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2986e6ccafaeSMatthew G Knepley 
2987b6555650SPierre Jolivet       if (sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0] && sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1] && sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2] && sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3]) {
2988e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2989e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2990e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2991e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2992e6ccafaeSMatthew G Knepley               break;
2993e6ccafaeSMatthew G Knepley             }
2994e6ccafaeSMatthew G Knepley           }
2995e6ccafaeSMatthew G Knepley         }
2996e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2997e6ccafaeSMatthew G Knepley         break;
2998e6ccafaeSMatthew G Knepley       }
2999e6ccafaeSMatthew G Knepley     }
300028b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
3001e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
30023ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3003e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
3004e6ccafaeSMatthew G Knepley   if (!posOrient) {
30059566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
3006e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
3007e6ccafaeSMatthew G Knepley   } else {
30089566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
3009e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
3010e6ccafaeSMatthew G Knepley   }
3011e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
30123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3013e6ccafaeSMatthew G Knepley }
3014e6ccafaeSMatthew G Knepley 
3015c08575a3SMatthew G. Knepley /*@
3016c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
3017c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
3018c08575a3SMatthew G. Knepley 
301920f4b53cSBarry Smith   Not Collective
3020c08575a3SMatthew G. Knepley 
3021c08575a3SMatthew G. Knepley   Input Parameters:
3022c08575a3SMatthew G. Knepley + dm           - The original mesh
3023c08575a3SMatthew G. Knepley . cell         - The cell mesh point
3024c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
3025c08575a3SMatthew G. Knepley . face         - The face vertices
3026c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
3027c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
3028c08575a3SMatthew G. Knepley - origVertices - Original face vertices
3029c08575a3SMatthew G. Knepley 
3030d8d19677SJose E. Roman   Output Parameters:
3031c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
303220f4b53cSBarry Smith - posOriented  - `PETSC_TRUE` if the face was oriented with outward normal
3033c08575a3SMatthew G. Knepley 
3034c08575a3SMatthew G. Knepley   Level: developer
3035c08575a3SMatthew G. Knepley 
30361cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
3037c08575a3SMatthew G. Knepley @*/
DMPlexGetOrientedFace(DM dm,PetscInt cell,PetscInt faceSize,const PetscInt face[],PetscInt numCorners,PetscInt indices[],PetscInt origVertices[],PetscInt faceVertices[],PetscBool * posOriented)3038d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
3039d71ae5a4SJacob Faibussowitsch {
30400298fd71SBarry Smith   const PetscInt *cone = NULL;
3041e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
3042e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
3043e6ccafaeSMatthew G Knepley 
3044e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
30469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
3047e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
3048e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
3049e6ccafaeSMatthew G Knepley 
3050e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
3051e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
30529371c9d4SSatish Balay         found = PETSC_TRUE;
30539371c9d4SSatish Balay         break;
3054e6ccafaeSMatthew G Knepley       }
3055e6ccafaeSMatthew G Knepley     }
3056e6ccafaeSMatthew G Knepley     if (found) {
3057e6ccafaeSMatthew G Knepley       indices[v2]      = v;
3058e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
3059e6ccafaeSMatthew G Knepley       ++v2;
3060e6ccafaeSMatthew G Knepley     } else {
3061e6ccafaeSMatthew G Knepley       oppositeVertex = v;
3062e6ccafaeSMatthew G Knepley     }
3063e6ccafaeSMatthew G Knepley   }
30649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
30653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3066e6ccafaeSMatthew G Knepley }
3067e6ccafaeSMatthew G Knepley 
3068e6ccafaeSMatthew G Knepley /*
3069cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
3070e6ccafaeSMatthew G Knepley 
307120f4b53cSBarry Smith   Not Collective
3072e6ccafaeSMatthew G Knepley 
3073e6ccafaeSMatthew G Knepley   Input Parameters:
307420f4b53cSBarry Smith   + dm              - The `DMPLEX`
3075e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
307620f4b53cSBarry Smith   . faceVertices    - The vertices in the face for `dm`
3077e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
307820f4b53cSBarry Smith   . numCorners      - The number of vertices in the `cell`
307920f4b53cSBarry Smith   . cell            - A cell in `dm` containing the face
3080e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
3081e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
3082e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
3083e6ccafaeSMatthew G Knepley 
30842fe279fdSBarry Smith   Output Parameter:
3085e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
3086e6ccafaeSMatthew G Knepley 
3087e6ccafaeSMatthew G Knepley   Level: developer
3088e6ccafaeSMatthew G Knepley */
DMPlexInsertFace_Internal(DM dm,DM subdm,PetscInt numFaceVertices,const PetscInt faceVertices[],const PetscInt subfaceVertices[],PetscInt numCorners,PetscInt cell,PetscInt subcell,PetscInt firstFace,PetscInt * newFacePoint)3089d71ae5a4SJacob 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)
3090d71ae5a4SJacob Faibussowitsch {
309182f516ccSBarry Smith   MPI_Comm        comm;
3092e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
3093e6ccafaeSMatthew G Knepley   const PetscInt *faces;
3094e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
3095e6ccafaeSMatthew G Knepley 
3096e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30979566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
30989566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
309963a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
3100e6ccafaeSMatthew G Knepley #if 0
3101e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
31029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3103e6ccafaeSMatthew G Knepley #else
3104e6ccafaeSMatthew G Knepley   {
3105e6ccafaeSMatthew G Knepley     PetscInt f;
3106e6ccafaeSMatthew G Knepley 
3107e6ccafaeSMatthew G Knepley     numFaces = 0;
31089566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3109e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
3110e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
3111e6ccafaeSMatthew G Knepley 
31129566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
31139566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
3114e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
3115e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
3116e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
3117e6ccafaeSMatthew G Knepley         PetscInt       v;
3118e6ccafaeSMatthew G Knepley 
3119e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
3120e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
3121e6ccafaeSMatthew G Knepley         }
3122e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
3123e6ccafaeSMatthew G Knepley       }
3124e6ccafaeSMatthew G Knepley       if (d == dof) {
3125e6ccafaeSMatthew G Knepley         numFaces               = 1;
3126e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
3127e6ccafaeSMatthew G Knepley       }
3128e6ccafaeSMatthew G Knepley     }
3129e6ccafaeSMatthew G Knepley   }
3130e6ccafaeSMatthew G Knepley #endif
313163a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3132f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3133e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
31349566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3135e6ccafaeSMatthew G Knepley   } else {
3136e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3137e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3138e6ccafaeSMatthew G Knepley 
31399566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3140e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3141e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
3142e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
31439566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3144e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3145e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3146e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3147e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3148e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3149e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3150e6ccafaeSMatthew G Knepley           break;
3151e6ccafaeSMatthew G Knepley         }
3152e6ccafaeSMatthew G Knepley       }
315363a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3154e6ccafaeSMatthew G Knepley     }
31559566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
31569566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
31579566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3158e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3159e6ccafaeSMatthew G Knepley   }
3160ef07cca7SMatthew G. Knepley #if 0
31619566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3162ef07cca7SMatthew G. Knepley #else
31639566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3164ef07cca7SMatthew G. Knepley #endif
31653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3166e6ccafaeSMatthew G Knepley }
3167e6ccafaeSMatthew G Knepley 
DMPlexCreateSubmesh_Uninterpolated(DM dm,DMLabel vertexLabel,PetscInt value,DM subdm)3168d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3169d71ae5a4SJacob Faibussowitsch {
317082f516ccSBarry Smith   MPI_Comm        comm;
317153156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3172efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
3173efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3174efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3175fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3176efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3177e6ccafaeSMatthew G Knepley 
3178e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
31799566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3180efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
31819566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
31829566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
31839566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3184efa14ee0SMatthew G Knepley   /* Setup chart */
31859566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
31869566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
31879566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
31889566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3189e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3190e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3191efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
3192e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
31939566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
31949566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
31959566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
31969566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
319748a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
319848a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
31999566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3200d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3201e6ccafaeSMatthew G Knepley   /* Create face cones */
32029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
32039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
32049566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3205e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3206e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3207efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
32080298fd71SBarry Smith     PetscInt      *closure = NULL;
3209efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3210e6ccafaeSMatthew G Knepley 
32119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3212efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3213efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3214e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3215e6ccafaeSMatthew G Knepley 
3216b6555650SPierre Jolivet       if (point >= vStart && point < vEnd) {
3217efa14ee0SMatthew G Knepley         ++numCorners;
32189566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3219efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3220efa14ee0SMatthew G Knepley           closure[faceSize] = point;
322165560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3222e6ccafaeSMatthew G Knepley           ++faceSize;
3223e6ccafaeSMatthew G Knepley         }
3224e6ccafaeSMatthew G Knepley       }
3225e6ccafaeSMatthew G Knepley     }
322663a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
322748a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
32289566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3229e6ccafaeSMatthew G Knepley   }
32309566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
32319566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
32329566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3233e6ccafaeSMatthew G Knepley   /* Build coordinates */
3234efa14ee0SMatthew G Knepley   {
3235efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3236efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3237efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3238285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
323924640c55SToby Isaac     const char  *name;
3240efa14ee0SMatthew G Knepley 
32419566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
32429566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
32439566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
32449566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
32459566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
32469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
32479566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3248efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3249efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3250efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3251efa14ee0SMatthew G Knepley       PetscInt       dof;
3252efa14ee0SMatthew G Knepley 
32539566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32549566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
32559566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3256e6ccafaeSMatthew G Knepley     }
32579566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
32589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
32599566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
32609566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
32619566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
32629566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
32639566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3264830e53efSMatthew G. Knepley     if (coordSize) {
32659566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
32669566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3267efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3268efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3269efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3270efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3271e6ccafaeSMatthew G Knepley 
32729566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
32739566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
32749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
32759566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
327663a3b9bcSJacob 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);
3277e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3278e6ccafaeSMatthew G Knepley       }
32799566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
32809566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
32813b399e24SMatthew G. Knepley     }
32829566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
32839566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3284e6ccafaeSMatthew G Knepley   }
3285efa14ee0SMatthew G Knepley   /* Cleanup */
32869566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
32879566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
32889566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
32899566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
32903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3291e6ccafaeSMatthew G Knepley }
3292e6ccafaeSMatthew G Knepley 
3293d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
DMPlexFilterPoint_Internal(PetscInt point,PetscInt firstSubPoint,PetscInt numSubPoints,const PetscInt subPoints[])3294d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
3295d71ae5a4SJacob Faibussowitsch {
32963982b651SMatthew G. Knepley   PetscInt       subPoint;
32973982b651SMatthew G. Knepley   PetscErrorCode ierr;
32983982b651SMatthew G. Knepley 
32999371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
33009371c9d4SSatish Balay   if (ierr) return -1;
33013982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
33023982b651SMatthew G. Knepley }
33033982b651SMatthew G. Knepley 
3304d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
DMPlexFilterPointPerm_Internal(PetscInt point,PetscInt firstSubPoint,PetscInt numSubPoints,const PetscInt subPoints[],const PetscInt subIndices[])3305d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[])
3306d71ae5a4SJacob Faibussowitsch {
3307dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3308dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3309dd05d810SMatthew G. Knepley 
3310dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3311dd05d810SMatthew G. Knepley   if (ierr) return -1;
3312dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3313dd05d810SMatthew G. Knepley }
3314dd05d810SMatthew G. Knepley 
DMPlexFilterLabels_Internal(DM dm,const PetscInt numSubPoints[],const PetscInt * subpoints[],const PetscInt firstSubPoint[],DM subdm)3315d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3316d71ae5a4SJacob Faibussowitsch {
3317695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3318212103e5SMatthew Knepley   PetscInt Nl, l, d;
3319212103e5SMatthew Knepley 
3320212103e5SMatthew Knepley   PetscFunctionBegin;
3321695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3322695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3323695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
33249566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3325212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3326212103e5SMatthew Knepley     DMLabel         label, newlabel;
3327212103e5SMatthew Knepley     const char     *lname;
3328d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3329212103e5SMatthew Knepley     IS              valueIS;
3330212103e5SMatthew Knepley     const PetscInt *values;
3331212103e5SMatthew Knepley     PetscInt        Nv, v;
3332212103e5SMatthew Knepley 
33339566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
33349566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
33359566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
33369566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
33379566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3338d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
33399566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
33409566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
33419566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
33429566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
33439566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
33449566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
33459566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
33469566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3347212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3348212103e5SMatthew Knepley       IS              pointIS;
3349212103e5SMatthew Knepley       const PetscInt *points;
3350212103e5SMatthew Knepley       PetscInt        Np, p;
3351212103e5SMatthew Knepley 
33529566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
33539566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
33549566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3355212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3356212103e5SMatthew Knepley         const PetscInt point = points[p];
3357f117cc94SBrad Aagaard         PetscInt       subp, subdepth;
3358212103e5SMatthew Knepley 
33599566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3360f117cc94SBrad Aagaard         PetscCall(DMPlexGetDepth(subdm, &subdepth));
3361f117cc94SBrad Aagaard         if (d > subdepth) continue;
3362212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
33639566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3364212103e5SMatthew Knepley       }
33659566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
33669566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3367212103e5SMatthew Knepley     }
33689566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
33699566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3370212103e5SMatthew Knepley   }
33713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3372212103e5SMatthew Knepley }
3373212103e5SMatthew Knepley 
DMPlexCreateSubmeshGeneric_Interpolated(DM dm,DMLabel label,PetscInt value,PetscBool markedFaces,PetscBool isCohesive,PetscInt cellHeight,PetscBool ignoreLabelHalo,PetscBool sanitizeSubmesh,PetscSF * ownershipTransferSF,DM subdm)337430cbcd5dSksagiyam static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, PetscBool ignoreLabelHalo, PetscBool sanitizeSubmesh, PetscSF *ownershipTransferSF, DM subdm)
3375d71ae5a4SJacob Faibussowitsch {
337682f516ccSBarry Smith   MPI_Comm         comm;
337753156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3378efa14ee0SMatthew G Knepley   IS              *subpointIS;
3379efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
33803982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
33813e98c3d0Sksagiyam   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, coordinate_type;
33820d366550SMatthew G. Knepley   PetscMPIInt      rank;
3383e6ccafaeSMatthew G Knepley 
3384e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
33859566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
33869566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3387efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
33889566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
33899566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3390bec263e5SMatthew G. Knepley   if (cellHeight) {
33919566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
33925390be7dSMatthew G. Knepley     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, PETSC_TRUE, subpointMap, subdm));
3393bec263e5SMatthew G. Knepley   } else {
3394bec263e5SMatthew G. Knepley     DMLabel         depth;
3395bec263e5SMatthew G. Knepley     IS              pointIS;
3396bec263e5SMatthew G. Knepley     const PetscInt *points;
339771f1c950SStefano Zampini     PetscInt        numPoints = 0, pStart, pEnd;
33984d00ad26Sksagiyam     PetscBool      *isGhost   = NULL;
3399bec263e5SMatthew G. Knepley 
340071f1c950SStefano Zampini     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
34019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
340271f1c950SStefano Zampini     if (label) PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
340371f1c950SStefano Zampini     else {
340471f1c950SStefano Zampini       PetscInt cStart, cEnd;
340571f1c950SStefano Zampini 
340671f1c950SStefano Zampini       PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
340771f1c950SStefano Zampini       PetscCall(ISCreateStride(PETSC_COMM_SELF, cEnd - cStart, cStart, 1, &pointIS));
340871f1c950SStefano Zampini     }
3409b85c8bf9SMatthew G. Knepley     if (pointIS) {
34109566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
34119566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3412b85c8bf9SMatthew G. Knepley     }
34134d00ad26Sksagiyam     if (ignoreLabelHalo) {
34144d00ad26Sksagiyam       PetscSF         pointSF;
341571f1c950SStefano Zampini       PetscInt        nleaves;
34164d00ad26Sksagiyam       const PetscInt *ilocal = NULL;
34174d00ad26Sksagiyam 
34184d00ad26Sksagiyam       PetscCall(DMGetPointSF(dm, &pointSF));
34194d00ad26Sksagiyam       PetscCall(PetscSFGetGraph(pointSF, NULL, &nleaves, &ilocal, NULL));
34204d00ad26Sksagiyam       PetscCall(PetscMalloc1(pEnd - pStart, &isGhost));
34214d00ad26Sksagiyam       for (p = 0; p < pEnd - pStart; ++p) isGhost[p] = PETSC_FALSE;
34224d00ad26Sksagiyam       for (p = 0; p < nleaves; ++p) isGhost[(ilocal ? ilocal[p] : p) - pStart] = PETSC_TRUE;
34234d00ad26Sksagiyam     }
3424bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3425bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3426bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3427bec263e5SMatthew G. Knepley 
342871f1c950SStefano Zampini       if (isGhost && isGhost[points[p] - pStart]) continue;
34299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3430bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
34319566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
34329566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3433bec263e5SMatthew G. Knepley       }
34349566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3435bec263e5SMatthew G. Knepley     }
34364d00ad26Sksagiyam     PetscCall(PetscFree(isGhost));
34379566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
34389566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3439bec263e5SMatthew G. Knepley   }
3440efa14ee0SMatthew G Knepley   /* Setup chart */
34419566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3442dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
34439566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3444e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
34459566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3446e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3447e6ccafaeSMatthew G Knepley   }
3448dd05d810SMatthew G. Knepley   // Determine submesh dimension
3449dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3450dd05d810SMatthew G. Knepley   if (sdim > 0) {
3451dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3452dd05d810SMatthew G. Knepley     sdim = dim;
3453dd05d810SMatthew G. Knepley   } else {
3454dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3455dd05d810SMatthew G. Knepley     PetscInt lsdim;
3456dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3457dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3458462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPI_MAX, comm));
3459dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3460dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3461dd05d810SMatthew G. Knepley   }
34629566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
34639566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3464e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3465dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3466dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3467dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3468dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3469dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
34709566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
34719566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3472e6ccafaeSMatthew G Knepley   }
3473412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
34749566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3475dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3476e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3477e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3478e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3479e6ccafaeSMatthew G Knepley       const PetscInt *cone;
348015100a53SVaclav Hapla       PetscInt        coneSize;
3481e6ccafaeSMatthew G Knepley 
34829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3483dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
348415100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
348515100a53SVaclav Hapla 
34869566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3487e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34889566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3489e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3490e6ccafaeSMatthew G Knepley         }
34919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
34929566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
349315100a53SVaclav Hapla       } else {
349415100a53SVaclav Hapla         DMPolytopeType ct;
349515100a53SVaclav Hapla 
349615100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
349715100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
349815100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3499e6ccafaeSMatthew G Knepley       }
3500e6ccafaeSMatthew G Knepley     }
3501e6ccafaeSMatthew G Knepley   }
35029566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
35039566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3504e6ccafaeSMatthew G Knepley   /* Set cones */
35059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
35069566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3507dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3508e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3509e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3510e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
35110e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
35120d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3513e6ccafaeSMatthew G Knepley 
3514dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
35150d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
35160d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
35170d366550SMatthew G. Knepley 
35189566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
35199566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
35200d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3521064cae4fSPierre Jolivet           PetscBool isHybrid = PETSC_FALSE;
3522412e9a14SMatthew G. Knepley 
35239566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3524412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
35259566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
35260d366550SMatthew G. Knepley           if (subc >= 0) {
35270d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
35280d366550SMatthew G. Knepley 
35299566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
35309566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
35319566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
35320d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
35330d366550SMatthew G. Knepley               if (cone[c] == point) {
35340d366550SMatthew G. Knepley                 fornt = ornt[c];
35350d366550SMatthew G. Knepley                 break;
35360d366550SMatthew G. Knepley               }
35370d366550SMatthew G. Knepley             }
35380d366550SMatthew G. Knepley             break;
35390d366550SMatthew G. Knepley           }
35400d366550SMatthew G. Knepley         }
35410d366550SMatthew G. Knepley       }
35429566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
35439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
35449566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
35459566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3546e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
35479566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
354801a2673eSMatthew G. Knepley         if (subc >= 0) {
354901a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
35503982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
355101a2673eSMatthew G. Knepley           ++coneSizeNew;
355201a2673eSMatthew G. Knepley         }
3553e6ccafaeSMatthew G Knepley       }
355463a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
35559566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
35569566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
35579566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3558e6ccafaeSMatthew G Knepley     }
3559e6ccafaeSMatthew G Knepley   }
35609566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
35619566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
35629566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3563e6ccafaeSMatthew G Knepley   /* Build coordinates */
35643e98c3d0Sksagiyam   for (coordinate_type = 0; coordinate_type < 2; ++coordinate_type) {
35653e98c3d0Sksagiyam     DM           coordDM = NULL, subCoordDM = NULL;
35663e98c3d0Sksagiyam     PetscSection coordSection = NULL, subCoordSection = NULL;
35673e98c3d0Sksagiyam     Vec          coordinates = NULL, subCoordinates = NULL;
35683e98c3d0Sksagiyam     PetscScalar *coords = NULL, *subCoords = NULL;
3569605a06ccSksagiyam     PetscInt     bs, numComp, coordSize, firstP, lastP, firstSubP = totSubPoints, lastSubP = -1, numFields;
357024640c55SToby Isaac     const char  *name;
35713e98c3d0Sksagiyam     PetscBool    localized = (PetscBool)coordinate_type;
3572e6ccafaeSMatthew G Knepley 
35733e98c3d0Sksagiyam     if (!dm->coordinates[coordinate_type].dm) continue;
35743e98c3d0Sksagiyam     if (!localized) {
35753e98c3d0Sksagiyam       PetscCall(DMGetCoordinateDM(dm, &coordDM));
35763e98c3d0Sksagiyam       PetscCall(DMGetCoordinateDM(subdm, &subCoordDM));
35773e98c3d0Sksagiyam     } else {
35783e98c3d0Sksagiyam       {
35793e98c3d0Sksagiyam         PetscInt  localizationHeight;
35803e98c3d0Sksagiyam         PetscBool sparseLocalize;
35813e98c3d0Sksagiyam 
35823e98c3d0Sksagiyam         PetscCall(DMGetSparseLocalize(dm, &sparseLocalize));
35833e98c3d0Sksagiyam         PetscCall(DMSetSparseLocalize(subdm, sparseLocalize));
35843e98c3d0Sksagiyam         PetscCall(DMGetCoordinateDM(dm, &coordDM));
35853e98c3d0Sksagiyam         PetscCall(DMGetCoordinateDM(subdm, &subCoordDM));
35863e98c3d0Sksagiyam         PetscCall(DMPlexGetMaxProjectionHeight(coordDM, &localizationHeight));
35873e98c3d0Sksagiyam         PetscCall(DMPlexSetMaxProjectionHeight(subCoordDM, localizationHeight - (dim - sdim) - cellHeight));
35883e98c3d0Sksagiyam         PetscUseTypeMethod(subdm, createcellcoordinatedm, &subCoordDM);
35893e98c3d0Sksagiyam         PetscCall(DMSetCellCoordinateDM(subdm, subCoordDM));
35903e98c3d0Sksagiyam         PetscCall(DMDestroy(&subCoordDM));
35913e98c3d0Sksagiyam       }
35923e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateDM(dm, &coordDM));
35933e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateDM(subdm, &subCoordDM));
35943e98c3d0Sksagiyam     }
35954c712d99Sksagiyam     PetscCall(DMGetNumFields(coordDM, &numFields));
35964c712d99Sksagiyam     if (numFields > 0) {
35974c712d99Sksagiyam       PetscFE      fe = NULL;
35984c712d99Sksagiyam       PetscSpace   P  = NULL;
35994c712d99Sksagiyam       PetscClassId id;
36004c712d99Sksagiyam       PetscInt     degree;
36014c712d99Sksagiyam 
36024c712d99Sksagiyam       PetscCall(DMGetField(coordDM, 0, NULL, (PetscObject *)&fe));
36034c712d99Sksagiyam       PetscCall(PetscObjectGetClassId((PetscObject)fe, &id));
36044c712d99Sksagiyam       if (id == PETSCFE_CLASSID) {
36054c712d99Sksagiyam         if (sdim == dim && cellHeight == 0) {
36064c712d99Sksagiyam           /* TODO: Handle Field labels correctly */
36074c712d99Sksagiyam           PetscCall(DMSetField(subCoordDM, 0, NULL, (PetscObject)fe));
36084c712d99Sksagiyam           PetscCall(DMCreateDS(subCoordDM));
36094c712d99Sksagiyam         } else {
36104c712d99Sksagiyam           /* TODO: Reconstruct the lower-dimensional FE more robustly */
36114c712d99Sksagiyam           PetscCall(PetscFEGetBasisSpace(fe, &P));
36124c712d99Sksagiyam           PetscCall(PetscSpaceGetDegree(P, &degree, NULL));
3613e65c294aSksagiyam           PetscCall(DMPlexCreateCoordinateSpace(subdm, degree, localized, PETSC_FALSE));
36144c712d99Sksagiyam         }
36154c712d99Sksagiyam       }
36164c712d99Sksagiyam     }
36173e98c3d0Sksagiyam     if (!localized) {
36189566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateSection(dm, &coordSection));
36199566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
36203e98c3d0Sksagiyam       PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
36213e98c3d0Sksagiyam     } else {
36223e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateSection(dm, &coordSection));
36233e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinateSection(subdm, &subCoordSection));
36243e98c3d0Sksagiyam       PetscCall(DMGetCellCoordinatesLocal(dm, &coordinates));
36253e98c3d0Sksagiyam     }
36269566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
36279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
36289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
36293e98c3d0Sksagiyam     PetscCall(PetscSectionGetChart(coordSection, &firstP, &lastP));
36303e98c3d0Sksagiyam     for (d = 0; d <= sdim; ++d) {
36313e98c3d0Sksagiyam       for (p = 0; p < numSubPoints[d]; ++p) {
36323e98c3d0Sksagiyam         const PetscInt point    = subpoints[d][p];
36333e98c3d0Sksagiyam         const PetscInt subpoint = firstSubPoint[d] + p;
36343e98c3d0Sksagiyam 
36353e98c3d0Sksagiyam         if (point >= firstP && point < lastP) {
36363e98c3d0Sksagiyam           firstSubP = PetscMin(firstSubP, subpoint);
36373e98c3d0Sksagiyam           lastSubP  = PetscMax(lastSubP, subpoint);
36383e98c3d0Sksagiyam         }
36393e98c3d0Sksagiyam       }
36403e98c3d0Sksagiyam     }
36413e98c3d0Sksagiyam     lastSubP += 1;
36423e98c3d0Sksagiyam     if (firstSubP == totSubPoints) {
36433e98c3d0Sksagiyam       /* Zero if there is no coordinate point. */
36443e98c3d0Sksagiyam       firstSubP = 0;
36453e98c3d0Sksagiyam       lastSubP  = 0;
36463e98c3d0Sksagiyam     }
36473e98c3d0Sksagiyam     PetscCall(PetscSectionSetChart(subCoordSection, firstSubP, lastSubP));
36483e98c3d0Sksagiyam     for (d = 0; d <= sdim; ++d) {
36493e98c3d0Sksagiyam       for (p = 0; p < numSubPoints[d]; ++p) {
36503e98c3d0Sksagiyam         const PetscInt point    = subpoints[d][p];
36513e98c3d0Sksagiyam         const PetscInt subpoint = firstSubPoint[d] + p;
3652e6ccafaeSMatthew G Knepley         PetscInt       dof;
3653e6ccafaeSMatthew G Knepley 
36543e98c3d0Sksagiyam         if (point >= firstP && point < lastP) {
36553e98c3d0Sksagiyam           PetscCall(PetscSectionGetDof(coordSection, point, &dof));
36563e98c3d0Sksagiyam           PetscCall(PetscSectionSetDof(subCoordSection, subpoint, dof));
36573e98c3d0Sksagiyam           PetscCall(PetscSectionSetFieldDof(subCoordSection, subpoint, 0, dof));
36583e98c3d0Sksagiyam         }
36593e98c3d0Sksagiyam       }
3660e6ccafaeSMatthew G Knepley     }
36619566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
36629566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
36639566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
36649566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
36659566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
36669566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
3667605a06ccSksagiyam     PetscCall(VecGetBlockSize(coordinates, &bs));
3668605a06ccSksagiyam     PetscCall(VecSetBlockSize(subCoordinates, bs));
36699566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
36709566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
36719566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
36723e98c3d0Sksagiyam     for (d = 0; d <= sdim; ++d) {
36733e98c3d0Sksagiyam       for (p = 0; p < numSubPoints[d]; ++p) {
36743e98c3d0Sksagiyam         const PetscInt point    = subpoints[d][p];
36753e98c3d0Sksagiyam         const PetscInt subpoint = firstSubPoint[d] + p;
3676e6ccafaeSMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3677e6ccafaeSMatthew G Knepley 
36783e98c3d0Sksagiyam         if (point >= firstP && point < lastP) {
36793e98c3d0Sksagiyam           PetscCall(PetscSectionGetDof(coordSection, point, &dof));
36803e98c3d0Sksagiyam           PetscCall(PetscSectionGetOffset(coordSection, point, &off));
36813e98c3d0Sksagiyam           PetscCall(PetscSectionGetDof(subCoordSection, subpoint, &sdof));
36823e98c3d0Sksagiyam           PetscCall(PetscSectionGetOffset(subCoordSection, subpoint, &soff));
36833e98c3d0Sksagiyam           PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subpoint %" PetscInt_FMT ", point %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subpoint, point, dof);
3684efa14ee0SMatthew G Knepley           for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3685e6ccafaeSMatthew G Knepley         }
36863e98c3d0Sksagiyam       }
36873e98c3d0Sksagiyam     }
36889566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
36899566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
36903e98c3d0Sksagiyam     switch (coordinate_type) {
36913e98c3d0Sksagiyam     case 0:
36929566063dSJacob Faibussowitsch       PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
36933e98c3d0Sksagiyam       break;
36943e98c3d0Sksagiyam     case 1:
36953e98c3d0Sksagiyam       PetscCall(DMSetCellCoordinatesLocal(subdm, subCoordinates));
36963e98c3d0Sksagiyam       break;
36973e98c3d0Sksagiyam     default:
36983e98c3d0Sksagiyam       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "coordinate_type must be {0, 1}");
36993e98c3d0Sksagiyam     }
37009566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3701e6ccafaeSMatthew G Knepley   }
37023982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
370371f1c950SStefano Zampini   // TODO parallel subcommunicators
370471f1c950SStefano Zampini   PetscMPIInt flag;
370571f1c950SStefano Zampini   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm), PetscObjectComm((PetscObject)subdm), &flag));
370671f1c950SStefano Zampini   if (flag == MPI_CONGRUENT || flag == MPI_IDENT) {
37073982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
37083982b651SMatthew G. Knepley     IS                 subpIS;
37093982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
37105033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
37115033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3712dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
37135033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
37145033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
37153982b651SMatthew G. Knepley 
37169566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
37175033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
37189566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
37199566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
37209566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
37219566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
37229566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
37233982b651SMatthew G. Knepley     if (subpIS) {
3724dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3725dd05d810SMatthew G. Knepley 
37269566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
37279566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3728dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3729dd05d810SMatthew G. Knepley       if (!sorted) {
3730dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3731dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3732dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3733dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3734dd05d810SMatthew G. Knepley       }
37353982b651SMatthew G. Knepley     }
37369566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
37373982b651SMatthew G. Knepley     if (numRoots >= 0) {
37385033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
37395033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
37409566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
37413982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
37423982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
37433982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
37443982b651SMatthew G. Knepley       }
374530cbcd5dSksagiyam       for (p = pStart; p < pEnd; ++p) {
374630cbcd5dSksagiyam         newOwners[p - pStart].rank  = -3;
374730cbcd5dSksagiyam         newOwners[p - pStart].index = -3;
374830cbcd5dSksagiyam       }
374930cbcd5dSksagiyam       if (sanitizeSubmesh) {
375030cbcd5dSksagiyam         /* A subpoint is forced to be owned by a rank that owns */
375130cbcd5dSksagiyam         /* a subcell that contains the subpoint in its closure. */
375230cbcd5dSksagiyam         PetscInt  cStart, cEnd, c, clSize, cl;
375330cbcd5dSksagiyam         PetscInt *ownedCells, *closure = NULL;
375430cbcd5dSksagiyam 
375530cbcd5dSksagiyam         /* claim ownership */
375630cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
375730cbcd5dSksagiyam           const PetscInt point = subpoints[p];
375830cbcd5dSksagiyam 
375930cbcd5dSksagiyam           newLocalPoints[point - pStart].rank  = rank;
376030cbcd5dSksagiyam           newLocalPoints[point - pStart].index = p;
376130cbcd5dSksagiyam         }
376230cbcd5dSksagiyam         PetscCall(DMGetDimension(subdm, &sdim));
376330cbcd5dSksagiyam         PetscCall(DMPlexGetDepthStratum(dm, sdim, &cStart, &cEnd));
376430cbcd5dSksagiyam         PetscCall(PetscMalloc1(cEnd - cStart, &ownedCells));
376530cbcd5dSksagiyam         for (c = cStart; c < cEnd; ++c) ownedCells[c - cStart] = 0;
376630cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
376730cbcd5dSksagiyam           c = subpoints[p];
376830cbcd5dSksagiyam           if (c >= cStart && c < cEnd) ownedCells[c - cStart] = 1;
376930cbcd5dSksagiyam         }
377030cbcd5dSksagiyam         for (l = 0; l < numLeaves; ++l) {
377130cbcd5dSksagiyam           c = localPoints ? localPoints[l] : l;
377230cbcd5dSksagiyam           if (c >= cStart && c < cEnd) ownedCells[c - cStart] = 0;
377330cbcd5dSksagiyam         }
377430cbcd5dSksagiyam         for (c = cStart; c < cEnd; ++c) {
377530cbcd5dSksagiyam           if (ownedCells[c - cStart] == 0) continue;
377630cbcd5dSksagiyam           PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
377730cbcd5dSksagiyam           for (cl = 0; cl < clSize * 2; cl += 2) {
377830cbcd5dSksagiyam             p = closure[cl];
377930cbcd5dSksagiyam             if (newLocalPoints[p - pStart].rank < size) newLocalPoints[p - pStart].rank += size;
378030cbcd5dSksagiyam           }
378130cbcd5dSksagiyam           PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
378230cbcd5dSksagiyam         }
378330cbcd5dSksagiyam         PetscCall(PetscFree(ownedCells));
378430cbcd5dSksagiyam         for (p = 0; p < numRoots; ++p) {
378530cbcd5dSksagiyam           newOwners[p].rank  = newLocalPoints[p].rank;
378630cbcd5dSksagiyam           newOwners[p].index = newLocalPoints[p].index;
378730cbcd5dSksagiyam         }
378830cbcd5dSksagiyam       } else {
37893982b651SMatthew G. Knepley         /* Set subleaves */
37903982b651SMatthew G. Knepley         for (l = 0; l < numLeaves; ++l) {
37913982b651SMatthew G. Knepley           const PetscInt point    = localPoints[l];
3792dd05d810SMatthew G. Knepley           const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
37933982b651SMatthew G. Knepley 
37943982b651SMatthew G. Knepley           if (subpoint < 0) continue;
37953982b651SMatthew G. Knepley           newLocalPoints[point - pStart].rank  = rank;
37963982b651SMatthew G. Knepley           newLocalPoints[point - pStart].index = subpoint;
37973982b651SMatthew G. Knepley           ++numSubleaves;
37983982b651SMatthew G. Knepley         }
37993982b651SMatthew G. Knepley         /* Must put in owned subpoints */
3800d9384f27SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) {
38015033f954SMatthew G. Knepley           /* Hold on to currently owned points */
38025033f954SMatthew G. Knepley           if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
38035033f954SMatthew G. Knepley           else newOwners[subpoints[p] - pStart].rank = rank;
3804d9384f27SMatthew G. Knepley           newOwners[subpoints[p] - pStart].index = p;
38053982b651SMatthew G. Knepley         }
380630cbcd5dSksagiyam       }
38076497c311SBarry Smith       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
38086497c311SBarry Smith       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
38099371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
38109371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
3811fdb4e32bSksagiyam       if (ownershipTransferSF) {
3812fdb4e32bSksagiyam         PetscSFNode *iremote1 = NULL, *newOwners1 = NULL;
3813fdb4e32bSksagiyam         PetscInt    *ilocal1 = NULL;
3814fdb4e32bSksagiyam         PetscInt     nleaves1, point;
3815fdb4e32bSksagiyam 
3816fdb4e32bSksagiyam         for (p = 0; p < numSubpoints; ++p) {
3817fdb4e32bSksagiyam           point                                = subpoints[p];
3818fdb4e32bSksagiyam           newLocalPoints[point - pStart].index = point;
3819fdb4e32bSksagiyam         }
3820fdb4e32bSksagiyam         PetscCall(PetscMalloc1(numRoots, &newOwners1));
3821fdb4e32bSksagiyam         for (p = 0; p < numRoots; ++p) {
3822fdb4e32bSksagiyam           newOwners1[p].rank  = -1;
3823fdb4e32bSksagiyam           newOwners1[p].index = -1;
3824fdb4e32bSksagiyam         }
38256497c311SBarry Smith         PetscCall(PetscSFReduceBegin(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners1, MPI_MAXLOC));
38266497c311SBarry Smith         PetscCall(PetscSFReduceEnd(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners1, MPI_MAXLOC));
3827fdb4e32bSksagiyam         for (p = 0, nleaves1 = 0; p < numRoots; ++p) {
3828ac530a7eSPierre Jolivet           if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) ++nleaves1;
3829fdb4e32bSksagiyam         }
3830fdb4e32bSksagiyam         PetscCall(PetscMalloc1(nleaves1, &ilocal1));
3831fdb4e32bSksagiyam         PetscCall(PetscMalloc1(nleaves1, &iremote1));
3832fdb4e32bSksagiyam         for (p = 0, nleaves1 = 0; p < numRoots; ++p) {
3833fdb4e32bSksagiyam           if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) {
3834fdb4e32bSksagiyam             ilocal1[nleaves1]        = pStart + p;
3835fdb4e32bSksagiyam             iremote1[nleaves1].rank  = newOwners[p].rank;
3836fdb4e32bSksagiyam             iremote1[nleaves1].index = newOwners1[p].index;
3837fdb4e32bSksagiyam             ++nleaves1;
3838fdb4e32bSksagiyam           }
3839fdb4e32bSksagiyam         }
3840fdb4e32bSksagiyam         PetscCall(PetscFree(newOwners1));
3841fdb4e32bSksagiyam         PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)sfPoint), ownershipTransferSF));
3842fdb4e32bSksagiyam         PetscCall(PetscSFSetFromOptions(*ownershipTransferSF));
3843fdb4e32bSksagiyam         PetscCall(PetscSFSetGraph(*ownershipTransferSF, pEnd - pStart, nleaves1, ilocal1, PETSC_OWN_POINTER, iremote1, PETSC_OWN_POINTER));
3844fdb4e32bSksagiyam       }
384530cbcd5dSksagiyam       if (sanitizeSubmesh) {
384630cbcd5dSksagiyam         for (p = pStart; p < pEnd; ++p) {
384730cbcd5dSksagiyam           newLocalPoints[p - pStart].rank  = newOwners[p - pStart].rank;
384830cbcd5dSksagiyam           newLocalPoints[p - pStart].index = newOwners[p - pStart].index;
384930cbcd5dSksagiyam         }
385030cbcd5dSksagiyam       }
38516497c311SBarry Smith       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
38526497c311SBarry Smith       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
385330cbcd5dSksagiyam       if (sanitizeSubmesh) {
385430cbcd5dSksagiyam         for (p = 0; p < numSubpoints; ++p) {
385530cbcd5dSksagiyam           const PetscInt point = subpoints[p];
385630cbcd5dSksagiyam 
385730cbcd5dSksagiyam           if (newLocalPoints[point - pStart].rank >= 0 && newLocalPoints[point - pStart].rank != rank) ++sl;
385830cbcd5dSksagiyam         }
385930cbcd5dSksagiyam         PetscCall(PetscMalloc1(sl, &slocalPoints));
386030cbcd5dSksagiyam         PetscCall(PetscMalloc1(sl, &sremotePoints));
386130cbcd5dSksagiyam         for (p = 0, sl = 0; p < numSubpoints; ++p) {
386230cbcd5dSksagiyam           const PetscInt point = subpoints[p];
386330cbcd5dSksagiyam 
386430cbcd5dSksagiyam           if (newLocalPoints[point - pStart].rank >= 0 && newLocalPoints[point - pStart].rank != rank) {
386530cbcd5dSksagiyam             slocalPoints[sl]        = p;
386630cbcd5dSksagiyam             sremotePoints[sl].rank  = newLocalPoints[point - pStart].rank;
386730cbcd5dSksagiyam             sremotePoints[sl].index = newLocalPoints[point - pStart].index;
386830cbcd5dSksagiyam             PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank");
386930cbcd5dSksagiyam             PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint");
387030cbcd5dSksagiyam             ++sl;
387130cbcd5dSksagiyam           }
387230cbcd5dSksagiyam         }
387330cbcd5dSksagiyam       } else {
38749566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
38759566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
38765033f954SMatthew G. Knepley         for (l = 0; l < numLeaves; ++l) {
38773982b651SMatthew G. Knepley           const PetscInt point    = localPoints[l];
3878dd05d810SMatthew G. Knepley           const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
38793982b651SMatthew G. Knepley 
38803982b651SMatthew G. Knepley           if (subpoint < 0) continue;
38819371c9d4SSatish Balay           if (newLocalPoints[point].rank == rank) {
38829371c9d4SSatish Balay             ++ll;
38839371c9d4SSatish Balay             continue;
38849371c9d4SSatish Balay           }
38853982b651SMatthew G. Knepley           slocalPoints[sl]        = subpoint;
38863982b651SMatthew G. Knepley           sremotePoints[sl].rank  = newLocalPoints[point].rank;
38873982b651SMatthew G. Knepley           sremotePoints[sl].index = newLocalPoints[point].index;
388863a3b9bcSJacob Faibussowitsch           PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
388963a3b9bcSJacob Faibussowitsch           PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
38903982b651SMatthew G. Knepley           ++sl;
38913982b651SMatthew G. Knepley         }
38921dca8a05SBarry 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);
389330cbcd5dSksagiyam       }
38949566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
38959566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
38963982b651SMatthew G. Knepley     }
389748a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3898dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
38993982b651SMatthew G. Knepley   }
3900212103e5SMatthew Knepley   /* Filter labels */
39019566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3902efa14ee0SMatthew G Knepley   /* Cleanup */
3903dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
39049566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
39059566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3906e6ccafaeSMatthew G Knepley   }
39079566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
39083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3909e6ccafaeSMatthew G Knepley }
3910e6ccafaeSMatthew G Knepley 
DMPlexCreateSubmesh_Interpolated(DM dm,DMLabel vertexLabel,PetscInt value,PetscBool markedFaces,DM subdm)3911d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
3912d71ae5a4SJacob Faibussowitsch {
39133982b651SMatthew G. Knepley   PetscFunctionBegin;
391430cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, PETSC_FALSE, PETSC_FALSE, NULL, subdm));
39153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39163982b651SMatthew G. Knepley }
39173982b651SMatthew G. Knepley 
3918d0fa310fSMatthew G. Knepley /*@
3919e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3920e6ccafaeSMatthew G Knepley 
3921e6ccafaeSMatthew G Knepley   Input Parameters:
3922e6ccafaeSMatthew G Knepley + dm          - The original mesh
392320f4b53cSBarry Smith . vertexLabel - The `DMLabel` marking points contained in the surface
3924158acfadSMatthew G. Knepley . value       - The label value to use
392520f4b53cSBarry Smith - markedFaces - `PETSC_TRUE` if surface faces are marked in addition to vertices, `PETSC_FALSE` if only vertices are marked
3926e6ccafaeSMatthew G Knepley 
3927e6ccafaeSMatthew G Knepley   Output Parameter:
3928e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3929e6ccafaeSMatthew G Knepley 
3930e6ccafaeSMatthew G Knepley   Level: developer
3931e6ccafaeSMatthew G Knepley 
393220f4b53cSBarry Smith   Note:
393320f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
393420f4b53cSBarry Smith 
39351cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3936830e53efSMatthew G. Knepley @*/
DMPlexCreateSubmesh(DM dm,DMLabel vertexLabel,PetscInt value,PetscBool markedFaces,DM * subdm)3937d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3938d71ae5a4SJacob Faibussowitsch {
3939827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3940827c4036SVaclav Hapla   PetscInt               dim, cdim;
3941e6ccafaeSMatthew G Knepley 
3942e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3943e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39444f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
39459566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
39469566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
39479566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
39489566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
39499566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
39509566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
39519566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
395208401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3953827c4036SVaclav Hapla   if (interpolated) {
39549566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3955e6ccafaeSMatthew G Knepley   } else {
39569566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3957e6ccafaeSMatthew G Knepley   }
39585de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
39593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3960e6ccafaeSMatthew G Knepley }
3961e6ccafaeSMatthew G Knepley 
DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm,PetscBool hasLagrange,const char label[],PetscInt value,DM subdm)3962d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3963d71ae5a4SJacob Faibussowitsch {
3964766ab985SMatthew G. Knepley   MPI_Comm        comm;
3965766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3966766ab985SMatthew G. Knepley   IS              subvertexIS;
3967766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3968fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3969766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3970412e9a14SMatthew G. Knepley   PetscInt        c, f;
3971766ab985SMatthew G. Knepley 
3972766ab985SMatthew G. Knepley   PetscFunctionBegin;
39739566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3974766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
39759566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
39769566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
39779566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3978766ab985SMatthew G. Knepley   /* Setup chart */
39799566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
39809566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
39819566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
39829566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3983766ab985SMatthew G. Knepley   /* Set cone sizes */
3984766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3985766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3986766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
39879566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
39889566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
398948a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
399048a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
39919566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3992d24a712aSStefano Zampini   PetscCall(DMLabelDestroy(&subpointMap));
3993766ab985SMatthew G. Knepley   /* Create face cones */
39949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
39959566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3996766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3997766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3998766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
399987feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
4000064cae4fSPierre Jolivet     PetscBool       isHybrid = PETSC_FALSE;
400187feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
4002766ab985SMatthew G. Knepley 
40039566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
4004412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
40059566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
400687feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
40079566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
400887feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
400987feddfdSMatthew G. Knepley     }
40109566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
40119566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
40129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
401327234c99SMatthew G. Knepley     /* Not true in parallel
401408401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
401587feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
401687feddfdSMatthew G. Knepley       PetscInt  negsubcell;
4017064cae4fSPierre Jolivet       PetscBool isHybrid = PETSC_FALSE;
4018766ab985SMatthew G. Knepley 
40199566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
4020412e9a14SMatthew G. Knepley       if (isHybrid) continue;
402187feddfdSMatthew G. Knepley       /* I know this is a crap search */
402287feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
402387feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
4024766ab985SMatthew G. Knepley       }
402563a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
40269566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
4027766ab985SMatthew G. Knepley     }
40289566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
402987feddfdSMatthew G. Knepley     ++newFacePoint;
4030766ab985SMatthew G. Knepley   }
40319566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
40329566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
40339566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
4034766ab985SMatthew G. Knepley   /* Build coordinates */
4035766ab985SMatthew G. Knepley   {
4036766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
4037766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
4038766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
4039c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
404024640c55SToby Isaac     const char  *name;
4041766ab985SMatthew G. Knepley 
40429566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
40439566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
40449566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
40459566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
40469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
40479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
40489566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
40499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
4050766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
4051766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
4052766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
4053766ab985SMatthew G. Knepley       PetscInt       dof;
4054766ab985SMatthew G. Knepley 
40559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
40569566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
40579566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
4058766ab985SMatthew G. Knepley     }
40599566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
40609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
40619566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
40629566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
40639566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
40649566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
40659566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
40669566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
40679566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
40689566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
4069766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
4070766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
4071766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
4072766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
4073766ab985SMatthew G. Knepley 
40749566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
40759566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
40769566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
40779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
407863a3b9bcSJacob 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);
4079766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
4080766ab985SMatthew G. Knepley     }
40819566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
40829566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
40839566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
40849566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
4085766ab985SMatthew G. Knepley   }
4086aca35d17SMatthew G. Knepley   /* Build SF */
4087aca35d17SMatthew G. Knepley   CHKMEMQ;
4088aca35d17SMatthew G. Knepley   {
4089aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
4090aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
4091bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
4092aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
4093bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
409449c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
4095bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
4096aca35d17SMatthew G. Knepley 
40979566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
40989566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
40999566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
41009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
41019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
41029566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
4103aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
4104aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
41059566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
4106bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
4107bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
4108bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
4109bdcf2095SMatthew G. Knepley       }
41109e0823b2SMatthew G. Knepley       /* Set subleaves */
4111aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
4112aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
4113bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
4114aca35d17SMatthew G. Knepley 
411563a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
4116bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
4117bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
4118bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
4119bdcf2095SMatthew G. Knepley         ++numSubLeaves;
4120aca35d17SMatthew G. Knepley       }
41219e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
41229e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
41239e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
41249e0823b2SMatthew G. Knepley 
41259e0823b2SMatthew G. Knepley         if (subPoint < 0) {
41269e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
41279e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
41289e0823b2SMatthew G. Knepley         } else {
41299e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
41309e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
41319e0823b2SMatthew G. Knepley         }
4132bdcf2095SMatthew G. Knepley       }
41336497c311SBarry Smith       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
41346497c311SBarry Smith       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_SF_NODE, newLocalPoints, newOwners, MPI_MAXLOC));
41356497c311SBarry Smith       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
41366497c311SBarry Smith       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_SF_NODE, newOwners, newLocalPoints, MPI_REPLACE));
41379566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
41389566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
413949c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
4140bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
4141bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
4142aca35d17SMatthew G. Knepley 
4143aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
41449371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
41459371c9d4SSatish Balay           ++ll;
41469371c9d4SSatish Balay           continue;
41479371c9d4SSatish Balay         }
4148aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
4149bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
4150bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
415163a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
415263a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
4153aca35d17SMatthew G. Knepley         ++sl;
4154aca35d17SMatthew G. Knepley       }
41559566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
41561dca8a05SBarry 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);
41579566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
4158aca35d17SMatthew G. Knepley     }
4159aca35d17SMatthew G. Knepley   }
4160aca35d17SMatthew G. Knepley   CHKMEMQ;
4161766ab985SMatthew G. Knepley   /* Cleanup */
41629566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
41639566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
41649566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
41653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4166766ab985SMatthew G. Knepley }
4167766ab985SMatthew G. Knepley 
DMPlexCreateCohesiveSubmesh_Interpolated(DM dm,const char labelname[],PetscInt value,DM subdm)4168d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
4169d71ae5a4SJacob Faibussowitsch {
41703982b651SMatthew G. Knepley   DMLabel label = NULL;
4171766ab985SMatthew G. Knepley 
4172766ab985SMatthew G. Knepley   PetscFunctionBegin;
41739566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
417430cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, PETSC_FALSE, PETSC_FALSE, NULL, subdm));
41753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4176766ab985SMatthew G. Knepley }
4177766ab985SMatthew G. Knepley 
4178cc4c1da9SBarry Smith /*@
417920f4b53cSBarry 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.
4180766ab985SMatthew G. Knepley 
4181766ab985SMatthew G. Knepley   Input Parameters:
4182766ab985SMatthew G. Knepley + dm          - The original mesh
418327c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
418420f4b53cSBarry Smith . label       - A label name, or `NULL`
418527c04023SMatthew G. Knepley - value       - A label value
4186766ab985SMatthew G. Knepley 
4187766ab985SMatthew G. Knepley   Output Parameter:
4188766ab985SMatthew G. Knepley . subdm - The surface mesh
4189766ab985SMatthew G. Knepley 
4190766ab985SMatthew G. Knepley   Level: developer
4191766ab985SMatthew G. Knepley 
419220f4b53cSBarry Smith   Note:
419320f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
419420f4b53cSBarry Smith 
41951cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
4196c08575a3SMatthew G. Knepley @*/
DMPlexCreateCohesiveSubmesh(DM dm,PetscBool hasLagrange,const char label[],PetscInt value,DM * subdm)4197d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
4198d71ae5a4SJacob Faibussowitsch {
4199c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
4200766ab985SMatthew G. Knepley 
4201766ab985SMatthew G. Knepley   PetscFunctionBegin;
4202766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42034f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
42049566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
42059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
42069566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
42079566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
42089566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
42099566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
42109566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
4211766ab985SMatthew G. Knepley   if (depth == dim) {
42129566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
4213766ab985SMatthew G. Knepley   } else {
42149566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
4215e6ccafaeSMatthew G Knepley   }
42165de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
42173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4218e6ccafaeSMatthew G Knepley }
4219e6ccafaeSMatthew G Knepley 
4220bec263e5SMatthew G. Knepley /*@
42210e18dc48SMatthew G. Knepley   DMPlexReorderCohesiveSupports - Ensure that face supports for cohesive end caps are ordered
42220e18dc48SMatthew G. Knepley 
42230e18dc48SMatthew G. Knepley   Not Collective
42240e18dc48SMatthew G. Knepley 
42250e18dc48SMatthew G. Knepley   Input Parameter:
42260e18dc48SMatthew G. Knepley . dm - The `DM` containing cohesive cells
42270e18dc48SMatthew G. Knepley 
42280e18dc48SMatthew G. Knepley   Level: developer
42290e18dc48SMatthew G. Knepley 
4230a4e35b19SJacob Faibussowitsch   Note:
4231a4e35b19SJacob Faibussowitsch   For the negative size (first) face, the cohesive cell should be first in the support, and for
4232a4e35b19SJacob Faibussowitsch   the positive side (second) face, the cohesive cell should be second in the support.
42330e18dc48SMatthew G. Knepley 
42340e18dc48SMatthew G. Knepley .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexCreateCohesiveSubmesh()`
42350e18dc48SMatthew G. Knepley @*/
DMPlexReorderCohesiveSupports(DM dm)42360e18dc48SMatthew G. Knepley PetscErrorCode DMPlexReorderCohesiveSupports(DM dm)
42370e18dc48SMatthew G. Knepley {
42380e18dc48SMatthew G. Knepley   PetscInt dim, cStart, cEnd;
42390e18dc48SMatthew G. Knepley 
42400e18dc48SMatthew G. Knepley   PetscFunctionBegin;
42410e18dc48SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
42420e18dc48SMatthew G. Knepley   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cStart, &cEnd));
42430e18dc48SMatthew G. Knepley   for (PetscInt c = cStart; c < cEnd; ++c) {
42440e18dc48SMatthew G. Knepley     const PetscInt *cone;
42450e18dc48SMatthew G. Knepley     PetscInt        coneSize;
42460e18dc48SMatthew G. Knepley 
42470e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
42480e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetCone(dm, c, &cone));
42490e18dc48SMatthew G. Knepley     PetscCheck(coneSize >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid cell %" PetscInt_FMT " cone size %" PetscInt_FMT " must be >= 2", c, coneSize);
42500e18dc48SMatthew G. Knepley     for (PetscInt s = 0; s < 2; ++s) {
42510e18dc48SMatthew G. Knepley       const PetscInt *supp;
42520e18dc48SMatthew G. Knepley       PetscInt        suppSize, newsupp[2];
42530e18dc48SMatthew G. Knepley 
42540e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupportSize(dm, cone[s], &suppSize));
42550e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupport(dm, cone[s], &supp));
42560e18dc48SMatthew G. Knepley       if (suppSize == 2) {
42570e18dc48SMatthew G. Knepley         /* Reorder hybrid end cap support */
42580e18dc48SMatthew G. Knepley         if (supp[s] == c) {
42590e18dc48SMatthew G. Knepley           newsupp[0] = supp[1];
42600e18dc48SMatthew G. Knepley           newsupp[1] = supp[0];
42610e18dc48SMatthew G. Knepley         } else {
42620e18dc48SMatthew G. Knepley           newsupp[0] = supp[0];
42630e18dc48SMatthew G. Knepley           newsupp[1] = supp[1];
42640e18dc48SMatthew G. Knepley         }
42650e18dc48SMatthew 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);
42660e18dc48SMatthew G. Knepley         PetscCall(DMPlexSetSupport(dm, cone[s], newsupp));
42670e18dc48SMatthew G. Knepley       }
42680e18dc48SMatthew G. Knepley     }
42690e18dc48SMatthew G. Knepley   }
42700e18dc48SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42710e18dc48SMatthew G. Knepley }
42720e18dc48SMatthew G. Knepley 
42730e18dc48SMatthew G. Knepley /*@
4274bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
4275bec263e5SMatthew G. Knepley 
4276bec263e5SMatthew G. Knepley   Input Parameters:
4277bec263e5SMatthew G. Knepley + dm              - The original mesh
427820f4b53cSBarry Smith . cellLabel       - The `DMLabel` marking cells contained in the new mesh
42794d00ad26Sksagiyam . value           - The label value to use
428030cbcd5dSksagiyam . ignoreLabelHalo - The flag indicating if labeled points that are in the halo are ignored
428171f1c950SStefano Zampini . sanitizeSubmesh - The flag indicating if a subpoint is forced to be owned by a rank that owns a subcell that contains that point in its closure
428271f1c950SStefano Zampini - comm            - The communicator you want the mesh on (currently supports only a sequential communicator or the same communicator of the original mesh)
4283bec263e5SMatthew G. Knepley 
42849cde84edSJose E. Roman   Output Parameters:
4285fdb4e32bSksagiyam + ownershipTransferSF - The `PetscSF` representing the ownership transfers between parent local meshes due to submeshing.
4286fdb4e32bSksagiyam - subdm               - The new mesh
4287bec263e5SMatthew G. Knepley 
4288bec263e5SMatthew G. Knepley   Level: developer
4289bec263e5SMatthew G. Knepley 
429020f4b53cSBarry Smith   Note:
429120f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
429220f4b53cSBarry Smith 
4293fdb4e32bSksagiyam   On a given rank, the leaves of the ownershipTransferSF are the points in the local mesh that this rank gives up ownership of (in the submesh), and
4294fdb4e32bSksagiyam   the remote locations for these leaves are tuples of rank and point that represent the new owners.
4295fdb4e32bSksagiyam 
42961cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
4297bec263e5SMatthew G. Knepley @*/
DMPlexFilter(DM dm,DMLabel cellLabel,PetscInt value,PetscBool ignoreLabelHalo,PetscBool sanitizeSubmesh,MPI_Comm comm,PetscSF * ownershipTransferSF,DM * subdm)429871f1c950SStefano Zampini PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, PetscBool ignoreLabelHalo, PetscBool sanitizeSubmesh, MPI_Comm comm, PetscSF *ownershipTransferSF, DM *subdm)
4299d71ae5a4SJacob Faibussowitsch {
4300b47cd10cSMatthew G. Knepley   PetscInt dim, overlap;
4301bec263e5SMatthew G. Knepley 
4302bec263e5SMatthew G. Knepley   PetscFunctionBegin;
4303bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
430471f1c950SStefano Zampini   if (ownershipTransferSF) PetscAssertPointer(ownershipTransferSF, 7);
430571f1c950SStefano Zampini   PetscAssertPointer(subdm, 8);
43069566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
430771f1c950SStefano Zampini   PetscCall(DMCreate(comm, subdm));
43089566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
4309bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
431030cbcd5dSksagiyam   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, ignoreLabelHalo, sanitizeSubmesh, ownershipTransferSF, *subdm));
43115de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
4312b47cd10cSMatthew G. Knepley   // It is possible to obtain a surface mesh where some faces are in SF
4313b47cd10cSMatthew G. Knepley   //   We should either mark the mesh as having an overlap, or delete these from the SF
4314b47cd10cSMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
4315b47cd10cSMatthew G. Knepley   if (!overlap) {
4316b47cd10cSMatthew G. Knepley     PetscSF         sf;
4317b47cd10cSMatthew G. Knepley     const PetscInt *leaves;
4318b47cd10cSMatthew G. Knepley     PetscInt        cStart, cEnd, Nl;
4319b47cd10cSMatthew G. Knepley     PetscBool       hasSubcell = PETSC_FALSE, ghasSubcell;
4320b47cd10cSMatthew G. Knepley 
4321b47cd10cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd));
4322b47cd10cSMatthew G. Knepley     PetscCall(DMGetPointSF(*subdm, &sf));
4323b47cd10cSMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
4324b47cd10cSMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
4325b47cd10cSMatthew G. Knepley       const PetscInt point = leaves ? leaves[l] : l;
4326b47cd10cSMatthew G. Knepley 
4327b47cd10cSMatthew G. Knepley       if (point >= cStart && point < cEnd) {
4328b47cd10cSMatthew G. Knepley         hasSubcell = PETSC_TRUE;
4329b47cd10cSMatthew G. Knepley         break;
4330b47cd10cSMatthew G. Knepley       }
4331b47cd10cSMatthew G. Knepley     }
43325440e5dcSBarry Smith     PetscCallMPI(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
4333b47cd10cSMatthew G. Knepley     if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1));
4334b47cd10cSMatthew G. Knepley   }
43353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4336bec263e5SMatthew G. Knepley }
4337bec263e5SMatthew G. Knepley 
433864beef6dSMatthew G. Knepley /*@
433920f4b53cSBarry Smith   DMPlexGetSubpointMap - Returns a `DMLabel` with point dimension as values
434064beef6dSMatthew G. Knepley 
434164beef6dSMatthew G. Knepley   Input Parameter:
434220f4b53cSBarry Smith . dm - The submesh `DM`
434364beef6dSMatthew G. Knepley 
434464beef6dSMatthew G. Knepley   Output Parameter:
434520f4b53cSBarry Smith . subpointMap - The `DMLabel` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
434664beef6dSMatthew G. Knepley 
434764beef6dSMatthew G. Knepley   Level: developer
434864beef6dSMatthew G. Knepley 
43491cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
435064beef6dSMatthew G. Knepley @*/
DMPlexGetSubpointMap(DM dm,DMLabel * subpointMap)4351d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
4352d71ae5a4SJacob Faibussowitsch {
4353e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4354e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43554f572ea9SToby Isaac   PetscAssertPointer(subpointMap, 2);
435665663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
43573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4358e6ccafaeSMatthew G Knepley }
4359e6ccafaeSMatthew G Knepley 
4360c08575a3SMatthew G. Knepley /*@
436120f4b53cSBarry Smith   DMPlexSetSubpointMap - Sets the `DMLabel` with point dimension as values
4362c08575a3SMatthew G. Knepley 
4363c08575a3SMatthew G. Knepley   Input Parameters:
436420f4b53cSBarry Smith + dm          - The submesh `DM`
436520f4b53cSBarry Smith - subpointMap - The `DMLabel` of all the points from the original mesh in this submesh
4366c08575a3SMatthew G. Knepley 
4367c08575a3SMatthew G. Knepley   Level: developer
4368c08575a3SMatthew G. Knepley 
436920f4b53cSBarry Smith   Note:
437020f4b53cSBarry Smith   Should normally not be called by the user, since it is set in `DMPlexCreateSubmesh()`
437120f4b53cSBarry Smith 
43721cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
4373c08575a3SMatthew G. Knepley @*/
DMPlexSetSubpointMap(DM dm,DMLabel subpointMap)4374d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
4375d71ae5a4SJacob Faibussowitsch {
4376e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
4377285d324eSMatthew G. Knepley   DMLabel  tmp;
4378e6ccafaeSMatthew G Knepley 
4379e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4380e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4381285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
4382e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
43839566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
43849566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
43853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4386e6ccafaeSMatthew G Knepley }
4387e6ccafaeSMatthew G Knepley 
DMPlexCreateSubpointIS_Internal(DM dm,IS * subpointIS)4388d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
4389d71ae5a4SJacob Faibussowitsch {
439097d8846cSMatthew Knepley   DMLabel  spmap;
439197d8846cSMatthew Knepley   PetscInt depth, d;
439297d8846cSMatthew Knepley 
439397d8846cSMatthew Knepley   PetscFunctionBegin;
43949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
43959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
439697d8846cSMatthew Knepley   if (spmap && depth >= 0) {
439797d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
439897d8846cSMatthew Knepley     PetscInt *points, *depths;
439997d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
440097d8846cSMatthew Knepley 
44019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
440263a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
44039566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
44049566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
440597d8846cSMatthew Knepley     depths[0] = depth;
440697d8846cSMatthew Knepley     depths[1] = 0;
4407ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
440897d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
440997d8846cSMatthew Knepley       const PetscInt dep = depths[d];
441097d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
441197d8846cSMatthew Knepley 
44129566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
44139566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
4414b6555650SPierre Jolivet       if ((d < 2 && depth > 1) || d == 1) { /* Only check vertices and cells for now since the map is broken for others */
441563a3b9bcSJacob 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);
441697d8846cSMatthew Knepley       } else {
441797d8846cSMatthew Knepley         if (!n) {
441897d8846cSMatthew Knepley           if (d == 0) {
441997d8846cSMatthew Knepley             /* Missing cells */
442097d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
442197d8846cSMatthew Knepley           } else {
442297d8846cSMatthew Knepley             /* Missing faces */
44231690c2aeSBarry Smith             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_INT_MAX;
442497d8846cSMatthew Knepley           }
442597d8846cSMatthew Knepley         }
442697d8846cSMatthew Knepley       }
442797d8846cSMatthew Knepley       if (n) {
442897d8846cSMatthew Knepley         IS              is;
442997d8846cSMatthew Knepley         const PetscInt *opoints;
443097d8846cSMatthew Knepley 
44319566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
44329566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
443397d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
44349566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
44359566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
443697d8846cSMatthew Knepley       }
443797d8846cSMatthew Knepley     }
44389566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
443963a3b9bcSJacob 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);
44409566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
44419566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
444297d8846cSMatthew Knepley   }
44433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
444497d8846cSMatthew Knepley }
444597d8846cSMatthew Knepley 
444664beef6dSMatthew G. Knepley /*@
444720f4b53cSBarry Smith   DMPlexGetSubpointIS - Returns an `IS` covering the entire subdm chart with the original points as data
4448e6ccafaeSMatthew G Knepley 
4449e6ccafaeSMatthew G Knepley   Input Parameter:
445020f4b53cSBarry Smith . dm - The submesh `DM`
4451e6ccafaeSMatthew G Knepley 
4452e6ccafaeSMatthew G Knepley   Output Parameter:
445320f4b53cSBarry Smith . subpointIS - The `IS` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
445464beef6dSMatthew G. Knepley 
445564beef6dSMatthew G. Knepley   Level: developer
445664beef6dSMatthew G. Knepley 
445720f4b53cSBarry Smith   Note:
445829d2e75bSMatthew G. Knepley   This `IS` is guaranteed to be sorted by the construction of the submesh. However, if the filtering operation removes an entire stratum, then the strata in the submesh can be in a different order, and the `subpointIS` will only be sorted within each stratum.
445920f4b53cSBarry Smith 
44601cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
446164beef6dSMatthew G. Knepley @*/
DMPlexGetSubpointIS(DM dm,IS * subpointIS)4462d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
4463d71ae5a4SJacob Faibussowitsch {
446497d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
446597d8846cSMatthew Knepley   DMLabel          spmap;
446697d8846cSMatthew Knepley   PetscObjectState state;
4467e6ccafaeSMatthew G Knepley 
4468e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4469e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44704f572ea9SToby Isaac   PetscAssertPointer(subpointIS, 2);
44719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
44729566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
44739566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
447497d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
44753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4476e6ccafaeSMatthew G Knepley }
4477559a1558SMatthew G. Knepley 
4478559a1558SMatthew G. Knepley /*@
447920f4b53cSBarry Smith   DMGetEnclosureRelation - Get the relationship between `dmA` and `dmB`
4480559a1558SMatthew G. Knepley 
4481559a1558SMatthew G. Knepley   Input Parameters:
448220f4b53cSBarry Smith + dmA - The first `DM`
448320f4b53cSBarry Smith - dmB - The second `DM`
4484559a1558SMatthew G. Knepley 
4485559a1558SMatthew G. Knepley   Output Parameter:
448620f4b53cSBarry Smith . rel - The relation of `dmA` to `dmB`
4487559a1558SMatthew G. Knepley 
4488a6e0b375SMatthew G. Knepley   Level: intermediate
4489559a1558SMatthew G. Knepley 
44901cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosurePoint()`
4491559a1558SMatthew G. Knepley @*/
DMGetEnclosureRelation(DM dmA,DM dmB,DMEnclosureType * rel)4492d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4493d71ae5a4SJacob Faibussowitsch {
4494a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
4495559a1558SMatthew G. Knepley   DMLabel  spmap;
4496a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4497559a1558SMatthew G. Knepley 
449844171101SMatthew G. Knepley   PetscFunctionBegin;
44994f572ea9SToby Isaac   PetscAssertPointer(rel, 3);
4500a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
45013ba16761SJacob Faibussowitsch   if (!dmA || !dmB) PetscFunctionReturn(PETSC_SUCCESS);
4502a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4503064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
45049371c9d4SSatish Balay   if (dmA == dmB) {
45059371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
45063ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
45079371c9d4SSatish Balay   }
45089566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
45099566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
45109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
45119566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4512a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4513a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4514b6555650SPierre Jolivet   if (pStartA == pStartB && pEndA == pEndB) {
4515a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4516a6e0b375SMatthew G. Knepley     goto end;
4517559a1558SMatthew G. Knepley   }
4518a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4519a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4520a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4521a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
45229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4523a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4524a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4525a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4526a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4527a6e0b375SMatthew G. Knepley   } else {
4528a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4529a6e0b375SMatthew G. Knepley   }
4530a6e0b375SMatthew G. Knepley end:
45319566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
45329566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
45333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4534559a1558SMatthew G. Knepley }
453544171101SMatthew G. Knepley 
453644171101SMatthew G. Knepley /*@
453720f4b53cSBarry Smith   DMGetEnclosurePoint - Get the point `pA` in `dmA` which corresponds to the point `pB` in `dmB`
453844171101SMatthew G. Knepley 
453944171101SMatthew G. Knepley   Input Parameters:
454020f4b53cSBarry Smith + dmA   - The first `DM`
454120f4b53cSBarry Smith . dmB   - The second `DM`
454220f4b53cSBarry Smith . etype - The type of enclosure relation that `dmA` has to `dmB`
454320f4b53cSBarry Smith - pB    - A point of `dmB`
454444171101SMatthew G. Knepley 
454544171101SMatthew G. Knepley   Output Parameter:
454620f4b53cSBarry Smith . pA - The corresponding point of `dmA`
454744171101SMatthew G. Knepley 
4548a6e0b375SMatthew G. Knepley   Level: intermediate
454944171101SMatthew G. Knepley 
45501cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosureRelation()`
455144171101SMatthew G. Knepley @*/
DMGetEnclosurePoint(DM dmA,DM dmB,DMEnclosureType etype,PetscInt pB,PetscInt * pA)4552d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
4553d71ae5a4SJacob Faibussowitsch {
4554a6e0b375SMatthew G. Knepley   DM              sdm;
4555a6e0b375SMatthew G. Knepley   IS              subpointIS;
4556a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4557a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
455844171101SMatthew G. Knepley 
455944171101SMatthew G. Knepley   PetscFunctionBegin;
4560a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4561a6e0b375SMatthew G. Knepley   switch (etype) {
4562a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4563a6e0b375SMatthew G. Knepley     sdm = dmB;
45649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
45659566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4566a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
45679566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4568a6e0b375SMatthew G. Knepley     break;
4569a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4570a6e0b375SMatthew G. Knepley     sdm = dmA;
45719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
45729566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
45739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
45749566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4575a6e0b375SMatthew G. Knepley     if (*pA < 0) {
45769566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
45779566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
457863a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4579a6e0b375SMatthew G. Knepley     }
45809566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4581a6e0b375SMatthew G. Knepley     break;
4582a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
4583d71ae5a4SJacob Faibussowitsch   case DM_ENC_NONE:
4584d71ae5a4SJacob Faibussowitsch     *pA = pB;
4585d71ae5a4SJacob Faibussowitsch     break;
45869371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4587a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
458844171101SMatthew G. Knepley 
45899566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
45909566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
45919371c9d4SSatish Balay   } break;
4592d71ae5a4SJacob Faibussowitsch   default:
4593d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
459444171101SMatthew G. Knepley   }
45953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
459644171101SMatthew G. Knepley }
4597