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 6*9f3102b2SMatthew G. Knepley #include <petscksp.h> 7*9f3102b2SMatthew 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; 13919ab0a2SMatthew G. Knepley DM udm, coordDM; 14919ab0a2SMatthew G. Knepley DMLabel bd; 15*9f3102b2SMatthew G. Knepley Mat A; 16*9f3102b2SMatthew G. Knepley Vec coordinates, mb, mx; 17919ab0a2SMatthew G. Knepley PetscSection coordSection; 18919ab0a2SMatthew G. Knepley const PetscScalar *coords; 19919ab0a2SMatthew G. Knepley double *coarseCoords; 20919ab0a2SMatthew G. Knepley IS bdIS; 21*9f3102b2SMatthew G. Knepley PetscReal *x, *y, *z, *eqns, *metric; 22*9f3102b2SMatthew G. Knepley PetscReal coarseRatio = PetscSqr(0.5); 23919ab0a2SMatthew G. Knepley const PetscInt *faces; 24919ab0a2SMatthew G. Knepley PetscInt *cells, *bdFaces, *bdFaceIds; 25*9f3102b2SMatthew G. Knepley PetscInt dim, numCorners, cStart, cEnd, numCells, numCoarseCells, c, vStart, vEnd, numVertices, numCoarseVertices, v, numBdFaces, numCoarseBdFaces, f, maxConeSize, size, bdSize, coff; 26919ab0a2SMatthew G. Knepley PetscErrorCode ierr; 27919ab0a2SMatthew G. Knepley 28919ab0a2SMatthew G. Knepley PetscFunctionBegin; 29919ab0a2SMatthew G. Knepley #ifdef PETSC_HAVE_PRAGMATIC 30919ab0a2SMatthew G. Knepley if (!mesh->coarseMesh) { 31919ab0a2SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 32919ab0a2SMatthew G. Knepley ierr = DMGetCoordinateDM(dm, &coordDM);CHKERRQ(ierr); 33919ab0a2SMatthew G. Knepley ierr = DMGetDefaultSection(coordDM, &coordSection);CHKERRQ(ierr); 34919ab0a2SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 35919ab0a2SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 36919ab0a2SMatthew G. Knepley ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 37919ab0a2SMatthew G. Knepley ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr); 38919ab0a2SMatthew G. Knepley ierr = DMPlexGetMaxSizes(udm, &maxConeSize, NULL);CHKERRQ(ierr); 39919ab0a2SMatthew G. Knepley numCells = cEnd - cStart; 40919ab0a2SMatthew G. Knepley numVertices = vEnd - vStart; 41d2d4c474SMatthew G. Knepley ierr = PetscCalloc5(numVertices, &x, numVertices, &y, numVertices, &z, numVertices*PetscSqr(dim), &metric, numCells*maxConeSize, &cells);CHKERRQ(ierr); 42919ab0a2SMatthew G. Knepley ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr); 43d2d4c474SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 44919ab0a2SMatthew G. Knepley PetscInt off; 45919ab0a2SMatthew G. Knepley 46919ab0a2SMatthew G. Knepley ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr); 47d2d4c474SMatthew G. Knepley x[v-vStart] = coords[off+0]; 48d2d4c474SMatthew G. Knepley y[v-vStart] = coords[off+1]; 49d2d4c474SMatthew G. Knepley if (dim > 2) z[v-vStart] = coords[off+2]; 50919ab0a2SMatthew G. Knepley } 51919ab0a2SMatthew G. Knepley ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr); 52919ab0a2SMatthew G. Knepley for (c = 0, coff = 0; c < numCells; ++c) { 53919ab0a2SMatthew G. Knepley const PetscInt *cone; 54919ab0a2SMatthew G. Knepley PetscInt coneSize, cl; 55919ab0a2SMatthew G. Knepley 56919ab0a2SMatthew G. Knepley ierr = DMPlexGetConeSize(udm, c, &coneSize);CHKERRQ(ierr); 57919ab0a2SMatthew G. Knepley ierr = DMPlexGetCone(udm, c, &cone);CHKERRQ(ierr); 58d2d4c474SMatthew G. Knepley for (cl = 0; cl < coneSize; ++cl) cells[coff++] = cone[cl] - vStart; 59919ab0a2SMatthew G. Knepley } 60919ab0a2SMatthew G. Knepley switch (dim) { 61919ab0a2SMatthew G. Knepley case 2: 62919ab0a2SMatthew G. Knepley pragmatic_2d_init(&numVertices, &numCells, cells, x, y); 63919ab0a2SMatthew G. Knepley break; 64919ab0a2SMatthew G. Knepley case 3: 65919ab0a2SMatthew G. Knepley pragmatic_3d_init(&numVertices, &numCells, cells, x, y, z); 66919ab0a2SMatthew G. Knepley break; 67919ab0a2SMatthew G. Knepley default: 68919ab0a2SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No Pragmatic coarsening defined for dimension %d", dim); 69919ab0a2SMatthew G. Knepley } 70919ab0a2SMatthew G. Knepley /* Create boundary mesh */ 71919ab0a2SMatthew G. Knepley ierr = DMLabelCreate("boundary", &bd);CHKERRQ(ierr); 72919ab0a2SMatthew G. Knepley ierr = DMPlexMarkBoundaryFaces(dm, bd);CHKERRQ(ierr); 73919ab0a2SMatthew G. Knepley ierr = DMLabelGetStratumIS(bd, 1, &bdIS);CHKERRQ(ierr); 74919ab0a2SMatthew G. Knepley ierr = DMLabelGetStratumSize(bd, 1, &numBdFaces);CHKERRQ(ierr); 75919ab0a2SMatthew G. Knepley ierr = ISGetIndices(bdIS, &faces);CHKERRQ(ierr); 76919ab0a2SMatthew G. Knepley for (f = 0, bdSize = 0; f < numBdFaces; ++f) { 77919ab0a2SMatthew G. Knepley PetscInt *closure = NULL; 78919ab0a2SMatthew G. Knepley PetscInt closureSize, cl; 79919ab0a2SMatthew G. Knepley 80d2d4c474SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, faces[f], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 81919ab0a2SMatthew G. Knepley for (cl = 0; cl < closureSize*2; cl += 2) { 82919ab0a2SMatthew G. Knepley if ((closure[cl] >= vStart) && (closure[cl] < vEnd)) ++bdSize; 83919ab0a2SMatthew G. Knepley } 84919ab0a2SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 85919ab0a2SMatthew G. Knepley } 86919ab0a2SMatthew G. Knepley ierr = PetscMalloc2(bdSize, &bdFaces, numBdFaces, &bdFaceIds);CHKERRQ(ierr); 87919ab0a2SMatthew G. Knepley for (f = 0, bdSize = 0; f < numBdFaces; ++f) { 88919ab0a2SMatthew G. Knepley PetscInt *closure = NULL; 89919ab0a2SMatthew G. Knepley PetscInt closureSize, cl; 90919ab0a2SMatthew G. Knepley 91d2d4c474SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, faces[f], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 92919ab0a2SMatthew G. Knepley for (cl = 0; cl < closureSize*2; cl += 2) { 93d2d4c474SMatthew G. Knepley if ((closure[cl] >= vStart) && (closure[cl] < vEnd)) bdFaces[bdSize++] = closure[cl] - vStart; 94919ab0a2SMatthew G. Knepley } 95919ab0a2SMatthew G. Knepley /* TODO Fix */ 96919ab0a2SMatthew G. Knepley bdFaceIds[f] = 1; 97919ab0a2SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 98919ab0a2SMatthew G. Knepley } 99919ab0a2SMatthew G. Knepley ierr = ISDestroy(&bdIS);CHKERRQ(ierr); 100d2d4c474SMatthew G. Knepley ierr = DMLabelDestroy(&bd);CHKERRQ(ierr); 101919ab0a2SMatthew G. Knepley pragmatic_set_boundary(&numBdFaces, bdFaces, bdFaceIds); 102919ab0a2SMatthew G. Knepley /* Create metric */ 103*9f3102b2SMatthew G. Knepley size = (dim*(dim+1))/2; 104*9f3102b2SMatthew G. Knepley ierr = PetscMalloc1(PetscSqr(size), &eqns);CHKERRQ(ierr); 105*9f3102b2SMatthew G. Knepley ierr = MatCreateSeqDense(PETSC_COMM_SELF, size, size, eqns, &A);CHKERRQ(ierr); 106*9f3102b2SMatthew G. Knepley ierr = MatCreateVecs(A, &mx, &mb);CHKERRQ(ierr); 107*9f3102b2SMatthew G. Knepley ierr = VecSet(mb, 1.0);CHKERRQ(ierr); 108*9f3102b2SMatthew G. Knepley for (c = 0; c < numCells; ++c) { 109*9f3102b2SMatthew G. Knepley const PetscScalar *sol; 110*9f3102b2SMatthew G. Knepley PetscScalar *cellCoords = NULL; 111*9f3102b2SMatthew G. Knepley PetscReal e[3], vol; 112*9f3102b2SMatthew G. Knepley const PetscInt *cone; 113*9f3102b2SMatthew G. Knepley PetscInt coneSize, cl, i, j, d, r; 114*9f3102b2SMatthew G. Knepley 115*9f3102b2SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, NULL, &cellCoords);CHKERRQ(ierr); 116*9f3102b2SMatthew G. Knepley /* Only works for simplices */ 117*9f3102b2SMatthew G. Knepley for (i = 0, r = 0; i < dim+1; ++i) { 118*9f3102b2SMatthew G. Knepley for (j = 0; j < i; ++j, ++r) { 119*9f3102b2SMatthew G. Knepley for (d = 0; d < dim; ++d) e[d] = cellCoords[i*dim+d] - cellCoords[j*dim+d]; 120*9f3102b2SMatthew G. Knepley /* FORTRAN ORDERING */ 121*9f3102b2SMatthew G. Knepley if (dim == 2) { 122*9f3102b2SMatthew G. Knepley eqns[0*size+r] = PetscSqr(e[0]); 123*9f3102b2SMatthew G. Knepley eqns[1*size+r] = 2.0*e[0]*e[1]; 124*9f3102b2SMatthew G. Knepley eqns[2*size+r] = PetscSqr(e[1]); 125*9f3102b2SMatthew G. Knepley } else { 126*9f3102b2SMatthew G. Knepley eqns[0*size+r] = PetscSqr(e[0]); 127*9f3102b2SMatthew G. Knepley eqns[1*size+r] = 2.0*e[0]*e[1]; 128*9f3102b2SMatthew G. Knepley eqns[2*size+r] = 2.0*e[0]*e[2]; 129*9f3102b2SMatthew G. Knepley eqns[3*size+r] = PetscSqr(e[1]); 130*9f3102b2SMatthew G. Knepley eqns[4*size+r] = 2.0*e[1]*e[2]; 131*9f3102b2SMatthew G. Knepley eqns[5*size+r] = PetscSqr(e[2]); 132*9f3102b2SMatthew G. Knepley } 133*9f3102b2SMatthew G. Knepley } 134*9f3102b2SMatthew G. Knepley } 135*9f3102b2SMatthew G. Knepley ierr = MatSetUnfactored(A);CHKERRQ(ierr); 136*9f3102b2SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, NULL, &cellCoords);CHKERRQ(ierr); 137*9f3102b2SMatthew G. Knepley ierr = MatLUFactor(A, NULL, NULL, NULL);CHKERRQ(ierr); 138*9f3102b2SMatthew G. Knepley ierr = DMPrintCellMatrix(c, "Metric", size, size, eqns);CHKERRQ(ierr); 139*9f3102b2SMatthew G. Knepley ierr = MatSolve(A, mb, mx);CHKERRQ(ierr); 140*9f3102b2SMatthew G. Knepley ierr = VecGetArrayRead(mx, &sol);CHKERRQ(ierr); 141*9f3102b2SMatthew G. Knepley ierr = DMPrintCellVector(c, "Metric", size, sol);CHKERRQ(ierr); 142*9f3102b2SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr); 143*9f3102b2SMatthew G. Knepley ierr = DMPlexGetCone(udm, c, &cone);CHKERRQ(ierr); 144*9f3102b2SMatthew G. Knepley ierr = DMPlexGetConeSize(udm, c, &coneSize);CHKERRQ(ierr); 145*9f3102b2SMatthew G. Knepley for (cl = 0; cl < coneSize; ++cl) { 146*9f3102b2SMatthew G. Knepley const PetscInt v = cone[cl] - vStart; 147*9f3102b2SMatthew G. Knepley 148*9f3102b2SMatthew G. Knepley if (dim == 2) { 149*9f3102b2SMatthew G. Knepley metric[v*4+0] += vol*coarseRatio*sol[0]; 150*9f3102b2SMatthew G. Knepley metric[v*4+1] += vol*coarseRatio*sol[1]; 151*9f3102b2SMatthew G. Knepley metric[v*4+2] += vol*coarseRatio*sol[1]; 152*9f3102b2SMatthew G. Knepley metric[v*4+3] += vol*coarseRatio*sol[2]; 153*9f3102b2SMatthew G. Knepley } else { 154*9f3102b2SMatthew G. Knepley metric[v*9+0] += vol*coarseRatio*sol[0]; 155*9f3102b2SMatthew G. Knepley metric[v*9+1] += vol*coarseRatio*sol[1]; 156*9f3102b2SMatthew G. Knepley metric[v*9+3] += vol*coarseRatio*sol[1]; 157*9f3102b2SMatthew G. Knepley metric[v*9+2] += vol*coarseRatio*sol[2]; 158*9f3102b2SMatthew G. Knepley metric[v*9+6] += vol*coarseRatio*sol[2]; 159*9f3102b2SMatthew G. Knepley metric[v*9+4] += vol*coarseRatio*sol[3]; 160*9f3102b2SMatthew G. Knepley metric[v*9+5] += vol*coarseRatio*sol[4]; 161*9f3102b2SMatthew G. Knepley metric[v*9+7] += vol*coarseRatio*sol[4]; 162*9f3102b2SMatthew G. Knepley metric[v*9+8] += vol*coarseRatio*sol[5]; 163*9f3102b2SMatthew G. Knepley } 164*9f3102b2SMatthew G. Knepley } 165*9f3102b2SMatthew G. Knepley ierr = VecRestoreArrayRead(mx, &sol);CHKERRQ(ierr); 166*9f3102b2SMatthew G. Knepley } 167*9f3102b2SMatthew G. Knepley for (v = 0; v < numVertices; ++v) { 168*9f3102b2SMatthew G. Knepley const PetscInt *support; 169*9f3102b2SMatthew G. Knepley PetscInt supportSize, s; 170*9f3102b2SMatthew G. Knepley PetscReal vol, totVol = 0.0; 171*9f3102b2SMatthew G. Knepley 172*9f3102b2SMatthew G. Knepley ierr = DMPlexGetSupport(udm, v+vStart, &support);CHKERRQ(ierr); 173*9f3102b2SMatthew G. Knepley ierr = DMPlexGetSupportSize(udm, v+vStart, &supportSize);CHKERRQ(ierr); 174*9f3102b2SMatthew G. Knepley for (s = 0; s < supportSize; ++s) {ierr = DMPlexComputeCellGeometryFVM(dm, support[s], &vol, NULL, NULL);CHKERRQ(ierr); totVol += vol;} 175*9f3102b2SMatthew G. Knepley for (s = 0; s < PetscSqr(dim); ++s) metric[v*PetscSqr(dim)+s] /= totVol; 176*9f3102b2SMatthew G. Knepley ierr = DMPrintCellMatrix(v, "Metric", dim, dim, &metric[v*PetscSqr(dim)]);CHKERRQ(ierr); 177*9f3102b2SMatthew G. Knepley } 178*9f3102b2SMatthew G. Knepley ierr = VecDestroy(&mx);CHKERRQ(ierr); 179*9f3102b2SMatthew G. Knepley ierr = VecDestroy(&mb);CHKERRQ(ierr); 180*9f3102b2SMatthew G. Knepley ierr = MatDestroy(&A);CHKERRQ(ierr); 181*9f3102b2SMatthew G. Knepley ierr = DMDestroy(&udm);CHKERRQ(ierr); 182*9f3102b2SMatthew G. Knepley ierr = PetscFree(eqns);CHKERRQ(ierr); 183919ab0a2SMatthew G. Knepley pragmatic_set_metric(metric); 184919ab0a2SMatthew G. Knepley pragmatic_adapt(); 185919ab0a2SMatthew G. Knepley /* Read out mesh */ 186919ab0a2SMatthew G. Knepley pragmatic_get_info(&numCoarseVertices, &numCoarseCells); 187d2d4c474SMatthew G. Knepley ierr = PetscMalloc1(numCoarseVertices*dim, &coarseCoords);CHKERRQ(ierr); 188919ab0a2SMatthew G. Knepley switch (dim) { 189919ab0a2SMatthew G. Knepley case 2: 190919ab0a2SMatthew G. Knepley pragmatic_get_coords_2d(x, y); 191919ab0a2SMatthew G. Knepley numCorners = 3; 192919ab0a2SMatthew G. Knepley for (v = 0; v < numCoarseVertices; ++v) {coarseCoords[v*2+0] = x[v]; coarseCoords[v*2+1] = y[v];} 193919ab0a2SMatthew G. Knepley break; 194919ab0a2SMatthew G. Knepley case 3: 195919ab0a2SMatthew G. Knepley pragmatic_get_coords_3d(x, y, z); 196919ab0a2SMatthew G. Knepley numCorners = 4; 197919ab0a2SMatthew 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];} 198919ab0a2SMatthew G. Knepley break; 199919ab0a2SMatthew G. Knepley default: 200919ab0a2SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No Pragmatic coarsening defined for dimension %d", dim); 201919ab0a2SMatthew G. Knepley } 202919ab0a2SMatthew G. Knepley pragmatic_get_elements(cells); 203919ab0a2SMatthew G. Knepley /* TODO Read out markers for boundary */ 204919ab0a2SMatthew G. Knepley ierr = DMPlexCreateFromCellList(PetscObjectComm((PetscObject) dm), dim, numCoarseCells, numCoarseVertices, numCorners, PETSC_TRUE, cells, dim, coarseCoords, &mesh->coarseMesh);CHKERRQ(ierr); 205919ab0a2SMatthew G. Knepley pragmatic_finalize(); 206d2d4c474SMatthew G. Knepley ierr = PetscFree5(x, y, z, metric, cells);CHKERRQ(ierr); 207919ab0a2SMatthew G. Knepley ierr = PetscFree2(bdFaces, bdFaceIds);CHKERRQ(ierr); 208919ab0a2SMatthew G. Knepley ierr = PetscFree(coarseCoords);CHKERRQ(ierr); 209919ab0a2SMatthew G. Knepley } 210919ab0a2SMatthew G. Knepley #endif 211919ab0a2SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) mesh->coarseMesh);CHKERRQ(ierr); 212919ab0a2SMatthew G. Knepley *dmCoarsened = mesh->coarseMesh; 213919ab0a2SMatthew G. Knepley PetscFunctionReturn(0); 214919ab0a2SMatthew G. Knepley } 215