xref: /petsc/src/dm/impls/plex/plexreorder.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2af0996ceSBarry Smith #include <petsc/private/matorderimpl.h> /*I      "petscmat.h"      I*/
3695799ffSMatthew G. Knepley #include <petsc/private/dmlabelimpl.h>
48ed5f475SMatthew G. Knepley 
5*9371c9d4SSatish Balay static PetscErrorCode DMPlexCreateOrderingClosure_Static(DM dm, PetscInt numPoints, const PetscInt pperm[], PetscInt **clperm, PetscInt **invclperm) {
68ed5f475SMatthew G. Knepley   PetscInt *perm, *iperm;
78ed5f475SMatthew G. Knepley   PetscInt  depth, d, pStart, pEnd, fStart, fMax, fEnd, p;
88ed5f475SMatthew G. Knepley 
98ed5f475SMatthew G. Knepley   PetscFunctionBegin;
109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
119566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
129566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(pEnd - pStart, &perm));
139566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(pEnd - pStart, &iperm));
148ed5f475SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) iperm[p] = -1;
158ed5f475SMatthew G. Knepley   for (d = depth; d > 0; --d) {
169566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
179566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d - 1, &fStart, &fEnd));
188ed5f475SMatthew G. Knepley     fMax = fStart;
198ed5f475SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
208ed5f475SMatthew G. Knepley       const PetscInt *cone;
218ed5f475SMatthew G. Knepley       PetscInt        point, coneSize, c;
228ed5f475SMatthew G. Knepley 
238ed5f475SMatthew G. Knepley       if (d == depth) {
248ed5f475SMatthew G. Knepley         perm[p]         = pperm[p];
258ed5f475SMatthew G. Knepley         iperm[pperm[p]] = p;
268ed5f475SMatthew G. Knepley       }
278ed5f475SMatthew G. Knepley       point = perm[p];
289566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
308ed5f475SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
318ed5f475SMatthew G. Knepley         const PetscInt oldc = cone[c];
328ed5f475SMatthew G. Knepley         const PetscInt newc = iperm[oldc];
338ed5f475SMatthew G. Knepley 
348ed5f475SMatthew G. Knepley         if (newc < 0) {
358ed5f475SMatthew G. Knepley           perm[fMax]  = oldc;
368ed5f475SMatthew G. Knepley           iperm[oldc] = fMax++;
378ed5f475SMatthew G. Knepley         }
388ed5f475SMatthew G. Knepley       }
398ed5f475SMatthew G. Knepley     }
4063a3b9bcSJacob Faibussowitsch     PetscCheck(fMax == fEnd, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of depth %" PetscInt_FMT " faces %" PetscInt_FMT " does not match permuted number %" PetscInt_FMT, d, fEnd - fStart, fMax - fStart);
418ed5f475SMatthew G. Knepley   }
428ed5f475SMatthew G. Knepley   *clperm    = perm;
438ed5f475SMatthew G. Knepley   *invclperm = iperm;
448ed5f475SMatthew G. Knepley   PetscFunctionReturn(0);
458ed5f475SMatthew G. Knepley }
468ed5f475SMatthew G. Knepley 
478ed5f475SMatthew G. Knepley /*@
488ed5f475SMatthew G. Knepley   DMPlexGetOrdering - Calculate a reordering of the mesh
498ed5f475SMatthew G. Knepley 
50d083f849SBarry Smith   Collective on dm
518ed5f475SMatthew G. Knepley 
52d8d19677SJose E. Roman   Input Parameters:
538ed5f475SMatthew G. Knepley + dm - The DMPlex object
54c99f2573SMatthew G. Knepley . otype - type of reordering, one of the following:
558ed5f475SMatthew G. Knepley $     MATORDERINGNATURAL - Natural
568ed5f475SMatthew G. Knepley $     MATORDERINGND - Nested Dissection
578ed5f475SMatthew G. Knepley $     MATORDERING1WD - One-way Dissection
588ed5f475SMatthew G. Knepley $     MATORDERINGRCM - Reverse Cuthill-McKee
598ed5f475SMatthew G. Knepley $     MATORDERINGQMD - Quotient Minimum Degree
60c99f2573SMatthew G. Knepley - label - [Optional] Label used to segregate ordering into sets, or NULL
618ed5f475SMatthew G. Knepley 
628ed5f475SMatthew G. Knepley   Output Parameter:
63c99f2573SMatthew G. Knepley . perm - The point permutation as an IS, perm[old point number] = new point number
64c99f2573SMatthew G. Knepley 
65c99f2573SMatthew G. Knepley   Note: The label is used to group sets of points together by label value. This makes it easy to reorder a mesh which
66c99f2573SMatthew G. Knepley   has different types of cells, and then loop over each set of reordered cells for assembly.
678ed5f475SMatthew G. Knepley 
688ed5f475SMatthew G. Knepley   Level: intermediate
698ed5f475SMatthew G. Knepley 
70db781477SPatrick Sanan .seealso: `DMPlexPermute()`, `MatGetOrdering()`
718ed5f475SMatthew G. Knepley @*/
72*9371c9d4SSatish Balay PetscErrorCode DMPlexGetOrdering(DM dm, MatOrderingType otype, DMLabel label, IS *perm) {
738ed5f475SMatthew G. Knepley   PetscInt  numCells = 0;
743c67d5adSSatish Balay   PetscInt *start = NULL, *adjacency = NULL, *cperm, *clperm = NULL, *invclperm = NULL, *mask, *xls, pStart, pEnd, c, i;
758ed5f475SMatthew G. Knepley 
768ed5f475SMatthew G. Knepley   PetscFunctionBegin;
778ed5f475SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78064a246eSJacob Faibussowitsch   PetscValidPointer(perm, 4);
799566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateNeighborCSR(dm, 0, &numCells, &start, &adjacency));
809566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(numCells, &cperm, numCells, &mask, numCells * 2, &xls));
81d80c2872SMatthew G. Knepley   if (numCells) {
828ed5f475SMatthew G. Knepley     /* Shift for Fortran numbering */
838ed5f475SMatthew G. Knepley     for (i = 0; i < start[numCells]; ++i) ++adjacency[i];
848ed5f475SMatthew G. Knepley     for (i = 0; i <= numCells; ++i) ++start[i];
859566063dSJacob Faibussowitsch     PetscCall(SPARSEPACKgenrcm(&numCells, start, adjacency, cperm, mask, xls));
86d80c2872SMatthew G. Knepley   }
879566063dSJacob Faibussowitsch   PetscCall(PetscFree(start));
889566063dSJacob Faibussowitsch   PetscCall(PetscFree(adjacency));
898ed5f475SMatthew G. Knepley   /* Shift for Fortran numbering */
908ed5f475SMatthew G. Knepley   for (c = 0; c < numCells; ++c) --cperm[c];
91c99f2573SMatthew G. Knepley   /* Segregate */
92c99f2573SMatthew G. Knepley   if (label) {
93c99f2573SMatthew G. Knepley     IS              valueIS;
9450be1cf5SMatthew G. Knepley     const PetscInt *valuesTmp;
9550be1cf5SMatthew G. Knepley     PetscInt       *values;
96c99f2573SMatthew G. Knepley     PetscInt        numValues, numPoints = 0;
97c99f2573SMatthew G. Knepley     PetscInt       *sperm, *vsize, *voff, v;
98c99f2573SMatthew G. Knepley 
9950be1cf5SMatthew G. Knepley     // Can't directly sort the valueIS, since it is a view into the DMLabel
1009566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
1019566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
10250be1cf5SMatthew G. Knepley     PetscCall(ISGetIndices(valueIS, &valuesTmp));
10350be1cf5SMatthew G. Knepley     PetscCall(PetscCalloc4(numCells, &sperm, numValues, &values, numValues, &vsize, numValues + 1, &voff));
10450be1cf5SMatthew G. Knepley     PetscCall(PetscArraycpy(values, valuesTmp, numValues));
10550be1cf5SMatthew G. Knepley     PetscCall(PetscSortInt(numValues, values));
10650be1cf5SMatthew G. Knepley     PetscCall(ISRestoreIndices(valueIS, &valuesTmp));
10750be1cf5SMatthew G. Knepley     PetscCall(ISDestroy(&valueIS));
108c99f2573SMatthew G. Knepley     for (v = 0; v < numValues; ++v) {
1099566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(label, values[v], &vsize[v]));
110c99f2573SMatthew G. Knepley       if (v < numValues - 1) voff[v + 2] += vsize[v] + voff[v + 1];
111c99f2573SMatthew G. Knepley       numPoints += vsize[v];
112c99f2573SMatthew G. Knepley     }
1136469bafaSMatthew G. Knepley     PetscCheck(numPoints == numCells, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label only covers %" PetscInt_FMT " cells != %" PetscInt_FMT " total cells", numPoints, numCells);
114c99f2573SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
115c99f2573SMatthew G. Knepley       const PetscInt oldc = cperm[c];
116c99f2573SMatthew G. Knepley       PetscInt       val, vloc;
117c99f2573SMatthew G. Knepley 
1189566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, oldc, &val));
11963a3b9bcSJacob Faibussowitsch       PetscCheck(val != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " not present in label", oldc);
1209566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, numValues, values, &vloc));
12163a3b9bcSJacob Faibussowitsch       PetscCheck(vloc >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Value %" PetscInt_FMT " not present label", val);
122c99f2573SMatthew G. Knepley       sperm[voff[vloc + 1]++] = oldc;
123c99f2573SMatthew G. Knepley     }
124*9371c9d4SSatish Balay     for (v = 0; v < numValues; ++v) { PetscCheck(voff[v + 1] - voff[v] == vsize[v], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of %" PetscInt_FMT " values found is %" PetscInt_FMT " != %" PetscInt_FMT, values[v], voff[v + 1] - voff[v], vsize[v]); }
1259566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(cperm, sperm, numCells));
12650be1cf5SMatthew G. Knepley     PetscCall(PetscFree4(sperm, values, vsize, voff));
127c99f2573SMatthew G. Knepley   }
1288ed5f475SMatthew G. Knepley   /* Construct closure */
1299566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateOrderingClosure_Static(dm, numCells, cperm, &clperm, &invclperm));
1309566063dSJacob Faibussowitsch   PetscCall(PetscFree3(cperm, mask, xls));
1319566063dSJacob Faibussowitsch   PetscCall(PetscFree(clperm));
1328ed5f475SMatthew G. Knepley   /* Invert permutation */
1339566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1349566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), pEnd - pStart, invclperm, PETSC_OWN_POINTER, perm));
1358ed5f475SMatthew G. Knepley   PetscFunctionReturn(0);
1368ed5f475SMatthew G. Knepley }
1378ed5f475SMatthew G. Knepley 
1388ed5f475SMatthew G. Knepley /*@
1396913077dSMatthew G. Knepley   DMPlexGetOrdering1D - Reorder the vertices so that the mesh is in a line
1406913077dSMatthew G. Knepley 
1416913077dSMatthew G. Knepley   Collective on dm
1426913077dSMatthew G. Knepley 
1436913077dSMatthew G. Knepley   Input Parameter:
1446913077dSMatthew G. Knepley . dm - The DMPlex object
1456913077dSMatthew G. Knepley 
1466913077dSMatthew G. Knepley   Output Parameter:
1476913077dSMatthew G. Knepley . perm - The point permutation as an IS, perm[old point number] = new point number
1486913077dSMatthew G. Knepley 
1496913077dSMatthew G. Knepley   Level: intermediate
1506913077dSMatthew G. Knepley 
151db781477SPatrick Sanan .seealso: `DMPlexGetOrdering()`, `DMPlexPermute()`, `MatGetOrdering()`
1526913077dSMatthew G. Knepley @*/
153*9371c9d4SSatish Balay PetscErrorCode DMPlexGetOrdering1D(DM dm, IS *perm) {
1546913077dSMatthew G. Knepley   PetscInt       *points;
1556913077dSMatthew G. Knepley   const PetscInt *support, *cone;
1566913077dSMatthew G. Knepley   PetscInt        dim, pStart, pEnd, cStart, cEnd, c, vStart, vEnd, v, suppSize, lastCell = 0;
1576913077dSMatthew G. Knepley 
1586913077dSMatthew G. Knepley   PetscFunctionBegin;
1599566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1606913077dSMatthew G. Knepley   PetscCheck(dim == 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Input mesh must be one dimensional, not %" PetscInt_FMT, dim);
1619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
1639566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1649566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(pEnd - pStart, &points));
1656913077dSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) points[c] = c;
1666913077dSMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) points[v] = v;
1676913077dSMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
1689566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, v, &suppSize));
1699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, v, &support));
170*9371c9d4SSatish Balay     if (suppSize == 1) {
171*9371c9d4SSatish Balay       lastCell = support[0];
172*9371c9d4SSatish Balay       break;
173*9371c9d4SSatish Balay     }
1746913077dSMatthew G. Knepley   }
1756913077dSMatthew G. Knepley   if (v < vEnd) {
1766913077dSMatthew G. Knepley     PetscInt pos = cEnd;
1776913077dSMatthew G. Knepley 
1786913077dSMatthew G. Knepley     points[v] = pos++;
1796913077dSMatthew G. Knepley     while (lastCell >= cStart) {
1809566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, lastCell, &cone));
1816913077dSMatthew G. Knepley       if (cone[0] == v) v = cone[1];
1826913077dSMatthew G. Knepley       else v = cone[0];
1839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, v, &support));
1849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, v, &suppSize));
185*9371c9d4SSatish Balay       if (suppSize == 1) {
186*9371c9d4SSatish Balay         lastCell = -1;
187*9371c9d4SSatish Balay       } else {
1886913077dSMatthew G. Knepley         if (support[0] == lastCell) lastCell = support[1];
1896913077dSMatthew G. Knepley         else lastCell = support[0];
1906913077dSMatthew G. Knepley       }
1916913077dSMatthew G. Knepley       points[v] = pos++;
1926913077dSMatthew G. Knepley     }
1936913077dSMatthew G. Knepley     PetscCheck(pos == pEnd, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Last vertex was %" PetscInt_FMT ", not %" PetscInt_FMT, pos, pEnd);
1946913077dSMatthew G. Knepley   }
1959566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), pEnd - pStart, points, PETSC_OWN_POINTER, perm));
1966913077dSMatthew G. Knepley   PetscFunctionReturn(0);
1976913077dSMatthew G. Knepley }
1986913077dSMatthew G. Knepley 
199*9371c9d4SSatish Balay static PetscErrorCode DMPlexRemapCoordinates_Private(IS perm, PetscSection cs, Vec coordinates, PetscSection *csNew, Vec *coordinatesNew) {
2006858538eSMatthew G. Knepley   PetscScalar    *coords, *coordsNew;
2016858538eSMatthew G. Knepley   const PetscInt *pperm;
2026858538eSMatthew G. Knepley   PetscInt        pStart, pEnd, p;
2036858538eSMatthew G. Knepley   const char     *name;
2046858538eSMatthew G. Knepley 
2056858538eSMatthew G. Knepley   PetscFunctionBegin;
2066858538eSMatthew G. Knepley   PetscCall(PetscSectionPermute(cs, perm, csNew));
2076858538eSMatthew G. Knepley   PetscCall(VecDuplicate(coordinates, coordinatesNew));
2086858538eSMatthew G. Knepley   PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
2096858538eSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)*coordinatesNew, name));
2106858538eSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
2116858538eSMatthew G. Knepley   PetscCall(VecGetArray(*coordinatesNew, &coordsNew));
2126858538eSMatthew G. Knepley   PetscCall(PetscSectionGetChart(*csNew, &pStart, &pEnd));
2136858538eSMatthew G. Knepley   PetscCall(ISGetIndices(perm, &pperm));
2146858538eSMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2156858538eSMatthew G. Knepley     PetscInt dof, off, offNew, d;
2166858538eSMatthew G. Knepley 
2176858538eSMatthew G. Knepley     PetscCall(PetscSectionGetDof(*csNew, p, &dof));
2186858538eSMatthew G. Knepley     PetscCall(PetscSectionGetOffset(cs, p, &off));
2196858538eSMatthew G. Knepley     PetscCall(PetscSectionGetOffset(*csNew, pperm[p], &offNew));
2206858538eSMatthew G. Knepley     for (d = 0; d < dof; ++d) coordsNew[offNew + d] = coords[off + d];
2216858538eSMatthew G. Knepley   }
2226858538eSMatthew G. Knepley   PetscCall(ISRestoreIndices(perm, &pperm));
2236858538eSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
2246858538eSMatthew G. Knepley   PetscCall(VecRestoreArray(*coordinatesNew, &coordsNew));
2256858538eSMatthew G. Knepley   PetscFunctionReturn(0);
2266858538eSMatthew G. Knepley }
2276858538eSMatthew G. Knepley 
2286913077dSMatthew G. Knepley /*@
2298ed5f475SMatthew G. Knepley   DMPlexPermute - Reorder the mesh according to the input permutation
2308ed5f475SMatthew G. Knepley 
231d083f849SBarry Smith   Collective on dm
2328ed5f475SMatthew G. Knepley 
233d8d19677SJose E. Roman   Input Parameters:
2348ed5f475SMatthew G. Knepley + dm - The DMPlex object
235c99f2573SMatthew G. Knepley - perm - The point permutation, perm[old point number] = new point number
2368ed5f475SMatthew G. Knepley 
2378ed5f475SMatthew G. Knepley   Output Parameter:
2388ed5f475SMatthew G. Knepley . pdm - The permuted DM
2398ed5f475SMatthew G. Knepley 
2408ed5f475SMatthew G. Knepley   Level: intermediate
2418ed5f475SMatthew G. Knepley 
242db781477SPatrick Sanan .seealso: `MatPermute()`
2438ed5f475SMatthew G. Knepley @*/
244*9371c9d4SSatish Balay PetscErrorCode DMPlexPermute(DM dm, IS perm, DM *pdm) {
2458ed5f475SMatthew G. Knepley   DM_Plex    *plex = (DM_Plex *)dm->data, *plexNew;
2462f300c92SMatthew Knepley   PetscInt    dim, cdim;
2476b74b800SMatthew G. Knepley   const char *name;
2488ed5f475SMatthew G. Knepley 
2498ed5f475SMatthew G. Knepley   PetscFunctionBegin;
2508ed5f475SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2518ed5f475SMatthew G. Knepley   PetscValidHeaderSpecific(perm, IS_CLASSID, 2);
2528ed5f475SMatthew G. Knepley   PetscValidPointer(pdm, 3);
2539566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), pdm));
2549566063dSJacob Faibussowitsch   PetscCall(DMSetType(*pdm, DMPLEX));
2559566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)dm, &name));
2569566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)*pdm, name));
2579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
2589566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*pdm, dim));
2599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
2609566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*pdm, cdim));
2619566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, *pdm));
2626b74b800SMatthew G. Knepley   if (dm->localSection) {
2636b74b800SMatthew G. Knepley     PetscSection section, sectionNew;
2646b74b800SMatthew G. Knepley 
2659566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
2669566063dSJacob Faibussowitsch     PetscCall(PetscSectionPermute(section, perm, &sectionNew));
2679566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(*pdm, sectionNew));
2689566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&sectionNew));
269dd742cf1SMatthew G. Knepley   }
2708ed5f475SMatthew G. Knepley   plexNew = (DM_Plex *)(*pdm)->data;
2718ed5f475SMatthew G. Knepley   /* Ignore ltogmap, ltogmapb */
2721bb6d2a8SBarry Smith   /* Ignore sf, sectionSF */
2738ed5f475SMatthew G. Knepley   /* Ignore globalVertexNumbers, globalCellNumbers */
2748ed5f475SMatthew G. Knepley   /* Reorder labels */
2758ed5f475SMatthew G. Knepley   {
276a85475f2SMatthew G. Knepley     PetscInt numLabels, l;
277a85475f2SMatthew G. Knepley     DMLabel  label, labelNew;
2788ed5f475SMatthew G. Knepley 
2799566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm, &numLabels));
2805d80c0bfSVaclav Hapla     for (l = 0; l < numLabels; ++l) {
2819566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
2829566063dSJacob Faibussowitsch       PetscCall(DMLabelPermute(label, perm, &labelNew));
2839566063dSJacob Faibussowitsch       PetscCall(DMAddLabel(*pdm, labelNew));
2849566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&labelNew));
2858ed5f475SMatthew G. Knepley     }
2869566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(*pdm, "depth", &(*pdm)->depthLabel));
2879566063dSJacob Faibussowitsch     if (plex->subpointMap) PetscCall(DMLabelPermute(plex->subpointMap, perm, &plexNew->subpointMap));
2888ed5f475SMatthew G. Knepley   }
289695799ffSMatthew G. Knepley   if ((*pdm)->celltypeLabel) {
290695799ffSMatthew G. Knepley     DMLabel ctLabel;
291695799ffSMatthew G. Knepley 
292695799ffSMatthew G. Knepley     // Reset label for fast lookup
293695799ffSMatthew G. Knepley     PetscCall(DMPlexGetCellTypeLabel(*pdm, &ctLabel));
294695799ffSMatthew G. Knepley     PetscCall(DMLabelMakeAllInvalid_Internal(ctLabel));
295695799ffSMatthew G. Knepley   }
2968ed5f475SMatthew G. Knepley   /* Reorder topology */
2978ed5f475SMatthew G. Knepley   {
2988ed5f475SMatthew G. Knepley     const PetscInt *pperm;
2996302a7fbSVaclav Hapla     PetscInt        n, pStart, pEnd, p;
3008ed5f475SMatthew G. Knepley 
3019566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&plexNew->coneSection));
3029566063dSJacob Faibussowitsch     PetscCall(PetscSectionPermute(plex->coneSection, perm, &plexNew->coneSection));
3039566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(plexNew->coneSection, &n));
3049566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &plexNew->cones));
3059566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &plexNew->coneOrientations));
3069566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(perm, &pperm));
3079566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(plex->coneSection, &pStart, &pEnd));
3088ed5f475SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
3098ed5f475SMatthew G. Knepley       PetscInt dof, off, offNew, d;
3108ed5f475SMatthew G. Knepley 
3119566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(plexNew->coneSection, pperm[p], &dof));
3129566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(plex->coneSection, p, &off));
3139566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(plexNew->coneSection, pperm[p], &offNew));
3148ed5f475SMatthew G. Knepley       for (d = 0; d < dof; ++d) {
3158ed5f475SMatthew G. Knepley         plexNew->cones[offNew + d]            = pperm[plex->cones[off + d]];
3168ed5f475SMatthew G. Knepley         plexNew->coneOrientations[offNew + d] = plex->coneOrientations[off + d];
3178ed5f475SMatthew G. Knepley       }
3188ed5f475SMatthew G. Knepley     }
3199566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&plexNew->supportSection));
3209566063dSJacob Faibussowitsch     PetscCall(PetscSectionPermute(plex->supportSection, perm, &plexNew->supportSection));
3219566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(plexNew->supportSection, &n));
3229566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &plexNew->supports));
3239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(plex->supportSection, &pStart, &pEnd));
3248ed5f475SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
3258ed5f475SMatthew G. Knepley       PetscInt dof, off, offNew, d;
3268ed5f475SMatthew G. Knepley 
3279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(plexNew->supportSection, pperm[p], &dof));
3289566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(plex->supportSection, p, &off));
3299566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(plexNew->supportSection, pperm[p], &offNew));
330*9371c9d4SSatish Balay       for (d = 0; d < dof; ++d) { plexNew->supports[offNew + d] = pperm[plex->supports[off + d]]; }
3318ed5f475SMatthew G. Knepley     }
3329566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(perm, &pperm));
3338ed5f475SMatthew G. Knepley   }
334a6e0b375SMatthew G. Knepley   /* Remap coordinates */
335a6e0b375SMatthew G. Knepley   {
336a6e0b375SMatthew G. Knepley     DM           cdm, cdmNew;
3376858538eSMatthew G. Knepley     PetscSection cs, csNew;
338a6e0b375SMatthew G. Knepley     Vec          coordinates, coordinatesNew;
339a6e0b375SMatthew G. Knepley 
3406858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateSection(dm, &cs));
3419566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
3426858538eSMatthew G. Knepley     PetscCall(DMPlexRemapCoordinates_Private(perm, cs, coordinates, &csNew, &coordinatesNew));
3436858538eSMatthew G. Knepley     PetscCall(DMSetCoordinateSection(*pdm, PETSC_DETERMINE, csNew));
3449566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*pdm, coordinatesNew));
3456858538eSMatthew G. Knepley     PetscCall(PetscSectionDestroy(&csNew));
3469566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinatesNew));
3476858538eSMatthew G. Knepley 
3486858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinateDM(dm, &cdm));
3496858538eSMatthew G. Knepley     if (cdm) {
3506858538eSMatthew G. Knepley       PetscCall(DMGetCoordinateDM(*pdm, &cdm));
3516858538eSMatthew G. Knepley       PetscCall(DMClone(cdm, &cdmNew));
3526858538eSMatthew G. Knepley       PetscCall(DMSetCellCoordinateDM(*pdm, cdmNew));
3536858538eSMatthew G. Knepley       PetscCall(DMDestroy(&cdmNew));
3546858538eSMatthew G. Knepley       PetscCall(DMGetCellCoordinateSection(dm, &cs));
3556858538eSMatthew G. Knepley       PetscCall(DMGetCellCoordinatesLocal(dm, &coordinates));
3566858538eSMatthew G. Knepley       PetscCall(DMPlexRemapCoordinates_Private(perm, cs, coordinates, &csNew, &coordinatesNew));
3576858538eSMatthew G. Knepley       PetscCall(DMSetCellCoordinateSection(*pdm, PETSC_DETERMINE, csNew));
3586858538eSMatthew G. Knepley       PetscCall(DMSetCellCoordinatesLocal(*pdm, coordinatesNew));
3596858538eSMatthew G. Knepley       PetscCall(PetscSectionDestroy(&csNew));
3606858538eSMatthew G. Knepley       PetscCall(VecDestroy(&coordinatesNew));
3616858538eSMatthew G. Knepley     }
362a6e0b375SMatthew G. Knepley   }
3635de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *pdm));
364e66f2fa0SMatthew G. Knepley   (*pdm)->setupcalled = PETSC_TRUE;
3658ed5f475SMatthew G. Knepley   PetscFunctionReturn(0);
3668ed5f475SMatthew G. Knepley }
3676bc1bd01Sksagiyam 
368*9371c9d4SSatish Balay PetscErrorCode DMPlexReorderSetDefault_Plex(DM dm, DMPlexReorderDefaultFlag reorder) {
3696bc1bd01Sksagiyam   DM_Plex *mesh = (DM_Plex *)dm->data;
3706bc1bd01Sksagiyam 
3716bc1bd01Sksagiyam   PetscFunctionBegin;
3726bc1bd01Sksagiyam   mesh->reorderDefault = reorder;
3736bc1bd01Sksagiyam   PetscFunctionReturn(0);
3746bc1bd01Sksagiyam }
3756bc1bd01Sksagiyam 
3766bc1bd01Sksagiyam /*@
3776bc1bd01Sksagiyam   DMPlexReorderSetDefault - Set flag indicating whether the DM should be reordered by default
3786bc1bd01Sksagiyam 
3796bc1bd01Sksagiyam   Logically collective
3806bc1bd01Sksagiyam 
3816bc1bd01Sksagiyam   Input Parameters:
3826bc1bd01Sksagiyam + dm        - The DM
3836bc1bd01Sksagiyam - reorder   - Flag for reordering
3846bc1bd01Sksagiyam 
3856bc1bd01Sksagiyam   Level: intermediate
3866bc1bd01Sksagiyam 
3876bc1bd01Sksagiyam .seealso: `DMPlexReorderGetDefault()`
3886bc1bd01Sksagiyam @*/
389*9371c9d4SSatish Balay PetscErrorCode DMPlexReorderSetDefault(DM dm, DMPlexReorderDefaultFlag reorder) {
3906bc1bd01Sksagiyam   PetscFunctionBegin;
3916bc1bd01Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3926bc1bd01Sksagiyam   PetscTryMethod(dm, "DMPlexReorderSetDefault_C", (DM, DMPlexReorderDefaultFlag), (dm, reorder));
3936bc1bd01Sksagiyam   PetscFunctionReturn(0);
3946bc1bd01Sksagiyam }
3956bc1bd01Sksagiyam 
396*9371c9d4SSatish Balay PetscErrorCode DMPlexReorderGetDefault_Plex(DM dm, DMPlexReorderDefaultFlag *reorder) {
3976bc1bd01Sksagiyam   DM_Plex *mesh = (DM_Plex *)dm->data;
3986bc1bd01Sksagiyam 
3996bc1bd01Sksagiyam   PetscFunctionBegin;
4006bc1bd01Sksagiyam   *reorder = mesh->reorderDefault;
4016bc1bd01Sksagiyam   PetscFunctionReturn(0);
4026bc1bd01Sksagiyam }
4036bc1bd01Sksagiyam 
4046bc1bd01Sksagiyam /*@
4056bc1bd01Sksagiyam   DMPlexReorderGetDefault - Get flag indicating whether the DM should be reordered by default
4066bc1bd01Sksagiyam 
4076bc1bd01Sksagiyam   Not collective
4086bc1bd01Sksagiyam 
4096bc1bd01Sksagiyam   Input Parameter:
4106bc1bd01Sksagiyam . dm      - The DM
4116bc1bd01Sksagiyam 
4126bc1bd01Sksagiyam   Output Parameter:
4136bc1bd01Sksagiyam . reorder - Flag for reordering
4146bc1bd01Sksagiyam 
4156bc1bd01Sksagiyam   Level: intermediate
4166bc1bd01Sksagiyam 
4176bc1bd01Sksagiyam .seealso: `DMPlexReorderSetDefault()`
4186bc1bd01Sksagiyam @*/
419*9371c9d4SSatish Balay PetscErrorCode DMPlexReorderGetDefault(DM dm, DMPlexReorderDefaultFlag *reorder) {
4206bc1bd01Sksagiyam   PetscFunctionBegin;
4216bc1bd01Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4226bc1bd01Sksagiyam   PetscValidPointer(reorder, 2);
4236bc1bd01Sksagiyam   PetscUseMethod(dm, "DMPlexReorderGetDefault_C", (DM, DMPlexReorderDefaultFlag *), (dm, reorder));
4246bc1bd01Sksagiyam   PetscFunctionReturn(0);
4256bc1bd01Sksagiyam }
426