xref: /petsc/src/dm/impls/plex/plexorient.c (revision feff33ee0b5b037fa8f9f294dede656a2f85cc47)
1 #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2 #include <petscsf.h>
3 
4 /*@
5   DMPlexReverseCell - Give a mesh cell the opposite orientation
6 
7   Input Parameters:
8 + dm   - The DM
9 - cell - The cell number
10 
11   Note: The modification of the DM is done in-place.
12 
13   Level: advanced
14 
15 .seealso: DMPlexOrient(), DMCreate(), DMPLEX
16 @*/
17 PetscErrorCode DMPlexReverseCell(DM dm, PetscInt cell)
18 {
19   /* Note that the reverse orientation ro of a face with orientation o is:
20 
21        ro = o >= 0 ? -(faceSize - o) : faceSize + o
22 
23      where faceSize is the size of the cone for the face.
24   */
25   const PetscInt *cone,    *coneO, *support;
26   PetscInt       *revcone, *revconeO;
27   PetscInt        maxConeSize, coneSize, supportSize, faceSize, cp, sp;
28   PetscErrorCode  ierr;
29 
30   PetscFunctionBegin;
31   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
32   ierr = DMGetWorkArray(dm, maxConeSize, MPIU_INT, &revcone);CHKERRQ(ierr);
33   ierr = DMGetWorkArray(dm, maxConeSize, MPIU_INT, &revconeO);CHKERRQ(ierr);
34   /* Reverse cone, and reverse orientations of faces */
35   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
36   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
37   ierr = DMPlexGetConeOrientation(dm, cell, &coneO);CHKERRQ(ierr);
38   for (cp = 0; cp < coneSize; ++cp) {
39     const PetscInt rcp = coneSize-cp-1;
40 
41     ierr = DMPlexGetConeSize(dm, cone[rcp], &faceSize);CHKERRQ(ierr);
42     revcone[cp]  = cone[rcp];
43     revconeO[cp] = coneO[rcp] >= 0 ? -(faceSize-coneO[rcp]) : faceSize+coneO[rcp];
44   }
45   ierr = DMPlexSetCone(dm, cell, revcone);CHKERRQ(ierr);
46   ierr = DMPlexSetConeOrientation(dm, cell, revconeO);CHKERRQ(ierr);
47   /* Reverse orientation of this cell in the support hypercells */
48   faceSize = coneSize;
49   ierr = DMPlexGetSupportSize(dm, cell, &supportSize);CHKERRQ(ierr);
50   ierr = DMPlexGetSupport(dm, cell, &support);CHKERRQ(ierr);
51   for (sp = 0; sp < supportSize; ++sp) {
52     ierr = DMPlexGetConeSize(dm, support[sp], &coneSize);CHKERRQ(ierr);
53     ierr = DMPlexGetCone(dm, support[sp], &cone);CHKERRQ(ierr);
54     ierr = DMPlexGetConeOrientation(dm, support[sp], &coneO);CHKERRQ(ierr);
55     for (cp = 0; cp < coneSize; ++cp) {
56       if (cone[cp] != cell) continue;
57       ierr = DMPlexInsertConeOrientation(dm, support[sp], cp, coneO[cp] >= 0 ? -(faceSize-coneO[cp]) : faceSize+coneO[cp]);CHKERRQ(ierr);
58     }
59   }
60   ierr = DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &revcone);CHKERRQ(ierr);
61   ierr = DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &revconeO);CHKERRQ(ierr);
62   PetscFunctionReturn(0);
63 }
64 
65 /*
66   - Checks face match
67     - Flips non-matching
68   - Inserts faces of support cells in FIFO
69 */
70 static PetscErrorCode DMPlexCheckFace_Internal(DM dm, PetscInt *faceFIFO, PetscInt *fTop, PetscInt *fBottom, PetscInt cStart, PetscInt fStart, PetscInt fEnd, PetscBT seenCells, PetscBT flippedCells, PetscBT seenFaces)
71 {
72   const PetscInt *support, *coneA, *coneB, *coneOA, *coneOB;
73   PetscInt        supportSize, coneSizeA, coneSizeB, posA = -1, posB = -1;
74   PetscInt        face, dim, seenA, flippedA, seenB, flippedB, mismatch, c;
75   PetscErrorCode  ierr;
76 
77   PetscFunctionBegin;
78   face = faceFIFO[(*fTop)++];
79   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
80   ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
81   ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
82   if (supportSize < 2) PetscFunctionReturn(0);
83   if (supportSize != 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Faces should separate only two cells, not %d", supportSize);
84   seenA    = PetscBTLookup(seenCells,    support[0]-cStart);
85   flippedA = PetscBTLookup(flippedCells, support[0]-cStart) ? 1 : 0;
86   seenB    = PetscBTLookup(seenCells,    support[1]-cStart);
87   flippedB = PetscBTLookup(flippedCells, support[1]-cStart) ? 1 : 0;
88 
89   ierr = DMPlexGetConeSize(dm, support[0], &coneSizeA);CHKERRQ(ierr);
90   ierr = DMPlexGetConeSize(dm, support[1], &coneSizeB);CHKERRQ(ierr);
91   ierr = DMPlexGetCone(dm, support[0], &coneA);CHKERRQ(ierr);
92   ierr = DMPlexGetCone(dm, support[1], &coneB);CHKERRQ(ierr);
93   ierr = DMPlexGetConeOrientation(dm, support[0], &coneOA);CHKERRQ(ierr);
94   ierr = DMPlexGetConeOrientation(dm, support[1], &coneOB);CHKERRQ(ierr);
95   for (c = 0; c < coneSizeA; ++c) {
96     if (!PetscBTLookup(seenFaces, coneA[c]-fStart)) {
97       faceFIFO[(*fBottom)++] = coneA[c];
98       ierr = PetscBTSet(seenFaces, coneA[c]-fStart);CHKERRQ(ierr);
99     }
100     if (coneA[c] == face) posA = c;
101     if (*fBottom > fEnd-fStart) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Face %d was pushed exceeding capacity %d > %d", coneA[c], *fBottom, fEnd-fStart);
102   }
103   if (posA < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d could not be located in cell %d", face, support[0]);
104   for (c = 0; c < coneSizeB; ++c) {
105     if (!PetscBTLookup(seenFaces, coneB[c]-fStart)) {
106       faceFIFO[(*fBottom)++] = coneB[c];
107       ierr = PetscBTSet(seenFaces, coneB[c]-fStart);CHKERRQ(ierr);
108     }
109     if (coneB[c] == face) posB = c;
110     if (*fBottom > fEnd-fStart) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Face %d was pushed exceeding capacity %d > %d", coneA[c], *fBottom, fEnd-fStart);
111   }
112   if (posB < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d could not be located in cell %d", face, support[1]);
113 
114   if (dim == 1) {
115     mismatch = posA == posB;
116   } else {
117     mismatch = coneOA[posA] == coneOB[posB];
118   }
119 
120   if (mismatch ^ (flippedA ^ flippedB)) {
121     if (seenA && seenB) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Previously seen cells %d and %d do not match: Fault mesh is non-orientable", support[0], support[1]);
122     if (!seenA && !flippedA) {
123       ierr = PetscBTSet(flippedCells, support[0]-cStart);CHKERRQ(ierr);
124     } else if (!seenB && !flippedB) {
125       ierr = PetscBTSet(flippedCells, support[1]-cStart);CHKERRQ(ierr);
126     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent mesh orientation: Fault mesh is non-orientable");
127   } else if (mismatch && flippedA && flippedB) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Attempt to flip already flipped cell: Fault mesh is non-orientable");
128   ierr = PetscBTSet(seenCells, support[0]-cStart);CHKERRQ(ierr);
129   ierr = PetscBTSet(seenCells, support[1]-cStart);CHKERRQ(ierr);
130   PetscFunctionReturn(0);
131 }
132 
133 /*@
134   DMPlexOrient - Give a consistent orientation to the input mesh
135 
136   Input Parameters:
137 . dm - The DM
138 
139   Note: The orientation data for the DM are change in-place.
140 $ This routine will fail for non-orientable surfaces, such as the Moebius strip.
141 
142   Level: advanced
143 
144 .seealso: DMCreate(), DMPLEX
145 @*/
146 PetscErrorCode DMPlexOrient(DM dm)
147 {
148   MPI_Comm           comm;
149   PetscSF            sf;
150   const PetscInt    *lpoints;
151   const PetscSFNode *rpoints;
152   PetscSFNode       *rorntComp = NULL, *lorntComp = NULL;
153   PetscInt          *numNeighbors, **neighbors;
154   PetscSFNode       *nrankComp;
155   PetscBool         *match, *flipped;
156   PetscBT            seenCells, flippedCells, seenFaces;
157   PetscInt          *faceFIFO, fTop, fBottom, *cellComp, *faceComp;
158   PetscInt           numLeaves, numRoots, dim, h, cStart, cEnd, c, cell, fStart, fEnd, face, off, totNeighbors = 0;
159   PetscMPIInt        rank, size, numComponents, comp = 0;
160   PetscBool          flg, flg2;
161   PetscViewer        viewer = NULL, selfviewer = NULL;
162   PetscErrorCode     ierr;
163 
164   PetscFunctionBegin;
165   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
166   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
167   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
168   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-orientation_view", &flg);CHKERRQ(ierr);
169   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-orientation_view_synchronized", &flg2);CHKERRQ(ierr);
170   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
171   ierr = PetscSFGetGraph(sf, &numRoots, &numLeaves, &lpoints, &rpoints);CHKERRQ(ierr);
172   /* Truth Table
173      mismatch    flips   do action   mismatch   flipA ^ flipB   action
174          F       0 flips     no         F             F           F
175          F       1 flip      yes        F             T           T
176          F       2 flips     no         T             F           T
177          T       0 flips     yes        T             T           F
178          T       1 flip      no
179          T       2 flips     yes
180   */
181   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
182   ierr = DMPlexGetVTKCellHeight(dm, &h);CHKERRQ(ierr);
183   ierr = DMPlexGetHeightStratum(dm, h,   &cStart, &cEnd);CHKERRQ(ierr);
184   ierr = DMPlexGetHeightStratum(dm, h+1, &fStart, &fEnd);CHKERRQ(ierr);
185   ierr = PetscBTCreate(cEnd - cStart, &seenCells);CHKERRQ(ierr);
186   ierr = PetscBTMemzero(cEnd - cStart, seenCells);CHKERRQ(ierr);
187   ierr = PetscBTCreate(cEnd - cStart, &flippedCells);CHKERRQ(ierr);
188   ierr = PetscBTMemzero(cEnd - cStart, flippedCells);CHKERRQ(ierr);
189   ierr = PetscBTCreate(fEnd - fStart, &seenFaces);CHKERRQ(ierr);
190   ierr = PetscBTMemzero(fEnd - fStart, seenFaces);CHKERRQ(ierr);
191   ierr = PetscCalloc3(fEnd - fStart, &faceFIFO, cEnd-cStart, &cellComp, fEnd-fStart, &faceComp);CHKERRQ(ierr);
192   /*
193    OLD STYLE
194    - Add an integer array over cells and faces (component) for connected component number
195    Foreach component
196      - Mark the initial cell as seen
197      - Process component as usual
198      - Set component for all seenCells
199      - Wipe seenCells and seenFaces (flippedCells can stay)
200    - Generate parallel adjacency for component using SF and seenFaces
201    - Collect numComponents adj data from each proc to 0
202    - Build same serial graph
203    - Use same solver
204    - Use Scatterv to to send back flipped flags for each component
205    - Negate flippedCells by component
206 
207    NEW STYLE
208    - Create the adj on each process
209    - Bootstrap to complete graph on proc 0
210   */
211   /* Loop over components */
212   for (cell = cStart; cell < cEnd; ++cell) cellComp[cell-cStart] = -1;
213   do {
214     /* Look for first unmarked cell */
215     for (cell = cStart; cell < cEnd; ++cell) if (cellComp[cell-cStart] < 0) break;
216     if (cell >= cEnd) break;
217     /* Initialize FIFO with first cell in component */
218     {
219       const PetscInt *cone;
220       PetscInt        coneSize;
221 
222       fTop = fBottom = 0;
223       ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
224       ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
225       for (c = 0; c < coneSize; ++c) {
226         faceFIFO[fBottom++] = cone[c];
227         ierr = PetscBTSet(seenFaces, cone[c]-fStart);CHKERRQ(ierr);
228       }
229       ierr = PetscBTSet(seenCells, cell-cStart);CHKERRQ(ierr);
230     }
231     /* Consider each face in FIFO */
232     while (fTop < fBottom) {
233       ierr = DMPlexCheckFace_Internal(dm, faceFIFO, &fTop, &fBottom, cStart, fStart, fEnd, seenCells, flippedCells, seenFaces);CHKERRQ(ierr);
234     }
235     /* Set component for cells and faces */
236     for (cell = 0; cell < cEnd-cStart; ++cell) {
237       if (PetscBTLookup(seenCells, cell)) cellComp[cell] = comp;
238     }
239     for (face = 0; face < fEnd-fStart; ++face) {
240       if (PetscBTLookup(seenFaces, face)) faceComp[face] = comp;
241     }
242     /* Wipe seenCells and seenFaces for next component */
243     ierr = PetscBTMemzero(fEnd - fStart, seenFaces);CHKERRQ(ierr);
244     ierr = PetscBTMemzero(cEnd - cStart, seenCells);CHKERRQ(ierr);
245     ++comp;
246   } while (1);
247   numComponents = comp;
248   if (flg) {
249     PetscViewer v;
250 
251     ierr = PetscViewerASCIIGetStdout(comm, &v);CHKERRQ(ierr);
252     ierr = PetscViewerASCIIPushSynchronized(v);CHKERRQ(ierr);
253     ierr = PetscViewerASCIISynchronizedPrintf(v, "[%d]BT for serial flipped cells:\n", rank);CHKERRQ(ierr);
254     ierr = PetscBTView(cEnd-cStart, flippedCells, v);CHKERRQ(ierr);
255     ierr = PetscViewerFlush(v);CHKERRQ(ierr);
256     ierr = PetscViewerASCIIPopSynchronized(v);CHKERRQ(ierr);
257   }
258   /* Now all subdomains are oriented, but we need a consistent parallel orientation */
259   if (numLeaves >= 0) {
260     /* Store orientations of boundary faces*/
261     ierr = PetscCalloc2(numRoots,&rorntComp,numRoots,&lorntComp);CHKERRQ(ierr);
262     for (face = fStart; face < fEnd; ++face) {
263       const PetscInt *cone, *support, *ornt;
264       PetscInt        coneSize, supportSize;
265 
266       ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
267       if (supportSize != 1) continue;
268       ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
269 
270       ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
271       ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
272       ierr = DMPlexGetConeOrientation(dm, support[0], &ornt);CHKERRQ(ierr);
273       for (c = 0; c < coneSize; ++c) if (cone[c] == face) break;
274       if (dim == 1) {
275         /* Use cone position instead, shifted to -1 or 1 */
276         if (PetscBTLookup(flippedCells, support[0]-cStart)) rorntComp[face].rank = 1-c*2;
277         else                                                rorntComp[face].rank = c*2-1;
278       } else {
279         if (PetscBTLookup(flippedCells, support[0]-cStart)) rorntComp[face].rank = ornt[c] < 0 ? -1 :  1;
280         else                                                rorntComp[face].rank = ornt[c] < 0 ?  1 : -1;
281       }
282       rorntComp[face].index = faceComp[face-fStart];
283     }
284     /* Communicate boundary edge orientations */
285     ierr = PetscSFBcastBegin(sf, MPIU_2INT, rorntComp, lorntComp);CHKERRQ(ierr);
286     ierr = PetscSFBcastEnd(sf, MPIU_2INT, rorntComp, lorntComp);CHKERRQ(ierr);
287   }
288   /* Get process adjacency */
289   ierr = PetscMalloc2(numComponents, &numNeighbors, numComponents, &neighbors);CHKERRQ(ierr);
290   viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm));
291   if (flg2) {ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);}
292   ierr = PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&selfviewer);CHKERRQ(ierr);
293   for (comp = 0; comp < numComponents; ++comp) {
294     PetscInt  l, n;
295 
296     numNeighbors[comp] = 0;
297     ierr = PetscMalloc1(PetscMax(numLeaves, 0), &neighbors[comp]);CHKERRQ(ierr);
298     /* I know this is p^2 time in general, but for bounded degree its alright */
299     for (l = 0; l < numLeaves; ++l) {
300       const PetscInt face = lpoints[l];
301 
302       /* Find a representative face (edge) separating pairs of procs */
303       if ((face >= fStart) && (face < fEnd) && (faceComp[face-fStart] == comp)) {
304         const PetscInt rrank = rpoints[l].rank;
305         const PetscInt rcomp = lorntComp[face].index;
306 
307         for (n = 0; n < numNeighbors[comp]; ++n) if ((rrank == rpoints[neighbors[comp][n]].rank) && (rcomp == lorntComp[lpoints[neighbors[comp][n]]].index)) break;
308         if (n >= numNeighbors[comp]) {
309           PetscInt supportSize;
310 
311           ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
312           if (supportSize != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Boundary faces should see one cell, not %d", supportSize);
313           if (flg) {ierr = PetscViewerASCIIPrintf(selfviewer, "[%d]: component %d, Found representative leaf %d (face %d) connecting to face %d on (%d, %d) with orientation %d\n", rank, comp, l, face, rpoints[l].index, rrank, rcomp, lorntComp[face].rank);CHKERRQ(ierr);}
314           neighbors[comp][numNeighbors[comp]++] = l;
315         }
316       }
317     }
318     totNeighbors += numNeighbors[comp];
319   }
320   ierr = PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&selfviewer);CHKERRQ(ierr);
321   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
322   if (flg2) {ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);}
323   ierr = PetscMalloc2(totNeighbors, &nrankComp, totNeighbors, &match);CHKERRQ(ierr);
324   for (comp = 0, off = 0; comp < numComponents; ++comp) {
325     PetscInt n;
326 
327     for (n = 0; n < numNeighbors[comp]; ++n, ++off) {
328       const PetscInt face = lpoints[neighbors[comp][n]];
329       const PetscInt o    = rorntComp[face].rank*lorntComp[face].rank;
330 
331       if      (o < 0) match[off] = PETSC_TRUE;
332       else if (o > 0) match[off] = PETSC_FALSE;
333       else SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid face %d (%d, %d) neighbor: %d comp: %d", face, rorntComp[face], lorntComp[face], neighbors[comp][n], comp);
334       nrankComp[off].rank  = rpoints[neighbors[comp][n]].rank;
335       nrankComp[off].index = lorntComp[lpoints[neighbors[comp][n]]].index;
336     }
337     ierr = PetscFree(neighbors[comp]);CHKERRQ(ierr);
338   }
339   /* Collect the graph on 0 */
340   if (numLeaves >= 0) {
341     Mat          G;
342     PetscBT      seenProcs, flippedProcs;
343     PetscInt    *procFIFO, pTop, pBottom;
344     PetscInt    *N   = NULL, *Noff;
345     PetscSFNode *adj = NULL;
346     PetscBool   *val = NULL;
347     PetscMPIInt *recvcounts = NULL, *displs = NULL, *Nc, p, o;
348     PetscMPIInt  size = 0;
349 
350     ierr = PetscCalloc1(numComponents, &flipped);CHKERRQ(ierr);
351     if (!rank) {ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);}
352     ierr = PetscCalloc4(size, &recvcounts, size+1, &displs, size, &Nc, size+1, &Noff);CHKERRQ(ierr);
353     ierr = MPI_Gather(&numComponents, 1, MPI_INT, Nc, 1, MPI_INT, 0, comm);CHKERRQ(ierr);
354     for (p = 0; p < size; ++p) {
355       displs[p+1] = displs[p] + Nc[p];
356     }
357     if (!rank) {ierr = PetscMalloc1(displs[size],&N);CHKERRQ(ierr);}
358     ierr = MPI_Gatherv(numNeighbors, numComponents, MPIU_INT, N, Nc, displs, MPIU_INT, 0, comm);CHKERRQ(ierr);
359     for (p = 0, o = 0; p < size; ++p) {
360       recvcounts[p] = 0;
361       for (c = 0; c < Nc[p]; ++c, ++o) recvcounts[p] += N[o];
362       displs[p+1] = displs[p] + recvcounts[p];
363     }
364     if (!rank) {ierr = PetscMalloc2(displs[size], &adj, displs[size], &val);CHKERRQ(ierr);}
365     ierr = MPI_Gatherv(nrankComp, totNeighbors, MPIU_2INT, adj, recvcounts, displs, MPIU_2INT, 0, comm);CHKERRQ(ierr);
366     ierr = MPI_Gatherv(match, totNeighbors, MPIU_BOOL, val, recvcounts, displs, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
367     ierr = PetscFree2(numNeighbors, neighbors);CHKERRQ(ierr);
368     if (!rank) {
369       for (p = 1; p <= size; ++p) {Noff[p] = Noff[p-1] + Nc[p-1];}
370       if (flg) {
371         PetscInt n;
372 
373         for (p = 0, off = 0; p < size; ++p) {
374           for (c = 0; c < Nc[p]; ++c) {
375             ierr = PetscPrintf(PETSC_COMM_SELF, "Proc %d Comp %d:\n", p, c);CHKERRQ(ierr);
376             for (n = 0; n < N[Noff[p]+c]; ++n, ++off) {
377               ierr = PetscPrintf(PETSC_COMM_SELF, "  edge (%d, %d) (%d):\n", adj[off].rank, adj[off].index, val[off]);CHKERRQ(ierr);
378             }
379           }
380         }
381       }
382       /* Symmetrize the graph */
383       ierr = MatCreate(PETSC_COMM_SELF, &G);CHKERRQ(ierr);
384       ierr = MatSetSizes(G, Noff[size], Noff[size], Noff[size], Noff[size]);CHKERRQ(ierr);
385       ierr = MatSetUp(G);CHKERRQ(ierr);
386       for (p = 0, off = 0; p < size; ++p) {
387         for (c = 0; c < Nc[p]; ++c) {
388           const PetscInt r = Noff[p]+c;
389           PetscInt       n;
390 
391           for (n = 0; n < N[r]; ++n, ++off) {
392             const PetscInt    q = Noff[adj[off].rank] + adj[off].index;
393             const PetscScalar o = val[off] ? 1.0 : 0.0;
394 
395             ierr = MatSetValues(G, 1, &r, 1, &q, &o, INSERT_VALUES);CHKERRQ(ierr);
396             ierr = MatSetValues(G, 1, &q, 1, &r, &o, INSERT_VALUES);CHKERRQ(ierr);
397           }
398         }
399       }
400       ierr = MatAssemblyBegin(G, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
401       ierr = MatAssemblyEnd(G, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
402 
403       ierr = PetscBTCreate(Noff[size], &seenProcs);CHKERRQ(ierr);
404       ierr = PetscBTMemzero(Noff[size], seenProcs);CHKERRQ(ierr);
405       ierr = PetscBTCreate(Noff[size], &flippedProcs);CHKERRQ(ierr);
406       ierr = PetscBTMemzero(Noff[size], flippedProcs);CHKERRQ(ierr);
407       ierr = PetscMalloc1(Noff[size], &procFIFO);CHKERRQ(ierr);
408       pTop = pBottom = 0;
409       for (p = 0; p < Noff[size]; ++p) {
410         if (PetscBTLookup(seenProcs, p)) continue;
411         /* Initialize FIFO with next proc */
412         procFIFO[pBottom++] = p;
413         ierr = PetscBTSet(seenProcs, p);CHKERRQ(ierr);
414         /* Consider each proc in FIFO */
415         while (pTop < pBottom) {
416           const PetscScalar *ornt;
417           const PetscInt    *neighbors;
418           PetscInt           proc, nproc, seen, flippedA, flippedB, mismatch, numNeighbors, n;
419 
420           proc     = procFIFO[pTop++];
421           flippedA = PetscBTLookup(flippedProcs, proc) ? 1 : 0;
422           ierr = MatGetRow(G, proc, &numNeighbors, &neighbors, &ornt);CHKERRQ(ierr);
423           /* Loop over neighboring procs */
424           for (n = 0; n < numNeighbors; ++n) {
425             nproc    = neighbors[n];
426             mismatch = PetscRealPart(ornt[n]) > 0.5 ? 0 : 1;
427             seen     = PetscBTLookup(seenProcs, nproc);
428             flippedB = PetscBTLookup(flippedProcs, nproc) ? 1 : 0;
429 
430             if (mismatch ^ (flippedA ^ flippedB)) {
431               if (seen) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Previously seen procs %d and %d do not match: Fault mesh is non-orientable", proc, nproc);
432               if (!flippedB) {
433                 ierr = PetscBTSet(flippedProcs, nproc);CHKERRQ(ierr);
434               } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent mesh orientation: Fault mesh is non-orientable");
435             } else if (mismatch && flippedA && flippedB) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Attempt to flip already flipped cell: Fault mesh is non-orientable");
436             if (!seen) {
437               procFIFO[pBottom++] = nproc;
438               ierr = PetscBTSet(seenProcs, nproc);CHKERRQ(ierr);
439             }
440           }
441         }
442       }
443       ierr = PetscFree(procFIFO);CHKERRQ(ierr);
444       ierr = MatDestroy(&G);CHKERRQ(ierr);
445       ierr = PetscFree2(adj, val);CHKERRQ(ierr);
446       ierr = PetscBTDestroy(&seenProcs);CHKERRQ(ierr);
447     }
448     /* Scatter flip flags */
449     {
450       PetscBool *flips = NULL;
451 
452       if (!rank) {
453         ierr = PetscMalloc1(Noff[size], &flips);CHKERRQ(ierr);
454         for (p = 0; p < Noff[size]; ++p) {
455           flips[p] = PetscBTLookup(flippedProcs, p) ? PETSC_TRUE : PETSC_FALSE;
456           if (flg && flips[p]) {ierr = PetscPrintf(comm, "Flipping Proc+Comp %d:\n", p);CHKERRQ(ierr);}
457         }
458         for (p = 0; p < size; ++p) {
459           displs[p+1] = displs[p] + Nc[p];
460         }
461       }
462       ierr = MPI_Scatterv(flips, Nc, displs, MPIU_BOOL, flipped, numComponents, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
463       ierr = PetscFree(flips);CHKERRQ(ierr);
464     }
465     if (!rank) {ierr = PetscBTDestroy(&flippedProcs);CHKERRQ(ierr);}
466     ierr = PetscFree(N);CHKERRQ(ierr);
467     ierr = PetscFree4(recvcounts, displs, Nc, Noff);CHKERRQ(ierr);
468     ierr = PetscFree2(nrankComp, match);CHKERRQ(ierr);
469 
470     /* Decide whether to flip cells in each component */
471     for (c = 0; c < cEnd-cStart; ++c) {if (flipped[cellComp[c]]) {ierr = PetscBTNegate(flippedCells, c);CHKERRQ(ierr);}}
472     ierr = PetscFree(flipped);CHKERRQ(ierr);
473   }
474   if (flg) {
475     PetscViewer v;
476 
477     ierr = PetscViewerASCIIGetStdout(comm, &v);CHKERRQ(ierr);
478     ierr = PetscViewerASCIIPushSynchronized(v);CHKERRQ(ierr);
479     ierr = PetscViewerASCIISynchronizedPrintf(v, "[%d]BT for parallel flipped cells:\n", rank);CHKERRQ(ierr);
480     ierr = PetscBTView(cEnd-cStart, flippedCells, v);CHKERRQ(ierr);
481     ierr = PetscViewerFlush(v);CHKERRQ(ierr);
482     ierr = PetscViewerASCIIPopSynchronized(v);CHKERRQ(ierr);
483   }
484   /* Reverse flipped cells in the mesh */
485   for (c = cStart; c < cEnd; ++c) {
486     if (PetscBTLookup(flippedCells, c-cStart)) {
487       ierr = DMPlexReverseCell(dm, c);CHKERRQ(ierr);
488     }
489   }
490   ierr = PetscBTDestroy(&seenCells);CHKERRQ(ierr);
491   ierr = PetscBTDestroy(&flippedCells);CHKERRQ(ierr);
492   ierr = PetscBTDestroy(&seenFaces);CHKERRQ(ierr);
493   ierr = PetscFree2(numNeighbors, neighbors);CHKERRQ(ierr);
494   ierr = PetscFree2(rorntComp, lorntComp);CHKERRQ(ierr);
495   ierr = PetscFree3(faceFIFO, cellComp, faceComp);CHKERRQ(ierr);
496   PetscFunctionReturn(0);
497 }
498