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: [](chapter_unstructured), `DM`, `DMPLEX`, `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;

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

 37:     DMPlexGetCellType(dm, cone[c], &ft);
 38:     nO         = DMPolytopeTypeGetNumArrangments(ft) / 2;
 39:     newcone[c] = cone[arr[c * 2 + 0]];
 40:     newornt[c] = DMPolytopeTypeComposeOrientation(ft, arr[c * 2 + 1], ornt[arr[c * 2 + 0]]);
 42:   }
 43:   DMPlexSetCone(dm, p, newcone);
 44:   DMPlexSetConeOrientation(dm, p, newornt);
 45:   DMRestoreWorkArray(dm, coneSize, MPIU_INT, &newcone);
 46:   DMRestoreWorkArray(dm, coneSize, MPIU_INT, &newornt);
 47:   /* Update orientation of this point in the support points */
 48:   DMPlexGetSupportSize(dm, p, &supportSize);
 49:   DMPlexGetSupport(dm, p, &support);
 50:   for (s = 0; s < supportSize; ++s) {
 51:     DMPlexGetConeSize(dm, support[s], &coneSize);
 52:     DMPlexGetCone(dm, support[s], &cone);
 53:     DMPlexGetConeOrientation(dm, support[s], &ornt);
 54:     for (c = 0; c < coneSize; ++c) {
 55:       PetscInt po;

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

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

 77:   face = faceFIFO[(*fTop)++];
 78:   DMGetDimension(dm, &dim);
 79:   DMPlexGetSupportSize(dm, face, &supportSize);
 80:   DMPlexGetSupport(dm, face, &support);
 81:   if (supportSize < 2) return 0;
 83:   seenA    = PetscBTLookup(seenCells, support[0] - cStart);
 84:   flippedA = PetscBTLookup(flippedCells, support[0] - cStart) ? 1 : 0;
 85:   seenB    = PetscBTLookup(seenCells, support[1] - cStart);
 86:   flippedB = PetscBTLookup(flippedCells, support[1] - cStart) ? 1 : 0;

 88:   DMPlexGetConeSize(dm, support[0], &coneSizeA);
 89:   DMPlexGetConeSize(dm, support[1], &coneSizeB);
 90:   DMPlexGetCone(dm, support[0], &coneA);
 91:   DMPlexGetCone(dm, support[1], &coneB);
 92:   DMPlexGetConeOrientation(dm, support[0], &coneOA);
 93:   DMPlexGetConeOrientation(dm, support[1], &coneOB);
 94:   for (c = 0; c < coneSizeA; ++c) {
 95:     if (!PetscBTLookup(seenFaces, coneA[c] - fStart)) {
 96:       faceFIFO[(*fBottom)++] = coneA[c];
 97:       PetscBTSet(seenFaces, coneA[c] - fStart);
 98:     }
 99:     if (coneA[c] == face) posA = c;
101:   }
103:   for (c = 0; c < coneSizeB; ++c) {
104:     if (!PetscBTLookup(seenFaces, coneB[c] - fStart)) {
105:       faceFIFO[(*fBottom)++] = coneB[c];
106:       PetscBTSet(seenFaces, coneB[c] - fStart);
107:     }
108:     if (coneB[c] == face) posB = c;
110:   }

113:   if (dim == 1) {
114:     mismatch = posA == posB;
115:   } else {
116:     mismatch = coneOA[posA] == coneOB[posB];
117:   }

119:   if (mismatch ^ (flippedA ^ flippedB)) {
121:     if (!seenA && !flippedA) {
122:       PetscBTSet(flippedCells, support[0] - cStart);
123:     } else if (!seenB && !flippedB) {
124:       PetscBTSet(flippedCells, support[1] - cStart);
125:     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent mesh orientation: Fault mesh is non-orientable");
127:   PetscBTSet(seenCells, support[0] - cStart);
128:   PetscBTSet(seenCells, support[1] - cStart);
129:   return 0;
130: }

132: /*@
133:   DMPlexOrient - Give a consistent orientation to the input mesh

135:   Input Parameters:
136: . dm - The `DM`

138:   Note:
139:   The orientation data for the `DM` are change in-place.

141:   This routine will fail for non-orientable surfaces, such as the Moebius strip.

143:   Level: advanced

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

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

206:    NEW STYLE
207:    - Create the adj on each process
208:    - Bootstrap to complete graph on proc 0
209:   */
210:   /* Loop over components */
211:   for (cell = cStart; cell < cEnd; ++cell) cellComp[cell - cStart] = -1;
212:   do {
213:     /* Look for first unmarked cell */
214:     for (cell = cStart; cell < cEnd; ++cell)
215:       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;

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

249:     PetscViewerASCIIGetStdout(comm, &v);
250:     PetscViewerASCIIPushSynchronized(v);
251:     PetscViewerASCIISynchronizedPrintf(v, "[%d]BT for serial flipped cells:\n", rank);
252:     PetscBTView(cEnd - cStart, flippedCells, v);
253:     PetscViewerFlush(v);
254:     PetscViewerASCIIPopSynchronized(v);
255:   }
256:   /* Now all subdomains are oriented, but we need a consistent parallel orientation */
257:   if (numLeaves >= 0) {
258:     PetscInt maxSupportSize, neighbor;

260:     /* Store orientations of boundary faces*/
261:     DMPlexGetMaxSizes(dm, NULL, &maxSupportSize);
262:     PetscCalloc3(numRoots, &rorntComp, numRoots, &lorntComp, maxSupportSize, &locSupport);
263:     for (face = fStart; face < fEnd; ++face) {
264:       const PetscInt *cone, *support, *ornt;
265:       PetscInt        coneSize, supportSize, Ns = 0, s, l;

267:       DMPlexGetSupportSize(dm, face, &supportSize);
268:       /* Ignore overlapping cells */
269:       DMPlexGetSupport(dm, face, &support);
270:       for (s = 0; s < supportSize; ++s) {
271:         PetscFindInt(support[s], numLeaves, lpoints, &l);
272:         if (l >= 0) continue;
273:         locSupport[Ns++] = support[s];
274:       }
275:       if (Ns != 1) continue;
276:       neighbor = locSupport[0];
277:       DMPlexGetCone(dm, neighbor, &cone);
278:       DMPlexGetConeSize(dm, neighbor, &coneSize);
279:       DMPlexGetConeOrientation(dm, neighbor, &ornt);
280:       for (c = 0; c < coneSize; ++c)
281:         if (cone[c] == face) break;
282:       if (dim == 1) {
283:         /* Use cone position instead, shifted to -1 or 1 */
284:         if (PetscBTLookup(flippedCells, neighbor - cStart)) rorntComp[face].rank = 1 - c * 2;
285:         else rorntComp[face].rank = c * 2 - 1;
286:       } else {
287:         if (PetscBTLookup(flippedCells, neighbor - cStart)) rorntComp[face].rank = ornt[c] < 0 ? -1 : 1;
288:         else rorntComp[face].rank = ornt[c] < 0 ? 1 : -1;
289:       }
290:       rorntComp[face].index = faceComp[face - fStart];
291:     }
292:     /* Communicate boundary edge orientations */
293:     PetscSFBcastBegin(sf, MPIU_2INT, rorntComp, lorntComp, MPI_REPLACE);
294:     PetscSFBcastEnd(sf, MPIU_2INT, rorntComp, lorntComp, MPI_REPLACE);
295:   }
296:   /* Get process adjacency */
297:   PetscMalloc2(numComponents, &numNeighbors, numComponents, &neighbors);
298:   viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm));
299:   if (flg2) PetscViewerASCIIPushSynchronized(viewer);
300:   PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &selfviewer);
301:   for (comp = 0; comp < numComponents; ++comp) {
302:     PetscInt l, n;

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

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

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

320:           DMPlexGetSupportSize(dm, face, &supportSize);
322:           if (flg)
323:             PetscCall(PetscViewerASCIIPrintf(selfviewer, "[%d]: component %d, Found representative leaf %" PetscInt_FMT " (face %" PetscInt_FMT ") connecting to face %" PetscInt_FMT " on (%" PetscInt_FMT ", %" PetscInt_FMT ") with orientation %" PetscInt_FMT "\n", rank, comp, l, face,
324:                                              rpoints[l].index, rrank, rcomp, lorntComp[face].rank));
325:           neighbors[comp][numNeighbors[comp]++] = l;
326:         }
327:       }
328:     }
329:     totNeighbors += numNeighbors[comp];
330:   }
331:   PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &selfviewer);
332:   PetscViewerFlush(viewer);
333:   if (flg2) PetscViewerASCIIPopSynchronized(viewer);
334:   PetscMalloc2(totNeighbors, &nrankComp, totNeighbors, &match);
335:   for (comp = 0, off = 0; comp < numComponents; ++comp) {
336:     PetscInt n;

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

342:       if (o < 0) match[off] = PETSC_TRUE;
343:       else if (o > 0) match[off] = PETSC_FALSE;
344:       else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid face %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ") neighbor: %" PetscInt_FMT " comp: %d", face, rorntComp[face].rank, lorntComp[face].rank, neighbors[comp][n], comp);
345:       nrankComp[off].rank  = rpoints[neighbors[comp][n]].rank;
346:       nrankComp[off].index = lorntComp[lpoints[neighbors[comp][n]]].index;
347:     }
348:     PetscFree(neighbors[comp]);
349:   }
350:   /* Collect the graph on 0 */
351:   if (numLeaves >= 0) {
352:     Mat          G;
353:     PetscBT      seenProcs, flippedProcs;
354:     PetscInt    *procFIFO, pTop, pBottom;
355:     PetscInt    *N          = NULL, *Noff;
356:     PetscSFNode *adj        = NULL;
357:     PetscBool   *val        = NULL;
358:     PetscMPIInt *recvcounts = NULL, *displs = NULL, *Nc, p, o;
359:     PetscMPIInt  size = 0;

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

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

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

402:             MatSetValues(G, 1, &r, 1, &q, &o, INSERT_VALUES);
403:             MatSetValues(G, 1, &q, 1, &r, &o, INSERT_VALUES);
404:           }
405:         }
406:       }
407:       MatAssemblyBegin(G, MAT_FINAL_ASSEMBLY);
408:       MatAssemblyEnd(G, MAT_FINAL_ASSEMBLY);

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

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

437:             if (mismatch ^ (flippedA ^ flippedB)) {
439:               if (!flippedB) {
440:                 PetscBTSet(flippedProcs, nproc);
441:               } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent mesh orientation: Fault mesh is non-orientable");
443:             if (!seen) {
444:               procFIFO[pBottom++] = nproc;
445:               PetscBTSet(seenProcs, nproc);
446:             }
447:           }
448:         }
449:       }
450:       PetscFree(procFIFO);
451:       MatDestroy(&G);
452:       PetscFree2(adj, val);
453:       PetscBTDestroy(&seenProcs);
454:     }
455:     /* Scatter flip flags */
456:     {
457:       PetscBool *flips = NULL;

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

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

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