xref: /petsc/src/dm/impls/plex/plexorient.c (revision cffa905921890a9bfbe0ffaebbf089a995a915ae)
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, PETSC_INT, &revcone);CHKERRQ(ierr);
33   ierr = DMGetWorkArray(dm, maxConeSize, PETSC_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, PETSC_INT, &revcone);CHKERRQ(ierr);
61   ierr = DMRestoreWorkArray(dm, maxConeSize, PETSC_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, numComponents, comp = 0;
160   PetscBool          flg;
161   PetscErrorCode     ierr;
162 
163   PetscFunctionBegin;
164   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
165   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
166   ierr = PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-orientation_view", &flg);CHKERRQ(ierr);
167   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
168   ierr = PetscSFGetGraph(sf, &numRoots, &numLeaves, &lpoints, &rpoints);CHKERRQ(ierr);
169   /* Truth Table
170      mismatch    flips   do action   mismatch   flipA ^ flipB   action
171          F       0 flips     no         F             F           F
172          F       1 flip      yes        F             T           T
173          F       2 flips     no         T             F           T
174          T       0 flips     yes        T             T           F
175          T       1 flip      no
176          T       2 flips     yes
177   */
178   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
179   ierr = DMPlexGetVTKCellHeight(dm, &h);CHKERRQ(ierr);
180   ierr = DMPlexGetHeightStratum(dm, h,   &cStart, &cEnd);CHKERRQ(ierr);
181   ierr = DMPlexGetHeightStratum(dm, h+1, &fStart, &fEnd);CHKERRQ(ierr);
182   ierr = PetscBTCreate(cEnd - cStart, &seenCells);CHKERRQ(ierr);
183   ierr = PetscBTMemzero(cEnd - cStart, seenCells);CHKERRQ(ierr);
184   ierr = PetscBTCreate(cEnd - cStart, &flippedCells);CHKERRQ(ierr);
185   ierr = PetscBTMemzero(cEnd - cStart, flippedCells);CHKERRQ(ierr);
186   ierr = PetscBTCreate(fEnd - fStart, &seenFaces);CHKERRQ(ierr);
187   ierr = PetscBTMemzero(fEnd - fStart, seenFaces);CHKERRQ(ierr);
188   ierr = PetscCalloc3(fEnd - fStart, &faceFIFO, cEnd-cStart, &cellComp, fEnd-fStart, &faceComp);CHKERRQ(ierr);
189   /*
190    OLD STYLE
191    - Add an integer array over cells and faces (component) for connected component number
192    Foreach component
193      - Mark the initial cell as seen
194      - Process component as usual
195      - Set component for all seenCells
196      - Wipe seenCells and seenFaces (flippedCells can stay)
197    - Generate parallel adjacency for component using SF and seenFaces
198    - Collect numComponents adj data from each proc to 0
199    - Build same serial graph
200    - Use same solver
201    - Use Scatterv to to send back flipped flags for each component
202    - Negate flippedCells by component
203 
204    NEW STYLE
205    - Create the adj on each process
206    - Bootstrap to complete graph on proc 0
207   */
208   /* Loop over components */
209   for (cell = cStart; cell < cEnd; ++cell) cellComp[cell-cStart] = -1;
210   do {
211     /* Look for first unmarked cell */
212     for (cell = cStart; cell < cEnd; ++cell) if (cellComp[cell-cStart] < 0) break;
213     if (cell >= cEnd) break;
214     /* Initialize FIFO with first cell in component */
215     {
216       const PetscInt *cone;
217       PetscInt        coneSize;
218 
219       fTop = fBottom = 0;
220       ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
221       ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
222       for (c = 0; c < coneSize; ++c) {
223         faceFIFO[fBottom++] = cone[c];
224         ierr = PetscBTSet(seenFaces, cone[c]-fStart);CHKERRQ(ierr);
225       }
226       ierr = PetscBTSet(seenCells, cell-cStart);CHKERRQ(ierr);
227     }
228     /* Consider each face in FIFO */
229     while (fTop < fBottom) {
230       ierr = DMPlexCheckFace_Internal(dm, faceFIFO, &fTop, &fBottom, cStart, fStart, fEnd, seenCells, flippedCells, seenFaces);CHKERRQ(ierr);
231     }
232     /* Set component for cells and faces */
233     for (cell = 0; cell < cEnd-cStart; ++cell) {
234       if (PetscBTLookup(seenCells, cell)) cellComp[cell] = comp;
235     }
236     for (face = 0; face < fEnd-fStart; ++face) {
237       if (PetscBTLookup(seenFaces, face)) faceComp[face] = comp;
238     }
239     /* Wipe seenCells and seenFaces for next component */
240     ierr = PetscBTMemzero(fEnd - fStart, seenFaces);CHKERRQ(ierr);
241     ierr = PetscBTMemzero(cEnd - cStart, seenCells);CHKERRQ(ierr);
242     ++comp;
243   } while (1);
244   numComponents = comp;
245   if (flg) {
246     PetscViewer v;
247 
248     ierr = PetscViewerASCIIGetStdout(comm, &v);CHKERRQ(ierr);
249     ierr = PetscViewerASCIIPushSynchronized(v);CHKERRQ(ierr);
250     ierr = PetscViewerASCIISynchronizedPrintf(v, "[%d]BT for serial flipped cells:\n", rank);CHKERRQ(ierr);
251     ierr = PetscBTView(cEnd-cStart, flippedCells, v);CHKERRQ(ierr);
252     ierr = PetscViewerFlush(v);CHKERRQ(ierr);
253     ierr = PetscViewerASCIIPopSynchronized(v);CHKERRQ(ierr);
254   }
255   /* Now all subdomains are oriented, but we need a consistent parallel orientation */
256   if (numLeaves >= 0) {
257     /* Store orientations of boundary faces*/
258     ierr = PetscCalloc2(numRoots,&rorntComp,numRoots,&lorntComp);CHKERRQ(ierr);
259     for (face = fStart; face < fEnd; ++face) {
260       const PetscInt *cone, *support, *ornt;
261       PetscInt        coneSize, supportSize;
262 
263       ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
264       if (supportSize != 1) continue;
265       ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
266 
267       ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
268       ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
269       ierr = DMPlexGetConeOrientation(dm, support[0], &ornt);CHKERRQ(ierr);
270       for (c = 0; c < coneSize; ++c) if (cone[c] == face) break;
271       if (dim == 1) {
272         /* Use cone position instead, shifted to -1 or 1 */
273         if (PetscBTLookup(flippedCells, support[0]-cStart)) rorntComp[face].rank = 1-c*2;
274         else                                                rorntComp[face].rank = c*2-1;
275       } else {
276         if (PetscBTLookup(flippedCells, support[0]-cStart)) rorntComp[face].rank = ornt[c] < 0 ? -1 :  1;
277         else                                                rorntComp[face].rank = ornt[c] < 0 ?  1 : -1;
278       }
279       rorntComp[face].index = faceComp[face-fStart];
280     }
281     /* Communicate boundary edge orientations */
282     ierr = PetscSFBcastBegin(sf, MPIU_2INT, rorntComp, lorntComp);CHKERRQ(ierr);
283     ierr = PetscSFBcastEnd(sf, MPIU_2INT, rorntComp, lorntComp);CHKERRQ(ierr);
284   }
285   /* Get process adjacency */
286   ierr = PetscMalloc2(numComponents, &numNeighbors, numComponents, &neighbors);CHKERRQ(ierr);
287   for (comp = 0; comp < numComponents; ++comp) {
288     PetscInt  l, n;
289 
290     numNeighbors[comp] = 0;
291     ierr = PetscMalloc1(PetscMax(numLeaves, 0), &neighbors[comp]);CHKERRQ(ierr);
292     /* I know this is p^2 time in general, but for bounded degree its alright */
293     for (l = 0; l < numLeaves; ++l) {
294       const PetscInt face = lpoints[l];
295 
296       /* Find a representative face (edge) separating pairs of procs */
297       if ((face >= fStart) && (face < fEnd) && (faceComp[face-fStart] == comp)) {
298         const PetscInt rrank = rpoints[l].rank;
299         const PetscInt rcomp = lorntComp[face].index;
300 
301         for (n = 0; n < numNeighbors[comp]; ++n) if ((rrank == rpoints[neighbors[comp][n]].rank) && (rcomp == lorntComp[lpoints[neighbors[comp][n]]].index)) break;
302         if (n >= numNeighbors[comp]) {
303           PetscInt supportSize;
304 
305           ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
306           if (supportSize != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Boundary faces should see one cell, not %d", supportSize);
307           if (flg) {ierr = PetscPrintf(PETSC_COMM_SELF, "[%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);}
308           neighbors[comp][numNeighbors[comp]++] = l;
309         }
310       }
311     }
312     totNeighbors += numNeighbors[comp];
313   }
314   ierr = PetscMalloc2(totNeighbors, &nrankComp, totNeighbors, &match);CHKERRQ(ierr);
315   for (comp = 0, off = 0; comp < numComponents; ++comp) {
316     PetscInt n;
317 
318     for (n = 0; n < numNeighbors[comp]; ++n, ++off) {
319       const PetscInt face = lpoints[neighbors[comp][n]];
320       const PetscInt o    = rorntComp[face].rank*lorntComp[face].rank;
321 
322       if      (o < 0) match[off] = PETSC_TRUE;
323       else if (o > 0) match[off] = PETSC_FALSE;
324       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);
325       nrankComp[off].rank  = rpoints[neighbors[comp][n]].rank;
326       nrankComp[off].index = lorntComp[lpoints[neighbors[comp][n]]].index;
327     }
328     ierr = PetscFree(neighbors[comp]);CHKERRQ(ierr);
329   }
330   /* Collect the graph on 0 */
331   if (numLeaves >= 0) {
332     Mat          G;
333     PetscBT      seenProcs, flippedProcs;
334     PetscInt    *procFIFO, pTop, pBottom;
335     PetscInt    *N   = NULL, *Noff;
336     PetscSFNode *adj = NULL;
337     PetscBool   *val = NULL;
338     PetscMPIInt *recvcounts = NULL, *displs = NULL, *Nc, p, o;
339     PetscMPIInt  size = 0;
340 
341     ierr = PetscCalloc1(numComponents, &flipped);CHKERRQ(ierr);
342     if (!rank) {ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);}
343     ierr = PetscCalloc4(size, &recvcounts, size+1, &displs, size, &Nc, size+1, &Noff);CHKERRQ(ierr);
344     ierr = MPI_Gather(&numComponents, 1, MPI_INT, Nc, 1, MPI_INT, 0, comm);CHKERRQ(ierr);
345     for (p = 0; p < size; ++p) {
346       displs[p+1] = displs[p] + Nc[p];
347     }
348     if (!rank) {ierr = PetscMalloc1(displs[size],&N);CHKERRQ(ierr);}
349     ierr = MPI_Gatherv(numNeighbors, numComponents, MPIU_INT, N, Nc, displs, MPIU_INT, 0, comm);CHKERRQ(ierr);
350     for (p = 0, o = 0; p < size; ++p) {
351       recvcounts[p] = 0;
352       for (c = 0; c < Nc[p]; ++c, ++o) recvcounts[p] += N[o];
353       displs[p+1] = displs[p] + recvcounts[p];
354     }
355     if (!rank) {ierr = PetscMalloc2(displs[size], &adj, displs[size], &val);CHKERRQ(ierr);}
356     ierr = MPI_Gatherv(nrankComp, totNeighbors, MPIU_2INT, adj, recvcounts, displs, MPIU_2INT, 0, comm);CHKERRQ(ierr);
357     ierr = MPI_Gatherv(match, totNeighbors, MPIU_BOOL, val, recvcounts, displs, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
358     ierr = PetscFree2(numNeighbors, neighbors);CHKERRQ(ierr);
359     if (!rank) {
360       for (p = 1; p <= size; ++p) {Noff[p] = Noff[p-1] + Nc[p-1];}
361       if (flg) {
362         PetscInt n;
363 
364         for (p = 0, off = 0; p < size; ++p) {
365           for (c = 0; c < Nc[p]; ++c) {
366             ierr = PetscPrintf(PETSC_COMM_SELF, "Proc %d Comp %d:\n", p, c);CHKERRQ(ierr);
367             for (n = 0; n < N[Noff[p]+c]; ++n, ++off) {
368               ierr = PetscPrintf(PETSC_COMM_SELF, "  edge (%d, %d) (%d):\n", adj[off].rank, adj[off].index, val[off]);CHKERRQ(ierr);
369             }
370           }
371         }
372       }
373       /* Symmetrize the graph */
374       ierr = MatCreate(PETSC_COMM_SELF, &G);CHKERRQ(ierr);
375       ierr = MatSetSizes(G, Noff[size], Noff[size], Noff[size], Noff[size]);CHKERRQ(ierr);
376       ierr = MatSetUp(G);CHKERRQ(ierr);
377       for (p = 0, off = 0; p < size; ++p) {
378         for (c = 0; c < Nc[p]; ++c) {
379           const PetscInt r = Noff[p]+c;
380           PetscInt       n;
381 
382           for (n = 0; n < N[r]; ++n, ++off) {
383             const PetscInt    q = Noff[adj[off].rank] + adj[off].index;
384             const PetscScalar o = val[off] ? 1.0 : 0.0;
385 
386             ierr = MatSetValues(G, 1, &r, 1, &q, &o, INSERT_VALUES);CHKERRQ(ierr);
387             ierr = MatSetValues(G, 1, &q, 1, &r, &o, INSERT_VALUES);CHKERRQ(ierr);
388           }
389         }
390       }
391       ierr = MatAssemblyBegin(G, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
392       ierr = MatAssemblyEnd(G, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
393 
394       ierr = PetscBTCreate(Noff[size], &seenProcs);CHKERRQ(ierr);
395       ierr = PetscBTMemzero(Noff[size], seenProcs);CHKERRQ(ierr);
396       ierr = PetscBTCreate(Noff[size], &flippedProcs);CHKERRQ(ierr);
397       ierr = PetscBTMemzero(Noff[size], flippedProcs);CHKERRQ(ierr);
398       ierr = PetscMalloc1(Noff[size], &procFIFO);CHKERRQ(ierr);
399       pTop = pBottom = 0;
400       for (p = 0; p < Noff[size]; ++p) {
401         if (PetscBTLookup(seenProcs, p)) continue;
402         /* Initialize FIFO with next proc */
403         procFIFO[pBottom++] = p;
404         ierr = PetscBTSet(seenProcs, p);CHKERRQ(ierr);
405         /* Consider each proc in FIFO */
406         while (pTop < pBottom) {
407           const PetscScalar *ornt;
408           const PetscInt    *neighbors;
409           PetscInt           proc, nproc, seen, flippedA, flippedB, mismatch, numNeighbors, n;
410 
411           proc     = procFIFO[pTop++];
412           flippedA = PetscBTLookup(flippedProcs, proc) ? 1 : 0;
413           ierr = MatGetRow(G, proc, &numNeighbors, &neighbors, &ornt);CHKERRQ(ierr);
414           /* Loop over neighboring procs */
415           for (n = 0; n < numNeighbors; ++n) {
416             nproc    = neighbors[n];
417             mismatch = PetscRealPart(ornt[n]) > 0.5 ? 0 : 1;
418             seen     = PetscBTLookup(seenProcs, nproc);
419             flippedB = PetscBTLookup(flippedProcs, nproc) ? 1 : 0;
420 
421             if (mismatch ^ (flippedA ^ flippedB)) {
422               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);
423               if (!flippedB) {
424                 ierr = PetscBTSet(flippedProcs, nproc);CHKERRQ(ierr);
425               } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent mesh orientation: Fault mesh is non-orientable");
426             } else if (mismatch && flippedA && flippedB) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Attempt to flip already flipped cell: Fault mesh is non-orientable");
427             if (!seen) {
428               procFIFO[pBottom++] = nproc;
429               ierr = PetscBTSet(seenProcs, nproc);CHKERRQ(ierr);
430             }
431           }
432         }
433       }
434       ierr = PetscFree(procFIFO);CHKERRQ(ierr);
435       ierr = MatDestroy(&G);CHKERRQ(ierr);
436       ierr = PetscFree2(adj, val);CHKERRQ(ierr);
437       ierr = PetscBTDestroy(&seenProcs);CHKERRQ(ierr);
438     }
439     /* Scatter flip flags */
440     {
441       PetscBool *flips = NULL;
442 
443       if (!rank) {
444         ierr = PetscMalloc1(Noff[size], &flips);CHKERRQ(ierr);
445         for (p = 0; p < Noff[size]; ++p) {
446           flips[p] = PetscBTLookup(flippedProcs, p) ? PETSC_TRUE : PETSC_FALSE;
447           if (flg && flips[p]) {ierr = PetscPrintf(comm, "Flipping Proc+Comp %d:\n", p);CHKERRQ(ierr);}
448         }
449         for (p = 0; p < size; ++p) {
450           displs[p+1] = displs[p] + Nc[p];
451         }
452       }
453       ierr = MPI_Scatterv(flips, Nc, displs, MPIU_BOOL, flipped, numComponents, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
454       ierr = PetscFree(flips);CHKERRQ(ierr);
455     }
456     if (!rank) {ierr = PetscBTDestroy(&flippedProcs);CHKERRQ(ierr);}
457     ierr = PetscFree(N);CHKERRQ(ierr);
458     ierr = PetscFree4(recvcounts, displs, Nc, Noff);CHKERRQ(ierr);
459     ierr = PetscFree2(nrankComp, match);CHKERRQ(ierr);
460 
461     /* Decide whether to flip cells in each component */
462     for (c = 0; c < cEnd-cStart; ++c) {if (flipped[cellComp[c]]) {ierr = PetscBTNegate(flippedCells, c);CHKERRQ(ierr);}}
463     ierr = PetscFree(flipped);CHKERRQ(ierr);
464   }
465   if (flg) {
466     PetscViewer v;
467 
468     ierr = PetscViewerASCIIGetStdout(comm, &v);CHKERRQ(ierr);
469     ierr = PetscViewerASCIIPushSynchronized(v);CHKERRQ(ierr);
470     ierr = PetscViewerASCIISynchronizedPrintf(v, "[%d]BT for parallel flipped cells:\n", rank);CHKERRQ(ierr);
471     ierr = PetscBTView(cEnd-cStart, flippedCells, v);CHKERRQ(ierr);
472     ierr = PetscViewerFlush(v);CHKERRQ(ierr);
473     ierr = PetscViewerASCIIPopSynchronized(v);CHKERRQ(ierr);
474   }
475   /* Reverse flipped cells in the mesh */
476   for (c = cStart; c < cEnd; ++c) {
477     if (PetscBTLookup(flippedCells, c-cStart)) {ierr = DMPlexReverseCell(dm, c);CHKERRQ(ierr);}
478   }
479   ierr = PetscBTDestroy(&seenCells);CHKERRQ(ierr);
480   ierr = PetscBTDestroy(&flippedCells);CHKERRQ(ierr);
481   ierr = PetscBTDestroy(&seenFaces);CHKERRQ(ierr);
482   ierr = PetscFree2(numNeighbors, neighbors);CHKERRQ(ierr);
483   ierr = PetscFree2(rorntComp, lorntComp);CHKERRQ(ierr);
484   ierr = PetscFree3(faceFIFO, cellComp, faceComp);CHKERRQ(ierr);
485   PetscFunctionReturn(0);
486 }
487