xref: /petsc/src/dm/impls/plex/plexreorder.c (revision d8d19677bbccf95218448bee62e6b87f4513e133)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2af0996ceSBarry Smith #include <petsc/private/matorderimpl.h> /*I      "petscmat.h"      I*/
38ed5f475SMatthew G. Knepley 
4a1fd77bcSMatthew G. Knepley static PetscErrorCode DMPlexCreateOrderingClosure_Static(DM dm, PetscInt numPoints, const PetscInt pperm[], PetscInt **clperm, PetscInt **invclperm)
58ed5f475SMatthew G. Knepley {
68ed5f475SMatthew G. Knepley   PetscInt      *perm, *iperm;
78ed5f475SMatthew G. Knepley   PetscInt       depth, d, pStart, pEnd, fStart, fMax, fEnd, p;
88ed5f475SMatthew G. Knepley   PetscErrorCode ierr;
98ed5f475SMatthew G. Knepley 
108ed5f475SMatthew G. Knepley   PetscFunctionBegin;
118ed5f475SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
128ed5f475SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
13854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd-pStart,&perm);CHKERRQ(ierr);
14854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd-pStart,&iperm);CHKERRQ(ierr);
158ed5f475SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) iperm[p] = -1;
168ed5f475SMatthew G. Knepley   for (d = depth; d > 0; --d) {
178ed5f475SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d,   &pStart, &pEnd);CHKERRQ(ierr);
188ed5f475SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d-1, &fStart, &fEnd);CHKERRQ(ierr);
198ed5f475SMatthew G. Knepley     fMax = fStart;
208ed5f475SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
218ed5f475SMatthew G. Knepley       const PetscInt *cone;
228ed5f475SMatthew G. Knepley       PetscInt        point, coneSize, c;
238ed5f475SMatthew G. Knepley 
248ed5f475SMatthew G. Knepley       if (d == depth) {
258ed5f475SMatthew G. Knepley         perm[p]         = pperm[p];
268ed5f475SMatthew G. Knepley         iperm[pperm[p]] = p;
278ed5f475SMatthew G. Knepley       }
288ed5f475SMatthew G. Knepley       point = perm[p];
298ed5f475SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
308ed5f475SMatthew G. Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
318ed5f475SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
328ed5f475SMatthew G. Knepley         const PetscInt oldc = cone[c];
338ed5f475SMatthew G. Knepley         const PetscInt newc = iperm[oldc];
348ed5f475SMatthew G. Knepley 
358ed5f475SMatthew G. Knepley         if (newc < 0) {
368ed5f475SMatthew G. Knepley           perm[fMax]  = oldc;
378ed5f475SMatthew G. Knepley           iperm[oldc] = fMax++;
388ed5f475SMatthew G. Knepley         }
398ed5f475SMatthew G. Knepley       }
408ed5f475SMatthew G. Knepley     }
412479783cSJose E. Roman     if (fMax != fEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of depth %d faces %d does not match permuted number %d", d, fEnd-fStart, fMax-fStart);
428ed5f475SMatthew G. Knepley   }
438ed5f475SMatthew G. Knepley   *clperm    = perm;
448ed5f475SMatthew G. Knepley   *invclperm = iperm;
458ed5f475SMatthew G. Knepley   PetscFunctionReturn(0);
468ed5f475SMatthew G. Knepley }
478ed5f475SMatthew G. Knepley 
488ed5f475SMatthew G. Knepley /*@
498ed5f475SMatthew G. Knepley   DMPlexGetOrdering - Calculate a reordering of the mesh
508ed5f475SMatthew G. Knepley 
51d083f849SBarry Smith   Collective on dm
528ed5f475SMatthew G. Knepley 
53*d8d19677SJose E. Roman   Input Parameters:
548ed5f475SMatthew G. Knepley + dm - The DMPlex object
55c99f2573SMatthew G. Knepley . otype - type of reordering, one of the following:
568ed5f475SMatthew G. Knepley $     MATORDERINGNATURAL - Natural
578ed5f475SMatthew G. Knepley $     MATORDERINGND - Nested Dissection
588ed5f475SMatthew G. Knepley $     MATORDERING1WD - One-way Dissection
598ed5f475SMatthew G. Knepley $     MATORDERINGRCM - Reverse Cuthill-McKee
608ed5f475SMatthew G. Knepley $     MATORDERINGQMD - Quotient Minimum Degree
61c99f2573SMatthew G. Knepley - label - [Optional] Label used to segregate ordering into sets, or NULL
628ed5f475SMatthew G. Knepley 
638ed5f475SMatthew G. Knepley   Output Parameter:
64c99f2573SMatthew G. Knepley . perm - The point permutation as an IS, perm[old point number] = new point number
65c99f2573SMatthew G. Knepley 
66c99f2573SMatthew 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
67c99f2573SMatthew G. Knepley   has different types of cells, and then loop over each set of reordered cells for assembly.
688ed5f475SMatthew G. Knepley 
698ed5f475SMatthew G. Knepley   Level: intermediate
708ed5f475SMatthew G. Knepley 
718ed5f475SMatthew G. Knepley .seealso: MatGetOrdering()
728ed5f475SMatthew G. Knepley @*/
73c99f2573SMatthew G. Knepley PetscErrorCode DMPlexGetOrdering(DM dm, MatOrderingType otype, DMLabel label, IS *perm)
748ed5f475SMatthew G. Knepley {
758ed5f475SMatthew G. Knepley   PetscInt       numCells = 0;
763c67d5adSSatish Balay   PetscInt      *start = NULL, *adjacency = NULL, *cperm, *clperm = NULL, *invclperm = NULL, *mask, *xls, pStart, pEnd, c, i;
778ed5f475SMatthew G. Knepley   PetscErrorCode ierr;
788ed5f475SMatthew G. Knepley 
798ed5f475SMatthew G. Knepley   PetscFunctionBegin;
808ed5f475SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81064a246eSJacob Faibussowitsch   PetscValidPointer(perm, 4);
828ed5f475SMatthew G. Knepley   ierr = DMPlexCreateNeighborCSR(dm, 0, &numCells, &start, &adjacency);CHKERRQ(ierr);
83dcca6d9dSJed Brown   ierr = PetscMalloc3(numCells,&cperm,numCells,&mask,numCells*2,&xls);CHKERRQ(ierr);
84d80c2872SMatthew G. Knepley   if (numCells) {
858ed5f475SMatthew G. Knepley     /* Shift for Fortran numbering */
868ed5f475SMatthew G. Knepley     for (i = 0; i < start[numCells]; ++i) ++adjacency[i];
878ed5f475SMatthew G. Knepley     for (i = 0; i <= numCells; ++i)       ++start[i];
888ed5f475SMatthew G. Knepley     ierr = SPARSEPACKgenrcm(&numCells, start, adjacency, cperm, mask, xls);CHKERRQ(ierr);
89d80c2872SMatthew G. Knepley   }
908ed5f475SMatthew G. Knepley   ierr = PetscFree(start);CHKERRQ(ierr);
918ed5f475SMatthew G. Knepley   ierr = PetscFree(adjacency);CHKERRQ(ierr);
928ed5f475SMatthew G. Knepley   /* Shift for Fortran numbering */
938ed5f475SMatthew G. Knepley   for (c = 0; c < numCells; ++c) --cperm[c];
94c99f2573SMatthew G. Knepley   /* Segregate */
95c99f2573SMatthew G. Knepley   if (label) {
96c99f2573SMatthew G. Knepley     IS              valueIS;
97c99f2573SMatthew G. Knepley     const PetscInt *values;
98c99f2573SMatthew G. Knepley     PetscInt        numValues, numPoints = 0;
99c99f2573SMatthew G. Knepley     PetscInt       *sperm, *vsize, *voff, v;
100c99f2573SMatthew G. Knepley 
101c99f2573SMatthew G. Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1029485cce2SMatthew G. Knepley     ierr = ISSort(valueIS);CHKERRQ(ierr);
103c99f2573SMatthew G. Knepley     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
104c99f2573SMatthew G. Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
105c99f2573SMatthew G. Knepley     ierr = PetscCalloc3(numCells,&sperm,numValues,&vsize,numValues+1,&voff);CHKERRQ(ierr);
106c99f2573SMatthew G. Knepley     for (v = 0; v < numValues; ++v) {
107c99f2573SMatthew G. Knepley       ierr = DMLabelGetStratumSize(label, values[v], &vsize[v]);CHKERRQ(ierr);
108c99f2573SMatthew G. Knepley       if (v < numValues-1) voff[v+2] += vsize[v] + voff[v+1];
109c99f2573SMatthew G. Knepley       numPoints += vsize[v];
110c99f2573SMatthew G. Knepley     }
111c99f2573SMatthew G. Knepley     if (numPoints != numCells) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label only covers %D cells < %D total", numPoints, numCells);
112c99f2573SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
113c99f2573SMatthew G. Knepley       const PetscInt oldc = cperm[c];
114c99f2573SMatthew G. Knepley       PetscInt       val, vloc;
115c99f2573SMatthew G. Knepley 
116c99f2573SMatthew G. Knepley       ierr = DMLabelGetValue(label, oldc, &val);CHKERRQ(ierr);
117c99f2573SMatthew G. Knepley       if (val == -1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cell %D not present in label", oldc);
118c99f2573SMatthew G. Knepley       ierr = PetscFindInt(val, numValues, values, &vloc);CHKERRQ(ierr);
119c99f2573SMatthew G. Knepley       if (vloc < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Value %D not present label", val);
120c99f2573SMatthew G. Knepley       sperm[voff[vloc+1]++] = oldc;
121c99f2573SMatthew G. Knepley     }
122c99f2573SMatthew G. Knepley     for (v = 0; v < numValues; ++v) {
123c99f2573SMatthew G. Knepley       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]);
124c99f2573SMatthew G. Knepley     }
125c99f2573SMatthew G. Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
126c99f2573SMatthew G. Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
127580bdb30SBarry Smith     ierr = PetscArraycpy(cperm, sperm, numCells);CHKERRQ(ierr);
128c99f2573SMatthew G. Knepley     ierr = PetscFree3(sperm, vsize, voff);CHKERRQ(ierr);
129c99f2573SMatthew G. Knepley   }
1308ed5f475SMatthew G. Knepley   /* Construct closure */
131302440fdSBarry Smith   ierr = DMPlexCreateOrderingClosure_Static(dm, numCells, cperm, &clperm, &invclperm);CHKERRQ(ierr);
1328ed5f475SMatthew G. Knepley   ierr = PetscFree3(cperm,mask,xls);CHKERRQ(ierr);
1338ed5f475SMatthew G. Knepley   ierr = PetscFree(clperm);CHKERRQ(ierr);
1348ed5f475SMatthew G. Knepley   /* Invert permutation */
1358ed5f475SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1368ed5f475SMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd-pStart, invclperm, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
1378ed5f475SMatthew G. Knepley   PetscFunctionReturn(0);
1388ed5f475SMatthew G. Knepley }
1398ed5f475SMatthew G. Knepley 
1408ed5f475SMatthew G. Knepley /*@
1418ed5f475SMatthew G. Knepley   DMPlexPermute - Reorder the mesh according to the input permutation
1428ed5f475SMatthew G. Knepley 
143d083f849SBarry Smith   Collective on dm
1448ed5f475SMatthew G. Knepley 
145*d8d19677SJose E. Roman   Input Parameters:
1468ed5f475SMatthew G. Knepley + dm - The DMPlex object
147c99f2573SMatthew G. Knepley - perm - The point permutation, perm[old point number] = new point number
1488ed5f475SMatthew G. Knepley 
1498ed5f475SMatthew G. Knepley   Output Parameter:
1508ed5f475SMatthew G. Knepley . pdm - The permuted DM
1518ed5f475SMatthew G. Knepley 
1528ed5f475SMatthew G. Knepley   Level: intermediate
1538ed5f475SMatthew G. Knepley 
1548ed5f475SMatthew G. Knepley .seealso: MatPermute()
1558ed5f475SMatthew G. Knepley @*/
1568ed5f475SMatthew G. Knepley PetscErrorCode DMPlexPermute(DM dm, IS perm, DM *pdm)
1578ed5f475SMatthew G. Knepley {
1588ed5f475SMatthew G. Knepley   DM_Plex       *plex = (DM_Plex *) dm->data, *plexNew;
1598ed5f475SMatthew G. Knepley   PetscSection   section, sectionNew;
1608ed5f475SMatthew G. Knepley   PetscInt       dim;
1618ed5f475SMatthew G. Knepley   PetscErrorCode ierr;
1628ed5f475SMatthew G. Knepley 
1638ed5f475SMatthew G. Knepley   PetscFunctionBegin;
1648ed5f475SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1658ed5f475SMatthew G. Knepley   PetscValidHeaderSpecific(perm, IS_CLASSID, 2);
1668ed5f475SMatthew G. Knepley   PetscValidPointer(pdm, 3);
1678ed5f475SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), pdm);CHKERRQ(ierr);
1688ed5f475SMatthew G. Knepley   ierr = DMSetType(*pdm, DMPLEX);CHKERRQ(ierr);
169c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
170c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*pdm, dim);CHKERRQ(ierr);
171d1d796f5SMatthew G. Knepley   ierr = DMCopyDisc(dm, *pdm);CHKERRQ(ierr);
17292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
173dd742cf1SMatthew G. Knepley   if (section) {
1748ed5f475SMatthew G. Knepley     ierr = PetscSectionPermute(section, perm, &sectionNew);CHKERRQ(ierr);
17592fd8e1eSJed Brown     ierr = DMSetLocalSection(*pdm, sectionNew);CHKERRQ(ierr);
1768ed5f475SMatthew G. Knepley     ierr = PetscSectionDestroy(&sectionNew);CHKERRQ(ierr);
177dd742cf1SMatthew G. Knepley   }
1788ed5f475SMatthew G. Knepley   plexNew = (DM_Plex *) (*pdm)->data;
1798ed5f475SMatthew G. Knepley   /* Ignore ltogmap, ltogmapb */
1801bb6d2a8SBarry Smith   /* Ignore sf, sectionSF */
1818ed5f475SMatthew G. Knepley   /* Ignore globalVertexNumbers, globalCellNumbers */
1828ed5f475SMatthew G. Knepley   /* Reorder labels */
1838ed5f475SMatthew G. Knepley   {
184a85475f2SMatthew G. Knepley     PetscInt numLabels, l;
185a85475f2SMatthew G. Knepley     DMLabel  label, labelNew;
1868ed5f475SMatthew G. Knepley 
187c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1885d80c0bfSVaclav Hapla     for (l = 0; l < numLabels; ++l) {
189c58f1c22SToby Isaac       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
190a85475f2SMatthew G. Knepley       ierr = DMLabelPermute(label, perm, &labelNew);CHKERRQ(ierr);
191c58f1c22SToby Isaac       ierr = DMAddLabel(*pdm, labelNew);CHKERRQ(ierr);
19208f633c4SVaclav Hapla       ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);
1938ed5f475SMatthew G. Knepley     }
194a6e0b375SMatthew G. Knepley     ierr = DMGetLabel(*pdm, "depth", &(*pdm)->depthLabel);CHKERRQ(ierr);
1958ed5f475SMatthew G. Knepley     if (plex->subpointMap) {ierr = DMLabelPermute(plex->subpointMap, perm, &plexNew->subpointMap);CHKERRQ(ierr);}
1968ed5f475SMatthew G. Knepley   }
1978ed5f475SMatthew G. Knepley   /* Reorder topology */
1988ed5f475SMatthew G. Knepley   {
1998ed5f475SMatthew G. Knepley     const PetscInt *pperm;
2008ed5f475SMatthew G. Knepley     PetscInt        maxConeSize, maxSupportSize, n, pStart, pEnd, p;
2018ed5f475SMatthew G. Knepley 
2028ed5f475SMatthew G. Knepley     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
2038ed5f475SMatthew G. Knepley     plexNew->maxConeSize    = maxConeSize;
2048ed5f475SMatthew G. Knepley     plexNew->maxSupportSize = maxSupportSize;
2058ed5f475SMatthew G. Knepley     ierr = PetscSectionDestroy(&plexNew->coneSection);CHKERRQ(ierr);
2068ed5f475SMatthew G. Knepley     ierr = PetscSectionPermute(plex->coneSection, perm, &plexNew->coneSection);CHKERRQ(ierr);
2078ed5f475SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(plexNew->coneSection, &n);CHKERRQ(ierr);
208785e854fSJed Brown     ierr = PetscMalloc1(n, &plexNew->cones);CHKERRQ(ierr);
209785e854fSJed Brown     ierr = PetscMalloc1(n, &plexNew->coneOrientations);CHKERRQ(ierr);
2108ed5f475SMatthew G. Knepley     ierr = ISGetIndices(perm, &pperm);CHKERRQ(ierr);
2118ed5f475SMatthew G. Knepley     ierr = PetscSectionGetChart(plex->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2128ed5f475SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2138ed5f475SMatthew G. Knepley       PetscInt dof, off, offNew, d;
2148ed5f475SMatthew G. Knepley 
2158ed5f475SMatthew G. Knepley       ierr = PetscSectionGetDof(plexNew->coneSection, pperm[p], &dof);CHKERRQ(ierr);
2168ed5f475SMatthew G. Knepley       ierr = PetscSectionGetOffset(plex->coneSection, p, &off);CHKERRQ(ierr);
2178ed5f475SMatthew G. Knepley       ierr = PetscSectionGetOffset(plexNew->coneSection, pperm[p], &offNew);CHKERRQ(ierr);
2188ed5f475SMatthew G. Knepley       for (d = 0; d < dof; ++d) {
2198ed5f475SMatthew G. Knepley         plexNew->cones[offNew+d]            = pperm[plex->cones[off+d]];
2208ed5f475SMatthew G. Knepley         plexNew->coneOrientations[offNew+d] = plex->coneOrientations[off+d];
2218ed5f475SMatthew G. Knepley       }
2228ed5f475SMatthew G. Knepley     }
2238ed5f475SMatthew G. Knepley     ierr = PetscSectionDestroy(&plexNew->supportSection);CHKERRQ(ierr);
2248ed5f475SMatthew G. Knepley     ierr = PetscSectionPermute(plex->supportSection, perm, &plexNew->supportSection);CHKERRQ(ierr);
2258ed5f475SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(plexNew->supportSection, &n);CHKERRQ(ierr);
226785e854fSJed Brown     ierr = PetscMalloc1(n, &plexNew->supports);CHKERRQ(ierr);
2278ed5f475SMatthew G. Knepley     ierr = PetscSectionGetChart(plex->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2288ed5f475SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2298ed5f475SMatthew G. Knepley       PetscInt dof, off, offNew, d;
2308ed5f475SMatthew G. Knepley 
2318ed5f475SMatthew G. Knepley       ierr = PetscSectionGetDof(plexNew->supportSection, pperm[p], &dof);CHKERRQ(ierr);
2328ed5f475SMatthew G. Knepley       ierr = PetscSectionGetOffset(plex->supportSection, p, &off);CHKERRQ(ierr);
2338ed5f475SMatthew G. Knepley       ierr = PetscSectionGetOffset(plexNew->supportSection, pperm[p], &offNew);CHKERRQ(ierr);
2348ed5f475SMatthew G. Knepley       for (d = 0; d < dof; ++d) {
2358ed5f475SMatthew G. Knepley         plexNew->supports[offNew+d] = pperm[plex->supports[off+d]];
2368ed5f475SMatthew G. Knepley       }
2378ed5f475SMatthew G. Knepley     }
2388ed5f475SMatthew G. Knepley     ierr = ISRestoreIndices(perm, &pperm);CHKERRQ(ierr);
2398ed5f475SMatthew G. Knepley   }
240a6e0b375SMatthew G. Knepley   /* Remap coordinates */
241a6e0b375SMatthew G. Knepley   {
242a6e0b375SMatthew G. Knepley     DM              cdm, cdmNew;
243a6e0b375SMatthew G. Knepley     PetscSection    csection, csectionNew;
244a6e0b375SMatthew G. Knepley     Vec             coordinates, coordinatesNew;
245a6e0b375SMatthew G. Knepley     PetscScalar    *coords, *coordsNew;
246a6e0b375SMatthew G. Knepley     const PetscInt *pperm;
247a6e0b375SMatthew G. Knepley     PetscInt        pStart, pEnd, p;
248a6e0b375SMatthew G. Knepley     const char     *name;
249a6e0b375SMatthew G. Knepley 
250a6e0b375SMatthew G. Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
251a6e0b375SMatthew G. Knepley     ierr = DMGetLocalSection(cdm, &csection);CHKERRQ(ierr);
252a6e0b375SMatthew G. Knepley     ierr = PetscSectionPermute(csection, perm, &csectionNew);CHKERRQ(ierr);
253a6e0b375SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
254a6e0b375SMatthew G. Knepley     ierr = VecDuplicate(coordinates, &coordinatesNew);CHKERRQ(ierr);
255a6e0b375SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
256a6e0b375SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject)coordinatesNew,name);CHKERRQ(ierr);
257a6e0b375SMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
258a6e0b375SMatthew G. Knepley     ierr = VecGetArray(coordinatesNew, &coordsNew);CHKERRQ(ierr);
259a6e0b375SMatthew G. Knepley     ierr = PetscSectionGetChart(csectionNew, &pStart, &pEnd);CHKERRQ(ierr);
260a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(perm, &pperm);CHKERRQ(ierr);
261a6e0b375SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
262a6e0b375SMatthew G. Knepley       PetscInt dof, off, offNew, d;
263a6e0b375SMatthew G. Knepley 
264a6e0b375SMatthew G. Knepley       ierr = PetscSectionGetDof(csectionNew, p, &dof);CHKERRQ(ierr);
265a6e0b375SMatthew G. Knepley       ierr = PetscSectionGetOffset(csection, p, &off);CHKERRQ(ierr);
266a6e0b375SMatthew G. Knepley       ierr = PetscSectionGetOffset(csectionNew, pperm[p], &offNew);CHKERRQ(ierr);
267a6e0b375SMatthew G. Knepley       for (d = 0; d < dof; ++d) coordsNew[offNew+d] = coords[off+d];
268a6e0b375SMatthew G. Knepley     }
269a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(perm, &pperm);CHKERRQ(ierr);
270a6e0b375SMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
271a6e0b375SMatthew G. Knepley     ierr = VecRestoreArray(coordinatesNew, &coordsNew);CHKERRQ(ierr);
272a6e0b375SMatthew G. Knepley     ierr = DMGetCoordinateDM(*pdm, &cdmNew);CHKERRQ(ierr);
273a6e0b375SMatthew G. Knepley     ierr = DMSetLocalSection(cdmNew, csectionNew);CHKERRQ(ierr);
274a6e0b375SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*pdm, coordinatesNew);CHKERRQ(ierr);
275a6e0b375SMatthew G. Knepley     ierr = PetscSectionDestroy(&csectionNew);CHKERRQ(ierr);
276a6e0b375SMatthew G. Knepley     ierr = VecDestroy(&coordinatesNew);CHKERRQ(ierr);
277a6e0b375SMatthew G. Knepley   }
278e66f2fa0SMatthew G. Knepley   (*pdm)->setupcalled = PETSC_TRUE;
2798ed5f475SMatthew G. Knepley   PetscFunctionReturn(0);
2808ed5f475SMatthew G. Knepley }
281