15d16530eSToby Isaac #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 25d16530eSToby Isaac #include <petscsf.h> 35d16530eSToby Isaac 45d16530eSToby Isaac #include <petsc/private/petscfeimpl.h> 55d16530eSToby Isaac #include <petsc/private/petscfvimpl.h> 65d16530eSToby Isaac 75d16530eSToby Isaac #undef __FUNCT__ 85d16530eSToby Isaac #define __FUNCT__ "DMPlexApplyLimiter_Internal" 9*df1f6d97SMatthew G. Knepley static PetscErrorCode DMPlexApplyLimiter_Internal(DM dm, DM dmCell, PetscLimiter lim, PetscInt dim, PetscInt dof, PetscInt off, PetscInt cell, PetscInt face, PetscInt fStart, PetscInt fEnd, 10*df1f6d97SMatthew G. Knepley PetscReal *cellPhi, const PetscScalar *x, const PetscScalar *cellgeom, const PetscFVCellGeom *cg, const PetscScalar *cx, const PetscScalar *cgrad) 115d16530eSToby Isaac { 125d16530eSToby Isaac const PetscInt *children; 135d16530eSToby Isaac PetscInt numChildren; 145d16530eSToby Isaac PetscErrorCode ierr; 155d16530eSToby Isaac 165d16530eSToby Isaac PetscFunctionBegin; 175d16530eSToby Isaac ierr = DMPlexGetTreeChildren(dm,face,&numChildren,&children);CHKERRQ(ierr); 185d16530eSToby Isaac if (numChildren) { 195d16530eSToby Isaac PetscInt c; 205d16530eSToby Isaac 215d16530eSToby Isaac for (c = 0; c < numChildren; c++) { 225d16530eSToby Isaac PetscInt childFace = children[c]; 235d16530eSToby Isaac 245d16530eSToby Isaac if (childFace >= fStart && childFace < fEnd) { 25*df1f6d97SMatthew G. Knepley ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,dof,off,cell,childFace,fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr); 265d16530eSToby Isaac } 275d16530eSToby Isaac } 28*df1f6d97SMatthew G. Knepley } else { 295d16530eSToby Isaac PetscScalar *ncx; 305d16530eSToby Isaac PetscFVCellGeom *ncg; 315d16530eSToby Isaac const PetscInt *fcells; 325d16530eSToby Isaac PetscInt ncell, d; 335d16530eSToby Isaac PetscReal v[3]; 345d16530eSToby Isaac 355d16530eSToby Isaac ierr = DMPlexGetSupport(dm, face, &fcells);CHKERRQ(ierr); 365d16530eSToby Isaac ncell = cell == fcells[0] ? fcells[1] : fcells[0]; 375d16530eSToby Isaac ierr = DMPlexPointLocalRead(dm, ncell, x, &ncx);CHKERRQ(ierr); 385d16530eSToby Isaac ierr = DMPlexPointLocalRead(dmCell, ncell, cellgeom, &ncg);CHKERRQ(ierr); 395d16530eSToby Isaac DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, ncg->centroid, v); 40*df1f6d97SMatthew G. Knepley for (d = 0; d < dof; ++d) { 415d16530eSToby Isaac /* We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005 */ 42*df1f6d97SMatthew G. Knepley PetscReal phi, flim = 0.5 * PetscRealPart(ncx[off+d] - cx[off+d]) / DMPlex_DotD_Internal(dim, &cgrad[d*dim], v); 435d16530eSToby Isaac 445d16530eSToby Isaac ierr = PetscLimiterLimit(lim, flim, &phi);CHKERRQ(ierr); 455d16530eSToby Isaac cellPhi[d] = PetscMin(cellPhi[d], phi); 465d16530eSToby Isaac } 475d16530eSToby Isaac } 485d16530eSToby Isaac PetscFunctionReturn(0); 495d16530eSToby Isaac } 505d16530eSToby Isaac 515d16530eSToby Isaac #undef __FUNCT__ 525d16530eSToby Isaac #define __FUNCT__ "DMPlexReconstructGradients_Internal" 53*df1f6d97SMatthew G. Knepley PetscErrorCode DMPlexReconstructGradients_Internal(DM dm, PetscFV fvm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, Vec locX, Vec grad) 545d16530eSToby Isaac { 555d16530eSToby Isaac DM dmFace, dmCell, dmGrad; 565d16530eSToby Isaac DMLabel ghostLabel; 575d16530eSToby Isaac PetscDS prob; 585d16530eSToby Isaac PetscLimiter lim; 595d16530eSToby Isaac const PetscScalar *facegeom, *cellgeom, *x; 605d16530eSToby Isaac PetscScalar *gr; 615d16530eSToby Isaac PetscReal *cellPhi; 62*df1f6d97SMatthew G. Knepley PetscInt dim, face, cell, field, dof, off, cStart, cEnd, cEndInterior; 635d16530eSToby Isaac PetscErrorCode ierr; 645d16530eSToby Isaac 655d16530eSToby Isaac PetscFunctionBegin; 665d16530eSToby Isaac ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 675d16530eSToby Isaac ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 68*df1f6d97SMatthew G. Knepley ierr = PetscDSGetFieldIndex(prob, (PetscObject) fvm, &field);CHKERRQ(ierr); 69*df1f6d97SMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, field, &off);CHKERRQ(ierr); 70*df1f6d97SMatthew G. Knepley ierr = PetscDSGetFieldSize(prob, field, &dof);CHKERRQ(ierr); 715d16530eSToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 725d16530eSToby Isaac ierr = PetscFVGetLimiter(fvm, &lim);CHKERRQ(ierr); 735d16530eSToby Isaac ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 745d16530eSToby Isaac ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 755d16530eSToby Isaac ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 765d16530eSToby Isaac ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 775d16530eSToby Isaac ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 785d16530eSToby Isaac ierr = VecGetDM(grad, &dmGrad);CHKERRQ(ierr); 795d16530eSToby Isaac ierr = VecZeroEntries(grad);CHKERRQ(ierr); 805d16530eSToby Isaac ierr = VecGetArray(grad, &gr);CHKERRQ(ierr); 815d16530eSToby Isaac /* Reconstruct gradients */ 825d16530eSToby Isaac for (face = fStart; face < fEnd; ++face) { 835d16530eSToby Isaac const PetscInt *cells; 845d16530eSToby Isaac PetscFVFaceGeom *fg; 855d16530eSToby Isaac PetscScalar *cx[2]; 865d16530eSToby Isaac PetscScalar *cgrad[2]; 875d16530eSToby Isaac PetscBool boundary; 885d16530eSToby Isaac PetscInt ghost, c, pd, d, numChildren, numCells; 895d16530eSToby Isaac 905d16530eSToby Isaac ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 915d16530eSToby Isaac ierr = DMIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr); 925d16530eSToby Isaac ierr = DMPlexGetTreeChildren(dm, face, &numChildren, NULL);CHKERRQ(ierr); 935d16530eSToby Isaac if (ghost >= 0 || boundary || numChildren) continue; 945d16530eSToby Isaac ierr = DMPlexGetSupportSize(dm, face, &numCells);CHKERRQ(ierr); 955d16530eSToby Isaac if (numCells != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "facet %d has %d support points: expected 2",face,numCells); 965d16530eSToby Isaac ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 975d16530eSToby Isaac ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 985d16530eSToby Isaac for (c = 0; c < 2; ++c) { 995d16530eSToby Isaac ierr = DMPlexPointLocalRead(dm, cells[c], x, &cx[c]);CHKERRQ(ierr); 1005d16530eSToby Isaac ierr = DMPlexPointGlobalRef(dmGrad, cells[c], gr, &cgrad[c]);CHKERRQ(ierr); 1015d16530eSToby Isaac } 102*df1f6d97SMatthew G. Knepley for (pd = 0; pd < dof; ++pd) { 103*df1f6d97SMatthew G. Knepley PetscScalar delta = cx[1][off+pd] - cx[0][off+pd]; 1045d16530eSToby Isaac 1055d16530eSToby Isaac for (d = 0; d < dim; ++d) { 1065d16530eSToby Isaac if (cgrad[0]) cgrad[0][pd*dim+d] += fg->grad[0][d] * delta; 1075d16530eSToby Isaac if (cgrad[1]) cgrad[1][pd*dim+d] -= fg->grad[1][d] * delta; 1085d16530eSToby Isaac } 1095d16530eSToby Isaac } 1105d16530eSToby Isaac } 1115d16530eSToby Isaac /* Limit interior gradients (using cell-based loop because it generalizes better to vector limiters) */ 1125d16530eSToby Isaac ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1135d16530eSToby Isaac ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1145d16530eSToby Isaac cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior; 115*df1f6d97SMatthew G. Knepley ierr = DMGetWorkArray(dm, dof, PETSC_REAL, &cellPhi);CHKERRQ(ierr); 1165d16530eSToby Isaac for (cell = dmGrad && lim ? cStart : cEnd; cell < cEndInterior; ++cell) { 1175d16530eSToby Isaac const PetscInt *faces; 1185d16530eSToby Isaac PetscScalar *cx; 1195d16530eSToby Isaac PetscFVCellGeom *cg; 1205d16530eSToby Isaac PetscScalar *cgrad; 1215d16530eSToby Isaac PetscInt coneSize, f, pd, d; 1225d16530eSToby Isaac 1235d16530eSToby Isaac ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 1245d16530eSToby Isaac ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr); 1255d16530eSToby Isaac ierr = DMPlexPointLocalRead(dm, cell, x, &cx);CHKERRQ(ierr); 1265d16530eSToby Isaac ierr = DMPlexPointLocalRead(dmCell, cell, cellgeom, &cg);CHKERRQ(ierr); 1275d16530eSToby Isaac ierr = DMPlexPointGlobalRef(dmGrad, cell, gr, &cgrad);CHKERRQ(ierr); 1285d16530eSToby Isaac if (!cgrad) continue; /* Unowned overlap cell, we do not compute */ 1295d16530eSToby Isaac /* Limiter will be minimum value over all neighbors */ 130*df1f6d97SMatthew G. Knepley for (d = 0; d < dof; ++d) cellPhi[d] = PETSC_MAX_REAL; 1315d16530eSToby Isaac for (f = 0; f < coneSize; ++f) { 132*df1f6d97SMatthew G. Knepley ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,dof,off,cell,faces[f],fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr); 1335d16530eSToby Isaac } 1345d16530eSToby Isaac /* Apply limiter to gradient */ 135*df1f6d97SMatthew G. Knepley for (pd = 0; pd < dof; ++pd) 1365d16530eSToby Isaac /* Scalar limiter applied to each component separately */ 1375d16530eSToby Isaac for (d = 0; d < dim; ++d) cgrad[pd*dim+d] *= cellPhi[pd]; 1385d16530eSToby Isaac } 139*df1f6d97SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, dof, PETSC_REAL, &cellPhi);CHKERRQ(ierr); 1405d16530eSToby Isaac ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 1415d16530eSToby Isaac ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 1425d16530eSToby Isaac ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 1435d16530eSToby Isaac ierr = VecRestoreArray(grad, &gr);CHKERRQ(ierr); 1445d16530eSToby Isaac PetscFunctionReturn(0); 1455d16530eSToby Isaac } 1465d16530eSToby Isaac 1475d16530eSToby Isaac #undef __FUNCT__ 1485d16530eSToby Isaac #define __FUNCT__ "DMPlexReconstructGradientsFVM" 1495d16530eSToby Isaac /*@ 1505d16530eSToby Isaac DMPlexReconstructGradientsFVM - reconstruct the gradient of a vector using a finite volume method. 1515d16530eSToby Isaac 1525d16530eSToby Isaac Input Parameters: 1535d16530eSToby Isaac + dm - the mesh 1545d16530eSToby Isaac - locX - the local representation of the vector 1555d16530eSToby Isaac 1565d16530eSToby Isaac Output Parameter: 1575d16530eSToby Isaac . grad - the global representation of the gradient 1585d16530eSToby Isaac 1595d16530eSToby Isaac Level: developer 1605d16530eSToby Isaac 1615d16530eSToby Isaac .seealso: DMPlexSNESGetGradientDM() 1625d16530eSToby Isaac @*/ 1635d16530eSToby Isaac PetscErrorCode DMPlexReconstructGradientsFVM(DM dm, Vec locX, Vec grad) 1645d16530eSToby Isaac { 1655d16530eSToby Isaac PetscDS prob; 1665d16530eSToby Isaac PetscInt Nf, f, fStart, fEnd; 1675d16530eSToby Isaac PetscBool useFVM = PETSC_FALSE; 1685d16530eSToby Isaac PetscFV fvm = NULL; 1695d16530eSToby Isaac Vec faceGeometryFVM, cellGeometryFVM; 1705d16530eSToby Isaac PetscFVCellGeom *cgeomFVM = NULL; 1715d16530eSToby Isaac PetscFVFaceGeom *fgeomFVM = NULL; 1725d16530eSToby Isaac DM dmGrad = NULL; 1735d16530eSToby Isaac PetscErrorCode ierr; 1745d16530eSToby Isaac 1755d16530eSToby Isaac PetscFunctionBegin; 1765d16530eSToby Isaac ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1775d16530eSToby Isaac ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1785d16530eSToby Isaac for (f = 0; f < Nf; ++f) { 1795d16530eSToby Isaac PetscObject obj; 1805d16530eSToby Isaac PetscClassId id; 1815d16530eSToby Isaac 1825d16530eSToby Isaac ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1835d16530eSToby Isaac ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1845d16530eSToby Isaac if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 1855d16530eSToby Isaac } 1865d16530eSToby Isaac if (!useFVM) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm does not have a finite volume discretization"); 1875d16530eSToby Isaac ierr = DMPlexGetDataFVM(dm, fvm, &cellGeometryFVM, &faceGeometryFVM, &dmGrad);CHKERRQ(ierr); 1885d16530eSToby Isaac if (!dmGrad) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm's finite volume discretization does not reconstruct gradients"); 1895d16530eSToby Isaac ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 1905d16530eSToby Isaac ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1915d16530eSToby Isaac ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 192*df1f6d97SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 1935d16530eSToby Isaac PetscFunctionReturn(0); 1945d16530eSToby Isaac } 1955d16530eSToby Isaac 196