xref: /petsc/src/dm/impls/plex/plexinterpolate.c (revision 5ebdaad1439ac5ccee6e774361c25dbf16b85bf4)
1 #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2 #include <petsc/private/hashmapi.h>
3 #include <petsc/private/hashmapij.h>
4 
5 /* HashIJKL */
6 
7 #include <petsc/private/hashmap.h>
8 
9 typedef struct _PetscHashIJKLKey { PetscInt i, j, k, l; } PetscHashIJKLKey;
10 
11 #define PetscHashIJKLKeyHash(key) \
12   PetscHashCombine(PetscHashCombine(PetscHashInt((key).i),PetscHashInt((key).j)), \
13                    PetscHashCombine(PetscHashInt((key).k),PetscHashInt((key).l)))
14 
15 #define PetscHashIJKLKeyEqual(k1,k2) \
16   (((k1).i==(k2).i) ? ((k1).j==(k2).j) ? ((k1).k==(k2).k) ? ((k1).l==(k2).l) : 0 : 0 : 0)
17 
18 PETSC_HASH_MAP(HashIJKL, PetscHashIJKLKey, PetscInt, PetscHashIJKLKeyHash, PetscHashIJKLKeyEqual, -1)
19 
20 
21 /*
22   DMPlexGetFaces_Internal - Gets groups of vertices that correspond to faces for the given cell
23   This assumes that the mesh is not interpolated from the depth of point p to the vertices
24 */
25 PetscErrorCode DMPlexGetFaces_Internal(DM dm, PetscInt dim, PetscInt p, PetscInt *numFaces, PetscInt *faceSize, const PetscInt *faces[])
26 {
27   const PetscInt *cone = NULL;
28   PetscInt        coneSize;
29   PetscErrorCode  ierr;
30 
31   PetscFunctionBegin;
32   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
34   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
35   ierr = DMPlexGetRawFaces_Internal(dm, dim, coneSize, cone, numFaces, faceSize, faces);CHKERRQ(ierr);
36   PetscFunctionReturn(0);
37 }
38 
39 /*
40   DMPlexRestoreFaces_Internal - Restores the array
41 */
42 PetscErrorCode DMPlexRestoreFaces_Internal(DM dm, PetscInt dim, PetscInt p, PetscInt *numFaces, PetscInt *faceSize, const PetscInt *faces[])
43 {
44   PetscErrorCode  ierr;
45 
46   PetscFunctionBegin;
47   if (faces) { ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void *) faces);CHKERRQ(ierr); }
48   PetscFunctionReturn(0);
49 }
50 
51 /*
52   DMPlexGetRawFaces_Internal - Gets groups of vertices that correspond to faces for the given cone
53 */
54 PetscErrorCode DMPlexGetRawFaces_Internal(DM dm, PetscInt dim, PetscInt coneSize, const PetscInt cone[], PetscInt *numFaces, PetscInt *faceSize, const PetscInt *faces[])
55 {
56   PetscInt       *facesTmp;
57   PetscInt        maxConeSize, maxSupportSize;
58   PetscErrorCode  ierr;
59 
60   PetscFunctionBegin;
61   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62   if (faces && coneSize) PetscValidIntPointer(cone,4);
63   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
64   if (faces) {ierr = DMGetWorkArray(dm, PetscSqr(PetscMax(maxConeSize, maxSupportSize)), MPIU_INT, &facesTmp);CHKERRQ(ierr);}
65   switch (dim) {
66   case 1:
67     switch (coneSize) {
68     case 2:
69       if (faces) {
70         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
71         *faces = facesTmp;
72       }
73       if (numFaces) *numFaces = 2;
74       if (faceSize) *faceSize = 1;
75       break;
76     default:
77       SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
78     }
79     break;
80   case 2:
81     switch (coneSize) {
82     case 3:
83       if (faces) {
84         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
85         facesTmp[2] = cone[1]; facesTmp[3] = cone[2];
86         facesTmp[4] = cone[2]; facesTmp[5] = cone[0];
87         *faces = facesTmp;
88       }
89       if (numFaces) *numFaces = 3;
90       if (faceSize) *faceSize = 2;
91       break;
92     case 4:
93       /* Vertices follow right hand rule */
94       if (faces) {
95         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
96         facesTmp[2] = cone[1]; facesTmp[3] = cone[2];
97         facesTmp[4] = cone[2]; facesTmp[5] = cone[3];
98         facesTmp[6] = cone[3]; facesTmp[7] = cone[0];
99         *faces = facesTmp;
100       }
101       if (numFaces) *numFaces = 4;
102       if (faceSize) *faceSize = 2;
103       break;
104     default:
105       SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
106     }
107     break;
108   case 3:
109     switch (coneSize) {
110     case 3:
111       if (faces) {
112         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
113         facesTmp[2] = cone[1]; facesTmp[3] = cone[2];
114         facesTmp[4] = cone[2]; facesTmp[5] = cone[0];
115         *faces = facesTmp;
116       }
117       if (numFaces) *numFaces = 3;
118       if (faceSize) *faceSize = 2;
119       break;
120     case 4:
121       /* Vertices of first face follow right hand rule and normal points away from last vertex */
122       if (faces) {
123         facesTmp[0] = cone[0]; facesTmp[1]  = cone[1]; facesTmp[2]  = cone[2];
124         facesTmp[3] = cone[0]; facesTmp[4]  = cone[3]; facesTmp[5]  = cone[1];
125         facesTmp[6] = cone[0]; facesTmp[7]  = cone[2]; facesTmp[8]  = cone[3];
126         facesTmp[9] = cone[2]; facesTmp[10] = cone[1]; facesTmp[11] = cone[3];
127         *faces = facesTmp;
128       }
129       if (numFaces) *numFaces = 4;
130       if (faceSize) *faceSize = 3;
131       break;
132     case 8:
133       /*  7--------6
134          /|       /|
135         / |      / |
136        4--------5  |
137        |  |     |  |
138        |  |     |  |
139        |  1--------2
140        | /      | /
141        |/       |/
142        0--------3
143        */
144       if (faces) {
145         facesTmp[0]  = cone[0]; facesTmp[1]  = cone[1]; facesTmp[2]  = cone[2]; facesTmp[3]  = cone[3]; /* Bottom */
146         facesTmp[4]  = cone[4]; facesTmp[5]  = cone[5]; facesTmp[6]  = cone[6]; facesTmp[7]  = cone[7]; /* Top */
147         facesTmp[8]  = cone[0]; facesTmp[9]  = cone[3]; facesTmp[10] = cone[5]; facesTmp[11] = cone[4]; /* Front */
148         facesTmp[12] = cone[2]; facesTmp[13] = cone[1]; facesTmp[14] = cone[7]; facesTmp[15] = cone[6]; /* Back */
149         facesTmp[16] = cone[3]; facesTmp[17] = cone[2]; facesTmp[18] = cone[6]; facesTmp[19] = cone[5]; /* Right */
150         facesTmp[20] = cone[0]; facesTmp[21] = cone[4]; facesTmp[22] = cone[7]; facesTmp[23] = cone[1]; /* Left */
151         *faces = facesTmp;
152       }
153       if (numFaces) *numFaces = 6;
154       if (faceSize) *faceSize = 4;
155       break;
156     default:
157       SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
158     }
159     break;
160   default:
161     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim);
162   }
163   PetscFunctionReturn(0);
164 }
165 
166 /*
167   DMPlexGetRawFacesHybrid_Internal - Gets groups of vertices that correspond to faces for the given cone using hybrid ordering (prisms)
168 */
169 static PetscErrorCode DMPlexGetRawFacesHybrid_Internal(DM dm, PetscInt dim, PetscInt coneSize, const PetscInt cone[], PetscInt *numFaces, PetscInt *numFacesNotH, PetscInt *faceSize, const PetscInt *faces[])
170 {
171   PetscInt       *facesTmp;
172   PetscInt        maxConeSize, maxSupportSize;
173   PetscErrorCode  ierr;
174 
175   PetscFunctionBegin;
176   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
177   if (faces && coneSize) PetscValidIntPointer(cone,4);
178   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
179   if (faces) {ierr = DMGetWorkArray(dm, PetscSqr(PetscMax(maxConeSize, maxSupportSize)), MPIU_INT, &facesTmp);CHKERRQ(ierr);}
180   switch (dim) {
181   case 1:
182     switch (coneSize) {
183     case 2:
184       if (faces) {
185         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
186         *faces = facesTmp;
187       }
188       if (numFaces)     *numFaces = 2;
189       if (numFacesNotH) *numFacesNotH = 2;
190       if (faceSize)     *faceSize = 1;
191       break;
192     default:
193       SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
194     }
195     break;
196   case 2:
197     switch (coneSize) {
198     case 4:
199       if (faces) {
200         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
201         facesTmp[2] = cone[2]; facesTmp[3] = cone[3];
202         facesTmp[4] = cone[0]; facesTmp[5] = cone[2];
203         facesTmp[6] = cone[1]; facesTmp[7] = cone[3];
204         *faces = facesTmp;
205       }
206       if (numFaces)     *numFaces = 4;
207       if (numFacesNotH) *numFacesNotH = 2;
208       if (faceSize)     *faceSize = 2;
209       break;
210     default:
211       SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
212     }
213     break;
214   case 3:
215     switch (coneSize) {
216     case 6: /* triangular prism */
217       if (faces) {
218         facesTmp[0]  = cone[0]; facesTmp[1]  = cone[1]; facesTmp[2]  = cone[2]; facesTmp[3]  = -1;      /* Bottom */
219         facesTmp[4]  = cone[3]; facesTmp[5]  = cone[4]; facesTmp[6]  = cone[5]; facesTmp[7]  = -1;      /* Top */
220         facesTmp[8]  = cone[0]; facesTmp[9]  = cone[1]; facesTmp[10] = cone[3]; facesTmp[11] = cone[4]; /* Back left */
221         facesTmp[12] = cone[1]; facesTmp[13] = cone[2]; facesTmp[14] = cone[4]; facesTmp[15] = cone[5]; /* Back right */
222         facesTmp[16] = cone[2]; facesTmp[17] = cone[0]; facesTmp[18] = cone[5]; facesTmp[19] = cone[3]; /* Front */
223         *faces = facesTmp;
224       }
225       if (numFaces)     *numFaces = 5;
226       if (numFacesNotH) *numFacesNotH = 2;
227       if (faceSize)     *faceSize = -4;
228       break;
229     default:
230       SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
231     }
232     break;
233   default:
234     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim);
235   }
236   PetscFunctionReturn(0);
237 }
238 
239 static PetscErrorCode DMPlexRestoreRawFacesHybrid_Internal(DM dm, PetscInt dim, PetscInt coneSize, const PetscInt cone[], PetscInt *numFaces, PetscInt *numFacesNotH, PetscInt *faceSize, const PetscInt *faces[])
240 {
241   PetscErrorCode  ierr;
242 
243   PetscFunctionBegin;
244   if (faces) { ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void *) faces);CHKERRQ(ierr); }
245   PetscFunctionReturn(0);
246 }
247 
248 static PetscErrorCode DMPlexGetFacesHybrid_Internal(DM dm, PetscInt dim, PetscInt p, PetscInt *numFaces, PetscInt *numFacesNotH, PetscInt *faceSize, const PetscInt *faces[])
249 {
250   const PetscInt *cone = NULL;
251   PetscInt        coneSize;
252   PetscErrorCode  ierr;
253 
254   PetscFunctionBegin;
255   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
256   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
257   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
258   ierr = DMPlexGetRawFacesHybrid_Internal(dm, dim, coneSize, cone, numFaces, numFacesNotH, faceSize, faces);CHKERRQ(ierr);
259   PetscFunctionReturn(0);
260 }
261 
262 /* This interpolates faces for cells at some stratum */
263 static PetscErrorCode DMPlexInterpolateFaces_Internal(DM dm, PetscInt cellDepth, DM idm)
264 {
265   DMLabel        subpointMap;
266   PetscHashIJKL  faceTable;
267   PetscInt      *pStart, *pEnd;
268   PetscInt       cellDim, depth, faceDepth = cellDepth, numPoints = 0, faceSizeAll = 0, face, c, d;
269   PetscInt       coneSizeH = 0, faceSizeAllH = 0, faceSizeAllT = 0, numCellFacesH = 0, faceT = 0, faceH, pMax = -1, dim, outerloop;
270   PetscInt       cMax, fMax, eMax, vMax;
271   PetscErrorCode ierr;
272 
273   PetscFunctionBegin;
274   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
275   /* HACK: I need a better way to determine face dimension, or an alternative to GetFaces() */
276   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
277   if (subpointMap) ++cellDim;
278   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
279   ++depth;
280   ++cellDepth;
281   cellDim -= depth - cellDepth;
282   ierr = PetscMalloc2(depth+1,&pStart,depth+1,&pEnd);CHKERRQ(ierr);
283   for (d = depth-1; d >= faceDepth; --d) {
284     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d+1], &pEnd[d+1]);CHKERRQ(ierr);
285   }
286   ierr = DMPlexGetDepthStratum(dm, -1, NULL, &pStart[faceDepth]);CHKERRQ(ierr);
287   pEnd[faceDepth] = pStart[faceDepth];
288   for (d = faceDepth-1; d >= 0; --d) {
289     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
290   }
291   cMax = fMax = eMax = vMax = PETSC_DETERMINE;
292   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
293   if (cellDim == dim) {
294     ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
295     pMax = cMax;
296   } else if (cellDim == dim -1) {
297     ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, NULL, NULL);CHKERRQ(ierr);
298     pMax = fMax;
299   }
300   pMax = pMax < 0 ? pEnd[cellDepth] : pMax;
301   if (pMax < pEnd[cellDepth]) {
302     const PetscInt *cellFaces, *cone;
303     PetscInt        numCellFacesT, faceSize, cf;
304 
305     /* First get normal cell face size (we now allow hybrid cells to meet normal cells on either hybrid or normal faces */
306     if (pStart[cellDepth] < pMax) {ierr = DMPlexGetFaces_Internal(dm, cellDim, pStart[cellDepth], NULL, &faceSizeAll, NULL);CHKERRQ(ierr);}
307 
308     ierr = DMPlexGetConeSize(dm, pMax, &coneSizeH);CHKERRQ(ierr);
309     ierr = DMPlexGetCone(dm, pMax, &cone);CHKERRQ(ierr);
310     ierr = DMPlexGetRawFacesHybrid_Internal(dm, cellDim, coneSizeH, cone, &numCellFacesH, &numCellFacesT, &faceSize, &cellFaces);CHKERRQ(ierr);
311     if (faceSize < 0) {
312       PetscInt *sizes, minv, maxv;
313 
314       /* count vertices of hybrid and non-hybrid faces */
315       ierr = PetscCalloc1(numCellFacesH, &sizes);CHKERRQ(ierr);
316       for (cf = 0; cf < numCellFacesT; ++cf) { /* These are the non-hybrid faces */
317         const PetscInt *cellFace = &cellFaces[-cf*faceSize];
318         PetscInt       f;
319 
320         for (f = 0; f < -faceSize; ++f) sizes[cf] += (cellFace[f] >= 0 ? 1 : 0);
321       }
322       ierr = PetscSortInt(numCellFacesT, sizes);CHKERRQ(ierr);
323       minv = sizes[0];
324       maxv = sizes[PetscMax(numCellFacesT-1, 0)];
325       if (minv != maxv) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP, "Different number of vertices for non-hybrid face %D != %D", minv, maxv);
326       faceSizeAllT = minv;
327       ierr = PetscMemzero(sizes, numCellFacesH*sizeof(PetscInt));CHKERRQ(ierr);
328       for (cf = numCellFacesT; cf < numCellFacesH; ++cf) { /* These are the hybrid faces */
329         const PetscInt *cellFace = &cellFaces[-cf*faceSize];
330         PetscInt       f;
331 
332         for (f = 0; f < -faceSize; ++f) sizes[cf-numCellFacesT] += (cellFace[f] >= 0 ? 1 : 0);
333       }
334       ierr = PetscSortInt(numCellFacesH - numCellFacesT, sizes);CHKERRQ(ierr);
335       minv = sizes[0];
336       maxv = sizes[PetscMax(numCellFacesH - numCellFacesT-1, 0)];
337       ierr = PetscFree(sizes);CHKERRQ(ierr);
338       if (minv != maxv) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP, "Different number of vertices for hybrid face %D != %D", minv, maxv);
339       faceSizeAllH = minv;
340       if (!faceSizeAll) faceSizeAll = faceSizeAllT;
341     } else { /* the size of the faces in hybrid cells is the same */
342       faceSizeAll = faceSizeAllH = faceSizeAllT = faceSize;
343     }
344     ierr = DMPlexRestoreRawFacesHybrid_Internal(dm, cellDim, coneSizeH, cone, &numCellFacesH, &numCellFacesT, &faceSize, &cellFaces);CHKERRQ(ierr);
345   } else if (pEnd[cellDepth] > pStart[cellDepth]) {
346     ierr = DMPlexGetFaces_Internal(dm, cellDim, pStart[cellDepth], NULL, &faceSizeAll, NULL);CHKERRQ(ierr);
347   }
348   if (faceSizeAll > 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not support interpolation of meshes with faces of %D vertices", faceSizeAll);
349 
350   /* With hybrid grids, we first iterate on hybrid cells and start numbering the non-hybrid faces
351      Then, faces for non-hybrid cells are numbered.
352      This is to guarantee consistent orientations (all 0) of all the points in the cone of the hybrid cells */
353   ierr = PetscHashIJKLCreate(&faceTable);CHKERRQ(ierr);
354   for (outerloop = 0, face = pStart[faceDepth]; outerloop < 2; outerloop++) {
355     PetscInt start, end;
356 
357     start = outerloop == 0 ? pMax : pStart[cellDepth];
358     end = outerloop == 0 ? pEnd[cellDepth] : pMax;
359     for (c = start; c < end; ++c) {
360       const PetscInt *cellFaces;
361       PetscInt        numCellFaces, faceSize, faceSizeInc, faceSizeCheck, cf;
362 
363       if (c < pMax) {
364         ierr = DMPlexGetFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
365         if (faceSize != faceSizeAll) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent face for cell %D of size %D != %D", c, faceSize, faceSizeAll);
366         faceSizeCheck = faceSizeAll;
367       } else { /* Hybrid cell */
368         const PetscInt *cone;
369         PetscInt        numCellFacesN, coneSize;
370 
371         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
372         if (coneSize != coneSizeH) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unexpected hybrid coneSize %D != %D", coneSize, coneSizeH);
373         ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
374         ierr = DMPlexGetRawFacesHybrid_Internal(dm, cellDim, coneSize, cone, &numCellFaces, &numCellFacesN, &faceSize, &cellFaces);CHKERRQ(ierr);
375         if (numCellFaces != numCellFacesH) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unexpected numCellFaces %D != %D for hybrid cell %D", numCellFaces, numCellFacesH, c);
376         faceSize = PetscMax(faceSize, -faceSize);
377         if (faceSize > 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not support interpolation of meshes with faces of %D vertices", faceSize);
378         numCellFaces = numCellFacesN; /* process only non-hybrid faces */
379         faceSizeCheck = faceSizeAllT;
380       }
381       faceSizeInc = faceSize;
382       for (cf = 0; cf < numCellFaces; ++cf) {
383         const PetscInt   *cellFace = &cellFaces[cf*faceSizeInc];
384         PetscInt          faceSizeH = faceSize;
385         PetscHashIJKLKey  key;
386         PetscHashIter     iter;
387         PetscBool         missing;
388 
389         if (faceSizeInc == 2) {
390           key.i = PetscMin(cellFace[0], cellFace[1]);
391           key.j = PetscMax(cellFace[0], cellFace[1]);
392           key.k = PETSC_MAX_INT;
393           key.l = PETSC_MAX_INT;
394         } else {
395           key.i = cellFace[0];
396           key.j = cellFace[1];
397           key.k = cellFace[2];
398           key.l = faceSize > 3 ? (cellFace[3] < 0 ? faceSizeH = 3, PETSC_MAX_INT : cellFace[3]) : PETSC_MAX_INT;
399           ierr  = PetscSortInt(faceSize, (PetscInt *) &key);CHKERRQ(ierr);
400         }
401         /* this check is redundant for non-hybrid meshes */
402         if (faceSizeH != faceSizeCheck) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unexpected number of vertices for face %D of point %D -> %D != %D", cf, c, faceSizeH, faceSizeCheck);
403         ierr = PetscHashIJKLPut(faceTable, key, &iter, &missing);CHKERRQ(ierr);
404         if (missing) {
405           ierr = PetscHashIJKLIterSet(faceTable, iter, face++);CHKERRQ(ierr);
406           if (c >= pMax) ++faceT;
407         }
408       }
409       if (c < pMax) {
410         ierr = DMPlexRestoreFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
411       } else {
412         ierr = DMPlexRestoreRawFacesHybrid_Internal(dm, cellDim, coneSizeH, NULL, NULL, NULL, NULL, &cellFaces);CHKERRQ(ierr);
413       }
414     }
415   }
416   pEnd[faceDepth] = face;
417 
418   /* Second pass for hybrid meshes: number hybrid faces */
419   for (c = pMax; c < pEnd[cellDepth]; ++c) {
420     const PetscInt *cellFaces, *cone;
421     PetscInt        numCellFaces, numCellFacesN, faceSize, cf, coneSize;
422 
423     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
424     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
425     ierr = DMPlexGetRawFacesHybrid_Internal(dm, cellDim, coneSize, cone, &numCellFaces, &numCellFacesN, &faceSize, &cellFaces);CHKERRQ(ierr);
426     if (numCellFaces != numCellFacesH) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unexpected hybrid numCellFaces %D != %D", numCellFaces, numCellFacesH);
427     faceSize = PetscMax(faceSize, -faceSize);
428     for (cf = numCellFacesN; cf < numCellFaces; ++cf) { /* These are the hybrid faces */
429       const PetscInt   *cellFace = &cellFaces[cf*faceSize];
430       PetscHashIJKLKey  key;
431       PetscHashIter     iter;
432       PetscBool         missing;
433       PetscInt          faceSizeH = faceSize;
434 
435       if (faceSize == 2) {
436         key.i = PetscMin(cellFace[0], cellFace[1]);
437         key.j = PetscMax(cellFace[0], cellFace[1]);
438         key.k = PETSC_MAX_INT;
439         key.l = PETSC_MAX_INT;
440       } else {
441         key.i = cellFace[0];
442         key.j = cellFace[1];
443         key.k = cellFace[2];
444         key.l = faceSize > 3 ? (cellFace[3] < 0 ? faceSizeH = 3, PETSC_MAX_INT : cellFace[3]) : PETSC_MAX_INT;
445         ierr  = PetscSortInt(faceSize, (PetscInt *) &key);CHKERRQ(ierr);
446       }
447       if (faceSizeH != faceSizeAllH) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unexpected number of vertices for hybrid face %D of point %D -> %D != %D", cf, c, faceSizeH, faceSizeAllH);
448       ierr = PetscHashIJKLPut(faceTable, key, &iter, &missing);CHKERRQ(ierr);
449       if (missing) {ierr = PetscHashIJKLIterSet(faceTable, iter, face++);CHKERRQ(ierr);}
450     }
451     ierr = DMPlexRestoreRawFacesHybrid_Internal(dm, cellDim, coneSize, cone, &numCellFaces, &numCellFacesN, &faceSize, &cellFaces);CHKERRQ(ierr);
452   }
453   faceH = face - pEnd[faceDepth];
454   if (faceH) {
455     if (fMax == PETSC_DETERMINE) fMax = pEnd[faceDepth];
456     else if (eMax == PETSC_DETERMINE) eMax = pEnd[faceDepth];
457     else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of unassigned hybrid facets %D for cellDim %D and dimension %D", faceH, cellDim, dim);
458   }
459   pEnd[faceDepth] = face;
460   ierr = PetscHashIJKLDestroy(&faceTable);CHKERRQ(ierr);
461   /* Count new points */
462   for (d = 0; d <= depth; ++d) {
463     numPoints += pEnd[d]-pStart[d];
464   }
465   ierr = DMPlexSetChart(idm, 0, numPoints);CHKERRQ(ierr);
466   /* Set cone sizes */
467   for (d = 0; d <= depth; ++d) {
468     PetscInt coneSize, p;
469 
470     if (d == faceDepth) {
471       /* Now we have two cases: */
472       if (faceSizeAll == faceSizeAllT) {
473         /* I see no way to do this if we admit faces of different shapes */
474         for (p = pStart[d]; p < pEnd[d]-faceH; ++p) {
475           ierr = DMPlexSetConeSize(idm, p, faceSizeAll);CHKERRQ(ierr);
476         }
477         for (p = pEnd[d]-faceH; p < pEnd[d]; ++p) {
478           ierr = DMPlexSetConeSize(idm, p, faceSizeAllH);CHKERRQ(ierr);
479         }
480       } else if (faceSizeAll == faceSizeAllH) {
481         for (p = pStart[d]; p < pStart[d]+faceT; ++p) {
482           ierr = DMPlexSetConeSize(idm, p, faceSizeAllT);CHKERRQ(ierr);
483         }
484         for (p = pStart[d]+faceT; p < pEnd[d]-faceH; ++p) {
485           ierr = DMPlexSetConeSize(idm, p, faceSizeAll);CHKERRQ(ierr);
486         }
487         for (p = pEnd[d]-faceH; p < pEnd[d]; ++p) {
488           ierr = DMPlexSetConeSize(idm, p, faceSizeAllH);CHKERRQ(ierr);
489         }
490       } else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent faces sizes N: %D T: %D H: %D", faceSizeAll, faceSizeAllT, faceSizeAllH);
491     } else if (d == cellDepth) {
492       for (p = pStart[d]; p < pEnd[d]; ++p) {
493         /* Number of cell faces may be different from number of cell vertices*/
494         if (p < pMax) {
495           ierr = DMPlexGetFaces_Internal(dm, cellDim, p, &coneSize, NULL, NULL);CHKERRQ(ierr);
496         } else {
497           ierr = DMPlexGetFacesHybrid_Internal(dm, cellDim, p, &coneSize, NULL, NULL, NULL);CHKERRQ(ierr);
498         }
499         ierr = DMPlexSetConeSize(idm, p, coneSize);CHKERRQ(ierr);
500       }
501     } else {
502       for (p = pStart[d]; p < pEnd[d]; ++p) {
503         ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
504         ierr = DMPlexSetConeSize(idm, p, coneSize);CHKERRQ(ierr);
505       }
506     }
507   }
508   ierr = DMSetUp(idm);CHKERRQ(ierr);
509   /* Get face cones from subsets of cell vertices */
510   if (faceSizeAll > 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not support interpolation of meshes with faces of %D vertices", faceSizeAll);
511   ierr = PetscHashIJKLCreate(&faceTable);CHKERRQ(ierr);
512   for (d = depth; d > cellDepth; --d) {
513     const PetscInt *cone;
514     PetscInt        p;
515 
516     for (p = pStart[d]; p < pEnd[d]; ++p) {
517       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
518       ierr = DMPlexSetCone(idm, p, cone);CHKERRQ(ierr);
519       ierr = DMPlexGetConeOrientation(dm, p, &cone);CHKERRQ(ierr);
520       ierr = DMPlexSetConeOrientation(idm, p, cone);CHKERRQ(ierr);
521     }
522   }
523   for (outerloop = 0, face = pStart[faceDepth]; outerloop < 2; outerloop++) {
524     PetscInt start, end;
525 
526     start = outerloop == 0 ? pMax : pStart[cellDepth];
527     end = outerloop == 0 ? pEnd[cellDepth] : pMax;
528     for (c = start; c < end; ++c) {
529       const PetscInt *cellFaces;
530       PetscInt        numCellFaces, faceSize, faceSizeInc, cf;
531 
532       if (c < pMax) {
533         ierr = DMPlexGetFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
534         if (faceSize != faceSizeAll) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent face for cell %D of size %D != %D", c, faceSize, faceSizeAll);
535       } else {
536         const PetscInt *cone;
537         PetscInt        numCellFacesN, coneSize;
538 
539         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
540         if (coneSize != coneSizeH) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unexpected hybrid coneSize %D != %D", coneSize, coneSizeH);
541         ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
542         ierr = DMPlexGetRawFacesHybrid_Internal(dm, cellDim, coneSize, cone, &numCellFaces, &numCellFacesN, &faceSize, &cellFaces);CHKERRQ(ierr);
543         if (numCellFaces != numCellFacesH) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unexpected numCellFaces %D != %D for hybrid cell %D", numCellFaces, numCellFacesH, c);
544         faceSize = PetscMax(faceSize, -faceSize);
545         if (faceSize > 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not support interpolation of meshes with faces of %D vertices", faceSize);
546         numCellFaces = numCellFacesN; /* process only non-hybrid faces */
547       }
548       faceSizeInc = faceSize;
549       for (cf = 0; cf < numCellFaces; ++cf) {
550         const PetscInt  *cellFace = &cellFaces[cf*faceSizeInc];
551         PetscHashIJKLKey key;
552         PetscHashIter    iter;
553         PetscBool        missing;
554 
555         if (faceSizeInc == 2) {
556           key.i = PetscMin(cellFace[0], cellFace[1]);
557           key.j = PetscMax(cellFace[0], cellFace[1]);
558           key.k = PETSC_MAX_INT;
559           key.l = PETSC_MAX_INT;
560         } else {
561           key.i = cellFace[0];
562           key.j = cellFace[1];
563           key.k = cellFace[2];
564           key.l = faceSizeInc > 3 ? (cellFace[3] < 0 ? faceSize = 3, PETSC_MAX_INT : cellFace[3]) : PETSC_MAX_INT;
565           ierr  = PetscSortInt(faceSizeInc, (PetscInt *) &key);CHKERRQ(ierr);
566         }
567         ierr = PetscHashIJKLPut(faceTable, key, &iter, &missing);CHKERRQ(ierr);
568         if (missing) {
569           ierr = DMPlexSetCone(idm, face, cellFace);CHKERRQ(ierr);
570           ierr = PetscHashIJKLIterSet(faceTable, iter, face);CHKERRQ(ierr);
571           ierr = DMPlexInsertCone(idm, c, cf, face++);CHKERRQ(ierr);
572         } else {
573           const PetscInt *cone;
574           PetscInt        coneSize, ornt, i, j, f;
575 
576           ierr = PetscHashIJKLIterGet(faceTable, iter, &f);CHKERRQ(ierr);
577           ierr = DMPlexInsertCone(idm, c, cf, f);CHKERRQ(ierr);
578           /* Orient face: Do not allow reverse orientation at the first vertex */
579           ierr = DMPlexGetConeSize(idm, f, &coneSize);CHKERRQ(ierr);
580           ierr = DMPlexGetCone(idm, f, &cone);CHKERRQ(ierr);
581           if (coneSize != faceSize) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of face vertices %D for face %D should be %D", coneSize, f, faceSize);
582           /* - First find the initial vertex */
583           for (i = 0; i < faceSize; ++i) if (cellFace[0] == cone[i]) break;
584           /* - Try forward comparison */
585           for (j = 0; j < faceSize; ++j) if (cellFace[j] != cone[(i+j)%faceSize]) break;
586           if (j == faceSize) {
587             if ((faceSize == 2) && (i == 1)) ornt = -2;
588             else                             ornt = i;
589           } else {
590             /* - Try backward comparison */
591             for (j = 0; j < faceSize; ++j) if (cellFace[j] != cone[(i+faceSize-j)%faceSize]) break;
592             if (j == faceSize) {
593               if (i == 0) ornt = -faceSize;
594               else        ornt = -i;
595             } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not determine orientation of face %D in cell %D", f, c);
596           }
597           ierr = DMPlexInsertConeOrientation(idm, c, cf, ornt);CHKERRQ(ierr);
598         }
599       }
600       if (c < pMax) {
601         ierr = DMPlexRestoreFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
602       } else {
603         ierr = DMPlexRestoreRawFacesHybrid_Internal(dm, cellDim, coneSizeH, NULL, NULL, NULL, NULL, &cellFaces);CHKERRQ(ierr);
604       }
605     }
606   }
607   /* Second pass for hybrid meshes: orient hybrid faces */
608   for (c = pMax; c < pEnd[cellDepth]; ++c) {
609     const PetscInt *cellFaces, *cone;
610     PetscInt        numCellFaces, numCellFacesN, faceSize, cf, coneSize;
611 
612     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
613     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
614     ierr = DMPlexGetRawFacesHybrid_Internal(dm, cellDim, coneSize, cone, &numCellFaces, &numCellFacesN, &faceSize, &cellFaces);CHKERRQ(ierr);
615     if (numCellFaces != numCellFacesH) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unexpected hybrid numCellFaces %D != %D", numCellFaces, numCellFacesH);
616     faceSize = PetscMax(faceSize, -faceSize);
617     for (cf = numCellFacesN; cf < numCellFaces; ++cf) { /* These are the hybrid faces */
618       const PetscInt   *cellFace = &cellFaces[cf*faceSize];
619       PetscHashIJKLKey key;
620       PetscHashIter    iter;
621       PetscBool        missing;
622       PetscInt         faceSizeH = faceSize;
623 
624       if (faceSize == 2) {
625         key.i = PetscMin(cellFace[0], cellFace[1]);
626         key.j = PetscMax(cellFace[0], cellFace[1]);
627         key.k = PETSC_MAX_INT;
628         key.l = PETSC_MAX_INT;
629       } else {
630         key.i = cellFace[0];
631         key.j = cellFace[1];
632         key.k = cellFace[2];
633         key.l = faceSize > 3 ? (cellFace[3] < 0 ? faceSizeH = 3, PETSC_MAX_INT : cellFace[3]) : PETSC_MAX_INT;
634         ierr  = PetscSortInt(faceSize, (PetscInt *) &key);CHKERRQ(ierr);
635       }
636       if (faceSizeH != faceSizeAllH) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unexpected number of vertices for hybrid face %D of point %D -> %D != %D", cf, c, faceSizeH, faceSizeAllH);
637       ierr = PetscHashIJKLPut(faceTable, key, &iter, &missing);CHKERRQ(ierr);
638       if (missing) {
639         ierr = DMPlexSetCone(idm, face, cellFace);CHKERRQ(ierr);
640         ierr = PetscHashIJKLIterSet(faceTable, iter, face);CHKERRQ(ierr);
641         ierr = DMPlexInsertCone(idm, c, cf, face++);CHKERRQ(ierr);
642       } else {
643         PetscInt        fv[4] = {0, 1, 2, 3};
644         const PetscInt *cone;
645         PetscInt        coneSize, ornt, i, j, f;
646 
647         ierr = PetscHashIJKLIterGet(faceTable, iter, &f);CHKERRQ(ierr);
648         ierr = DMPlexInsertCone(idm, c, cf, f);CHKERRQ(ierr);
649         /* Orient face: Do not allow reverse orientation at the first vertex */
650         ierr = DMPlexGetConeSize(idm, f, &coneSize);CHKERRQ(ierr);
651         ierr = DMPlexGetCone(idm, f, &cone);CHKERRQ(ierr);
652         if (coneSize != faceSizeH) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of face vertices %D for face %D should be %D", coneSize, f, faceSizeH);
653         /* Hybrid faces are stored as tensor products of edges, so to compare them to normal faces, we have to flip */
654         if (faceSize == 4 && c >= pMax && faceSizeAll != faceSizeAllT && f < pEnd[faceDepth] - faceH) {fv[2] = 3; fv[3] = 2;}
655         /* - First find the initial vertex */
656         for (i = 0; i < faceSizeH; ++i) if (cellFace[fv[0]] == cone[i]) break;
657         /* - Try forward comparison */
658         for (j = 0; j < faceSizeH; ++j) if (cellFace[fv[j]] != cone[(i+j)%faceSizeH]) break;
659         if (j == faceSizeH) {
660           if ((faceSizeH == 2) && (i == 1)) ornt = -2;
661           else                              ornt = i;
662         } else {
663           /* - Try backward comparison */
664           for (j = 0; j < faceSizeH; ++j) if (cellFace[fv[j]] != cone[(i+faceSizeH-j)%faceSizeH]) break;
665           if (j == faceSizeH) {
666             if (i == 0) ornt = -faceSizeH;
667             else        ornt = -i;
668           } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not determine orientation of face %D in cell %D", f, c);
669         }
670         ierr = DMPlexInsertConeOrientation(idm, c, cf, ornt);CHKERRQ(ierr);
671       }
672     }
673     ierr = DMPlexRestoreRawFacesHybrid_Internal(dm, cellDim, coneSize, cone, &numCellFaces, &numCellFacesN, &faceSize, &cellFaces);CHKERRQ(ierr);
674   }
675   if (face != pEnd[faceDepth]) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of faces %D should be %D", face-pStart[faceDepth], pEnd[faceDepth]-pStart[faceDepth]);
676   ierr = PetscFree2(pStart,pEnd);CHKERRQ(ierr);
677   ierr = PetscHashIJKLDestroy(&faceTable);CHKERRQ(ierr);
678   ierr = PetscFree2(pStart,pEnd);CHKERRQ(ierr);
679   ierr = DMPlexSetHybridBounds(idm, cMax, fMax, eMax, vMax);CHKERRQ(ierr);
680   ierr = DMPlexSymmetrize(idm);CHKERRQ(ierr);
681   ierr = DMPlexStratify(idm);CHKERRQ(ierr);
682   PetscFunctionReturn(0);
683 }
684 
685 PetscErrorCode DMPlexOrientCell(DM dm, PetscInt p, PetscInt masterConeSize, const PetscInt masterCone[])
686 {
687   PetscInt coneSize;
688   PetscInt start1=0;
689   PetscBool reverse1=PETSC_FALSE;
690   const PetscInt *cone=NULL;
691   PetscErrorCode ierr;
692 
693   PetscFunctionBegin;
694   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
695   if (!coneSize) PetscFunctionReturn(0); /* do nothing for points with no cone */
696   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
697   ierr = DMPlexFixFaceOrientations_Orient_Private(coneSize, masterConeSize, masterCone, cone, &start1, &reverse1);CHKERRQ(ierr);
698 #if defined(PETSC_USE_DEBUG)
699   if (PetscUnlikely(cone[start1] != masterCone[0])) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "The algorithm above is wrong as cone[%d] = %d != %d = masterCone[0]", start1, cone[start1], masterCone[0]);
700 #endif
701   ierr = DMPlexOrientCell_Internal(dm, p, start1, reverse1);CHKERRQ(ierr);
702 #if defined(PETSC_USE_DEBUG)
703   {
704     PetscInt c;
705     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
706     for (c = 0; c < 2; c++) {
707       if (PetscUnlikely(cone[c] != masterCone[c])) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_PLIB, "The algorithm above is wrong as cone[%d] = %d != %d = masterCone[%d]", c, cone[c], masterCone[c], c);
708     }
709   }
710 #endif
711   PetscFunctionReturn(0);
712 }
713 
714 PetscErrorCode DMPlexOrientCell_Internal(DM dm, PetscInt p, PetscInt start1, PetscBool reverse1)
715 {
716   PetscInt i, j, k, maxConeSize, coneSize, coneConeSize, supportSize, supportConeSize;
717   PetscInt start0, start;
718   PetscBool reverse0, reverse;
719   PetscInt newornt;
720   const PetscInt *cone=NULL, *support=NULL, *supportCone=NULL, *ornts=NULL;
721   PetscInt *newcone=NULL, *newornts=NULL;
722   PetscErrorCode ierr;
723 
724   PetscFunctionBegin;
725   if (!start1 && !reverse1) PetscFunctionReturn(0);
726   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
727   if (!coneSize) PetscFunctionReturn(0); /* do nothing for points with no cone */
728   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
729   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
730   /* permute p's cone and orientations */
731   ierr = DMPlexGetConeOrientation(dm, p, &ornts);CHKERRQ(ierr);
732   ierr = DMGetWorkArray(dm, maxConeSize, MPIU_INT, &newcone);CHKERRQ(ierr);
733   ierr = DMGetWorkArray(dm, maxConeSize, MPIU_INT, &newornts);CHKERRQ(ierr);
734   ierr = DMPlexFixFaceOrientations_Permute_Private(coneSize, cone, start1, reverse1, newcone);CHKERRQ(ierr);
735   ierr = DMPlexFixFaceOrientations_Permute_Private(coneSize, ornts, start1, reverse1, newornts);CHKERRQ(ierr);
736   /* if direction of p (face) is flipped, flip also p's cone points (edges) */
737   if (reverse1) {
738     for (i=0; i<coneSize; i++) {
739       ierr = DMPlexGetConeSize(dm, cone[i], &coneConeSize);CHKERRQ(ierr);
740       ierr = DMPlexFixFaceOrientations_Translate_Private(newornts[i], &start0, &reverse0);CHKERRQ(ierr);
741       ierr = DMPlexFixFaceOrientations_Combine_Private(coneConeSize, start0, reverse0, 1, PETSC_FALSE, &start, &reverse);CHKERRQ(ierr);
742       ierr = DMPlexFixFaceOrientations_TranslateBack_Private(coneConeSize, start, reverse, &newornts[i]);CHKERRQ(ierr);
743     }
744   }
745   ierr = DMPlexSetConeOrientation(dm, p, newornts);CHKERRQ(ierr);
746   /* fix oriention of p within cones of p's support points */
747   ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
748   ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
749   for (j=0; j<supportSize; j++) {
750     ierr = DMPlexGetCone(dm, support[j], &supportCone);CHKERRQ(ierr);
751     ierr = DMPlexGetConeSize(dm, support[j], &supportConeSize);CHKERRQ(ierr);
752     ierr = DMPlexGetConeOrientation(dm, support[j], &ornts);CHKERRQ(ierr);
753     for (k=0; k<supportConeSize; k++) {
754       if (supportCone[k] != p) continue;
755       ierr = DMPlexFixFaceOrientations_Translate_Private(ornts[k], &start0, &reverse0);CHKERRQ(ierr);
756       ierr = DMPlexFixFaceOrientations_Combine_Private(coneSize, start0, reverse0, start1, reverse1, &start, &reverse);CHKERRQ(ierr);
757       ierr = DMPlexFixFaceOrientations_TranslateBack_Private(coneSize, start, reverse, &newornt);CHKERRQ(ierr);
758       ierr = DMPlexInsertConeOrientation(dm, support[j], k, newornt);CHKERRQ(ierr);
759     }
760   }
761   /* rewrite cone */
762   ierr = DMPlexSetCone(dm, p, newcone);CHKERRQ(ierr);
763   ierr = DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &newcone);CHKERRQ(ierr);
764   ierr = DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &newornts);CHKERRQ(ierr);
765   PetscFunctionReturn(0);
766 }
767 
768 static PetscErrorCode SortRmineRremoteByRemote_Private(PetscSF sf, PetscInt *rmine1[], PetscInt *rremote1[])
769 {
770   PetscInt            nleaves;
771   PetscInt            nranks;
772   const PetscMPIInt  *ranks=NULL;
773   const PetscInt     *roffset=NULL, *rmine=NULL, *rremote=NULL;
774   PetscInt            n, o, r;
775   PetscErrorCode      ierr;
776 
777   PetscFunctionBegin;
778   ierr = PetscSFGetRanks(sf, &nranks, &ranks, &roffset, &rmine, &rremote);CHKERRQ(ierr);
779   nleaves = roffset[nranks];
780   ierr = PetscMalloc2(nleaves, rmine1, nleaves, rremote1);CHKERRQ(ierr);
781   for (r=0; r<nranks; r++) {
782     /* simultaneously sort rank-wise portions of rmine & rremote by values in rremote
783        - to unify order with the other side */
784     o = roffset[r];
785     n = roffset[r+1] - o;
786     ierr = PetscMemcpy(&(*rmine1)[o], &rmine[o], n*sizeof(PetscInt));CHKERRQ(ierr);
787     ierr = PetscMemcpy(&(*rremote1)[o], &rremote[o], n*sizeof(PetscInt));CHKERRQ(ierr);
788     ierr = PetscSortIntWithArray(n, &(*rremote1)[o], &(*rmine1)[o]);CHKERRQ(ierr);
789   }
790   PetscFunctionReturn(0);
791 }
792 
793 PetscErrorCode DMPlexOrientInterface(DM dm)
794 {
795   PetscSF           sf=NULL;
796   PetscInt          (*roots)[2], (*leaves)[2];
797   PetscMPIInt       (*rootsRanks)[2], (*leavesRanks)[2];
798   const PetscInt    *locals=NULL;
799   const PetscSFNode *remotes=NULL;
800   PetscInt           nroots, nleaves, p, c;
801   PetscInt           nranks, n, o, r;
802   const PetscMPIInt *ranks=NULL;
803   const PetscInt    *roffset=NULL;
804   PetscInt          *rmine1=NULL, *rremote1=NULL; /* rmine and rremote copies simultaneously sorted by rank and rremote */
805   const PetscInt    *cone=NULL;
806   PetscInt           coneSize, ind0;
807   MPI_Comm           comm;
808   PetscMPIInt        rank;
809   PetscInt           debug = 0;
810   PetscErrorCode     ierr;
811 
812   PetscFunctionBegin;
813   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
814   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, &locals, &remotes);CHKERRQ(ierr);
815   if (nroots < 0) PetscFunctionReturn(0);
816   ierr = PetscSFSetUp(sf);CHKERRQ(ierr);
817   ierr = PetscSFGetRanks(sf, &nranks, &ranks, &roffset, NULL, NULL);CHKERRQ(ierr);
818 #if defined(PETSC_USE_DEBUG)
819   ierr = DMViewFromOptions(dm, NULL, "-before_fix_dm_view");CHKERRQ(ierr);
820   ierr = DMPlexCheckPointSF(dm);CHKERRQ(ierr);
821 #endif
822   ierr = SortRmineRremoteByRemote_Private(sf, &rmine1, &rremote1);CHKERRQ(ierr);
823   ierr = PetscMalloc4(nroots, &roots, nroots, &leaves, nroots, &rootsRanks, nroots, &leavesRanks);CHKERRQ(ierr);
824   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
825   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
826   if (debug && rank == 0) {ierr = PetscSynchronizedPrintf(comm, "Roots\n");CHKERRQ(ierr);}
827   for (p = 0; p < nroots; ++p) {
828     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
829     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
830     /* Translate all points to root numbering */
831     for (c = 0; c < 2; c++) {
832       if (coneSize > 1) {
833         ierr = PetscFindInt(cone[c], nleaves, locals, &ind0);CHKERRQ(ierr);
834         if (ind0 < 0) {
835           roots[p][c] = cone[c];
836           rootsRanks[p][c] = rank;
837         } else {
838           roots[p][c] = remotes[ind0].index;
839           rootsRanks[p][c] = remotes[ind0].rank;
840         }
841       } else {
842         roots[p][c] = -1;
843         rootsRanks[p][c] = -1;
844       }
845     }
846   }
847   if (debug) {
848     for (p = 0; p < nroots; ++p) {
849       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
850       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
851       if (coneSize > 1) {
852         ierr = PetscSynchronizedPrintf(comm, "[%d]  %D: cone=[%D %D] roots=[%D %D] rootsRanks=[%D %D]\n", rank, p, cone[0], cone[1], roots[p][0], roots[p][1], rootsRanks[p][0], rootsRanks[p][1]);CHKERRQ(ierr);
853       }
854     }
855     ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
856   }
857   for (p = 0; p < nroots; ++p) {
858     for (c = 0; c < 2; c++) {
859       leaves[p][c] = -2;
860       leavesRanks[p][c] = -2;
861     }
862   }
863   ierr = PetscSFBcastBegin(sf, MPIU_2INT, roots, leaves);CHKERRQ(ierr);
864   ierr = PetscSFBcastBegin(sf, MPI_2INT, rootsRanks, leavesRanks);CHKERRQ(ierr);
865   ierr = PetscSFBcastEnd(sf, MPIU_2INT, roots, leaves);CHKERRQ(ierr);
866   ierr = PetscSFBcastEnd(sf, MPI_2INT, rootsRanks, leavesRanks);CHKERRQ(ierr);
867   if (debug) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
868   if (debug && rank == 0) {ierr = PetscSynchronizedPrintf(comm, "Referred leaves\n");CHKERRQ(ierr);}
869   for (p = 0; p < nroots; ++p) {
870     if (leaves[p][0] < 0) continue;
871     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
872     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
873     if (debug) {ierr = PetscSynchronizedPrintf(comm, "[%d]  %D: cone=[%D %D] leaves=[%D %D] roots=[%D %D] leavesRanks=[%D %D] rootsRanks=[%D %D]\n", rank, p, cone[0], cone[1], leaves[p][0], leaves[p][1], roots[p][0], roots[p][1], leavesRanks[p][0], leavesRanks[p][1], rootsRanks[p][0], rootsRanks[p][1]);CHKERRQ(ierr);}
874     if ((leaves[p][0] != roots[p][0]) || (leaves[p][1] != roots[p][1]) || (leavesRanks[p][0] != rootsRanks[p][0]) || (leavesRanks[p][0] != rootsRanks[p][0])) {
875       PetscInt masterCone[2];
876       /* Translate these two cone points back to leave numbering */
877       for (c = 0; c < 2; c++) {
878         if (leavesRanks[p][c] == rank) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "this should never happen - remote rank of point %D is the same rank",leavesRanks[p][c]);
879         /* Find index of rank leavesRanks[p][c] among remote ranks */
880         /* No need for PetscMPIIntCast because these integers were originally cast from PetscMPIInt. */
881         ierr = PetscFindMPIInt((PetscMPIInt)leavesRanks[p][c], nranks, ranks, &r);CHKERRQ(ierr);
882         if (PetscUnlikely(r < 0)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "this should never happen - rank %D not found among remote ranks",leavesRanks[p][c]);
883         /* Find point leaves[p][c] among remote points aimed at rank leavesRanks[p][c] */
884         o = roffset[r];
885         n = roffset[r+1] - o;
886         ierr = PetscFindInt(leaves[p][c], n, &rremote1[o], &ind0);CHKERRQ(ierr);
887         if (PetscUnlikely(ind0 < 0)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "No cone point of %D is connected to (%D, %D) - it seems there is missing connection in point SF!",p,ranks[r],leaves[p][c]);
888         /* Get the corresponding local point */
889         masterCone[c] = rmine1[o+ind0];CHKERRQ(ierr);
890       }
891       if (debug) {ierr = PetscSynchronizedPrintf(comm, "[%d]  %D: masterCone=[%D %D]\n", rank, p, masterCone[0], masterCone[1]);CHKERRQ(ierr);}
892       /* Vaclav's note: Here we only compare first 2 points of the cone. Full cone size would lead to stronger self-checking. */
893       ierr = DMPlexOrientCell(dm, p, 2, masterCone);CHKERRQ(ierr);
894     }
895   }
896 #if defined(PETSC_USE_DEBUG)
897   ierr = DMViewFromOptions(dm, NULL, "-after_fix_dm_view");CHKERRQ(ierr);
898   for (r = 0; r < nleaves; ++r) {
899     p = locals[r];
900     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
901     if (!coneSize) continue;
902     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
903     for (c = 0; c < 2; c++) {
904       ierr = PetscFindInt(cone[c], nleaves, locals, &ind0);CHKERRQ(ierr);
905       if (ind0 < 0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D but is missing its cone point cone[%D] = %D!", p, c, cone[c]);
906       if (leaves[p][c] != remotes[ind0].index || leavesRanks[p][c] != remotes[ind0].rank) {
907         if (leavesRanks[p][c] == rank) {
908           PetscInt ind1;
909           ierr = PetscFindInt(leaves[p][c], nleaves, locals, &ind1);CHKERRQ(ierr);
910           if (ind1 < 0) {
911             SETERRQ8(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D = locals[%d]: cone[%D]=%D --> (%D, %D) differs from the enforced (%D, %D). The latter was not even found among the local SF points - it is probably broken!", p, r, c, cone[c], remotes[ind0].rank, remotes[ind0].index, leavesRanks[p][c], leaves[p][c]);
912           } else {
913             SETERRQ9(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D = locals[%d]: cone[%D]=%D --> (%D, %D) differs from the enforced %D --> (%D, %D). Is the algorithm above or the point SF broken?", p, r, c, cone[c], remotes[ind0].rank, remotes[ind0].index, leaves[p][c], remotes[ind1].rank, remotes[ind1].index);
914           }
915         } else {
916           SETERRQ8(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D = locals[%d]: cone[%D]=%D --> (%D, %D) differs from the enforced (%D, %D). Is the algorithm above or the point SF broken?", p, r, c, cone[c], remotes[ind0].rank, remotes[ind0].index, leavesRanks[p][c], leaves[p][c]);
917         }
918       }
919     }
920   }
921 #endif
922   if (debug) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
923   ierr = PetscFree4(roots, leaves, rootsRanks, leavesRanks);CHKERRQ(ierr);
924   ierr = PetscFree2(rmine1, rremote1);CHKERRQ(ierr);
925   PetscFunctionReturn(0);
926 }
927 
928 static PetscErrorCode IntArrayViewFromOptions(MPI_Comm comm, const char opt[], const char name[], const char idxname[], const char valname[], PetscInt n, const PetscInt a[])
929 {
930   PetscInt       idx;
931   PetscMPIInt    rank;
932   PetscBool      flg;
933   PetscErrorCode ierr;
934 
935   PetscFunctionBegin;
936   ierr = PetscOptionsHasName(NULL, NULL, opt, &flg);CHKERRQ(ierr);
937   if (!flg) PetscFunctionReturn(0);
938   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
939   ierr = PetscSynchronizedPrintf(comm, "[%d]%s:\n", rank, name);CHKERRQ(ierr);
940   for (idx = 0; idx < n; ++idx) {ierr = PetscSynchronizedPrintf(comm, "[%d]%s %D %s %D\n", rank, idxname, idx, valname, a[idx]);CHKERRQ(ierr);}
941   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
942   PetscFunctionReturn(0);
943 }
944 
945 static PetscErrorCode SFNodeArrayViewFromOptions(MPI_Comm comm, const char opt[], const char name[], const char idxname[], PetscInt n, const PetscSFNode a[])
946 {
947   PetscInt       idx;
948   PetscMPIInt    rank;
949   PetscBool      flg;
950   PetscErrorCode ierr;
951 
952   PetscFunctionBegin;
953   ierr = PetscOptionsHasName(NULL, NULL, opt, &flg);CHKERRQ(ierr);
954   if (!flg) PetscFunctionReturn(0);
955   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
956   ierr = PetscSynchronizedPrintf(comm, "[%d]%s:\n", rank, name);CHKERRQ(ierr);
957   if (idxname) {
958     for (idx = 0; idx < n; ++idx) {ierr = PetscSynchronizedPrintf(comm, "[%d]%s %D rank %D index %D\n", rank, idxname, idx, a[idx].rank, a[idx].index);CHKERRQ(ierr);}
959   } else {
960     for (idx = 0; idx < n; ++idx) {ierr = PetscSynchronizedPrintf(comm, "[%d]rank %D index %D\n", rank, a[idx].rank, a[idx].index);CHKERRQ(ierr);}
961   }
962   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
963   PetscFunctionReturn(0);
964 }
965 
966 static PetscErrorCode DMPlexMapToLocalPoint(PetscHMapIJ roothash, const PetscInt localPoints[], PetscMPIInt rank, PetscSFNode remotePoint, PetscInt *localPoint)
967 {
968   PetscErrorCode ierr;
969 
970   PetscFunctionBegin;
971   if (remotePoint.rank == rank) {
972     *localPoint = remotePoint.index;
973   } else {
974     PetscHashIJKey key;
975     PetscInt       root;
976 
977     key.i = remotePoint.index;
978     key.j = remotePoint.rank;
979     ierr = PetscHMapIJGet(roothash, key, &root);CHKERRQ(ierr);
980     if (root >= 0) {
981       *localPoint = localPoints[root];
982     } else PetscFunctionReturn(1);
983   }
984   PetscFunctionReturn(0);
985 }
986 
987 /*@
988   DMPlexInterpolatePointSF - Insert interpolated points in the overlap into the PointSF in parallel, following local interpolation
989 
990   Collective on DM
991 
992   Input Parameters:
993 + dm      - The interpolated DM
994 - pointSF - The initial SF without interpolated points
995 
996   Output Parameter:
997 . pointSF - The SF including interpolated points
998 
999   Level: intermediate
1000 
1001    Note: All debugging for this process can be turned on with the options: -dm_interp_pre_view -petscsf_interp_pre_view -petscsection_interp_candidate_view -petscsection_interp_candidate_remote_view -petscsection_interp_claim_view -petscsf_interp_pre_view -dmplex_interp_debug
1002 
1003 .keywords: mesh
1004 .seealso: DMPlexInterpolate(), DMPlexUninterpolate()
1005 @*/
1006 PetscErrorCode DMPlexInterpolatePointSF(DM dm, PetscSF pointSF)
1007 {
1008   /*
1009        Okay, the algorithm is:
1010          - Take each point in the overlap (root)
1011          - Look at the neighboring points in the overlap (candidates)
1012          - Send these candidate points to neighbors
1013          - Neighbor checks for edge between root and candidate
1014          - If edge is found, it replaces candidate point with edge point
1015          - Send back the overwritten candidates (claims)
1016          - Original guy checks for edges, different from original candidate, and gets its own edge
1017          - This pair is put into SF
1018 
1019        We need a new algorithm that tolerates groups larger than 2.
1020          - Take each point in the overlap (root)
1021          - Find all collections of points in the overlap which make faces (do early join)
1022          - Send collections as candidates (add size as first number)
1023            - Make sure to send collection to all owners of all overlap points in collection
1024          - Neighbor check for face in collections
1025          - If face is found, it replaces candidate point with face point
1026          - Send back the overwritten candidates (claims)
1027          - Original guy checks for faces, different from original candidate, and gets its own face
1028          - This pair is put into SF
1029   */
1030   PetscHMapI         leafhash;
1031   PetscHMapIJ        roothash;
1032   const PetscInt    *localPoints, *rootdegree;
1033   const PetscSFNode *remotePoints;
1034   PetscSFNode       *candidates, *candidatesRemote, *claims;
1035   PetscSection       candidateSection, candidateSectionRemote, claimSection;
1036   PetscInt           numLeaves, l, numRoots, r, candidatesSize, candidatesRemoteSize;
1037   PetscMPIInt        size, rank;
1038   PetscHashIJKey     key;
1039   PetscBool          debug = PETSC_FALSE;
1040   PetscErrorCode     ierr;
1041 
1042   PetscFunctionBegin;
1043   ierr = PetscOptionsHasName(NULL, ((PetscObject) dm)->prefix, "-dmplex_interp_debug", &debug);CHKERRQ(ierr);
1044   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
1045   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1046   ierr = PetscSFGetGraph(pointSF, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
1047   if (size < 2 || numRoots < 0) PetscFunctionReturn(0);
1048   ierr = PetscObjectViewFromOptions((PetscObject) dm, NULL, "-dm_interp_pre_view");CHKERRQ(ierr);
1049   ierr = PetscObjectViewFromOptions((PetscObject) pointSF, NULL, "-petscsf_interp_pre_view");CHKERRQ(ierr);
1050   ierr = PetscLogEventBegin(DMPLEX_InterpolateSF,dm,0,0,0);CHKERRQ(ierr);
1051   /* Build hashes of points in the SF for efficient lookup */
1052   ierr = PetscHMapICreate(&leafhash);CHKERRQ(ierr);
1053   ierr = PetscHMapIJCreate(&roothash);CHKERRQ(ierr);
1054   for (l = 0; l < numLeaves; ++l) {
1055     key.i = remotePoints[l].index;
1056     key.j = remotePoints[l].rank;
1057     ierr = PetscHMapISet(leafhash, localPoints[l], l);CHKERRQ(ierr);
1058     ierr = PetscHMapIJSet(roothash, key, l);CHKERRQ(ierr);
1059   }
1060   /* Compute root degree to identify shared points */
1061   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
1062   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
1063   ierr = IntArrayViewFromOptions(PetscObjectComm((PetscObject) dm), "-interp_root_degree_view", "Root degree", "point", "degree", numRoots, rootdegree);CHKERRQ(ierr);
1064   /* Build a section / SFNode array of candidate points (face bd points) in the cone(support(leaf)),
1065      where each candidate is defined by a set of remote points (roots) for the other points that define the face. */
1066   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &candidateSection);CHKERRQ(ierr);
1067   ierr = PetscSectionSetChart(candidateSection, 0, numRoots);CHKERRQ(ierr);
1068   {
1069     PetscHMapIJ facehash;
1070 
1071     ierr = PetscHMapIJCreate(&facehash);CHKERRQ(ierr);
1072     for (l = 0; l < numLeaves; ++l) {
1073       const PetscInt    localPoint = localPoints[l];
1074       const PetscInt   *support;
1075       PetscInt          supportSize, s;
1076 
1077       if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]  Checking local point %D\n", rank, localPoint);CHKERRQ(ierr);}
1078       ierr = DMPlexGetSupportSize(dm, localPoint, &supportSize);CHKERRQ(ierr);
1079       ierr = DMPlexGetSupport(dm, localPoint, &support);CHKERRQ(ierr);
1080       for (s = 0; s < supportSize; ++s) {
1081         const PetscInt  face = support[s];
1082         const PetscInt *cone;
1083         PetscInt        coneSize, c, f, root;
1084         PetscBool       isFace = PETSC_TRUE;
1085 
1086         /* Only add face once */
1087         if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]    Support point %D\n", rank, face);CHKERRQ(ierr);}
1088         key.i = localPoint;
1089         key.j = face;
1090         ierr = PetscHMapIJGet(facehash, key, &f);CHKERRQ(ierr);
1091         if (f >= 0) continue;
1092         ierr = DMPlexGetConeSize(dm, face, &coneSize);CHKERRQ(ierr);
1093         ierr = DMPlexGetCone(dm, face, &cone);CHKERRQ(ierr);
1094         /* If a cone point does not map to leaves on any proc, then do not put face in SF */
1095         for (c = 0; c < coneSize; ++c) {
1096           if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]      Cone point %D\n", rank, cone[c]);CHKERRQ(ierr);}
1097           ierr = PetscHMapIGet(leafhash, cone[c], &root);CHKERRQ(ierr);
1098           if (!rootdegree[cone[c]] && (root < 0)) {isFace = PETSC_FALSE; break;}
1099         }
1100         if (isFace) {
1101           if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]    Found shared face %D\n", rank, face);CHKERRQ(ierr);}
1102           ierr = PetscHMapIJSet(facehash, key, l);CHKERRQ(ierr);
1103           ierr = PetscSectionAddDof(candidateSection, localPoint, coneSize);CHKERRQ(ierr);
1104         }
1105       }
1106     }
1107     if (debug) {ierr = PetscSynchronizedFlush(PetscObjectComm((PetscObject) dm), NULL);CHKERRQ(ierr);}
1108     ierr = PetscHMapIJClear(facehash);CHKERRQ(ierr);
1109     ierr = PetscSectionSetUp(candidateSection);CHKERRQ(ierr);
1110     ierr = PetscSectionGetStorageSize(candidateSection, &candidatesSize);CHKERRQ(ierr);
1111     ierr = PetscMalloc1(candidatesSize, &candidates);CHKERRQ(ierr);
1112     for (l = 0; l < numLeaves; ++l) {
1113       const PetscInt    localPoint = localPoints[l];
1114       const PetscInt   *support;
1115       PetscInt          supportSize, s, offset, idx = 0;
1116 
1117       if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]  Checking local point %D\n", rank, localPoint);CHKERRQ(ierr);}
1118       ierr = PetscSectionGetOffset(candidateSection, localPoint, &offset);CHKERRQ(ierr);
1119       ierr = DMPlexGetSupportSize(dm, localPoint, &supportSize);CHKERRQ(ierr);
1120       ierr = DMPlexGetSupport(dm, localPoint, &support);CHKERRQ(ierr);
1121       for (s = 0; s < supportSize; ++s) {
1122         const PetscInt  face = support[s];
1123         const PetscInt *cone;
1124         PetscInt        coneSize, c, f, root;
1125         PetscBool       isFace = PETSC_TRUE;
1126 
1127         /* Only add face once */
1128         if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]    Support point %D\n", rank, face);CHKERRQ(ierr);}
1129         key.i = localPoint;
1130         key.j = face;
1131         ierr = PetscHMapIJGet(facehash, key, &f);CHKERRQ(ierr);
1132         if (f >= 0) continue;
1133         ierr = DMPlexGetConeSize(dm, face, &coneSize);CHKERRQ(ierr);
1134         ierr = DMPlexGetCone(dm, face, &cone);CHKERRQ(ierr);
1135         /* If a cone point does not map to leaves on any proc, then do not put face in SF */
1136         for (c = 0; c < coneSize; ++c) {
1137           if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]      Cone point %D\n", rank, cone[c]);CHKERRQ(ierr);}
1138           ierr = PetscHMapIGet(leafhash, cone[c], &root);CHKERRQ(ierr);
1139           if (!rootdegree[cone[c]] && (root < 0)) {isFace = PETSC_FALSE; break;}
1140         }
1141         if (isFace) {
1142           if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]    Adding shared face %D at idx %D\n", rank, face, idx);CHKERRQ(ierr);}
1143           ierr = PetscHMapIJSet(facehash, key, l);CHKERRQ(ierr);
1144           candidates[offset+idx].rank    = -1;
1145           candidates[offset+idx++].index = coneSize-1;
1146           for (c = 0; c < coneSize; ++c) {
1147             if (cone[c] == localPoint) continue;
1148             if (rootdegree[cone[c]]) {
1149               candidates[offset+idx].rank    = rank;
1150               candidates[offset+idx++].index = cone[c];
1151             } else {
1152               ierr = PetscHMapIGet(leafhash, cone[c], &root);CHKERRQ(ierr);
1153               if (root < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Cannot locate local point %D in SF", cone[c]);
1154               candidates[offset+idx++] = remotePoints[root];
1155             }
1156           }
1157         }
1158       }
1159     }
1160     if (debug) {ierr = PetscSynchronizedFlush(PetscObjectComm((PetscObject) dm), NULL);CHKERRQ(ierr);}
1161     ierr = PetscHMapIJDestroy(&facehash);CHKERRQ(ierr);
1162     ierr = PetscObjectViewFromOptions((PetscObject) candidateSection, NULL, "-petscsection_interp_candidate_view");CHKERRQ(ierr);
1163     ierr = SFNodeArrayViewFromOptions(PetscObjectComm((PetscObject) dm), "-petscsection_interp_candidate_view", "Candidates", NULL, candidatesSize, candidates);CHKERRQ(ierr);
1164   }
1165   /* Gather candidate section / array pair into the root partition via inverse(multi(pointSF)). */
1166   /*   Note that this section is indexed by offsets into leaves, not by point number */
1167   {
1168     PetscSF   sfMulti, sfInverse, sfCandidates;
1169     PetscInt *remoteOffsets;
1170 
1171     ierr = PetscSFGetMultiSF(pointSF, &sfMulti);CHKERRQ(ierr);
1172     ierr = PetscSFCreateInverseSF(sfMulti, &sfInverse);CHKERRQ(ierr);
1173     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &candidateSectionRemote);CHKERRQ(ierr);
1174     ierr = PetscSFDistributeSection(sfInverse, candidateSection, &remoteOffsets, candidateSectionRemote);CHKERRQ(ierr);
1175     ierr = PetscSFCreateSectionSF(sfInverse, candidateSection, remoteOffsets, candidateSectionRemote, &sfCandidates);CHKERRQ(ierr);
1176     ierr = PetscSectionGetStorageSize(candidateSectionRemote, &candidatesRemoteSize);CHKERRQ(ierr);
1177     ierr = PetscMalloc1(candidatesRemoteSize, &candidatesRemote);CHKERRQ(ierr);
1178     ierr = PetscSFBcastBegin(sfCandidates, MPIU_2INT, candidates, candidatesRemote);CHKERRQ(ierr);
1179     ierr = PetscSFBcastEnd(sfCandidates, MPIU_2INT, candidates, candidatesRemote);CHKERRQ(ierr);
1180     ierr = PetscSFDestroy(&sfInverse);CHKERRQ(ierr);
1181     ierr = PetscSFDestroy(&sfCandidates);CHKERRQ(ierr);
1182     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
1183 
1184     ierr = PetscObjectViewFromOptions((PetscObject) candidateSectionRemote, NULL, "-petscsection_interp_candidate_remote_view");CHKERRQ(ierr);
1185     ierr = SFNodeArrayViewFromOptions(PetscObjectComm((PetscObject) dm), "-petscsection_interp_candidate_remote_view", "Remote Candidates", NULL, candidatesRemoteSize, candidatesRemote);CHKERRQ(ierr);
1186   }
1187   /* */
1188   {
1189     PetscInt idx;
1190     /* There is a section point for every leaf attached to a given root point */
1191     for (r = 0, idx = 0; r < numRoots; ++r) {
1192       PetscInt deg;
1193       for (deg = 0; deg < rootdegree[r]; ++deg, ++idx) {
1194         PetscInt offset, dof, d;
1195 
1196         ierr = PetscSectionGetDof(candidateSectionRemote, idx, &dof);CHKERRQ(ierr);
1197         ierr = PetscSectionGetOffset(candidateSectionRemote, idx, &offset);CHKERRQ(ierr);
1198         for (d = 0; d < dof; ++d) {
1199           const PetscInt  sizeInd   = offset+d;
1200           const PetscInt  numPoints = candidatesRemote[sizeInd].index;
1201           const PetscInt *join      = NULL;
1202           PetscInt        points[1024], p, joinSize;
1203 
1204           points[0] = r;
1205           for (p = 0; p < numPoints; ++p) {
1206             ierr = DMPlexMapToLocalPoint(roothash, localPoints, rank, candidatesRemote[offset+(++d)], &points[p+1]);
1207             if (ierr) {d += numPoints-1 - p; break;} /* We got a point not in our overlap */
1208             if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]  Checking local candidate %D\n", rank, points[p+1]);CHKERRQ(ierr);}
1209           }
1210           if (ierr) continue;
1211           ierr = DMPlexGetJoin(dm, numPoints+1, points, &joinSize, &join);CHKERRQ(ierr);
1212           if (joinSize == 1) {
1213             if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]    Adding face %D at idx %D\n", rank, join[0], sizeInd);CHKERRQ(ierr);}
1214             candidatesRemote[sizeInd].rank  = rank;
1215             candidatesRemote[sizeInd].index = join[0];
1216           }
1217           ierr = DMPlexRestoreJoin(dm, numPoints+1, points, &joinSize, &join);CHKERRQ(ierr);
1218         }
1219       }
1220     }
1221     if (debug) {ierr = PetscSynchronizedFlush(PetscObjectComm((PetscObject) dm), NULL);CHKERRQ(ierr);}
1222   }
1223   /* Push claims back to receiver via the MultiSF and derive new pointSF mapping on receiver */
1224   {
1225     PetscSF         sfMulti, sfClaims, sfPointNew;
1226     PetscSFNode    *remotePointsNew;
1227     PetscHMapI      claimshash;
1228     PetscInt       *remoteOffsets, *localPointsNew;
1229     PetscInt        claimsSize, pStart, pEnd, root, numLocalNew, p, d;
1230 
1231     ierr = PetscSFGetMultiSF(pointSF, &sfMulti);CHKERRQ(ierr);
1232     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &claimSection);CHKERRQ(ierr);
1233     ierr = PetscSFDistributeSection(sfMulti, candidateSectionRemote, &remoteOffsets, claimSection);CHKERRQ(ierr);
1234     ierr = PetscSFCreateSectionSF(sfMulti, candidateSectionRemote, remoteOffsets, claimSection, &sfClaims);CHKERRQ(ierr);
1235     ierr = PetscSectionGetStorageSize(claimSection, &claimsSize);CHKERRQ(ierr);
1236     ierr = PetscMalloc1(claimsSize, &claims);CHKERRQ(ierr);
1237     ierr = PetscSFBcastBegin(sfClaims, MPIU_2INT, candidatesRemote, claims);CHKERRQ(ierr);
1238     ierr = PetscSFBcastEnd(sfClaims, MPIU_2INT, candidatesRemote, claims);CHKERRQ(ierr);
1239     ierr = PetscSFDestroy(&sfClaims);CHKERRQ(ierr);
1240     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
1241     ierr = PetscObjectViewFromOptions((PetscObject) claimSection, NULL, "-petscsection_interp_claim_view");CHKERRQ(ierr);
1242     ierr = SFNodeArrayViewFromOptions(PetscObjectComm((PetscObject) dm), "-petscsection_interp_claim_view", "Claims", NULL, claimsSize, claims);CHKERRQ(ierr);
1243     /* Walk the original section of local supports and add an SF entry for each updated item */
1244     ierr = PetscHMapICreate(&claimshash);CHKERRQ(ierr);
1245     for (p = 0; p < numRoots; ++p) {
1246       PetscInt dof, offset;
1247 
1248       if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]  Checking root for claims %D\n", rank, p);CHKERRQ(ierr);}
1249       ierr = PetscSectionGetDof(candidateSection, p, &dof);CHKERRQ(ierr);
1250       ierr = PetscSectionGetOffset(candidateSection, p, &offset);CHKERRQ(ierr);
1251       for (d = 0; d < dof;) {
1252         if (claims[offset+d].rank >= 0) {
1253           const PetscInt  faceInd   = offset+d;
1254           const PetscInt  numPoints = candidates[faceInd].index;
1255           const PetscInt *join      = NULL;
1256           PetscInt        joinSize, points[1024], c;
1257 
1258           if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]    Found claim for remote point (%D, %D)\n", rank, claims[faceInd].rank, claims[faceInd].index);CHKERRQ(ierr);}
1259           points[0] = p;
1260           if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]      point %D\n", rank, points[0]);CHKERRQ(ierr);}
1261           for (c = 0, ++d; c < numPoints; ++c, ++d) {
1262             key.i = candidates[offset+d].index;
1263             key.j = candidates[offset+d].rank;
1264             ierr = PetscHMapIJGet(roothash, key, &root);CHKERRQ(ierr);
1265             points[c+1] = localPoints[root];
1266             if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]      point %D\n", rank, points[c+1]);CHKERRQ(ierr);}
1267           }
1268           ierr = DMPlexGetJoin(dm, numPoints+1, points, &joinSize, &join);CHKERRQ(ierr);
1269           if (joinSize == 1) {
1270             if (debug) {ierr = PetscSynchronizedPrintf(PetscObjectComm((PetscObject) dm), "[%d]    Found local face %D\n", rank, join[0]);CHKERRQ(ierr);}
1271             ierr = PetscHMapISet(claimshash, join[0], faceInd);CHKERRQ(ierr);
1272           }
1273           ierr = DMPlexRestoreJoin(dm, numPoints+1, points, &joinSize, &join);CHKERRQ(ierr);
1274         } else d += claims[offset+d].index+1;
1275       }
1276     }
1277     if (debug) {ierr = PetscSynchronizedFlush(PetscObjectComm((PetscObject) dm), NULL);CHKERRQ(ierr);}
1278     /* Create new pointSF from hashed claims */
1279     ierr = PetscHMapIGetSize(claimshash, &numLocalNew);CHKERRQ(ierr);
1280     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1281     ierr = PetscMalloc1(numLeaves + numLocalNew, &localPointsNew);CHKERRQ(ierr);
1282     ierr = PetscMalloc1(numLeaves + numLocalNew, &remotePointsNew);CHKERRQ(ierr);
1283     for (p = 0; p < numLeaves; ++p) {
1284       localPointsNew[p] = localPoints[p];
1285       remotePointsNew[p].index = remotePoints[p].index;
1286       remotePointsNew[p].rank  = remotePoints[p].rank;
1287     }
1288     p = numLeaves;
1289     ierr = PetscHMapIGetKeys(claimshash, &p, localPointsNew);CHKERRQ(ierr);
1290     ierr = PetscSortInt(numLocalNew, &localPointsNew[numLeaves]);CHKERRQ(ierr);
1291     for (p = numLeaves; p < numLeaves + numLocalNew; ++p) {
1292       PetscInt offset;
1293       ierr = PetscHMapIGet(claimshash, localPointsNew[p], &offset);CHKERRQ(ierr);
1294       remotePointsNew[p] = claims[offset];
1295     }
1296     ierr = PetscSFCreate(PetscObjectComm((PetscObject) dm), &sfPointNew);CHKERRQ(ierr);
1297     ierr = PetscSFSetGraph(sfPointNew, pEnd-pStart, numLeaves+numLocalNew, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER);CHKERRQ(ierr);
1298     ierr = DMSetPointSF(dm, sfPointNew);CHKERRQ(ierr);
1299     ierr = PetscSFDestroy(&sfPointNew);CHKERRQ(ierr);
1300     ierr = PetscHMapIDestroy(&claimshash);CHKERRQ(ierr);
1301   }
1302   ierr = PetscHMapIDestroy(&leafhash);CHKERRQ(ierr);
1303   ierr = PetscHMapIJDestroy(&roothash);CHKERRQ(ierr);
1304   ierr = PetscSectionDestroy(&candidateSection);CHKERRQ(ierr);
1305   ierr = PetscSectionDestroy(&candidateSectionRemote);CHKERRQ(ierr);
1306   ierr = PetscSectionDestroy(&claimSection);CHKERRQ(ierr);
1307   ierr = PetscFree(candidates);CHKERRQ(ierr);
1308   ierr = PetscFree(candidatesRemote);CHKERRQ(ierr);
1309   ierr = PetscFree(claims);CHKERRQ(ierr);
1310   ierr = PetscLogEventEnd(DMPLEX_InterpolateSF,dm,0,0,0);CHKERRQ(ierr);
1311   PetscFunctionReturn(0);
1312 }
1313 
1314 /*@C
1315   DMPlexInterpolate - Take in a cell-vertex mesh and return one with all intermediate faces, edges, etc.
1316 
1317   Collective on DM
1318 
1319   Input Parameters:
1320 + dm - The DMPlex object with only cells and vertices
1321 - dmInt - The interpolated DM
1322 
1323   Output Parameter:
1324 . dmInt - The complete DMPlex object
1325 
1326   Level: intermediate
1327 
1328   Notes:
1329     It does not copy over the coordinates.
1330 
1331 .keywords: mesh
1332 .seealso: DMPlexUninterpolate(), DMPlexCreateFromCellList(), DMPlexCopyCoordinates()
1333 @*/
1334 PetscErrorCode DMPlexInterpolate(DM dm, DM *dmInt)
1335 {
1336   DM             idm, odm = dm;
1337   PetscSF        sfPoint;
1338   PetscInt       depth, dim, d;
1339   const char    *name;
1340   PetscBool      flg=PETSC_TRUE;
1341   PetscErrorCode ierr;
1342 
1343   PetscFunctionBegin;
1344   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1345   PetscValidPointer(dmInt, 2);
1346   ierr = PetscLogEventBegin(DMPLEX_Interpolate,dm,0,0,0);CHKERRQ(ierr);
1347   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1348   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1349   if ((depth == dim) || (dim <= 1)) {
1350     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
1351     idm  = dm;
1352   } else {
1353     for (d = 1; d < dim; ++d) {
1354       /* Create interpolated mesh */
1355       ierr = DMCreate(PetscObjectComm((PetscObject)dm), &idm);CHKERRQ(ierr);
1356       ierr = DMSetType(idm, DMPLEX);CHKERRQ(ierr);
1357       ierr = DMSetDimension(idm, dim);CHKERRQ(ierr);
1358       if (depth > 0) {
1359         ierr = DMPlexInterpolateFaces_Internal(odm, 1, idm);CHKERRQ(ierr);
1360         ierr = DMGetPointSF(odm, &sfPoint);CHKERRQ(ierr);
1361         ierr = DMPlexInterpolatePointSF(idm, sfPoint);CHKERRQ(ierr);
1362       }
1363       if (odm != dm) {ierr = DMDestroy(&odm);CHKERRQ(ierr);}
1364       odm = idm;
1365     }
1366     ierr = PetscObjectGetName((PetscObject) dm,  &name);CHKERRQ(ierr);
1367     ierr = PetscObjectSetName((PetscObject) idm,  name);CHKERRQ(ierr);
1368     ierr = DMPlexCopyCoordinates(dm, idm);CHKERRQ(ierr);
1369     ierr = DMCopyLabels(dm, idm);CHKERRQ(ierr);
1370     ierr = PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_interpolate_orient_interfaces", &flg, NULL);CHKERRQ(ierr);
1371     if (flg) {ierr = DMPlexOrientInterface(idm);CHKERRQ(ierr);}
1372   }
1373   {
1374     PetscBool            isper;
1375     const PetscReal      *maxCell, *L;
1376     const DMBoundaryType *bd;
1377 
1378     ierr = DMGetPeriodicity(dm,&isper,&maxCell,&L,&bd);CHKERRQ(ierr);
1379     ierr = DMSetPeriodicity(idm,isper,maxCell,L,bd);CHKERRQ(ierr);
1380   }
1381   *dmInt = idm;
1382   ierr = PetscLogEventEnd(DMPLEX_Interpolate,dm,0,0,0);CHKERRQ(ierr);
1383   PetscFunctionReturn(0);
1384 }
1385 
1386 /*@
1387   DMPlexCopyCoordinates - Copy coordinates from one mesh to another with the same vertices
1388 
1389   Collective on DM
1390 
1391   Input Parameter:
1392 . dmA - The DMPlex object with initial coordinates
1393 
1394   Output Parameter:
1395 . dmB - The DMPlex object with copied coordinates
1396 
1397   Level: intermediate
1398 
1399   Note: This is typically used when adding pieces other than vertices to a mesh
1400 
1401 .keywords: mesh
1402 .seealso: DMCopyLabels(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
1403 @*/
1404 PetscErrorCode DMPlexCopyCoordinates(DM dmA, DM dmB)
1405 {
1406   Vec            coordinatesA, coordinatesB;
1407   VecType        vtype;
1408   PetscSection   coordSectionA, coordSectionB;
1409   PetscScalar   *coordsA, *coordsB;
1410   PetscInt       spaceDim, Nf, vStartA, vStartB, vEndA, vEndB, coordSizeB, v, d;
1411   PetscInt       cStartA, cEndA, cStartB, cEndB, cS, cE;
1412   PetscBool      lc = PETSC_FALSE;
1413   PetscErrorCode ierr;
1414 
1415   PetscFunctionBegin;
1416   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
1417   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
1418   if (dmA == dmB) PetscFunctionReturn(0);
1419   ierr = DMPlexGetDepthStratum(dmA, 0, &vStartA, &vEndA);CHKERRQ(ierr);
1420   ierr = DMPlexGetDepthStratum(dmB, 0, &vStartB, &vEndB);CHKERRQ(ierr);
1421   if ((vEndA-vStartA) != (vEndB-vStartB)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of vertices in first DM %d != %d in the second DM", vEndA-vStartA, vEndB-vStartB);
1422   ierr = DMPlexGetHeightStratum(dmA, 0, &cStartA, &cEndA);CHKERRQ(ierr);
1423   ierr = DMPlexGetHeightStratum(dmB, 0, &cStartB, &cEndB);CHKERRQ(ierr);
1424   ierr = DMGetCoordinateSection(dmA, &coordSectionA);CHKERRQ(ierr);
1425   ierr = DMGetCoordinateSection(dmB, &coordSectionB);CHKERRQ(ierr);
1426   if (coordSectionA == coordSectionB) PetscFunctionReturn(0);
1427   ierr = PetscSectionGetNumFields(coordSectionA, &Nf);CHKERRQ(ierr);
1428   if (!Nf) PetscFunctionReturn(0);
1429   if (Nf > 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of coordinate fields must be 1, not %D", Nf);
1430   if (!coordSectionB) {
1431     PetscInt dim;
1432 
1433     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) coordSectionA), &coordSectionB);CHKERRQ(ierr);
1434     ierr = DMGetCoordinateDim(dmA, &dim);CHKERRQ(ierr);
1435     ierr = DMSetCoordinateSection(dmB, dim, coordSectionB);CHKERRQ(ierr);
1436     ierr = PetscObjectDereference((PetscObject) coordSectionB);CHKERRQ(ierr);
1437   }
1438   ierr = PetscSectionSetNumFields(coordSectionB, 1);CHKERRQ(ierr);
1439   ierr = PetscSectionGetFieldComponents(coordSectionA, 0, &spaceDim);CHKERRQ(ierr);
1440   ierr = PetscSectionSetFieldComponents(coordSectionB, 0, spaceDim);CHKERRQ(ierr);
1441   ierr = PetscSectionGetChart(coordSectionA, &cS, &cE);CHKERRQ(ierr);
1442   if (cStartA <= cS && cS < cEndA) { /* localized coordinates */
1443     if ((cEndA-cStartA) != (cEndB-cStartB)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cells in first DM %D != %D in the second DM", cEndA-cStartA, cEndB-cStartB);
1444     cS = cS - cStartA + cStartB;
1445     cE = vEndB;
1446     lc = PETSC_TRUE;
1447   } else {
1448     cS = vStartB;
1449     cE = vEndB;
1450   }
1451   ierr = PetscSectionSetChart(coordSectionB, cS, cE);CHKERRQ(ierr);
1452   for (v = vStartB; v < vEndB; ++v) {
1453     ierr = PetscSectionSetDof(coordSectionB, v, spaceDim);CHKERRQ(ierr);
1454     ierr = PetscSectionSetFieldDof(coordSectionB, v, 0, spaceDim);CHKERRQ(ierr);
1455   }
1456   if (lc) { /* localized coordinates */
1457     PetscInt c;
1458 
1459     for (c = cS-cStartB; c < cEndB-cStartB; c++) {
1460       PetscInt dof;
1461 
1462       ierr = PetscSectionGetDof(coordSectionA, c + cStartA, &dof);CHKERRQ(ierr);
1463       ierr = PetscSectionSetDof(coordSectionB, c + cStartB, dof);CHKERRQ(ierr);
1464       ierr = PetscSectionSetFieldDof(coordSectionB, c + cStartB, 0, dof);CHKERRQ(ierr);
1465     }
1466   }
1467   ierr = PetscSectionSetUp(coordSectionB);CHKERRQ(ierr);
1468   ierr = PetscSectionGetStorageSize(coordSectionB, &coordSizeB);CHKERRQ(ierr);
1469   ierr = DMGetCoordinatesLocal(dmA, &coordinatesA);CHKERRQ(ierr);
1470   ierr = VecCreate(PETSC_COMM_SELF, &coordinatesB);CHKERRQ(ierr);
1471   ierr = PetscObjectSetName((PetscObject) coordinatesB, "coordinates");CHKERRQ(ierr);
1472   ierr = VecSetSizes(coordinatesB, coordSizeB, PETSC_DETERMINE);CHKERRQ(ierr);
1473   ierr = VecGetBlockSize(coordinatesA, &d);CHKERRQ(ierr);
1474   ierr = VecSetBlockSize(coordinatesB, d);CHKERRQ(ierr);
1475   ierr = VecGetType(coordinatesA, &vtype);CHKERRQ(ierr);
1476   ierr = VecSetType(coordinatesB, vtype);CHKERRQ(ierr);
1477   ierr = VecGetArray(coordinatesA, &coordsA);CHKERRQ(ierr);
1478   ierr = VecGetArray(coordinatesB, &coordsB);CHKERRQ(ierr);
1479   for (v = 0; v < vEndB-vStartB; ++v) {
1480     PetscInt offA, offB;
1481 
1482     ierr = PetscSectionGetOffset(coordSectionA, v + vStartA, &offA);CHKERRQ(ierr);
1483     ierr = PetscSectionGetOffset(coordSectionB, v + vStartB, &offB);CHKERRQ(ierr);
1484     for (d = 0; d < spaceDim; ++d) {
1485       coordsB[offB+d] = coordsA[offA+d];
1486     }
1487   }
1488   if (lc) { /* localized coordinates */
1489     PetscInt c;
1490 
1491     for (c = cS-cStartB; c < cEndB-cStartB; c++) {
1492       PetscInt dof, offA, offB;
1493 
1494       ierr = PetscSectionGetOffset(coordSectionA, c + cStartA, &offA);CHKERRQ(ierr);
1495       ierr = PetscSectionGetOffset(coordSectionB, c + cStartB, &offB);CHKERRQ(ierr);
1496       ierr = PetscSectionGetDof(coordSectionA, c + cStartA, &dof);CHKERRQ(ierr);
1497       ierr = PetscMemcpy(coordsB + offB,coordsA + offA,dof*sizeof(*coordsB));CHKERRQ(ierr);
1498     }
1499   }
1500   ierr = VecRestoreArray(coordinatesA, &coordsA);CHKERRQ(ierr);
1501   ierr = VecRestoreArray(coordinatesB, &coordsB);CHKERRQ(ierr);
1502   ierr = DMSetCoordinatesLocal(dmB, coordinatesB);CHKERRQ(ierr);
1503   ierr = VecDestroy(&coordinatesB);CHKERRQ(ierr);
1504   PetscFunctionReturn(0);
1505 }
1506 
1507 /*@
1508   DMPlexUninterpolate - Take in a mesh with all intermediate faces, edges, etc. and return a cell-vertex mesh
1509 
1510   Collective on DM
1511 
1512   Input Parameter:
1513 . dm - The complete DMPlex object
1514 
1515   Output Parameter:
1516 . dmUnint - The DMPlex object with only cells and vertices
1517 
1518   Level: intermediate
1519 
1520   Notes:
1521     It does not copy over the coordinates.
1522 
1523 .keywords: mesh
1524 .seealso: DMPlexInterpolate(), DMPlexCreateFromCellList(), DMPlexCopyCoordinates()
1525 @*/
1526 PetscErrorCode DMPlexUninterpolate(DM dm, DM *dmUnint)
1527 {
1528   DM             udm;
1529   PetscInt       dim, vStart, vEnd, cStart, cEnd, cMax, c, maxConeSize = 0, *cone;
1530   PetscErrorCode ierr;
1531 
1532   PetscFunctionBegin;
1533   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1534   PetscValidPointer(dmUnint, 2);
1535   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1536   if (dim <= 1) {
1537     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
1538     *dmUnint = dm;
1539     PetscFunctionReturn(0);
1540   }
1541   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1542   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1543   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1544   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &udm);CHKERRQ(ierr);
1545   ierr = DMSetType(udm, DMPLEX);CHKERRQ(ierr);
1546   ierr = DMSetDimension(udm, dim);CHKERRQ(ierr);
1547   ierr = DMPlexSetChart(udm, cStart, vEnd);CHKERRQ(ierr);
1548   for (c = cStart; c < cEnd; ++c) {
1549     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
1550 
1551     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1552     for (cl = 0; cl < closureSize*2; cl += 2) {
1553       const PetscInt p = closure[cl];
1554 
1555       if ((p >= vStart) && (p < vEnd)) ++coneSize;
1556     }
1557     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1558     ierr = DMPlexSetConeSize(udm, c, coneSize);CHKERRQ(ierr);
1559     maxConeSize = PetscMax(maxConeSize, coneSize);
1560   }
1561   ierr = DMSetUp(udm);CHKERRQ(ierr);
1562   ierr = PetscMalloc1(maxConeSize, &cone);CHKERRQ(ierr);
1563   for (c = cStart; c < cEnd; ++c) {
1564     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
1565 
1566     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1567     for (cl = 0; cl < closureSize*2; cl += 2) {
1568       const PetscInt p = closure[cl];
1569 
1570       if ((p >= vStart) && (p < vEnd)) cone[coneSize++] = p;
1571     }
1572     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1573     ierr = DMPlexSetCone(udm, c, cone);CHKERRQ(ierr);
1574   }
1575   ierr = PetscFree(cone);CHKERRQ(ierr);
1576   ierr = DMPlexSetHybridBounds(udm, cMax, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1577   ierr = DMPlexSymmetrize(udm);CHKERRQ(ierr);
1578   ierr = DMPlexStratify(udm);CHKERRQ(ierr);
1579   /* Reduce SF */
1580   {
1581     PetscSF            sfPoint, sfPointUn;
1582     const PetscSFNode *remotePoints;
1583     const PetscInt    *localPoints;
1584     PetscSFNode       *remotePointsUn;
1585     PetscInt          *localPointsUn;
1586     PetscInt           vEnd, numRoots, numLeaves, l;
1587     PetscInt           numLeavesUn = 0, n = 0;
1588     PetscErrorCode     ierr;
1589 
1590     /* Get original SF information */
1591     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
1592     ierr = DMGetPointSF(udm, &sfPointUn);CHKERRQ(ierr);
1593     ierr = DMPlexGetDepthStratum(dm, 0, NULL, &vEnd);CHKERRQ(ierr);
1594     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
1595     /* Allocate space for cells and vertices */
1596     for (l = 0; l < numLeaves; ++l) if (localPoints[l] < vEnd) numLeavesUn++;
1597     /* Fill in leaves */
1598     if (vEnd >= 0) {
1599       ierr = PetscMalloc1(numLeavesUn, &remotePointsUn);CHKERRQ(ierr);
1600       ierr = PetscMalloc1(numLeavesUn, &localPointsUn);CHKERRQ(ierr);
1601       for (l = 0; l < numLeaves; l++) {
1602         if (localPoints[l] < vEnd) {
1603           localPointsUn[n]        = localPoints[l];
1604           remotePointsUn[n].rank  = remotePoints[l].rank;
1605           remotePointsUn[n].index = remotePoints[l].index;
1606           ++n;
1607         }
1608       }
1609       if (n != numLeavesUn) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent number of leaves %d != %d", n, numLeavesUn);
1610       ierr = PetscSFSetGraph(sfPointUn, vEnd, numLeavesUn, localPointsUn, PETSC_OWN_POINTER, remotePointsUn, PETSC_OWN_POINTER);CHKERRQ(ierr);
1611     }
1612   }
1613   {
1614     PetscBool            isper;
1615     const PetscReal      *maxCell, *L;
1616     const DMBoundaryType *bd;
1617 
1618     ierr = DMGetPeriodicity(dm,&isper,&maxCell,&L,&bd);CHKERRQ(ierr);
1619     ierr = DMSetPeriodicity(udm,isper,maxCell,L,bd);CHKERRQ(ierr);
1620   }
1621 
1622   *dmUnint = udm;
1623   PetscFunctionReturn(0);
1624 }
1625