Actual source code: plexreorder.c
petsc-3.12.5 2020-03-29
1: #include <petsc/private/dmpleximpl.h>
2: #include <petsc/private/matorderimpl.h>
4: static PetscErrorCode DMPlexCreateOrderingClosure_Static(DM dm, PetscInt numPoints, const PetscInt pperm[], PetscInt **clperm, PetscInt **invclperm)
5: {
6: PetscInt *perm, *iperm;
7: PetscInt depth, d, pStart, pEnd, fStart, fMax, fEnd, p;
11: DMPlexGetDepth(dm, &depth);
12: DMPlexGetChart(dm, &pStart, &pEnd);
13: PetscMalloc1(pEnd-pStart,&perm);
14: PetscMalloc1(pEnd-pStart,&iperm);
15: for (p = pStart; p < pEnd; ++p) iperm[p] = -1;
16: for (d = depth; d > 0; --d) {
17: DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
18: DMPlexGetDepthStratum(dm, d-1, &fStart, &fEnd);
19: fMax = fStart;
20: for (p = pStart; p < pEnd; ++p) {
21: const PetscInt *cone;
22: PetscInt point, coneSize, c;
24: if (d == depth) {
25: perm[p] = pperm[p];
26: iperm[pperm[p]] = p;
27: }
28: point = perm[p];
29: DMPlexGetConeSize(dm, point, &coneSize);
30: DMPlexGetCone(dm, point, &cone);
31: for (c = 0; c < coneSize; ++c) {
32: const PetscInt oldc = cone[c];
33: const PetscInt newc = iperm[oldc];
35: if (newc < 0) {
36: perm[fMax] = oldc;
37: iperm[oldc] = fMax++;
38: }
39: }
40: }
41: if (fMax != fEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of depth %d faces %d does not match permuted nubmer %d", d, fEnd-fStart, fMax-fStart);
42: }
43: *clperm = perm;
44: *invclperm = iperm;
45: return(0);
46: }
48: /*@
49: DMPlexGetOrdering - Calculate a reordering of the mesh
51: Collective on dm
53: Input Parameter:
54: + dm - The DMPlex object
55: . otype - type of reordering, one of the following:
56: $ MATORDERINGNATURAL - Natural
57: $ MATORDERINGND - Nested Dissection
58: $ MATORDERING1WD - One-way Dissection
59: $ MATORDERINGRCM - Reverse Cuthill-McKee
60: $ MATORDERINGQMD - Quotient Minimum Degree
61: - label - [Optional] Label used to segregate ordering into sets, or NULL
64: Output Parameter:
65: . perm - The point permutation as an IS, perm[old point number] = new point number
67: Note: The label is used to group sets of points together by label value. This makes it easy to reorder a mesh which
68: has different types of cells, and then loop over each set of reordered cells for assembly.
70: Level: intermediate
72: .seealso: MatGetOrdering()
73: @*/
74: PetscErrorCode DMPlexGetOrdering(DM dm, MatOrderingType otype, DMLabel label, IS *perm)
75: {
76: PetscInt numCells = 0;
77: PetscInt *start = NULL, *adjacency = NULL, *cperm, *clperm = NULL, *invclperm = NULL, *mask, *xls, pStart, pEnd, c, i;
83: DMPlexCreateNeighborCSR(dm, 0, &numCells, &start, &adjacency);
84: PetscMalloc3(numCells,&cperm,numCells,&mask,numCells*2,&xls);
85: if (numCells) {
86: /* Shift for Fortran numbering */
87: for (i = 0; i < start[numCells]; ++i) ++adjacency[i];
88: for (i = 0; i <= numCells; ++i) ++start[i];
89: SPARSEPACKgenrcm(&numCells, start, adjacency, cperm, mask, xls);
90: }
91: PetscFree(start);
92: PetscFree(adjacency);
93: /* Shift for Fortran numbering */
94: for (c = 0; c < numCells; ++c) --cperm[c];
95: /* Segregate */
96: if (label) {
97: IS valueIS;
98: const PetscInt *values;
99: PetscInt numValues, numPoints = 0;
100: PetscInt *sperm, *vsize, *voff, v;
102: DMLabelGetValueIS(label, &valueIS);
103: ISSort(valueIS);
104: ISGetLocalSize(valueIS, &numValues);
105: ISGetIndices(valueIS, &values);
106: PetscCalloc3(numCells,&sperm,numValues,&vsize,numValues+1,&voff);
107: for (v = 0; v < numValues; ++v) {
108: DMLabelGetStratumSize(label, values[v], &vsize[v]);
109: if (v < numValues-1) voff[v+2] += vsize[v] + voff[v+1];
110: numPoints += vsize[v];
111: }
112: if (numPoints != numCells) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label only covers %D cells < %D total", numPoints, numCells);
113: for (c = 0; c < numCells; ++c) {
114: const PetscInt oldc = cperm[c];
115: PetscInt val, vloc;
117: DMLabelGetValue(label, oldc, &val);
118: if (val == -1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cell %D not present in label", oldc);
119: PetscFindInt(val, numValues, values, &vloc);
120: if (vloc < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Value %D not present label", val);
121: sperm[voff[vloc+1]++] = oldc;
122: }
123: for (v = 0; v < numValues; ++v) {
124: if (voff[v+1] - voff[v] != vsize[v]) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of %D values found is %D != %D", values[v], voff[v+1] - voff[v], vsize[v]);
125: }
126: ISRestoreIndices(valueIS, &values);
127: ISDestroy(&valueIS);
128: PetscArraycpy(cperm, sperm, numCells);
129: PetscFree3(sperm, vsize, voff);
130: }
131: /* Construct closure */
132: DMPlexCreateOrderingClosure_Static(dm, numCells, cperm, &clperm, &invclperm);
133: PetscFree3(cperm,mask,xls);
134: PetscFree(clperm);
135: /* Invert permutation */
136: DMPlexGetChart(dm, &pStart, &pEnd);
137: ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd-pStart, invclperm, PETSC_OWN_POINTER, perm);
138: return(0);
139: }
141: /*@
142: DMPlexPermute - Reorder the mesh according to the input permutation
144: Collective on dm
146: Input Parameter:
147: + dm - The DMPlex object
148: - perm - The point permutation, perm[old point number] = new point number
150: Output Parameter:
151: . pdm - The permuted DM
153: Level: intermediate
155: .seealso: MatPermute()
156: @*/
157: PetscErrorCode DMPlexPermute(DM dm, IS perm, DM *pdm)
158: {
159: DM_Plex *plex = (DM_Plex *) dm->data, *plexNew;
160: PetscSection section, sectionNew;
161: PetscInt dim;
168: DMCreate(PetscObjectComm((PetscObject) dm), pdm);
169: DMSetType(*pdm, DMPLEX);
170: DMGetDimension(dm, &dim);
171: DMSetDimension(*pdm, dim);
172: DMCopyDisc(dm, *pdm);
173: DMGetLocalSection(dm, §ion);
174: if (section) {
175: PetscSectionPermute(section, perm, §ionNew);
176: DMSetLocalSection(*pdm, sectionNew);
177: PetscSectionDestroy(§ionNew);
178: }
179: plexNew = (DM_Plex *) (*pdm)->data;
180: /* Ignore ltogmap, ltogmapb */
181: /* Ignore sf, sectionSF */
182: /* Ignore globalVertexNumbers, globalCellNumbers */
183: /* Remap coordinates */
184: {
185: DM cdm, cdmNew;
186: PetscSection csection, csectionNew;
187: Vec coordinates, coordinatesNew;
188: PetscScalar *coords, *coordsNew;
189: const PetscInt *pperm;
190: PetscInt pStart, pEnd, p;
191: const char *name;
193: DMGetCoordinateDM(dm, &cdm);
194: DMGetLocalSection(cdm, &csection);
195: PetscSectionPermute(csection, perm, &csectionNew);
196: DMGetCoordinatesLocal(dm, &coordinates);
197: VecDuplicate(coordinates, &coordinatesNew);
198: PetscObjectGetName((PetscObject)coordinates,&name);
199: PetscObjectSetName((PetscObject)coordinatesNew,name);
200: VecGetArray(coordinates, &coords);
201: VecGetArray(coordinatesNew, &coordsNew);
202: PetscSectionGetChart(csectionNew, &pStart, &pEnd);
203: ISGetIndices(perm, &pperm);
204: for (p = pStart; p < pEnd; ++p) {
205: PetscInt dof, off, offNew, d;
207: PetscSectionGetDof(csectionNew, p, &dof);
208: PetscSectionGetOffset(csection, p, &off);
209: PetscSectionGetOffset(csectionNew, pperm[p], &offNew);
210: for (d = 0; d < dof; ++d) coordsNew[offNew+d] = coords[off+d];
211: }
212: ISRestoreIndices(perm, &pperm);
213: VecRestoreArray(coordinates, &coords);
214: VecRestoreArray(coordinatesNew, &coordsNew);
215: DMGetCoordinateDM(*pdm, &cdmNew);
216: DMSetLocalSection(cdmNew, csectionNew);
217: DMSetCoordinatesLocal(*pdm, coordinatesNew);
218: PetscSectionDestroy(&csectionNew);
219: VecDestroy(&coordinatesNew);
220: }
221: /* Reorder labels */
222: {
223: PetscInt numLabels, l;
224: DMLabel label, labelNew;
226: DMGetNumLabels(dm, &numLabels);
227: for (l = numLabels-1; l >= 0; --l) {
228: DMGetLabelByNum(dm, l, &label);
229: DMLabelPermute(label, perm, &labelNew);
230: DMAddLabel(*pdm, labelNew);
231: DMLabelDestroy(&labelNew);
232: }
233: if (plex->subpointMap) {DMLabelPermute(plex->subpointMap, perm, &plexNew->subpointMap);}
234: }
235: /* Reorder topology */
236: {
237: const PetscInt *pperm;
238: PetscInt maxConeSize, maxSupportSize, n, pStart, pEnd, p;
240: DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
241: plexNew->maxConeSize = maxConeSize;
242: plexNew->maxSupportSize = maxSupportSize;
243: PetscSectionDestroy(&plexNew->coneSection);
244: PetscSectionPermute(plex->coneSection, perm, &plexNew->coneSection);
245: PetscSectionGetStorageSize(plexNew->coneSection, &n);
246: PetscMalloc1(n, &plexNew->cones);
247: PetscMalloc1(n, &plexNew->coneOrientations);
248: ISGetIndices(perm, &pperm);
249: PetscSectionGetChart(plex->coneSection, &pStart, &pEnd);
250: for (p = pStart; p < pEnd; ++p) {
251: PetscInt dof, off, offNew, d;
253: PetscSectionGetDof(plexNew->coneSection, pperm[p], &dof);
254: PetscSectionGetOffset(plex->coneSection, p, &off);
255: PetscSectionGetOffset(plexNew->coneSection, pperm[p], &offNew);
256: for (d = 0; d < dof; ++d) {
257: plexNew->cones[offNew+d] = pperm[plex->cones[off+d]];
258: plexNew->coneOrientations[offNew+d] = plex->coneOrientations[off+d];
259: }
260: }
261: PetscSectionDestroy(&plexNew->supportSection);
262: PetscSectionPermute(plex->supportSection, perm, &plexNew->supportSection);
263: PetscSectionGetStorageSize(plexNew->supportSection, &n);
264: PetscMalloc1(n, &plexNew->supports);
265: PetscSectionGetChart(plex->supportSection, &pStart, &pEnd);
266: for (p = pStart; p < pEnd; ++p) {
267: PetscInt dof, off, offNew, d;
269: PetscSectionGetDof(plexNew->supportSection, pperm[p], &dof);
270: PetscSectionGetOffset(plex->supportSection, p, &off);
271: PetscSectionGetOffset(plexNew->supportSection, pperm[p], &offNew);
272: for (d = 0; d < dof; ++d) {
273: plexNew->supports[offNew+d] = pperm[plex->supports[off+d]];
274: }
275: }
276: ISRestoreIndices(perm, &pperm);
277: }
278: DMCopyDisc(dm, *pdm);
279: (*pdm)->setupcalled = PETSC_TRUE;
280: return(0);
281: }