Actual source code: plexorient.c

  1: #include <petsc/private/dmpleximpl.h>
  2: #include <petscsf.h>

  4: /*@
  5:   DMPlexOrientPoint - Act with the given orientation on the cone points of this mesh point, and update its use in the mesh.

  7:   Not Collective

  9:   Input Parameters:
 10: + dm - The DM
 11: . p  - The mesh point
 12: - o  - The orientation

 14:   Level: intermediate

 16: .seealso: DMPlexOrient(), DMPlexGetCone(), DMPlexGetConeOrientation(), DMPlexInterpolate(), DMPlexGetChart()
 17: @*/
 18: PetscErrorCode DMPlexOrientPoint(DM dm, PetscInt p, PetscInt o)
 19: {
 20:   DMPolytopeType  ct;
 21:   const PetscInt *arr, *cone, *ornt, *support;
 22:   PetscInt       *newcone, *newornt;
 23:   PetscInt        coneSize, c, supportSize, s;
 24:   PetscErrorCode  ierr;

 28:   DMPlexGetCellType(dm, p, &ct);
 29:   arr  = DMPolytopeTypeGetArrangment(ct, o);
 30:   DMPlexGetConeSize(dm, p, &coneSize);
 31:   DMPlexGetCone(dm, p, &cone);
 32:   DMPlexGetConeOrientation(dm, p, &ornt);
 33:   DMGetWorkArray(dm, coneSize, MPIU_INT, &newcone);
 34:   DMGetWorkArray(dm, coneSize, MPIU_INT, &newornt);
 35:   for (c = 0; c < coneSize; ++c) {
 36:     DMPolytopeType ft;
 37:     PetscInt       nO;

 39:     DMPlexGetCellType(dm, cone[c], &ft);
 40:     nO   = DMPolytopeTypeGetNumArrangments(ft)/2;
 41:     newcone[c] = cone[arr[c*2+0]];
 42:     newornt[c] = DMPolytopeTypeComposeOrientation(ft, arr[c*2+1], ornt[arr[c*2+0]]);
 43:     if (newornt[c] && (newornt[c] >= nO || newornt[c] < -nO)) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid orientation %D not in [%D,%D) for %s %D", newornt[c], -nO, nO, DMPolytopeTypes[ft], cone[c]);
 44:   }
 45:   DMPlexSetCone(dm, p, newcone);
 46:   DMPlexSetConeOrientation(dm, p, newornt);
 47:   DMRestoreWorkArray(dm, coneSize, MPIU_INT, &newcone);
 48:   DMRestoreWorkArray(dm, coneSize, MPIU_INT, &newornt);
 49:   /* Update orientation of this point in the support points */
 50:   DMPlexGetSupportSize(dm, p, &supportSize);
 51:   DMPlexGetSupport(dm, p, &support);
 52:   for (s = 0; s < supportSize; ++s) {
 53:     DMPlexGetConeSize(dm, support[s], &coneSize);
 54:     DMPlexGetCone(dm, support[s], &cone);
 55:     DMPlexGetConeOrientation(dm, support[s], &ornt);
 56:     for (c = 0; c < coneSize; ++c) {
 57:       PetscInt po;

 59:       if (cone[c] != p) continue;
 60:       /* ornt[c] * 0 = target = po * o so that po = ornt[c] * o^{-1} */
 61:       po   = DMPolytopeTypeComposeOrientationInv(ct, ornt[c], o);
 62:       DMPlexInsertConeOrientation(dm, support[s], c, po);
 63:     }
 64:   }
 65:   return(0);
 66: }

 68: /*
 69:   - Checks face match
 70:     - Flips non-matching
 71:   - Inserts faces of support cells in FIFO
 72: */
 73: static PetscErrorCode DMPlexCheckFace_Internal(DM dm, PetscInt *faceFIFO, PetscInt *fTop, PetscInt *fBottom, PetscInt cStart, PetscInt fStart, PetscInt fEnd, PetscBT seenCells, PetscBT flippedCells, PetscBT seenFaces)
 74: {
 75:   const PetscInt *support, *coneA, *coneB, *coneOA, *coneOB;
 76:   PetscInt        supportSize, coneSizeA, coneSizeB, posA = -1, posB = -1;
 77:   PetscInt        face, dim, seenA, flippedA, seenB, flippedB, mismatch, c;
 78:   PetscErrorCode  ierr;

 81:   face = faceFIFO[(*fTop)++];
 82:   DMGetDimension(dm, &dim);
 83:   DMPlexGetSupportSize(dm, face, &supportSize);
 84:   DMPlexGetSupport(dm, face, &support);
 85:   if (supportSize < 2) return(0);
 86:   if (supportSize != 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Faces should separate only two cells, not %d", supportSize);
 87:   seenA    = PetscBTLookup(seenCells,    support[0]-cStart);
 88:   flippedA = PetscBTLookup(flippedCells, support[0]-cStart) ? 1 : 0;
 89:   seenB    = PetscBTLookup(seenCells,    support[1]-cStart);
 90:   flippedB = PetscBTLookup(flippedCells, support[1]-cStart) ? 1 : 0;

 92:   DMPlexGetConeSize(dm, support[0], &coneSizeA);
 93:   DMPlexGetConeSize(dm, support[1], &coneSizeB);
 94:   DMPlexGetCone(dm, support[0], &coneA);
 95:   DMPlexGetCone(dm, support[1], &coneB);
 96:   DMPlexGetConeOrientation(dm, support[0], &coneOA);
 97:   DMPlexGetConeOrientation(dm, support[1], &coneOB);
 98:   for (c = 0; c < coneSizeA; ++c) {
 99:     if (!PetscBTLookup(seenFaces, coneA[c]-fStart)) {
100:       faceFIFO[(*fBottom)++] = coneA[c];
101:       PetscBTSet(seenFaces, coneA[c]-fStart);
102:     }
103:     if (coneA[c] == face) posA = c;
104:     if (*fBottom > fEnd-fStart) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Face %d was pushed exceeding capacity %d > %d", coneA[c], *fBottom, fEnd-fStart);
105:   }
106:   if (posA < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d could not be located in cell %d", face, support[0]);
107:   for (c = 0; c < coneSizeB; ++c) {
108:     if (!PetscBTLookup(seenFaces, coneB[c]-fStart)) {
109:       faceFIFO[(*fBottom)++] = coneB[c];
110:       PetscBTSet(seenFaces, coneB[c]-fStart);
111:     }
112:     if (coneB[c] == face) posB = c;
113:     if (*fBottom > fEnd-fStart) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Face %d was pushed exceeding capacity %d > %d", coneA[c], *fBottom, fEnd-fStart);
114:   }
115:   if (posB < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d could not be located in cell %d", face, support[1]);

117:   if (dim == 1) {
118:     mismatch = posA == posB;
119:   } else {
120:     mismatch = coneOA[posA] == coneOB[posB];
121:   }

123:   if (mismatch ^ (flippedA ^ flippedB)) {
124:     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]);
125:     if (!seenA && !flippedA) {
126:       PetscBTSet(flippedCells, support[0]-cStart);
127:     } else if (!seenB && !flippedB) {
128:       PetscBTSet(flippedCells, support[1]-cStart);
129:     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent mesh orientation: Fault mesh is non-orientable");
130:   } else if (mismatch && flippedA && flippedB) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Attempt to flip already flipped cell: Fault mesh is non-orientable");
131:   PetscBTSet(seenCells, support[0]-cStart);
132:   PetscBTSet(seenCells, support[1]-cStart);
133:   return(0);
134: }

136: /*@
137:   DMPlexOrient - Give a consistent orientation to the input mesh

139:   Input Parameters:
140: . dm - The DM

142:   Note: The orientation data for the DM are change in-place.
143: $ This routine will fail for non-orientable surfaces, such as the Moebius strip.

145:   Level: advanced

147: .seealso: DMCreate(), DMPLEX
148: @*/
149: PetscErrorCode DMPlexOrient(DM dm)
150: {
151:   MPI_Comm           comm;
152:   PetscSF            sf;
153:   const PetscInt    *lpoints;
154:   const PetscSFNode *rpoints;
155:   PetscSFNode       *rorntComp = NULL, *lorntComp = NULL;
156:   PetscInt          *numNeighbors, **neighbors;
157:   PetscSFNode       *nrankComp;
158:   PetscBool         *match, *flipped;
159:   PetscBT            seenCells, flippedCells, seenFaces;
160:   PetscInt          *faceFIFO, fTop, fBottom, *cellComp, *faceComp;
161:   PetscInt           numLeaves, numRoots, dim, h, cStart, cEnd, c, cell, fStart, fEnd, face, off, totNeighbors = 0;
162:   PetscMPIInt        rank, size, numComponents, comp = 0;
163:   PetscBool          flg, flg2;
164:   PetscViewer        viewer = NULL, selfviewer = NULL;
165:   PetscErrorCode     ierr;

168:   PetscObjectGetComm((PetscObject) dm, &comm);
169:   MPI_Comm_rank(comm, &rank);
170:   MPI_Comm_size(comm, &size);
171:   PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-orientation_view", &flg);
172:   PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-orientation_view_synchronized", &flg2);
173:   DMGetPointSF(dm, &sf);
174:   PetscSFGetGraph(sf, &numRoots, &numLeaves, &lpoints, &rpoints);
175:   /* Truth Table
176:      mismatch    flips   do action   mismatch   flipA ^ flipB   action
177:          F       0 flips     no         F             F           F
178:          F       1 flip      yes        F             T           T
179:          F       2 flips     no         T             F           T
180:          T       0 flips     yes        T             T           F
181:          T       1 flip      no
182:          T       2 flips     yes
183:   */
184:   DMGetDimension(dm, &dim);
185:   DMPlexGetVTKCellHeight(dm, &h);
186:   DMPlexGetHeightStratum(dm, h,   &cStart, &cEnd);
187:   DMPlexGetHeightStratum(dm, h+1, &fStart, &fEnd);
188:   PetscBTCreate(cEnd - cStart, &seenCells);
189:   PetscBTMemzero(cEnd - cStart, seenCells);
190:   PetscBTCreate(cEnd - cStart, &flippedCells);
191:   PetscBTMemzero(cEnd - cStart, flippedCells);
192:   PetscBTCreate(fEnd - fStart, &seenFaces);
193:   PetscBTMemzero(fEnd - fStart, seenFaces);
194:   PetscCalloc3(fEnd - fStart, &faceFIFO, cEnd-cStart, &cellComp, fEnd-fStart, &faceComp);
195:   /*
196:    OLD STYLE
197:    - Add an integer array over cells and faces (component) for connected component number
198:    Foreach component
199:      - Mark the initial cell as seen
200:      - Process component as usual
201:      - Set component for all seenCells
202:      - Wipe seenCells and seenFaces (flippedCells can stay)
203:    - Generate parallel adjacency for component using SF and seenFaces
204:    - Collect numComponents adj data from each proc to 0
205:    - Build same serial graph
206:    - Use same solver
207:    - Use Scatterv to to send back flipped flags for each component
208:    - Negate flippedCells by component

210:    NEW STYLE
211:    - Create the adj on each process
212:    - Bootstrap to complete graph on proc 0
213:   */
214:   /* Loop over components */
215:   for (cell = cStart; cell < cEnd; ++cell) cellComp[cell-cStart] = -1;
216:   do {
217:     /* Look for first unmarked cell */
218:     for (cell = cStart; cell < cEnd; ++cell) if (cellComp[cell-cStart] < 0) break;
219:     if (cell >= cEnd) break;
220:     /* Initialize FIFO with first cell in component */
221:     {
222:       const PetscInt *cone;
223:       PetscInt        coneSize;

225:       fTop = fBottom = 0;
226:       DMPlexGetConeSize(dm, cell, &coneSize);
227:       DMPlexGetCone(dm, cell, &cone);
228:       for (c = 0; c < coneSize; ++c) {
229:         faceFIFO[fBottom++] = cone[c];
230:         PetscBTSet(seenFaces, cone[c]-fStart);
231:       }
232:       PetscBTSet(seenCells, cell-cStart);
233:     }
234:     /* Consider each face in FIFO */
235:     while (fTop < fBottom) {
236:       DMPlexCheckFace_Internal(dm, faceFIFO, &fTop, &fBottom, cStart, fStart, fEnd, seenCells, flippedCells, seenFaces);
237:     }
238:     /* Set component for cells and faces */
239:     for (cell = 0; cell < cEnd-cStart; ++cell) {
240:       if (PetscBTLookup(seenCells, cell)) cellComp[cell] = comp;
241:     }
242:     for (face = 0; face < fEnd-fStart; ++face) {
243:       if (PetscBTLookup(seenFaces, face)) faceComp[face] = comp;
244:     }
245:     /* Wipe seenCells and seenFaces for next component */
246:     PetscBTMemzero(fEnd - fStart, seenFaces);
247:     PetscBTMemzero(cEnd - cStart, seenCells);
248:     ++comp;
249:   } while (1);
250:   numComponents = comp;
251:   if (flg) {
252:     PetscViewer v;

254:     PetscViewerASCIIGetStdout(comm, &v);
255:     PetscViewerASCIIPushSynchronized(v);
256:     PetscViewerASCIISynchronizedPrintf(v, "[%d]BT for serial flipped cells:\n", rank);
257:     PetscBTView(cEnd-cStart, flippedCells, v);
258:     PetscViewerFlush(v);
259:     PetscViewerASCIIPopSynchronized(v);
260:   }
261:   /* Now all subdomains are oriented, but we need a consistent parallel orientation */
262:   if (numLeaves >= 0) {
263:     /* Store orientations of boundary faces*/
264:     PetscCalloc2(numRoots,&rorntComp,numRoots,&lorntComp);
265:     for (face = fStart; face < fEnd; ++face) {
266:       const PetscInt *cone, *support, *ornt;
267:       PetscInt        coneSize, supportSize;

269:       DMPlexGetSupportSize(dm, face, &supportSize);
270:       if (supportSize != 1) continue;
271:       DMPlexGetSupport(dm, face, &support);

273:       DMPlexGetCone(dm, support[0], &cone);
274:       DMPlexGetConeSize(dm, support[0], &coneSize);
275:       DMPlexGetConeOrientation(dm, support[0], &ornt);
276:       for (c = 0; c < coneSize; ++c) if (cone[c] == face) break;
277:       if (dim == 1) {
278:         /* Use cone position instead, shifted to -1 or 1 */
279:         if (PetscBTLookup(flippedCells, support[0]-cStart)) rorntComp[face].rank = 1-c*2;
280:         else                                                rorntComp[face].rank = c*2-1;
281:       } else {
282:         if (PetscBTLookup(flippedCells, support[0]-cStart)) rorntComp[face].rank = ornt[c] < 0 ? -1 :  1;
283:         else                                                rorntComp[face].rank = ornt[c] < 0 ?  1 : -1;
284:       }
285:       rorntComp[face].index = faceComp[face-fStart];
286:     }
287:     /* Communicate boundary edge orientations */
288:     PetscSFBcastBegin(sf, MPIU_2INT, rorntComp, lorntComp,MPI_REPLACE);
289:     PetscSFBcastEnd(sf, MPIU_2INT, rorntComp, lorntComp,MPI_REPLACE);
290:   }
291:   /* Get process adjacency */
292:   PetscMalloc2(numComponents, &numNeighbors, numComponents, &neighbors);
293:   viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm));
294:   if (flg2) {PetscViewerASCIIPushSynchronized(viewer);}
295:   PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&selfviewer);
296:   for (comp = 0; comp < numComponents; ++comp) {
297:     PetscInt  l, n;

299:     numNeighbors[comp] = 0;
300:     PetscMalloc1(PetscMax(numLeaves, 0), &neighbors[comp]);
301:     /* I know this is p^2 time in general, but for bounded degree its alright */
302:     for (l = 0; l < numLeaves; ++l) {
303:       const PetscInt face = lpoints[l];

305:       /* Find a representative face (edge) separating pairs of procs */
306:       if ((face >= fStart) && (face < fEnd) && (faceComp[face-fStart] == comp)) {
307:         const PetscInt rrank = rpoints[l].rank;
308:         const PetscInt rcomp = lorntComp[face].index;

310:         for (n = 0; n < numNeighbors[comp]; ++n) if ((rrank == rpoints[neighbors[comp][n]].rank) && (rcomp == lorntComp[lpoints[neighbors[comp][n]]].index)) break;
311:         if (n >= numNeighbors[comp]) {
312:           PetscInt supportSize;

314:           DMPlexGetSupportSize(dm, face, &supportSize);
315:           if (supportSize != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Boundary faces should see one cell, not %d", supportSize);
316:           if (flg) {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);}
317:           neighbors[comp][numNeighbors[comp]++] = l;
318:         }
319:       }
320:     }
321:     totNeighbors += numNeighbors[comp];
322:   }
323:   PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&selfviewer);
324:   PetscViewerFlush(viewer);
325:   if (flg2) {PetscViewerASCIIPopSynchronized(viewer);}
326:   PetscMalloc2(totNeighbors, &nrankComp, totNeighbors, &match);
327:   for (comp = 0, off = 0; comp < numComponents; ++comp) {
328:     PetscInt n;

330:     for (n = 0; n < numNeighbors[comp]; ++n, ++off) {
331:       const PetscInt face = lpoints[neighbors[comp][n]];
332:       const PetscInt o    = rorntComp[face].rank*lorntComp[face].rank;

334:       if      (o < 0) match[off] = PETSC_TRUE;
335:       else if (o > 0) match[off] = PETSC_FALSE;
336:       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);
337:       nrankComp[off].rank  = rpoints[neighbors[comp][n]].rank;
338:       nrankComp[off].index = lorntComp[lpoints[neighbors[comp][n]]].index;
339:     }
340:     PetscFree(neighbors[comp]);
341:   }
342:   /* Collect the graph on 0 */
343:   if (numLeaves >= 0) {
344:     Mat          G;
345:     PetscBT      seenProcs, flippedProcs;
346:     PetscInt    *procFIFO, pTop, pBottom;
347:     PetscInt    *N   = NULL, *Noff;
348:     PetscSFNode *adj = NULL;
349:     PetscBool   *val = NULL;
350:     PetscMPIInt *recvcounts = NULL, *displs = NULL, *Nc, p, o;
351:     PetscMPIInt  size = 0;

353:     PetscCalloc1(numComponents, &flipped);
354:     if (rank == 0) {MPI_Comm_size(comm, &size);}
355:     PetscCalloc4(size, &recvcounts, size+1, &displs, size, &Nc, size+1, &Noff);
356:     MPI_Gather(&numComponents, 1, MPI_INT, Nc, 1, MPI_INT, 0, comm);
357:     for (p = 0; p < size; ++p) {
358:       displs[p+1] = displs[p] + Nc[p];
359:     }
360:     if (rank == 0) {PetscMalloc1(displs[size],&N);}
361:     MPI_Gatherv(numNeighbors, numComponents, MPIU_INT, N, Nc, displs, MPIU_INT, 0, comm);
362:     for (p = 0, o = 0; p < size; ++p) {
363:       recvcounts[p] = 0;
364:       for (c = 0; c < Nc[p]; ++c, ++o) recvcounts[p] += N[o];
365:       displs[p+1] = displs[p] + recvcounts[p];
366:     }
367:     if (rank == 0) {PetscMalloc2(displs[size], &adj, displs[size], &val);}
368:     MPI_Gatherv(nrankComp, totNeighbors, MPIU_2INT, adj, recvcounts, displs, MPIU_2INT, 0, comm);
369:     MPI_Gatherv(match, totNeighbors, MPIU_BOOL, val, recvcounts, displs, MPIU_BOOL, 0, comm);
370:     PetscFree2(numNeighbors, neighbors);
371:     if (rank == 0) {
372:       for (p = 1; p <= size; ++p) {Noff[p] = Noff[p-1] + Nc[p-1];}
373:       if (flg) {
374:         PetscInt n;

376:         for (p = 0, off = 0; p < size; ++p) {
377:           for (c = 0; c < Nc[p]; ++c) {
378:             PetscPrintf(PETSC_COMM_SELF, "Proc %d Comp %d:\n", p, c);
379:             for (n = 0; n < N[Noff[p]+c]; ++n, ++off) {
380:               PetscPrintf(PETSC_COMM_SELF, "  edge (%d, %d) (%d):\n", adj[off].rank, adj[off].index, val[off]);
381:             }
382:           }
383:         }
384:       }
385:       /* Symmetrize the graph */
386:       MatCreate(PETSC_COMM_SELF, &G);
387:       MatSetSizes(G, Noff[size], Noff[size], Noff[size], Noff[size]);
388:       MatSetUp(G);
389:       for (p = 0, off = 0; p < size; ++p) {
390:         for (c = 0; c < Nc[p]; ++c) {
391:           const PetscInt r = Noff[p]+c;
392:           PetscInt       n;

394:           for (n = 0; n < N[r]; ++n, ++off) {
395:             const PetscInt    q = Noff[adj[off].rank] + adj[off].index;
396:             const PetscScalar o = val[off] ? 1.0 : 0.0;

398:             MatSetValues(G, 1, &r, 1, &q, &o, INSERT_VALUES);
399:             MatSetValues(G, 1, &q, 1, &r, &o, INSERT_VALUES);
400:           }
401:         }
402:       }
403:       MatAssemblyBegin(G, MAT_FINAL_ASSEMBLY);
404:       MatAssemblyEnd(G, MAT_FINAL_ASSEMBLY);

406:       PetscBTCreate(Noff[size], &seenProcs);
407:       PetscBTMemzero(Noff[size], seenProcs);
408:       PetscBTCreate(Noff[size], &flippedProcs);
409:       PetscBTMemzero(Noff[size], flippedProcs);
410:       PetscMalloc1(Noff[size], &procFIFO);
411:       pTop = pBottom = 0;
412:       for (p = 0; p < Noff[size]; ++p) {
413:         if (PetscBTLookup(seenProcs, p)) continue;
414:         /* Initialize FIFO with next proc */
415:         procFIFO[pBottom++] = p;
416:         PetscBTSet(seenProcs, p);
417:         /* Consider each proc in FIFO */
418:         while (pTop < pBottom) {
419:           const PetscScalar *ornt;
420:           const PetscInt    *neighbors;
421:           PetscInt           proc, nproc, seen, flippedA, flippedB, mismatch, numNeighbors, n;

423:           proc     = procFIFO[pTop++];
424:           flippedA = PetscBTLookup(flippedProcs, proc) ? 1 : 0;
425:           MatGetRow(G, proc, &numNeighbors, &neighbors, &ornt);
426:           /* Loop over neighboring procs */
427:           for (n = 0; n < numNeighbors; ++n) {
428:             nproc    = neighbors[n];
429:             mismatch = PetscRealPart(ornt[n]) > 0.5 ? 0 : 1;
430:             seen     = PetscBTLookup(seenProcs, nproc);
431:             flippedB = PetscBTLookup(flippedProcs, nproc) ? 1 : 0;

433:             if (mismatch ^ (flippedA ^ flippedB)) {
434:               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);
435:               if (!flippedB) {
436:                 PetscBTSet(flippedProcs, nproc);
437:               } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent mesh orientation: Fault mesh is non-orientable");
438:             } else if (mismatch && flippedA && flippedB) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Attempt to flip already flipped cell: Fault mesh is non-orientable");
439:             if (!seen) {
440:               procFIFO[pBottom++] = nproc;
441:               PetscBTSet(seenProcs, nproc);
442:             }
443:           }
444:         }
445:       }
446:       PetscFree(procFIFO);
447:       MatDestroy(&G);
448:       PetscFree2(adj, val);
449:       PetscBTDestroy(&seenProcs);
450:     }
451:     /* Scatter flip flags */
452:     {
453:       PetscBool *flips = NULL;

455:       if (rank == 0) {
456:         PetscMalloc1(Noff[size], &flips);
457:         for (p = 0; p < Noff[size]; ++p) {
458:           flips[p] = PetscBTLookup(flippedProcs, p) ? PETSC_TRUE : PETSC_FALSE;
459:           if (flg && flips[p]) {PetscPrintf(comm, "Flipping Proc+Comp %d:\n", p);}
460:         }
461:         for (p = 0; p < size; ++p) {
462:           displs[p+1] = displs[p] + Nc[p];
463:         }
464:       }
465:       MPI_Scatterv(flips, Nc, displs, MPIU_BOOL, flipped, numComponents, MPIU_BOOL, 0, comm);
466:       PetscFree(flips);
467:     }
468:     if (rank == 0) {PetscBTDestroy(&flippedProcs);}
469:     PetscFree(N);
470:     PetscFree4(recvcounts, displs, Nc, Noff);
471:     PetscFree2(nrankComp, match);

473:     /* Decide whether to flip cells in each component */
474:     for (c = 0; c < cEnd-cStart; ++c) {if (flipped[cellComp[c]]) {PetscBTNegate(flippedCells, c);}}
475:     PetscFree(flipped);
476:   }
477:   if (flg) {
478:     PetscViewer v;

480:     PetscViewerASCIIGetStdout(comm, &v);
481:     PetscViewerASCIIPushSynchronized(v);
482:     PetscViewerASCIISynchronizedPrintf(v, "[%d]BT for parallel flipped cells:\n", rank);
483:     PetscBTView(cEnd-cStart, flippedCells, v);
484:     PetscViewerFlush(v);
485:     PetscViewerASCIIPopSynchronized(v);
486:   }
487:   /* Reverse flipped cells in the mesh */
488:   for (c = cStart; c < cEnd; ++c) {
489:     if (PetscBTLookup(flippedCells, c-cStart)) {
490:       DMPlexOrientPoint(dm, c, -1);
491:     }
492:   }
493:   PetscBTDestroy(&seenCells);
494:   PetscBTDestroy(&flippedCells);
495:   PetscBTDestroy(&seenFaces);
496:   PetscFree2(numNeighbors, neighbors);
497:   PetscFree2(rorntComp, lorntComp);
498:   PetscFree3(faceFIFO, cellComp, faceComp);
499:   return(0);
500: }