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;
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: The orientation data for the DM are change in-place.
139: $ This routine will fail for non-orientable surfaces, such as the Moebius strip.
141: Level: advanced
143: .seealso: DMCreate(), DMPLEX
144: @*/
145: PetscErrorCode DMPlexOrient(DM dm)
146: {
147: MPI_Comm comm;
148: PetscSF sf;
149: const PetscInt *lpoints;
150: const PetscSFNode *rpoints;
151: PetscSFNode *rorntComp = NULL, *lorntComp = NULL;
152: PetscInt *numNeighbors, **neighbors;
153: PetscSFNode *nrankComp;
154: PetscBool *match, *flipped;
155: PetscBT seenCells, flippedCells, seenFaces;
156: PetscInt *faceFIFO, fTop, fBottom, *cellComp, *faceComp;
157: PetscInt numLeaves, numRoots, dim, h, cStart, cEnd, c, cell, fStart, fEnd, face, off, totNeighbors = 0;
158: PetscMPIInt rank, size, numComponents, comp = 0;
159: PetscBool flg, flg2;
160: PetscViewer viewer = NULL, selfviewer = NULL;
162: PetscObjectGetComm((PetscObject) dm, &comm);
163: MPI_Comm_rank(comm, &rank);
164: MPI_Comm_size(comm, &size);
165: PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-orientation_view", &flg);
166: PetscOptionsHasName(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-orientation_view_synchronized", &flg2);
167: DMGetPointSF(dm, &sf);
168: PetscSFGetGraph(sf, &numRoots, &numLeaves, &lpoints, &rpoints);
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: DMGetDimension(dm, &dim);
179: DMPlexGetVTKCellHeight(dm, &h);
180: DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);
181: DMPlexGetHeightStratum(dm, h+1, &fStart, &fEnd);
182: PetscBTCreate(cEnd - cStart, &seenCells);
183: PetscBTMemzero(cEnd - cStart, seenCells);
184: PetscBTCreate(cEnd - cStart, &flippedCells);
185: PetscBTMemzero(cEnd - cStart, flippedCells);
186: PetscBTCreate(fEnd - fStart, &seenFaces);
187: PetscBTMemzero(fEnd - fStart, seenFaces);
188: PetscCalloc3(fEnd - fStart, &faceFIFO, cEnd-cStart, &cellComp, fEnd-fStart, &faceComp);
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
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;
219: fTop = fBottom = 0;
220: DMPlexGetConeSize(dm, cell, &coneSize);
221: DMPlexGetCone(dm, cell, &cone);
222: for (c = 0; c < coneSize; ++c) {
223: faceFIFO[fBottom++] = cone[c];
224: PetscBTSet(seenFaces, cone[c]-fStart);
225: }
226: PetscBTSet(seenCells, cell-cStart);
227: }
228: /* Consider each face in FIFO */
229: while (fTop < fBottom) {
230: DMPlexCheckFace_Internal(dm, faceFIFO, &fTop, &fBottom, cStart, fStart, fEnd, seenCells, flippedCells, seenFaces);
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: PetscBTMemzero(fEnd - fStart, seenFaces);
241: PetscBTMemzero(cEnd - cStart, seenCells);
242: ++comp;
243: } while (1);
244: numComponents = comp;
245: if (flg) {
246: PetscViewer v;
248: PetscViewerASCIIGetStdout(comm, &v);
249: PetscViewerASCIIPushSynchronized(v);
250: PetscViewerASCIISynchronizedPrintf(v, "[%d]BT for serial flipped cells:\n", rank);
251: PetscBTView(cEnd-cStart, flippedCells, v);
252: PetscViewerFlush(v);
253: PetscViewerASCIIPopSynchronized(v);
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: PetscCalloc2(numRoots,&rorntComp,numRoots,&lorntComp);
259: for (face = fStart; face < fEnd; ++face) {
260: const PetscInt *cone, *support, *ornt;
261: PetscInt coneSize, supportSize;
263: DMPlexGetSupportSize(dm, face, &supportSize);
264: if (supportSize != 1) continue;
265: DMPlexGetSupport(dm, face, &support);
267: DMPlexGetCone(dm, support[0], &cone);
268: DMPlexGetConeSize(dm, support[0], &coneSize);
269: DMPlexGetConeOrientation(dm, support[0], &ornt);
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: PetscSFBcastBegin(sf, MPIU_2INT, rorntComp, lorntComp,MPI_REPLACE);
283: PetscSFBcastEnd(sf, MPIU_2INT, rorntComp, lorntComp,MPI_REPLACE);
284: }
285: /* Get process adjacency */
286: PetscMalloc2(numComponents, &numNeighbors, numComponents, &neighbors);
287: viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm));
288: if (flg2) PetscViewerASCIIPushSynchronized(viewer);
289: PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&selfviewer);
290: for (comp = 0; comp < numComponents; ++comp) {
291: PetscInt l, n;
293: numNeighbors[comp] = 0;
294: PetscMalloc1(PetscMax(numLeaves, 0), &neighbors[comp]);
295: /* I know this is p^2 time in general, but for bounded degree its alright */
296: for (l = 0; l < numLeaves; ++l) {
297: const PetscInt face = lpoints[l];
299: /* Find a representative face (edge) separating pairs of procs */
300: if ((face >= fStart) && (face < fEnd) && (faceComp[face-fStart] == comp)) {
301: const PetscInt rrank = rpoints[l].rank;
302: const PetscInt rcomp = lorntComp[face].index;
304: for (n = 0; n < numNeighbors[comp]; ++n) if ((rrank == rpoints[neighbors[comp][n]].rank) && (rcomp == lorntComp[lpoints[neighbors[comp][n]]].index)) break;
305: if (n >= numNeighbors[comp]) {
306: PetscInt supportSize;
308: DMPlexGetSupportSize(dm, face, &supportSize);
310: 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);
311: neighbors[comp][numNeighbors[comp]++] = l;
312: }
313: }
314: }
315: totNeighbors += numNeighbors[comp];
316: }
317: PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&selfviewer);
318: PetscViewerFlush(viewer);
319: if (flg2) PetscViewerASCIIPopSynchronized(viewer);
320: PetscMalloc2(totNeighbors, &nrankComp, totNeighbors, &match);
321: for (comp = 0, off = 0; comp < numComponents; ++comp) {
322: PetscInt n;
324: for (n = 0; n < numNeighbors[comp]; ++n, ++off) {
325: const PetscInt face = lpoints[neighbors[comp][n]];
326: const PetscInt o = rorntComp[face].rank*lorntComp[face].rank;
328: if (o < 0) match[off] = PETSC_TRUE;
329: else if (o > 0) match[off] = PETSC_FALSE;
330: else SETERRQ(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);
331: nrankComp[off].rank = rpoints[neighbors[comp][n]].rank;
332: nrankComp[off].index = lorntComp[lpoints[neighbors[comp][n]]].index;
333: }
334: PetscFree(neighbors[comp]);
335: }
336: /* Collect the graph on 0 */
337: if (numLeaves >= 0) {
338: Mat G;
339: PetscBT seenProcs, flippedProcs;
340: PetscInt *procFIFO, pTop, pBottom;
341: PetscInt *N = NULL, *Noff;
342: PetscSFNode *adj = NULL;
343: PetscBool *val = NULL;
344: PetscMPIInt *recvcounts = NULL, *displs = NULL, *Nc, p, o;
345: PetscMPIInt size = 0;
347: PetscCalloc1(numComponents, &flipped);
348: if (rank == 0) MPI_Comm_size(comm, &size);
349: PetscCalloc4(size, &recvcounts, size+1, &displs, size, &Nc, size+1, &Noff);
350: MPI_Gather(&numComponents, 1, MPI_INT, Nc, 1, MPI_INT, 0, comm);
351: for (p = 0; p < size; ++p) {
352: displs[p+1] = displs[p] + Nc[p];
353: }
354: if (rank == 0) PetscMalloc1(displs[size],&N);
355: MPI_Gatherv(numNeighbors, numComponents, MPIU_INT, N, Nc, displs, MPIU_INT, 0, comm);
356: for (p = 0, o = 0; p < size; ++p) {
357: recvcounts[p] = 0;
358: for (c = 0; c < Nc[p]; ++c, ++o) recvcounts[p] += N[o];
359: displs[p+1] = displs[p] + recvcounts[p];
360: }
361: if (rank == 0) PetscMalloc2(displs[size], &adj, displs[size], &val);
362: MPI_Gatherv(nrankComp, totNeighbors, MPIU_2INT, adj, recvcounts, displs, MPIU_2INT, 0, comm);
363: MPI_Gatherv(match, totNeighbors, MPIU_BOOL, val, recvcounts, displs, MPIU_BOOL, 0, comm);
364: PetscFree2(numNeighbors, neighbors);
365: if (rank == 0) {
366: for (p = 1; p <= size; ++p) {Noff[p] = Noff[p-1] + Nc[p-1];}
367: if (flg) {
368: PetscInt n;
370: for (p = 0, off = 0; p < size; ++p) {
371: for (c = 0; c < Nc[p]; ++c) {
372: PetscPrintf(PETSC_COMM_SELF, "Proc %d Comp %d:\n", p, c);
373: for (n = 0; n < N[Noff[p]+c]; ++n, ++off) {
374: PetscPrintf(PETSC_COMM_SELF, " edge (%d, %d) (%d):\n", adj[off].rank, adj[off].index, val[off]);
375: }
376: }
377: }
378: }
379: /* Symmetrize the graph */
380: MatCreate(PETSC_COMM_SELF, &G);
381: MatSetSizes(G, Noff[size], Noff[size], Noff[size], Noff[size]);
382: MatSetUp(G);
383: for (p = 0, off = 0; p < size; ++p) {
384: for (c = 0; c < Nc[p]; ++c) {
385: const PetscInt r = Noff[p]+c;
386: PetscInt n;
388: for (n = 0; n < N[r]; ++n, ++off) {
389: const PetscInt q = Noff[adj[off].rank] + adj[off].index;
390: const PetscScalar o = val[off] ? 1.0 : 0.0;
392: MatSetValues(G, 1, &r, 1, &q, &o, INSERT_VALUES);
393: MatSetValues(G, 1, &q, 1, &r, &o, INSERT_VALUES);
394: }
395: }
396: }
397: MatAssemblyBegin(G, MAT_FINAL_ASSEMBLY);
398: MatAssemblyEnd(G, MAT_FINAL_ASSEMBLY);
400: PetscBTCreate(Noff[size], &seenProcs);
401: PetscBTMemzero(Noff[size], seenProcs);
402: PetscBTCreate(Noff[size], &flippedProcs);
403: PetscBTMemzero(Noff[size], flippedProcs);
404: PetscMalloc1(Noff[size], &procFIFO);
405: pTop = pBottom = 0;
406: for (p = 0; p < Noff[size]; ++p) {
407: if (PetscBTLookup(seenProcs, p)) continue;
408: /* Initialize FIFO with next proc */
409: procFIFO[pBottom++] = p;
410: PetscBTSet(seenProcs, p);
411: /* Consider each proc in FIFO */
412: while (pTop < pBottom) {
413: const PetscScalar *ornt;
414: const PetscInt *neighbors;
415: PetscInt proc, nproc, seen, flippedA, flippedB, mismatch, numNeighbors, n;
417: proc = procFIFO[pTop++];
418: flippedA = PetscBTLookup(flippedProcs, proc) ? 1 : 0;
419: MatGetRow(G, proc, &numNeighbors, &neighbors, &ornt);
420: /* Loop over neighboring procs */
421: for (n = 0; n < numNeighbors; ++n) {
422: nproc = neighbors[n];
423: mismatch = PetscRealPart(ornt[n]) > 0.5 ? 0 : 1;
424: seen = PetscBTLookup(seenProcs, nproc);
425: flippedB = PetscBTLookup(flippedProcs, nproc) ? 1 : 0;
427: if (mismatch ^ (flippedA ^ flippedB)) {
429: if (!flippedB) {
430: PetscBTSet(flippedProcs, nproc);
431: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent mesh orientation: Fault mesh is non-orientable");
433: if (!seen) {
434: procFIFO[pBottom++] = nproc;
435: PetscBTSet(seenProcs, nproc);
436: }
437: }
438: }
439: }
440: PetscFree(procFIFO);
441: MatDestroy(&G);
442: PetscFree2(adj, val);
443: PetscBTDestroy(&seenProcs);
444: }
445: /* Scatter flip flags */
446: {
447: PetscBool *flips = NULL;
449: if (rank == 0) {
450: PetscMalloc1(Noff[size], &flips);
451: for (p = 0; p < Noff[size]; ++p) {
452: flips[p] = PetscBTLookup(flippedProcs, p) ? PETSC_TRUE : PETSC_FALSE;
453: if (flg && flips[p]) PetscPrintf(comm, "Flipping Proc+Comp %d:\n", p);
454: }
455: for (p = 0; p < size; ++p) {
456: displs[p+1] = displs[p] + Nc[p];
457: }
458: }
459: MPI_Scatterv(flips, Nc, displs, MPIU_BOOL, flipped, numComponents, MPIU_BOOL, 0, comm);
460: PetscFree(flips);
461: }
462: if (rank == 0) PetscBTDestroy(&flippedProcs);
463: PetscFree(N);
464: PetscFree4(recvcounts, displs, Nc, Noff);
465: PetscFree2(nrankComp, match);
467: /* Decide whether to flip cells in each component */
468: for (c = 0; c < cEnd-cStart; ++c) {if (flipped[cellComp[c]]) PetscBTNegate(flippedCells, c);}
469: PetscFree(flipped);
470: }
471: if (flg) {
472: PetscViewer v;
474: PetscViewerASCIIGetStdout(comm, &v);
475: PetscViewerASCIIPushSynchronized(v);
476: PetscViewerASCIISynchronizedPrintf(v, "[%d]BT for parallel flipped cells:\n", rank);
477: PetscBTView(cEnd-cStart, flippedCells, v);
478: PetscViewerFlush(v);
479: PetscViewerASCIIPopSynchronized(v);
480: }
481: /* Reverse flipped cells in the mesh */
482: for (c = cStart; c < cEnd; ++c) {
483: if (PetscBTLookup(flippedCells, c-cStart)) {
484: DMPlexOrientPoint(dm, c, -1);
485: }
486: }
487: PetscBTDestroy(&seenCells);
488: PetscBTDestroy(&flippedCells);
489: PetscBTDestroy(&seenFaces);
490: PetscFree2(numNeighbors, neighbors);
491: PetscFree2(rorntComp, lorntComp);
492: PetscFree3(faceFIFO, cellComp, faceComp);
493: return 0;
494: }