xref: /petsc/src/dm/impls/plex/plexcoarsen.c (revision 17f047d895ea9c297dd24b3df2aa1863c019c8ef)
1919ab0a2SMatthew G. Knepley #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2919ab0a2SMatthew G. Knepley #ifdef PETSC_HAVE_PRAGMATIC
3919ab0a2SMatthew G. Knepley #include <pragmatic/cpragmatic.h>
4919ab0a2SMatthew G. Knepley #endif
5919ab0a2SMatthew G. Knepley 
69f3102b2SMatthew G. Knepley #include <petscksp.h>
79f3102b2SMatthew G. Knepley 
8919ab0a2SMatthew G. Knepley #undef __FUNCT__
9919ab0a2SMatthew G. Knepley #define __FUNCT__ "DMCoarsen_Plex"
10919ab0a2SMatthew G. Knepley PetscErrorCode DMCoarsen_Plex(DM dm, MPI_Comm comm, DM *dmCoarsened)
11919ab0a2SMatthew G. Knepley {
12919ab0a2SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
13*17f047d8SMatthew G. Knepley #ifdef PETSC_HAVE_PRAGMATIC
14919ab0a2SMatthew G. Knepley   DM             udm, coordDM;
15919ab0a2SMatthew G. Knepley   DMLabel        bd;
169f3102b2SMatthew G. Knepley   Mat            A;
179f3102b2SMatthew G. Knepley   Vec            coordinates, mb, mx;
18919ab0a2SMatthew G. Knepley   PetscSection   coordSection;
19919ab0a2SMatthew G. Knepley   const PetscScalar *coords;
20919ab0a2SMatthew G. Knepley   double        *coarseCoords;
21919ab0a2SMatthew G. Knepley   IS             bdIS;
229f3102b2SMatthew G. Knepley   PetscReal     *x, *y, *z, *eqns, *metric;
239f3102b2SMatthew G. Knepley   PetscReal      coarseRatio = PetscSqr(0.5);
24919ab0a2SMatthew G. Knepley   const PetscInt *faces;
25919ab0a2SMatthew G. Knepley   PetscInt      *cells, *bdFaces, *bdFaceIds;
2677902bb5SPatrick Farrell   PetscInt       dim, numCorners, cStart, cEnd, numCells, numCoarseCells, c, vStart, vEnd, numVertices, numCoarseVertices, v, numBdFaces, f, maxConeSize, size, bdSize, coff;
27*17f047d8SMatthew G. Knepley #endif
28919ab0a2SMatthew G. Knepley   PetscErrorCode ierr;
29919ab0a2SMatthew G. Knepley 
30919ab0a2SMatthew G. Knepley   PetscFunctionBegin;
31919ab0a2SMatthew G. Knepley #ifdef PETSC_HAVE_PRAGMATIC
32919ab0a2SMatthew G. Knepley   if (!mesh->coarseMesh) {
33919ab0a2SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
34919ab0a2SMatthew G. Knepley     ierr = DMGetCoordinateDM(dm, &coordDM);CHKERRQ(ierr);
35919ab0a2SMatthew G. Knepley     ierr = DMGetDefaultSection(coordDM, &coordSection);CHKERRQ(ierr);
36919ab0a2SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
37919ab0a2SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
38919ab0a2SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
39919ab0a2SMatthew G. Knepley     ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
40919ab0a2SMatthew G. Knepley     ierr = DMPlexGetMaxSizes(udm, &maxConeSize, NULL);CHKERRQ(ierr);
41919ab0a2SMatthew G. Knepley     numCells    = cEnd - cStart;
42919ab0a2SMatthew G. Knepley     numVertices = vEnd - vStart;
43d2d4c474SMatthew G. Knepley     ierr = PetscCalloc5(numVertices, &x, numVertices, &y, numVertices, &z, numVertices*PetscSqr(dim), &metric, numCells*maxConeSize, &cells);CHKERRQ(ierr);
44919ab0a2SMatthew G. Knepley     ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
45d2d4c474SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
46919ab0a2SMatthew G. Knepley       PetscInt off;
47919ab0a2SMatthew G. Knepley 
48919ab0a2SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
49d2d4c474SMatthew G. Knepley       x[v-vStart] = coords[off+0];
50d2d4c474SMatthew G. Knepley       y[v-vStart] = coords[off+1];
51d2d4c474SMatthew G. Knepley       if (dim > 2) z[v-vStart] = coords[off+2];
52919ab0a2SMatthew G. Knepley     }
53919ab0a2SMatthew G. Knepley     ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
54919ab0a2SMatthew G. Knepley     for (c = 0, coff = 0; c < numCells; ++c) {
55919ab0a2SMatthew G. Knepley       const PetscInt *cone;
56919ab0a2SMatthew G. Knepley       PetscInt        coneSize, cl;
57919ab0a2SMatthew G. Knepley 
58919ab0a2SMatthew G. Knepley       ierr = DMPlexGetConeSize(udm, c, &coneSize);CHKERRQ(ierr);
59919ab0a2SMatthew G. Knepley       ierr = DMPlexGetCone(udm, c, &cone);CHKERRQ(ierr);
60d2d4c474SMatthew G. Knepley       for (cl = 0; cl < coneSize; ++cl) cells[coff++] = cone[cl] - vStart;
61919ab0a2SMatthew G. Knepley     }
62919ab0a2SMatthew G. Knepley     switch (dim) {
63919ab0a2SMatthew G. Knepley     case 2:
64919ab0a2SMatthew G. Knepley       pragmatic_2d_init(&numVertices, &numCells, cells, x, y);
65919ab0a2SMatthew G. Knepley       break;
66919ab0a2SMatthew G. Knepley     case 3:
67919ab0a2SMatthew G. Knepley       pragmatic_3d_init(&numVertices, &numCells, cells, x, y, z);
68919ab0a2SMatthew G. Knepley       break;
69919ab0a2SMatthew G. Knepley     default:
70919ab0a2SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No Pragmatic coarsening defined for dimension %d", dim);
71919ab0a2SMatthew G. Knepley     }
72919ab0a2SMatthew G. Knepley     /* Create boundary mesh */
73919ab0a2SMatthew G. Knepley     ierr = DMLabelCreate("boundary", &bd);CHKERRQ(ierr);
74919ab0a2SMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, bd);CHKERRQ(ierr);
75919ab0a2SMatthew G. Knepley     ierr = DMLabelGetStratumIS(bd, 1, &bdIS);CHKERRQ(ierr);
76919ab0a2SMatthew G. Knepley     ierr = DMLabelGetStratumSize(bd, 1, &numBdFaces);CHKERRQ(ierr);
77919ab0a2SMatthew G. Knepley     ierr = ISGetIndices(bdIS, &faces);CHKERRQ(ierr);
78919ab0a2SMatthew G. Knepley     for (f = 0, bdSize = 0; f < numBdFaces; ++f) {
79919ab0a2SMatthew G. Knepley       PetscInt *closure = NULL;
80919ab0a2SMatthew G. Knepley       PetscInt  closureSize, cl;
81919ab0a2SMatthew G. Knepley 
82d2d4c474SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, faces[f], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
83919ab0a2SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
84919ab0a2SMatthew G. Knepley         if ((closure[cl] >= vStart) && (closure[cl] < vEnd)) ++bdSize;
85919ab0a2SMatthew G. Knepley       }
86919ab0a2SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
87919ab0a2SMatthew G. Knepley     }
88919ab0a2SMatthew G. Knepley     ierr = PetscMalloc2(bdSize, &bdFaces, numBdFaces, &bdFaceIds);CHKERRQ(ierr);
89919ab0a2SMatthew G. Knepley     for (f = 0, bdSize = 0; f < numBdFaces; ++f) {
90919ab0a2SMatthew G. Knepley       PetscInt *closure = NULL;
91919ab0a2SMatthew G. Knepley       PetscInt  closureSize, cl;
92919ab0a2SMatthew G. Knepley 
93d2d4c474SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, faces[f], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
94919ab0a2SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
95d2d4c474SMatthew G. Knepley         if ((closure[cl] >= vStart) && (closure[cl] < vEnd)) bdFaces[bdSize++] = closure[cl] - vStart;
96919ab0a2SMatthew G. Knepley       }
97919ab0a2SMatthew G. Knepley       /* TODO Fix */
98919ab0a2SMatthew G. Knepley       bdFaceIds[f] = 1;
99919ab0a2SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
100919ab0a2SMatthew G. Knepley     }
101919ab0a2SMatthew G. Knepley     ierr = ISDestroy(&bdIS);CHKERRQ(ierr);
102d2d4c474SMatthew G. Knepley     ierr = DMLabelDestroy(&bd);CHKERRQ(ierr);
103919ab0a2SMatthew G. Knepley     pragmatic_set_boundary(&numBdFaces, bdFaces, bdFaceIds);
104919ab0a2SMatthew G. Knepley     /* Create metric */
1059f3102b2SMatthew G. Knepley     size = (dim*(dim+1))/2;
1069f3102b2SMatthew G. Knepley     ierr = PetscMalloc1(PetscSqr(size), &eqns);CHKERRQ(ierr);
1079f3102b2SMatthew G. Knepley     ierr = MatCreateSeqDense(PETSC_COMM_SELF, size, size, eqns, &A);CHKERRQ(ierr);
1089f3102b2SMatthew G. Knepley     ierr = MatCreateVecs(A, &mx, &mb);CHKERRQ(ierr);
1099f3102b2SMatthew G. Knepley     ierr = VecSet(mb, 1.0);CHKERRQ(ierr);
1109f3102b2SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
1119f3102b2SMatthew G. Knepley       const PetscScalar *sol;
1129f3102b2SMatthew G. Knepley       PetscScalar       *cellCoords = NULL;
1139f3102b2SMatthew G. Knepley       PetscReal          e[3], vol;
1149f3102b2SMatthew G. Knepley       const PetscInt    *cone;
1159f3102b2SMatthew G. Knepley       PetscInt           coneSize, cl, i, j, d, r;
1169f3102b2SMatthew G. Knepley 
1179f3102b2SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, NULL, &cellCoords);CHKERRQ(ierr);
1189f3102b2SMatthew G. Knepley       /* Only works for simplices */
1199f3102b2SMatthew G. Knepley       for (i = 0, r = 0; i < dim+1; ++i) {
1209f3102b2SMatthew G. Knepley         for (j = 0; j < i; ++j, ++r) {
1219f3102b2SMatthew G. Knepley           for (d = 0; d < dim; ++d) e[d] = cellCoords[i*dim+d] - cellCoords[j*dim+d];
1229f3102b2SMatthew G. Knepley           /* FORTRAN ORDERING */
1239f3102b2SMatthew G. Knepley           if (dim == 2) {
1249f3102b2SMatthew G. Knepley             eqns[0*size+r] = PetscSqr(e[0]);
1259f3102b2SMatthew G. Knepley             eqns[1*size+r] = 2.0*e[0]*e[1];
1269f3102b2SMatthew G. Knepley             eqns[2*size+r] = PetscSqr(e[1]);
1279f3102b2SMatthew G. Knepley           } else {
1289f3102b2SMatthew G. Knepley             eqns[0*size+r] = PetscSqr(e[0]);
1299f3102b2SMatthew G. Knepley             eqns[1*size+r] = 2.0*e[0]*e[1];
1309f3102b2SMatthew G. Knepley             eqns[2*size+r] = 2.0*e[0]*e[2];
1319f3102b2SMatthew G. Knepley             eqns[3*size+r] = PetscSqr(e[1]);
1329f3102b2SMatthew G. Knepley             eqns[4*size+r] = 2.0*e[1]*e[2];
1339f3102b2SMatthew G. Knepley             eqns[5*size+r] = PetscSqr(e[2]);
1349f3102b2SMatthew G. Knepley           }
1359f3102b2SMatthew G. Knepley         }
1369f3102b2SMatthew G. Knepley       }
1379f3102b2SMatthew G. Knepley       ierr = MatSetUnfactored(A);CHKERRQ(ierr);
1389f3102b2SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, NULL, &cellCoords);CHKERRQ(ierr);
1399f3102b2SMatthew G. Knepley       ierr = MatLUFactor(A, NULL, NULL, NULL);CHKERRQ(ierr);
1409f3102b2SMatthew G. Knepley       ierr = MatSolve(A, mb, mx);CHKERRQ(ierr);
1419f3102b2SMatthew G. Knepley       ierr = VecGetArrayRead(mx, &sol);CHKERRQ(ierr);
1429f3102b2SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
1439f3102b2SMatthew G. Knepley       ierr = DMPlexGetCone(udm, c, &cone);CHKERRQ(ierr);
1449f3102b2SMatthew G. Knepley       ierr = DMPlexGetConeSize(udm, c, &coneSize);CHKERRQ(ierr);
1459f3102b2SMatthew G. Knepley       for (cl = 0; cl < coneSize; ++cl) {
1469f3102b2SMatthew G. Knepley         const PetscInt v = cone[cl] - vStart;
1479f3102b2SMatthew G. Knepley 
1489f3102b2SMatthew G. Knepley         if (dim == 2) {
1499f3102b2SMatthew G. Knepley           metric[v*4+0] += vol*coarseRatio*sol[0];
1509f3102b2SMatthew G. Knepley           metric[v*4+1] += vol*coarseRatio*sol[1];
1519f3102b2SMatthew G. Knepley           metric[v*4+2] += vol*coarseRatio*sol[1];
1529f3102b2SMatthew G. Knepley           metric[v*4+3] += vol*coarseRatio*sol[2];
1539f3102b2SMatthew G. Knepley         } else {
1549f3102b2SMatthew G. Knepley           metric[v*9+0] += vol*coarseRatio*sol[0];
1559f3102b2SMatthew G. Knepley           metric[v*9+1] += vol*coarseRatio*sol[1];
1569f3102b2SMatthew G. Knepley           metric[v*9+3] += vol*coarseRatio*sol[1];
1579f3102b2SMatthew G. Knepley           metric[v*9+2] += vol*coarseRatio*sol[2];
1589f3102b2SMatthew G. Knepley           metric[v*9+6] += vol*coarseRatio*sol[2];
1599f3102b2SMatthew G. Knepley           metric[v*9+4] += vol*coarseRatio*sol[3];
1609f3102b2SMatthew G. Knepley           metric[v*9+5] += vol*coarseRatio*sol[4];
1619f3102b2SMatthew G. Knepley           metric[v*9+7] += vol*coarseRatio*sol[4];
1629f3102b2SMatthew G. Knepley           metric[v*9+8] += vol*coarseRatio*sol[5];
1639f3102b2SMatthew G. Knepley         }
1649f3102b2SMatthew G. Knepley       }
1659f3102b2SMatthew G. Knepley       ierr = VecRestoreArrayRead(mx, &sol);CHKERRQ(ierr);
1669f3102b2SMatthew G. Knepley     }
1679f3102b2SMatthew G. Knepley     for (v = 0; v < numVertices; ++v) {
1689f3102b2SMatthew G. Knepley       const PetscInt *support;
1699f3102b2SMatthew G. Knepley       PetscInt        supportSize, s;
1709f3102b2SMatthew G. Knepley       PetscReal       vol, totVol = 0.0;
1719f3102b2SMatthew G. Knepley 
1729f3102b2SMatthew G. Knepley       ierr = DMPlexGetSupport(udm, v+vStart, &support);CHKERRQ(ierr);
1739f3102b2SMatthew G. Knepley       ierr = DMPlexGetSupportSize(udm, v+vStart, &supportSize);CHKERRQ(ierr);
1749f3102b2SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {ierr = DMPlexComputeCellGeometryFVM(dm, support[s], &vol, NULL, NULL);CHKERRQ(ierr); totVol += vol;}
1759f3102b2SMatthew G. Knepley       for (s = 0; s < PetscSqr(dim); ++s) metric[v*PetscSqr(dim)+s] /= totVol;
1769f3102b2SMatthew G. Knepley     }
1779f3102b2SMatthew G. Knepley     ierr = VecDestroy(&mx);CHKERRQ(ierr);
1789f3102b2SMatthew G. Knepley     ierr = VecDestroy(&mb);CHKERRQ(ierr);
1799f3102b2SMatthew G. Knepley     ierr = MatDestroy(&A);CHKERRQ(ierr);
1809f3102b2SMatthew G. Knepley     ierr = DMDestroy(&udm);CHKERRQ(ierr);
1819f3102b2SMatthew G. Knepley     ierr = PetscFree(eqns);CHKERRQ(ierr);
182919ab0a2SMatthew G. Knepley     pragmatic_set_metric(metric);
183919ab0a2SMatthew G. Knepley     pragmatic_adapt();
184919ab0a2SMatthew G. Knepley     /* Read out mesh */
185919ab0a2SMatthew G. Knepley     pragmatic_get_info(&numCoarseVertices, &numCoarseCells);
186d2d4c474SMatthew G. Knepley     ierr = PetscMalloc1(numCoarseVertices*dim, &coarseCoords);CHKERRQ(ierr);
187919ab0a2SMatthew G. Knepley     switch (dim) {
188919ab0a2SMatthew G. Knepley     case 2:
189919ab0a2SMatthew G. Knepley       pragmatic_get_coords_2d(x, y);
190919ab0a2SMatthew G. Knepley       numCorners = 3;
191919ab0a2SMatthew G. Knepley       for (v = 0; v < numCoarseVertices; ++v) {coarseCoords[v*2+0] = x[v]; coarseCoords[v*2+1] = y[v];}
192919ab0a2SMatthew G. Knepley       break;
193919ab0a2SMatthew G. Knepley     case 3:
194919ab0a2SMatthew G. Knepley       pragmatic_get_coords_3d(x, y, z);
195919ab0a2SMatthew G. Knepley       numCorners = 4;
196919ab0a2SMatthew G. Knepley       for (v = 0; v < numCoarseVertices; ++v) {coarseCoords[v*3+0] = x[v]; coarseCoords[v*3+1] = y[v]; coarseCoords[v*3+2] = z[v];}
197919ab0a2SMatthew G. Knepley       break;
198919ab0a2SMatthew G. Knepley     default:
199919ab0a2SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No Pragmatic coarsening defined for dimension %d", dim);
200919ab0a2SMatthew G. Knepley     }
201919ab0a2SMatthew G. Knepley     pragmatic_get_elements(cells);
202919ab0a2SMatthew G. Knepley     /* TODO Read out markers for boundary */
203919ab0a2SMatthew G. Knepley     ierr = DMPlexCreateFromCellList(PetscObjectComm((PetscObject) dm), dim, numCoarseCells, numCoarseVertices, numCorners, PETSC_TRUE, cells, dim, coarseCoords, &mesh->coarseMesh);CHKERRQ(ierr);
204919ab0a2SMatthew G. Knepley     pragmatic_finalize();
205d2d4c474SMatthew G. Knepley     ierr = PetscFree5(x, y, z, metric, cells);CHKERRQ(ierr);
206919ab0a2SMatthew G. Knepley     ierr = PetscFree2(bdFaces, bdFaceIds);CHKERRQ(ierr);
207919ab0a2SMatthew G. Knepley     ierr = PetscFree(coarseCoords);CHKERRQ(ierr);
208919ab0a2SMatthew G. Knepley   }
209919ab0a2SMatthew G. Knepley #endif
210919ab0a2SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) mesh->coarseMesh);CHKERRQ(ierr);
211919ab0a2SMatthew G. Knepley   *dmCoarsened = mesh->coarseMesh;
212919ab0a2SMatthew G. Knepley   PetscFunctionReturn(0);
213919ab0a2SMatthew G. Knepley }
214b653a561SMatthew G. Knepley 
215b653a561SMatthew G. Knepley #undef __FUNCT__
216b653a561SMatthew G. Knepley #define __FUNCT__ "DMCoarsenHierarchy_Plex"
217b653a561SMatthew G. Knepley PetscErrorCode DMCoarsenHierarchy_Plex(DM dm, PetscInt nlevels, DM dmCoarsened[])
218b653a561SMatthew G. Knepley {
219b653a561SMatthew G. Knepley   DM             rdm = dm;
220b653a561SMatthew G. Knepley   PetscInt       c;
221b653a561SMatthew G. Knepley   PetscErrorCode ierr;
222b653a561SMatthew G. Knepley 
223b653a561SMatthew G. Knepley   PetscFunctionBegin;
224b653a561SMatthew G. Knepley   for (c = nlevels-1; c >= 0; --c) {
225b653a561SMatthew G. Knepley     ierr = DMCoarsen(rdm, PetscObjectComm((PetscObject) dm), &dmCoarsened[c]);CHKERRQ(ierr);
226b653a561SMatthew G. Knepley     ierr = DMPlexCopyBoundary(rdm, dmCoarsened[c]);CHKERRQ(ierr);
227b653a561SMatthew G. Knepley     ierr = DMPlexSetCoarseDM(rdm, dmCoarsened[c]);CHKERRQ(ierr);
228b653a561SMatthew G. Knepley     rdm  = dmCoarsened[c];
229b653a561SMatthew G. Knepley   }
230b653a561SMatthew G. Knepley   PetscFunctionReturn(0);
231b653a561SMatthew G. Knepley }
232