xref: /petsc/src/dm/impls/plex/plexinterpolate.c (revision a014044e415c55fe30c6f4fe8d8df7779f6c854b)
19f074e33SMatthew G Knepley #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
29f074e33SMatthew G Knepley #include <../src/sys/utils/hash.h>
39f074e33SMatthew G Knepley 
49f074e33SMatthew G Knepley #undef __FUNCT__
59a5b13a2SMatthew G Knepley #define __FUNCT__ "DMPlexGetFaces_Internal"
69f074e33SMatthew G Knepley /*
79a5b13a2SMatthew G Knepley   DMPlexGetFaces_Internal - Gets groups of vertices that correspond to faces for the given cell
89f074e33SMatthew G Knepley */
99a5b13a2SMatthew G Knepley static PetscErrorCode DMPlexGetFaces_Internal(DM dm, PetscInt dim, PetscInt p, PetscInt *numFaces, PetscInt *faceSize, const PetscInt *faces[])
109f074e33SMatthew G Knepley {
119f074e33SMatthew G Knepley   const PetscInt *cone = NULL;
129a5b13a2SMatthew G Knepley   PetscInt       *facesTmp;
139a5b13a2SMatthew G Knepley   PetscInt        maxConeSize, maxSupportSize, coneSize;
149f074e33SMatthew G Knepley   PetscErrorCode  ierr;
159f074e33SMatthew G Knepley 
169f074e33SMatthew G Knepley   PetscFunctionBegin;
179f074e33SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
189a5b13a2SMatthew G Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
199a5b13a2SMatthew G Knepley   ierr = DMGetWorkArray(dm, PetscSqr(PetscMax(maxConeSize, maxSupportSize)), PETSC_INT, &facesTmp);CHKERRQ(ierr);
209f074e33SMatthew G Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
219f074e33SMatthew G Knepley   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
229f074e33SMatthew G Knepley   switch (dim) {
239f074e33SMatthew G Knepley   case 2:
249f074e33SMatthew G Knepley     switch (coneSize) {
259f074e33SMatthew G Knepley     case 3:
269a5b13a2SMatthew G Knepley       if (faces) {
279a5b13a2SMatthew G Knepley         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
289a5b13a2SMatthew G Knepley         facesTmp[2] = cone[1]; facesTmp[3] = cone[2];
299a5b13a2SMatthew G Knepley         facesTmp[4] = cone[2]; facesTmp[5] = cone[0];
309a5b13a2SMatthew G Knepley         *faces = facesTmp;
319a5b13a2SMatthew G Knepley       }
329a5b13a2SMatthew G Knepley       if (numFaces) *numFaces         = 3;
339a5b13a2SMatthew G Knepley       if (faceSize) *faceSize         = 2;
349f074e33SMatthew G Knepley       break;
359f074e33SMatthew G Knepley     case 4:
369a5b13a2SMatthew G Knepley       /* Vertices follow right hand rule */
379a5b13a2SMatthew G Knepley       if (faces) {
389a5b13a2SMatthew G Knepley         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
399a5b13a2SMatthew G Knepley         facesTmp[2] = cone[1]; facesTmp[3] = cone[2];
409a5b13a2SMatthew G Knepley         facesTmp[4] = cone[2]; facesTmp[5] = cone[3];
419a5b13a2SMatthew G Knepley         facesTmp[6] = cone[3]; facesTmp[7] = cone[0];
429a5b13a2SMatthew G Knepley         *faces = facesTmp;
439a5b13a2SMatthew G Knepley       }
449a5b13a2SMatthew G Knepley       if (numFaces) *numFaces         = 4;
459a5b13a2SMatthew G Knepley       if (faceSize) *faceSize         = 2;
469a5b13a2SMatthew G Knepley       if (faces)    *faces            = facesTmp;
479f074e33SMatthew G Knepley       break;
489f074e33SMatthew G Knepley     default:
499f074e33SMatthew G Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
509f074e33SMatthew G Knepley     }
519f074e33SMatthew G Knepley     break;
529f074e33SMatthew G Knepley   case 3:
539f074e33SMatthew G Knepley     switch (coneSize) {
549f074e33SMatthew G Knepley     case 3:
559a5b13a2SMatthew G Knepley       if (faces) {
569a5b13a2SMatthew G Knepley         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
579a5b13a2SMatthew G Knepley         facesTmp[2] = cone[1]; facesTmp[3] = cone[2];
589a5b13a2SMatthew G Knepley         facesTmp[4] = cone[2]; facesTmp[5] = cone[0];
599a5b13a2SMatthew G Knepley         *faces = facesTmp;
609a5b13a2SMatthew G Knepley       }
619a5b13a2SMatthew G Knepley       if (numFaces) *numFaces         = 3;
629a5b13a2SMatthew G Knepley       if (faceSize) *faceSize         = 2;
639a5b13a2SMatthew G Knepley       if (faces)    *faces            = facesTmp;
649f074e33SMatthew G Knepley       break;
659f074e33SMatthew G Knepley     case 4:
669a5b13a2SMatthew G Knepley       /* Vertices of first face follow right hand rule and normal points towards last vertex */
679a5b13a2SMatthew G Knepley       if (faces) {
689a5b13a2SMatthew G Knepley         facesTmp[0] = cone[0]; facesTmp[1]  = cone[2]; facesTmp[2]  = cone[1];
699a5b13a2SMatthew G Knepley         facesTmp[3] = cone[0]; facesTmp[4]  = cone[1]; facesTmp[5]  = cone[3];
709a5b13a2SMatthew G Knepley         facesTmp[6] = cone[0]; facesTmp[7]  = cone[3]; facesTmp[8]  = cone[2];
719a5b13a2SMatthew G Knepley         facesTmp[9] = cone[1]; facesTmp[10] = cone[2]; facesTmp[11] = cone[3];
729a5b13a2SMatthew G Knepley         *faces = facesTmp;
739a5b13a2SMatthew G Knepley       }
749a5b13a2SMatthew G Knepley       if (numFaces) *numFaces         = 4;
759a5b13a2SMatthew G Knepley       if (faceSize) *faceSize         = 3;
769a5b13a2SMatthew G Knepley       if (faces)    *faces            = facesTmp;
779a5b13a2SMatthew G Knepley       break;
789a5b13a2SMatthew G Knepley     case 8:
799a5b13a2SMatthew G Knepley       if (faces) {
809a5b13a2SMatthew G Knepley         facesTmp[0]  = cone[0]; facesTmp[1]  = cone[3]; facesTmp[2]  = cone[2]; facesTmp[3]  = cone[1];
81*a014044eSMatthew G Knepley         facesTmp[4]  = cone[4]; facesTmp[5]  = cone[5]; facesTmp[6]  = cone[6]; facesTmp[7]  = cone[7];
829a5b13a2SMatthew G Knepley         facesTmp[8]  = cone[0]; facesTmp[9]  = cone[1]; facesTmp[10] = cone[5]; facesTmp[11] = cone[4];
839a5b13a2SMatthew G Knepley         facesTmp[12] = cone[2]; facesTmp[13] = cone[3]; facesTmp[14] = cone[7]; facesTmp[15] = cone[6];
849a5b13a2SMatthew G Knepley         facesTmp[16] = cone[1]; facesTmp[17] = cone[2]; facesTmp[18] = cone[6]; facesTmp[19] = cone[5];
859a5b13a2SMatthew G Knepley         facesTmp[20] = cone[0]; facesTmp[21] = cone[4]; facesTmp[22] = cone[7]; facesTmp[23] = cone[3];
869a5b13a2SMatthew G Knepley         *faces = facesTmp;
879a5b13a2SMatthew G Knepley       }
889a5b13a2SMatthew G Knepley       if (numFaces) *numFaces         = 6;
899a5b13a2SMatthew G Knepley       if (faceSize) *faceSize         = 4;
909f074e33SMatthew G Knepley       break;
919f074e33SMatthew G Knepley     default:
929f074e33SMatthew G Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
939f074e33SMatthew G Knepley     }
949f074e33SMatthew G Knepley     break;
959f074e33SMatthew G Knepley   default:
969f074e33SMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim);
979f074e33SMatthew G Knepley   }
989a5b13a2SMatthew G Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, &facesTmp);CHKERRQ(ierr);
999f074e33SMatthew G Knepley   PetscFunctionReturn(0);
1009f074e33SMatthew G Knepley }
1019f074e33SMatthew G Knepley 
1029f074e33SMatthew G Knepley #undef __FUNCT__
1039a5b13a2SMatthew G Knepley #define __FUNCT__ "DMPlexInterpolateFaces_Internal"
1049a5b13a2SMatthew G Knepley /* This interpolates faces for cells at some stratum */
1059a5b13a2SMatthew G Knepley static PetscErrorCode DMPlexInterpolateFaces_Internal(DM dm, PetscInt cellDepth, DM idm)
1069f074e33SMatthew G Knepley {
1079a5b13a2SMatthew G Knepley   PetscHashIJKL  faceTable;
1089a5b13a2SMatthew G Knepley   PetscInt      *pStart, *pEnd;
1099a5b13a2SMatthew G Knepley   PetscInt       cellDim, depth, faceDepth = cellDepth, numPoints = 0, faceSizeAll = 0, face, c, d;
1109f074e33SMatthew G Knepley   PetscErrorCode ierr;
1119f074e33SMatthew G Knepley 
1129f074e33SMatthew G Knepley   PetscFunctionBegin;
1139a5b13a2SMatthew G Knepley   ierr = DMPlexGetDimension(dm, &cellDim);CHKERRQ(ierr);
1149a5b13a2SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1159a5b13a2SMatthew G Knepley   ++depth;
1169a5b13a2SMatthew G Knepley   ++cellDepth;
1179a5b13a2SMatthew G Knepley   cellDim -= depth - cellDepth;
1189a5b13a2SMatthew G Knepley   ierr = PetscMalloc2(depth+1,PetscInt,&pStart,depth+1,PetscInt,&pEnd);CHKERRQ(ierr);
1199a5b13a2SMatthew G Knepley   for (d = depth-1; d >= faceDepth; --d) {
1209a5b13a2SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d+1], &pEnd[d+1]);CHKERRQ(ierr);
1219f074e33SMatthew G Knepley   }
1229a5b13a2SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, -1, NULL, &pStart[faceDepth]);CHKERRQ(ierr);
1239a5b13a2SMatthew G Knepley   pEnd[faceDepth] = pStart[faceDepth];
1249a5b13a2SMatthew G Knepley   for (d = faceDepth-1; d >= 0; --d) {
1259a5b13a2SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
1269f074e33SMatthew G Knepley   }
1279a5b13a2SMatthew G Knepley   if (pEnd[cellDepth] > pStart[cellDepth]) {ierr = DMPlexGetFaces_Internal(dm, cellDim, pStart[cellDepth], NULL, &faceSizeAll, NULL);CHKERRQ(ierr);}
1289a5b13a2SMatthew G Knepley   if (faceSizeAll > 4) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Do not support interpolation of meshes with faces of %D vertices", faceSizeAll);
1299a5b13a2SMatthew G Knepley   ierr = PetscHashIJKLCreate(&faceTable);CHKERRQ(ierr);
1309a5b13a2SMatthew G Knepley   ierr = PetscHashIJKLSetMultivalued(faceTable, PETSC_FALSE);CHKERRQ(ierr);
1319a5b13a2SMatthew G Knepley   for (c = pStart[cellDepth], face = pStart[faceDepth]; c < pEnd[cellDepth]; ++c) {
1329f074e33SMatthew G Knepley     const PetscInt *cellFaces;
1339a5b13a2SMatthew G Knepley     PetscInt        numCellFaces, faceSize, cf, f;
1349f074e33SMatthew G Knepley 
1359a5b13a2SMatthew G Knepley     ierr = DMPlexGetFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
1369a5b13a2SMatthew G Knepley     if (faceSize != faceSizeAll) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent face for cell %D of size %D != %D", c, faceSize, faceSizeAll);
1379f074e33SMatthew G Knepley     for (cf = 0; cf < numCellFaces; ++cf) {
1389a5b13a2SMatthew G Knepley       const PetscInt  *cellFace = &cellFaces[cf*faceSize];
1399a5b13a2SMatthew G Knepley       PetscHashIJKLKey key;
1409f074e33SMatthew G Knepley 
1419a5b13a2SMatthew G Knepley       if (faceSize == 2) {
1429a5b13a2SMatthew G Knepley         key.i = PetscMin(cellFace[0], cellFace[1]);
1439a5b13a2SMatthew G Knepley         key.j = PetscMax(cellFace[0], cellFace[1]);
1449a5b13a2SMatthew G Knepley       } else {
1459a5b13a2SMatthew G Knepley         key.i = cellFace[0]; key.j = cellFace[1]; key.k = cellFace[2]; key.l = faceSize > 3 ? cellFace[3] : 0;
1469a5b13a2SMatthew G Knepley         ierr = PetscSortInt(faceSize, (PetscInt *) &key);
1479f074e33SMatthew G Knepley       }
1489a5b13a2SMatthew G Knepley       ierr  = PetscHashIJKLGet(faceTable, key, &f);CHKERRQ(ierr);
1499a5b13a2SMatthew G Knepley       if (f < 0) {
1509a5b13a2SMatthew G Knepley         ierr = PetscHashIJKLAdd(faceTable, key, face);CHKERRQ(ierr);
1519a5b13a2SMatthew G Knepley         f    = face++;
1529a5b13a2SMatthew G Knepley       }
1539a5b13a2SMatthew G Knepley     }
1549a5b13a2SMatthew G Knepley   }
1559a5b13a2SMatthew G Knepley   pEnd[faceDepth] = face;
1569a5b13a2SMatthew G Knepley   ierr = PetscHashIJKLDestroy(&faceTable);CHKERRQ(ierr);
1579a5b13a2SMatthew G Knepley   /* Count new points */
1589a5b13a2SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
1599a5b13a2SMatthew G Knepley     numPoints += pEnd[d]-pStart[d];
1609a5b13a2SMatthew G Knepley   }
1619a5b13a2SMatthew G Knepley   ierr = DMPlexSetChart(idm, 0, numPoints);CHKERRQ(ierr);
1629a5b13a2SMatthew G Knepley   /* Set cone sizes */
1639a5b13a2SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
1649a5b13a2SMatthew G Knepley     PetscInt coneSize, p;
1659f074e33SMatthew G Knepley 
1669a5b13a2SMatthew G Knepley     if (d == faceDepth) {
1679a5b13a2SMatthew G Knepley       for (p = pStart[d]; p < pEnd[d]; ++p) {
1689a5b13a2SMatthew G Knepley         /* I see no way to do this if we admit faces of different shapes */
1699a5b13a2SMatthew G Knepley         ierr = DMPlexSetConeSize(idm, p, faceSizeAll);CHKERRQ(ierr);
1709a5b13a2SMatthew G Knepley       }
171*a014044eSMatthew G Knepley     } else if (d == cellDepth) {
172*a014044eSMatthew G Knepley       for (p = pStart[d]; p < pEnd[d]; ++p) {
173*a014044eSMatthew G Knepley         /* Number of cell faces may be different from number of cell vertices*/
174*a014044eSMatthew G Knepley         ierr = DMPlexGetFaces_Internal(dm, cellDim, p, &coneSize, NULL, NULL);CHKERRQ(ierr);
175*a014044eSMatthew G Knepley         ierr = DMPlexSetConeSize(idm, p, coneSize);CHKERRQ(ierr);
176*a014044eSMatthew G Knepley       }
1779a5b13a2SMatthew G Knepley     } else {
1789a5b13a2SMatthew G Knepley       for (p = pStart[d]; p < pEnd[d]; ++p) {
1799a5b13a2SMatthew G Knepley         ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1809a5b13a2SMatthew G Knepley         ierr = DMPlexSetConeSize(idm, p, coneSize);CHKERRQ(ierr);
1819f074e33SMatthew G Knepley       }
1829f074e33SMatthew G Knepley     }
1839f074e33SMatthew G Knepley   }
1849f074e33SMatthew G Knepley   ierr = DMSetUp(idm);CHKERRQ(ierr);
1859f074e33SMatthew G Knepley   /* Get face cones from subsets of cell vertices */
1869a5b13a2SMatthew G Knepley   if (faceSizeAll > 4) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Do not support interpolation of meshes with faces of %D vertices", faceSizeAll);
1879a5b13a2SMatthew G Knepley   ierr = PetscHashIJKLCreate(&faceTable);CHKERRQ(ierr);
1889a5b13a2SMatthew G Knepley   ierr = PetscHashIJKLSetMultivalued(faceTable, PETSC_FALSE);CHKERRQ(ierr);
1899a5b13a2SMatthew G Knepley   for (d = depth; d > cellDepth; --d) {
1909a5b13a2SMatthew G Knepley     const PetscInt *cone;
1919a5b13a2SMatthew G Knepley     PetscInt        p;
1929a5b13a2SMatthew G Knepley 
1939a5b13a2SMatthew G Knepley     for (p = pStart[d]; p < pEnd[d]; ++p) {
1949a5b13a2SMatthew G Knepley       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1959a5b13a2SMatthew G Knepley       ierr = DMPlexSetCone(idm, p, cone);CHKERRQ(ierr);
1969a5b13a2SMatthew G Knepley       ierr = DMPlexGetConeOrientation(dm, p, &cone);CHKERRQ(ierr);
1979a5b13a2SMatthew G Knepley       ierr = DMPlexSetConeOrientation(idm, p, cone);CHKERRQ(ierr);
1989f074e33SMatthew G Knepley     }
1999a5b13a2SMatthew G Knepley   }
2009a5b13a2SMatthew G Knepley   for (c = pStart[cellDepth], face = pStart[faceDepth]; c < pEnd[cellDepth]; ++c) {
2019f074e33SMatthew G Knepley     const PetscInt *cellFaces;
2029a5b13a2SMatthew G Knepley     PetscInt        numCellFaces, faceSize, cf, f;
2039f074e33SMatthew G Knepley 
2049a5b13a2SMatthew G Knepley     ierr = DMPlexGetFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
2059a5b13a2SMatthew G Knepley     if (faceSize != faceSizeAll) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent face for cell %D of size %D != %D", c, faceSize, faceSizeAll);
2069f074e33SMatthew G Knepley     for (cf = 0; cf < numCellFaces; ++cf) {
2079a5b13a2SMatthew G Knepley       const PetscInt  *cellFace = &cellFaces[cf*faceSize];
2089a5b13a2SMatthew G Knepley       PetscHashIJKLKey key;
2099f074e33SMatthew G Knepley 
2109a5b13a2SMatthew G Knepley       if (faceSize == 2) {
2119a5b13a2SMatthew G Knepley         key.i = PetscMin(cellFace[0], cellFace[1]);
2129a5b13a2SMatthew G Knepley         key.j = PetscMax(cellFace[0], cellFace[1]);
2139a5b13a2SMatthew G Knepley       } else {
2149a5b13a2SMatthew G Knepley         key.i = cellFace[0]; key.j = cellFace[1]; key.k = cellFace[2]; key.l = faceSize > 3 ? cellFace[3] : 0;
2159a5b13a2SMatthew G Knepley         ierr = PetscSortInt(faceSize, (PetscInt *) &key);
2169f074e33SMatthew G Knepley       }
2179a5b13a2SMatthew G Knepley       ierr  = PetscHashIJKLGet(faceTable, key, &f);CHKERRQ(ierr);
2189a5b13a2SMatthew G Knepley       if (f < 0) {
2199a5b13a2SMatthew G Knepley         ierr = DMPlexSetCone(idm, face, cellFace);CHKERRQ(ierr);
2209a5b13a2SMatthew G Knepley         ierr = PetscHashIJKLAdd(faceTable, key, face);CHKERRQ(ierr);
2219a5b13a2SMatthew G Knepley         f    = face++;
2229f074e33SMatthew G Knepley         ierr = DMPlexInsertCone(idm, c, cf, f);CHKERRQ(ierr);
2239a5b13a2SMatthew G Knepley       } else {
2249a5b13a2SMatthew G Knepley         const PetscInt *cone;
2259a5b13a2SMatthew G Knepley         PetscInt        coneSize, ornt, i, j;
2269f074e33SMatthew G Knepley 
2279a5b13a2SMatthew G Knepley         ierr = DMPlexInsertCone(idm, c, cf, f);CHKERRQ(ierr);
2289a5b13a2SMatthew G Knepley         /* Orient face */
2299f074e33SMatthew G Knepley         ierr = DMPlexGetConeSize(idm, f, &coneSize);CHKERRQ(ierr);
2309f074e33SMatthew G Knepley         ierr = DMPlexGetCone(idm, f, &cone);CHKERRQ(ierr);
2319a5b13a2SMatthew G Knepley         if (coneSize != faceSize) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of face vertices %D for face %D should be %D", coneSize, f, faceSize);
2329a5b13a2SMatthew G Knepley         /* - First find the initial vertex */
2339a5b13a2SMatthew G Knepley         for (i = 0; i < faceSize; ++i) if (cellFace[0] == cone[i]) break;
2349a5b13a2SMatthew G Knepley         /* - Try forward comparison */
2359a5b13a2SMatthew G Knepley         for (j = 0; j < faceSize; ++j) if (cellFace[j] != cone[(i+j)%faceSize]) break;
2369a5b13a2SMatthew G Knepley         if (j == faceSize) {
2379a5b13a2SMatthew G Knepley           if ((faceSize == 2) && (i == 1)) ornt = -2;
2389a5b13a2SMatthew G Knepley           else                             ornt = i;
2399a5b13a2SMatthew G Knepley         } else {
2409a5b13a2SMatthew G Knepley           /* - Try backward comparison */
2419a5b13a2SMatthew G Knepley           for (j = 0; j < faceSize; ++j) if (cellFace[j] != cone[(i+faceSize-j)%faceSize]) break;
2429a5b13a2SMatthew G Knepley           if (j == faceSize) ornt = -(i+1);
2439a5b13a2SMatthew G Knepley           else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not determine face orientation");
2449a5b13a2SMatthew G Knepley         }
2459a5b13a2SMatthew G Knepley         ierr = DMPlexInsertConeOrientation(idm, c, cf, ornt);CHKERRQ(ierr);
2469f074e33SMatthew G Knepley       }
2479f074e33SMatthew G Knepley     }
2489f074e33SMatthew G Knepley   }
2499a5b13a2SMatthew G Knepley   if (face != pEnd[faceDepth]) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid number of faces %D should be %D", face-pStart[faceDepth], pEnd[faceDepth]-pStart[faceDepth]);
2509a5b13a2SMatthew G Knepley   ierr = PetscHashIJKLDestroy(&faceTable);CHKERRQ(ierr);
2519a5b13a2SMatthew G Knepley   ierr = DMPlexSymmetrize(idm);CHKERRQ(ierr);
2529a5b13a2SMatthew G Knepley   ierr = DMPlexStratify(idm);CHKERRQ(ierr);
2539f074e33SMatthew G Knepley   PetscFunctionReturn(0);
2549f074e33SMatthew G Knepley }
2559f074e33SMatthew G Knepley 
2569f074e33SMatthew G Knepley #undef __FUNCT__
2579f074e33SMatthew G Knepley #define __FUNCT__ "DMPlexInterpolate"
2589f074e33SMatthew G Knepley PetscErrorCode DMPlexInterpolate(DM dm, DM *dmInt)
2599f074e33SMatthew G Knepley {
2609a5b13a2SMatthew G Knepley   DM             idm, odm = dm;
2619a5b13a2SMatthew G Knepley   PetscInt       dim, d;
2629f074e33SMatthew G Knepley   PetscErrorCode ierr;
2639f074e33SMatthew G Knepley 
2649f074e33SMatthew G Knepley   PetscFunctionBegin;
2659f074e33SMatthew G Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
2669a5b13a2SMatthew G Knepley   for (d = 1; d < dim; ++d) {
2679a5b13a2SMatthew G Knepley     /* Create interpolated mesh */
2689a5b13a2SMatthew G Knepley     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &idm);CHKERRQ(ierr);
2699a5b13a2SMatthew G Knepley     ierr = DMSetType(idm, DMPLEX);CHKERRQ(ierr);
2709a5b13a2SMatthew G Knepley     ierr = DMPlexSetDimension(idm, dim);CHKERRQ(ierr);
2719a5b13a2SMatthew G Knepley     ierr = DMPlexInterpolateFaces_Internal(odm, 1, idm);CHKERRQ(ierr);
2729a5b13a2SMatthew G Knepley     if (dim == 3) {ierr = DMView(idm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
2739a5b13a2SMatthew G Knepley     if (odm != dm) {ierr = DMDestroy(&odm);CHKERRQ(ierr);}
2749a5b13a2SMatthew G Knepley     odm  = idm;
2759f074e33SMatthew G Knepley   }
2769a5b13a2SMatthew G Knepley   *dmInt = idm;
2779f074e33SMatthew G Knepley   PetscFunctionReturn(0);
2789f074e33SMatthew G Knepley }
27907b0a1fcSMatthew G Knepley 
28007b0a1fcSMatthew G Knepley #undef __FUNCT__
28107b0a1fcSMatthew G Knepley #define __FUNCT__ "DMPlexCopyCoordinates"
28207b0a1fcSMatthew G Knepley PetscErrorCode DMPlexCopyCoordinates(DM dmA, DM dmB)
28307b0a1fcSMatthew G Knepley {
28407b0a1fcSMatthew G Knepley   Vec            coordinatesA, coordinatesB;
28507b0a1fcSMatthew G Knepley   PetscSection   coordSectionA, coordSectionB;
28607b0a1fcSMatthew G Knepley   PetscScalar   *coordsA, *coordsB;
28707b0a1fcSMatthew G Knepley   PetscInt       spaceDim, vStartA, vStartB, vEndA, vEndB, coordSizeB, v, d;
28807b0a1fcSMatthew G Knepley   PetscErrorCode ierr;
28907b0a1fcSMatthew G Knepley 
29007b0a1fcSMatthew G Knepley   PetscFunctionBegin;
29107b0a1fcSMatthew G Knepley   ierr = DMPlexGetDepthStratum(dmA, 0, &vStartA, &vEndA);CHKERRQ(ierr);
29207b0a1fcSMatthew G Knepley   ierr = DMPlexGetDepthStratum(dmB, 0, &vStartB, &vEndB);CHKERRQ(ierr);
29307b0a1fcSMatthew G Knepley   if ((vEndA-vStartA) != (vEndB-vStartB)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of vertices in first DM %d != %d in the second DM", vEndA-vStartA, vEndB-vStartB);
29407b0a1fcSMatthew G Knepley   ierr = DMPlexGetCoordinateSection(dmA, &coordSectionA);CHKERRQ(ierr);
29507b0a1fcSMatthew G Knepley   ierr = DMPlexGetCoordinateSection(dmB, &coordSectionB);CHKERRQ(ierr);
29607b0a1fcSMatthew G Knepley   ierr = PetscSectionSetNumFields(coordSectionB, 1);CHKERRQ(ierr);
29707b0a1fcSMatthew G Knepley   ierr = PetscSectionGetFieldComponents(coordSectionA, 0, &spaceDim);CHKERRQ(ierr);
29807b0a1fcSMatthew G Knepley   ierr = PetscSectionSetFieldComponents(coordSectionB, 0, spaceDim);CHKERRQ(ierr);
29907b0a1fcSMatthew G Knepley   ierr = PetscSectionSetChart(coordSectionB, vStartB, vEndB);CHKERRQ(ierr);
30007b0a1fcSMatthew G Knepley   for (v = vStartB; v < vEndB; ++v) {
30107b0a1fcSMatthew G Knepley     ierr = PetscSectionSetDof(coordSectionB, v, spaceDim);CHKERRQ(ierr);
30207b0a1fcSMatthew G Knepley     ierr = PetscSectionSetFieldDof(coordSectionB, v, 0, spaceDim);CHKERRQ(ierr);
30307b0a1fcSMatthew G Knepley   }
30407b0a1fcSMatthew G Knepley   ierr = PetscSectionSetUp(coordSectionB);CHKERRQ(ierr);
30507b0a1fcSMatthew G Knepley   ierr = PetscSectionGetStorageSize(coordSectionB, &coordSizeB);CHKERRQ(ierr);
30607b0a1fcSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dmA, &coordinatesA);CHKERRQ(ierr);
30707b0a1fcSMatthew G Knepley   ierr = VecCreate(PetscObjectComm((PetscObject) dmB), &coordinatesB);CHKERRQ(ierr);
30807b0a1fcSMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) coordinatesB, "coordinates");CHKERRQ(ierr);
30907b0a1fcSMatthew G Knepley   ierr = VecSetSizes(coordinatesB, coordSizeB, PETSC_DETERMINE);CHKERRQ(ierr);
31007b0a1fcSMatthew G Knepley   ierr = VecSetFromOptions(coordinatesB);CHKERRQ(ierr);
31107b0a1fcSMatthew G Knepley   ierr = VecGetArray(coordinatesA, &coordsA);CHKERRQ(ierr);
31207b0a1fcSMatthew G Knepley   ierr = VecGetArray(coordinatesB, &coordsB);CHKERRQ(ierr);
31307b0a1fcSMatthew G Knepley   for (v = 0; v < vEndB-vStartB; ++v) {
31407b0a1fcSMatthew G Knepley     for (d = 0; d < spaceDim; ++d) {
31507b0a1fcSMatthew G Knepley       coordsB[v*spaceDim+d] = coordsA[v*spaceDim+d];
31607b0a1fcSMatthew G Knepley     }
31707b0a1fcSMatthew G Knepley   }
31807b0a1fcSMatthew G Knepley   ierr = VecRestoreArray(coordinatesA, &coordsA);CHKERRQ(ierr);
31907b0a1fcSMatthew G Knepley   ierr = VecRestoreArray(coordinatesB, &coordsB);CHKERRQ(ierr);
32007b0a1fcSMatthew G Knepley   ierr = DMSetCoordinatesLocal(dmB, coordinatesB);CHKERRQ(ierr);
32107b0a1fcSMatthew G Knepley   ierr = VecDestroy(&coordinatesB);CHKERRQ(ierr);
32207b0a1fcSMatthew G Knepley   PetscFunctionReturn(0);
32307b0a1fcSMatthew G Knepley }
324