xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision a4e35b1925eceef64945ea472b84f2bf06a67b5e)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>  /*I      "petscdmplex.h"    I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I      "petscdmlabel.h"   I*/
30c312b8eSJed Brown #include <petscsf.h>
4e6ccafaeSMatthew G Knepley 
5d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCellIsHybrid_Internal(DM dm, PetscInt p, PetscBool *isHybrid)
6d71ae5a4SJacob Faibussowitsch {
7412e9a14SMatthew G. Knepley   DMPolytopeType ct;
8412e9a14SMatthew G. Knepley 
9412e9a14SMatthew G. Knepley   PetscFunctionBegin;
109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, p, &ct));
11412e9a14SMatthew G. Knepley   switch (ct) {
12412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
13412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
14412e9a14SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR:
15d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
16d71ae5a4SJacob Faibussowitsch     *isHybrid = PETSC_TRUE;
17d71ae5a4SJacob Faibussowitsch     break;
18d71ae5a4SJacob Faibussowitsch   default:
19d71ae5a4SJacob Faibussowitsch     *isHybrid = PETSC_FALSE;
20d71ae5a4SJacob Faibussowitsch     break;
21412e9a14SMatthew G. Knepley   }
223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23412e9a14SMatthew G. Knepley }
24412e9a14SMatthew G. Knepley 
25d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetTensorPrismBounds_Internal(DM dm, PetscInt dim, PetscInt *cStart, PetscInt *cEnd)
26d71ae5a4SJacob Faibussowitsch {
27412e9a14SMatthew G. Knepley   DMLabel ctLabel;
28412e9a14SMatthew G. Knepley 
29412e9a14SMatthew G. Knepley   PetscFunctionBegin;
30412e9a14SMatthew G. Knepley   if (cStart) *cStart = -1;
31412e9a14SMatthew G. Knepley   if (cEnd) *cEnd = -1;
329566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
33412e9a14SMatthew G. Knepley   switch (dim) {
34d71ae5a4SJacob Faibussowitsch   case 1:
35d71ae5a4SJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_POINT_PRISM_TENSOR, cStart, cEnd));
36d71ae5a4SJacob Faibussowitsch     break;
37d71ae5a4SJacob Faibussowitsch   case 2:
38d71ae5a4SJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_SEG_PRISM_TENSOR, cStart, cEnd));
39d71ae5a4SJacob Faibussowitsch     break;
40412e9a14SMatthew G. Knepley   case 3:
419566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_TRI_PRISM_TENSOR, cStart, cEnd));
429566063dSJacob Faibussowitsch     if (*cStart < 0) PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_QUAD_PRISM_TENSOR, cStart, cEnd));
43412e9a14SMatthew G. Knepley     break;
44d71ae5a4SJacob Faibussowitsch   default:
453ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
46412e9a14SMatthew G. Knepley   }
473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48412e9a14SMatthew G. Knepley }
49412e9a14SMatthew G. Knepley 
50d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label)
51d71ae5a4SJacob Faibussowitsch {
52429fa399SMatthew G. Knepley   PetscSF         sf;
53429fa399SMatthew G. Knepley   const PetscInt *rootdegree, *leaves;
54429fa399SMatthew G. Knepley   PetscInt        overlap, Nr = -1, Nl, pStart, fStart, fEnd;
5530560a7bSMatthew G. Knepley 
5630560a7bSMatthew G. Knepley   PetscFunctionBegin;
57429fa399SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &sf));
58429fa399SMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
59429fa399SMatthew G. Knepley   if (sf && !overlap) PetscCall(PetscSFGetGraph(sf, &Nr, &Nl, &leaves, NULL));
60429fa399SMatthew G. Knepley   if (Nr > 0) {
61429fa399SMatthew G. Knepley     PetscCall(PetscSFComputeDegreeBegin(sf, &rootdegree));
62429fa399SMatthew G. Knepley     PetscCall(PetscSFComputeDegreeEnd(sf, &rootdegree));
63429fa399SMatthew G. Knepley   } else rootdegree = NULL;
64429fa399SMatthew G. Knepley   PetscCall(DMPlexGetChart(dm, &pStart, NULL));
659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight + 1, &fStart, &fEnd));
66429fa399SMatthew G. Knepley   for (PetscInt f = fStart; f < fEnd; ++f) {
67429fa399SMatthew G. Knepley     PetscInt supportSize, loc = -1;
6830560a7bSMatthew G. Knepley 
699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
70e752be1aSMatthew G. Knepley     if (supportSize == 1) {
71429fa399SMatthew G. Knepley       /* Do not mark faces which are shared, meaning
72429fa399SMatthew G. Knepley            they are  present in the pointSF, or
73429fa399SMatthew G. Knepley            they have rootdegree > 0
74429fa399SMatthew G. Knepley          since they presumably have cells on the other side */
75429fa399SMatthew G. Knepley       if (Nr > 0) {
76429fa399SMatthew G. Knepley         PetscCall(PetscFindInt(f, Nl, leaves, &loc));
77429fa399SMatthew G. Knepley         if (rootdegree[f - pStart] || loc >= 0) continue;
78429fa399SMatthew G. Knepley       }
79e752be1aSMatthew G. Knepley       if (val < 0) {
80e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
81e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
82e752be1aSMatthew G. Knepley 
839566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
84e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize * 2; cl += 2) {
859566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, closure[cl], &cval));
86e752be1aSMatthew G. Knepley           if (cval < 0) continue;
879566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, cval));
88e752be1aSMatthew G. Knepley           break;
89e752be1aSMatthew G. Knepley         }
909566063dSJacob Faibussowitsch         if (cl == clSize * 2) PetscCall(DMLabelSetValue(label, f, 1));
919566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
92e752be1aSMatthew G. Knepley       } else {
939566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, f, val));
94e752be1aSMatthew G. Knepley       }
95e752be1aSMatthew G. Knepley     }
9630560a7bSMatthew G. Knepley   }
973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9830560a7bSMatthew G. Knepley }
9930560a7bSMatthew G. Knepley 
100cd0c2139SMatthew G Knepley /*@
101cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
102cd0c2139SMatthew G Knepley 
103cd0c2139SMatthew G Knepley   Not Collective
104cd0c2139SMatthew G Knepley 
105d8d19677SJose E. Roman   Input Parameters:
106429fa399SMatthew G. Knepley + dm  - The original `DM`
10720f4b53cSBarry Smith - val - The marker value, or `PETSC_DETERMINE` to use some value in the closure (or 1 if none are found)
108cd0c2139SMatthew G Knepley 
109cd0c2139SMatthew G Knepley   Output Parameter:
11020f4b53cSBarry Smith . label - The `DMLabel` marking boundary faces with the given value
111429fa399SMatthew G. Knepley 
112cd0c2139SMatthew G Knepley   Level: developer
113cd0c2139SMatthew G Knepley 
11420f4b53cSBarry Smith   Note:
11520f4b53cSBarry Smith   This function will use the point `PetscSF` from the input `DM` to exclude points on the partition boundary from being marked, unless the partition overlap is greater than zero. 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.
11620f4b53cSBarry Smith 
1171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLabelCreate()`, `DMCreateLabel()`
11809f723d9SJed Brown @*/
119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
120d71ae5a4SJacob Faibussowitsch {
121827c4036SVaclav Hapla   DMPlexInterpolatedFlag flg;
122cd0c2139SMatthew G Knepley 
123cd0c2139SMatthew G Knepley   PetscFunctionBegin;
124827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1259566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &flg));
12608401ef6SPierre Jolivet   PetscCheck(flg == DMPLEX_INTERPOLATED_FULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
1279566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces_Internal(dm, val, 0, label));
1283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
129cd0c2139SMatthew G Knepley }
130cd0c2139SMatthew G Knepley 
131d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
132d71ae5a4SJacob Faibussowitsch {
133b0bf5782SToby Isaac   IS              valueIS;
134ac51f24eSSander Arens   PetscSF         sfPoint;
135b0bf5782SToby Isaac   const PetscInt *values;
136ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
137b0bf5782SToby Isaac 
138b0bf5782SToby Isaac   PetscFunctionBegin;
1399566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
1409566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
1419566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
1429566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
143b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
144b0bf5782SToby Isaac     IS              pointIS;
145b0bf5782SToby Isaac     const PetscInt *points;
146b0bf5782SToby Isaac     PetscInt        numPoints, p;
147b0bf5782SToby Isaac 
1489566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
1499566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
1509566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
151b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
152b0bf5782SToby Isaac       PetscInt  q       = points[p];
153b0bf5782SToby Isaac       PetscInt *closure = NULL;
154b0bf5782SToby Isaac       PetscInt  closureSize, c;
155b0bf5782SToby Isaac 
156b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
157b0bf5782SToby Isaac         continue;
158b0bf5782SToby Isaac       }
1599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
16048a46eb9SPierre Jolivet       for (c = 0; c < closureSize * 2; c += 2) PetscCall(DMLabelSetValue(label, closure[c], values[v]));
1619566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
162b0bf5782SToby Isaac     }
1639566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
1649566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
165b0bf5782SToby Isaac   }
1669566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
1679566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
1689566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
1699566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
170ac51f24eSSander Arens   if (nroots >= 0) {
17126279d81SSanderA     DMLabel         lblRoots, lblLeaves;
17226279d81SSanderA     IS              valueIS, pointIS;
17326279d81SSanderA     const PetscInt *values;
17426279d81SSanderA     PetscInt        numValues, v;
17526279d81SSanderA 
17626279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
1779566063dSJacob Faibussowitsch     PetscCall(DMLabelGather(label, sfPoint, &lblLeaves));
1789566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS));
1799566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
1809566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
18126279d81SSanderA     for (v = 0; v < numValues; ++v) {
18226279d81SSanderA       const PetscInt value = values[v];
18326279d81SSanderA 
1849566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
1859566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
1869566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
18726279d81SSanderA     }
1889566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
1899566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1909566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblLeaves));
19126279d81SSanderA     /* Push point contributions from roots into remote leaves */
1929566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots));
1939566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblRoots, &valueIS));
1949566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
1959566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
19626279d81SSanderA     for (v = 0; v < numValues; ++v) {
19726279d81SSanderA       const PetscInt value = values[v];
19826279d81SSanderA 
1999566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS));
2009566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
2019566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
20226279d81SSanderA     }
2039566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
2049566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
2059566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblRoots));
20626279d81SSanderA   }
2073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
208b0bf5782SToby Isaac }
209b0bf5782SToby Isaac 
2102be2b188SMatthew G Knepley /*@
2112be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
2122be2b188SMatthew G Knepley 
2132be2b188SMatthew G Knepley   Input Parameters:
21420f4b53cSBarry Smith + dm    - The `DM`
21520f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
2162be2b188SMatthew G Knepley 
2172be2b188SMatthew G Knepley   Output Parameter:
21820f4b53cSBarry Smith . label - A `DMLabel` marking all surface points in the transitive closure
2192be2b188SMatthew G Knepley 
2202be2b188SMatthew G Knepley   Level: developer
2212be2b188SMatthew G Knepley 
2221cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelCohesiveComplete()`
2232be2b188SMatthew G Knepley @*/
224d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
225d71ae5a4SJacob Faibussowitsch {
2262be2b188SMatthew G Knepley   PetscFunctionBegin;
2279566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
2283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2292be2b188SMatthew G Knepley }
2302be2b188SMatthew G Knepley 
2316cf0e42fSMatthew G. Knepley /*@
232a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
2336cf0e42fSMatthew G. Knepley 
2346cf0e42fSMatthew G. Knepley   Input Parameters:
23520f4b53cSBarry Smith + dm    - The `DM`
23620f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
2376cf0e42fSMatthew G. Knepley 
2386cf0e42fSMatthew G. Knepley   Output Parameter:
23920f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
2406cf0e42fSMatthew G. Knepley 
2416cf0e42fSMatthew G. Knepley   Level: developer
2426cf0e42fSMatthew G. Knepley 
24320f4b53cSBarry Smith   Note:
24420f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
2456cf0e42fSMatthew G. Knepley 
2461cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
2476cf0e42fSMatthew G. Knepley @*/
248d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
249d71ae5a4SJacob Faibussowitsch {
2506cf0e42fSMatthew G. Knepley   IS              valueIS;
2516cf0e42fSMatthew G. Knepley   const PetscInt *values;
2529df54392SMatthew G. Knepley   PetscInt        numValues, v, csStart, csEnd, chStart, chEnd;
2536cf0e42fSMatthew G. Knepley 
2546cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
2559df54392SMatthew G. Knepley   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &csStart, &csEnd));
2569df54392SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(dm, 0, &chStart, &chEnd));
2579566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2589566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2599566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
2606cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2616cf0e42fSMatthew G. Knepley     IS              pointIS;
2626cf0e42fSMatthew G. Knepley     const PetscInt *points;
2636cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2646cf0e42fSMatthew G. Knepley 
2659566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2669566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2679566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
2686cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2699df54392SMatthew G. Knepley       const PetscInt point   = points[p];
2706cf0e42fSMatthew G. Knepley       PetscInt      *closure = NULL;
2719df54392SMatthew G. Knepley       PetscInt       closureSize, cl, h, pStart, pEnd, cStart, cEnd;
2729df54392SMatthew G. Knepley 
2739df54392SMatthew G. Knepley       // If the point is a hybrid, allow hybrid cells
2749df54392SMatthew G. Knepley       PetscCall(DMPlexGetPointHeight(dm, point, &h));
2759df54392SMatthew G. Knepley       PetscCall(DMPlexGetSimplexOrBoxCells(dm, h, &pStart, &pEnd));
2769df54392SMatthew G. Knepley       if (point >= pStart && point < pEnd) {
2779df54392SMatthew G. Knepley         cStart = csStart;
2789df54392SMatthew G. Knepley         cEnd   = csEnd;
2799df54392SMatthew G. Knepley       } else {
2809df54392SMatthew G. Knepley         cStart = chStart;
2819df54392SMatthew G. Knepley         cEnd   = chEnd;
2829df54392SMatthew G. Knepley       }
2836cf0e42fSMatthew G. Knepley 
2849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
28522eabd52SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
286a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
2879371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
2889371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
2899371c9d4SSatish Balay           break;
2909371c9d4SSatish Balay         }
29122eabd52SMatthew G. Knepley       }
2929566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
2936cf0e42fSMatthew G. Knepley     }
2949566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2959566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
2966cf0e42fSMatthew G. Knepley   }
2979566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2989566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3006cf0e42fSMatthew G. Knepley }
3016cf0e42fSMatthew G. Knepley 
302f402d5e4SToby Isaac /*@
303a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
304a6e0b375SMatthew G. Knepley 
305a6e0b375SMatthew G. Knepley   Input Parameters:
30620f4b53cSBarry Smith + dm    - The `DM`
30720f4b53cSBarry Smith - label - A `DMLabel` marking the surface points
308a6e0b375SMatthew G. Knepley 
309a6e0b375SMatthew G. Knepley   Output Parameter:
31020f4b53cSBarry Smith . label - A `DMLabel` incorporating cells
311a6e0b375SMatthew G. Knepley 
312a6e0b375SMatthew G. Knepley   Level: developer
313a6e0b375SMatthew G. Knepley 
31420f4b53cSBarry Smith   Note:
31520f4b53cSBarry Smith   The cells allow FEM boundary conditions to be applied using the cell geometry
316a6e0b375SMatthew G. Knepley 
3171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
318a6e0b375SMatthew G. Knepley @*/
319d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
320d71ae5a4SJacob Faibussowitsch {
321a6e0b375SMatthew G. Knepley   IS              valueIS;
322a6e0b375SMatthew G. Knepley   const PetscInt *values;
323a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
324a6e0b375SMatthew G. Knepley 
325a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
3269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
3279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
3289566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3299566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3309566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
331a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
332a6e0b375SMatthew G. Knepley     IS              pointIS;
333a6e0b375SMatthew G. Knepley     const PetscInt *points;
334a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
335a6e0b375SMatthew G. Knepley 
3369566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3379566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3389566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
339a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
340a6e0b375SMatthew G. Knepley       const PetscInt face    = points[p];
341a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
342a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
343a6e0b375SMatthew G. Knepley 
344a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
3459566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
346a6e0b375SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
347a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
3489371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
3499371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
3509371c9d4SSatish Balay           break;
3519371c9d4SSatish Balay         }
352a6e0b375SMatthew G. Knepley       }
3539566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
354a6e0b375SMatthew G. Knepley     }
3559566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3569566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
357a6e0b375SMatthew G. Knepley   }
3589566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3599566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
3603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
361a6e0b375SMatthew G. Knepley }
362a6e0b375SMatthew G. Knepley 
363a6e0b375SMatthew G. Knepley /*@
364f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
365f402d5e4SToby Isaac 
366f402d5e4SToby Isaac   Input Parameters:
36720f4b53cSBarry Smith + dm    - The `DM`
36820f4b53cSBarry Smith - label - A `DMLabel` marking surface points and their adjacent cells
369f402d5e4SToby Isaac 
370f402d5e4SToby Isaac   Output Parameter:
37120f4b53cSBarry Smith . label - A `DMLabel` without cells
372f402d5e4SToby Isaac 
373f402d5e4SToby Isaac   Level: developer
374f402d5e4SToby Isaac 
37520f4b53cSBarry Smith   Note:
37620f4b53cSBarry Smith   This undoes `DMPlexLabelAddCells()` or `DMPlexLabelAddFaceCells()`
377f402d5e4SToby Isaac 
3781cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()`
379f402d5e4SToby Isaac @*/
380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
381d71ae5a4SJacob Faibussowitsch {
382f402d5e4SToby Isaac   IS              valueIS;
383f402d5e4SToby Isaac   const PetscInt *values;
384485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
385f402d5e4SToby Isaac 
386f402d5e4SToby Isaac   PetscFunctionBegin;
3879566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
3889566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3899566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3909566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
391f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
392f402d5e4SToby Isaac     IS              pointIS;
393f402d5e4SToby Isaac     const PetscInt *points;
394f402d5e4SToby Isaac     PetscInt        numPoints, p;
395f402d5e4SToby Isaac 
3969566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3979566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3989566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
399f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
400f402d5e4SToby Isaac       PetscInt point = points[p];
401f402d5e4SToby Isaac 
40248a46eb9SPierre Jolivet       if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v]));
403f402d5e4SToby Isaac     }
4049566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
4059566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
406f402d5e4SToby Isaac   }
4079566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
4089566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
4093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
410f402d5e4SToby Isaac }
411f402d5e4SToby Isaac 
41259eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
41359eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
414d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
415d71ae5a4SJacob Faibussowitsch {
4162582d50cSToby Isaac   PetscInt d, off = 0;
4172582d50cSToby Isaac 
4182582d50cSToby Isaac   PetscFunctionBegin;
41959eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
4200974a383SToby Isaac   for (d = 0; d < depth; d++) {
4212582d50cSToby Isaac     PetscInt firstd     = d;
4220974a383SToby Isaac     PetscInt firstStart = depthShift[2 * d];
4232582d50cSToby Isaac     PetscInt e;
4242582d50cSToby Isaac 
4252582d50cSToby Isaac     for (e = d + 1; e <= depth; e++) {
4262582d50cSToby Isaac       if (depthShift[2 * e] < firstStart) {
4272582d50cSToby Isaac         firstd     = e;
4282582d50cSToby Isaac         firstStart = depthShift[2 * d];
4292582d50cSToby Isaac       }
4302582d50cSToby Isaac     }
4312582d50cSToby Isaac     if (firstd != d) {
4322582d50cSToby Isaac       PetscInt swap[2];
4332582d50cSToby Isaac 
4342582d50cSToby Isaac       e                     = firstd;
4352582d50cSToby Isaac       swap[0]               = depthShift[2 * d];
4362582d50cSToby Isaac       swap[1]               = depthShift[2 * d + 1];
4372582d50cSToby Isaac       depthShift[2 * d]     = depthShift[2 * e];
4382582d50cSToby Isaac       depthShift[2 * d + 1] = depthShift[2 * e + 1];
4392582d50cSToby Isaac       depthShift[2 * e]     = swap[0];
4402582d50cSToby Isaac       depthShift[2 * e + 1] = swap[1];
4412582d50cSToby Isaac     }
4422582d50cSToby Isaac   }
4432582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
4440974a383SToby Isaac   for (d = 0; d <= depth; d++) {
4452582d50cSToby Isaac     off += depthShift[2 * d + 1];
44659eef20bSToby Isaac     depthShift[2 * d + 1] = depthShift[2 * d] + off;
4472582d50cSToby Isaac   }
4483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4492582d50cSToby Isaac }
4502582d50cSToby Isaac 
4512582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
452d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
453d71ae5a4SJacob Faibussowitsch {
4542582d50cSToby Isaac   PetscInt d;
4552582d50cSToby Isaac   PetscInt newOff = 0;
4562582d50cSToby Isaac 
4572582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4582582d50cSToby Isaac     if (p < depthShift[2 * d]) return p + newOff;
4592582d50cSToby Isaac     else newOff = depthShift[2 * d + 1] - depthShift[2 * d];
4602582d50cSToby Isaac   }
4610974a383SToby Isaac   return p + newOff;
4622582d50cSToby Isaac }
4632582d50cSToby Isaac 
4642582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
465d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
466d71ae5a4SJacob Faibussowitsch {
4672582d50cSToby Isaac   PetscInt d;
4682582d50cSToby Isaac   PetscInt newOff = 0;
4692582d50cSToby Isaac 
4702582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4712582d50cSToby Isaac     if (p < depthShift[2 * d + 1]) return p + newOff;
4722582d50cSToby Isaac     else newOff = depthShift[2 * d] - depthShift[2 * d + 1];
4732582d50cSToby Isaac   }
4740974a383SToby Isaac   return p + newOff;
475cd0c2139SMatthew G Knepley }
476cd0c2139SMatthew G Knepley 
477d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
478d71ae5a4SJacob Faibussowitsch {
479cd0c2139SMatthew G Knepley   PetscInt depth = 0, d, pStart, pEnd, p;
480fa8e8ae5SToby Isaac   DMLabel  depthLabel;
481cd0c2139SMatthew G Knepley 
482cd0c2139SMatthew G Knepley   PetscFunctionBegin;
4839566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
4843ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
485cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
4869566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4870974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
4889566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
4899566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "depth"));
4909566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel));
4919566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
492cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
493cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
494fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
495fa8e8ae5SToby Isaac     IS       pIS;
496fa8e8ae5SToby Isaac 
4979566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
498fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
499fa8e8ae5SToby Isaac     pEndNew   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
5009566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
5019566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
5029566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
503cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
5042582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
505cd0c2139SMatthew G Knepley       PetscInt       size;
506412e9a14SMatthew G. Knepley       DMPolytopeType ct;
507cd0c2139SMatthew G Knepley 
5089566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
5099566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
5109566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
5119566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
5129566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
5139566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
514cd0c2139SMatthew G Knepley     }
515cd0c2139SMatthew G Knepley   }
5163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
517cd0c2139SMatthew G Knepley }
518cd0c2139SMatthew G Knepley 
519d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
520d71ae5a4SJacob Faibussowitsch {
5212582d50cSToby Isaac   PetscInt *newpoints;
5222582d50cSToby Isaac   PetscInt  depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
523cd0c2139SMatthew G Knepley 
524cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5259566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5263ba16761SJacob Faibussowitsch   if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS);
5279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
5289566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
5299566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints));
530cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
5319566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
532cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
533cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
5342582d50cSToby Isaac     PetscInt        size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
535cd0c2139SMatthew G Knepley 
5369566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
5379566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
5389566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
539ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
5409566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
5419566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
5429566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
5439566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
5449566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
545ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
546dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
5479566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
548cd0c2139SMatthew G Knepley   }
5499566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
5503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
551cd0c2139SMatthew G Knepley }
552cd0c2139SMatthew G Knepley 
553d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
554d71ae5a4SJacob Faibussowitsch {
555cd0c2139SMatthew G Knepley   PetscSection coordSection, newCoordSection;
556cd0c2139SMatthew G Knepley   Vec          coordinates, newCoordinates;
557cd0c2139SMatthew G Knepley   PetscScalar *coords, *newCoords;
558f2b8cce1SMatthew G. Knepley   PetscInt     coordSize, sStart, sEnd;
559f2b8cce1SMatthew G. Knepley   PetscInt     dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
560f2b8cce1SMatthew G. Knepley   PetscBool    hasCells;
561cd0c2139SMatthew G Knepley 
562cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5639566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
5649566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
5659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
566cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
5679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
5689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
5709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
5719566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
5729566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
5739566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
5749566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
5759566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
576f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
5779566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
578f2b8cce1SMatthew G. Knepley   if (hasCells) {
579f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
580f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
581f2b8cce1SMatthew G. Knepley 
5829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
5839566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
5849566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
585f2b8cce1SMatthew G. Knepley     }
586f2b8cce1SMatthew G. Knepley   }
587cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
5889566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
5899566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
590cd0c2139SMatthew G Knepley   }
5919566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
5929566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
5939566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
5949566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
5959566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates"));
5969566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
5979566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
5989566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates, VECSTANDARD));
5999566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
6009566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
6019566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6029566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
603f2b8cce1SMatthew G. Knepley   if (hasCells) {
604f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
605f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
606f2b8cce1SMatthew G. Knepley 
6079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
6089566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
6099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
610f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
611f2b8cce1SMatthew G. Knepley     }
612f2b8cce1SMatthew G. Knepley   }
613cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
614cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
615cd0c2139SMatthew G Knepley 
6169566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
6179566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
6189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
619f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
620cd0c2139SMatthew G Knepley   }
6219566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6229566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
6239566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
6249566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
6253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
626cd0c2139SMatthew G Knepley }
627cd0c2139SMatthew G Knepley 
628d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
629d71ae5a4SJacob Faibussowitsch {
630cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
631cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
632cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
633cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
6340e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
635cd0c2139SMatthew G Knepley 
636cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
6389566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
6399566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
6406e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
641cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
6429566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation));
6430e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
6449566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
6459566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
6469566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
6479566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
648cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
6492582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
650cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
651cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
652cd0c2139SMatthew G Knepley     }
6539566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
6549566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
6550e33faafSMatthew G. Knepley   }
6563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6570e33faafSMatthew G. Knepley }
6580e33faafSMatthew G. Knepley 
659d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
660d71ae5a4SJacob Faibussowitsch {
6610e33faafSMatthew G. Knepley   PetscSF   sfPoint, sfPointNew;
6620e33faafSMatthew G. Knepley   PetscBool useNatural;
6630e33faafSMatthew G. Knepley 
6640e33faafSMatthew G. Knepley   PetscFunctionBegin;
6650e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
6669566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
6679566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
6689566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
6690e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
6709566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
6710e33faafSMatthew G. Knepley   if (useNatural) {
6720e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
6730e33faafSMatthew G. Knepley 
6749566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
6759566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
6769566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
6779566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
678cd0c2139SMatthew G Knepley   }
6793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
680cd0c2139SMatthew G Knepley }
681cd0c2139SMatthew G Knepley 
682d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
683d71ae5a4SJacob Faibussowitsch {
684d56405f8SMatthew G. Knepley   PetscInt depth = 0, numLabels, l;
685cd0c2139SMatthew G Knepley 
686cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6879566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
688cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
6899566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
690cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
691cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
692cd0c2139SMatthew G Knepley     const char     *lname;
693fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
694cd0c2139SMatthew G Knepley     IS              valueIS;
695cd0c2139SMatthew G Knepley     const PetscInt *values;
696cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
697cd0c2139SMatthew G Knepley 
6989566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
6999566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
700cd0c2139SMatthew G Knepley     if (isDepth) continue;
7019566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
702fa8e8ae5SToby Isaac     if (isDim) continue;
7039566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
7049566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
7059566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
7069566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &val));
7079566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, val));
7089566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
7099566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
7109566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
711cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
712cd0c2139SMatthew G Knepley       IS              pointIS;
713cd0c2139SMatthew G Knepley       const PetscInt *points;
714cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
715cd0c2139SMatthew G Knepley 
7169566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
7179566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
7189566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
719cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
7202582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
721cd0c2139SMatthew G Knepley 
7229566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
723cd0c2139SMatthew G Knepley       }
7249566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
7259566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
726cd0c2139SMatthew G Knepley     }
7279566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
7289566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
729cd0c2139SMatthew G Knepley   }
7303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
731d56405f8SMatthew G. Knepley }
732d56405f8SMatthew G. Knepley 
733d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
734d71ae5a4SJacob Faibussowitsch {
735d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
736d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
737d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
738d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
739d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
740d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
741d56405f8SMatthew G. Knepley 
742d56405f8SMatthew G. Knepley   PetscFunctionBegin;
743cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
7449566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
7459566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
7469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
7479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
7489566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
7499566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
7509566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
751d56405f8SMatthew G. Knepley   if (createGhostLabel) {
7529566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
7539566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
754d56405f8SMatthew G. Knepley   }
755cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
75648a46eb9SPierre Jolivet     for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
757cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
758cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
7599566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
7601baa6e33SBarry Smith     } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
761cd0c2139SMatthew G Knepley   }
76248a46eb9SPierre Jolivet   for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
763d56405f8SMatthew G. Knepley   if (ghostLabel) {
7649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
765cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
766cd0c2139SMatthew G Knepley       PetscInt numCells;
767cd0c2139SMatthew G Knepley 
7689566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
769cd0c2139SMatthew G Knepley       if (numCells < 2) {
7709566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
771cd0c2139SMatthew G Knepley       } else {
772cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
773cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
774cd0c2139SMatthew G Knepley 
7759566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
7769566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
7779566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
7789566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
779cd0c2139SMatthew G Knepley       }
780cd0c2139SMatthew G Knepley     }
781d56405f8SMatthew G. Knepley   }
7823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
783cd0c2139SMatthew G Knepley }
784cd0c2139SMatthew G Knepley 
785d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
786d71ae5a4SJacob Faibussowitsch {
787ca04dac2SToby Isaac   DM           refTree;
788ca04dac2SToby Isaac   PetscSection pSec;
789ca04dac2SToby Isaac   PetscInt    *parents, *childIDs;
790ca04dac2SToby Isaac 
791ca04dac2SToby Isaac   PetscFunctionBegin;
7929566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
7939566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew, refTree));
7949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL));
795ca04dac2SToby Isaac   if (pSec) {
7962582d50cSToby Isaac     PetscInt     p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
797fb4630b5SToby Isaac     PetscInt    *childIDsShifted;
798ca04dac2SToby Isaac     PetscSection pSecShifted;
799ca04dac2SToby Isaac 
8009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd));
8019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
8022582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
8032582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
8049566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted));
8059566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted));
8069566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted));
807ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
808fb4630b5SToby Isaac       /* start off assuming no children */
8099566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, p, 0));
810fb4630b5SToby Isaac     }
811fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
812fb4630b5SToby Isaac       PetscInt dof;
813fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
814ca04dac2SToby Isaac 
8159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
8169566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof));
817ca04dac2SToby Isaac     }
8189566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
819fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
820fb4630b5SToby Isaac       PetscInt dof;
821fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
822fb4630b5SToby Isaac 
8239566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
824fb4630b5SToby Isaac       if (dof) {
825fb4630b5SToby Isaac         PetscInt off, offNew;
826fb4630b5SToby Isaac 
8279566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec, p, &off));
8289566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew));
829fb4630b5SToby Isaac         parentsShifted[offNew]  = DMPlexShiftPoint_Internal(parents[off], depth, depthShift);
830fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
831fb4630b5SToby Isaac       }
832fb4630b5SToby Isaac     }
8339566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted));
8349566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted, childIDsShifted));
8359566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
836ca04dac2SToby Isaac   }
8373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
838ca04dac2SToby Isaac }
839ca04dac2SToby Isaac 
840d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
841d71ae5a4SJacob Faibussowitsch {
842da97024aSMatthew G. Knepley   PetscSF          sf;
843cd0c2139SMatthew G Knepley   IS               valueIS;
844da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
845cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
8462582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
8474fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
848cd0c2139SMatthew G Knepley 
849cd0c2139SMatthew G Knepley   PetscFunctionBegin;
8509566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
8519566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
852da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
8539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
854cd0c2139SMatthew G Knepley   /* Count ghost cells */
8559566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
8569566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
8579566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
8584a6cfa73SMatthew G. Knepley   Ng = 0;
859cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
86046c796b9SMatthew G. Knepley     IS              faceIS;
86146c796b9SMatthew G. Knepley     const PetscInt *faces;
86246c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
863cd0c2139SMatthew G Knepley 
8649566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8659566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8669566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
86746c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
868ca04dac2SToby Isaac       PetscInt numChildren;
869ca04dac2SToby Isaac 
8709566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8719566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
872ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
873ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
87446c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
87546c796b9SMatthew G. Knepley     }
8764a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
8779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
879cd0c2139SMatthew G Knepley   }
8809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
8819566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift));
8822582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
88359eef20bSToby Isaac     PetscInt dEnd;
8842582d50cSToby Isaac 
8859566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd));
88659eef20bSToby Isaac     depthShift[2 * d]     = dEnd;
8872582d50cSToby Isaac     depthShift[2 * d + 1] = 0;
8882582d50cSToby Isaac   }
8892582d50cSToby Isaac   if (depth >= 0) depthShift[2 * depth + 1] = Ng;
8909566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
8919566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
892cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
8939566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
89448a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1));
895cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
896cd0c2139SMatthew G Knepley     IS              faceIS;
897cd0c2139SMatthew G Knepley     const PetscInt *faces;
898cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
899cd0c2139SMatthew G Knepley 
9009566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9019566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9029566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
903cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
904ca04dac2SToby Isaac       PetscInt size, numChildren;
905cd0c2139SMatthew G Knepley 
9069566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9079566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
908ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
90946c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9109566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
91163a3b9bcSJacob 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);
9129566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
913cd0c2139SMatthew G Knepley     }
9149566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9159566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
916cd0c2139SMatthew G Knepley   }
917cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
9189566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
9199566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
920cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
921cd0c2139SMatthew G Knepley   ghostCell = cEnd;
922cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
923cd0c2139SMatthew G Knepley     IS              faceIS;
924cd0c2139SMatthew G Knepley     const PetscInt *faces;
925cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
926cd0c2139SMatthew G Knepley 
9279566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9289566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9299566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
93046c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
931ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
932cd0c2139SMatthew G Knepley 
9339566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9349566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
935ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
93646c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9379566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
9389566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
93946c796b9SMatthew G. Knepley       ++ghostCell;
940cd0c2139SMatthew G Knepley     }
9419566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9429566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
943cd0c2139SMatthew G Knepley   }
9449566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
9459566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
9469566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
9479566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
9489566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
9499566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
9509566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
9519566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
95248a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
953966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
9544fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
9554fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L));
9564a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
9573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
958cd0c2139SMatthew G Knepley }
959cd0c2139SMatthew G Knepley 
960cd0c2139SMatthew G Knepley /*@C
961cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
962cd0c2139SMatthew G Knepley 
96320f4b53cSBarry Smith   Collective
964cd0c2139SMatthew G Knepley 
965cd0c2139SMatthew G Knepley   Input Parameters:
96620f4b53cSBarry Smith + dm        - The original `DM`
96720f4b53cSBarry Smith - labelName - The label specifying the boundary faces, or "Face Sets" if this is `NULL`
968cd0c2139SMatthew G Knepley 
969cd0c2139SMatthew G Knepley   Output Parameters:
97020f4b53cSBarry Smith + numGhostCells - The number of ghost cells added to the `DM`
97120f4b53cSBarry Smith - dmGhosted     - The new `DM`
972cd0c2139SMatthew G Knepley 
973cd0c2139SMatthew G Knepley   Level: developer
974cd0c2139SMatthew G Knepley 
97520f4b53cSBarry Smith   Note:
97620f4b53cSBarry Smith   If no label exists of that name, one will be created marking all boundary faces
97720f4b53cSBarry Smith 
9781cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`
97931266bc0SMatthew G. Knepley @*/
980d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
981d71ae5a4SJacob Faibussowitsch {
982cd0c2139SMatthew G Knepley   DM          gdm;
983cd0c2139SMatthew G Knepley   DMLabel     label;
984cd0c2139SMatthew G Knepley   const char *name = labelName ? labelName : "Face Sets";
985412e9a14SMatthew G. Knepley   PetscInt    dim, Ng = 0;
986b0441da4SMatthew G. Knepley   PetscBool   useCone, useClosure;
987cd0c2139SMatthew G Knepley 
988cd0c2139SMatthew G Knepley   PetscFunctionBegin;
989cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9904f572ea9SToby Isaac   if (numGhostCells) PetscAssertPointer(numGhostCells, 3);
9914f572ea9SToby Isaac   PetscAssertPointer(dmGhosted, 4);
9929566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
9939566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
9949566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
9959566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
9969566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
9979566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure));
9989566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
999cd0c2139SMatthew G Knepley   if (!label) {
1000cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
10019566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
10029566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
10039566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
1004cd0c2139SMatthew G Knepley   }
10059566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
10069566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
10075de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
1008cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
1009d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
1010cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
10113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1012cd0c2139SMatthew G Knepley }
1013cd0c2139SMatthew G Knepley 
1014d71ae5a4SJacob Faibussowitsch static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue)
1015d71ae5a4SJacob Faibussowitsch {
1016accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
1017accc9626SMatthew G. Knepley 
1018accc9626SMatthew G. Knepley   PetscFunctionBegin;
1019accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1020accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
102148a46eb9SPierre Jolivet   for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
1022accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
1023accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
1024accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
1025accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
1026accc9626SMatthew G. Knepley 
1027accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
1028accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
1029accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1030accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
1031accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1032accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1033accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
1034accc9626SMatthew G. Knepley         PetscInt       clval;
1035accc9626SMatthew G. Knepley 
1036accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
1037accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
1038accc9626SMatthew G. Knepley         if (clval == -1) {
1039accc9626SMatthew G. Knepley           const PetscInt *cone;
1040accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
1041accc9626SMatthew G. Knepley 
1042accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
1043accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
1044accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
1045accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
1046accc9626SMatthew G. Knepley             PetscInt cpval;
1047accc9626SMatthew G. Knepley 
1048accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
1049accc9626SMatthew G. Knepley             if (cpval != -1) {
1050accc9626SMatthew G. Knepley               PetscInt dep;
1051accc9626SMatthew G. Knepley 
1052accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
1053accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift + dep) : shift + dep;
1054accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
1055accc9626SMatthew G. Knepley               break;
1056accc9626SMatthew G. Knepley             }
1057accc9626SMatthew G. Knepley           }
1058accc9626SMatthew G. Knepley         }
1059accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
1060accc9626SMatthew G. Knepley         if (d == dim - 1 && clval != -1) {
1061accc9626SMatthew G. Knepley           const PetscInt *support;
1062accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
1063accc9626SMatthew G. Knepley 
1064accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
1065accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
1066accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
1067accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
1068accc9626SMatthew G. Knepley             if (nval == -1) {
1069accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1));
1070accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1071accc9626SMatthew G. Knepley             }
1072accc9626SMatthew G. Knepley           }
1073accc9626SMatthew G. Knepley         }
1074accc9626SMatthew G. Knepley       }
1075accc9626SMatthew G. Knepley     }
1076accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1077accc9626SMatthew G. Knepley   }
1078accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
10793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1080accc9626SMatthew G. Knepley }
1081accc9626SMatthew G. Knepley 
1082accc9626SMatthew G. Knepley typedef struct {
1083accc9626SMatthew G. Knepley   DM               dm;
1084accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1085accc9626SMatthew G. Knepley } PointDivision;
1086accc9626SMatthew G. Knepley 
1087d71ae5a4SJacob Faibussowitsch static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx)
1088d71ae5a4SJacob Faibussowitsch {
1089accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *)ctx;
1090accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val - 1 : val + 1;
1091accc9626SMatthew G. Knepley   const PetscInt *support;
1092accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1093accc9626SMatthew G. Knepley 
1094accc9626SMatthew G. Knepley   PetscFunctionBegin;
1095accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1096accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1097accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1098accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1099accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1100accc9626SMatthew G. Knepley   }
11013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1102accc9626SMatthew G. Knepley }
1103accc9626SMatthew G. Knepley 
1104accc9626SMatthew G. Knepley /* Mark cells by label propagation */
1105d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel)
1106d71ae5a4SJacob Faibussowitsch {
1107accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1108accc9626SMatthew G. Knepley   PointDivision    div;
1109accc9626SMatthew G. Knepley   PetscSF          pointSF;
1110accc9626SMatthew G. Knepley   IS               pointIS;
1111accc9626SMatthew G. Knepley   const PetscInt  *points;
1112accc9626SMatthew G. Knepley   PetscBool        empty;
1113accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1114accc9626SMatthew G. Knepley 
1115accc9626SMatthew G. Knepley   PetscFunctionBegin;
1116accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1117accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1118accc9626SMatthew G. Knepley   div.dm    = dm;
1119accc9626SMatthew G. Knepley   div.queue = queue;
1120accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1121accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS));
1122accc9626SMatthew G. Knepley   if (pointIS) {
1123accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1124accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
112548a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1126accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1127accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1128accc9626SMatthew G. Knepley   }
1129accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS));
1130accc9626SMatthew G. Knepley   if (pointIS) {
1131accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1132accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
113348a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1134accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1135accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1136accc9626SMatthew G. Knepley   }
1137accc9626SMatthew G. Knepley 
1138accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1139accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1140accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1141accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1142accc9626SMatthew G. Knepley   while (!empty) {
1143accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1144accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1145accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1146accc9626SMatthew G. Knepley   }
1147accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1148accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
11493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1150accc9626SMatthew G. Knepley }
1151accc9626SMatthew G. Knepley 
1152607ab7a9SMatthew G. Knepley /*
1153faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1154607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1155faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1156b6dfa339SMatthew 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
1157607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1158a6ae58d1SMatthew G. Knepley 
1159a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1160a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1161607ab7a9SMatthew G. Knepley */
1162d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1163d71ae5a4SJacob Faibussowitsch {
1164cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1165607ab7a9SMatthew G. Knepley   IS               valueIS;
1166607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0; /* The number of depths for which we have replicated points */
1167607ab7a9SMatthew G. Knepley   const PetscInt  *values;    /* List of depths for which we have replicated points */
116818c5995bSMatthew G. Knepley   IS              *splitIS;
116918c5995bSMatthew G. Knepley   IS              *unsplitIS;
1170b6dfa339SMatthew G. Knepley   IS               ghostIS;
1171607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
117218c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
117336dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
117436dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1175b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1176607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
117718c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1178b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1179cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1180cd0c2139SMatthew G Knepley   Vec              coordinates;
1181cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1182a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;   /* The first hybrid point at each depth in the original mesh */
1183a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;   /* The point limit at each depth in the original mesh */
1184607ab7a9SMatthew G. Knepley   PetscInt        *depthShift; /* Number of replicated+hybrid points at each depth */
1185607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;    /* The first replicated point at each depth in the new mesh, hybrids come after this */
1186607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
118718c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1188cd0c2139SMatthew G Knepley 
1189cd0c2139SMatthew G Knepley   PetscFunctionBegin;
11909566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
11919566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
11929566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
11939566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1194412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
11959566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1196cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
11979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
11989566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld));
11999566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints));
1200607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
12019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
12029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1203a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1204a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1205607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
120618c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1207607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1208a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1209607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
121018c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
121118c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
121218c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
121359eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
121459eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
121559eef20bSToby Isaac     depthShift[2 * d]     = depthMax[d];
121659eef20bSToby Isaac     depthShift[2 * d + 1] = 0;
1217607ab7a9SMatthew G. Knepley   }
1218b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1219b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1220cd0c2139SMatthew G Knepley   if (label) {
12219566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
12229566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
12239566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1224cd0c2139SMatthew G Knepley   }
1225cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1226cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1227cd0c2139SMatthew G Knepley 
1228cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
12299566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
123018c5995bSMatthew G. Knepley     if (splitIS[dep]) {
12319566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
12329566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
123318c5995bSMatthew G. Knepley     }
12349566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep]));
123518c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
12369566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
12379566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1238cd0c2139SMatthew G Knepley     }
1239cd0c2139SMatthew G Knepley   }
1240b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS));
1241b6dfa339SMatthew G. Knepley   if (ghostIS) {
1242b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1243b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1244b6dfa339SMatthew G. Knepley   }
1245607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
124618c5995bSMatthew 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   */
12472582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d];
12489566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
124959eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
125059eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
125159eef20bSToby Isaac    * added points */
12522582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
12539566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1254cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1255cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1256cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1257cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
12582582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
12594c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1260cd0c2139SMatthew G Knepley       const PetscInt *support;
1261394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
12624c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1263cd0c2139SMatthew G Knepley 
12649566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
12659566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
12669566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
12679566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
12689566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
12699566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1270cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
12714c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
12724c367dbcSMatthew G. Knepley 
1273cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
12749566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1275412e9a14SMatthew G. Knepley         switch (coneSize) {
1276d71ae5a4SJacob Faibussowitsch         case 2:
1277d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));
1278d71ae5a4SJacob Faibussowitsch           break;
1279d71ae5a4SJacob Faibussowitsch         case 3:
1280d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));
1281d71ae5a4SJacob Faibussowitsch           break;
1282d71ae5a4SJacob Faibussowitsch         case 4:
1283d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));
1284d71ae5a4SJacob Faibussowitsch           break;
1285412e9a14SMatthew G. Knepley         }
1286b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1287b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1288b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1289b6dfa339SMatthew G. Knepley           const PetscInt *support;
1290b6dfa339SMatthew G. Knepley           PetscInt        val;
1291b6dfa339SMatthew G. Knepley 
1292b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1293b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1294b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1295b6dfa339SMatthew G. Knepley           else PetscCall(DMPlexSetSupportSize(sdm, newp, 2));
1296b6dfa339SMatthew G. Knepley         }
1297cd0c2139SMatthew G Knepley       } else if (dep == 0) {
12984c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1299cd0c2139SMatthew G Knepley 
13009566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13014c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1302cd0c2139SMatthew G Knepley           PetscInt val;
1303cd0c2139SMatthew G Knepley 
13049566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13054c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
13064c367dbcSMatthew G. Knepley           if ((val == 1) || (val == (shift + 1))) ++qn;
13074c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1308cd0c2139SMatthew G Knepley         }
13094c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
13109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13114c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
13129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13134c367dbcSMatthew G. Knepley         /* Add hybrid edge */
13149566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
13159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1317cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
13184c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13194c367dbcSMatthew G. Knepley 
13209566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13214c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1322cd0c2139SMatthew G Knepley           PetscInt val;
1323cd0c2139SMatthew G Knepley 
13249566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13254c367dbcSMatthew G. Knepley           if (val == dim - 1) ++qf;
13264c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn;
13274c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp;
1328cd0c2139SMatthew G Knepley         }
13294c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
13309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13314c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
13329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13334c367dbcSMatthew G. Knepley         /* Add hybrid face */
13349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
13359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
13369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1337cd0c2139SMatthew G Knepley       }
1338cd0c2139SMatthew G Knepley     }
1339cd0c2139SMatthew G Knepley   }
134018c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
134118c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
134218c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
13432582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
134418c5995bSMatthew G. Knepley       const PetscInt *support;
1345da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
134618c5995bSMatthew G. Knepley 
13479566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13489566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13499566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
135018c5995bSMatthew G. Knepley       if (dep == 0) {
135118c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
135218c5995bSMatthew G. Knepley 
135339254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
135439254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
13559566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
135639254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
135739254ff6SMatthew G. Knepley         }
13589566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
135918c5995bSMatthew G. Knepley         /* Add hybrid edge */
13609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1361e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1362e1757548SMatthew G. Knepley           PetscInt val;
1363e1757548SMatthew G. Knepley 
13649566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1365e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1366da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1367da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1368e1757548SMatthew G. Knepley         }
13699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13709566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
137118c5995bSMatthew G. Knepley       } else if (dep == dim - 2) {
137218c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1373cd0c2139SMatthew G Knepley         PetscInt       val;
1374cd0c2139SMatthew G Knepley 
1375da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13769566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1377da1dd7e4SMatthew G. Knepley           if (val == dim - 1) qf += 2;
1378da1dd7e4SMatthew G. Knepley           else ++qf;
1379cd0c2139SMatthew G Knepley         }
138018c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
13819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
138218c5995bSMatthew G. Knepley         /* Add hybrid face */
1383da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13849566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1385da1dd7e4SMatthew G. Knepley           if (val == dim - 1) ++qf;
1386da1dd7e4SMatthew G. Knepley         }
13879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
13889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
13899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1390cd0c2139SMatthew G Knepley       }
1391cd0c2139SMatthew G Knepley     }
1392cd0c2139SMatthew G Knepley   }
1393cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
13949566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
13959566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
13969566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
13979566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew));
1398cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1399cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1400cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1401cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
14022582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
14034c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1404cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1405b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
14064c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1407cd0c2139SMatthew G Knepley 
14089566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
14099566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14109566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
14119566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
14129566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14139566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1414cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
141596a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
14164c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1417cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1418cd0c2139SMatthew G Knepley 
1419b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1420cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
14219566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp, &supportF));
14229566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1423cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1424cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1425cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
14269566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
142718c5995bSMatthew G. Knepley           if (v < 0) {
14289566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
142963a3b9bcSJacob 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);
14302582d50cSToby Isaac             coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
143196a07cd0SMatthew G. Knepley             hasUnsplit     = PETSC_TRUE;
143218c5995bSMatthew G. Knepley           } else {
14334c367dbcSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep - 1];
1434163235baSMatthew G. Knepley             if (dep > 1) {
1435163235baSMatthew G. Knepley               const PetscInt *econe;
1436163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1437163235baSMatthew G. Knepley 
14389566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
14399566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1440163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
14419566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs));
14429566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu));
1443163235baSMatthew G. Knepley                 if (vs >= 0) continue;
144463a3b9bcSJacob 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);
1445163235baSMatthew G. Knepley                 hasUnsplit = PETSC_TRUE;
1446163235baSMatthew G. Knepley               }
1447163235baSMatthew G. Knepley             }
1448cd0c2139SMatthew G Knepley           }
1449cd0c2139SMatthew G Knepley         }
14509566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
14519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1452e537020bSMatthew G. Knepley         /* Face support */
1453b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1454cd0c2139SMatthew G Knepley 
1455b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1456b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1457b6dfa339SMatthew G. Knepley         else vals[1] = -vals[0];
1458b6dfa339SMatthew 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]);
1459b6dfa339SMatthew G. Knepley 
1460b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1461b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1462b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1463b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1464accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1465b6dfa339SMatthew G. Knepley             } else {
1466b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1467accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1468b6dfa339SMatthew G. Knepley             }
1469b6dfa339SMatthew G. Knepley           } else {
1470b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1471cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
14729566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1473cd0c2139SMatthew G Knepley             } else {
1474cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
14759566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1476b6dfa339SMatthew G. Knepley             }
1477b6dfa339SMatthew G. Knepley           }
1478b6dfa339SMatthew G. Knepley         }
1479b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1480e537020bSMatthew G. Knepley         {
1481e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1482b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1483accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1484e537020bSMatthew G. Knepley 
14859371c9d4SSatish Balay           if (supportSize > 1) {
14869371c9d4SSatish Balay             ocell = vals[0] < 0 ? support[1] : support[0];
14879371c9d4SSatish Balay           } else {
14889371c9d4SSatish Balay             ocell = support[0];
14899371c9d4SSatish Balay             flip  = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;
14909371c9d4SSatish Balay           }
1491b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1492b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1493e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1494e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1495e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1496b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc];
1497e537020bSMatthew G. Knepley               break;
1498cd0c2139SMatthew G Knepley             }
1499cd0c2139SMatthew G Knepley           }
1500b6dfa339SMatthew 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);
1501e537020bSMatthew G. Knepley         }
15024c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1503b6dfa339SMatthew G. Knepley         {
1504b6dfa339SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangments(ct) / 2;
1505b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1506b6dfa339SMatthew G. Knepley         }
1507b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1508b5a892a1SMatthew G. Knepley 
1509fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp; /* Extracted negative side orientation above */
15104c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
15114c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1512e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1513412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1514b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q * 2 + 0];
1515b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q * 2 + 1];
1516b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1517412e9a14SMatthew G. Knepley 
15189566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
151918c5995bSMatthew G. Knepley           if (v < 0) {
15209566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
152118c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
152218c5995bSMatthew G. Knepley           } else {
152318c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep];
152418c5995bSMatthew G. Knepley           }
1525b5a892a1SMatthew G. Knepley           coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1526e537020bSMatthew G. Knepley         }
15279566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
15289566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
152996a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
15309566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1531cd0c2139SMatthew G Knepley       } else if (dep == 0) {
15324c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1533cd0c2139SMatthew G Knepley 
1534cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
15354c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1536cd0c2139SMatthew G Knepley           PetscInt val;
1537cd0c2139SMatthew G Knepley 
15389566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1539ad540459SPierre Jolivet           if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1540cd0c2139SMatthew G Knepley         }
15414c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
15429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1543cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
15444c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1545cd0c2139SMatthew G Knepley           PetscInt val, edge;
1546cd0c2139SMatthew G Knepley 
15479566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1548cd0c2139SMatthew G Knepley           if (val == 1) {
15499566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
155063a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
15514c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep + 1];
1552cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
15532582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1554cd0c2139SMatthew G Knepley           }
1555cd0c2139SMatthew G Knepley         }
15564c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
15579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
15584c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1559cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1560cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
15619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
15624c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
15634c367dbcSMatthew G. Knepley           PetscInt val, edge;
15644c367dbcSMatthew G. Knepley 
15659566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
15664c367dbcSMatthew G. Knepley           if (val == 1) {
15679566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
156863a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
15694c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
15704c367dbcSMatthew G. Knepley           }
15714c367dbcSMatthew G. Knepley         }
15729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1573cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
15744c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
15754c367dbcSMatthew G. Knepley 
1576cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1577cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1578cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
15799566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
1580e1757548SMatthew G. Knepley           if (v < 0) {
15819566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
158263a3b9bcSJacob 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);
15832582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1584e1757548SMatthew G. Knepley           } else {
15854c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep - 1];
1586cd0c2139SMatthew G Knepley           }
1587e1757548SMatthew G. Knepley         }
15889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1589cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1590cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1591cd0c2139SMatthew G Knepley           PetscInt val;
1592cd0c2139SMatthew G Knepley 
15939566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
15944c367dbcSMatthew G. Knepley           if (val == dim - 1) {
15952582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
15964c367dbcSMatthew G. Knepley           } else if (val == (shift + dim - 1)) {
15972582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1598cd0c2139SMatthew G Knepley           }
1599cd0c2139SMatthew G Knepley         }
1600b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16019566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1602cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1603cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1604cd0c2139SMatthew G Knepley           PetscInt val, face;
1605cd0c2139SMatthew G Knepley 
16069566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1607cd0c2139SMatthew G Knepley           if (val == dim - 1) {
16089566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
160963a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
16104c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep + 1];
1611cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim - 1)) {
16122582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1613cd0c2139SMatthew G Knepley           }
1614cd0c2139SMatthew G Knepley         }
1615b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
16169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16174c367dbcSMatthew G. Knepley         /* Hybrid face */
16184c367dbcSMatthew G. Knepley         coneNew[0] = newp;
16194c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
16204c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
16214c367dbcSMatthew G. Knepley           PetscInt vertex;
16229566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex));
1623e1757548SMatthew G. Knepley           if (vertex < 0) {
16249566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex));
162563a3b9bcSJacob 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);
1626e1757548SMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
1627e1757548SMatthew G. Knepley           } else {
16284c367dbcSMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
16294c367dbcSMatthew G. Knepley           }
1630e1757548SMatthew G. Knepley         }
16319566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
16324c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
16334c367dbcSMatthew G. Knepley           PetscInt val, face;
16344c367dbcSMatthew G. Knepley 
16359566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16364c367dbcSMatthew G. Knepley           if (val == dim - 1) {
16379566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
163863a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
16394c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
16404c367dbcSMatthew G. Knepley           }
16414c367dbcSMatthew G. Knepley         }
16429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1643cd0c2139SMatthew G Knepley       }
1644cd0c2139SMatthew G Knepley     }
1645cd0c2139SMatthew G Knepley   }
164618c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
164718c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
164818c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
16492582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1650b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1651e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
165218c5995bSMatthew G. Knepley 
16539566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
16549566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
16559566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
16569566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
165718c5995bSMatthew G. Knepley       if (dep == 0) {
165818c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
165918c5995bSMatthew G. Knepley 
166018c5995bSMatthew G. Knepley         /* Unsplit vertex */
16619566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
166218c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
16632582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
16649566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
1665ad540459SPierre Jolivet           if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1];
166618c5995bSMatthew G. Knepley         }
166718c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
166818c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
166963a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
16709566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
167118c5995bSMatthew G. Knepley         /* Hybrid edge */
167218c5995bSMatthew G. Knepley         coneNew[0] = newp;
167318c5995bSMatthew G. Knepley         coneNew[1] = newp;
16749566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
167518c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
167618c5995bSMatthew G. Knepley           PetscInt val, edge;
167718c5995bSMatthew G. Knepley 
16789566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
167918c5995bSMatthew G. Knepley           if (val == 1) {
16809566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
168163a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
168218c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1683e1757548SMatthew G. Knepley           } else if (val == (shift2 + 1)) {
16849566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge));
168563a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1686e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1];
168718c5995bSMatthew G. Knepley           }
168818c5995bSMatthew G. Knepley         }
16899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1690e1757548SMatthew G. Knepley       } else if (dep == dim - 2) {
1691e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1692e1757548SMatthew G. Knepley 
1693da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1694e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1695e1757548SMatthew G. Knepley           PetscInt val, face;
1696e1757548SMatthew G. Knepley 
16979566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1698e1757548SMatthew G. Knepley           if (val == dim - 1) {
16999566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
170063a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
17012582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1702e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 1];
1703e1757548SMatthew G. Knepley           } else {
17042582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1705e1757548SMatthew G. Knepley           }
1706e1757548SMatthew G. Knepley         }
1707e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1708e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
17099566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
171063a3b9bcSJacob 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);
17119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1712e1757548SMatthew G. Knepley         /* Add hybrid face */
1713e1757548SMatthew G. Knepley         coneNew[0] = newp;
1714212cc919SMatthew G. Knepley         coneNew[1] = newp;
17159566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
171663a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1717212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17189566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
171963a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]);
1720e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1722da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1723da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1724da1dd7e4SMatthew G. Knepley 
17259566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1726da1dd7e4SMatthew G. Knepley           if (val == dim - 1) {
17279566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
1728da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1729da1dd7e4SMatthew G. Knepley           }
1730da1dd7e4SMatthew G. Knepley         }
17319566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
173263a3b9bcSJacob 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);
17339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1734cd0c2139SMatthew G Knepley       }
1735cd0c2139SMatthew G Knepley     }
1736cd0c2139SMatthew G Knepley   }
1737cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1738cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1739cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1740cd0c2139SMatthew G Knepley     IS              pIS;
1741cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1742cd0c2139SMatthew G Knepley     const PetscInt *points;
1743cd0c2139SMatthew G Knepley 
1744cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
17459566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1746cd0c2139SMatthew G Knepley     if (!pIS) continue;
1747cd0c2139SMatthew G Knepley     dep = -dep - shift;
17489566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
17499566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1750cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1751cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
17522582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1753cd0c2139SMatthew G Knepley       const PetscInt *cone;
1754cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
175550cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1756cd0c2139SMatthew G Knepley 
1757cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1758cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
17599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
17609566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1761cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1762e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift);
1763cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1764cd0c2139SMatthew G Knepley 
17659566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1766cd0c2139SMatthew G Knepley         if (val == dep - 1) {
17679566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp));
176863a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1);
1769cd0c2139SMatthew G Knepley           csplitp = pMaxNew[dep - 1] + cp;
17709566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
177150cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1772cd0c2139SMatthew G Knepley         }
1773cd0c2139SMatthew G Knepley       }
17744a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
177528b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1776cd0c2139SMatthew G Knepley     }
17779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
17789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1779cd0c2139SMatthew G Knepley   }
17800e18dc48SMatthew G. Knepley   PetscCall(DMPlexReorderCohesiveSupports(sdm));
1781fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
17829566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
17839566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
17849566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
17859566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1786cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
17872582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1788cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1789cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1790cd0c2139SMatthew G Knepley 
17919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
17929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
17939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1794cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d];
1795cd0c2139SMatthew G Knepley   }
17969566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1797fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
17989566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1799b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1800fa8e8ae5SToby Isaac   /* Step 9: Labels */
18019566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
18029566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
18039566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1804cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1805cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
18062582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1807cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1808cd0c2139SMatthew G Knepley       PetscInt       l;
1809cd0c2139SMatthew G Knepley 
18107db7e0a7SMatthew G. Knepley       if (splitLabel) {
18117db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
18127db7e0a7SMatthew G. Knepley 
18139566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp, val));
18149566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
18157db7e0a7SMatthew G. Knepley       }
1816cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1817cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1818cd0c2139SMatthew G Knepley         const char *lname;
1819cd0c2139SMatthew G Knepley         PetscInt    val;
18209a356370SMatthew G. Knepley         PetscBool   isDepth;
1821cd0c2139SMatthew G Knepley 
18229566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
18239566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
18249a356370SMatthew G. Knepley         if (isDepth) continue;
18259566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
18269566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
182748a46eb9SPierre Jolivet         if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val));
1828cd0c2139SMatthew G Knepley       }
1829cd0c2139SMatthew G Knepley     }
1830cd0c2139SMatthew G Knepley   }
1831cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1832cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1833cd0c2139SMatthew G Knepley 
1834cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
18359566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
18369566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
18379566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
18389566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1839cd0c2139SMatthew G Knepley   }
1840b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1841b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1842cd0c2139SMatthew G Knepley   if (label) {
18439566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
18449566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1845cd0c2139SMatthew G Knepley   }
18460d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
18479566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
184836dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
18490d4d4d06SMatthew G. Knepley   }
18509566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
18519566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
18529566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
18533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1854cd0c2139SMatthew G Knepley }
1855cd0c2139SMatthew G Knepley 
1856cd0c2139SMatthew G Knepley /*@C
1857cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1858cd0c2139SMatthew G Knepley 
185920f4b53cSBarry Smith   Collective
1860cd0c2139SMatthew G Knepley 
1861cd0c2139SMatthew G Knepley   Input Parameters:
186220f4b53cSBarry Smith + dm    - The original `DM`
186320f4b53cSBarry Smith - label - The `DMLabel` specifying the boundary faces (this could be auto-generated)
1864cd0c2139SMatthew G Knepley 
1865cd0c2139SMatthew G Knepley   Output Parameters:
186620f4b53cSBarry Smith + splitLabel - The `DMLabel` containing the split points, or `NULL` if no output is desired
186720f4b53cSBarry Smith - dmSplit    - The new `DM`
1868cd0c2139SMatthew G Knepley 
1869cd0c2139SMatthew G Knepley   Level: developer
1870cd0c2139SMatthew G Knepley 
18711cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1872cd0c2139SMatthew G Knepley @*/
1873d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1874d71ae5a4SJacob Faibussowitsch {
1875cd0c2139SMatthew G Knepley   DM       sdm;
1876cd0c2139SMatthew G Knepley   PetscInt dim;
1877cd0c2139SMatthew G Knepley 
1878cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1879cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18804f572ea9SToby Isaac   PetscAssertPointer(dmSplit, 4);
18819566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
18829566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
18839566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
18849566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1885cd0c2139SMatthew G Knepley   switch (dim) {
1886cd0c2139SMatthew G Knepley   case 2:
1887d71ae5a4SJacob Faibussowitsch   case 3:
1888d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1889d71ae5a4SJacob Faibussowitsch     break;
1890d71ae5a4SJacob Faibussowitsch   default:
1891d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1892cd0c2139SMatthew G Knepley   }
1893cd0c2139SMatthew G Knepley   *dmSplit = sdm;
18943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1895cd0c2139SMatthew G Knepley }
1896cd0c2139SMatthew G Knepley 
18970f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
1898d71ae5a4SJacob Faibussowitsch static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1899d71ae5a4SJacob Faibussowitsch {
19000f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
19010f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
19020f66a230SMatthew G. Knepley 
19030f66a230SMatthew G. Knepley   PetscFunctionBegin;
19040f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
19059566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19079566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
19089566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
19090f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
19100f66a230SMatthew G. Knepley     if (cone[c] == face) {
19110f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
19120f66a230SMatthew G. Knepley 
19130f66a230SMatthew G. Knepley       if (subdm) {
19140f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
19150f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
19160f66a230SMatthew G. Knepley 
19179566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
19189566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
19199566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
19209566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
19219566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
19220f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
19230f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
19240f66a230SMatthew G. Knepley             o = subornt[0];
19250f66a230SMatthew G. Knepley             break;
19260f66a230SMatthew G. Knepley           }
19270f66a230SMatthew G. Knepley         }
192863a3b9bcSJacob 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);
19290f66a230SMatthew G. Knepley       }
19300f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
19310f66a230SMatthew G. Knepley       else *pos = PETSC_FALSE;
19320f66a230SMatthew G. Knepley       break;
19330f66a230SMatthew G. Knepley     }
19340f66a230SMatthew G. Knepley   }
193563a3b9bcSJacob 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);
19363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19370f66a230SMatthew G. Knepley }
19380f66a230SMatthew G. Knepley 
1939d71ae5a4SJacob Faibussowitsch static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label)
1940d71ae5a4SJacob Faibussowitsch {
1941accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
1942accc9626SMatthew G. Knepley   const PetscInt *points;
1943accc9626SMatthew G. Knepley   PetscInt        dim, numPoints, p, shift = 100, shift2 = 200;
1944accc9626SMatthew G. Knepley 
1945accc9626SMatthew G. Knepley   PetscFunctionBegin;
1946accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1947accc9626SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
1948accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
1949accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
1950accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
1951accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
1952accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
19533ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
1954accc9626SMatthew G. Knepley   }
1955accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
1956accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
1957accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
1958accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
1959accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
1960accc9626SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
1961accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
1962accc9626SMatthew G. Knepley     const PetscInt *support;
1963accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
1964accc9626SMatthew G. Knepley 
1965accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
1966accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
1967accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
1968accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
1969accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
1970accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
1971accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
1972e1a13daeSMatthew G. Knepley     /* Check that this face is not incident on only unsplit faces, meaning has at least one split face */
1973e1a13daeSMatthew G. Knepley     {
1974e1a13daeSMatthew G. Knepley       PetscInt *closure = NULL;
1975e1a13daeSMatthew G. Knepley       PetscBool split   = PETSC_FALSE;
1976e1a13daeSMatthew G. Knepley       PetscInt  closureSize, cl;
1977e1a13daeSMatthew G. Knepley 
1978e1a13daeSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1979e1a13daeSMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1980e1a13daeSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
19819371c9d4SSatish Balay         if ((valA >= 0) && (valA <= dim)) {
19829371c9d4SSatish Balay           split = PETSC_TRUE;
19839371c9d4SSatish Balay           break;
19849371c9d4SSatish Balay         }
1985e1a13daeSMatthew G. Knepley       }
1986e1a13daeSMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1987e1a13daeSMatthew G. Knepley       if (!split) continue;
1988e1a13daeSMatthew G. Knepley     }
1989accc9626SMatthew G. Knepley     /* Split the face */
1990accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, point, &valA));
1991accc9626SMatthew G. Knepley     PetscCall(DMLabelClearValue(label, point, valA));
1992accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, dim - 1));
1993accc9626SMatthew G. Knepley     /* Label its closure:
1994accc9626SMatthew G. Knepley       unmarked: label as unsplit
1995accc9626SMatthew G. Knepley       incident: relabel as split
1996accc9626SMatthew G. Knepley       split:    do nothing
1997accc9626SMatthew G. Knepley     */
1998accc9626SMatthew G. Knepley     {
1999accc9626SMatthew G. Knepley       PetscInt *closure = NULL;
2000accc9626SMatthew G. Knepley       PetscInt  closureSize, cl, dep;
2001accc9626SMatthew G. Knepley 
2002accc9626SMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2003accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2004accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
2005accc9626SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
2006accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2007accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
2008accc9626SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2009accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
2010accc9626SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], valA));
2011accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], dep));
2012accc9626SMatthew G. Knepley         }
2013accc9626SMatthew G. Knepley       }
2014accc9626SMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2015accc9626SMatthew G. Knepley     }
2016accc9626SMatthew G. Knepley   }
2017accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2018accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
20193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2020accc9626SMatthew G. Knepley }
2021accc9626SMatthew G. Knepley 
2022cd0c2139SMatthew G Knepley /*@
20230f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
2024cd0c2139SMatthew G Knepley   to complete the surface
2025cd0c2139SMatthew G Knepley 
2026cd0c2139SMatthew G Knepley   Input Parameters:
202720f4b53cSBarry Smith + dm     - The `DM`
202820f4b53cSBarry Smith . label  - A `DMLabel` marking the surface
202920f4b53cSBarry Smith . blabel - A `DMLabel` marking the vertices on the boundary which will not be duplicated, or `NULL` to find them automatically
203020f4b53cSBarry Smith . bvalue - Value of `DMLabel` marking the vertices on the boundary
2031bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
203220f4b53cSBarry Smith - subdm  - The `DM` associated with the label, or `NULL`
2033cd0c2139SMatthew G Knepley 
2034cd0c2139SMatthew G Knepley   Output Parameter:
203520f4b53cSBarry Smith . label - A `DMLabel` marking all surface points
20360f66a230SMatthew G. Knepley 
2037cd0c2139SMatthew G Knepley   Level: developer
2038cd0c2139SMatthew G Knepley 
203920f4b53cSBarry Smith   Note:
204020f4b53cSBarry Smith   The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
204120f4b53cSBarry Smith 
20421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2043cd0c2139SMatthew G Knepley @*/
2044d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, DM subdm)
2045d71ae5a4SJacob Faibussowitsch {
2046d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2047accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
204847946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2049bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
2050accc9626SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = 300, dim, depth, numPoints, numSubpoints, p, val;
2051cd0c2139SMatthew G Knepley 
2052cd0c2139SMatthew G Knepley   PetscFunctionBegin;
20539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
20549566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
20559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
205647946fd8SMatthew G. Knepley   if (subdm) {
20579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
205847946fd8SMatthew G. Knepley     if (subpointIS) {
20599566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
20609566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
206147946fd8SMatthew G. Knepley     }
206247946fd8SMatthew G. Knepley   }
2063d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
20649566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2065accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
20669566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
20679566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2068d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2069cd0c2139SMatthew G Knepley     const PetscInt *support;
2070cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2071cd0c2139SMatthew G Knepley 
20729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2073c4419245SMatthew G. Knepley #if 0
2074c4419245SMatthew G. Knepley     if (supportSize != 2) {
2075c4419245SMatthew G. Knepley       const PetscInt *lp;
2076c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2077c4419245SMatthew G. Knepley 
2078c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2079c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
20809566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
20819566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
208263a3b9bcSJacob 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);
2083c4419245SMatthew G. Knepley     }
2084c4419245SMatthew G. Knepley #endif
20859566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2086cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
20870f66a230SMatthew G. Knepley       const PetscInt *cone;
2088cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
20890f66a230SMatthew G. Knepley       PetscBool       pos;
2090cd0c2139SMatthew G Knepley 
20919566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
20929566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
20939566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
20940f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
20950f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
20969566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
20979566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2098cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2099cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2100cd0c2139SMatthew G Knepley 
21019566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2102cd0c2139SMatthew G Knepley         if (val == -1) {
2103cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2104cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2105cd0c2139SMatthew G Knepley 
21069566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2107cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2108cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2109a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2110cd0c2139SMatthew G Knepley 
21119566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
21129566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2113a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim - 1) && (bval < 0)) {
21149566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2115cd0c2139SMatthew G Knepley               break;
2116cd0c2139SMatthew G Knepley             }
2117cd0c2139SMatthew G Knepley           }
21189566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2119cd0c2139SMatthew G Knepley         }
2120cd0c2139SMatthew G Knepley       }
2121cd0c2139SMatthew G Knepley     }
2122cd0c2139SMatthew G Knepley   }
2123accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2124accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2125a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
212686200784SMatthew G. Knepley   if (blabel) {
2127accc9626SMatthew G. Knepley     IS bdIS;
2128accc9626SMatthew G. Knepley 
2129caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2130accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2131accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2132a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2133a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2134a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2135a0541d8aSMatthew G. Knepley 
21369566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2137a0541d8aSMatthew G. Knepley       if (bval >= 0) {
21389566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2139f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2140f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
21419566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2142f7019248SMatthew G. Knepley         }
2143f7019248SMatthew G. Knepley       }
2144f7019248SMatthew G. Knepley     }
2145f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2146f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2147f7019248SMatthew G. Knepley       PetscInt       val, bval;
2148f7019248SMatthew G. Knepley 
21499566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2150f7019248SMatthew G. Knepley       if (bval >= 0) {
215186200784SMatthew G. Knepley         const PetscInt *cone, *support;
215286200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
215386200784SMatthew G. Knepley 
2154a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
21559566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2156e1a13daeSMatthew 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);
21579566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
21589566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
21592c06a818SMatthew G. Knepley         /* Check for cross-edge
21602c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
216186200784SMatthew G. Knepley         if (val != 0) continue;
21629566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
21639566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
216486200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
21659566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
21669566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
216763a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
21689566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
21699566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
21709566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
21719566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
217286200784SMatthew G. Knepley         }
2173a0541d8aSMatthew G. Knepley       }
2174a0541d8aSMatthew G. Knepley     }
2175accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2176accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2177a0541d8aSMatthew G. Knepley   }
2178b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2179b6dfa339SMatthew G. Knepley   {
2180b6dfa339SMatthew G. Knepley     PetscSF         sf;
2181b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2182b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2183b6dfa339SMatthew G. Knepley 
2184b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2185b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2186accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2187accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2188accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2189accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2190b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2191b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2192b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2193b6dfa339SMatthew G. Knepley         PetscInt       val;
2194b6dfa339SMatthew G. Knepley 
2195b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2196b6dfa339SMatthew G. Knepley         if (l >= 0) {
2197b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2198b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2199b6dfa339SMatthew G. Knepley 
2200b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2201b6dfa339SMatthew 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);
2202b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2203b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2204b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2205accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2206b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2207b6dfa339SMatthew G. Knepley 
2208b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2209b6dfa339SMatthew 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);
2210b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2211b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2212b6dfa339SMatthew G. Knepley           }
2213b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2214b6dfa339SMatthew G. Knepley         }
2215b6dfa339SMatthew G. Knepley       }
2216b6dfa339SMatthew G. Knepley     }
2217b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2218b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2219accc9626SMatthew G. Knepley   }
2220accc9626SMatthew G. Knepley divide:
2221b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2222accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
2223accc9626SMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label));
22243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2225cd0c2139SMatthew G Knepley }
2226cd0c2139SMatthew G Knepley 
2227720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
2228d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2229d71ae5a4SJacob Faibussowitsch {
2230720e594eSMatthew G. Knepley   IS              subpointIS;
2231720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2232720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2233720e594eSMatthew G. Knepley 
2234720e594eSMatthew G. Knepley   PetscFunctionBegin;
22353ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
22369566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
22379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
22383ba16761SJacob Faibussowitsch   if (!subpointIS) PetscFunctionReturn(PETSC_SUCCESS);
22399566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
22409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
22419566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2242720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2243720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2244720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2245720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2246720e594eSMatthew G. Knepley 
22479566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2248720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2249720e594eSMatthew G. Knepley       PetscInt value = 0;
2250720e594eSMatthew G. Knepley 
2251720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
22529566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
22539371c9d4SSatish Balay       if (value == defaultValue) {
22549371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
22559371c9d4SSatish Balay         break;
22569371c9d4SSatish Balay       }
2257720e594eSMatthew G. Knepley     }
22589566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2259720e594eSMatthew G. Knepley     if (invalidCell) {
22609566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
22619566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
22629566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
226363a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2264720e594eSMatthew G. Knepley     }
2265720e594eSMatthew G. Knepley   }
22669566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
22673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2268720e594eSMatthew G. Knepley }
2269720e594eSMatthew G. Knepley 
2270c08575a3SMatthew G. Knepley /*@
22713cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
22723cf72582SMatthew G. Knepley 
227320f4b53cSBarry Smith   Collective
22743cf72582SMatthew G. Knepley 
22753cf72582SMatthew G. Knepley   Input Parameters:
227620f4b53cSBarry Smith + dm      - The original `DM`
2277720e594eSMatthew G. Knepley . label   - The label specifying the interface vertices
2278caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2279caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
22803cf72582SMatthew G. Knepley 
22813cf72582SMatthew G. Knepley   Output Parameters:
228220f4b53cSBarry Smith + hybridLabel - The label fully marking the interface, or `NULL` if no output is desired
228320f4b53cSBarry Smith . splitLabel  - The label containing the split points, or `NULL` if no output is desired
228420f4b53cSBarry Smith . dmInterface - The new interface `DM`, or `NULL`
228520f4b53cSBarry Smith - dmHybrid    - The new `DM` with cohesive cells
22863cf72582SMatthew G. Knepley 
228720f4b53cSBarry Smith   Level: developer
228820f4b53cSBarry Smith 
228920f4b53cSBarry Smith   Note:
229020f4b53cSBarry Smith   The hybridLabel indicates what parts of the original mesh impinged on the division surface. For points
22916eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
22926eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
22936eccb800SMatthew 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
22946eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
22956eccb800SMatthew Knepley 
22966eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
229740cdb708SMatthew 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
22986eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
22996eccb800SMatthew Knepley 
230020f4b53cSBarry Smith   The dmInterface is a `DM` built from the original division surface. It has a label which can be retrieved using
230120f4b53cSBarry Smith   `DMPlexGetSubpointMap()` which maps each point back to the point in the surface of the original mesh.
23026eccb800SMatthew Knepley 
23031cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
23043cf72582SMatthew G. Knepley @*/
2305d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
2306d71ae5a4SJacob Faibussowitsch {
23073cf72582SMatthew G. Knepley   DM       idm;
23087db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
23093cf72582SMatthew G. Knepley   PetscInt dim;
23103cf72582SMatthew G. Knepley 
23113cf72582SMatthew G. Knepley   PetscFunctionBegin;
23123cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23134f572ea9SToby Isaac   if (label) PetscAssertPointer(label, 2);
23144f572ea9SToby Isaac   if (bdlabel) PetscAssertPointer(bdlabel, 3);
23154f572ea9SToby Isaac   if (hybridLabel) PetscAssertPointer(hybridLabel, 5);
23164f572ea9SToby Isaac   if (splitLabel) PetscAssertPointer(splitLabel, 6);
23174f572ea9SToby Isaac   if (dmInterface) PetscAssertPointer(dmInterface, 7);
23184f572ea9SToby Isaac   PetscAssertPointer(dmHybrid, 8);
23199566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
23209566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
23219566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
23229566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
23239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
23249566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
23259566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
23267db7e0a7SMatthew G. Knepley   if (splitLabel) {
23277db7e0a7SMatthew G. Knepley     const char *name;
23287db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
23297db7e0a7SMatthew G. Knepley 
23309566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
2331c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(sname, name, sizeof(sname)));
2332c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(sname, " split", sizeof(sname)));
23339566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
23347db7e0a7SMatthew G. Knepley   }
2335caf9e14dSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, idm));
23369371c9d4SSatish Balay   if (dmInterface) {
23379371c9d4SSatish Balay     *dmInterface = idm;
23389371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
23399566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
23405de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
23413cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
23429566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
23437db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
23444a7ee7d0SMatthew G. Knepley   {
23454a7ee7d0SMatthew G. Knepley     DM      cdm;
23464a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
23474a7ee7d0SMatthew G. Knepley 
23484a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
23499566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
23509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
23519566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
23524a7ee7d0SMatthew G. Knepley   }
23533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2354cd0c2139SMatthew G Knepley }
2355cd0c2139SMatthew G Knepley 
2356efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2357efa14ee0SMatthew G Knepley 
2358efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2359efa14ee0SMatthew G Knepley */
2360d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2361d71ae5a4SJacob Faibussowitsch {
2362fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2363efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2364412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2365efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2366efa14ee0SMatthew G Knepley 
2367efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2368efa14ee0SMatthew G Knepley   *numFaces = 0;
2369efa14ee0SMatthew G Knepley   *nFV      = 0;
23709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
23719566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
237277d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
23739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
237448a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2375efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
23769566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2377efa14ee0SMatthew G Knepley   if (subvertexIS) {
23789566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
23799566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2380efa14ee0SMatthew G Knepley   }
2381efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2382efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
23830298fd71SBarry Smith     PetscInt      *star   = NULL;
2384efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2385efa14ee0SMatthew G Knepley 
23869566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2387efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2388efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2389efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2390efa14ee0SMatthew G Knepley     }
2391efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2392efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
23930298fd71SBarry Smith       PetscInt      *closure = NULL;
2394efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2395efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2396efa14ee0SMatthew G Knepley 
23979566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
239865560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
239963a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
24009566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2401efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2402efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2403efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2404efa14ee0SMatthew G Knepley 
2405efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2406efa14ee0SMatthew G Knepley           ++numCorners;
24079566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2408830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2409efa14ee0SMatthew G Knepley         }
2410efa14ee0SMatthew G Knepley       }
24119566063dSJacob Faibussowitsch       if (!(*nFV)) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
241263a3b9bcSJacob 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);
2413efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2414007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2415007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2416007baee2SMatthew G. Knepley 
2417efa14ee0SMatthew G Knepley         ++(*numFaces);
241848a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
24199566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
242048a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
24219566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2422efa14ee0SMatthew G Knepley       }
24239566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2424efa14ee0SMatthew G Knepley     }
24259566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2426efa14ee0SMatthew G Knepley   }
242748a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
24289566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
24299566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
24303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2431efa14ee0SMatthew G Knepley }
2432efa14ee0SMatthew G Knepley 
2433d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2434d71ae5a4SJacob Faibussowitsch {
243534b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2436efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2437412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2438efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2439efa14ee0SMatthew G Knepley 
2440efa14ee0SMatthew G Knepley   PetscFunctionBegin;
24419566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24429566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
244348a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2444efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
244534b4c39eSMatthew G. Knepley   if (vertexLabel) {
24469566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2447efa14ee0SMatthew G Knepley     if (subvertexIS) {
24489566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24499566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2450efa14ee0SMatthew G Knepley     }
245134b4c39eSMatthew G. Knepley   }
2452efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2453efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24540298fd71SBarry Smith     PetscInt      *star   = NULL;
2455efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2456efa14ee0SMatthew G Knepley 
24579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2458efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2459efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2460158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2461158acfadSMatthew G. Knepley 
2462158acfadSMatthew G. Knepley       if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) {
2463158acfadSMatthew G. Knepley         if (markedFaces) {
24649566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2465158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2466158acfadSMatthew G. Knepley         }
2467158acfadSMatthew G. Knepley         star[numFaces++] = point;
2468158acfadSMatthew G. Knepley       }
2469efa14ee0SMatthew G Knepley     }
2470efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2471efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
24720298fd71SBarry Smith       PetscInt      *closure = NULL;
2473efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2474efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2475efa14ee0SMatthew G Knepley 
24769566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2477efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
247863a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
24799566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2480efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2481efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2482efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2483efa14ee0SMatthew G Knepley 
2484efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
24859566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2486830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2487efa14ee0SMatthew G Knepley         }
2488efa14ee0SMatthew G Knepley       }
2489efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2490efa14ee0SMatthew G Knepley         const PetscInt *support;
2491efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2492efa14ee0SMatthew G Knepley 
2493efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2494efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2495efa14ee0SMatthew G Knepley 
2496efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2497efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
24989566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2499efa14ee0SMatthew G Knepley               break;
2500efa14ee0SMatthew G Knepley             }
2501efa14ee0SMatthew G Knepley           }
2502efa14ee0SMatthew G Knepley         }
25039566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
25049566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
250548a46eb9SPierre Jolivet         for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2506efa14ee0SMatthew G Knepley       }
25079566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2508efa14ee0SMatthew G Knepley     }
25099566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2510efa14ee0SMatthew G Knepley   }
25119566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
25129566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
25139566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
25143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2515efa14ee0SMatthew G Knepley }
2516efa14ee0SMatthew G Knepley 
2517d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2518d71ae5a4SJacob Faibussowitsch {
251927c04023SMatthew G. Knepley   DMLabel         label = NULL;
2520766ab985SMatthew G. Knepley   const PetscInt *cone;
25219fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2522766ab985SMatthew G. Knepley 
2523812bfc34SJed Brown   PetscFunctionBegin;
2524c0ed958bSJed Brown   *numFaces = 0;
2525c0ed958bSJed Brown   *nFV      = 0;
25269566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2527fed694aaSMatthew G. Knepley   *subCells = NULL;
25289566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25299566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
25303ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
253127c04023SMatthew G. Knepley   if (label) {
253227c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
253327c04023SMatthew G. Knepley       PetscInt val;
253427c04023SMatthew G. Knepley 
25359566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
253627c04023SMatthew G. Knepley       if (val == value) {
253727c04023SMatthew G. Knepley         ++(*numFaces);
25389566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
253927c04023SMatthew G. Knepley       }
254027c04023SMatthew G. Knepley     }
254127c04023SMatthew G. Knepley   } else {
2542766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
25439566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
254427c04023SMatthew G. Knepley   }
25459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
25463ba16761SJacob Faibussowitsch   if (!(*numFaces)) PetscFunctionReturn(PETSC_SUCCESS);
25479fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2548766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2549766ab985SMatthew G. Knepley     const PetscInt *cells;
2550766ab985SMatthew G. Knepley     PetscInt        numCells;
2551766ab985SMatthew G. Knepley 
255227c04023SMatthew G. Knepley     if (label) {
255327c04023SMatthew G. Knepley       PetscInt val;
255427c04023SMatthew G. Knepley 
25559566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
255627c04023SMatthew G. Knepley       if (val != value) continue;
255727c04023SMatthew G. Knepley     }
25589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
255948a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2560766ab985SMatthew G. Knepley     /* Negative face */
25619566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
256227234c99SMatthew G. Knepley     /* Not true in parallel
256308401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2564766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
25659566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
256627234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2567766ab985SMatthew G. Knepley     }
25689566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2569766ab985SMatthew G. Knepley     /* Positive face is not included */
2570766ab985SMatthew G. Knepley   }
25713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2572766ab985SMatthew G. Knepley }
2573766ab985SMatthew G. Knepley 
2574d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2575d71ae5a4SJacob Faibussowitsch {
2576766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2577766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2578766ab985SMatthew G. Knepley 
2579812bfc34SJed Brown   PetscFunctionBegin;
25809566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
25823ba16761SJacob Faibussowitsch   if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS);
25839566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
25849566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2585766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2586766ab985SMatthew G. Knepley     const PetscInt *cone;
2587766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2588b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2589766ab985SMatthew G. Knepley 
259027c04023SMatthew G. Knepley     if (label) {
25919566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
259227c04023SMatthew G. Knepley       if (val != value) continue;
259327c04023SMatthew G. Knepley     }
25949566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
25959566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
25969566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
25971dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2598b3154360SMatthew G. Knepley     /* Negative face */
25999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2600766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2601766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2602766ab985SMatthew G. Knepley 
2603766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2604766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
26059566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2606766ab985SMatthew G. Knepley           break;
2607766ab985SMatthew G. Knepley         }
2608766ab985SMatthew G. Knepley       }
2609766ab985SMatthew G. Knepley     }
26109566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2611766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2612766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2613766ab985SMatthew G. Knepley       const PetscInt *support;
2614766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2615766ab985SMatthew G. Knepley 
26169566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
261708401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
26189566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
261948a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2620766ab985SMatthew G. Knepley     }
2621766ab985SMatthew G. Knepley   }
26229566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
26233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2624766ab985SMatthew G. Knepley }
2625766ab985SMatthew G. Knepley 
2626d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2627d71ae5a4SJacob Faibussowitsch {
262882f516ccSBarry Smith   MPI_Comm       comm;
2629e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2630e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2631e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2632e6ccafaeSMatthew G Knepley 
263382f516ccSBarry Smith   PetscFunctionBegin;
26349566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
26359566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
263663a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2637e6ccafaeSMatthew G Knepley 
2638ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2639ddeab2a6SMatthew G. Knepley     /* Triangle */
2640e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2641e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2642ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2643ddeab2a6SMatthew G. Knepley     /* Triangle */
2644ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2645ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2646ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2647ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2648ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2649ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2650e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2651e6ccafaeSMatthew G Knepley     /* Quadratic line */
2652e6ccafaeSMatthew G Knepley     faceSize  = 1;
2653e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2654e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2655e6ccafaeSMatthew G Knepley     /* Quads */
2656e6ccafaeSMatthew G Knepley     faceSize = 2;
2657e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2658e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2659e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2660e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2661e6ccafaeSMatthew G Knepley     } else {
2662e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2663e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2664e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2665e6ccafaeSMatthew G Knepley     }
2666e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2667e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2668e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2669e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2670e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2671e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2672e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2673e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2674e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2675e6ccafaeSMatthew G Knepley       2, 3, 5, /* left */
2676e6ccafaeSMatthew G Knepley     };
2677e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2678e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2679e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2680e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2681e6ccafaeSMatthew G Knepley     };
2682e6ccafaeSMatthew G Knepley 
2683e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
26849566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2685e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2686e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2687e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2688e6ccafaeSMatthew G Knepley 
26899371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) {
2690e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2691e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2692e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2693e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2694e6ccafaeSMatthew G Knepley               break;
2695e6ccafaeSMatthew G Knepley             }
2696e6ccafaeSMatthew G Knepley           }
2697e6ccafaeSMatthew G Knepley         }
2698e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2699e6ccafaeSMatthew G Knepley         break;
2700e6ccafaeSMatthew G Knepley       }
2701e6ccafaeSMatthew G Knepley     }
270228b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2703e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
27043ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2705e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2706e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2707e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2708e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2709e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2710e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2711e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2712e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2713e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2714e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2715e6ccafaeSMatthew G Knepley       0, 3, 7, /* left */
2716e6ccafaeSMatthew G Knepley     };
2717e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2718e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2719e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2720e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2721e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2722e6ccafaeSMatthew G Knepley     };
2723e6ccafaeSMatthew G Knepley 
2724e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
27259566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2726e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2727e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2728e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2729e6ccafaeSMatthew G Knepley 
27309371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) {
2731e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2732e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2733e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2734e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2735e6ccafaeSMatthew G Knepley               break;
2736e6ccafaeSMatthew G Knepley             }
2737e6ccafaeSMatthew G Knepley           }
2738e6ccafaeSMatthew G Knepley         }
2739e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2740e6ccafaeSMatthew G Knepley         break;
2741e6ccafaeSMatthew G Knepley       }
2742e6ccafaeSMatthew G Knepley     }
274328b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2744e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
27453ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2746e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2747e6ccafaeSMatthew G Knepley     /* Hexes
2748e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2749e6ccafaeSMatthew G Knepley        pointing up at the second.
2750e6ccafaeSMatthew G Knepley 
2751e6ccafaeSMatthew G Knepley           7---6
2752e6ccafaeSMatthew G Knepley          /|  /|
2753e6ccafaeSMatthew G Knepley         4---5 |
2754ddeab2a6SMatthew G. Knepley         | 1-|-2
2755e6ccafaeSMatthew G Knepley         |/  |/
2756ddeab2a6SMatthew G. Knepley         0---3
2757e6ccafaeSMatthew G Knepley 
2758e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2759e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2760e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2761e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2762e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2763e6ccafaeSMatthew G Knepley       0, 1, 2, 3, /* bottom */
2764e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2765ddeab2a6SMatthew G. Knepley       0, 3, 4, 5, /* front */
2766ddeab2a6SMatthew G. Knepley       2, 3, 5, 6, /* right */
2767ddeab2a6SMatthew G. Knepley       1, 2, 6, 7, /* back */
2768ddeab2a6SMatthew G. Knepley       0, 1, 4, 7, /* left */
2769e6ccafaeSMatthew G Knepley     };
2770e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2771ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2772e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2773ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2774ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2775ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2776ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2777e6ccafaeSMatthew G Knepley     };
2778e6ccafaeSMatthew G Knepley 
2779e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
27809566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2781e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2782e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2783e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2784e6ccafaeSMatthew G Knepley 
27859371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) {
2786e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2787e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2788e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2789e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2790e6ccafaeSMatthew G Knepley               break;
2791e6ccafaeSMatthew G Knepley             }
2792e6ccafaeSMatthew G Knepley           }
2793e6ccafaeSMatthew G Knepley         }
2794e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2795e6ccafaeSMatthew G Knepley         break;
2796e6ccafaeSMatthew G Knepley       }
2797e6ccafaeSMatthew G Knepley     }
279828b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2799e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28003ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2801e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2802e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2803e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2804e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2805e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2806e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2807e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2808e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2809e6ccafaeSMatthew G Knepley       0, 3, 4, 6, 7, 9, /* front */
2810e6ccafaeSMatthew G Knepley       1, 4, 5, 7, 8, 9, /* right */
2811e6ccafaeSMatthew G Knepley       2, 3, 5, 6, 8, 9, /* left */
2812e6ccafaeSMatthew G Knepley     };
2813e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2814e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2815e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2816e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2817e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2818e6ccafaeSMatthew G Knepley     };
2819e6ccafaeSMatthew G Knepley 
2820e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
28219566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2822e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2823e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2824e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2825e6ccafaeSMatthew G Knepley 
28269371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) {
2827e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2828e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2829e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2830e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2831e6ccafaeSMatthew G Knepley               break;
2832e6ccafaeSMatthew G Knepley             }
2833e6ccafaeSMatthew G Knepley           }
2834e6ccafaeSMatthew G Knepley         }
2835e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2836e6ccafaeSMatthew G Knepley         break;
2837e6ccafaeSMatthew G Knepley       }
2838e6ccafaeSMatthew G Knepley     }
283928b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2840e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28413ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2842e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2843e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2844e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2845e6ccafaeSMatthew G Knepley        pointing up at the second.
2846e6ccafaeSMatthew G Knepley 
2847e6ccafaeSMatthew G Knepley          7---6
2848e6ccafaeSMatthew G Knepley         /|  /|
2849e6ccafaeSMatthew G Knepley        4---5 |
2850e6ccafaeSMatthew G Knepley        | 3-|-2
2851e6ccafaeSMatthew G Knepley        |/  |/
2852e6ccafaeSMatthew G Knepley        0---1
2853e6ccafaeSMatthew G Knepley 
2854e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2855e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2856e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2857e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2858e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2859e6ccafaeSMatthew G Knepley       0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2860e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2861e6ccafaeSMatthew G Knepley       0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2862e6ccafaeSMatthew G Knepley       1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2863e6ccafaeSMatthew G Knepley       2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2864e6ccafaeSMatthew G Knepley       0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2865e6ccafaeSMatthew G Knepley     };
2866e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2867e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2868e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2869e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2870e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2871e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2872e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2873e6ccafaeSMatthew G Knepley     };
2874e6ccafaeSMatthew G Knepley 
2875e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
28769566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2877e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2878e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2879e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2880e6ccafaeSMatthew G Knepley 
28819371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) {
2882e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2883e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2884e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2885e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2886e6ccafaeSMatthew G Knepley               break;
2887e6ccafaeSMatthew G Knepley             }
2888e6ccafaeSMatthew G Knepley           }
2889e6ccafaeSMatthew G Knepley         }
2890e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2891e6ccafaeSMatthew G Knepley         break;
2892e6ccafaeSMatthew G Knepley       }
2893e6ccafaeSMatthew G Knepley     }
289428b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2895e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
28963ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2897e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2898e6ccafaeSMatthew G Knepley   if (!posOrient) {
28999566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
2900e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
2901e6ccafaeSMatthew G Knepley   } else {
29029566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
2903e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2904e6ccafaeSMatthew G Knepley   }
2905e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
29063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2907e6ccafaeSMatthew G Knepley }
2908e6ccafaeSMatthew G Knepley 
2909c08575a3SMatthew G. Knepley /*@
2910c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2911c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2912c08575a3SMatthew G. Knepley 
291320f4b53cSBarry Smith   Not Collective
2914c08575a3SMatthew G. Knepley 
2915c08575a3SMatthew G. Knepley   Input Parameters:
2916c08575a3SMatthew G. Knepley + dm           - The original mesh
2917c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2918c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2919c08575a3SMatthew G. Knepley . face         - The face vertices
2920c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2921c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2922c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2923c08575a3SMatthew G. Knepley 
2924d8d19677SJose E. Roman   Output Parameters:
2925c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
292620f4b53cSBarry Smith - posOriented  - `PETSC_TRUE` if the face was oriented with outward normal
2927c08575a3SMatthew G. Knepley 
2928c08575a3SMatthew G. Knepley   Level: developer
2929c08575a3SMatthew G. Knepley 
29301cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
2931c08575a3SMatthew G. Knepley @*/
2932d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2933d71ae5a4SJacob Faibussowitsch {
29340298fd71SBarry Smith   const PetscInt *cone = NULL;
2935e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2936e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2937e6ccafaeSMatthew G Knepley 
2938e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
29399566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
29409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
2941e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2942e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2943e6ccafaeSMatthew G Knepley 
2944e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2945e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
29469371c9d4SSatish Balay         found = PETSC_TRUE;
29479371c9d4SSatish Balay         break;
2948e6ccafaeSMatthew G Knepley       }
2949e6ccafaeSMatthew G Knepley     }
2950e6ccafaeSMatthew G Knepley     if (found) {
2951e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2952e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2953e6ccafaeSMatthew G Knepley       ++v2;
2954e6ccafaeSMatthew G Knepley     } else {
2955e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2956e6ccafaeSMatthew G Knepley     }
2957e6ccafaeSMatthew G Knepley   }
29589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
29593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2960e6ccafaeSMatthew G Knepley }
2961e6ccafaeSMatthew G Knepley 
2962e6ccafaeSMatthew G Knepley /*
2963cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2964e6ccafaeSMatthew G Knepley 
296520f4b53cSBarry Smith   Not Collective
2966e6ccafaeSMatthew G Knepley 
2967e6ccafaeSMatthew G Knepley   Input Parameters:
296820f4b53cSBarry Smith   + dm              - The `DMPLEX`
2969e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
297020f4b53cSBarry Smith   . faceVertices    - The vertices in the face for `dm`
2971e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
297220f4b53cSBarry Smith   . numCorners      - The number of vertices in the `cell`
297320f4b53cSBarry Smith   . cell            - A cell in `dm` containing the face
2974e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2975e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2976e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2977e6ccafaeSMatthew G Knepley 
29782fe279fdSBarry Smith   Output Parameter:
2979e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2980e6ccafaeSMatthew G Knepley 
2981e6ccafaeSMatthew G Knepley   Level: developer
2982e6ccafaeSMatthew G Knepley */
2983d71ae5a4SJacob 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)
2984d71ae5a4SJacob Faibussowitsch {
298582f516ccSBarry Smith   MPI_Comm        comm;
2986e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
2987e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2988e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2989e6ccafaeSMatthew G Knepley 
2990e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
29919566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
29929566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
299363a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
2994e6ccafaeSMatthew G Knepley #if 0
2995e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
29969566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
2997e6ccafaeSMatthew G Knepley #else
2998e6ccafaeSMatthew G Knepley   {
2999e6ccafaeSMatthew G Knepley     PetscInt f;
3000e6ccafaeSMatthew G Knepley 
3001e6ccafaeSMatthew G Knepley     numFaces = 0;
30029566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3003e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
3004e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
3005e6ccafaeSMatthew G Knepley 
30069566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
30079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
3008e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
3009e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
3010e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
3011e6ccafaeSMatthew G Knepley         PetscInt       v;
3012e6ccafaeSMatthew G Knepley 
3013e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
3014e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
3015e6ccafaeSMatthew G Knepley         }
3016e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
3017e6ccafaeSMatthew G Knepley       }
3018e6ccafaeSMatthew G Knepley       if (d == dof) {
3019e6ccafaeSMatthew G Knepley         numFaces               = 1;
3020e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
3021e6ccafaeSMatthew G Knepley       }
3022e6ccafaeSMatthew G Knepley     }
3023e6ccafaeSMatthew G Knepley   }
3024e6ccafaeSMatthew G Knepley #endif
302563a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3026f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3027e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
30289566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3029e6ccafaeSMatthew G Knepley   } else {
3030e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3031e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3032e6ccafaeSMatthew G Knepley 
30339566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3034e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3035e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
3036e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
30379566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3038e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3039e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3040e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3041e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3042e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3043e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3044e6ccafaeSMatthew G Knepley           break;
3045e6ccafaeSMatthew G Knepley         }
3046e6ccafaeSMatthew G Knepley       }
304763a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3048e6ccafaeSMatthew G Knepley     }
30499566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
30509566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
30519566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3052e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3053e6ccafaeSMatthew G Knepley   }
3054ef07cca7SMatthew G. Knepley #if 0
30559566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3056ef07cca7SMatthew G. Knepley #else
30579566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3058ef07cca7SMatthew G. Knepley #endif
30593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3060e6ccafaeSMatthew G Knepley }
3061e6ccafaeSMatthew G Knepley 
3062d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3063d71ae5a4SJacob Faibussowitsch {
306482f516ccSBarry Smith   MPI_Comm        comm;
306553156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3066efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
3067efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3068efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3069fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3070efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3071e6ccafaeSMatthew G Knepley 
3072e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30739566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3074efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
30759566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
30769566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
30779566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
30789566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3079efa14ee0SMatthew G Knepley   /* Setup chart */
30809566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
30819566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
30829566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
30839566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3084e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3085e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3086efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
3087e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
30889566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
30899566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
30909566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
30919566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
309248a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
309348a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
30949566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3095e6ccafaeSMatthew G Knepley   /* Create face cones */
30969566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
30979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
30989566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3099e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3100e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3101efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
31020298fd71SBarry Smith     PetscInt      *closure = NULL;
3103efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3104e6ccafaeSMatthew G Knepley 
31059566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3106efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3107efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3108e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3109e6ccafaeSMatthew G Knepley 
3110efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3111efa14ee0SMatthew G Knepley         ++numCorners;
31129566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3113efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3114efa14ee0SMatthew G Knepley           closure[faceSize] = point;
311565560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3116e6ccafaeSMatthew G Knepley           ++faceSize;
3117e6ccafaeSMatthew G Knepley         }
3118e6ccafaeSMatthew G Knepley       }
3119e6ccafaeSMatthew G Knepley     }
312063a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
312148a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
31229566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3123e6ccafaeSMatthew G Knepley   }
31249566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
31259566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
31269566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3127e6ccafaeSMatthew G Knepley   /* Build coordinates */
3128efa14ee0SMatthew G Knepley   {
3129efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3130efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3131efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3132285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
313324640c55SToby Isaac     const char  *name;
3134efa14ee0SMatthew G Knepley 
31359566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
31369566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
31379566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
31389566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
31399566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
31409566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
31419566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3142efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3143efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3144efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3145efa14ee0SMatthew G Knepley       PetscInt       dof;
3146efa14ee0SMatthew G Knepley 
31479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
31489566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
31499566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3150e6ccafaeSMatthew G Knepley     }
31519566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
31529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
31539566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
31549566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
31559566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
31569566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
31579566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3158830e53efSMatthew G. Knepley     if (coordSize) {
31599566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
31609566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3161efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3162efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3163efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3164efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3165e6ccafaeSMatthew G Knepley 
31669566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
31679566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
31689566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
31699566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
317063a3b9bcSJacob 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);
3171e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3172e6ccafaeSMatthew G Knepley       }
31739566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
31749566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
31753b399e24SMatthew G. Knepley     }
31769566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
31779566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3178e6ccafaeSMatthew G Knepley   }
3179efa14ee0SMatthew G Knepley   /* Cleanup */
31809566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
31819566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
31829566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
31839566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
31843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3185e6ccafaeSMatthew G Knepley }
3186e6ccafaeSMatthew G Knepley 
3187d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3188d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
3189d71ae5a4SJacob Faibussowitsch {
31903982b651SMatthew G. Knepley   PetscInt       subPoint;
31913982b651SMatthew G. Knepley   PetscErrorCode ierr;
31923982b651SMatthew G. Knepley 
31939371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
31949371c9d4SSatish Balay   if (ierr) return -1;
31953982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
31963982b651SMatthew G. Knepley }
31973982b651SMatthew G. Knepley 
3198d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3199d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[])
3200d71ae5a4SJacob Faibussowitsch {
3201dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3202dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3203dd05d810SMatthew G. Knepley 
3204dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3205dd05d810SMatthew G. Knepley   if (ierr) return -1;
3206dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3207dd05d810SMatthew G. Knepley }
3208dd05d810SMatthew G. Knepley 
3209d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3210d71ae5a4SJacob Faibussowitsch {
3211695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3212212103e5SMatthew Knepley   PetscInt Nl, l, d;
3213212103e5SMatthew Knepley 
3214212103e5SMatthew Knepley   PetscFunctionBegin;
3215695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3216695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3217695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
32189566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3219212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3220212103e5SMatthew Knepley     DMLabel         label, newlabel;
3221212103e5SMatthew Knepley     const char     *lname;
3222d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3223212103e5SMatthew Knepley     IS              valueIS;
3224212103e5SMatthew Knepley     const PetscInt *values;
3225212103e5SMatthew Knepley     PetscInt        Nv, v;
3226212103e5SMatthew Knepley 
32279566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
32289566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
32299566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
32309566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
32319566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3232d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
32339566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
32349566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
32359566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
32369566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
32379566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
32389566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
32399566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
32409566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3241212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3242212103e5SMatthew Knepley       IS              pointIS;
3243212103e5SMatthew Knepley       const PetscInt *points;
3244212103e5SMatthew Knepley       PetscInt        Np, p;
3245212103e5SMatthew Knepley 
32469566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
32479566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
32489566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3249212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3250212103e5SMatthew Knepley         const PetscInt point = points[p];
3251212103e5SMatthew Knepley         PetscInt       subp;
3252212103e5SMatthew Knepley 
32539566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3254212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
32559566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3256212103e5SMatthew Knepley       }
32579566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
32589566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3259212103e5SMatthew Knepley     }
32609566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
32619566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3262212103e5SMatthew Knepley   }
32633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3264212103e5SMatthew Knepley }
3265212103e5SMatthew Knepley 
3266d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3267d71ae5a4SJacob Faibussowitsch {
326882f516ccSBarry Smith   MPI_Comm         comm;
326953156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3270efa14ee0SMatthew G Knepley   IS              *subpointIS;
3271efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
32723982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3273dd05d810SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, v;
32740d366550SMatthew G. Knepley   PetscMPIInt      rank;
3275e6ccafaeSMatthew G Knepley 
3276e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
32779566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
32789566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3279efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
32809566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
32819566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3282bec263e5SMatthew G. Knepley   if (cellHeight) {
32839566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
32849566063dSJacob Faibussowitsch     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3285bec263e5SMatthew G. Knepley   } else {
3286bec263e5SMatthew G. Knepley     DMLabel         depth;
3287bec263e5SMatthew G. Knepley     IS              pointIS;
3288bec263e5SMatthew G. Knepley     const PetscInt *points;
3289b85c8bf9SMatthew G. Knepley     PetscInt        numPoints = 0;
3290bec263e5SMatthew G. Knepley 
32919566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
32929566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3293b85c8bf9SMatthew G. Knepley     if (pointIS) {
32949566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
32959566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3296b85c8bf9SMatthew G. Knepley     }
3297bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3298bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3299bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3300bec263e5SMatthew G. Knepley 
33019566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3302bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
33039566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
33049566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3305bec263e5SMatthew G. Knepley       }
33069566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3307bec263e5SMatthew G. Knepley     }
33089566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
33099566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3310bec263e5SMatthew G. Knepley   }
3311efa14ee0SMatthew G Knepley   /* Setup chart */
33129566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3313dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
33149566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3315e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
33169566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3317e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3318e6ccafaeSMatthew G Knepley   }
3319dd05d810SMatthew G. Knepley   // Determine submesh dimension
3320dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3321dd05d810SMatthew G. Knepley   if (sdim > 0) {
3322dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3323dd05d810SMatthew G. Knepley     sdim = dim;
3324dd05d810SMatthew G. Knepley   } else {
3325dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3326dd05d810SMatthew G. Knepley     PetscInt lsdim;
3327dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3328dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3329a5a714f4SStefano Zampini     PetscCall(MPIU_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPI_MAX, comm));
3330dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3331dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3332dd05d810SMatthew G. Knepley   }
33339566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
33349566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3335e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3336dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3337dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3338dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3339dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3340dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
33419566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
33429566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3343e6ccafaeSMatthew G Knepley   }
3344412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
33459566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3346dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3347e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3348e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3349e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3350e6ccafaeSMatthew G Knepley       const PetscInt *cone;
335115100a53SVaclav Hapla       PetscInt        coneSize;
3352e6ccafaeSMatthew G Knepley 
33539566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3354dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
335515100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
335615100a53SVaclav Hapla 
33579566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3358e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
33599566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3360e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3361e6ccafaeSMatthew G Knepley         }
33629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
33639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
336415100a53SVaclav Hapla       } else {
336515100a53SVaclav Hapla         DMPolytopeType ct;
336615100a53SVaclav Hapla 
336715100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
336815100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
336915100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3370e6ccafaeSMatthew G Knepley       }
3371e6ccafaeSMatthew G Knepley     }
3372e6ccafaeSMatthew G Knepley   }
33739566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
33749566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3375e6ccafaeSMatthew G Knepley   /* Set cones */
33769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
33779566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3378dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3379e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3380e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3381e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
33820e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
33830d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3384e6ccafaeSMatthew G Knepley 
3385dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
33860d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
33870d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
33880d366550SMatthew G. Knepley 
33899566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
33909566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
33910d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3392064cae4fSPierre Jolivet           PetscBool isHybrid = PETSC_FALSE;
3393412e9a14SMatthew G. Knepley 
33949566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3395412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
33969566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
33970d366550SMatthew G. Knepley           if (subc >= 0) {
33980d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
33990d366550SMatthew G. Knepley 
34009566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
34019566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
34029566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
34030d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
34040d366550SMatthew G. Knepley               if (cone[c] == point) {
34050d366550SMatthew G. Knepley                 fornt = ornt[c];
34060d366550SMatthew G. Knepley                 break;
34070d366550SMatthew G. Knepley               }
34080d366550SMatthew G. Knepley             }
34090d366550SMatthew G. Knepley             break;
34100d366550SMatthew G. Knepley           }
34110d366550SMatthew G. Knepley         }
34120d366550SMatthew G. Knepley       }
34139566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
34149566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
34159566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
34169566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3417e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
34189566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
341901a2673eSMatthew G. Knepley         if (subc >= 0) {
342001a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
34213982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
342201a2673eSMatthew G. Knepley           ++coneSizeNew;
342301a2673eSMatthew G. Knepley         }
3424e6ccafaeSMatthew G Knepley       }
342563a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
34269566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
34279566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
34289566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3429e6ccafaeSMatthew G Knepley     }
3430e6ccafaeSMatthew G Knepley   }
34319566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
34329566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
34339566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3434e6ccafaeSMatthew G Knepley   /* Build coordinates */
3435e6ccafaeSMatthew G Knepley   {
3436e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3437e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3438e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3439c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
344024640c55SToby Isaac     const char  *name;
3441e6ccafaeSMatthew G Knepley 
34429566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
34439566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
34449566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
34459566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
34469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
34479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
34489566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
34499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0] + numSubPoints[0]));
3450e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3451e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3452e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3453e6ccafaeSMatthew G Knepley       PetscInt       dof;
3454e6ccafaeSMatthew G Knepley 
34559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
34569566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
34579566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3458e6ccafaeSMatthew G Knepley     }
34599566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
34609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
34619566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
34629566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
34639566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
34649566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
34659566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
34669566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
34679566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
34689566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3469e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3470e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3471e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3472e6ccafaeSMatthew G Knepley       PetscInt       dof, off, sdof, soff, d;
3473e6ccafaeSMatthew G Knepley 
34749566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
34759566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
34769566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
34779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
347863a3b9bcSJacob 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);
3479efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3480e6ccafaeSMatthew G Knepley     }
34819566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
34829566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
34839566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
34849566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3485e6ccafaeSMatthew G Knepley   }
34863982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
34873982b651SMatthew G. Knepley   {
34883982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
34893982b651SMatthew G. Knepley     IS                 subpIS;
34903982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
34915033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
34925033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3493dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
34945033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
34955033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
34963982b651SMatthew G. Knepley 
34979566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
34985033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
34999566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
35009566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
35019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
35029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
35039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
35043982b651SMatthew G. Knepley     if (subpIS) {
3505dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3506dd05d810SMatthew G. Knepley 
35079566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
35089566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3509dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3510dd05d810SMatthew G. Knepley       if (!sorted) {
3511dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3512dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3513dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3514dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3515dd05d810SMatthew G. Knepley       }
35163982b651SMatthew G. Knepley     }
35179566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
35183982b651SMatthew G. Knepley     if (numRoots >= 0) {
35195033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
35205033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
35219566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
35223982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
35233982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
35243982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
35253982b651SMatthew G. Knepley       }
35263982b651SMatthew G. Knepley       /* Set subleaves */
35273982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
35283982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3529dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
35303982b651SMatthew G. Knepley 
35313982b651SMatthew G. Knepley         if (subpoint < 0) continue;
35323982b651SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
35333982b651SMatthew G. Knepley         newLocalPoints[point - pStart].index = subpoint;
35343982b651SMatthew G. Knepley         ++numSubleaves;
35353982b651SMatthew G. Knepley       }
35363982b651SMatthew G. Knepley       /* Must put in owned subpoints */
35373982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
35383982b651SMatthew G. Knepley         newOwners[p - pStart].rank  = -3;
35393982b651SMatthew G. Knepley         newOwners[p - pStart].index = -3;
35403982b651SMatthew G. Knepley       }
3541d9384f27SMatthew G. Knepley       for (p = 0; p < numSubpoints; ++p) {
35425033f954SMatthew G. Knepley         /* Hold on to currently owned points */
35435033f954SMatthew G. Knepley         if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
35445033f954SMatthew G. Knepley         else newOwners[subpoints[p] - pStart].rank = rank;
3545d9384f27SMatthew G. Knepley         newOwners[subpoints[p] - pStart].index = p;
35463982b651SMatthew G. Knepley       }
35479566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
35489566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
35499371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
35509371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
35519566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
35529566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
35539566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
35549566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
35555033f954SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
35563982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3557dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
35583982b651SMatthew G. Knepley 
35593982b651SMatthew G. Knepley         if (subpoint < 0) continue;
35609371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
35619371c9d4SSatish Balay           ++ll;
35629371c9d4SSatish Balay           continue;
35639371c9d4SSatish Balay         }
35643982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
35653982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
35663982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
356763a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
356863a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
35693982b651SMatthew G. Knepley         ++sl;
35703982b651SMatthew G. Knepley       }
35711dca8a05SBarry 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);
35729566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
35739566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
35743982b651SMatthew G. Knepley     }
357548a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3576dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
35773982b651SMatthew G. Knepley   }
3578212103e5SMatthew Knepley   /* Filter labels */
35799566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3580efa14ee0SMatthew G Knepley   /* Cleanup */
3581dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
35829566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
35839566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3584e6ccafaeSMatthew G Knepley   }
35859566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
35863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3587e6ccafaeSMatthew G Knepley }
3588e6ccafaeSMatthew G Knepley 
3589d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
3590d71ae5a4SJacob Faibussowitsch {
35913982b651SMatthew G. Knepley   PetscFunctionBegin;
35929566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm));
35933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35943982b651SMatthew G. Knepley }
35953982b651SMatthew G. Knepley 
3596d0fa310fSMatthew G. Knepley /*@
3597e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3598e6ccafaeSMatthew G Knepley 
3599e6ccafaeSMatthew G Knepley   Input Parameters:
3600e6ccafaeSMatthew G Knepley + dm          - The original mesh
360120f4b53cSBarry Smith . vertexLabel - The `DMLabel` marking points contained in the surface
3602158acfadSMatthew G. Knepley . value       - The label value to use
360320f4b53cSBarry Smith - markedFaces - `PETSC_TRUE` if surface faces are marked in addition to vertices, `PETSC_FALSE` if only vertices are marked
3604e6ccafaeSMatthew G Knepley 
3605e6ccafaeSMatthew G Knepley   Output Parameter:
3606e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3607e6ccafaeSMatthew G Knepley 
3608e6ccafaeSMatthew G Knepley   Level: developer
3609e6ccafaeSMatthew G Knepley 
361020f4b53cSBarry Smith   Note:
361120f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
361220f4b53cSBarry Smith 
36131cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3614830e53efSMatthew G. Knepley @*/
3615d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3616d71ae5a4SJacob Faibussowitsch {
3617827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3618827c4036SVaclav Hapla   PetscInt               dim, cdim;
3619e6ccafaeSMatthew G Knepley 
3620e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3621e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36224f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
36239566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
36249566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
36259566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
36269566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
36279566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
36289566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
36299566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
363008401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3631827c4036SVaclav Hapla   if (interpolated) {
36329566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3633e6ccafaeSMatthew G Knepley   } else {
36349566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3635e6ccafaeSMatthew G Knepley   }
36365de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
36373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3638e6ccafaeSMatthew G Knepley }
3639e6ccafaeSMatthew G Knepley 
3640d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3641d71ae5a4SJacob Faibussowitsch {
3642766ab985SMatthew G. Knepley   MPI_Comm        comm;
3643766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3644766ab985SMatthew G. Knepley   IS              subvertexIS;
3645766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3646fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3647766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3648412e9a14SMatthew G. Knepley   PetscInt        c, f;
3649766ab985SMatthew G. Knepley 
3650766ab985SMatthew G. Knepley   PetscFunctionBegin;
36519566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3652766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
36539566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
36549566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
36559566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
36569566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3657766ab985SMatthew G. Knepley   /* Setup chart */
36589566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
36599566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
36609566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
36619566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3662766ab985SMatthew G. Knepley   /* Set cone sizes */
3663766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3664766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3665766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
36669566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
36679566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
366848a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
366948a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
36709566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3671766ab985SMatthew G. Knepley   /* Create face cones */
36729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
36739566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3674766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3675766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3676766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
367787feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3678064cae4fSPierre Jolivet     PetscBool       isHybrid = PETSC_FALSE;
367987feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3680766ab985SMatthew G. Knepley 
36819566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3682412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
36839566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
368487feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
36859566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
368687feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
368787feddfdSMatthew G. Knepley     }
36889566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
36899566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
36909566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
369127234c99SMatthew G. Knepley     /* Not true in parallel
369208401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
369387feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
369487feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3695064cae4fSPierre Jolivet       PetscBool isHybrid = PETSC_FALSE;
3696766ab985SMatthew G. Knepley 
36979566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3698412e9a14SMatthew G. Knepley       if (isHybrid) continue;
369987feddfdSMatthew G. Knepley       /* I know this is a crap search */
370087feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
370187feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3702766ab985SMatthew G. Knepley       }
370363a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
37049566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3705766ab985SMatthew G. Knepley     }
37069566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
370787feddfdSMatthew G. Knepley     ++newFacePoint;
3708766ab985SMatthew G. Knepley   }
37099566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
37109566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
37119566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3712766ab985SMatthew G. Knepley   /* Build coordinates */
3713766ab985SMatthew G. Knepley   {
3714766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3715766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3716766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3717c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
371824640c55SToby Isaac     const char  *name;
3719766ab985SMatthew G. Knepley 
37209566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
37219566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
37229566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
37239566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
37249566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
37259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
37269566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
37279566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3728766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3729766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3730766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3731766ab985SMatthew G. Knepley       PetscInt       dof;
3732766ab985SMatthew G. Knepley 
37339566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
37349566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
37359566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3736766ab985SMatthew G. Knepley     }
37379566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
37389566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
37399566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
37409566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
37419566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
37429566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
37439566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
37449566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
37459566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
37469566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3747766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3748766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3749766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3750766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3751766ab985SMatthew G. Knepley 
37529566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
37539566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
37549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
37559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
375663a3b9bcSJacob 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);
3757766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3758766ab985SMatthew G. Knepley     }
37599566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
37609566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
37619566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
37629566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3763766ab985SMatthew G. Knepley   }
3764aca35d17SMatthew G. Knepley   /* Build SF */
3765aca35d17SMatthew G. Knepley   CHKMEMQ;
3766aca35d17SMatthew G. Knepley   {
3767aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3768aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3769bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3770aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3771bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
377249c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3773bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3774aca35d17SMatthew G. Knepley 
37759566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
37769566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
37779566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
37789566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
37799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
37809566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
3781aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3782aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
37839566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
3784bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
3785bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3786bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3787bdcf2095SMatthew G. Knepley       }
37889e0823b2SMatthew G. Knepley       /* Set subleaves */
3789aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3790aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3791bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3792aca35d17SMatthew G. Knepley 
379363a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
3794bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3795bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
3796bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
3797bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3798aca35d17SMatthew G. Knepley       }
37999e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
38009e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
38019e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
38029e0823b2SMatthew G. Knepley 
38039e0823b2SMatthew G. Knepley         if (subPoint < 0) {
38049e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
38059e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
38069e0823b2SMatthew G. Knepley         } else {
38079e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
38089e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
38099e0823b2SMatthew G. Knepley         }
3810bdcf2095SMatthew G. Knepley       }
38119566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
38129566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
38139566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
38149566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
38159566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
38169566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
381749c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3818bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3819bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3820aca35d17SMatthew G. Knepley 
3821aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
38229371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
38239371c9d4SSatish Balay           ++ll;
38249371c9d4SSatish Balay           continue;
38259371c9d4SSatish Balay         }
3826aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3827bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3828bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
382963a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
383063a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
3831aca35d17SMatthew G. Knepley         ++sl;
3832aca35d17SMatthew G. Knepley       }
38339566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
38341dca8a05SBarry 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);
38359566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
3836aca35d17SMatthew G. Knepley     }
3837aca35d17SMatthew G. Knepley   }
3838aca35d17SMatthew G. Knepley   CHKMEMQ;
3839766ab985SMatthew G. Knepley   /* Cleanup */
38409566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
38419566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
38429566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
38433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3844766ab985SMatthew G. Knepley }
3845766ab985SMatthew G. Knepley 
3846d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3847d71ae5a4SJacob Faibussowitsch {
38483982b651SMatthew G. Knepley   DMLabel label = NULL;
3849766ab985SMatthew G. Knepley 
3850766ab985SMatthew G. Knepley   PetscFunctionBegin;
38519566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
38529566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm));
38533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3854766ab985SMatthew G. Knepley }
3855766ab985SMatthew G. Knepley 
3856c08575a3SMatthew G. Knepley /*@C
385720f4b53cSBarry 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.
3858766ab985SMatthew G. Knepley 
3859766ab985SMatthew G. Knepley   Input Parameters:
3860766ab985SMatthew G. Knepley + dm          - The original mesh
386127c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
386220f4b53cSBarry Smith . label       - A label name, or `NULL`
386327c04023SMatthew G. Knepley - value       - A label value
3864766ab985SMatthew G. Knepley 
3865766ab985SMatthew G. Knepley   Output Parameter:
3866766ab985SMatthew G. Knepley . subdm - The surface mesh
3867766ab985SMatthew G. Knepley 
3868766ab985SMatthew G. Knepley   Level: developer
3869766ab985SMatthew G. Knepley 
387020f4b53cSBarry Smith   Note:
387120f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
387220f4b53cSBarry Smith 
38731cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
3874c08575a3SMatthew G. Knepley @*/
3875d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3876d71ae5a4SJacob Faibussowitsch {
3877c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
3878766ab985SMatthew G. Knepley 
3879766ab985SMatthew G. Knepley   PetscFunctionBegin;
3880766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38814f572ea9SToby Isaac   PetscAssertPointer(subdm, 5);
38829566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
38839566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
38849566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
38859566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
38869566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
38879566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
38889566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
3889766ab985SMatthew G. Knepley   if (depth == dim) {
38909566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
3891766ab985SMatthew G. Knepley   } else {
38929566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
3893e6ccafaeSMatthew G Knepley   }
38945de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
38953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3896e6ccafaeSMatthew G Knepley }
3897e6ccafaeSMatthew G Knepley 
3898bec263e5SMatthew G. Knepley /*@
38990e18dc48SMatthew G. Knepley   DMPlexReorderCohesiveSupports - Ensure that face supports for cohesive end caps are ordered
39000e18dc48SMatthew G. Knepley 
39010e18dc48SMatthew G. Knepley   Not Collective
39020e18dc48SMatthew G. Knepley 
39030e18dc48SMatthew G. Knepley   Input Parameter:
39040e18dc48SMatthew G. Knepley . dm - The `DM` containing cohesive cells
39050e18dc48SMatthew G. Knepley 
39060e18dc48SMatthew G. Knepley   Level: developer
39070e18dc48SMatthew G. Knepley 
3908*a4e35b19SJacob Faibussowitsch   Note:
3909*a4e35b19SJacob Faibussowitsch   For the negative size (first) face, the cohesive cell should be first in the support, and for
3910*a4e35b19SJacob Faibussowitsch   the positive side (second) face, the cohesive cell should be second in the support.
39110e18dc48SMatthew G. Knepley 
39120e18dc48SMatthew G. Knepley .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexCreateCohesiveSubmesh()`
39130e18dc48SMatthew G. Knepley @*/
39140e18dc48SMatthew G. Knepley PetscErrorCode DMPlexReorderCohesiveSupports(DM dm)
39150e18dc48SMatthew G. Knepley {
39160e18dc48SMatthew G. Knepley   PetscInt dim, cStart, cEnd;
39170e18dc48SMatthew G. Knepley 
39180e18dc48SMatthew G. Knepley   PetscFunctionBegin;
39190e18dc48SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
39200e18dc48SMatthew G. Knepley   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cStart, &cEnd));
39210e18dc48SMatthew G. Knepley   for (PetscInt c = cStart; c < cEnd; ++c) {
39220e18dc48SMatthew G. Knepley     const PetscInt *cone;
39230e18dc48SMatthew G. Knepley     PetscInt        coneSize;
39240e18dc48SMatthew G. Knepley 
39250e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
39260e18dc48SMatthew G. Knepley     PetscCall(DMPlexGetCone(dm, c, &cone));
39270e18dc48SMatthew G. Knepley     PetscCheck(coneSize >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid cell %" PetscInt_FMT " cone size %" PetscInt_FMT " must be >= 2", c, coneSize);
39280e18dc48SMatthew G. Knepley     for (PetscInt s = 0; s < 2; ++s) {
39290e18dc48SMatthew G. Knepley       const PetscInt *supp;
39300e18dc48SMatthew G. Knepley       PetscInt        suppSize, newsupp[2];
39310e18dc48SMatthew G. Knepley 
39320e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupportSize(dm, cone[s], &suppSize));
39330e18dc48SMatthew G. Knepley       PetscCall(DMPlexGetSupport(dm, cone[s], &supp));
39340e18dc48SMatthew G. Knepley       if (suppSize == 2) {
39350e18dc48SMatthew G. Knepley         /* Reorder hybrid end cap support */
39360e18dc48SMatthew G. Knepley         if (supp[s] == c) {
39370e18dc48SMatthew G. Knepley           newsupp[0] = supp[1];
39380e18dc48SMatthew G. Knepley           newsupp[1] = supp[0];
39390e18dc48SMatthew G. Knepley         } else {
39400e18dc48SMatthew G. Knepley           newsupp[0] = supp[0];
39410e18dc48SMatthew G. Knepley           newsupp[1] = supp[1];
39420e18dc48SMatthew G. Knepley         }
39430e18dc48SMatthew 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);
39440e18dc48SMatthew G. Knepley         PetscCall(DMPlexSetSupport(dm, cone[s], newsupp));
39450e18dc48SMatthew G. Knepley       }
39460e18dc48SMatthew G. Knepley     }
39470e18dc48SMatthew G. Knepley   }
39480e18dc48SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
39490e18dc48SMatthew G. Knepley }
39500e18dc48SMatthew G. Knepley 
39510e18dc48SMatthew G. Knepley /*@
3952bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3953bec263e5SMatthew G. Knepley 
3954bec263e5SMatthew G. Knepley   Input Parameters:
3955bec263e5SMatthew G. Knepley + dm        - The original mesh
395620f4b53cSBarry Smith . cellLabel - The `DMLabel` marking cells contained in the new mesh
3957bec263e5SMatthew G. Knepley - value     - The label value to use
3958bec263e5SMatthew G. Knepley 
3959bec263e5SMatthew G. Knepley   Output Parameter:
3960bec263e5SMatthew G. Knepley . subdm - The new mesh
3961bec263e5SMatthew G. Knepley 
3962bec263e5SMatthew G. Knepley   Level: developer
3963bec263e5SMatthew G. Knepley 
396420f4b53cSBarry Smith   Note:
396520f4b53cSBarry Smith   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
396620f4b53cSBarry Smith 
39671cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
3968bec263e5SMatthew G. Knepley @*/
3969d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3970d71ae5a4SJacob Faibussowitsch {
3971b47cd10cSMatthew G. Knepley   PetscInt dim, overlap;
3972bec263e5SMatthew G. Knepley 
3973bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3974bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39754f572ea9SToby Isaac   PetscAssertPointer(subdm, 4);
39769566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
39779566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
39789566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
3979bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
39809566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm));
39815de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3982b47cd10cSMatthew G. Knepley   // It is possible to obtain a surface mesh where some faces are in SF
3983b47cd10cSMatthew G. Knepley   //   We should either mark the mesh as having an overlap, or delete these from the SF
3984b47cd10cSMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
3985b47cd10cSMatthew G. Knepley   if (!overlap) {
3986b47cd10cSMatthew G. Knepley     PetscSF         sf;
3987b47cd10cSMatthew G. Knepley     const PetscInt *leaves;
3988b47cd10cSMatthew G. Knepley     PetscInt        cStart, cEnd, Nl;
3989b47cd10cSMatthew G. Knepley     PetscBool       hasSubcell = PETSC_FALSE, ghasSubcell;
3990b47cd10cSMatthew G. Knepley 
3991b47cd10cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd));
3992b47cd10cSMatthew G. Knepley     PetscCall(DMGetPointSF(*subdm, &sf));
3993b47cd10cSMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
3994b47cd10cSMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
3995b47cd10cSMatthew G. Knepley       const PetscInt point = leaves ? leaves[l] : l;
3996b47cd10cSMatthew G. Knepley 
3997b47cd10cSMatthew G. Knepley       if (point >= cStart && point < cEnd) {
3998b47cd10cSMatthew G. Knepley         hasSubcell = PETSC_TRUE;
3999b47cd10cSMatthew G. Knepley         break;
4000b47cd10cSMatthew G. Knepley       }
4001b47cd10cSMatthew G. Knepley     }
4002b47cd10cSMatthew G. Knepley     PetscCall(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
4003b47cd10cSMatthew G. Knepley     if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1));
4004b47cd10cSMatthew G. Knepley   }
40053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4006bec263e5SMatthew G. Knepley }
4007bec263e5SMatthew G. Knepley 
400864beef6dSMatthew G. Knepley /*@
400920f4b53cSBarry Smith   DMPlexGetSubpointMap - Returns a `DMLabel` with point dimension as values
401064beef6dSMatthew G. Knepley 
401164beef6dSMatthew G. Knepley   Input Parameter:
401220f4b53cSBarry Smith . dm - The submesh `DM`
401364beef6dSMatthew G. Knepley 
401464beef6dSMatthew G. Knepley   Output Parameter:
401520f4b53cSBarry Smith . subpointMap - The `DMLabel` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
401664beef6dSMatthew G. Knepley 
401764beef6dSMatthew G. Knepley   Level: developer
401864beef6dSMatthew G. Knepley 
40191cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
402064beef6dSMatthew G. Knepley @*/
4021d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
4022d71ae5a4SJacob Faibussowitsch {
4023e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4024e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40254f572ea9SToby Isaac   PetscAssertPointer(subpointMap, 2);
402665663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
40273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4028e6ccafaeSMatthew G Knepley }
4029e6ccafaeSMatthew G Knepley 
4030c08575a3SMatthew G. Knepley /*@
403120f4b53cSBarry Smith   DMPlexSetSubpointMap - Sets the `DMLabel` with point dimension as values
4032c08575a3SMatthew G. Knepley 
4033c08575a3SMatthew G. Knepley   Input Parameters:
403420f4b53cSBarry Smith + dm          - The submesh `DM`
403520f4b53cSBarry Smith - subpointMap - The `DMLabel` of all the points from the original mesh in this submesh
4036c08575a3SMatthew G. Knepley 
4037c08575a3SMatthew G. Knepley   Level: developer
4038c08575a3SMatthew G. Knepley 
403920f4b53cSBarry Smith   Note:
404020f4b53cSBarry Smith   Should normally not be called by the user, since it is set in `DMPlexCreateSubmesh()`
404120f4b53cSBarry Smith 
40421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
4043c08575a3SMatthew G. Knepley @*/
4044d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
4045d71ae5a4SJacob Faibussowitsch {
4046e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
4047285d324eSMatthew G. Knepley   DMLabel  tmp;
4048e6ccafaeSMatthew G Knepley 
4049e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4050e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4051285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
4052e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
40539566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
40549566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
40553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4056e6ccafaeSMatthew G Knepley }
4057e6ccafaeSMatthew G Knepley 
4058d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
4059d71ae5a4SJacob Faibussowitsch {
406097d8846cSMatthew Knepley   DMLabel  spmap;
406197d8846cSMatthew Knepley   PetscInt depth, d;
406297d8846cSMatthew Knepley 
406397d8846cSMatthew Knepley   PetscFunctionBegin;
40649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
40659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
406697d8846cSMatthew Knepley   if (spmap && depth >= 0) {
406797d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
406897d8846cSMatthew Knepley     PetscInt *points, *depths;
406997d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
407097d8846cSMatthew Knepley 
40719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
407263a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
40739566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
40749566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
407597d8846cSMatthew Knepley     depths[0] = depth;
407697d8846cSMatthew Knepley     depths[1] = 0;
4077ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
407897d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
407997d8846cSMatthew Knepley       const PetscInt dep = depths[d];
408097d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
408197d8846cSMatthew Knepley 
40829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
40839566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
408497d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
408563a3b9bcSJacob 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);
408697d8846cSMatthew Knepley       } else {
408797d8846cSMatthew Knepley         if (!n) {
408897d8846cSMatthew Knepley           if (d == 0) {
408997d8846cSMatthew Knepley             /* Missing cells */
409097d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
409197d8846cSMatthew Knepley           } else {
409297d8846cSMatthew Knepley             /* Missing faces */
409397d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
409497d8846cSMatthew Knepley           }
409597d8846cSMatthew Knepley         }
409697d8846cSMatthew Knepley       }
409797d8846cSMatthew Knepley       if (n) {
409897d8846cSMatthew Knepley         IS              is;
409997d8846cSMatthew Knepley         const PetscInt *opoints;
410097d8846cSMatthew Knepley 
41019566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
41029566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
410397d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
41049566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
41059566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
410697d8846cSMatthew Knepley       }
410797d8846cSMatthew Knepley     }
41089566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
410963a3b9bcSJacob 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);
41109566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
41119566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
411297d8846cSMatthew Knepley   }
41133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
411497d8846cSMatthew Knepley }
411597d8846cSMatthew Knepley 
411664beef6dSMatthew G. Knepley /*@
411720f4b53cSBarry Smith   DMPlexGetSubpointIS - Returns an `IS` covering the entire subdm chart with the original points as data
4118e6ccafaeSMatthew G Knepley 
4119e6ccafaeSMatthew G Knepley   Input Parameter:
412020f4b53cSBarry Smith . dm - The submesh `DM`
4121e6ccafaeSMatthew G Knepley 
4122e6ccafaeSMatthew G Knepley   Output Parameter:
412320f4b53cSBarry Smith . subpointIS - The `IS` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh
412464beef6dSMatthew G. Knepley 
412564beef6dSMatthew G. Knepley   Level: developer
412664beef6dSMatthew G. Knepley 
412720f4b53cSBarry Smith   Note:
412820f4b53cSBarry Smith   This `IS` is guaranteed to be sorted by the construction of the submesh
412920f4b53cSBarry Smith 
41301cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
413164beef6dSMatthew G. Knepley @*/
4132d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
4133d71ae5a4SJacob Faibussowitsch {
413497d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
413597d8846cSMatthew Knepley   DMLabel          spmap;
413697d8846cSMatthew Knepley   PetscObjectState state;
4137e6ccafaeSMatthew G Knepley 
4138e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4139e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41404f572ea9SToby Isaac   PetscAssertPointer(subpointIS, 2);
41419566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
41429566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
41439566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
414497d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
41453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4146e6ccafaeSMatthew G Knepley }
4147559a1558SMatthew G. Knepley 
4148559a1558SMatthew G. Knepley /*@
414920f4b53cSBarry Smith   DMGetEnclosureRelation - Get the relationship between `dmA` and `dmB`
4150559a1558SMatthew G. Knepley 
4151559a1558SMatthew G. Knepley   Input Parameters:
415220f4b53cSBarry Smith + dmA - The first `DM`
415320f4b53cSBarry Smith - dmB - The second `DM`
4154559a1558SMatthew G. Knepley 
4155559a1558SMatthew G. Knepley   Output Parameter:
415620f4b53cSBarry Smith . rel - The relation of `dmA` to `dmB`
4157559a1558SMatthew G. Knepley 
4158a6e0b375SMatthew G. Knepley   Level: intermediate
4159559a1558SMatthew G. Knepley 
41601cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosurePoint()`
4161559a1558SMatthew G. Knepley @*/
4162d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4163d71ae5a4SJacob Faibussowitsch {
4164a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
4165559a1558SMatthew G. Knepley   DMLabel  spmap;
4166a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4167559a1558SMatthew G. Knepley 
416844171101SMatthew G. Knepley   PetscFunctionBegin;
41694f572ea9SToby Isaac   PetscAssertPointer(rel, 3);
4170a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
41713ba16761SJacob Faibussowitsch   if (!dmA || !dmB) PetscFunctionReturn(PETSC_SUCCESS);
4172a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4173064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
41749371c9d4SSatish Balay   if (dmA == dmB) {
41759371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
41763ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
41779371c9d4SSatish Balay   }
41789566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
41799566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
41809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
41819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4182a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4183a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4184a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
4185a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4186a6e0b375SMatthew G. Knepley     goto end;
4187559a1558SMatthew G. Knepley   }
4188a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4189a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4190a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4191a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
41929566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4193a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4194a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4195a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4196a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4197a6e0b375SMatthew G. Knepley   } else {
4198a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4199a6e0b375SMatthew G. Knepley   }
4200a6e0b375SMatthew G. Knepley end:
42019566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
42029566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
42033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4204559a1558SMatthew G. Knepley }
420544171101SMatthew G. Knepley 
420644171101SMatthew G. Knepley /*@
420720f4b53cSBarry Smith   DMGetEnclosurePoint - Get the point `pA` in `dmA` which corresponds to the point `pB` in `dmB`
420844171101SMatthew G. Knepley 
420944171101SMatthew G. Knepley   Input Parameters:
421020f4b53cSBarry Smith + dmA   - The first `DM`
421120f4b53cSBarry Smith . dmB   - The second `DM`
421220f4b53cSBarry Smith . etype - The type of enclosure relation that `dmA` has to `dmB`
421320f4b53cSBarry Smith - pB    - A point of `dmB`
421444171101SMatthew G. Knepley 
421544171101SMatthew G. Knepley   Output Parameter:
421620f4b53cSBarry Smith . pA - The corresponding point of `dmA`
421744171101SMatthew G. Knepley 
4218a6e0b375SMatthew G. Knepley   Level: intermediate
421944171101SMatthew G. Knepley 
42201cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosureRelation()`
422144171101SMatthew G. Knepley @*/
4222d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
4223d71ae5a4SJacob Faibussowitsch {
4224a6e0b375SMatthew G. Knepley   DM              sdm;
4225a6e0b375SMatthew G. Knepley   IS              subpointIS;
4226a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4227a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
422844171101SMatthew G. Knepley 
422944171101SMatthew G. Knepley   PetscFunctionBegin;
4230a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4231a6e0b375SMatthew G. Knepley   switch (etype) {
4232a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4233a6e0b375SMatthew G. Knepley     sdm = dmB;
42349566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
42359566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4236a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
42379566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4238a6e0b375SMatthew G. Knepley     break;
4239a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4240a6e0b375SMatthew G. Knepley     sdm = dmA;
42419566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
42429566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
42439566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
42449566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4245a6e0b375SMatthew G. Knepley     if (*pA < 0) {
42469566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
42479566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
424863a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4249a6e0b375SMatthew G. Knepley     }
42509566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4251a6e0b375SMatthew G. Knepley     break;
4252a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
4253d71ae5a4SJacob Faibussowitsch   case DM_ENC_NONE:
4254d71ae5a4SJacob Faibussowitsch     *pA = pB;
4255d71ae5a4SJacob Faibussowitsch     break;
42569371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4257a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
425844171101SMatthew G. Knepley 
42599566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
42609566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
42619371c9d4SSatish Balay   } break;
4262d71ae5a4SJacob Faibussowitsch   default:
4263d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
426444171101SMatthew G. Knepley   }
42653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
426644171101SMatthew G. Knepley }
4267