xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 429fa399fc3cd6fd42f3ca9697415d505b9dce5d)
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   }
22412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
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:
45d71ae5a4SJacob Faibussowitsch     PetscFunctionReturn(0);
46412e9a14SMatthew G. Knepley   }
47412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
48412e9a14SMatthew G. Knepley }
49412e9a14SMatthew G. Knepley 
50d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label)
51d71ae5a4SJacob Faibussowitsch {
52*429fa399SMatthew G. Knepley   PetscSF         sf;
53*429fa399SMatthew G. Knepley   const PetscInt *rootdegree, *leaves;
54*429fa399SMatthew G. Knepley   PetscInt        overlap, Nr = -1, Nl, pStart, fStart, fEnd;
5530560a7bSMatthew G. Knepley 
5630560a7bSMatthew G. Knepley   PetscFunctionBegin;
57*429fa399SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &sf));
58*429fa399SMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
59*429fa399SMatthew G. Knepley   if (sf && !overlap) PetscCall(PetscSFGetGraph(sf, &Nr, &Nl, &leaves, NULL));
60*429fa399SMatthew G. Knepley   if (Nr > 0) {
61*429fa399SMatthew G. Knepley     PetscCall(PetscSFComputeDegreeBegin(sf, &rootdegree));
62*429fa399SMatthew G. Knepley     PetscCall(PetscSFComputeDegreeEnd(sf, &rootdegree));
63*429fa399SMatthew G. Knepley   } else rootdegree = NULL;
64*429fa399SMatthew G. Knepley   PetscCall(DMPlexGetChart(dm, &pStart, NULL));
659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight + 1, &fStart, &fEnd));
66*429fa399SMatthew G. Knepley   for (PetscInt f = fStart; f < fEnd; ++f) {
67*429fa399SMatthew G. Knepley     PetscInt supportSize, loc = -1;
6830560a7bSMatthew G. Knepley 
699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
70e752be1aSMatthew G. Knepley     if (supportSize == 1) {
71*429fa399SMatthew G. Knepley       /* Do not mark faces which are shared, meaning
72*429fa399SMatthew G. Knepley            they are  present in the pointSF, or
73*429fa399SMatthew G. Knepley            they have rootdegree > 0
74*429fa399SMatthew G. Knepley          since they presumably have cells on the other side */
75*429fa399SMatthew G. Knepley       if (Nr > 0) {
76*429fa399SMatthew G. Knepley         PetscCall(PetscFindInt(f, Nl, leaves, &loc));
77*429fa399SMatthew G. Knepley         if (rootdegree[f - pStart] || loc >= 0) continue;
78*429fa399SMatthew 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   }
9730560a7bSMatthew G. Knepley   PetscFunctionReturn(0);
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:
106*429fa399SMatthew G. Knepley + dm - The original `DM`
107e752be1aSMatthew G. Knepley - 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:
110e752be1aSMatthew G. Knepley . label - The DMLabel marking boundary faces with the given value
111cd0c2139SMatthew G Knepley 
112*429fa399SMatthew G. Knepley   Note:
113*429fa399SMatthew G. Knepley   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.
114*429fa399SMatthew G. Knepley 
115cd0c2139SMatthew G Knepley   Level: developer
116cd0c2139SMatthew G Knepley 
117db781477SPatrick Sanan .seealso: `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));
128cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
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   }
207b0bf5782SToby Isaac   PetscFunctionReturn(0);
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:
2142be2b188SMatthew G Knepley + dm - The DM
2152be2b188SMatthew G Knepley - label - A DMLabel marking the surface points
2162be2b188SMatthew G Knepley 
2172be2b188SMatthew G Knepley   Output Parameter:
2182be2b188SMatthew G Knepley . label - A DMLabel marking all surface points in the transitive closure
2192be2b188SMatthew G Knepley 
2202be2b188SMatthew G Knepley   Level: developer
2212be2b188SMatthew G Knepley 
222db781477SPatrick Sanan .seealso: `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));
2282be2b188SMatthew G Knepley   PetscFunctionReturn(0);
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:
2356cf0e42fSMatthew G. Knepley + dm - The DM
2366cf0e42fSMatthew G. Knepley - label - A DMLabel marking the surface points
2376cf0e42fSMatthew G. Knepley 
2386cf0e42fSMatthew G. Knepley   Output Parameter:
2396cf0e42fSMatthew G. Knepley . label - A DMLabel incorporating cells
2406cf0e42fSMatthew G. Knepley 
2416cf0e42fSMatthew G. Knepley   Level: developer
2426cf0e42fSMatthew G. Knepley 
2436cf0e42fSMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
2446cf0e42fSMatthew G. Knepley 
245db781477SPatrick Sanan .seealso: `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
2466cf0e42fSMatthew G. Knepley @*/
247d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
248d71ae5a4SJacob Faibussowitsch {
2496cf0e42fSMatthew G. Knepley   IS              valueIS;
2506cf0e42fSMatthew G. Knepley   const PetscInt *values;
251485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
2526cf0e42fSMatthew G. Knepley 
2536cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
2549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
2559566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2569566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2579566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
2586cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2596cf0e42fSMatthew G. Knepley     IS              pointIS;
2606cf0e42fSMatthew G. Knepley     const PetscInt *points;
2616cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2626cf0e42fSMatthew G. Knepley 
2639566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2649566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2659566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
2666cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2676cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
268a6e0b375SMatthew G. Knepley       PetscInt  closureSize, cl;
2696cf0e42fSMatthew G. Knepley 
2709566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
27122eabd52SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
272a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
2739371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
2749371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
2759371c9d4SSatish Balay           break;
2769371c9d4SSatish Balay         }
27722eabd52SMatthew G. Knepley       }
2789566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
2796cf0e42fSMatthew G. Knepley     }
2809566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2819566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
2826cf0e42fSMatthew G. Knepley   }
2839566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2849566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2856cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2866cf0e42fSMatthew G. Knepley }
2876cf0e42fSMatthew G. Knepley 
288f402d5e4SToby Isaac /*@
289a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
290a6e0b375SMatthew G. Knepley 
291a6e0b375SMatthew G. Knepley   Input Parameters:
292a6e0b375SMatthew G. Knepley + dm - The DM
293a6e0b375SMatthew G. Knepley - label - A DMLabel marking the surface points
294a6e0b375SMatthew G. Knepley 
295a6e0b375SMatthew G. Knepley   Output Parameter:
296a6e0b375SMatthew G. Knepley . label - A DMLabel incorporating cells
297a6e0b375SMatthew G. Knepley 
298a6e0b375SMatthew G. Knepley   Level: developer
299a6e0b375SMatthew G. Knepley 
300a6e0b375SMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
301a6e0b375SMatthew G. Knepley 
302db781477SPatrick Sanan .seealso: `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
303a6e0b375SMatthew G. Knepley @*/
304d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
305d71ae5a4SJacob Faibussowitsch {
306a6e0b375SMatthew G. Knepley   IS              valueIS;
307a6e0b375SMatthew G. Knepley   const PetscInt *values;
308a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
309a6e0b375SMatthew G. Knepley 
310a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
3119566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
3129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
3139566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3149566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3159566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
316a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
317a6e0b375SMatthew G. Knepley     IS              pointIS;
318a6e0b375SMatthew G. Knepley     const PetscInt *points;
319a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
320a6e0b375SMatthew G. Knepley 
3219566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3229566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3239566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
324a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
325a6e0b375SMatthew G. Knepley       const PetscInt face    = points[p];
326a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
327a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
328a6e0b375SMatthew G. Knepley 
329a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
3309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
331a6e0b375SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
332a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
3339371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
3349371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
3359371c9d4SSatish Balay           break;
3369371c9d4SSatish Balay         }
337a6e0b375SMatthew G. Knepley       }
3389566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
339a6e0b375SMatthew G. Knepley     }
3409566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3419566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
342a6e0b375SMatthew G. Knepley   }
3439566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3449566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
345a6e0b375SMatthew G. Knepley   PetscFunctionReturn(0);
346a6e0b375SMatthew G. Knepley }
347a6e0b375SMatthew G. Knepley 
348a6e0b375SMatthew G. Knepley /*@
349f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
350f402d5e4SToby Isaac 
351f402d5e4SToby Isaac   Input Parameters:
352f402d5e4SToby Isaac + dm - The DM
353f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
354f402d5e4SToby Isaac 
355f402d5e4SToby Isaac   Output Parameter:
356f402d5e4SToby Isaac . label - A DMLabel without cells
357f402d5e4SToby Isaac 
358f402d5e4SToby Isaac   Level: developer
359f402d5e4SToby Isaac 
360a6e0b375SMatthew G. Knepley   Note: This undoes DMPlexLabelAddCells() or DMPlexLabelAddFaceCells()
361f402d5e4SToby Isaac 
362db781477SPatrick Sanan .seealso: `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()`
363f402d5e4SToby Isaac @*/
364d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
365d71ae5a4SJacob Faibussowitsch {
366f402d5e4SToby Isaac   IS              valueIS;
367f402d5e4SToby Isaac   const PetscInt *values;
368485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
369f402d5e4SToby Isaac 
370f402d5e4SToby Isaac   PetscFunctionBegin;
3719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
3729566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3739566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3749566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
375f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
376f402d5e4SToby Isaac     IS              pointIS;
377f402d5e4SToby Isaac     const PetscInt *points;
378f402d5e4SToby Isaac     PetscInt        numPoints, p;
379f402d5e4SToby Isaac 
3809566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3819566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3829566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
383f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
384f402d5e4SToby Isaac       PetscInt point = points[p];
385f402d5e4SToby Isaac 
38648a46eb9SPierre Jolivet       if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v]));
387f402d5e4SToby Isaac     }
3889566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3899566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
390f402d5e4SToby Isaac   }
3919566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3929566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
393f402d5e4SToby Isaac   PetscFunctionReturn(0);
394f402d5e4SToby Isaac }
395f402d5e4SToby Isaac 
39659eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
39759eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
398d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
399d71ae5a4SJacob Faibussowitsch {
4002582d50cSToby Isaac   PetscInt d, off = 0;
4012582d50cSToby Isaac 
4022582d50cSToby Isaac   PetscFunctionBegin;
40359eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
4040974a383SToby Isaac   for (d = 0; d < depth; d++) {
4052582d50cSToby Isaac     PetscInt firstd     = d;
4060974a383SToby Isaac     PetscInt firstStart = depthShift[2 * d];
4072582d50cSToby Isaac     PetscInt e;
4082582d50cSToby Isaac 
4092582d50cSToby Isaac     for (e = d + 1; e <= depth; e++) {
4102582d50cSToby Isaac       if (depthShift[2 * e] < firstStart) {
4112582d50cSToby Isaac         firstd     = e;
4122582d50cSToby Isaac         firstStart = depthShift[2 * d];
4132582d50cSToby Isaac       }
4142582d50cSToby Isaac     }
4152582d50cSToby Isaac     if (firstd != d) {
4162582d50cSToby Isaac       PetscInt swap[2];
4172582d50cSToby Isaac 
4182582d50cSToby Isaac       e                     = firstd;
4192582d50cSToby Isaac       swap[0]               = depthShift[2 * d];
4202582d50cSToby Isaac       swap[1]               = depthShift[2 * d + 1];
4212582d50cSToby Isaac       depthShift[2 * d]     = depthShift[2 * e];
4222582d50cSToby Isaac       depthShift[2 * d + 1] = depthShift[2 * e + 1];
4232582d50cSToby Isaac       depthShift[2 * e]     = swap[0];
4242582d50cSToby Isaac       depthShift[2 * e + 1] = swap[1];
4252582d50cSToby Isaac     }
4262582d50cSToby Isaac   }
4272582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
4280974a383SToby Isaac   for (d = 0; d <= depth; d++) {
4292582d50cSToby Isaac     off += depthShift[2 * d + 1];
43059eef20bSToby Isaac     depthShift[2 * d + 1] = depthShift[2 * d] + off;
4312582d50cSToby Isaac   }
4322582d50cSToby Isaac   PetscFunctionReturn(0);
4332582d50cSToby Isaac }
4342582d50cSToby Isaac 
4352582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
436d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
437d71ae5a4SJacob Faibussowitsch {
4382582d50cSToby Isaac   PetscInt d;
4392582d50cSToby Isaac   PetscInt newOff = 0;
4402582d50cSToby Isaac 
4412582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4422582d50cSToby Isaac     if (p < depthShift[2 * d]) return p + newOff;
4432582d50cSToby Isaac     else newOff = depthShift[2 * d + 1] - depthShift[2 * d];
4442582d50cSToby Isaac   }
4450974a383SToby Isaac   return p + newOff;
4462582d50cSToby Isaac }
4472582d50cSToby Isaac 
4482582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
449d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
450d71ae5a4SJacob Faibussowitsch {
4512582d50cSToby Isaac   PetscInt d;
4522582d50cSToby Isaac   PetscInt newOff = 0;
4532582d50cSToby Isaac 
4542582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4552582d50cSToby Isaac     if (p < depthShift[2 * d + 1]) return p + newOff;
4562582d50cSToby Isaac     else newOff = depthShift[2 * d] - depthShift[2 * d + 1];
4572582d50cSToby Isaac   }
4580974a383SToby Isaac   return p + newOff;
459cd0c2139SMatthew G Knepley }
460cd0c2139SMatthew G Knepley 
461d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
462d71ae5a4SJacob Faibussowitsch {
463cd0c2139SMatthew G Knepley   PetscInt depth = 0, d, pStart, pEnd, p;
464fa8e8ae5SToby Isaac   DMLabel  depthLabel;
465cd0c2139SMatthew G Knepley 
466cd0c2139SMatthew G Knepley   PetscFunctionBegin;
4679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
468cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
469cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
4709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4710974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
4729566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
4739566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "depth"));
4749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel));
4759566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
476cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
477cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
478fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
479fa8e8ae5SToby Isaac     IS       pIS;
480fa8e8ae5SToby Isaac 
4819566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
482fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
483fa8e8ae5SToby Isaac     pEndNew   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
4849566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
4859566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
4869566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
487cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
4882582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
489cd0c2139SMatthew G Knepley       PetscInt       size;
490412e9a14SMatthew G. Knepley       DMPolytopeType ct;
491cd0c2139SMatthew G Knepley 
4929566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
4939566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
4949566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
4959566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
4969566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
4979566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
498cd0c2139SMatthew G Knepley     }
499cd0c2139SMatthew G Knepley   }
500cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
501cd0c2139SMatthew G Knepley }
502cd0c2139SMatthew G Knepley 
503d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
504d71ae5a4SJacob Faibussowitsch {
5052582d50cSToby Isaac   PetscInt *newpoints;
5062582d50cSToby Isaac   PetscInt  depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
507cd0c2139SMatthew G Knepley 
508cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5099566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
510cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
5119566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
5129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
5139566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints));
514cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
5159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
516cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
517cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
5182582d50cSToby Isaac     PetscInt        size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
519cd0c2139SMatthew G Knepley 
5209566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
5219566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
5229566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
523ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
5249566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
5259566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
5269566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
5279566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
5289566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
529ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
530dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
5319566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
532cd0c2139SMatthew G Knepley   }
5339566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
534cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
535cd0c2139SMatthew G Knepley }
536cd0c2139SMatthew G Knepley 
537d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
538d71ae5a4SJacob Faibussowitsch {
539cd0c2139SMatthew G Knepley   PetscSection coordSection, newCoordSection;
540cd0c2139SMatthew G Knepley   Vec          coordinates, newCoordinates;
541cd0c2139SMatthew G Knepley   PetscScalar *coords, *newCoords;
542f2b8cce1SMatthew G. Knepley   PetscInt     coordSize, sStart, sEnd;
543f2b8cce1SMatthew G. Knepley   PetscInt     dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
544f2b8cce1SMatthew G. Knepley   PetscBool    hasCells;
545cd0c2139SMatthew G Knepley 
546cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5479566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
5489566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
5499566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
550cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
5519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
5529566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
5549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
5559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
5569566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
5579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
5589566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
5599566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
560f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
5619566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
562f2b8cce1SMatthew G. Knepley   if (hasCells) {
563f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
564f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
565f2b8cce1SMatthew G. Knepley 
5669566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
5679566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
5689566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
569f2b8cce1SMatthew G. Knepley     }
570f2b8cce1SMatthew G. Knepley   }
571cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
5729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
5739566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
574cd0c2139SMatthew G Knepley   }
5759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
5769566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
5779566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
5789566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
5799566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates"));
5809566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
5819566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
5829566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates, VECSTANDARD));
5839566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
5849566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
5859566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5869566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
587f2b8cce1SMatthew G. Knepley   if (hasCells) {
588f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
589f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
590f2b8cce1SMatthew G. Knepley 
5919566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
5929566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
5939566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
594f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
595f2b8cce1SMatthew G. Knepley     }
596f2b8cce1SMatthew G. Knepley   }
597cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
598cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
599cd0c2139SMatthew G Knepley 
6009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
6019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
6029566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
603f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
604cd0c2139SMatthew G Knepley   }
6059566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6069566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
6079566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
6089566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
609cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
610cd0c2139SMatthew G Knepley }
611cd0c2139SMatthew G Knepley 
612d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
613d71ae5a4SJacob Faibussowitsch {
614cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
615cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
616cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
617cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
6180e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
619cd0c2139SMatthew G Knepley 
620cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
6229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
6239566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
6246e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
625cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
6269566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation));
6270e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
6289566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
6299566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
6309566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
6319566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
632cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
6332582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
634cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
635cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
636cd0c2139SMatthew G Knepley     }
6379566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
6389566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
6390e33faafSMatthew G. Knepley   }
6400e33faafSMatthew G. Knepley   PetscFunctionReturn(0);
6410e33faafSMatthew G. Knepley }
6420e33faafSMatthew G. Knepley 
643d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
644d71ae5a4SJacob Faibussowitsch {
6450e33faafSMatthew G. Knepley   PetscSF   sfPoint, sfPointNew;
6460e33faafSMatthew G. Knepley   PetscBool useNatural;
6470e33faafSMatthew G. Knepley 
6480e33faafSMatthew G. Knepley   PetscFunctionBegin;
6490e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
6509566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
6519566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
6529566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
6530e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
6549566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
6550e33faafSMatthew G. Knepley   if (useNatural) {
6560e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
6570e33faafSMatthew G. Knepley 
6589566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
6599566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
6609566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
6619566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
662cd0c2139SMatthew G Knepley   }
663cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
664cd0c2139SMatthew G Knepley }
665cd0c2139SMatthew G Knepley 
666d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
667d71ae5a4SJacob Faibussowitsch {
668d56405f8SMatthew G. Knepley   PetscInt depth = 0, numLabels, l;
669cd0c2139SMatthew G Knepley 
670cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
672cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
6739566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
674cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
675cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
676cd0c2139SMatthew G Knepley     const char     *lname;
677fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
678cd0c2139SMatthew G Knepley     IS              valueIS;
679cd0c2139SMatthew G Knepley     const PetscInt *values;
680cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
681cd0c2139SMatthew G Knepley 
6829566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
6839566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
684cd0c2139SMatthew G Knepley     if (isDepth) continue;
6859566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
686fa8e8ae5SToby Isaac     if (isDim) continue;
6879566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
6889566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
6899566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
6909566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &val));
6919566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, val));
6929566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
6939566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
6949566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
695cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
696cd0c2139SMatthew G Knepley       IS              pointIS;
697cd0c2139SMatthew G Knepley       const PetscInt *points;
698cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
699cd0c2139SMatthew G Knepley 
7009566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
7019566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
7029566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
703cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
7042582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
705cd0c2139SMatthew G Knepley 
7069566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
707cd0c2139SMatthew G Knepley       }
7089566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
7099566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
710cd0c2139SMatthew G Knepley     }
7119566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
7129566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
713cd0c2139SMatthew G Knepley   }
714d56405f8SMatthew G. Knepley   PetscFunctionReturn(0);
715d56405f8SMatthew G. Knepley }
716d56405f8SMatthew G. Knepley 
717d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
718d71ae5a4SJacob Faibussowitsch {
719d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
720d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
721d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
722d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
723d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
724d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
725d56405f8SMatthew G. Knepley 
726d56405f8SMatthew G. Knepley   PetscFunctionBegin;
727cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
7289566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
7299566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
7309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
7319566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
7329566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
7339566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
7349566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
735d56405f8SMatthew G. Knepley   if (createGhostLabel) {
7369566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
7379566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
738d56405f8SMatthew G. Knepley   }
739cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
74048a46eb9SPierre Jolivet     for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
741cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
742cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
7439566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
7441baa6e33SBarry Smith     } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
745cd0c2139SMatthew G Knepley   }
74648a46eb9SPierre Jolivet   for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
747d56405f8SMatthew G. Knepley   if (ghostLabel) {
7489566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
749cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
750cd0c2139SMatthew G Knepley       PetscInt numCells;
751cd0c2139SMatthew G Knepley 
7529566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
753cd0c2139SMatthew G Knepley       if (numCells < 2) {
7549566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
755cd0c2139SMatthew G Knepley       } else {
756cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
757cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
758cd0c2139SMatthew G Knepley 
7599566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
7609566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
7619566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
7629566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
763cd0c2139SMatthew G Knepley       }
764cd0c2139SMatthew G Knepley     }
765d56405f8SMatthew G. Knepley   }
766cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
767cd0c2139SMatthew G Knepley }
768cd0c2139SMatthew G Knepley 
769d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
770d71ae5a4SJacob Faibussowitsch {
771ca04dac2SToby Isaac   DM           refTree;
772ca04dac2SToby Isaac   PetscSection pSec;
773ca04dac2SToby Isaac   PetscInt    *parents, *childIDs;
774ca04dac2SToby Isaac 
775ca04dac2SToby Isaac   PetscFunctionBegin;
7769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
7779566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew, refTree));
7789566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL));
779ca04dac2SToby Isaac   if (pSec) {
7802582d50cSToby Isaac     PetscInt     p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
781fb4630b5SToby Isaac     PetscInt    *childIDsShifted;
782ca04dac2SToby Isaac     PetscSection pSecShifted;
783ca04dac2SToby Isaac 
7849566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd));
7859566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
7862582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
7872582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
7889566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted));
7899566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted));
7909566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted));
791ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
792fb4630b5SToby Isaac       /* start off assuming no children */
7939566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, p, 0));
794fb4630b5SToby Isaac     }
795fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
796fb4630b5SToby Isaac       PetscInt dof;
797fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
798ca04dac2SToby Isaac 
7999566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
8009566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof));
801ca04dac2SToby Isaac     }
8029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
803fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
804fb4630b5SToby Isaac       PetscInt dof;
805fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
806fb4630b5SToby Isaac 
8079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
808fb4630b5SToby Isaac       if (dof) {
809fb4630b5SToby Isaac         PetscInt off, offNew;
810fb4630b5SToby Isaac 
8119566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec, p, &off));
8129566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew));
813fb4630b5SToby Isaac         parentsShifted[offNew]  = DMPlexShiftPoint_Internal(parents[off], depth, depthShift);
814fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
815fb4630b5SToby Isaac       }
816fb4630b5SToby Isaac     }
8179566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted));
8189566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted, childIDsShifted));
8199566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
820ca04dac2SToby Isaac   }
821ca04dac2SToby Isaac   PetscFunctionReturn(0);
822ca04dac2SToby Isaac }
823ca04dac2SToby Isaac 
824d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
825d71ae5a4SJacob Faibussowitsch {
826da97024aSMatthew G. Knepley   PetscSF          sf;
827cd0c2139SMatthew G Knepley   IS               valueIS;
828da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
829cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
8302582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
8314fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
832cd0c2139SMatthew G Knepley 
833cd0c2139SMatthew G Knepley   PetscFunctionBegin;
8349566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
8359566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
836da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
8379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
838cd0c2139SMatthew G Knepley   /* Count ghost cells */
8399566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
8409566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
8419566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
8424a6cfa73SMatthew G. Knepley   Ng = 0;
843cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
84446c796b9SMatthew G. Knepley     IS              faceIS;
84546c796b9SMatthew G. Knepley     const PetscInt *faces;
84646c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
847cd0c2139SMatthew G Knepley 
8489566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8499566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8509566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
85146c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
852ca04dac2SToby Isaac       PetscInt numChildren;
853ca04dac2SToby Isaac 
8549566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8559566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
856ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
857ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
85846c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
85946c796b9SMatthew G. Knepley     }
8604a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
8619566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8629566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
863cd0c2139SMatthew G Knepley   }
8649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
8659566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift));
8662582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
86759eef20bSToby Isaac     PetscInt dEnd;
8682582d50cSToby Isaac 
8699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd));
87059eef20bSToby Isaac     depthShift[2 * d]     = dEnd;
8712582d50cSToby Isaac     depthShift[2 * d + 1] = 0;
8722582d50cSToby Isaac   }
8732582d50cSToby Isaac   if (depth >= 0) depthShift[2 * depth + 1] = Ng;
8749566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
8759566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
876cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
8779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
87848a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1));
879cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
880cd0c2139SMatthew G Knepley     IS              faceIS;
881cd0c2139SMatthew G Knepley     const PetscInt *faces;
882cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
883cd0c2139SMatthew G Knepley 
8849566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8859566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8869566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
887cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
888ca04dac2SToby Isaac       PetscInt size, numChildren;
889cd0c2139SMatthew G Knepley 
8909566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
892ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
89346c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
8949566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
89563a3b9bcSJacob 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);
8969566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
897cd0c2139SMatthew G Knepley     }
8989566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8999566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
900cd0c2139SMatthew G Knepley   }
901cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
9029566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
9039566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
904cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
905cd0c2139SMatthew G Knepley   ghostCell = cEnd;
906cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
907cd0c2139SMatthew G Knepley     IS              faceIS;
908cd0c2139SMatthew G Knepley     const PetscInt *faces;
909cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
910cd0c2139SMatthew G Knepley 
9119566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
9129566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
9139566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
91446c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
915ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
916cd0c2139SMatthew G Knepley 
9179566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9189566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
919ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
92046c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9219566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
9229566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
92346c796b9SMatthew G. Knepley       ++ghostCell;
924cd0c2139SMatthew G Knepley     }
9259566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9269566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
927cd0c2139SMatthew G Knepley   }
9289566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
9299566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
9309566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
9319566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
9329566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
9339566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
9349566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
9359566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
93648a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
937966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
9384fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
9394fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L));
9404a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
941cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
942cd0c2139SMatthew G Knepley }
943cd0c2139SMatthew G Knepley 
944cd0c2139SMatthew G Knepley /*@C
945cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
946cd0c2139SMatthew G Knepley 
947cd0c2139SMatthew G Knepley   Collective on dm
948cd0c2139SMatthew G Knepley 
949cd0c2139SMatthew G Knepley   Input Parameters:
950cd0c2139SMatthew G Knepley + dm - The original DM
951cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
952cd0c2139SMatthew G Knepley 
953cd0c2139SMatthew G Knepley   Output Parameters:
954cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
955cd0c2139SMatthew G Knepley - dmGhosted - The new DM
956cd0c2139SMatthew G Knepley 
957cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
958cd0c2139SMatthew G Knepley 
959cd0c2139SMatthew G Knepley   Level: developer
960cd0c2139SMatthew G Knepley 
961db781477SPatrick Sanan .seealso: `DMCreate()`
96231266bc0SMatthew G. Knepley @*/
963d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
964d71ae5a4SJacob Faibussowitsch {
965cd0c2139SMatthew G Knepley   DM          gdm;
966cd0c2139SMatthew G Knepley   DMLabel     label;
967cd0c2139SMatthew G Knepley   const char *name = labelName ? labelName : "Face Sets";
968412e9a14SMatthew G. Knepley   PetscInt    dim, Ng = 0;
969b0441da4SMatthew G. Knepley   PetscBool   useCone, useClosure;
970cd0c2139SMatthew G Knepley 
971cd0c2139SMatthew G Knepley   PetscFunctionBegin;
972cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
973dadcf809SJacob Faibussowitsch   if (numGhostCells) PetscValidIntPointer(numGhostCells, 3);
974cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
9759566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
9769566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
9779566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
9789566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
9799566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
9809566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure));
9819566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
982cd0c2139SMatthew G Knepley   if (!label) {
983cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
9849566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
9859566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
9869566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
987cd0c2139SMatthew G Knepley   }
9889566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
9899566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
9905de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
991cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
992d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
993cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
994cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
995cd0c2139SMatthew G Knepley }
996cd0c2139SMatthew G Knepley 
997d71ae5a4SJacob Faibussowitsch static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue)
998d71ae5a4SJacob Faibussowitsch {
999accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
1000accc9626SMatthew G. Knepley 
1001accc9626SMatthew G. Knepley   PetscFunctionBegin;
1002accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1003accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
100448a46eb9SPierre Jolivet   for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
1005accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
1006accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
1007accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
1008accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
1009accc9626SMatthew G. Knepley 
1010accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
1011accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
1012accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1013accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
1014accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1015accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1016accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
1017accc9626SMatthew G. Knepley         PetscInt       clval;
1018accc9626SMatthew G. Knepley 
1019accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
1020accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
1021accc9626SMatthew G. Knepley         if (clval == -1) {
1022accc9626SMatthew G. Knepley           const PetscInt *cone;
1023accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
1024accc9626SMatthew G. Knepley 
1025accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
1026accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
1027accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
1028accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
1029accc9626SMatthew G. Knepley             PetscInt cpval;
1030accc9626SMatthew G. Knepley 
1031accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
1032accc9626SMatthew G. Knepley             if (cpval != -1) {
1033accc9626SMatthew G. Knepley               PetscInt dep;
1034accc9626SMatthew G. Knepley 
1035accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
1036accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift + dep) : shift + dep;
1037accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
1038accc9626SMatthew G. Knepley               break;
1039accc9626SMatthew G. Knepley             }
1040accc9626SMatthew G. Knepley           }
1041accc9626SMatthew G. Knepley         }
1042accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
1043accc9626SMatthew G. Knepley         if (d == dim - 1 && clval != -1) {
1044accc9626SMatthew G. Knepley           const PetscInt *support;
1045accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
1046accc9626SMatthew G. Knepley 
1047accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
1048accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
1049accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
1050accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
1051accc9626SMatthew G. Knepley             if (nval == -1) {
1052accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1));
1053accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1054accc9626SMatthew G. Knepley             }
1055accc9626SMatthew G. Knepley           }
1056accc9626SMatthew G. Knepley         }
1057accc9626SMatthew G. Knepley       }
1058accc9626SMatthew G. Knepley     }
1059accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1060accc9626SMatthew G. Knepley   }
1061accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
1062accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1063accc9626SMatthew G. Knepley }
1064accc9626SMatthew G. Knepley 
1065accc9626SMatthew G. Knepley typedef struct {
1066accc9626SMatthew G. Knepley   DM               dm;
1067accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1068accc9626SMatthew G. Knepley } PointDivision;
1069accc9626SMatthew G. Knepley 
1070d71ae5a4SJacob Faibussowitsch static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx)
1071d71ae5a4SJacob Faibussowitsch {
1072accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *)ctx;
1073accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val - 1 : val + 1;
1074accc9626SMatthew G. Knepley   const PetscInt *support;
1075accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1076accc9626SMatthew G. Knepley 
1077accc9626SMatthew G. Knepley   PetscFunctionBegin;
1078accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1079accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1080accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1081accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1082accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1083accc9626SMatthew G. Knepley   }
1084accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1085accc9626SMatthew G. Knepley }
1086accc9626SMatthew G. Knepley 
1087accc9626SMatthew G. Knepley /* Mark cells by label propagation */
1088d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel)
1089d71ae5a4SJacob Faibussowitsch {
1090accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1091accc9626SMatthew G. Knepley   PointDivision    div;
1092accc9626SMatthew G. Knepley   PetscSF          pointSF;
1093accc9626SMatthew G. Knepley   IS               pointIS;
1094accc9626SMatthew G. Knepley   const PetscInt  *points;
1095accc9626SMatthew G. Knepley   PetscBool        empty;
1096accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1097accc9626SMatthew G. Knepley 
1098accc9626SMatthew G. Knepley   PetscFunctionBegin;
1099accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1100accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1101accc9626SMatthew G. Knepley   div.dm    = dm;
1102accc9626SMatthew G. Knepley   div.queue = queue;
1103accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1104accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS));
1105accc9626SMatthew G. Knepley   if (pointIS) {
1106accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1107accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
110848a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1109accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1110accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1111accc9626SMatthew G. Knepley   }
1112accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS));
1113accc9626SMatthew G. Knepley   if (pointIS) {
1114accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1115accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
111648a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1117accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1118accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1119accc9626SMatthew G. Knepley   }
1120accc9626SMatthew G. Knepley 
1121accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1122accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1123accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1124accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1125accc9626SMatthew G. Knepley   while (!empty) {
1126accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1127accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1128accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1129accc9626SMatthew G. Knepley   }
1130accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1131accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
1132accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1133accc9626SMatthew G. Knepley }
1134accc9626SMatthew G. Knepley 
1135607ab7a9SMatthew G. Knepley /*
1136faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1137607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1138faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1139b6dfa339SMatthew 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
1140607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1141a6ae58d1SMatthew G. Knepley 
1142a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1143a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1144607ab7a9SMatthew G. Knepley */
1145d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1146d71ae5a4SJacob Faibussowitsch {
1147cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1148607ab7a9SMatthew G. Knepley   IS               valueIS;
1149607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0; /* The number of depths for which we have replicated points */
1150607ab7a9SMatthew G. Knepley   const PetscInt  *values;    /* List of depths for which we have replicated points */
115118c5995bSMatthew G. Knepley   IS              *splitIS;
115218c5995bSMatthew G. Knepley   IS              *unsplitIS;
1153b6dfa339SMatthew G. Knepley   IS               ghostIS;
1154607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
115518c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
115636dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
115736dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1158b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1159607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
116018c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1161b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1162cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1163cd0c2139SMatthew G Knepley   Vec              coordinates;
1164cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1165a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;   /* The first hybrid point at each depth in the original mesh */
1166a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;   /* The point limit at each depth in the original mesh */
1167607ab7a9SMatthew G. Knepley   PetscInt        *depthShift; /* Number of replicated+hybrid points at each depth */
1168607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;    /* The first replicated point at each depth in the new mesh, hybrids come after this */
1169607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
117018c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1171cd0c2139SMatthew G Knepley 
1172cd0c2139SMatthew G Knepley   PetscFunctionBegin;
11739566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
11749566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
11759566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
11769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1177412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
11789566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1179cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
11809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
11819566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld));
11829566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints));
1183607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
11849566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
11859566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1186a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1187a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1188607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
118918c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1190607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1191a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1192607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
119318c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
119418c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
119518c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
119659eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
119759eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
119859eef20bSToby Isaac     depthShift[2 * d]     = depthMax[d];
119959eef20bSToby Isaac     depthShift[2 * d + 1] = 0;
1200607ab7a9SMatthew G. Knepley   }
1201b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1202b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1203cd0c2139SMatthew G Knepley   if (label) {
12049566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
12059566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
12069566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1207cd0c2139SMatthew G Knepley   }
1208cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1209cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1210cd0c2139SMatthew G Knepley 
1211cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
12129566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
121318c5995bSMatthew G. Knepley     if (splitIS[dep]) {
12149566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
12159566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
121618c5995bSMatthew G. Knepley     }
12179566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep]));
121818c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
12199566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
12209566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1221cd0c2139SMatthew G Knepley     }
1222cd0c2139SMatthew G Knepley   }
1223b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS));
1224b6dfa339SMatthew G. Knepley   if (ghostIS) {
1225b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1226b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1227b6dfa339SMatthew G. Knepley   }
1228607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
122918c5995bSMatthew 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   */
12302582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d];
12319566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
123259eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
123359eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
123459eef20bSToby Isaac    * added points */
12352582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
12369566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1237cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1238cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1239cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1240cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
12412582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
12424c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1243cd0c2139SMatthew G Knepley       const PetscInt *support;
1244394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
12454c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1246cd0c2139SMatthew G Knepley 
12479566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
12489566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
12499566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
12509566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
12519566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
12529566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1253cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
12544c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
12554c367dbcSMatthew G. Knepley 
1256cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
12579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1258412e9a14SMatthew G. Knepley         switch (coneSize) {
1259d71ae5a4SJacob Faibussowitsch         case 2:
1260d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));
1261d71ae5a4SJacob Faibussowitsch           break;
1262d71ae5a4SJacob Faibussowitsch         case 3:
1263d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));
1264d71ae5a4SJacob Faibussowitsch           break;
1265d71ae5a4SJacob Faibussowitsch         case 4:
1266d71ae5a4SJacob Faibussowitsch           PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));
1267d71ae5a4SJacob Faibussowitsch           break;
1268412e9a14SMatthew G. Knepley         }
1269b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1270b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1271b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1272b6dfa339SMatthew G. Knepley           const PetscInt *support;
1273b6dfa339SMatthew G. Knepley           PetscInt        val;
1274b6dfa339SMatthew G. Knepley 
1275b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1276b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1277b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1278b6dfa339SMatthew G. Knepley           else PetscCall(DMPlexSetSupportSize(sdm, newp, 2));
1279b6dfa339SMatthew G. Knepley         }
1280cd0c2139SMatthew G Knepley       } else if (dep == 0) {
12814c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1282cd0c2139SMatthew G Knepley 
12839566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
12844c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1285cd0c2139SMatthew G Knepley           PetscInt val;
1286cd0c2139SMatthew G Knepley 
12879566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
12884c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
12894c367dbcSMatthew G. Knepley           if ((val == 1) || (val == (shift + 1))) ++qn;
12904c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1291cd0c2139SMatthew G Knepley         }
12924c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
12939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
12944c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
12959566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
12964c367dbcSMatthew G. Knepley         /* Add hybrid edge */
12979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
12989566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
12999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1300cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
13014c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
13024c367dbcSMatthew G. Knepley 
13039566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
13044c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1305cd0c2139SMatthew G Knepley           PetscInt val;
1306cd0c2139SMatthew G Knepley 
13079566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13084c367dbcSMatthew G. Knepley           if (val == dim - 1) ++qf;
13094c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn;
13104c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp;
1311cd0c2139SMatthew G Knepley         }
13124c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
13139566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
13144c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
13159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
13164c367dbcSMatthew G. Knepley         /* Add hybrid face */
13179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
13189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
13199566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1320cd0c2139SMatthew G Knepley       }
1321cd0c2139SMatthew G Knepley     }
1322cd0c2139SMatthew G Knepley   }
132318c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
132418c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
132518c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
13262582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
132718c5995bSMatthew G. Knepley       const PetscInt *support;
1328da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
132918c5995bSMatthew G. Knepley 
13309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13319566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13329566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
133318c5995bSMatthew G. Knepley       if (dep == 0) {
133418c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
133518c5995bSMatthew G. Knepley 
133639254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
133739254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
13389566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
133939254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
134039254ff6SMatthew G. Knepley         }
13419566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
134218c5995bSMatthew G. Knepley         /* Add hybrid edge */
13439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1344e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1345e1757548SMatthew G. Knepley           PetscInt val;
1346e1757548SMatthew G. Knepley 
13479566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1348e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1349da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1350da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1351e1757548SMatthew G. Knepley         }
13529566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
135418c5995bSMatthew G. Knepley       } else if (dep == dim - 2) {
135518c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1356cd0c2139SMatthew G Knepley         PetscInt       val;
1357cd0c2139SMatthew G Knepley 
1358da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13599566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1360da1dd7e4SMatthew G. Knepley           if (val == dim - 1) qf += 2;
1361da1dd7e4SMatthew G. Knepley           else ++qf;
1362cd0c2139SMatthew G Knepley         }
136318c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
13649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
136518c5995bSMatthew G. Knepley         /* Add hybrid face */
1366da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13679566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1368da1dd7e4SMatthew G. Knepley           if (val == dim - 1) ++qf;
1369da1dd7e4SMatthew G. Knepley         }
13709566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
13719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
13729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1373cd0c2139SMatthew G Knepley       }
1374cd0c2139SMatthew G Knepley     }
1375cd0c2139SMatthew G Knepley   }
1376cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
13779566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
13789566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
13799566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
13809566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew));
1381cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1382cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1383cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1384cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
13852582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
13864c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1387cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1388b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
13894c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1390cd0c2139SMatthew G Knepley 
13919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
13929566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13939566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
13949566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
13959566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13969566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1397cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
139896a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
13994c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1400cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1401cd0c2139SMatthew G Knepley 
1402b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1403cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
14049566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp, &supportF));
14059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1406cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1407cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1408cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
14099566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
141018c5995bSMatthew G. Knepley           if (v < 0) {
14119566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
141263a3b9bcSJacob 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);
14132582d50cSToby Isaac             coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
141496a07cd0SMatthew G. Knepley             hasUnsplit     = PETSC_TRUE;
141518c5995bSMatthew G. Knepley           } else {
14164c367dbcSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep - 1];
1417163235baSMatthew G. Knepley             if (dep > 1) {
1418163235baSMatthew G. Knepley               const PetscInt *econe;
1419163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1420163235baSMatthew G. Knepley 
14219566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
14229566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1423163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
14249566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs));
14259566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu));
1426163235baSMatthew G. Knepley                 if (vs >= 0) continue;
142763a3b9bcSJacob 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);
1428163235baSMatthew G. Knepley                 hasUnsplit = PETSC_TRUE;
1429163235baSMatthew G. Knepley               }
1430163235baSMatthew G. Knepley             }
1431cd0c2139SMatthew G Knepley           }
1432cd0c2139SMatthew G Knepley         }
14339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
14349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1435e537020bSMatthew G. Knepley         /* Face support */
1436b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1437cd0c2139SMatthew G Knepley 
1438b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1439b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1440b6dfa339SMatthew G. Knepley         else vals[1] = -vals[0];
1441b6dfa339SMatthew 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]);
1442b6dfa339SMatthew G. Knepley 
1443b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1444b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1445b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1446b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1447accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1448b6dfa339SMatthew G. Knepley             } else {
1449b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1450accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1451b6dfa339SMatthew G. Knepley             }
1452b6dfa339SMatthew G. Knepley           } else {
1453b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1454cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
14559566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1456cd0c2139SMatthew G Knepley             } else {
1457cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
14589566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1459b6dfa339SMatthew G. Knepley             }
1460b6dfa339SMatthew G. Knepley           }
1461b6dfa339SMatthew G. Knepley         }
1462b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1463e537020bSMatthew G. Knepley         {
1464e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1465b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1466accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1467e537020bSMatthew G. Knepley 
14689371c9d4SSatish Balay           if (supportSize > 1) {
14699371c9d4SSatish Balay             ocell = vals[0] < 0 ? support[1] : support[0];
14709371c9d4SSatish Balay           } else {
14719371c9d4SSatish Balay             ocell = support[0];
14729371c9d4SSatish Balay             flip  = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;
14739371c9d4SSatish Balay           }
1474b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1475b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1476e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1477e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1478e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1479b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc];
1480e537020bSMatthew G. Knepley               break;
1481cd0c2139SMatthew G Knepley             }
1482cd0c2139SMatthew G Knepley           }
1483b6dfa339SMatthew 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);
1484e537020bSMatthew G. Knepley         }
14854c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1486b6dfa339SMatthew G. Knepley         {
1487b6dfa339SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangments(ct) / 2;
1488b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1489b6dfa339SMatthew G. Knepley         }
1490b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1491b5a892a1SMatthew G. Knepley 
1492fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp; /* Extracted negative side orientation above */
14934c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
14944c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1495e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1496412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1497b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q * 2 + 0];
1498b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q * 2 + 1];
1499b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1500412e9a14SMatthew G. Knepley 
15019566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
150218c5995bSMatthew G. Knepley           if (v < 0) {
15039566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
150418c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
150518c5995bSMatthew G. Knepley           } else {
150618c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep];
150718c5995bSMatthew G. Knepley           }
1508b5a892a1SMatthew G. Knepley           coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1509e537020bSMatthew G. Knepley         }
15109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
15119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
151296a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
15139566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1514cd0c2139SMatthew G Knepley       } else if (dep == 0) {
15154c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1516cd0c2139SMatthew G Knepley 
1517cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
15184c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1519cd0c2139SMatthew G Knepley           PetscInt val;
1520cd0c2139SMatthew G Knepley 
15219566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1522ad540459SPierre Jolivet           if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1523cd0c2139SMatthew G Knepley         }
15244c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
15259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1526cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
15274c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1528cd0c2139SMatthew G Knepley           PetscInt val, edge;
1529cd0c2139SMatthew G Knepley 
15309566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1531cd0c2139SMatthew G Knepley           if (val == 1) {
15329566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
153363a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
15344c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep + 1];
1535cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
15362582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1537cd0c2139SMatthew G Knepley           }
1538cd0c2139SMatthew G Knepley         }
15394c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
15409566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
15414c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1542cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1543cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
15449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
15454c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
15464c367dbcSMatthew G. Knepley           PetscInt val, edge;
15474c367dbcSMatthew G. Knepley 
15489566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
15494c367dbcSMatthew G. Knepley           if (val == 1) {
15509566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
155163a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
15524c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
15534c367dbcSMatthew G. Knepley           }
15544c367dbcSMatthew G. Knepley         }
15559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1556cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
15574c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
15584c367dbcSMatthew G. Knepley 
1559cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1560cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1561cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
15629566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
1563e1757548SMatthew G. Knepley           if (v < 0) {
15649566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
156563a3b9bcSJacob 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);
15662582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1567e1757548SMatthew G. Knepley           } else {
15684c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep - 1];
1569cd0c2139SMatthew G Knepley           }
1570e1757548SMatthew G. Knepley         }
15719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1572cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1573cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1574cd0c2139SMatthew G Knepley           PetscInt val;
1575cd0c2139SMatthew G Knepley 
15769566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
15774c367dbcSMatthew G. Knepley           if (val == dim - 1) {
15782582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
15794c367dbcSMatthew G. Knepley           } else if (val == (shift + dim - 1)) {
15802582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1581cd0c2139SMatthew G Knepley           }
1582cd0c2139SMatthew G Knepley         }
1583b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
15849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1585cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1586cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1587cd0c2139SMatthew G Knepley           PetscInt val, face;
1588cd0c2139SMatthew G Knepley 
15899566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1590cd0c2139SMatthew G Knepley           if (val == dim - 1) {
15919566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
159263a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
15934c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep + 1];
1594cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim - 1)) {
15952582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1596cd0c2139SMatthew G Knepley           }
1597cd0c2139SMatthew G Knepley         }
1598b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
15999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
16004c367dbcSMatthew G. Knepley         /* Hybrid face */
16014c367dbcSMatthew G. Knepley         coneNew[0] = newp;
16024c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
16034c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
16044c367dbcSMatthew G. Knepley           PetscInt vertex;
16059566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex));
1606e1757548SMatthew G. Knepley           if (vertex < 0) {
16079566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex));
160863a3b9bcSJacob 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);
1609e1757548SMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
1610e1757548SMatthew G. Knepley           } else {
16114c367dbcSMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
16124c367dbcSMatthew G. Knepley           }
1613e1757548SMatthew G. Knepley         }
16149566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
16154c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
16164c367dbcSMatthew G. Knepley           PetscInt val, face;
16174c367dbcSMatthew G. Knepley 
16189566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
16194c367dbcSMatthew G. Knepley           if (val == dim - 1) {
16209566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
162163a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
16224c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
16234c367dbcSMatthew G. Knepley           }
16244c367dbcSMatthew G. Knepley         }
16259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1626cd0c2139SMatthew G Knepley       }
1627cd0c2139SMatthew G Knepley     }
1628cd0c2139SMatthew G Knepley   }
162918c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
163018c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
163118c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
16322582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1633b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1634e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
163518c5995bSMatthew G. Knepley 
16369566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
16379566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
16389566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
16399566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
164018c5995bSMatthew G. Knepley       if (dep == 0) {
164118c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
164218c5995bSMatthew G. Knepley 
164318c5995bSMatthew G. Knepley         /* Unsplit vertex */
16449566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
164518c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
16462582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
16479566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
1648ad540459SPierre Jolivet           if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1];
164918c5995bSMatthew G. Knepley         }
165018c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
165118c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
165263a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
16539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
165418c5995bSMatthew G. Knepley         /* Hybrid edge */
165518c5995bSMatthew G. Knepley         coneNew[0] = newp;
165618c5995bSMatthew G. Knepley         coneNew[1] = newp;
16579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
165818c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
165918c5995bSMatthew G. Knepley           PetscInt val, edge;
166018c5995bSMatthew G. Knepley 
16619566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
166218c5995bSMatthew G. Knepley           if (val == 1) {
16639566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
166463a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
166518c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1666e1757548SMatthew G. Knepley           } else if (val == (shift2 + 1)) {
16679566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge));
166863a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1669e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1];
167018c5995bSMatthew G. Knepley           }
167118c5995bSMatthew G. Knepley         }
16729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1673e1757548SMatthew G. Knepley       } else if (dep == dim - 2) {
1674e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1675e1757548SMatthew G. Knepley 
1676da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1677e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1678e1757548SMatthew G. Knepley           PetscInt val, face;
1679e1757548SMatthew G. Knepley 
16809566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1681e1757548SMatthew G. Knepley           if (val == dim - 1) {
16829566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
168363a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
16842582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1685e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 1];
1686e1757548SMatthew G. Knepley           } else {
16872582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1688e1757548SMatthew G. Knepley           }
1689e1757548SMatthew G. Knepley         }
1690e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1691e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
16929566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
169363a3b9bcSJacob 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);
16949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1695e1757548SMatthew G. Knepley         /* Add hybrid face */
1696e1757548SMatthew G. Knepley         coneNew[0] = newp;
1697212cc919SMatthew G. Knepley         coneNew[1] = newp;
16989566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
169963a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1700212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17019566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
170263a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]);
1703e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
17049566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1705da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1706da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1707da1dd7e4SMatthew G. Knepley 
17089566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1709da1dd7e4SMatthew G. Knepley           if (val == dim - 1) {
17109566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
1711da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1712da1dd7e4SMatthew G. Knepley           }
1713da1dd7e4SMatthew G. Knepley         }
17149566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
171563a3b9bcSJacob 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);
17169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1717cd0c2139SMatthew G Knepley       }
1718cd0c2139SMatthew G Knepley     }
1719cd0c2139SMatthew G Knepley   }
1720cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1721cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1722cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1723cd0c2139SMatthew G Knepley     IS              pIS;
1724cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1725cd0c2139SMatthew G Knepley     const PetscInt *points;
1726cd0c2139SMatthew G Knepley 
1727cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
17289566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1729cd0c2139SMatthew G Knepley     if (!pIS) continue;
1730cd0c2139SMatthew G Knepley     dep = -dep - shift;
17319566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
17329566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1733cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1734cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
17352582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1736cd0c2139SMatthew G Knepley       const PetscInt *cone;
1737cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
173850cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1739cd0c2139SMatthew G Knepley 
1740cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1741cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
17429566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
17439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1744cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1745e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift);
1746cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1747cd0c2139SMatthew G Knepley 
17489566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1749cd0c2139SMatthew G Knepley         if (val == dep - 1) {
17509566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp));
175163a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1);
1752cd0c2139SMatthew G Knepley           csplitp = pMaxNew[dep - 1] + cp;
17539566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
175450cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1755cd0c2139SMatthew G Knepley         }
1756cd0c2139SMatthew G Knepley       }
17574a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
175828b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1759cd0c2139SMatthew G Knepley     }
17609566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
17619566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1762cd0c2139SMatthew G Knepley   }
1763fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
17649566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
17659566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
17669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
17679566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1768cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
17692582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1770cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1771cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1772cd0c2139SMatthew G Knepley 
17739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
17749566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
17759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1776cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d];
1777cd0c2139SMatthew G Knepley   }
17789566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1779fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
17809566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1781b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1782fa8e8ae5SToby Isaac   /* Step 9: Labels */
17839566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
17849566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
17859566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1786cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1787cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
17882582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1789cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1790cd0c2139SMatthew G Knepley       PetscInt       l;
1791cd0c2139SMatthew G Knepley 
17927db7e0a7SMatthew G. Knepley       if (splitLabel) {
17937db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
17947db7e0a7SMatthew G. Knepley 
17959566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp, val));
17969566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
17977db7e0a7SMatthew G. Knepley       }
1798cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1799cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1800cd0c2139SMatthew G Knepley         const char *lname;
1801cd0c2139SMatthew G Knepley         PetscInt    val;
18029a356370SMatthew G. Knepley         PetscBool   isDepth;
1803cd0c2139SMatthew G Knepley 
18049566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
18059566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
18069a356370SMatthew G. Knepley         if (isDepth) continue;
18079566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
18089566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
180948a46eb9SPierre Jolivet         if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val));
1810cd0c2139SMatthew G Knepley       }
1811cd0c2139SMatthew G Knepley     }
1812cd0c2139SMatthew G Knepley   }
1813cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1814cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1815cd0c2139SMatthew G Knepley 
1816cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
18179566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
18189566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
18199566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
18209566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1821cd0c2139SMatthew G Knepley   }
1822b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1823b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1824cd0c2139SMatthew G Knepley   if (label) {
18259566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
18269566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1827cd0c2139SMatthew G Knepley   }
18280d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
18299566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
183036dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
18310d4d4d06SMatthew G. Knepley   }
18329566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
18339566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
18349566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
1835cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1836cd0c2139SMatthew G Knepley }
1837cd0c2139SMatthew G Knepley 
1838cd0c2139SMatthew G Knepley /*@C
1839cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1840cd0c2139SMatthew G Knepley 
1841cd0c2139SMatthew G Knepley   Collective on dm
1842cd0c2139SMatthew G Knepley 
1843cd0c2139SMatthew G Knepley   Input Parameters:
1844cd0c2139SMatthew G Knepley + dm - The original DM
184553156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1846cd0c2139SMatthew G Knepley 
1847cd0c2139SMatthew G Knepley   Output Parameters:
18487db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1849cd0c2139SMatthew G Knepley - dmSplit - The new DM
1850cd0c2139SMatthew G Knepley 
1851cd0c2139SMatthew G Knepley   Level: developer
1852cd0c2139SMatthew G Knepley 
1853db781477SPatrick Sanan .seealso: `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1854cd0c2139SMatthew G Knepley @*/
1855d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1856d71ae5a4SJacob Faibussowitsch {
1857cd0c2139SMatthew G Knepley   DM       sdm;
1858cd0c2139SMatthew G Knepley   PetscInt dim;
1859cd0c2139SMatthew G Knepley 
1860cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1861cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1862064a246eSJacob Faibussowitsch   PetscValidPointer(dmSplit, 4);
18639566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
18649566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
18659566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
18669566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1867cd0c2139SMatthew G Knepley   switch (dim) {
1868cd0c2139SMatthew G Knepley   case 2:
1869d71ae5a4SJacob Faibussowitsch   case 3:
1870d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1871d71ae5a4SJacob Faibussowitsch     break;
1872d71ae5a4SJacob Faibussowitsch   default:
1873d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1874cd0c2139SMatthew G Knepley   }
1875cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1876cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1877cd0c2139SMatthew G Knepley }
1878cd0c2139SMatthew G Knepley 
18790f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
1880d71ae5a4SJacob Faibussowitsch static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1881d71ae5a4SJacob Faibussowitsch {
18820f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
18830f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
18840f66a230SMatthew G. Knepley 
18850f66a230SMatthew G. Knepley   PetscFunctionBegin;
18860f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
18879566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
18889566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
18899566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
18909566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
18910f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
18920f66a230SMatthew G. Knepley     if (cone[c] == face) {
18930f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
18940f66a230SMatthew G. Knepley 
18950f66a230SMatthew G. Knepley       if (subdm) {
18960f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
18970f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
18980f66a230SMatthew G. Knepley 
18999566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
19009566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
19019566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
19029566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
19039566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
19040f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
19050f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
19060f66a230SMatthew G. Knepley             o = subornt[0];
19070f66a230SMatthew G. Knepley             break;
19080f66a230SMatthew G. Knepley           }
19090f66a230SMatthew G. Knepley         }
191063a3b9bcSJacob 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);
19110f66a230SMatthew G. Knepley       }
19120f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
19130f66a230SMatthew G. Knepley       else *pos = PETSC_FALSE;
19140f66a230SMatthew G. Knepley       break;
19150f66a230SMatthew G. Knepley     }
19160f66a230SMatthew G. Knepley   }
191763a3b9bcSJacob 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);
19180f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
19190f66a230SMatthew G. Knepley }
19200f66a230SMatthew G. Knepley 
1921d71ae5a4SJacob Faibussowitsch static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label)
1922d71ae5a4SJacob Faibussowitsch {
1923accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
1924accc9626SMatthew G. Knepley   const PetscInt *points;
1925accc9626SMatthew G. Knepley   PetscInt        dim, numPoints, p, shift = 100, shift2 = 200;
1926accc9626SMatthew G. Knepley 
1927accc9626SMatthew G. Knepley   PetscFunctionBegin;
1928accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1929accc9626SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
1930accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
1931accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
1932accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
1933accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
1934accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
1935accc9626SMatthew G. Knepley     PetscFunctionReturn(0);
1936accc9626SMatthew G. Knepley   }
1937accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
1938accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
1939accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
1940accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
1941accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
1942accc9626SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
1943accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
1944accc9626SMatthew G. Knepley     const PetscInt *support;
1945accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
1946accc9626SMatthew G. Knepley 
1947accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
1948accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
1949accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
1950accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
1951accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
1952accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
1953accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
1954e1a13daeSMatthew G. Knepley     /* Check that this face is not incident on only unsplit faces, meaning has at least one split face */
1955e1a13daeSMatthew G. Knepley     {
1956e1a13daeSMatthew G. Knepley       PetscInt *closure = NULL;
1957e1a13daeSMatthew G. Knepley       PetscBool split   = PETSC_FALSE;
1958e1a13daeSMatthew G. Knepley       PetscInt  closureSize, cl;
1959e1a13daeSMatthew G. Knepley 
1960e1a13daeSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1961e1a13daeSMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1962e1a13daeSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
19639371c9d4SSatish Balay         if ((valA >= 0) && (valA <= dim)) {
19649371c9d4SSatish Balay           split = PETSC_TRUE;
19659371c9d4SSatish Balay           break;
19669371c9d4SSatish Balay         }
1967e1a13daeSMatthew G. Knepley       }
1968e1a13daeSMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1969e1a13daeSMatthew G. Knepley       if (!split) continue;
1970e1a13daeSMatthew G. Knepley     }
1971accc9626SMatthew G. Knepley     /* Split the face */
1972accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, point, &valA));
1973accc9626SMatthew G. Knepley     PetscCall(DMLabelClearValue(label, point, valA));
1974accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, dim - 1));
1975accc9626SMatthew G. Knepley     /* Label its closure:
1976accc9626SMatthew G. Knepley       unmarked: label as unsplit
1977accc9626SMatthew G. Knepley       incident: relabel as split
1978accc9626SMatthew G. Knepley       split:    do nothing
1979accc9626SMatthew G. Knepley     */
1980accc9626SMatthew G. Knepley     {
1981accc9626SMatthew G. Knepley       PetscInt *closure = NULL;
1982accc9626SMatthew G. Knepley       PetscInt  closureSize, cl, dep;
1983accc9626SMatthew G. Knepley 
1984accc9626SMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1985accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1986accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
1987accc9626SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
1988accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
1989accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
1990accc9626SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1991accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
1992accc9626SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], valA));
1993accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], dep));
1994accc9626SMatthew G. Knepley         }
1995accc9626SMatthew G. Knepley       }
1996accc9626SMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1997accc9626SMatthew G. Knepley     }
1998accc9626SMatthew G. Knepley   }
1999accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2000accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2001accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
2002accc9626SMatthew G. Knepley }
2003accc9626SMatthew G. Knepley 
2004cd0c2139SMatthew G Knepley /*@
20050f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
2006cd0c2139SMatthew G Knepley   to complete the surface
2007cd0c2139SMatthew G Knepley 
2008cd0c2139SMatthew G Knepley   Input Parameters:
2009cd0c2139SMatthew G Knepley + dm     - The DM
20100f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
20110f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
2012caf9e14dSMatthew G. Knepley . bvalue - Value of DMLabel marking the vertices on the boundary
2013bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
201447946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
2015cd0c2139SMatthew G Knepley 
2016cd0c2139SMatthew G Knepley   Output Parameter:
2017cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
2018cd0c2139SMatthew G Knepley 
20190f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
20200f66a230SMatthew G. Knepley 
2021cd0c2139SMatthew G Knepley   Level: developer
2022cd0c2139SMatthew G Knepley 
2023db781477SPatrick Sanan .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2024cd0c2139SMatthew G Knepley @*/
2025d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, DM subdm)
2026d71ae5a4SJacob Faibussowitsch {
2027d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2028accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
202947946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2030bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
2031accc9626SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = 300, dim, depth, numPoints, numSubpoints, p, val;
2032cd0c2139SMatthew G Knepley 
2033cd0c2139SMatthew G Knepley   PetscFunctionBegin;
20349566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
20359566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
20369566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
203747946fd8SMatthew G. Knepley   if (subdm) {
20389566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
203947946fd8SMatthew G. Knepley     if (subpointIS) {
20409566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
20419566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
204247946fd8SMatthew G. Knepley     }
204347946fd8SMatthew G. Knepley   }
2044d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
20459566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2046accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
20479566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
20489566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2049d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2050cd0c2139SMatthew G Knepley     const PetscInt *support;
2051cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2052cd0c2139SMatthew G Knepley 
20539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2054c4419245SMatthew G. Knepley #if 0
2055c4419245SMatthew G. Knepley     if (supportSize != 2) {
2056c4419245SMatthew G. Knepley       const PetscInt *lp;
2057c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2058c4419245SMatthew G. Knepley 
2059c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2060c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
20619566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
20629566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
206363a3b9bcSJacob 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);
2064c4419245SMatthew G. Knepley     }
2065c4419245SMatthew G. Knepley #endif
20669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2067cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
20680f66a230SMatthew G. Knepley       const PetscInt *cone;
2069cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
20700f66a230SMatthew G. Knepley       PetscBool       pos;
2071cd0c2139SMatthew G Knepley 
20729566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
20739566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
20749566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
20750f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
20760f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
20779566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
20789566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2079cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2080cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2081cd0c2139SMatthew G Knepley 
20829566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2083cd0c2139SMatthew G Knepley         if (val == -1) {
2084cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2085cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2086cd0c2139SMatthew G Knepley 
20879566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2088cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2089cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2090a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2091cd0c2139SMatthew G Knepley 
20929566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
20939566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2094a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim - 1) && (bval < 0)) {
20959566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2096cd0c2139SMatthew G Knepley               break;
2097cd0c2139SMatthew G Knepley             }
2098cd0c2139SMatthew G Knepley           }
20999566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2100cd0c2139SMatthew G Knepley         }
2101cd0c2139SMatthew G Knepley       }
2102cd0c2139SMatthew G Knepley     }
2103cd0c2139SMatthew G Knepley   }
2104accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2105accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2106a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
210786200784SMatthew G. Knepley   if (blabel) {
2108accc9626SMatthew G. Knepley     IS bdIS;
2109accc9626SMatthew G. Knepley 
2110caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2111accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2112accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2113a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2114a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2115a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2116a0541d8aSMatthew G. Knepley 
21179566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2118a0541d8aSMatthew G. Knepley       if (bval >= 0) {
21199566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2120f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2121f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
21229566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2123f7019248SMatthew G. Knepley         }
2124f7019248SMatthew G. Knepley       }
2125f7019248SMatthew G. Knepley     }
2126f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2127f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2128f7019248SMatthew G. Knepley       PetscInt       val, bval;
2129f7019248SMatthew G. Knepley 
21309566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2131f7019248SMatthew G. Knepley       if (bval >= 0) {
213286200784SMatthew G. Knepley         const PetscInt *cone, *support;
213386200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
213486200784SMatthew G. Knepley 
2135a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
21369566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2137e1a13daeSMatthew 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);
21389566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
21399566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
21402c06a818SMatthew G. Knepley         /* Check for cross-edge
21412c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
214286200784SMatthew G. Knepley         if (val != 0) continue;
21439566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
21449566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
214586200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
21469566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
21479566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
214863a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
21499566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
21509566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
21519566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
21529566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
215386200784SMatthew G. Knepley         }
2154a0541d8aSMatthew G. Knepley       }
2155a0541d8aSMatthew G. Knepley     }
2156accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2157accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2158a0541d8aSMatthew G. Knepley   }
2159b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2160b6dfa339SMatthew G. Knepley   {
2161b6dfa339SMatthew G. Knepley     PetscSF         sf;
2162b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2163b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2164b6dfa339SMatthew G. Knepley 
2165b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2166b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2167accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2168accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2169accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2170accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2171b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2172b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2173b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2174b6dfa339SMatthew G. Knepley         PetscInt       val;
2175b6dfa339SMatthew G. Knepley 
2176b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2177b6dfa339SMatthew G. Knepley         if (l >= 0) {
2178b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2179b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2180b6dfa339SMatthew G. Knepley 
2181b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2182b6dfa339SMatthew 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);
2183b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2184b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2185b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2186accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2187b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2188b6dfa339SMatthew G. Knepley 
2189b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2190b6dfa339SMatthew 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);
2191b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2192b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2193b6dfa339SMatthew G. Knepley           }
2194b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2195b6dfa339SMatthew G. Knepley         }
2196b6dfa339SMatthew G. Knepley       }
2197b6dfa339SMatthew G. Knepley     }
2198b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2199b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2200accc9626SMatthew G. Knepley   }
2201accc9626SMatthew G. Knepley divide:
2202b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2203accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
2204accc9626SMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label));
2205cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2206cd0c2139SMatthew G Knepley }
2207cd0c2139SMatthew G Knepley 
2208720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
2209d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2210d71ae5a4SJacob Faibussowitsch {
2211720e594eSMatthew G. Knepley   IS              subpointIS;
2212720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2213720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2214720e594eSMatthew G. Knepley 
2215720e594eSMatthew G. Knepley   PetscFunctionBegin;
2216720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
22179566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
22189566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
2219720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
22209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
22219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
22229566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2223720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2224720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2225720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2226720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2227720e594eSMatthew G. Knepley 
22289566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2229720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2230720e594eSMatthew G. Knepley       PetscInt value = 0;
2231720e594eSMatthew G. Knepley 
2232720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
22339566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
22349371c9d4SSatish Balay       if (value == defaultValue) {
22359371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
22369371c9d4SSatish Balay         break;
22379371c9d4SSatish Balay       }
2238720e594eSMatthew G. Knepley     }
22399566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2240720e594eSMatthew G. Knepley     if (invalidCell) {
22419566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
22429566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
22439566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
224463a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2245720e594eSMatthew G. Knepley     }
2246720e594eSMatthew G. Knepley   }
22479566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
2248720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
2249720e594eSMatthew G. Knepley }
2250720e594eSMatthew G. Knepley 
2251c08575a3SMatthew G. Knepley /*@
22523cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
22533cf72582SMatthew G. Knepley 
22543cf72582SMatthew G. Knepley   Collective on dm
22553cf72582SMatthew G. Knepley 
22563cf72582SMatthew G. Knepley   Input Parameters:
22573cf72582SMatthew G. Knepley + dm - The original DM
2258720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
2259caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2260caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
22613cf72582SMatthew G. Knepley 
22623cf72582SMatthew G. Knepley   Output Parameters:
22637db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
22647db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
2265720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
2266720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
22673cf72582SMatthew G. Knepley 
22686eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
22696eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
22706eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
22716eccb800SMatthew 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
22726eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
22736eccb800SMatthew Knepley 
22746eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
22756eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
22766eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
22776eccb800SMatthew Knepley 
22786eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
22796eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
22806eccb800SMatthew Knepley 
22813cf72582SMatthew G. Knepley   Level: developer
22823cf72582SMatthew G. Knepley 
2283db781477SPatrick Sanan .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
22843cf72582SMatthew G. Knepley @*/
2285d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
2286d71ae5a4SJacob Faibussowitsch {
22873cf72582SMatthew G. Knepley   DM       idm;
22887db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
22893cf72582SMatthew G. Knepley   PetscInt dim;
22903cf72582SMatthew G. Knepley 
22913cf72582SMatthew G. Knepley   PetscFunctionBegin;
22923cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
229392ef8ba2SVaclav Hapla   if (label) PetscValidPointer(label, 2);
2294720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
229592ef8ba2SVaclav Hapla   if (hybridLabel) PetscValidPointer(hybridLabel, 5);
229692ef8ba2SVaclav Hapla   if (splitLabel) PetscValidPointer(splitLabel, 6);
229792ef8ba2SVaclav Hapla   if (dmInterface) PetscValidPointer(dmInterface, 7);
229892ef8ba2SVaclav Hapla   PetscValidPointer(dmHybrid, 8);
22999566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
23009566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
23019566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
23029566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
23039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
23049566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
23059566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
23067db7e0a7SMatthew G. Knepley   if (splitLabel) {
23077db7e0a7SMatthew G. Knepley     const char *name;
23087db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
23097db7e0a7SMatthew G. Knepley 
23109566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
23119566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN));
23129566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(sname, " split"));
23139566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
23147db7e0a7SMatthew G. Knepley   }
2315caf9e14dSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, idm));
23169371c9d4SSatish Balay   if (dmInterface) {
23179371c9d4SSatish Balay     *dmInterface = idm;
23189371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
23199566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
23205de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
23213cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
23229566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
23237db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
23244a7ee7d0SMatthew G. Knepley   {
23254a7ee7d0SMatthew G. Knepley     DM      cdm;
23264a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
23274a7ee7d0SMatthew G. Knepley 
23284a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
23299566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
23309566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
23319566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
23324a7ee7d0SMatthew G. Knepley   }
2333cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2334cd0c2139SMatthew G Knepley }
2335cd0c2139SMatthew G Knepley 
2336efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2337efa14ee0SMatthew G Knepley 
2338efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2339efa14ee0SMatthew G Knepley */
2340d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2341d71ae5a4SJacob Faibussowitsch {
2342fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2343efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2344412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2345efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2346efa14ee0SMatthew G Knepley 
2347efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2348efa14ee0SMatthew G Knepley   *numFaces = 0;
2349efa14ee0SMatthew G Knepley   *nFV      = 0;
23509566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
23519566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
235277d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
23539566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
235448a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2355efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
23569566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2357efa14ee0SMatthew G Knepley   if (subvertexIS) {
23589566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
23599566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2360efa14ee0SMatthew G Knepley   }
2361efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2362efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
23630298fd71SBarry Smith     PetscInt      *star   = NULL;
2364efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2365efa14ee0SMatthew G Knepley 
23669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2367efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2368efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2369efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2370efa14ee0SMatthew G Knepley     }
2371efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2372efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
23730298fd71SBarry Smith       PetscInt      *closure = NULL;
2374efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2375efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2376efa14ee0SMatthew G Knepley 
23779566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
237865560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
237963a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
23809566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2381efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2382efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2383efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2384efa14ee0SMatthew G Knepley 
2385efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2386efa14ee0SMatthew G Knepley           ++numCorners;
23879566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2388830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2389efa14ee0SMatthew G Knepley         }
2390efa14ee0SMatthew G Knepley       }
23919566063dSJacob Faibussowitsch       if (!(*nFV)) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
239263a3b9bcSJacob 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);
2393efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2394007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2395007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2396007baee2SMatthew G. Knepley 
2397efa14ee0SMatthew G Knepley         ++(*numFaces);
239848a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
23999566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
240048a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
24019566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2402efa14ee0SMatthew G Knepley       }
24039566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2404efa14ee0SMatthew G Knepley     }
24059566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2406efa14ee0SMatthew G Knepley   }
240748a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
24089566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
24099566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2410efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2411efa14ee0SMatthew G Knepley }
2412efa14ee0SMatthew G Knepley 
2413d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2414d71ae5a4SJacob Faibussowitsch {
241534b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2416efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2417412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2418efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2419efa14ee0SMatthew G Knepley 
2420efa14ee0SMatthew G Knepley   PetscFunctionBegin;
24219566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24229566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
242348a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2424efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
242534b4c39eSMatthew G. Knepley   if (vertexLabel) {
24269566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2427efa14ee0SMatthew G Knepley     if (subvertexIS) {
24289566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24299566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2430efa14ee0SMatthew G Knepley     }
243134b4c39eSMatthew G. Knepley   }
2432efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2433efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24340298fd71SBarry Smith     PetscInt      *star   = NULL;
2435efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2436efa14ee0SMatthew G Knepley 
24379566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2438efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2439efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2440158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2441158acfadSMatthew G. Knepley 
2442158acfadSMatthew G. Knepley       if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) {
2443158acfadSMatthew G. Knepley         if (markedFaces) {
24449566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2445158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2446158acfadSMatthew G. Knepley         }
2447158acfadSMatthew G. Knepley         star[numFaces++] = point;
2448158acfadSMatthew G. Knepley       }
2449efa14ee0SMatthew G Knepley     }
2450efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2451efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
24520298fd71SBarry Smith       PetscInt      *closure = NULL;
2453efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2454efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2455efa14ee0SMatthew G Knepley 
24569566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2457efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
245863a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
24599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2460efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2461efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2462efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2463efa14ee0SMatthew G Knepley 
2464efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
24659566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2466830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2467efa14ee0SMatthew G Knepley         }
2468efa14ee0SMatthew G Knepley       }
2469efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2470efa14ee0SMatthew G Knepley         const PetscInt *support;
2471efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2472efa14ee0SMatthew G Knepley 
2473efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2474efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2475efa14ee0SMatthew G Knepley 
2476efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2477efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
24789566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2479efa14ee0SMatthew G Knepley               break;
2480efa14ee0SMatthew G Knepley             }
2481efa14ee0SMatthew G Knepley           }
2482efa14ee0SMatthew G Knepley         }
24839566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
24849566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
248548a46eb9SPierre Jolivet         for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2486efa14ee0SMatthew G Knepley       }
24879566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2488efa14ee0SMatthew G Knepley     }
24899566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2490efa14ee0SMatthew G Knepley   }
24919566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
24929566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
24939566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2494efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2495efa14ee0SMatthew G Knepley }
2496efa14ee0SMatthew G Knepley 
2497d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2498d71ae5a4SJacob Faibussowitsch {
249927c04023SMatthew G. Knepley   DMLabel         label = NULL;
2500766ab985SMatthew G. Knepley   const PetscInt *cone;
25019fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2502766ab985SMatthew G. Knepley 
2503812bfc34SJed Brown   PetscFunctionBegin;
2504c0ed958bSJed Brown   *numFaces = 0;
2505c0ed958bSJed Brown   *nFV      = 0;
25069566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2507fed694aaSMatthew G. Knepley   *subCells = NULL;
25089566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25099566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2510766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
251127c04023SMatthew G. Knepley   if (label) {
251227c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
251327c04023SMatthew G. Knepley       PetscInt val;
251427c04023SMatthew G. Knepley 
25159566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
251627c04023SMatthew G. Knepley       if (val == value) {
251727c04023SMatthew G. Knepley         ++(*numFaces);
25189566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
251927c04023SMatthew G. Knepley       }
252027c04023SMatthew G. Knepley     }
252127c04023SMatthew G. Knepley   } else {
2522766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
25239566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
252427c04023SMatthew G. Knepley   }
25259566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
25269fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
25279fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2528766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2529766ab985SMatthew G. Knepley     const PetscInt *cells;
2530766ab985SMatthew G. Knepley     PetscInt        numCells;
2531766ab985SMatthew G. Knepley 
253227c04023SMatthew G. Knepley     if (label) {
253327c04023SMatthew G. Knepley       PetscInt val;
253427c04023SMatthew G. Knepley 
25359566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
253627c04023SMatthew G. Knepley       if (val != value) continue;
253727c04023SMatthew G. Knepley     }
25389566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
253948a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2540766ab985SMatthew G. Knepley     /* Negative face */
25419566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
254227234c99SMatthew G. Knepley     /* Not true in parallel
254308401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2544766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
25459566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
254627234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2547766ab985SMatthew G. Knepley     }
25489566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2549766ab985SMatthew G. Knepley     /* Positive face is not included */
2550766ab985SMatthew G. Knepley   }
2551766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2552766ab985SMatthew G. Knepley }
2553766ab985SMatthew G. Knepley 
2554d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2555d71ae5a4SJacob Faibussowitsch {
2556766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2557766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2558766ab985SMatthew G. Knepley 
2559812bfc34SJed Brown   PetscFunctionBegin;
25609566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2562766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
25639566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
25649566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2565766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2566766ab985SMatthew G. Knepley     const PetscInt *cone;
2567766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2568b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2569766ab985SMatthew G. Knepley 
257027c04023SMatthew G. Knepley     if (label) {
25719566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
257227c04023SMatthew G. Knepley       if (val != value) continue;
257327c04023SMatthew G. Knepley     }
25749566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
25759566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
25769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
25771dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2578b3154360SMatthew G. Knepley     /* Negative face */
25799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2580766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2581766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2582766ab985SMatthew G. Knepley 
2583766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2584766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
25859566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2586766ab985SMatthew G. Knepley           break;
2587766ab985SMatthew G. Knepley         }
2588766ab985SMatthew G. Knepley       }
2589766ab985SMatthew G. Knepley     }
25909566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2591766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2592766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2593766ab985SMatthew G. Knepley       const PetscInt *support;
2594766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2595766ab985SMatthew G. Knepley 
25969566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
259708401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
25989566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
259948a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2600766ab985SMatthew G. Knepley     }
2601766ab985SMatthew G. Knepley   }
26029566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2603766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2604766ab985SMatthew G. Knepley }
2605766ab985SMatthew G. Knepley 
2606d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2607d71ae5a4SJacob Faibussowitsch {
260882f516ccSBarry Smith   MPI_Comm       comm;
2609e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2610e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2611e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2612e6ccafaeSMatthew G Knepley 
261382f516ccSBarry Smith   PetscFunctionBegin;
26149566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
26159566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
261663a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2617e6ccafaeSMatthew G Knepley 
2618ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2619ddeab2a6SMatthew G. Knepley     /* Triangle */
2620e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2621e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2622ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2623ddeab2a6SMatthew G. Knepley     /* Triangle */
2624ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2625ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2626ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2627ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2628ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2629ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2630e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2631e6ccafaeSMatthew G Knepley     /* Quadratic line */
2632e6ccafaeSMatthew G Knepley     faceSize  = 1;
2633e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2634e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2635e6ccafaeSMatthew G Knepley     /* Quads */
2636e6ccafaeSMatthew G Knepley     faceSize = 2;
2637e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2638e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2639e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2640e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2641e6ccafaeSMatthew G Knepley     } else {
2642e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2643e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2644e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2645e6ccafaeSMatthew G Knepley     }
2646e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2647e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2648e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2649e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2650e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2651e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2652e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2653e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2654e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2655e6ccafaeSMatthew G Knepley       2, 3, 5, /* left */
2656e6ccafaeSMatthew G Knepley     };
2657e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2658e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2659e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2660e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2661e6ccafaeSMatthew G Knepley     };
2662e6ccafaeSMatthew G Knepley 
2663e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
26649566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2665e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2666e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2667e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2668e6ccafaeSMatthew G Knepley 
26699371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) {
2670e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2671e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2672e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2673e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2674e6ccafaeSMatthew G Knepley               break;
2675e6ccafaeSMatthew G Knepley             }
2676e6ccafaeSMatthew G Knepley           }
2677e6ccafaeSMatthew G Knepley         }
2678e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2679e6ccafaeSMatthew G Knepley         break;
2680e6ccafaeSMatthew G Knepley       }
2681e6ccafaeSMatthew G Knepley     }
268228b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2683e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2684e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2685e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2686e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2687e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2688e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2689e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2690e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2691e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2692e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2693e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2694e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2695e6ccafaeSMatthew G Knepley       0, 3, 7, /* left */
2696e6ccafaeSMatthew G Knepley     };
2697e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2698e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2699e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2700e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2701e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2702e6ccafaeSMatthew G Knepley     };
2703e6ccafaeSMatthew G Knepley 
2704e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
27059566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2706e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2707e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2708e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2709e6ccafaeSMatthew G Knepley 
27109371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) {
2711e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2712e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2713e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2714e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2715e6ccafaeSMatthew G Knepley               break;
2716e6ccafaeSMatthew G Knepley             }
2717e6ccafaeSMatthew G Knepley           }
2718e6ccafaeSMatthew G Knepley         }
2719e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2720e6ccafaeSMatthew G Knepley         break;
2721e6ccafaeSMatthew G Knepley       }
2722e6ccafaeSMatthew G Knepley     }
272328b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2724e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2725e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2726e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2727e6ccafaeSMatthew G Knepley     /* Hexes
2728e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2729e6ccafaeSMatthew G Knepley        pointing up at the second.
2730e6ccafaeSMatthew G Knepley 
2731e6ccafaeSMatthew G Knepley           7---6
2732e6ccafaeSMatthew G Knepley          /|  /|
2733e6ccafaeSMatthew G Knepley         4---5 |
2734ddeab2a6SMatthew G. Knepley         | 1-|-2
2735e6ccafaeSMatthew G Knepley         |/  |/
2736ddeab2a6SMatthew G. Knepley         0---3
2737e6ccafaeSMatthew G Knepley 
2738e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2739e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2740e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2741e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2742e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2743e6ccafaeSMatthew G Knepley       0, 1, 2, 3, /* bottom */
2744e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2745ddeab2a6SMatthew G. Knepley       0, 3, 4, 5, /* front */
2746ddeab2a6SMatthew G. Knepley       2, 3, 5, 6, /* right */
2747ddeab2a6SMatthew G. Knepley       1, 2, 6, 7, /* back */
2748ddeab2a6SMatthew G. Knepley       0, 1, 4, 7, /* left */
2749e6ccafaeSMatthew G Knepley     };
2750e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2751ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2752e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2753ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2754ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2755ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2756ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2757e6ccafaeSMatthew G Knepley     };
2758e6ccafaeSMatthew G Knepley 
2759e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
27609566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2761e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2762e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2763e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2764e6ccafaeSMatthew G Knepley 
27659371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) {
2766e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2767e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2768e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2769e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2770e6ccafaeSMatthew G Knepley               break;
2771e6ccafaeSMatthew G Knepley             }
2772e6ccafaeSMatthew G Knepley           }
2773e6ccafaeSMatthew G Knepley         }
2774e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2775e6ccafaeSMatthew G Knepley         break;
2776e6ccafaeSMatthew G Knepley       }
2777e6ccafaeSMatthew G Knepley     }
277828b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2779e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2780e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2781e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2782e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2783e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2784e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2785e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2786e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2787e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2788e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2789e6ccafaeSMatthew G Knepley       0, 3, 4, 6, 7, 9, /* front */
2790e6ccafaeSMatthew G Knepley       1, 4, 5, 7, 8, 9, /* right */
2791e6ccafaeSMatthew G Knepley       2, 3, 5, 6, 8, 9, /* left */
2792e6ccafaeSMatthew G Knepley     };
2793e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2794e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2795e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2796e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2797e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2798e6ccafaeSMatthew G Knepley     };
2799e6ccafaeSMatthew G Knepley 
2800e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
28019566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2802e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2803e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2804e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2805e6ccafaeSMatthew G Knepley 
28069371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) {
2807e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2808e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2809e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2810e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2811e6ccafaeSMatthew G Knepley               break;
2812e6ccafaeSMatthew G Knepley             }
2813e6ccafaeSMatthew G Knepley           }
2814e6ccafaeSMatthew G Knepley         }
2815e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2816e6ccafaeSMatthew G Knepley         break;
2817e6ccafaeSMatthew G Knepley       }
2818e6ccafaeSMatthew G Knepley     }
281928b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2820e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2821e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2822e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2823e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2824e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2825e6ccafaeSMatthew G Knepley        pointing up at the second.
2826e6ccafaeSMatthew G Knepley 
2827e6ccafaeSMatthew G Knepley          7---6
2828e6ccafaeSMatthew G Knepley         /|  /|
2829e6ccafaeSMatthew G Knepley        4---5 |
2830e6ccafaeSMatthew G Knepley        | 3-|-2
2831e6ccafaeSMatthew G Knepley        |/  |/
2832e6ccafaeSMatthew G Knepley        0---1
2833e6ccafaeSMatthew G Knepley 
2834e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2835e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2836e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2837e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2838e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2839e6ccafaeSMatthew G Knepley       0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2840e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2841e6ccafaeSMatthew G Knepley       0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2842e6ccafaeSMatthew G Knepley       1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2843e6ccafaeSMatthew G Knepley       2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2844e6ccafaeSMatthew G Knepley       0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2845e6ccafaeSMatthew G Knepley     };
2846e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2847e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2848e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2849e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2850e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2851e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2852e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2853e6ccafaeSMatthew G Knepley     };
2854e6ccafaeSMatthew G Knepley 
2855e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
28569566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2857e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2858e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2859e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2860e6ccafaeSMatthew G Knepley 
28619371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) {
2862e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2863e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2864e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2865e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2866e6ccafaeSMatthew G Knepley               break;
2867e6ccafaeSMatthew G Knepley             }
2868e6ccafaeSMatthew G Knepley           }
2869e6ccafaeSMatthew G Knepley         }
2870e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2871e6ccafaeSMatthew G Knepley         break;
2872e6ccafaeSMatthew G Knepley       }
2873e6ccafaeSMatthew G Knepley     }
287428b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2875e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2876e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2877e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2878e6ccafaeSMatthew G Knepley   if (!posOrient) {
28799566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
2880e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
2881e6ccafaeSMatthew G Knepley   } else {
28829566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
2883e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2884e6ccafaeSMatthew G Knepley   }
2885e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2886e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2887e6ccafaeSMatthew G Knepley }
2888e6ccafaeSMatthew G Knepley 
2889c08575a3SMatthew G. Knepley /*@
2890c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2891c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2892c08575a3SMatthew G. Knepley 
2893c08575a3SMatthew G. Knepley   Not collective
2894c08575a3SMatthew G. Knepley 
2895c08575a3SMatthew G. Knepley   Input Parameters:
2896c08575a3SMatthew G. Knepley + dm           - The original mesh
2897c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2898c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2899c08575a3SMatthew G. Knepley . face         - The face vertices
2900c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2901c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2902c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2903c08575a3SMatthew G. Knepley 
2904d8d19677SJose E. Roman   Output Parameters:
2905c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2906c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2907c08575a3SMatthew G. Knepley 
2908c08575a3SMatthew G. Knepley   Level: developer
2909c08575a3SMatthew G. Knepley 
2910db781477SPatrick Sanan .seealso: `DMPlexGetCone()`
2911c08575a3SMatthew G. Knepley @*/
2912d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2913d71ae5a4SJacob Faibussowitsch {
29140298fd71SBarry Smith   const PetscInt *cone = NULL;
2915e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2916e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2917e6ccafaeSMatthew G Knepley 
2918e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
29199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
29209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
2921e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2922e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2923e6ccafaeSMatthew G Knepley 
2924e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2925e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
29269371c9d4SSatish Balay         found = PETSC_TRUE;
29279371c9d4SSatish Balay         break;
2928e6ccafaeSMatthew G Knepley       }
2929e6ccafaeSMatthew G Knepley     }
2930e6ccafaeSMatthew G Knepley     if (found) {
2931e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2932e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2933e6ccafaeSMatthew G Knepley       ++v2;
2934e6ccafaeSMatthew G Knepley     } else {
2935e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2936e6ccafaeSMatthew G Knepley     }
2937e6ccafaeSMatthew G Knepley   }
29389566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
2939e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2940e6ccafaeSMatthew G Knepley }
2941e6ccafaeSMatthew G Knepley 
2942e6ccafaeSMatthew G Knepley /*
2943cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2944e6ccafaeSMatthew G Knepley 
2945e6ccafaeSMatthew G Knepley   Not collective
2946e6ccafaeSMatthew G Knepley 
2947e6ccafaeSMatthew G Knepley   Input Parameters:
2948e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2949e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2950e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2951e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2952e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2953e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2954e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2955e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2956e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2957e6ccafaeSMatthew G Knepley 
2958e6ccafaeSMatthew G Knepley   Output Parameters:
2959e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2960e6ccafaeSMatthew G Knepley 
2961e6ccafaeSMatthew G Knepley   Level: developer
2962e6ccafaeSMatthew G Knepley */
2963d71ae5a4SJacob 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)
2964d71ae5a4SJacob Faibussowitsch {
296582f516ccSBarry Smith   MPI_Comm        comm;
2966e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
2967e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2968e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2969e6ccafaeSMatthew G Knepley 
2970e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
29719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
29729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
297363a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
2974e6ccafaeSMatthew G Knepley #if 0
2975e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
29769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
2977e6ccafaeSMatthew G Knepley #else
2978e6ccafaeSMatthew G Knepley   {
2979e6ccafaeSMatthew G Knepley     PetscInt f;
2980e6ccafaeSMatthew G Knepley 
2981e6ccafaeSMatthew G Knepley     numFaces = 0;
29829566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
2983e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2984e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2985e6ccafaeSMatthew G Knepley 
29869566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
29879566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
2988e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2989e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2990e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
2991e6ccafaeSMatthew G Knepley         PetscInt       v;
2992e6ccafaeSMatthew G Knepley 
2993e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2994e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2995e6ccafaeSMatthew G Knepley         }
2996e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2997e6ccafaeSMatthew G Knepley       }
2998e6ccafaeSMatthew G Knepley       if (d == dof) {
2999e6ccafaeSMatthew G Knepley         numFaces               = 1;
3000e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
3001e6ccafaeSMatthew G Knepley       }
3002e6ccafaeSMatthew G Knepley     }
3003e6ccafaeSMatthew G Knepley   }
3004e6ccafaeSMatthew G Knepley #endif
300563a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3006f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3007e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
30089566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3009e6ccafaeSMatthew G Knepley   } else {
3010e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3011e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3012e6ccafaeSMatthew G Knepley 
30139566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3014e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3015e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
3016e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
30179566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3018e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3019e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3020e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3021e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3022e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3023e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3024e6ccafaeSMatthew G Knepley           break;
3025e6ccafaeSMatthew G Knepley         }
3026e6ccafaeSMatthew G Knepley       }
302763a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3028e6ccafaeSMatthew G Knepley     }
30299566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
30309566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
30319566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3032e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3033e6ccafaeSMatthew G Knepley   }
3034ef07cca7SMatthew G. Knepley #if 0
30359566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3036ef07cca7SMatthew G. Knepley #else
30379566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
3038ef07cca7SMatthew G. Knepley #endif
3039e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3040e6ccafaeSMatthew G Knepley }
3041e6ccafaeSMatthew G Knepley 
3042d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3043d71ae5a4SJacob Faibussowitsch {
304482f516ccSBarry Smith   MPI_Comm        comm;
304553156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3046efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
3047efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3048efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3049fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3050efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3051e6ccafaeSMatthew G Knepley 
3052e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30539566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3054efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
30559566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
30569566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
30579566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
30589566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3059efa14ee0SMatthew G Knepley   /* Setup chart */
30609566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
30619566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
30629566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
30639566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3064e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3065e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3066efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
3067e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
30689566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
30699566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
30709566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
30719566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
307248a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
307348a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
30749566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3075e6ccafaeSMatthew G Knepley   /* Create face cones */
30769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
30779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
30789566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3079e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3080e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3081efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
30820298fd71SBarry Smith     PetscInt      *closure = NULL;
3083efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3084e6ccafaeSMatthew G Knepley 
30859566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3086efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3087efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3088e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3089e6ccafaeSMatthew G Knepley 
3090efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3091efa14ee0SMatthew G Knepley         ++numCorners;
30929566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3093efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3094efa14ee0SMatthew G Knepley           closure[faceSize] = point;
309565560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3096e6ccafaeSMatthew G Knepley           ++faceSize;
3097e6ccafaeSMatthew G Knepley         }
3098e6ccafaeSMatthew G Knepley       }
3099e6ccafaeSMatthew G Knepley     }
310063a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
310148a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
31029566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3103e6ccafaeSMatthew G Knepley   }
31049566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
31059566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
31069566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3107e6ccafaeSMatthew G Knepley   /* Build coordinates */
3108efa14ee0SMatthew G Knepley   {
3109efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3110efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3111efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3112285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
311324640c55SToby Isaac     const char  *name;
3114efa14ee0SMatthew G Knepley 
31159566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
31169566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
31179566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
31189566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
31199566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
31209566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
31219566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3122efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3123efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3124efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3125efa14ee0SMatthew G Knepley       PetscInt       dof;
3126efa14ee0SMatthew G Knepley 
31279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
31289566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
31299566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3130e6ccafaeSMatthew G Knepley     }
31319566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
31329566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
31339566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
31349566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
31359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
31369566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
31379566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3138830e53efSMatthew G. Knepley     if (coordSize) {
31399566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
31409566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3141efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3142efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3143efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3144efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3145e6ccafaeSMatthew G Knepley 
31469566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
31479566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
31489566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
31499566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
315063a3b9bcSJacob 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);
3151e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3152e6ccafaeSMatthew G Knepley       }
31539566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
31549566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
31553b399e24SMatthew G. Knepley     }
31569566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
31579566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3158e6ccafaeSMatthew G Knepley   }
3159efa14ee0SMatthew G Knepley   /* Cleanup */
31609566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
31619566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
31629566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
31639566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
3164e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3165e6ccafaeSMatthew G Knepley }
3166e6ccafaeSMatthew G Knepley 
3167d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3168d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
3169d71ae5a4SJacob Faibussowitsch {
31703982b651SMatthew G. Knepley   PetscInt       subPoint;
31713982b651SMatthew G. Knepley   PetscErrorCode ierr;
31723982b651SMatthew G. Knepley 
31739371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
31749371c9d4SSatish Balay   if (ierr) return -1;
31753982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
31763982b651SMatthew G. Knepley }
31773982b651SMatthew G. Knepley 
3178d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */
3179d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[])
3180d71ae5a4SJacob Faibussowitsch {
3181dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3182dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3183dd05d810SMatthew G. Knepley 
3184dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3185dd05d810SMatthew G. Knepley   if (ierr) return -1;
3186dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3187dd05d810SMatthew G. Knepley }
3188dd05d810SMatthew G. Knepley 
3189d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3190d71ae5a4SJacob Faibussowitsch {
3191695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3192212103e5SMatthew Knepley   PetscInt Nl, l, d;
3193212103e5SMatthew Knepley 
3194212103e5SMatthew Knepley   PetscFunctionBegin;
3195695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3196695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3197695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
31989566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3199212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3200212103e5SMatthew Knepley     DMLabel         label, newlabel;
3201212103e5SMatthew Knepley     const char     *lname;
3202d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3203212103e5SMatthew Knepley     IS              valueIS;
3204212103e5SMatthew Knepley     const PetscInt *values;
3205212103e5SMatthew Knepley     PetscInt        Nv, v;
3206212103e5SMatthew Knepley 
32079566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
32089566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
32099566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
32109566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
32119566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3212d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
32139566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
32149566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
32159566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
32169566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
32179566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
32189566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
32199566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
32209566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3221212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3222212103e5SMatthew Knepley       IS              pointIS;
3223212103e5SMatthew Knepley       const PetscInt *points;
3224212103e5SMatthew Knepley       PetscInt        Np, p;
3225212103e5SMatthew Knepley 
32269566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
32279566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
32289566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3229212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3230212103e5SMatthew Knepley         const PetscInt point = points[p];
3231212103e5SMatthew Knepley         PetscInt       subp;
3232212103e5SMatthew Knepley 
32339566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3234212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
32359566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3236212103e5SMatthew Knepley       }
32379566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
32389566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3239212103e5SMatthew Knepley     }
32409566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
32419566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3242212103e5SMatthew Knepley   }
3243212103e5SMatthew Knepley   PetscFunctionReturn(0);
3244212103e5SMatthew Knepley }
3245212103e5SMatthew Knepley 
3246d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3247d71ae5a4SJacob Faibussowitsch {
324882f516ccSBarry Smith   MPI_Comm         comm;
324953156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3250efa14ee0SMatthew G Knepley   IS              *subpointIS;
3251efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
32523982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3253dd05d810SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, v;
32540d366550SMatthew G. Knepley   PetscMPIInt      rank;
3255e6ccafaeSMatthew G Knepley 
3256e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
32579566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
32589566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3259efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
32609566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
32619566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3262bec263e5SMatthew G. Knepley   if (cellHeight) {
32639566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
32649566063dSJacob Faibussowitsch     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3265bec263e5SMatthew G. Knepley   } else {
3266bec263e5SMatthew G. Knepley     DMLabel         depth;
3267bec263e5SMatthew G. Knepley     IS              pointIS;
3268bec263e5SMatthew G. Knepley     const PetscInt *points;
3269b85c8bf9SMatthew G. Knepley     PetscInt        numPoints = 0;
3270bec263e5SMatthew G. Knepley 
32719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
32729566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3273b85c8bf9SMatthew G. Knepley     if (pointIS) {
32749566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
32759566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3276b85c8bf9SMatthew G. Knepley     }
3277bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3278bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3279bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3280bec263e5SMatthew G. Knepley 
32819566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3282bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
32839566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
32849566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3285bec263e5SMatthew G. Knepley       }
32869566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3287bec263e5SMatthew G. Knepley     }
32889566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
32899566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3290bec263e5SMatthew G. Knepley   }
3291efa14ee0SMatthew G Knepley   /* Setup chart */
32929566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3293dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
32949566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3295e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32969566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3297e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3298e6ccafaeSMatthew G Knepley   }
3299dd05d810SMatthew G. Knepley   // Determine submesh dimension
3300dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3301dd05d810SMatthew G. Knepley   if (sdim > 0) {
3302dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3303dd05d810SMatthew G. Knepley     sdim = dim;
3304dd05d810SMatthew G. Knepley   } else {
3305dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3306dd05d810SMatthew G. Knepley     PetscInt lsdim;
3307dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3308dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3309dd05d810SMatthew G. Knepley     PetscCall(MPI_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPIU_MAX, comm));
3310dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3311dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3312dd05d810SMatthew G. Knepley   }
33139566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
33149566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3315e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3316dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3317dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3318dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3319dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3320dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
33219566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
33229566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3323e6ccafaeSMatthew G Knepley   }
3324412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
33259566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3326dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3327e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3328e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3329e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3330e6ccafaeSMatthew G Knepley       const PetscInt *cone;
333115100a53SVaclav Hapla       PetscInt        coneSize;
3332e6ccafaeSMatthew G Knepley 
33339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3334dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
333515100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
333615100a53SVaclav Hapla 
33379566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3338e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
33399566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3340e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3341e6ccafaeSMatthew G Knepley         }
33429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
33439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
334415100a53SVaclav Hapla       } else {
334515100a53SVaclav Hapla         DMPolytopeType ct;
334615100a53SVaclav Hapla 
334715100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
334815100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
334915100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3350e6ccafaeSMatthew G Knepley       }
3351e6ccafaeSMatthew G Knepley     }
3352e6ccafaeSMatthew G Knepley   }
33539566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
33549566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3355e6ccafaeSMatthew G Knepley   /* Set cones */
33569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
33579566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3358dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3359e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3360e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3361e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
33620e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
33630d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3364e6ccafaeSMatthew G Knepley 
3365dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
33660d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
33670d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
33680d366550SMatthew G. Knepley 
33699566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
33709566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
33710d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3372064cae4fSPierre Jolivet           PetscBool isHybrid = PETSC_FALSE;
3373412e9a14SMatthew G. Knepley 
33749566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3375412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
33769566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
33770d366550SMatthew G. Knepley           if (subc >= 0) {
33780d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
33790d366550SMatthew G. Knepley 
33809566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
33819566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
33829566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
33830d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
33840d366550SMatthew G. Knepley               if (cone[c] == point) {
33850d366550SMatthew G. Knepley                 fornt = ornt[c];
33860d366550SMatthew G. Knepley                 break;
33870d366550SMatthew G. Knepley               }
33880d366550SMatthew G. Knepley             }
33890d366550SMatthew G. Knepley             break;
33900d366550SMatthew G. Knepley           }
33910d366550SMatthew G. Knepley         }
33920d366550SMatthew G. Knepley       }
33939566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
33949566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
33959566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
33969566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3397e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
33989566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
339901a2673eSMatthew G. Knepley         if (subc >= 0) {
340001a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
34013982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
340201a2673eSMatthew G. Knepley           ++coneSizeNew;
340301a2673eSMatthew G. Knepley         }
3404e6ccafaeSMatthew G Knepley       }
340563a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
34069566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
34079566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
34089566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3409e6ccafaeSMatthew G Knepley     }
3410e6ccafaeSMatthew G Knepley   }
34119566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
34129566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
34139566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3414e6ccafaeSMatthew G Knepley   /* Build coordinates */
3415e6ccafaeSMatthew G Knepley   {
3416e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3417e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3418e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3419c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
342024640c55SToby Isaac     const char  *name;
3421e6ccafaeSMatthew G Knepley 
34229566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
34239566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
34249566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
34259566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
34269566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
34279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
34289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
34299566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0] + numSubPoints[0]));
3430e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3431e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3432e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3433e6ccafaeSMatthew G Knepley       PetscInt       dof;
3434e6ccafaeSMatthew G Knepley 
34359566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
34369566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
34379566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3438e6ccafaeSMatthew G Knepley     }
34399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
34409566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
34419566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
34429566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
34439566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
34449566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
34459566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
34469566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
34479566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
34489566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3449e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3450e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3451e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3452e6ccafaeSMatthew G Knepley       PetscInt       dof, off, sdof, soff, d;
3453e6ccafaeSMatthew G Knepley 
34549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
34559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
34569566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
34579566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
345863a3b9bcSJacob 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);
3459efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3460e6ccafaeSMatthew G Knepley     }
34619566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
34629566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
34639566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
34649566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3465e6ccafaeSMatthew G Knepley   }
34663982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
34673982b651SMatthew G. Knepley   {
34683982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
34693982b651SMatthew G. Knepley     IS                 subpIS;
34703982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
34715033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
34725033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3473dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
34745033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
34755033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
34763982b651SMatthew G. Knepley 
34779566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
34785033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
34799566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
34809566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
34819566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
34829566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
34839566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
34843982b651SMatthew G. Knepley     if (subpIS) {
3485dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3486dd05d810SMatthew G. Knepley 
34879566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
34889566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3489dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3490dd05d810SMatthew G. Knepley       if (!sorted) {
3491dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3492dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3493dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3494dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3495dd05d810SMatthew G. Knepley       }
34963982b651SMatthew G. Knepley     }
34979566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
34983982b651SMatthew G. Knepley     if (numRoots >= 0) {
34995033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
35005033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
35019566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
35023982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
35033982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
35043982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
35053982b651SMatthew G. Knepley       }
35063982b651SMatthew G. Knepley       /* Set subleaves */
35073982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
35083982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3509dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
35103982b651SMatthew G. Knepley 
35113982b651SMatthew G. Knepley         if (subpoint < 0) continue;
35123982b651SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
35133982b651SMatthew G. Knepley         newLocalPoints[point - pStart].index = subpoint;
35143982b651SMatthew G. Knepley         ++numSubleaves;
35153982b651SMatthew G. Knepley       }
35163982b651SMatthew G. Knepley       /* Must put in owned subpoints */
35173982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
35183982b651SMatthew G. Knepley         newOwners[p - pStart].rank  = -3;
35193982b651SMatthew G. Knepley         newOwners[p - pStart].index = -3;
35203982b651SMatthew G. Knepley       }
3521d9384f27SMatthew G. Knepley       for (p = 0; p < numSubpoints; ++p) {
35225033f954SMatthew G. Knepley         /* Hold on to currently owned points */
35235033f954SMatthew G. Knepley         if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
35245033f954SMatthew G. Knepley         else newOwners[subpoints[p] - pStart].rank = rank;
3525d9384f27SMatthew G. Knepley         newOwners[subpoints[p] - pStart].index = p;
35263982b651SMatthew G. Knepley       }
35279566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
35289566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
35299371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
35309371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
35319566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
35329566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
35339566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
35349566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
35355033f954SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
35363982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3537dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
35383982b651SMatthew G. Knepley 
35393982b651SMatthew G. Knepley         if (subpoint < 0) continue;
35409371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
35419371c9d4SSatish Balay           ++ll;
35429371c9d4SSatish Balay           continue;
35439371c9d4SSatish Balay         }
35443982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
35453982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
35463982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
354763a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
354863a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
35493982b651SMatthew G. Knepley         ++sl;
35503982b651SMatthew G. Knepley       }
35511dca8a05SBarry 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);
35529566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
35539566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
35543982b651SMatthew G. Knepley     }
355548a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3556dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
35573982b651SMatthew G. Knepley   }
3558212103e5SMatthew Knepley   /* Filter labels */
35599566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3560efa14ee0SMatthew G Knepley   /* Cleanup */
3561dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
35629566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
35639566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3564e6ccafaeSMatthew G Knepley   }
35659566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
3566e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3567e6ccafaeSMatthew G Knepley }
3568e6ccafaeSMatthew G Knepley 
3569d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
3570d71ae5a4SJacob Faibussowitsch {
35713982b651SMatthew G. Knepley   PetscFunctionBegin;
35729566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm));
35733982b651SMatthew G. Knepley   PetscFunctionReturn(0);
35743982b651SMatthew G. Knepley }
35753982b651SMatthew G. Knepley 
3576d0fa310fSMatthew G. Knepley /*@
3577e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3578e6ccafaeSMatthew G Knepley 
3579e6ccafaeSMatthew G Knepley   Input Parameters:
3580e6ccafaeSMatthew G Knepley + dm           - The original mesh
3581158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3582158acfadSMatthew G. Knepley . value        - The label value to use
3583158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3584e6ccafaeSMatthew G Knepley 
3585e6ccafaeSMatthew G Knepley   Output Parameter:
3586e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3587e6ccafaeSMatthew G Knepley 
3588e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3589e6ccafaeSMatthew G Knepley 
3590e6ccafaeSMatthew G Knepley   Level: developer
3591e6ccafaeSMatthew G Knepley 
3592db781477SPatrick Sanan .seealso: `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3593830e53efSMatthew G. Knepley @*/
3594d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3595d71ae5a4SJacob Faibussowitsch {
3596827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3597827c4036SVaclav Hapla   PetscInt               dim, cdim;
3598e6ccafaeSMatthew G Knepley 
3599e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3600e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3601064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 5);
36029566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
36039566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
36049566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
36059566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
36069566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
36079566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
36089566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
360908401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3610827c4036SVaclav Hapla   if (interpolated) {
36119566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3612e6ccafaeSMatthew G Knepley   } else {
36139566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3614e6ccafaeSMatthew G Knepley   }
36155de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3616e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3617e6ccafaeSMatthew G Knepley }
3618e6ccafaeSMatthew G Knepley 
3619d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3620d71ae5a4SJacob Faibussowitsch {
3621766ab985SMatthew G. Knepley   MPI_Comm        comm;
3622766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3623766ab985SMatthew G. Knepley   IS              subvertexIS;
3624766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3625fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3626766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3627412e9a14SMatthew G. Knepley   PetscInt        c, f;
3628766ab985SMatthew G. Knepley 
3629766ab985SMatthew G. Knepley   PetscFunctionBegin;
36309566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3631766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
36329566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
36339566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
36349566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
36359566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3636766ab985SMatthew G. Knepley   /* Setup chart */
36379566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
36389566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
36399566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
36409566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3641766ab985SMatthew G. Knepley   /* Set cone sizes */
3642766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3643766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3644766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
36459566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
36469566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
364748a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
364848a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
36499566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3650766ab985SMatthew G. Knepley   /* Create face cones */
36519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
36529566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3653766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3654766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3655766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
365687feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3657064cae4fSPierre Jolivet     PetscBool       isHybrid = PETSC_FALSE;
365887feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3659766ab985SMatthew G. Knepley 
36609566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3661412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
36629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
366387feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
36649566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
366587feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
366687feddfdSMatthew G. Knepley     }
36679566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
36689566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
36699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
367027234c99SMatthew G. Knepley     /* Not true in parallel
367108401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
367287feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
367387feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3674064cae4fSPierre Jolivet       PetscBool isHybrid = PETSC_FALSE;
3675766ab985SMatthew G. Knepley 
36769566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3677412e9a14SMatthew G. Knepley       if (isHybrid) continue;
367887feddfdSMatthew G. Knepley       /* I know this is a crap search */
367987feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
368087feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3681766ab985SMatthew G. Knepley       }
368263a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
36839566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3684766ab985SMatthew G. Knepley     }
36859566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
368687feddfdSMatthew G. Knepley     ++newFacePoint;
3687766ab985SMatthew G. Knepley   }
36889566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
36899566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
36909566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3691766ab985SMatthew G. Knepley   /* Build coordinates */
3692766ab985SMatthew G. Knepley   {
3693766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3694766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3695766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3696c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
369724640c55SToby Isaac     const char  *name;
3698766ab985SMatthew G. Knepley 
36999566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
37009566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
37019566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
37029566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
37039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
37049566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
37059566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
37069566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3707766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3708766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3709766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3710766ab985SMatthew G. Knepley       PetscInt       dof;
3711766ab985SMatthew G. Knepley 
37129566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
37139566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
37149566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3715766ab985SMatthew G. Knepley     }
37169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
37179566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
37189566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
37199566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
37209566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
37219566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
37229566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
37239566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
37249566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
37259566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3726766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3727766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3728766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3729766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3730766ab985SMatthew G. Knepley 
37319566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
37329566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
37339566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
37349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
373563a3b9bcSJacob 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);
3736766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3737766ab985SMatthew G. Knepley     }
37389566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
37399566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
37409566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
37419566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3742766ab985SMatthew G. Knepley   }
3743aca35d17SMatthew G. Knepley   /* Build SF */
3744aca35d17SMatthew G. Knepley   CHKMEMQ;
3745aca35d17SMatthew G. Knepley   {
3746aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3747aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3748bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3749aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3750bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
375149c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3752bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3753aca35d17SMatthew G. Knepley 
37549566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
37559566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
37569566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
37579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
37589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
37599566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
3760aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3761aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
37629566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
3763bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
3764bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3765bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3766bdcf2095SMatthew G. Knepley       }
37679e0823b2SMatthew G. Knepley       /* Set subleaves */
3768aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3769aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3770bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3771aca35d17SMatthew G. Knepley 
377263a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
3773bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3774bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
3775bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
3776bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3777aca35d17SMatthew G. Knepley       }
37789e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
37799e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
37809e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
37819e0823b2SMatthew G. Knepley 
37829e0823b2SMatthew G. Knepley         if (subPoint < 0) {
37839e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
37849e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
37859e0823b2SMatthew G. Knepley         } else {
37869e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
37879e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
37889e0823b2SMatthew G. Knepley         }
3789bdcf2095SMatthew G. Knepley       }
37909566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
37919566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
37929566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
37939566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
37949566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
37959566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
379649c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3797bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3798bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3799aca35d17SMatthew G. Knepley 
3800aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
38019371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
38029371c9d4SSatish Balay           ++ll;
38039371c9d4SSatish Balay           continue;
38049371c9d4SSatish Balay         }
3805aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3806bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3807bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
380863a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
380963a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
3810aca35d17SMatthew G. Knepley         ++sl;
3811aca35d17SMatthew G. Knepley       }
38129566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
38131dca8a05SBarry 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);
38149566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
3815aca35d17SMatthew G. Knepley     }
3816aca35d17SMatthew G. Knepley   }
3817aca35d17SMatthew G. Knepley   CHKMEMQ;
3818766ab985SMatthew G. Knepley   /* Cleanup */
38199566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
38209566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
38219566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
3822766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3823766ab985SMatthew G. Knepley }
3824766ab985SMatthew G. Knepley 
3825d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3826d71ae5a4SJacob Faibussowitsch {
38273982b651SMatthew G. Knepley   DMLabel label = NULL;
3828766ab985SMatthew G. Knepley 
3829766ab985SMatthew G. Knepley   PetscFunctionBegin;
38309566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
38319566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm));
3832766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3833766ab985SMatthew G. Knepley }
3834766ab985SMatthew G. Knepley 
3835c08575a3SMatthew G. Knepley /*@C
38367d2fefb6SMatthew G. Knepley   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.
3837766ab985SMatthew G. Knepley 
3838766ab985SMatthew G. Knepley   Input Parameters:
3839766ab985SMatthew G. Knepley + dm          - The original mesh
384027c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
38417afc1a8bSJed Brown . label       - A label name, or NULL
384227c04023SMatthew G. Knepley - value  - A label value
3843766ab985SMatthew G. Knepley 
3844766ab985SMatthew G. Knepley   Output Parameter:
3845766ab985SMatthew G. Knepley . subdm - The surface mesh
3846766ab985SMatthew G. Knepley 
3847766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3848766ab985SMatthew G. Knepley 
3849766ab985SMatthew G. Knepley   Level: developer
3850766ab985SMatthew G. Knepley 
3851db781477SPatrick Sanan .seealso: `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
3852c08575a3SMatthew G. Knepley @*/
3853d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3854d71ae5a4SJacob Faibussowitsch {
3855c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
3856766ab985SMatthew G. Knepley 
3857766ab985SMatthew G. Knepley   PetscFunctionBegin;
3858766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
385927c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
38609566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
38619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
38629566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
38639566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
38649566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
38659566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
38669566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
3867766ab985SMatthew G. Knepley   if (depth == dim) {
38689566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
3869766ab985SMatthew G. Knepley   } else {
38709566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
3871e6ccafaeSMatthew G Knepley   }
38725de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3873e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3874e6ccafaeSMatthew G Knepley }
3875e6ccafaeSMatthew G Knepley 
3876bec263e5SMatthew G. Knepley /*@
3877bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3878bec263e5SMatthew G. Knepley 
3879bec263e5SMatthew G. Knepley   Input Parameters:
3880bec263e5SMatthew G. Knepley + dm        - The original mesh
3881bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3882bec263e5SMatthew G. Knepley - value     - The label value to use
3883bec263e5SMatthew G. Knepley 
3884bec263e5SMatthew G. Knepley   Output Parameter:
3885bec263e5SMatthew G. Knepley . subdm - The new mesh
3886bec263e5SMatthew G. Knepley 
3887dd05d810SMatthew G. Knepley   Notes:
3888dd05d810SMatthew G. Knepley   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
3889bec263e5SMatthew G. Knepley 
3890bec263e5SMatthew G. Knepley   Level: developer
3891bec263e5SMatthew G. Knepley 
3892dd05d810SMatthew G. Knepley .seealso: `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
3893bec263e5SMatthew G. Knepley @*/
3894d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3895d71ae5a4SJacob Faibussowitsch {
3896b47cd10cSMatthew G. Knepley   PetscInt dim, overlap;
3897bec263e5SMatthew G. Knepley 
3898bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3899bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3900064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 4);
39019566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
39029566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
39039566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
3904bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
39059566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm));
39065de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3907b47cd10cSMatthew G. Knepley   // It is possible to obtain a surface mesh where some faces are in SF
3908b47cd10cSMatthew G. Knepley   //   We should either mark the mesh as having an overlap, or delete these from the SF
3909b47cd10cSMatthew G. Knepley   PetscCall(DMPlexGetOverlap(dm, &overlap));
3910b47cd10cSMatthew G. Knepley   if (!overlap) {
3911b47cd10cSMatthew G. Knepley     PetscSF         sf;
3912b47cd10cSMatthew G. Knepley     const PetscInt *leaves;
3913b47cd10cSMatthew G. Knepley     PetscInt        cStart, cEnd, Nl;
3914b47cd10cSMatthew G. Knepley     PetscBool       hasSubcell = PETSC_FALSE, ghasSubcell;
3915b47cd10cSMatthew G. Knepley 
3916b47cd10cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd));
3917b47cd10cSMatthew G. Knepley     PetscCall(DMGetPointSF(*subdm, &sf));
3918b47cd10cSMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
3919b47cd10cSMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
3920b47cd10cSMatthew G. Knepley       const PetscInt point = leaves ? leaves[l] : l;
3921b47cd10cSMatthew G. Knepley 
3922b47cd10cSMatthew G. Knepley       if (point >= cStart && point < cEnd) {
3923b47cd10cSMatthew G. Knepley         hasSubcell = PETSC_TRUE;
3924b47cd10cSMatthew G. Knepley         break;
3925b47cd10cSMatthew G. Knepley       }
3926b47cd10cSMatthew G. Knepley     }
3927b47cd10cSMatthew G. Knepley     PetscCall(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
3928b47cd10cSMatthew G. Knepley     if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1));
3929b47cd10cSMatthew G. Knepley   }
3930bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3931bec263e5SMatthew G. Knepley }
3932bec263e5SMatthew G. Knepley 
393364beef6dSMatthew G. Knepley /*@
393464beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
393564beef6dSMatthew G. Knepley 
393664beef6dSMatthew G. Knepley   Input Parameter:
393764beef6dSMatthew G. Knepley . dm - The submesh DM
393864beef6dSMatthew G. Knepley 
393964beef6dSMatthew G. Knepley   Output Parameter:
394064beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
394164beef6dSMatthew G. Knepley 
394264beef6dSMatthew G. Knepley   Level: developer
394364beef6dSMatthew G. Knepley 
3944db781477SPatrick Sanan .seealso: `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
394564beef6dSMatthew G. Knepley @*/
3946d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3947d71ae5a4SJacob Faibussowitsch {
3948e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3949e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3950e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
395165663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
3952e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3953e6ccafaeSMatthew G Knepley }
3954e6ccafaeSMatthew G Knepley 
3955c08575a3SMatthew G. Knepley /*@
3956c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3957c08575a3SMatthew G. Knepley 
3958c08575a3SMatthew G. Knepley   Input Parameters:
3959c08575a3SMatthew G. Knepley + dm - The submesh DM
3960c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3961c08575a3SMatthew G. Knepley 
3962c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3963c08575a3SMatthew G. Knepley 
3964c08575a3SMatthew G. Knepley   Level: developer
3965c08575a3SMatthew G. Knepley 
3966db781477SPatrick Sanan .seealso: `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
3967c08575a3SMatthew G. Knepley @*/
3968d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3969d71ae5a4SJacob Faibussowitsch {
3970e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
3971285d324eSMatthew G. Knepley   DMLabel  tmp;
3972e6ccafaeSMatthew G Knepley 
3973e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3974e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3975285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
3976e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
39779566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
39789566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
3979e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3980e6ccafaeSMatthew G Knepley }
3981e6ccafaeSMatthew G Knepley 
3982d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
3983d71ae5a4SJacob Faibussowitsch {
398497d8846cSMatthew Knepley   DMLabel  spmap;
398597d8846cSMatthew Knepley   PetscInt depth, d;
398697d8846cSMatthew Knepley 
398797d8846cSMatthew Knepley   PetscFunctionBegin;
39889566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
39899566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
399097d8846cSMatthew Knepley   if (spmap && depth >= 0) {
399197d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
399297d8846cSMatthew Knepley     PetscInt *points, *depths;
399397d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
399497d8846cSMatthew Knepley 
39959566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
399663a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
39979566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
39989566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
399997d8846cSMatthew Knepley     depths[0] = depth;
400097d8846cSMatthew Knepley     depths[1] = 0;
4001ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
400297d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
400397d8846cSMatthew Knepley       const PetscInt dep = depths[d];
400497d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
400597d8846cSMatthew Knepley 
40069566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
40079566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
400897d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
400963a3b9bcSJacob 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);
401097d8846cSMatthew Knepley       } else {
401197d8846cSMatthew Knepley         if (!n) {
401297d8846cSMatthew Knepley           if (d == 0) {
401397d8846cSMatthew Knepley             /* Missing cells */
401497d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
401597d8846cSMatthew Knepley           } else {
401697d8846cSMatthew Knepley             /* Missing faces */
401797d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
401897d8846cSMatthew Knepley           }
401997d8846cSMatthew Knepley         }
402097d8846cSMatthew Knepley       }
402197d8846cSMatthew Knepley       if (n) {
402297d8846cSMatthew Knepley         IS              is;
402397d8846cSMatthew Knepley         const PetscInt *opoints;
402497d8846cSMatthew Knepley 
40259566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
40269566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
402797d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
40289566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
40299566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
403097d8846cSMatthew Knepley       }
403197d8846cSMatthew Knepley     }
40329566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
403363a3b9bcSJacob 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);
40349566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
40359566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
403697d8846cSMatthew Knepley   }
403797d8846cSMatthew Knepley   PetscFunctionReturn(0);
403897d8846cSMatthew Knepley }
403997d8846cSMatthew Knepley 
404064beef6dSMatthew G. Knepley /*@
404197d8846cSMatthew Knepley   DMPlexGetSubpointIS - Returns an IS covering the entire subdm chart with the original points as data
4042e6ccafaeSMatthew G Knepley 
4043e6ccafaeSMatthew G Knepley   Input Parameter:
4044e6ccafaeSMatthew G Knepley . dm - The submesh DM
4045e6ccafaeSMatthew G Knepley 
4046e6ccafaeSMatthew G Knepley   Output Parameter:
40470298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
4048e6ccafaeSMatthew G Knepley 
40493982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
405064beef6dSMatthew G. Knepley 
405164beef6dSMatthew G. Knepley   Level: developer
405264beef6dSMatthew G. Knepley 
4053db781477SPatrick Sanan .seealso: `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
405464beef6dSMatthew G. Knepley @*/
4055d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
4056d71ae5a4SJacob Faibussowitsch {
405797d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
405897d8846cSMatthew Knepley   DMLabel          spmap;
405997d8846cSMatthew Knepley   PetscObjectState state;
4060e6ccafaeSMatthew G Knepley 
4061e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
4062e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4063e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
40649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
40659566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
40669566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
406797d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
4068e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
4069e6ccafaeSMatthew G Knepley }
4070559a1558SMatthew G. Knepley 
4071559a1558SMatthew G. Knepley /*@
4072a6e0b375SMatthew G. Knepley   DMGetEnclosureRelation - Get the relationship between dmA and dmB
4073559a1558SMatthew G. Knepley 
4074559a1558SMatthew G. Knepley   Input Parameters:
4075a6e0b375SMatthew G. Knepley + dmA - The first DM
4076a6e0b375SMatthew G. Knepley - dmB - The second DM
4077559a1558SMatthew G. Knepley 
4078559a1558SMatthew G. Knepley   Output Parameter:
4079a6e0b375SMatthew G. Knepley . rel - The relation of dmA to dmB
4080559a1558SMatthew G. Knepley 
4081a6e0b375SMatthew G. Knepley   Level: intermediate
4082559a1558SMatthew G. Knepley 
4083db781477SPatrick Sanan .seealso: `DMGetEnclosurePoint()`
4084559a1558SMatthew G. Knepley @*/
4085d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4086d71ae5a4SJacob Faibussowitsch {
4087a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
4088559a1558SMatthew G. Knepley   DMLabel  spmap;
4089a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4090559a1558SMatthew G. Knepley 
409144171101SMatthew G. Knepley   PetscFunctionBegin;
4092a6e0b375SMatthew G. Knepley   PetscValidPointer(rel, 3);
4093a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
4094a6e0b375SMatthew G. Knepley   if (!dmA || !dmB) PetscFunctionReturn(0);
4095a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4096064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
40979371c9d4SSatish Balay   if (dmA == dmB) {
40989371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
40999371c9d4SSatish Balay     PetscFunctionReturn(0);
41009371c9d4SSatish Balay   }
41019566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
41029566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
41039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
41049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4105a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4106a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4107a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
4108a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4109a6e0b375SMatthew G. Knepley     goto end;
4110559a1558SMatthew G. Knepley   }
4111a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4112a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4113a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4114a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
41159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4116a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4117a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4118a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4119a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4120a6e0b375SMatthew G. Knepley   } else {
4121a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4122a6e0b375SMatthew G. Knepley   }
4123a6e0b375SMatthew G. Knepley end:
41249566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
41259566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
4126559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
4127559a1558SMatthew G. Knepley }
412844171101SMatthew G. Knepley 
412944171101SMatthew G. Knepley /*@
4130a6e0b375SMatthew G. Knepley   DMGetEnclosurePoint - Get the point pA in dmA which corresponds to the point pB in dmB
413144171101SMatthew G. Knepley 
413244171101SMatthew G. Knepley   Input Parameters:
4133a6e0b375SMatthew G. Knepley + dmA   - The first DM
4134a6e0b375SMatthew G. Knepley . dmB   - The second DM
4135a6e0b375SMatthew G. Knepley . etype - The type of enclosure relation that dmA has to dmB
4136a6e0b375SMatthew G. Knepley - pB    - A point of dmB
413744171101SMatthew G. Knepley 
413844171101SMatthew G. Knepley   Output Parameter:
4139a6e0b375SMatthew G. Knepley . pA    - The corresponding point of dmA
414044171101SMatthew G. Knepley 
4141a6e0b375SMatthew G. Knepley   Level: intermediate
414244171101SMatthew G. Knepley 
4143db781477SPatrick Sanan .seealso: `DMGetEnclosureRelation()`
414444171101SMatthew G. Knepley @*/
4145d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
4146d71ae5a4SJacob Faibussowitsch {
4147a6e0b375SMatthew G. Knepley   DM              sdm;
4148a6e0b375SMatthew G. Knepley   IS              subpointIS;
4149a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4150a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
415144171101SMatthew G. Knepley 
415244171101SMatthew G. Knepley   PetscFunctionBegin;
4153a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4154a6e0b375SMatthew G. Knepley   switch (etype) {
4155a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4156a6e0b375SMatthew G. Knepley     sdm = dmB;
41579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
41589566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4159a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
41609566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4161a6e0b375SMatthew G. Knepley     break;
4162a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4163a6e0b375SMatthew G. Knepley     sdm = dmA;
41649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
41659566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
41669566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
41679566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4168a6e0b375SMatthew G. Knepley     if (*pA < 0) {
41699566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
41709566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
417163a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4172a6e0b375SMatthew G. Knepley     }
41739566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4174a6e0b375SMatthew G. Knepley     break;
4175a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
4176d71ae5a4SJacob Faibussowitsch   case DM_ENC_NONE:
4177d71ae5a4SJacob Faibussowitsch     *pA = pB;
4178d71ae5a4SJacob Faibussowitsch     break;
41799371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4180a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
418144171101SMatthew G. Knepley 
41829566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
41839566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
41849371c9d4SSatish Balay   } break;
4185d71ae5a4SJacob Faibussowitsch   default:
4186d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
418744171101SMatthew G. Knepley   }
418844171101SMatthew G. Knepley   PetscFunctionReturn(0);
418944171101SMatthew G. Knepley }
4190