1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2ccd2543fSMatthew G Knepley 3ccd2543fSMatthew G Knepley #undef __FUNCT__ 4ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal" 5ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 6ccd2543fSMatthew G Knepley { 7ccd2543fSMatthew G Knepley const PetscInt embedDim = 2; 8ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 9ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 10ccd2543fSMatthew G Knepley PetscReal v0[2], J[4], invJ[4], detJ; 11ccd2543fSMatthew G Knepley PetscReal xi, eta; 12ccd2543fSMatthew G Knepley PetscErrorCode ierr; 13ccd2543fSMatthew G Knepley 14ccd2543fSMatthew G Knepley PetscFunctionBegin; 158e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 16ccd2543fSMatthew G Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]); 17ccd2543fSMatthew G Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]); 18ccd2543fSMatthew G Knepley 19ccd2543fSMatthew G Knepley if ((xi >= 0.0) && (eta >= 0.0) && (xi + eta <= 2.0)) *cell = c; 20ccd2543fSMatthew G Knepley else *cell = -1; 21ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 22ccd2543fSMatthew G Knepley } 23ccd2543fSMatthew G Knepley 24ccd2543fSMatthew G Knepley #undef __FUNCT__ 25ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal" 26ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 27ccd2543fSMatthew G Knepley { 28ccd2543fSMatthew G Knepley PetscSection coordSection; 29ccd2543fSMatthew G Knepley Vec coordsLocal; 30a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 31ccd2543fSMatthew G Knepley const PetscInt faces[8] = {0, 1, 1, 2, 2, 3, 3, 0}; 32ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 33ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 34ccd2543fSMatthew G Knepley PetscInt crossings = 0, f; 35ccd2543fSMatthew G Knepley PetscErrorCode ierr; 36ccd2543fSMatthew G Knepley 37ccd2543fSMatthew G Knepley PetscFunctionBegin; 38ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 3969d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 40ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 41ccd2543fSMatthew G Knepley for (f = 0; f < 4; ++f) { 42ccd2543fSMatthew G Knepley PetscReal x_i = PetscRealPart(coords[faces[2*f+0]*2+0]); 43ccd2543fSMatthew G Knepley PetscReal y_i = PetscRealPart(coords[faces[2*f+0]*2+1]); 44ccd2543fSMatthew G Knepley PetscReal x_j = PetscRealPart(coords[faces[2*f+1]*2+0]); 45ccd2543fSMatthew G Knepley PetscReal y_j = PetscRealPart(coords[faces[2*f+1]*2+1]); 46ccd2543fSMatthew G Knepley PetscReal slope = (y_j - y_i) / (x_j - x_i); 47ccd2543fSMatthew G Knepley PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE; 48ccd2543fSMatthew G Knepley PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE; 49ccd2543fSMatthew G Knepley PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE; 50ccd2543fSMatthew G Knepley if ((cond1 || cond2) && above) ++crossings; 51ccd2543fSMatthew G Knepley } 52ccd2543fSMatthew G Knepley if (crossings % 2) *cell = c; 53ccd2543fSMatthew G Knepley else *cell = -1; 54ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 55ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 56ccd2543fSMatthew G Knepley } 57ccd2543fSMatthew G Knepley 58ccd2543fSMatthew G Knepley #undef __FUNCT__ 59ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal" 60ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 61ccd2543fSMatthew G Knepley { 62ccd2543fSMatthew G Knepley const PetscInt embedDim = 3; 63ccd2543fSMatthew G Knepley PetscReal v0[3], J[9], invJ[9], detJ; 64ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 65ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 66ccd2543fSMatthew G Knepley PetscReal z = PetscRealPart(point[2]); 67ccd2543fSMatthew G Knepley PetscReal xi, eta, zeta; 68ccd2543fSMatthew G Knepley PetscErrorCode ierr; 69ccd2543fSMatthew G Knepley 70ccd2543fSMatthew G Knepley PetscFunctionBegin; 718e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 72ccd2543fSMatthew G Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]); 73ccd2543fSMatthew G Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]); 74ccd2543fSMatthew G Knepley zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]); 75ccd2543fSMatthew G Knepley 76ccd2543fSMatthew G Knepley if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c; 77ccd2543fSMatthew G Knepley else *cell = -1; 78ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 79ccd2543fSMatthew G Knepley } 80ccd2543fSMatthew G Knepley 81ccd2543fSMatthew G Knepley #undef __FUNCT__ 82ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal" 83ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 84ccd2543fSMatthew G Knepley { 85ccd2543fSMatthew G Knepley PetscSection coordSection; 86ccd2543fSMatthew G Knepley Vec coordsLocal; 877c1f9639SMatthew G Knepley PetscScalar *coords; 88fb150da6SMatthew G. Knepley const PetscInt faces[24] = {0, 3, 2, 1, 5, 4, 7, 6, 3, 0, 4, 5, 89fb150da6SMatthew G. Knepley 1, 2, 6, 7, 3, 5, 6, 2, 0, 1, 7, 4}; 90ccd2543fSMatthew G Knepley PetscBool found = PETSC_TRUE; 91ccd2543fSMatthew G Knepley PetscInt f; 92ccd2543fSMatthew G Knepley PetscErrorCode ierr; 93ccd2543fSMatthew G Knepley 94ccd2543fSMatthew G Knepley PetscFunctionBegin; 95ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 9669d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 97ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 98ccd2543fSMatthew G Knepley for (f = 0; f < 6; ++f) { 99ccd2543fSMatthew G Knepley /* Check the point is under plane */ 100ccd2543fSMatthew G Knepley /* Get face normal */ 101ccd2543fSMatthew G Knepley PetscReal v_i[3]; 102ccd2543fSMatthew G Knepley PetscReal v_j[3]; 103ccd2543fSMatthew G Knepley PetscReal normal[3]; 104ccd2543fSMatthew G Knepley PetscReal pp[3]; 105ccd2543fSMatthew G Knepley PetscReal dot; 106ccd2543fSMatthew G Knepley 107ccd2543fSMatthew G Knepley v_i[0] = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]); 108ccd2543fSMatthew G Knepley v_i[1] = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]); 109ccd2543fSMatthew G Knepley v_i[2] = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]); 110ccd2543fSMatthew G Knepley v_j[0] = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]); 111ccd2543fSMatthew G Knepley v_j[1] = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]); 112ccd2543fSMatthew G Knepley v_j[2] = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]); 113ccd2543fSMatthew G Knepley normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1]; 114ccd2543fSMatthew G Knepley normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2]; 115ccd2543fSMatthew G Knepley normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0]; 116ccd2543fSMatthew G Knepley pp[0] = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]); 117ccd2543fSMatthew G Knepley pp[1] = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]); 118ccd2543fSMatthew G Knepley pp[2] = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]); 119ccd2543fSMatthew G Knepley dot = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2]; 120ccd2543fSMatthew G Knepley 121ccd2543fSMatthew G Knepley /* Check that projected point is in face (2D location problem) */ 122ccd2543fSMatthew G Knepley if (dot < 0.0) { 123ccd2543fSMatthew G Knepley found = PETSC_FALSE; 124ccd2543fSMatthew G Knepley break; 125ccd2543fSMatthew G Knepley } 126ccd2543fSMatthew G Knepley } 127ccd2543fSMatthew G Knepley if (found) *cell = c; 128ccd2543fSMatthew G Knepley else *cell = -1; 129ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 130ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 131ccd2543fSMatthew G Knepley } 132ccd2543fSMatthew G Knepley 133ccd2543fSMatthew G Knepley #undef __FUNCT__ 134c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal" 135c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[]) 136c4eade1cSMatthew G. Knepley { 137c4eade1cSMatthew G. Knepley PetscInt d; 138c4eade1cSMatthew G. Knepley 139c4eade1cSMatthew G. Knepley PetscFunctionBegin; 140c4eade1cSMatthew G. Knepley box->dim = dim; 141c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]); 142c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 143c4eade1cSMatthew G. Knepley } 144c4eade1cSMatthew G. Knepley 145c4eade1cSMatthew G. Knepley #undef __FUNCT__ 146c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate" 147c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box) 148c4eade1cSMatthew G. Knepley { 149c4eade1cSMatthew G. Knepley PetscErrorCode ierr; 150c4eade1cSMatthew G. Knepley 151c4eade1cSMatthew G. Knepley PetscFunctionBegin; 152c4eade1cSMatthew G. Knepley ierr = PetscMalloc1(1, box);CHKERRQ(ierr); 153c4eade1cSMatthew G. Knepley ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr); 154c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 155c4eade1cSMatthew G. Knepley } 156c4eade1cSMatthew G. Knepley 157c4eade1cSMatthew G. Knepley #undef __FUNCT__ 158c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge" 159c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[]) 160c4eade1cSMatthew G. Knepley { 161c4eade1cSMatthew G. Knepley PetscInt d; 162c4eade1cSMatthew G. Knepley 163c4eade1cSMatthew G. Knepley PetscFunctionBegin; 164c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 165c4eade1cSMatthew G. Knepley box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d])); 166c4eade1cSMatthew G. Knepley box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d])); 167c4eade1cSMatthew G. Knepley } 168c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 169c4eade1cSMatthew G. Knepley } 170c4eade1cSMatthew G. Knepley 171c4eade1cSMatthew G. Knepley #undef __FUNCT__ 172c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid" 173c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[]) 174c4eade1cSMatthew G. Knepley { 175c4eade1cSMatthew G. Knepley PetscInt d; 176c4eade1cSMatthew G. Knepley 177c4eade1cSMatthew G. Knepley PetscFunctionBegin; 178c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 179c4eade1cSMatthew G. Knepley box->extent[d] = box->upper[d] - box->lower[d]; 180c4eade1cSMatthew G. Knepley if (n[d] == PETSC_DETERMINE) { 181c4eade1cSMatthew G. Knepley box->h[d] = h[d]; 182c4eade1cSMatthew G. Knepley box->n[d] = PetscCeilReal(box->extent[d]/h[d]); 183c4eade1cSMatthew G. Knepley } else { 184c4eade1cSMatthew G. Knepley box->n[d] = n[d]; 185c4eade1cSMatthew G. Knepley box->h[d] = box->extent[d]/n[d]; 186c4eade1cSMatthew G. Knepley } 187c4eade1cSMatthew G. Knepley } 188c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 189c4eade1cSMatthew G. Knepley } 190c4eade1cSMatthew G. Knepley 191c4eade1cSMatthew G. Knepley #undef __FUNCT__ 192c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox" 1931c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[]) 194c4eade1cSMatthew G. Knepley { 195c4eade1cSMatthew G. Knepley const PetscReal *lower = box->lower; 196c4eade1cSMatthew G. Knepley const PetscReal *upper = box->upper; 197c4eade1cSMatthew G. Knepley const PetscReal *h = box->h; 198c4eade1cSMatthew G. Knepley const PetscInt *n = box->n; 199c4eade1cSMatthew G. Knepley const PetscInt dim = box->dim; 200c4eade1cSMatthew G. Knepley PetscInt d, p; 201c4eade1cSMatthew G. Knepley 202c4eade1cSMatthew G. Knepley PetscFunctionBegin; 203c4eade1cSMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 204c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) { 2051c6dfc3eSMatthew G. Knepley PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]); 206c4eade1cSMatthew G. Knepley 2071c6dfc3eSMatthew G. Knepley if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1; 208c4eade1cSMatthew G. Knepley if (dbox < 0 || dbox >= n[d]) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %d (%g, %g, %g) is outside of our bounding box", 2091c6dfc3eSMatthew G. Knepley p, PetscRealPart(points[p*dim+0]), dim > 1 ? PetscRealPart(points[p*dim+1]) : 0.0, dim > 2 ? PetscRealPart(points[p*dim+2]) : 0.0); 210c4eade1cSMatthew G. Knepley dboxes[p*dim+d] = dbox; 211c4eade1cSMatthew G. Knepley } 212c4eade1cSMatthew G. Knepley if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1]; 213c4eade1cSMatthew G. Knepley } 214c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 215c4eade1cSMatthew G. Knepley } 216c4eade1cSMatthew G. Knepley 217c4eade1cSMatthew G. Knepley #undef __FUNCT__ 218c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy" 219c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box) 220c4eade1cSMatthew G. Knepley { 221c4eade1cSMatthew G. Knepley PetscErrorCode ierr; 222c4eade1cSMatthew G. Knepley 223c4eade1cSMatthew G. Knepley PetscFunctionBegin; 224c4eade1cSMatthew G. Knepley if (*box) { 225c4eade1cSMatthew G. Knepley ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr); 226c4eade1cSMatthew G. Knepley ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr); 227c4eade1cSMatthew G. Knepley ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr); 228c4eade1cSMatthew G. Knepley } 229c4eade1cSMatthew G. Knepley ierr = PetscFree(*box);CHKERRQ(ierr); 230c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 231c4eade1cSMatthew G. Knepley } 232c4eade1cSMatthew G. Knepley 233cafe43deSMatthew G. Knepley #undef __FUNCT__ 234cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexLocatePoint_Internal" 235cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell) 236cafe43deSMatthew G. Knepley { 237cafe43deSMatthew G. Knepley PetscInt coneSize; 238cafe43deSMatthew G. Knepley PetscErrorCode ierr; 239cafe43deSMatthew G. Knepley 240cafe43deSMatthew G. Knepley PetscFunctionBegin; 241cafe43deSMatthew G. Knepley switch (dim) { 242cafe43deSMatthew G. Knepley case 2: 243cafe43deSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr); 244cafe43deSMatthew G. Knepley switch (coneSize) { 245cafe43deSMatthew G. Knepley case 3: 246cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 247cafe43deSMatthew G. Knepley break; 248cafe43deSMatthew G. Knepley case 4: 249cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 250cafe43deSMatthew G. Knepley break; 251cafe43deSMatthew G. Knepley default: 252cafe43deSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize); 253cafe43deSMatthew G. Knepley } 254cafe43deSMatthew G. Knepley break; 255cafe43deSMatthew G. Knepley case 3: 256cafe43deSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr); 257cafe43deSMatthew G. Knepley switch (coneSize) { 258cafe43deSMatthew G. Knepley case 4: 259cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 260cafe43deSMatthew G. Knepley break; 261cafe43deSMatthew G. Knepley case 6: 262cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 263cafe43deSMatthew G. Knepley break; 264cafe43deSMatthew G. Knepley default: 265cafe43deSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize); 266cafe43deSMatthew G. Knepley } 267cafe43deSMatthew G. Knepley break; 268cafe43deSMatthew G. Knepley default: 269cafe43deSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim); 270cafe43deSMatthew G. Knepley } 271cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 272cafe43deSMatthew G. Knepley } 273cafe43deSMatthew G. Knepley 274cafe43deSMatthew G. Knepley #undef __FUNCT__ 275cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGridHash_Internal" 276cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox) 277cafe43deSMatthew G. Knepley { 278cafe43deSMatthew G. Knepley MPI_Comm comm; 279cafe43deSMatthew G. Knepley PetscGridHash lbox; 280cafe43deSMatthew G. Knepley Vec coordinates; 281cafe43deSMatthew G. Knepley PetscSection coordSection; 282cafe43deSMatthew G. Knepley Vec coordsLocal; 283cafe43deSMatthew G. Knepley const PetscScalar *coords; 284*722d0f5cSMatthew G. Knepley PetscInt *dboxes, *boxes; 285cafe43deSMatthew G. Knepley PetscInt n[3] = {10, 10, 10}; 286cafe43deSMatthew G. Knepley PetscInt dim, N, cStart, cEnd, c, i; 287cafe43deSMatthew G. Knepley PetscErrorCode ierr; 288cafe43deSMatthew G. Knepley 289cafe43deSMatthew G. Knepley PetscFunctionBegin; 290cafe43deSMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 291cafe43deSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 292cafe43deSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 293cafe43deSMatthew G. Knepley ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr); 294cafe43deSMatthew G. Knepley ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr); 295cafe43deSMatthew G. Knepley ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr); 296cafe43deSMatthew G. Knepley for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);} 297cafe43deSMatthew G. Knepley ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr); 298cafe43deSMatthew G. Knepley ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr); 299cafe43deSMatthew G. Knepley #if 0 300cafe43deSMatthew G. Knepley /* Could define a custom reduction to merge these */ 301cafe43deSMatthew G. Knepley ierr = MPI_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr); 302cafe43deSMatthew G. Knepley ierr = MPI_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr); 303cafe43deSMatthew G. Knepley #endif 304cafe43deSMatthew G. Knepley /* Is there a reason to snap the local bounding box to a division of the global box? */ 305cafe43deSMatthew G. Knepley /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */ 306cafe43deSMatthew G. Knepley /* Create label */ 307cafe43deSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 308cafe43deSMatthew G. Knepley ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr); 309cafe43deSMatthew G. Knepley ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr); 310*722d0f5cSMatthew G. Knepley /* Compute boxes which overlap each cell: http://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */ 311cafe43deSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 312cafe43deSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 313*722d0f5cSMatthew G. Knepley ierr = PetscCalloc2((dim+1) * dim, &dboxes, dim+1, &boxes);CHKERRQ(ierr); 314cafe43deSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 315cafe43deSMatthew G. Knepley const PetscReal *h = lbox->h; 316cafe43deSMatthew G. Knepley PetscScalar *ccoords = NULL; 317cafe43deSMatthew G. Knepley PetscScalar point[3]; 318cafe43deSMatthew G. Knepley PetscInt dlim[6], d, e, i, j, k; 319cafe43deSMatthew G. Knepley 320cafe43deSMatthew G. Knepley /* Find boxes enclosing each vertex */ 321cafe43deSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr); 322cafe43deSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr); 323*722d0f5cSMatthew G. Knepley ierr = PetscGridHashGetEnclosingBox(lbox, dim+1, ccoords, dboxes, boxes);CHKERRQ(ierr); 324*722d0f5cSMatthew G. Knepley /* Mark cells containing the vertices */ 325*722d0f5cSMatthew G. Knepley for (e = 0; e < dim+1; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);} 326cafe43deSMatthew G. Knepley /* Get grid of boxes containing these */ 327cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];} 3282291669eSMatthew G. Knepley for (d = dim; d < 3; ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;} 329cafe43deSMatthew G. Knepley for (e = 1; e < dim+1; ++e) { 330cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) { 331cafe43deSMatthew G. Knepley dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]); 332cafe43deSMatthew G. Knepley dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]); 333cafe43deSMatthew G. Knepley } 334cafe43deSMatthew G. Knepley } 335cafe43deSMatthew G. Knepley /* Check whether cell contains any vertex of these subboxes TODO vectorize this */ 336cafe43deSMatthew G. Knepley for (k = dlim[2*2+0], point[2] = lbox->lower[2] + k*h[2]; k <= dlim[2*2+1]; ++k, point[2] += h[2]) { 337cafe43deSMatthew G. Knepley for (j = dlim[1*2+0], point[1] = lbox->lower[1] + j*h[1]; j <= dlim[1*2+1]; ++j, point[1] += h[1]) { 338cafe43deSMatthew G. Knepley for (i = dlim[0*2+0], point[0] = lbox->lower[0] + i*h[0]; i <= dlim[0*2+1]; ++i, point[0] += h[0]) { 339cafe43deSMatthew G. Knepley const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i; 340cafe43deSMatthew G. Knepley PetscScalar cpoint[3]; 341cafe43deSMatthew G. Knepley PetscInt cell, ii, jj, kk; 342cafe43deSMatthew G. Knepley 343cafe43deSMatthew G. Knepley for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) { 344cafe43deSMatthew G. Knepley for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) { 345cafe43deSMatthew G. Knepley for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) { 346cafe43deSMatthew G. Knepley 347cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr); 348cafe43deSMatthew G. Knepley if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;} 349cafe43deSMatthew G. Knepley } 350cafe43deSMatthew G. Knepley } 351cafe43deSMatthew G. Knepley } 352cafe43deSMatthew G. Knepley } 353cafe43deSMatthew G. Knepley } 354cafe43deSMatthew G. Knepley } 355cafe43deSMatthew G. Knepley } 356*722d0f5cSMatthew G. Knepley ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr); 357cafe43deSMatthew G. Knepley ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr); 358cafe43deSMatthew G. Knepley ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr); 359cafe43deSMatthew G. Knepley *localBox = lbox; 360cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 361cafe43deSMatthew G. Knepley } 362cafe43deSMatthew G. Knepley 363cafe43deSMatthew G. Knepley #undef __FUNCT__ 364ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex" 365ccd2543fSMatthew G Knepley /* 366ccd2543fSMatthew G Knepley Need to implement using the guess 367ccd2543fSMatthew G Knepley */ 368ccd2543fSMatthew G Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, IS *cellIS) 369ccd2543fSMatthew G Knepley { 370cafe43deSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 371ccd2543fSMatthew G Knepley PetscInt bs, numPoints, p; 3721318edbeSMatthew G. Knepley PetscInt dim, cStart, cEnd, cMax, numCells, c; 373cafe43deSMatthew G. Knepley const PetscInt *boxCells; 374ccd2543fSMatthew G Knepley PetscInt *cells; 375ccd2543fSMatthew G Knepley PetscScalar *a; 376ccd2543fSMatthew G Knepley PetscErrorCode ierr; 377ccd2543fSMatthew G Knepley 378ccd2543fSMatthew G Knepley PetscFunctionBegin; 379cafe43deSMatthew G. Knepley if (!mesh->lbox) {ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);} 380cafe43deSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 381cafe43deSMatthew G. Knepley ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr); 382cafe43deSMatthew G. Knepley if (bs != dim) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Block size for point vector %D must be the mesh coordinate dimension %D", bs, dim); 383ccd2543fSMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 384ccd2543fSMatthew G Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 385ccd2543fSMatthew G Knepley if (cMax >= 0) cEnd = PetscMin(cEnd, cMax); 386ccd2543fSMatthew G Knepley ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr); 387ccd2543fSMatthew G Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 388ccd2543fSMatthew G Knepley numPoints /= bs; 389785e854fSJed Brown ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr); 390cafe43deSMatthew G. Knepley /* Designate the local box for each point */ 391cafe43deSMatthew G. Knepley /* Send points to correct process */ 392cafe43deSMatthew G. Knepley /* Search cells that lie in each subbox */ 393cafe43deSMatthew G. Knepley /* Should we bin points before doing search? */ 394cafe43deSMatthew G. Knepley ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr); 395ccd2543fSMatthew G Knepley for (p = 0; p < numPoints; ++p) { 396ccd2543fSMatthew G Knepley const PetscScalar *point = &a[p*bs]; 3971318edbeSMatthew G. Knepley PetscInt dbin[3], bin, cell, cellOffset; 398ccd2543fSMatthew G Knepley 399cafe43deSMatthew G. Knepley ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr); 400cafe43deSMatthew G. Knepley /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */ 401cafe43deSMatthew G. Knepley ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr); 402cafe43deSMatthew G. Knepley ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr); 403cafe43deSMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 404cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr); 405ccd2543fSMatthew G Knepley if (cell >= 0) break; 406ccd2543fSMatthew G Knepley } 4074f6087d8SMatthew G. Knepley if (cell < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D not found in mesh", p); 408ccd2543fSMatthew G Knepley cells[p] = cell; 409ccd2543fSMatthew G Knepley } 410cafe43deSMatthew G. Knepley ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr); 411cafe43deSMatthew G. Knepley /* Check for highest numbered proc that claims a point (do we care?) */ 412ccd2543fSMatthew G Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 413ccd2543fSMatthew G Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, cells, PETSC_OWN_POINTER, cellIS);CHKERRQ(ierr); 414ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 415ccd2543fSMatthew G Knepley } 416ccd2543fSMatthew G Knepley 417ccd2543fSMatthew G Knepley #undef __FUNCT__ 41817fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D_Internal" 41917fe8556SMatthew G. Knepley /* 42017fe8556SMatthew G. Knepley DMPlexComputeProjection2Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 2D 42117fe8556SMatthew G. Knepley */ 4223beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D_Internal(PetscScalar coords[], PetscReal R[]) 42317fe8556SMatthew G. Knepley { 42417fe8556SMatthew G. Knepley const PetscReal x = PetscRealPart(coords[2] - coords[0]); 42517fe8556SMatthew G. Knepley const PetscReal y = PetscRealPart(coords[3] - coords[1]); 4268b49ba18SBarry Smith const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r; 42717fe8556SMatthew G. Knepley 42817fe8556SMatthew G. Knepley PetscFunctionBegin; 4291c99cf0cSGeoffrey Irving R[0] = c; R[1] = -s; 4301c99cf0cSGeoffrey Irving R[2] = s; R[3] = c; 43117fe8556SMatthew G. Knepley coords[0] = 0.0; 4327f07f362SMatthew G. Knepley coords[1] = r; 43317fe8556SMatthew G. Knepley PetscFunctionReturn(0); 43417fe8556SMatthew G. Knepley } 43517fe8556SMatthew G. Knepley 43617fe8556SMatthew G. Knepley #undef __FUNCT__ 43728dbe442SToby Isaac #define __FUNCT__ "DMPlexComputeProjection3Dto1D_Internal" 43828dbe442SToby Isaac /* 43928dbe442SToby Isaac DMPlexComputeProjection3Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 3D 44028dbe442SToby Isaac 44128dbe442SToby Isaac This uses the basis completion described by Frisvad, 44228dbe442SToby Isaac 44328dbe442SToby Isaac http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html 44428dbe442SToby Isaac DOI:10.1080/2165347X.2012.689606 44528dbe442SToby Isaac */ 4463beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D_Internal(PetscScalar coords[], PetscReal R[]) 44728dbe442SToby Isaac { 44828dbe442SToby Isaac PetscReal x = PetscRealPart(coords[3] - coords[0]); 44928dbe442SToby Isaac PetscReal y = PetscRealPart(coords[4] - coords[1]); 45028dbe442SToby Isaac PetscReal z = PetscRealPart(coords[5] - coords[2]); 45128dbe442SToby Isaac PetscReal r = PetscSqrtReal(x*x + y*y + z*z); 45228dbe442SToby Isaac PetscReal rinv = 1. / r; 45328dbe442SToby Isaac PetscFunctionBegin; 45428dbe442SToby Isaac 45528dbe442SToby Isaac x *= rinv; y *= rinv; z *= rinv; 45628dbe442SToby Isaac if (x > 0.) { 45728dbe442SToby Isaac PetscReal inv1pX = 1./ (1. + x); 45828dbe442SToby Isaac 45928dbe442SToby Isaac R[0] = x; R[1] = -y; R[2] = -z; 46028dbe442SToby Isaac R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] = -y*z*inv1pX; 46128dbe442SToby Isaac R[6] = z; R[7] = -y*z*inv1pX; R[8] = 1. - z*z*inv1pX; 46228dbe442SToby Isaac } 46328dbe442SToby Isaac else { 46428dbe442SToby Isaac PetscReal inv1mX = 1./ (1. - x); 46528dbe442SToby Isaac 46628dbe442SToby Isaac R[0] = x; R[1] = z; R[2] = y; 46728dbe442SToby Isaac R[3] = y; R[4] = -y*z*inv1mX; R[5] = 1. - y*y*inv1mX; 46828dbe442SToby Isaac R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] = -y*z*inv1mX; 46928dbe442SToby Isaac } 47028dbe442SToby Isaac coords[0] = 0.0; 47128dbe442SToby Isaac coords[1] = r; 47228dbe442SToby Isaac PetscFunctionReturn(0); 47328dbe442SToby Isaac } 47428dbe442SToby Isaac 47528dbe442SToby Isaac #undef __FUNCT__ 476ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D_Internal" 477ccd2543fSMatthew G Knepley /* 478ccd2543fSMatthew G Knepley DMPlexComputeProjection3Dto2D_Internal - Rewrite coordinates to be the 2D projection of the 3D 479ccd2543fSMatthew G Knepley */ 4803beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D_Internal(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) 481ccd2543fSMatthew G Knepley { 4821ee9d5ecSMatthew G. Knepley PetscReal x1[3], x2[3], n[3], norm; 48399dec3a6SMatthew G. Knepley PetscReal x1p[3], x2p[3], xnp[3]; 4844a217a95SMatthew G. Knepley PetscReal sqrtz, alpha; 485ccd2543fSMatthew G Knepley const PetscInt dim = 3; 48699dec3a6SMatthew G. Knepley PetscInt d, e, p; 487ccd2543fSMatthew G Knepley 488ccd2543fSMatthew G Knepley PetscFunctionBegin; 489ccd2543fSMatthew G Knepley /* 0) Calculate normal vector */ 490ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 4911ee9d5ecSMatthew G. Knepley x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]); 4921ee9d5ecSMatthew G. Knepley x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]); 493ccd2543fSMatthew G Knepley } 494ccd2543fSMatthew G Knepley n[0] = x1[1]*x2[2] - x1[2]*x2[1]; 495ccd2543fSMatthew G Knepley n[1] = x1[2]*x2[0] - x1[0]*x2[2]; 496ccd2543fSMatthew G Knepley n[2] = x1[0]*x2[1] - x1[1]*x2[0]; 4978b49ba18SBarry Smith norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]); 498ccd2543fSMatthew G Knepley n[0] /= norm; 499ccd2543fSMatthew G Knepley n[1] /= norm; 500ccd2543fSMatthew G Knepley n[2] /= norm; 501ccd2543fSMatthew G Knepley /* 1) Take the normal vector and rotate until it is \hat z 502ccd2543fSMatthew G Knepley 503ccd2543fSMatthew G Knepley Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then 504ccd2543fSMatthew G Knepley 505ccd2543fSMatthew G Knepley R = / alpha nx nz alpha ny nz -1/alpha \ 506ccd2543fSMatthew G Knepley | -alpha ny alpha nx 0 | 507ccd2543fSMatthew G Knepley \ nx ny nz / 508ccd2543fSMatthew G Knepley 509ccd2543fSMatthew G Knepley will rotate the normal vector to \hat z 510ccd2543fSMatthew G Knepley */ 5118b49ba18SBarry Smith sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]); 51273868372SMatthew G. Knepley /* Check for n = z */ 51373868372SMatthew G. Knepley if (sqrtz < 1.0e-10) { 5141ee9d5ecSMatthew G. Knepley if (n[2] < 0.0) { 51599dec3a6SMatthew G. Knepley if (coordSize > 9) { 51699dec3a6SMatthew G. Knepley coords[2] = PetscRealPart(coords[3*dim+0] - coords[0*dim+0]); 51708b58242SMatthew G. Knepley coords[3] = PetscRealPart(coords[3*dim+1] - coords[0*dim+1]); 51899dec3a6SMatthew G. Knepley coords[4] = x2[0]; 51999dec3a6SMatthew G. Knepley coords[5] = x2[1]; 52099dec3a6SMatthew G. Knepley coords[6] = x1[0]; 52199dec3a6SMatthew G. Knepley coords[7] = x1[1]; 52299dec3a6SMatthew G. Knepley } else { 52373868372SMatthew G. Knepley coords[2] = x2[0]; 52473868372SMatthew G. Knepley coords[3] = x2[1]; 52573868372SMatthew G. Knepley coords[4] = x1[0]; 52673868372SMatthew G. Knepley coords[5] = x1[1]; 52799dec3a6SMatthew G. Knepley } 528b7ad821dSMatthew G. Knepley R[0] = 1.0; R[1] = 0.0; R[2] = 0.0; 529b7ad821dSMatthew G. Knepley R[3] = 0.0; R[4] = 1.0; R[5] = 0.0; 530b7ad821dSMatthew G. Knepley R[6] = 0.0; R[7] = 0.0; R[8] = -1.0; 53173868372SMatthew G. Knepley } else { 53299dec3a6SMatthew G. Knepley for (p = 3; p < coordSize/3; ++p) { 53399dec3a6SMatthew G. Knepley coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]); 53499dec3a6SMatthew G. Knepley coords[p*2+1] = PetscRealPart(coords[p*dim+1] - coords[0*dim+1]); 53599dec3a6SMatthew G. Knepley } 53673868372SMatthew G. Knepley coords[2] = x1[0]; 53773868372SMatthew G. Knepley coords[3] = x1[1]; 53873868372SMatthew G. Knepley coords[4] = x2[0]; 53973868372SMatthew G. Knepley coords[5] = x2[1]; 540b7ad821dSMatthew G. Knepley R[0] = 1.0; R[1] = 0.0; R[2] = 0.0; 541b7ad821dSMatthew G. Knepley R[3] = 0.0; R[4] = 1.0; R[5] = 0.0; 542b7ad821dSMatthew G. Knepley R[6] = 0.0; R[7] = 0.0; R[8] = 1.0; 54373868372SMatthew G. Knepley } 54499dec3a6SMatthew G. Knepley coords[0] = 0.0; 54599dec3a6SMatthew G. Knepley coords[1] = 0.0; 54673868372SMatthew G. Knepley PetscFunctionReturn(0); 54773868372SMatthew G. Knepley } 548da18b5e6SMatthew G Knepley alpha = 1.0/sqrtz; 549ccd2543fSMatthew G Knepley R[0] = alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz; 550ccd2543fSMatthew G Knepley R[3] = -alpha*n[1]; R[4] = alpha*n[0]; R[5] = 0.0; 551ccd2543fSMatthew G Knepley R[6] = n[0]; R[7] = n[1]; R[8] = n[2]; 552ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 553ccd2543fSMatthew G Knepley x1p[d] = 0.0; 554ccd2543fSMatthew G Knepley x2p[d] = 0.0; 555ccd2543fSMatthew G Knepley for (e = 0; e < dim; ++e) { 556ccd2543fSMatthew G Knepley x1p[d] += R[d*dim+e]*x1[e]; 557ccd2543fSMatthew G Knepley x2p[d] += R[d*dim+e]*x2[e]; 558ccd2543fSMatthew G Knepley } 559ccd2543fSMatthew G Knepley } 5608763be8eSMatthew G. Knepley if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 5618763be8eSMatthew G. Knepley if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 562ccd2543fSMatthew G Knepley /* 2) Project to (x, y) */ 56399dec3a6SMatthew G. Knepley for (p = 3; p < coordSize/3; ++p) { 56499dec3a6SMatthew G. Knepley for (d = 0; d < dim; ++d) { 56599dec3a6SMatthew G. Knepley xnp[d] = 0.0; 56699dec3a6SMatthew G. Knepley for (e = 0; e < dim; ++e) { 56799dec3a6SMatthew G. Knepley xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]); 56899dec3a6SMatthew G. Knepley } 56999dec3a6SMatthew G. Knepley if (d < dim-1) coords[p*2+d] = xnp[d]; 57099dec3a6SMatthew G. Knepley } 57199dec3a6SMatthew G. Knepley } 572ccd2543fSMatthew G Knepley coords[0] = 0.0; 573ccd2543fSMatthew G Knepley coords[1] = 0.0; 574ccd2543fSMatthew G Knepley coords[2] = x1p[0]; 575ccd2543fSMatthew G Knepley coords[3] = x1p[1]; 576ccd2543fSMatthew G Knepley coords[4] = x2p[0]; 577ccd2543fSMatthew G Knepley coords[5] = x2p[1]; 5787f07f362SMatthew G. Knepley /* Output R^T which rotates \hat z to the input normal */ 5797f07f362SMatthew G. Knepley for (d = 0; d < dim; ++d) { 5807f07f362SMatthew G. Knepley for (e = d+1; e < dim; ++e) { 5817f07f362SMatthew G. Knepley PetscReal tmp; 5827f07f362SMatthew G. Knepley 5837f07f362SMatthew G. Knepley tmp = R[d*dim+e]; 5847f07f362SMatthew G. Knepley R[d*dim+e] = R[e*dim+d]; 5857f07f362SMatthew G. Knepley R[e*dim+d] = tmp; 5867f07f362SMatthew G. Knepley } 5877f07f362SMatthew G. Knepley } 588ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 589ccd2543fSMatthew G Knepley } 590ccd2543fSMatthew G Knepley 591ccd2543fSMatthew G Knepley #undef __FUNCT__ 592834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal" 5936322fe33SJed Brown PETSC_UNUSED 594834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) 595834e62ceSMatthew G. Knepley { 596834e62ceSMatthew G. Knepley /* Signed volume is 1/2 the determinant 597834e62ceSMatthew G. Knepley 598834e62ceSMatthew G. Knepley | 1 1 1 | 599834e62ceSMatthew G. Knepley | x0 x1 x2 | 600834e62ceSMatthew G. Knepley | y0 y1 y2 | 601834e62ceSMatthew G. Knepley 602834e62ceSMatthew G. Knepley but if x0,y0 is the origin, we have 603834e62ceSMatthew G. Knepley 604834e62ceSMatthew G. Knepley | x1 x2 | 605834e62ceSMatthew G. Knepley | y1 y2 | 606834e62ceSMatthew G. Knepley */ 607834e62ceSMatthew G. Knepley const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1]; 608834e62ceSMatthew G. Knepley const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1]; 609834e62ceSMatthew G. Knepley PetscReal M[4], detM; 610834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; 61186623015SMatthew G. Knepley M[2] = y1; M[3] = y2; 612923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(&detM, M); 613834e62ceSMatthew G. Knepley *vol = 0.5*detM; 614834e62ceSMatthew G. Knepley PetscLogFlops(5.0); 615834e62ceSMatthew G. Knepley } 616834e62ceSMatthew G. Knepley 617834e62ceSMatthew G. Knepley #undef __FUNCT__ 618834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal" 619834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[]) 620834e62ceSMatthew G. Knepley { 621923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(vol, coords); 622834e62ceSMatthew G. Knepley *vol *= 0.5; 623834e62ceSMatthew G. Knepley } 624834e62ceSMatthew G. Knepley 625834e62ceSMatthew G. Knepley #undef __FUNCT__ 626834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal" 6276322fe33SJed Brown PETSC_UNUSED 628834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) 629834e62ceSMatthew G. Knepley { 630834e62ceSMatthew G. Knepley /* Signed volume is 1/6th of the determinant 631834e62ceSMatthew G. Knepley 632834e62ceSMatthew G. Knepley | 1 1 1 1 | 633834e62ceSMatthew G. Knepley | x0 x1 x2 x3 | 634834e62ceSMatthew G. Knepley | y0 y1 y2 y3 | 635834e62ceSMatthew G. Knepley | z0 z1 z2 z3 | 636834e62ceSMatthew G. Knepley 637834e62ceSMatthew G. Knepley but if x0,y0,z0 is the origin, we have 638834e62ceSMatthew G. Knepley 639834e62ceSMatthew G. Knepley | x1 x2 x3 | 640834e62ceSMatthew G. Knepley | y1 y2 y3 | 641834e62ceSMatthew G. Knepley | z1 z2 z3 | 642834e62ceSMatthew G. Knepley */ 643834e62ceSMatthew G. Knepley const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2]; 644834e62ceSMatthew G. Knepley const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2]; 645834e62ceSMatthew G. Knepley const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2]; 646834e62ceSMatthew G. Knepley PetscReal M[9], detM; 647834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; M[2] = x3; 648834e62ceSMatthew G. Knepley M[3] = y1; M[4] = y2; M[5] = y3; 649834e62ceSMatthew G. Knepley M[6] = z1; M[7] = z2; M[8] = z3; 650923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(&detM, M); 651b7ad821dSMatthew G. Knepley *vol = -0.16666666666666666666666*detM; 652834e62ceSMatthew G. Knepley PetscLogFlops(10.0); 653834e62ceSMatthew G. Knepley } 654834e62ceSMatthew G. Knepley 655834e62ceSMatthew G. Knepley #undef __FUNCT__ 6560ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal" 6570ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) 6580ec8681fSMatthew G. Knepley { 659923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(vol, coords); 660b7ad821dSMatthew G. Knepley *vol *= -0.16666666666666666666666; 6610ec8681fSMatthew G. Knepley } 6620ec8681fSMatthew G. Knepley 6630ec8681fSMatthew G. Knepley #undef __FUNCT__ 66417fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal" 66517fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 66617fe8556SMatthew G. Knepley { 66717fe8556SMatthew G. Knepley PetscSection coordSection; 66817fe8556SMatthew G. Knepley Vec coordinates; 669a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 6707f07f362SMatthew G. Knepley PetscInt numCoords, d; 67117fe8556SMatthew G. Knepley PetscErrorCode ierr; 67217fe8556SMatthew G. Knepley 67317fe8556SMatthew G. Knepley PetscFunctionBegin; 67417fe8556SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 67569d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 67617fe8556SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 6777f07f362SMatthew G. Knepley *detJ = 0.0; 67828dbe442SToby Isaac if (numCoords == 6) { 67928dbe442SToby Isaac const PetscInt dim = 3; 68028dbe442SToby Isaac PetscReal R[9], J0; 68128dbe442SToby Isaac 68228dbe442SToby Isaac if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 68328dbe442SToby Isaac ierr = DMPlexComputeProjection3Dto1D_Internal(coords, R);CHKERRQ(ierr); 68428dbe442SToby Isaac if (J) { 68528dbe442SToby Isaac J0 = 0.5*PetscRealPart(coords[1]); 68628dbe442SToby Isaac J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2]; 68728dbe442SToby Isaac J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5]; 68828dbe442SToby Isaac J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8]; 68928dbe442SToby Isaac DMPlex_Det3D_Internal(detJ, J); 69028dbe442SToby Isaac } 69128dbe442SToby Isaac if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 69228dbe442SToby Isaac } else if (numCoords == 4) { 6937f07f362SMatthew G. Knepley const PetscInt dim = 2; 6947f07f362SMatthew G. Knepley PetscReal R[4], J0; 6957f07f362SMatthew G. Knepley 6967f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 6977f07f362SMatthew G. Knepley ierr = DMPlexComputeProjection2Dto1D_Internal(coords, R);CHKERRQ(ierr); 69817fe8556SMatthew G. Knepley if (J) { 6997f07f362SMatthew G. Knepley J0 = 0.5*PetscRealPart(coords[1]); 7007f07f362SMatthew G. Knepley J[0] = R[0]*J0; J[1] = R[1]; 7017f07f362SMatthew G. Knepley J[2] = R[2]*J0; J[3] = R[3]; 702923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 70317fe8556SMatthew G. Knepley } 704923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 7057f07f362SMatthew G. Knepley } else if (numCoords == 2) { 7067f07f362SMatthew G. Knepley const PetscInt dim = 1; 7077f07f362SMatthew G. Knepley 7087f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 7097f07f362SMatthew G. Knepley if (J) { 7107f07f362SMatthew G. Knepley J[0] = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0])); 71117fe8556SMatthew G. Knepley *detJ = J[0]; 71217fe8556SMatthew G. Knepley PetscLogFlops(2.0); 71317fe8556SMatthew G. Knepley } 7147f07f362SMatthew G. Knepley if (invJ) {invJ[0] = 1.0/J[0]; PetscLogFlops(1.0);} 715796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords); 71617fe8556SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 71717fe8556SMatthew G. Knepley PetscFunctionReturn(0); 71817fe8556SMatthew G. Knepley } 71917fe8556SMatthew G. Knepley 72017fe8556SMatthew G. Knepley #undef __FUNCT__ 721ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal" 722ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 723ccd2543fSMatthew G Knepley { 724ccd2543fSMatthew G Knepley PetscSection coordSection; 725ccd2543fSMatthew G Knepley Vec coordinates; 726a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 7277f07f362SMatthew G. Knepley PetscInt numCoords, d, f, g; 728ccd2543fSMatthew G Knepley PetscErrorCode ierr; 729ccd2543fSMatthew G Knepley 730ccd2543fSMatthew G Knepley PetscFunctionBegin; 731ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 73269d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 733ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 7347f07f362SMatthew G. Knepley *detJ = 0.0; 735ccd2543fSMatthew G Knepley if (numCoords == 9) { 7367f07f362SMatthew G. Knepley const PetscInt dim = 3; 7377f07f362SMatthew G. Knepley PetscReal R[9], J0[9] = {1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0}; 7387f07f362SMatthew G. Knepley 7397f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 74099dec3a6SMatthew G. Knepley ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr); 7417f07f362SMatthew G. Knepley if (J) { 742b7ad821dSMatthew G. Knepley const PetscInt pdim = 2; 743b7ad821dSMatthew G. Knepley 744b7ad821dSMatthew G. Knepley for (d = 0; d < pdim; d++) { 745b7ad821dSMatthew G. Knepley for (f = 0; f < pdim; f++) { 746b7ad821dSMatthew G. Knepley J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 747ccd2543fSMatthew G Knepley } 7487f07f362SMatthew G. Knepley } 7497f07f362SMatthew G. Knepley PetscLogFlops(8.0); 750923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 7517f07f362SMatthew G. Knepley for (d = 0; d < dim; d++) { 7527f07f362SMatthew G. Knepley for (f = 0; f < dim; f++) { 7537f07f362SMatthew G. Knepley J[d*dim+f] = 0.0; 7547f07f362SMatthew G. Knepley for (g = 0; g < dim; g++) { 7557f07f362SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 7567f07f362SMatthew G. Knepley } 7577f07f362SMatthew G. Knepley } 7587f07f362SMatthew G. Knepley } 7597f07f362SMatthew G. Knepley PetscLogFlops(18.0); 7607f07f362SMatthew G. Knepley } 761923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 7627f07f362SMatthew G. Knepley } else if (numCoords == 6) { 7637f07f362SMatthew G. Knepley const PetscInt dim = 2; 7647f07f362SMatthew G. Knepley 7657f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 766ccd2543fSMatthew G Knepley if (J) { 767ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 768ccd2543fSMatthew G Knepley for (f = 0; f < dim; f++) { 769ccd2543fSMatthew G Knepley J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d])); 770ccd2543fSMatthew G Knepley } 771ccd2543fSMatthew G Knepley } 7727f07f362SMatthew G. Knepley PetscLogFlops(8.0); 773923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 774ccd2543fSMatthew G Knepley } 775923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 776796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords); 777ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 778ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 779ccd2543fSMatthew G Knepley } 780ccd2543fSMatthew G Knepley 781ccd2543fSMatthew G Knepley #undef __FUNCT__ 782ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal" 783ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 784ccd2543fSMatthew G Knepley { 785ccd2543fSMatthew G Knepley PetscSection coordSection; 786ccd2543fSMatthew G Knepley Vec coordinates; 787a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 78899dec3a6SMatthew G. Knepley PetscInt numCoords, d, f, g; 789ccd2543fSMatthew G Knepley PetscErrorCode ierr; 790ccd2543fSMatthew G Knepley 791ccd2543fSMatthew G Knepley PetscFunctionBegin; 792ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 79369d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 79499dec3a6SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 7957f07f362SMatthew G. Knepley *detJ = 0.0; 79699dec3a6SMatthew G. Knepley if (numCoords == 12) { 79799dec3a6SMatthew G. Knepley const PetscInt dim = 3; 79899dec3a6SMatthew G. Knepley PetscReal R[9], J0[9] = {1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0}; 79999dec3a6SMatthew G. Knepley 80099dec3a6SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 80199dec3a6SMatthew G. Knepley ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr); 80299dec3a6SMatthew G. Knepley if (J) { 80399dec3a6SMatthew G. Knepley const PetscInt pdim = 2; 80499dec3a6SMatthew G. Knepley 80599dec3a6SMatthew G. Knepley for (d = 0; d < pdim; d++) { 80699dec3a6SMatthew G. Knepley J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 80799dec3a6SMatthew G. Knepley J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 80899dec3a6SMatthew G. Knepley } 80999dec3a6SMatthew G. Knepley PetscLogFlops(8.0); 810923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 81199dec3a6SMatthew G. Knepley for (d = 0; d < dim; d++) { 81299dec3a6SMatthew G. Knepley for (f = 0; f < dim; f++) { 81399dec3a6SMatthew G. Knepley J[d*dim+f] = 0.0; 81499dec3a6SMatthew G. Knepley for (g = 0; g < dim; g++) { 81599dec3a6SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 81699dec3a6SMatthew G. Knepley } 81799dec3a6SMatthew G. Knepley } 81899dec3a6SMatthew G. Knepley } 81999dec3a6SMatthew G. Knepley PetscLogFlops(18.0); 82099dec3a6SMatthew G. Knepley } 821923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 82297f1a218SMatthew G. Knepley } else if ((numCoords == 8) || (numCoords == 16)) { 82399dec3a6SMatthew G. Knepley const PetscInt dim = 2; 82499dec3a6SMatthew G. Knepley 8257f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 826ccd2543fSMatthew G Knepley if (J) { 827ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 82899dec3a6SMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 82999dec3a6SMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 830ccd2543fSMatthew G Knepley } 8317f07f362SMatthew G. Knepley PetscLogFlops(8.0); 832923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 833ccd2543fSMatthew G Knepley } 834923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 835796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords); 83699dec3a6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 837ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 838ccd2543fSMatthew G Knepley } 839ccd2543fSMatthew G Knepley 840ccd2543fSMatthew G Knepley #undef __FUNCT__ 841ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal" 842ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 843ccd2543fSMatthew G Knepley { 844ccd2543fSMatthew G Knepley PetscSection coordSection; 845ccd2543fSMatthew G Knepley Vec coordinates; 846a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 847ccd2543fSMatthew G Knepley const PetscInt dim = 3; 84899dec3a6SMatthew G. Knepley PetscInt d; 849ccd2543fSMatthew G Knepley PetscErrorCode ierr; 850ccd2543fSMatthew G Knepley 851ccd2543fSMatthew G Knepley PetscFunctionBegin; 852ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 85369d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 854ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 8557f07f362SMatthew G. Knepley *detJ = 0.0; 8567f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 857ccd2543fSMatthew G Knepley if (J) { 858ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 859f0df753eSMatthew G. Knepley /* I orient with outward face normals */ 860f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d])); 861f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 862f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 863ccd2543fSMatthew G Knepley } 8647f07f362SMatthew G. Knepley PetscLogFlops(18.0); 865923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 866ccd2543fSMatthew G Knepley } 867923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 868ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 869ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 870ccd2543fSMatthew G Knepley } 871ccd2543fSMatthew G Knepley 872ccd2543fSMatthew G Knepley #undef __FUNCT__ 873ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal" 874ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 875ccd2543fSMatthew G Knepley { 876ccd2543fSMatthew G Knepley PetscSection coordSection; 877ccd2543fSMatthew G Knepley Vec coordinates; 878a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 879ccd2543fSMatthew G Knepley const PetscInt dim = 3; 880ccd2543fSMatthew G Knepley PetscInt d; 881ccd2543fSMatthew G Knepley PetscErrorCode ierr; 882ccd2543fSMatthew G Knepley 883ccd2543fSMatthew G Knepley PetscFunctionBegin; 884ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 88569d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 886ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 8877f07f362SMatthew G. Knepley *detJ = 0.0; 8887f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 889ccd2543fSMatthew G Knepley if (J) { 890ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 891f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 892f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 893f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d])); 894ccd2543fSMatthew G Knepley } 8957f07f362SMatthew G. Knepley PetscLogFlops(18.0); 896923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 897ccd2543fSMatthew G Knepley } 898923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 899ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 900ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 901ccd2543fSMatthew G Knepley } 902ccd2543fSMatthew G Knepley 903ccd2543fSMatthew G Knepley #undef __FUNCT__ 9048e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM" 905ccd2543fSMatthew G Knepley /*@C 9068e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell 907ccd2543fSMatthew G Knepley 908ccd2543fSMatthew G Knepley Collective on DM 909ccd2543fSMatthew G Knepley 910ccd2543fSMatthew G Knepley Input Arguments: 911ccd2543fSMatthew G Knepley + dm - the DM 912ccd2543fSMatthew G Knepley - cell - the cell 913ccd2543fSMatthew G Knepley 914ccd2543fSMatthew G Knepley Output Arguments: 915ccd2543fSMatthew G Knepley + v0 - the translation part of this affine transform 916ccd2543fSMatthew G Knepley . J - the Jacobian of the transform from the reference element 917ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian 918ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant 919ccd2543fSMatthew G Knepley 920ccd2543fSMatthew G Knepley Level: advanced 921ccd2543fSMatthew G Knepley 922ccd2543fSMatthew G Knepley Fortran Notes: 923ccd2543fSMatthew G Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 924ccd2543fSMatthew G Knepley include petsc.h90 in your code. 925ccd2543fSMatthew G Knepley 9268e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec() 927ccd2543fSMatthew G Knepley @*/ 9288e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 929ccd2543fSMatthew G Knepley { 93049dc4407SMatthew G. Knepley PetscInt depth, dim, coneSize; 931ccd2543fSMatthew G Knepley PetscErrorCode ierr; 932ccd2543fSMatthew G Knepley 933ccd2543fSMatthew G Knepley PetscFunctionBegin; 934139a35ccSMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 935ccd2543fSMatthew G Knepley ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 93649dc4407SMatthew G. Knepley if (depth == 1) { 9378e0841e0SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 9388e0841e0SMatthew G. Knepley } else { 9398e0841e0SMatthew G. Knepley DMLabel depth; 9408e0841e0SMatthew G. Knepley 9418e0841e0SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 9428e0841e0SMatthew G. Knepley ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr); 9438e0841e0SMatthew G. Knepley } 944ccd2543fSMatthew G Knepley switch (dim) { 94517fe8556SMatthew G. Knepley case 1: 94617fe8556SMatthew G. Knepley ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 94717fe8556SMatthew G. Knepley break; 948ccd2543fSMatthew G Knepley case 2: 949ccd2543fSMatthew G Knepley switch (coneSize) { 950ccd2543fSMatthew G Knepley case 3: 951ccd2543fSMatthew G Knepley ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 952ccd2543fSMatthew G Knepley break; 953ccd2543fSMatthew G Knepley case 4: 954ccd2543fSMatthew G Knepley ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 955ccd2543fSMatthew G Knepley break; 956ccd2543fSMatthew G Knepley default: 9578e0841e0SMatthew G. Knepley SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 958ccd2543fSMatthew G Knepley } 959ccd2543fSMatthew G Knepley break; 960ccd2543fSMatthew G Knepley case 3: 961ccd2543fSMatthew G Knepley switch (coneSize) { 962ccd2543fSMatthew G Knepley case 4: 963ccd2543fSMatthew G Knepley ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 964ccd2543fSMatthew G Knepley break; 9658e0841e0SMatthew G. Knepley case 6: /* Faces */ 9668e0841e0SMatthew G. Knepley case 8: /* Vertices */ 967ccd2543fSMatthew G Knepley ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 968ccd2543fSMatthew G Knepley break; 969ccd2543fSMatthew G Knepley default: 9708e0841e0SMatthew G. Knepley SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 971ccd2543fSMatthew G Knepley } 972ccd2543fSMatthew G Knepley break; 973ccd2543fSMatthew G Knepley default: 974ccd2543fSMatthew G Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 975ccd2543fSMatthew G Knepley } 9768e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 9778e0841e0SMatthew G. Knepley } 9788e0841e0SMatthew G. Knepley 9798e0841e0SMatthew G. Knepley #undef __FUNCT__ 9808e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal" 9818e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 9828e0841e0SMatthew G. Knepley { 9838e0841e0SMatthew G. Knepley PetscQuadrature quad; 9848e0841e0SMatthew G. Knepley PetscSection coordSection; 9858e0841e0SMatthew G. Knepley Vec coordinates; 9868e0841e0SMatthew G. Knepley PetscScalar *coords = NULL; 9878e0841e0SMatthew G. Knepley const PetscReal *quadPoints; 9888e0841e0SMatthew G. Knepley PetscReal *basisDer; 9898e0841e0SMatthew G. Knepley PetscInt dim, cdim, pdim, qdim, Nq, numCoords, d, q; 9908e0841e0SMatthew G. Knepley PetscErrorCode ierr; 9918e0841e0SMatthew G. Knepley 9928e0841e0SMatthew G. Knepley PetscFunctionBegin; 9938e0841e0SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 9948e0841e0SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 9958e0841e0SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 9968e0841e0SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 9978e0841e0SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr); 9988e0841e0SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 999954b1791SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr); 10008e0841e0SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr); 10018e0841e0SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 10028e0841e0SMatthew G. Knepley *detJ = 0.0; 10038e0841e0SMatthew G. Knepley if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim); 10048e0841e0SMatthew G. Knepley if (numCoords != pdim*cdim) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "There are %d coordinates for point %d != %d*%d", numCoords, point, pdim, cdim); 10058e0841e0SMatthew G. Knepley if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);} 10068e0841e0SMatthew G. Knepley if (J) { 10078e0841e0SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 10088e0841e0SMatthew G. Knepley PetscInt i, j, k, c, r; 10098e0841e0SMatthew G. Knepley 10108e0841e0SMatthew G. Knepley /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */ 10118e0841e0SMatthew G. Knepley for (k = 0; k < pdim; ++k) 10128e0841e0SMatthew G. Knepley for (j = 0; j < dim; ++j) 10138e0841e0SMatthew G. Knepley for (i = 0; i < cdim; ++i) 101471d6e60fSMatthew G. Knepley J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]); 10158e0841e0SMatthew G. Knepley PetscLogFlops(2.0*pdim*dim*cdim); 10168e0841e0SMatthew G. Knepley if (cdim > dim) { 10178e0841e0SMatthew G. Knepley for (c = dim; c < cdim; ++c) 10188e0841e0SMatthew G. Knepley for (r = 0; r < cdim; ++r) 10198e0841e0SMatthew G. Knepley J[r*cdim+c] = r == c ? 1.0 : 0.0; 10208e0841e0SMatthew G. Knepley } 10218e0841e0SMatthew G. Knepley switch (cdim) { 10228e0841e0SMatthew G. Knepley case 3: 10238e0841e0SMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 10248e0841e0SMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 102517fe8556SMatthew G. Knepley break; 102649dc4407SMatthew G. Knepley case 2: 10278e0841e0SMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 10288e0841e0SMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 102949dc4407SMatthew G. Knepley break; 10308e0841e0SMatthew G. Knepley case 1: 10318e0841e0SMatthew G. Knepley *detJ = J[0]; 10328e0841e0SMatthew G. Knepley if (invJ) invJ[0] = 1.0/J[0]; 103349dc4407SMatthew G. Knepley } 103449dc4407SMatthew G. Knepley } 10358e0841e0SMatthew G. Knepley } 10368e0841e0SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 10378e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 10388e0841e0SMatthew G. Knepley } 10398e0841e0SMatthew G. Knepley 10408e0841e0SMatthew G. Knepley #undef __FUNCT__ 10418e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM" 10428e0841e0SMatthew G. Knepley /*@C 10438e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell 10448e0841e0SMatthew G. Knepley 10458e0841e0SMatthew G. Knepley Collective on DM 10468e0841e0SMatthew G. Knepley 10478e0841e0SMatthew G. Knepley Input Arguments: 10488e0841e0SMatthew G. Knepley + dm - the DM 10498e0841e0SMatthew G. Knepley . cell - the cell 10508e0841e0SMatthew G. Knepley - fe - the finite element containing the quadrature 10518e0841e0SMatthew G. Knepley 10528e0841e0SMatthew G. Knepley Output Arguments: 10538e0841e0SMatthew G. Knepley + v0 - the translation part of this transform 10548e0841e0SMatthew G. Knepley . J - the Jacobian of the transform from the reference element at each quadrature point 10558e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point 10568e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point 10578e0841e0SMatthew G. Knepley 10588e0841e0SMatthew G. Knepley Level: advanced 10598e0841e0SMatthew G. Knepley 10608e0841e0SMatthew G. Knepley Fortran Notes: 10618e0841e0SMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 10628e0841e0SMatthew G. Knepley include petsc.h90 in your code. 10638e0841e0SMatthew G. Knepley 10648e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 10658e0841e0SMatthew G. Knepley @*/ 10668e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 10678e0841e0SMatthew G. Knepley { 10688e0841e0SMatthew G. Knepley PetscErrorCode ierr; 10698e0841e0SMatthew G. Knepley 10708e0841e0SMatthew G. Knepley PetscFunctionBegin; 10718e0841e0SMatthew G. Knepley if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);} 10728e0841e0SMatthew G. Knepley else {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);} 1073ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1074ccd2543fSMatthew G Knepley } 1075834e62ceSMatthew G. Knepley 1076834e62ceSMatthew G. Knepley #undef __FUNCT__ 1077cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal" 1078011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1079cc08537eSMatthew G. Knepley { 1080cc08537eSMatthew G. Knepley PetscSection coordSection; 1081cc08537eSMatthew G. Knepley Vec coordinates; 1082a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 108306e2781eSMatthew G. Knepley PetscScalar tmp[2]; 1084cc08537eSMatthew G. Knepley PetscInt coordSize; 1085cc08537eSMatthew G. Knepley PetscErrorCode ierr; 1086cc08537eSMatthew G. Knepley 1087cc08537eSMatthew G. Knepley PetscFunctionBegin; 1088cc08537eSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 108969d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1090cc08537eSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1091011ea5d8SMatthew G. Knepley if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now"); 109206e2781eSMatthew G. Knepley ierr = DMPlexLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr); 1093cc08537eSMatthew G. Knepley if (centroid) { 109406e2781eSMatthew G. Knepley centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]); 109506e2781eSMatthew G. Knepley centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]); 1096cc08537eSMatthew G. Knepley } 1097cc08537eSMatthew G. Knepley if (normal) { 1098a60a936bSMatthew G. Knepley PetscReal norm; 1099a60a936bSMatthew G. Knepley 110006e2781eSMatthew G. Knepley normal[0] = -PetscRealPart(coords[1] - tmp[1]); 110106e2781eSMatthew G. Knepley normal[1] = PetscRealPart(coords[0] - tmp[0]); 1102a60a936bSMatthew G. Knepley norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]); 1103a60a936bSMatthew G. Knepley normal[0] /= norm; 1104a60a936bSMatthew G. Knepley normal[1] /= norm; 1105cc08537eSMatthew G. Knepley } 1106cc08537eSMatthew G. Knepley if (vol) { 110706e2781eSMatthew G. Knepley *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1]))); 1108cc08537eSMatthew G. Knepley } 1109cc08537eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1110cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 1111cc08537eSMatthew G. Knepley } 1112cc08537eSMatthew G. Knepley 1113cc08537eSMatthew G. Knepley #undef __FUNCT__ 1114cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal" 1115cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */ 1116011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1117cc08537eSMatthew G. Knepley { 1118cc08537eSMatthew G. Knepley PetscSection coordSection; 1119cc08537eSMatthew G. Knepley Vec coordinates; 1120cc08537eSMatthew G. Knepley PetscScalar *coords = NULL; 11210a1d6728SMatthew G. Knepley PetscReal vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9]; 11220a1d6728SMatthew G. Knepley PetscInt tdim = 2, coordSize, numCorners, p, d, e; 1123cc08537eSMatthew G. Knepley PetscErrorCode ierr; 1124cc08537eSMatthew G. Knepley 1125cc08537eSMatthew G. Knepley PetscFunctionBegin; 1126cc08537eSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 11270a1d6728SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr); 112869d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1129cc08537eSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 11300bce18caSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 1131011ea5d8SMatthew G. Knepley if (normal) { 1132011ea5d8SMatthew G. Knepley if (dim > 2) { 11331ee9d5ecSMatthew G. Knepley const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]); 11341ee9d5ecSMatthew G. Knepley const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]); 11351ee9d5ecSMatthew G. Knepley const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]); 11360a1d6728SMatthew G. Knepley PetscReal norm; 11370a1d6728SMatthew G. Knepley 11381ee9d5ecSMatthew G. Knepley v0[0] = PetscRealPart(coords[0]); 11391ee9d5ecSMatthew G. Knepley v0[1] = PetscRealPart(coords[1]); 11401ee9d5ecSMatthew G. Knepley v0[2] = PetscRealPart(coords[2]); 11410a1d6728SMatthew G. Knepley normal[0] = y0*z1 - z0*y1; 11420a1d6728SMatthew G. Knepley normal[1] = z0*x1 - x0*z1; 11430a1d6728SMatthew G. Knepley normal[2] = x0*y1 - y0*x1; 11448b49ba18SBarry Smith norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]); 11450a1d6728SMatthew G. Knepley normal[0] /= norm; 11460a1d6728SMatthew G. Knepley normal[1] /= norm; 11470a1d6728SMatthew G. Knepley normal[2] /= norm; 1148011ea5d8SMatthew G. Knepley } else { 1149011ea5d8SMatthew G. Knepley for (d = 0; d < dim; ++d) normal[d] = 0.0; 1150011ea5d8SMatthew G. Knepley } 1151011ea5d8SMatthew G. Knepley } 115299dec3a6SMatthew G. Knepley if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D_Internal(coordSize, coords, R);CHKERRQ(ierr);} 11530a1d6728SMatthew G. Knepley for (p = 0; p < numCorners; ++p) { 11540a1d6728SMatthew G. Knepley /* Need to do this copy to get types right */ 11550a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 11561ee9d5ecSMatthew G. Knepley ctmp[d] = PetscRealPart(coords[p*tdim+d]); 11571ee9d5ecSMatthew G. Knepley ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]); 11580a1d6728SMatthew G. Knepley } 11590a1d6728SMatthew G. Knepley Volume_Triangle_Origin_Internal(&vtmp, ctmp); 11600a1d6728SMatthew G. Knepley vsum += vtmp; 11610a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 11620a1d6728SMatthew G. Knepley csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp; 11630a1d6728SMatthew G. Knepley } 11640a1d6728SMatthew G. Knepley } 11650a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 11660a1d6728SMatthew G. Knepley csum[d] /= (tdim+1)*vsum; 11670a1d6728SMatthew G. Knepley } 11680a1d6728SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1169ee6bbdb2SSatish Balay if (vol) *vol = PetscAbsReal(vsum); 11700a1d6728SMatthew G. Knepley if (centroid) { 11710a1d6728SMatthew G. Knepley if (dim > 2) { 11720a1d6728SMatthew G. Knepley for (d = 0; d < dim; ++d) { 11730a1d6728SMatthew G. Knepley centroid[d] = v0[d]; 11740a1d6728SMatthew G. Knepley for (e = 0; e < dim; ++e) { 11750a1d6728SMatthew G. Knepley centroid[d] += R[d*dim+e]*csum[e]; 11760a1d6728SMatthew G. Knepley } 11770a1d6728SMatthew G. Knepley } 11780a1d6728SMatthew G. Knepley } else for (d = 0; d < dim; ++d) centroid[d] = csum[d]; 11790a1d6728SMatthew G. Knepley } 1180cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 1181cc08537eSMatthew G. Knepley } 1182cc08537eSMatthew G. Knepley 1183cc08537eSMatthew G. Knepley #undef __FUNCT__ 11840ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal" 11850ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */ 1186011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 11870ec8681fSMatthew G. Knepley { 11880ec8681fSMatthew G. Knepley PetscSection coordSection; 11890ec8681fSMatthew G. Knepley Vec coordinates; 11900ec8681fSMatthew G. Knepley PetscScalar *coords = NULL; 119186623015SMatthew G. Knepley PetscReal vsum = 0.0, vtmp, coordsTmp[3*3]; 1192a7df9edeSMatthew G. Knepley const PetscInt *faces, *facesO; 11930ec8681fSMatthew G. Knepley PetscInt numFaces, f, coordSize, numCorners, p, d; 11940ec8681fSMatthew G. Knepley PetscErrorCode ierr; 11950ec8681fSMatthew G. Knepley 11960ec8681fSMatthew G. Knepley PetscFunctionBegin; 1197f6dae198SJed Brown if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim); 11980ec8681fSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 119969d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 12000ec8681fSMatthew G. Knepley 1201d9a81ebdSMatthew G. Knepley if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0; 12020ec8681fSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr); 12030ec8681fSMatthew G. Knepley ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr); 1204a7df9edeSMatthew G. Knepley ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr); 12050ec8681fSMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 1206011ea5d8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 12070ec8681fSMatthew G. Knepley numCorners = coordSize/dim; 12080ec8681fSMatthew G. Knepley switch (numCorners) { 12090ec8681fSMatthew G. Knepley case 3: 12100ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12111ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 12121ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 12131ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]); 12140ec8681fSMatthew G. Knepley } 12150ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1216a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 12170ec8681fSMatthew G. Knepley vsum += vtmp; 12184f25033aSJed Brown if (centroid) { /* Centroid of OABC = (a+b+c)/4 */ 12190ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12201ee9d5ecSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 12210ec8681fSMatthew G. Knepley } 12220ec8681fSMatthew G. Knepley } 12230ec8681fSMatthew G. Knepley break; 12240ec8681fSMatthew G. Knepley case 4: 12250ec8681fSMatthew G. Knepley /* DO FOR PYRAMID */ 12260ec8681fSMatthew G. Knepley /* First tet */ 12270ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12281ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 12291ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 12301ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 12310ec8681fSMatthew G. Knepley } 12320ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1233a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 12340ec8681fSMatthew G. Knepley vsum += vtmp; 12350ec8681fSMatthew G. Knepley if (centroid) { 12360ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12370ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 12380ec8681fSMatthew G. Knepley } 12390ec8681fSMatthew G. Knepley } 12400ec8681fSMatthew G. Knepley /* Second tet */ 12410ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12421ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]); 12431ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]); 12441ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 12450ec8681fSMatthew G. Knepley } 12460ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1247a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 12480ec8681fSMatthew G. Knepley vsum += vtmp; 12490ec8681fSMatthew G. Knepley if (centroid) { 12500ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12510ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 12520ec8681fSMatthew G. Knepley } 12530ec8681fSMatthew G. Knepley } 12540ec8681fSMatthew G. Knepley break; 12550ec8681fSMatthew G. Knepley default: 1256796f034aSJed Brown SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners); 12570ec8681fSMatthew G. Knepley } 12584f25033aSJed Brown ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 12590ec8681fSMatthew G. Knepley } 12608763be8eSMatthew G. Knepley if (vol) *vol = PetscAbsReal(vsum); 12610ec8681fSMatthew G. Knepley if (normal) for (d = 0; d < dim; ++d) normal[d] = 0.0; 1262d9a81ebdSMatthew G. Knepley if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4); 12630ec8681fSMatthew G. Knepley PetscFunctionReturn(0); 12640ec8681fSMatthew G. Knepley } 12650ec8681fSMatthew G. Knepley 12660ec8681fSMatthew G. Knepley #undef __FUNCT__ 1267834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM" 1268834e62ceSMatthew G. Knepley /*@C 1269834e62ceSMatthew G. Knepley DMPlexComputeCellGeometryFVM - Compute the volume for a given cell 1270834e62ceSMatthew G. Knepley 1271834e62ceSMatthew G. Knepley Collective on DM 1272834e62ceSMatthew G. Knepley 1273834e62ceSMatthew G. Knepley Input Arguments: 1274834e62ceSMatthew G. Knepley + dm - the DM 1275834e62ceSMatthew G. Knepley - cell - the cell 1276834e62ceSMatthew G. Knepley 1277834e62ceSMatthew G. Knepley Output Arguments: 1278834e62ceSMatthew G. Knepley + volume - the cell volume 1279cc08537eSMatthew G. Knepley . centroid - the cell centroid 1280cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate 1281834e62ceSMatthew G. Knepley 1282834e62ceSMatthew G. Knepley Level: advanced 1283834e62ceSMatthew G. Knepley 1284834e62ceSMatthew G. Knepley Fortran Notes: 1285834e62ceSMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 1286834e62ceSMatthew G. Knepley include petsc.h90 in your code. 1287834e62ceSMatthew G. Knepley 128869d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 1289834e62ceSMatthew G. Knepley @*/ 1290cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1291834e62ceSMatthew G. Knepley { 12920ec8681fSMatthew G. Knepley PetscInt depth, dim; 1293834e62ceSMatthew G. Knepley PetscErrorCode ierr; 1294834e62ceSMatthew G. Knepley 1295834e62ceSMatthew G. Knepley PetscFunctionBegin; 1296834e62ceSMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 1297c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1298834e62ceSMatthew G. Knepley if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated"); 1299834e62ceSMatthew G. Knepley /* We need to keep a pointer to the depth label */ 1300011ea5d8SMatthew G. Knepley ierr = DMPlexGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr); 1301834e62ceSMatthew G. Knepley /* Cone size is now the number of faces */ 1302011ea5d8SMatthew G. Knepley switch (depth) { 1303cc08537eSMatthew G. Knepley case 1: 1304011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1305cc08537eSMatthew G. Knepley break; 1306834e62ceSMatthew G. Knepley case 2: 1307011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1308834e62ceSMatthew G. Knepley break; 1309834e62ceSMatthew G. Knepley case 3: 1310011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1311834e62ceSMatthew G. Knepley break; 1312834e62ceSMatthew G. Knepley default: 1313834e62ceSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 1314834e62ceSMatthew G. Knepley } 1315834e62ceSMatthew G. Knepley PetscFunctionReturn(0); 1316834e62ceSMatthew G. Knepley } 1317113c68e6SMatthew G. Knepley 1318113c68e6SMatthew G. Knepley #undef __FUNCT__ 1319c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM" 1320c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */ 1321c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom) 1322c0d900a5SMatthew G. Knepley { 1323c0d900a5SMatthew G. Knepley DM dmCell; 1324c0d900a5SMatthew G. Knepley Vec coordinates; 1325c0d900a5SMatthew G. Knepley PetscSection coordSection, sectionCell; 1326c0d900a5SMatthew G. Knepley PetscScalar *cgeom; 1327c0d900a5SMatthew G. Knepley PetscInt cStart, cEnd, cMax, c; 1328c0d900a5SMatthew G. Knepley PetscErrorCode ierr; 1329c0d900a5SMatthew G. Knepley 1330c0d900a5SMatthew G. Knepley PetscFunctionBegin; 1331c0d900a5SMatthew G. Knepley ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1332c0d900a5SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1333c0d900a5SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1334c0d900a5SMatthew G. Knepley ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1335c0d900a5SMatthew G. Knepley ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1336c0d900a5SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1337c0d900a5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1338c0d900a5SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 1339c0d900a5SMatthew G. Knepley cEnd = cMax < 0 ? cEnd : cMax; 1340c0d900a5SMatthew G. Knepley ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 1341c0d900a5SMatthew G. Knepley /* TODO This needs to be multiplied by Nq for non-affine */ 13429e5edeeeSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1343c0d900a5SMatthew G. Knepley ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1344c0d900a5SMatthew G. Knepley ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1345c0d900a5SMatthew G. Knepley ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1346c0d900a5SMatthew G. Knepley ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 1347c0d900a5SMatthew G. Knepley ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1348c0d900a5SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1349c0d900a5SMatthew G. Knepley PetscFECellGeom *cg; 1350c0d900a5SMatthew G. Knepley 1351c0d900a5SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1352c0d900a5SMatthew G. Knepley ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1353c0d900a5SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr); 1354c0d900a5SMatthew G. Knepley if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c); 1355c0d900a5SMatthew G. Knepley } 1356c0d900a5SMatthew G. Knepley ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1357c0d900a5SMatthew G. Knepley ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 1358c0d900a5SMatthew G. Knepley PetscFunctionReturn(0); 1359c0d900a5SMatthew G. Knepley } 1360c0d900a5SMatthew G. Knepley 1361c0d900a5SMatthew G. Knepley #undef __FUNCT__ 1362113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM" 1363891a9168SMatthew G. Knepley /*@ 1364891a9168SMatthew G. Knepley DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method 1365891a9168SMatthew G. Knepley 1366891a9168SMatthew G. Knepley Input Parameter: 1367891a9168SMatthew G. Knepley . dm - The DM 1368891a9168SMatthew G. Knepley 1369891a9168SMatthew G. Knepley Output Parameters: 1370891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data 1371891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data 1372891a9168SMatthew G. Knepley 1373891a9168SMatthew G. Knepley Level: developer 1374891a9168SMatthew G. Knepley 1375891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM() 1376891a9168SMatthew G. Knepley @*/ 1377113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) 1378113c68e6SMatthew G. Knepley { 1379113c68e6SMatthew G. Knepley DM dmFace, dmCell; 1380113c68e6SMatthew G. Knepley DMLabel ghostLabel; 1381113c68e6SMatthew G. Knepley PetscSection sectionFace, sectionCell; 1382113c68e6SMatthew G. Knepley PetscSection coordSection; 1383113c68e6SMatthew G. Knepley Vec coordinates; 1384113c68e6SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 1385113c68e6SMatthew G. Knepley PetscReal minradius, gminradius; 1386113c68e6SMatthew G. Knepley PetscInt dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f; 1387113c68e6SMatthew G. Knepley PetscErrorCode ierr; 1388113c68e6SMatthew G. Knepley 1389113c68e6SMatthew G. Knepley PetscFunctionBegin; 1390113c68e6SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1391113c68e6SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1392113c68e6SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1393113c68e6SMatthew G. Knepley /* Make cell centroids and volumes */ 1394113c68e6SMatthew G. Knepley ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1395113c68e6SMatthew G. Knepley ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1396113c68e6SMatthew G. Knepley ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1397113c68e6SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1398113c68e6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1399113c68e6SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1400113c68e6SMatthew G. Knepley ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 14019e5edeeeSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1402113c68e6SMatthew G. Knepley ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1403113c68e6SMatthew G. Knepley ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1404113c68e6SMatthew G. Knepley ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1405113c68e6SMatthew G. Knepley ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 1406113c68e6SMatthew G. Knepley ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1407113c68e6SMatthew G. Knepley for (c = cStart; c < cEndInterior; ++c) { 1408113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 1409113c68e6SMatthew G. Knepley 1410113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1411113c68e6SMatthew G. Knepley ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1412113c68e6SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr); 1413113c68e6SMatthew G. Knepley } 1414113c68e6SMatthew G. Knepley /* Compute face normals and minimum cell radius */ 1415113c68e6SMatthew G. Knepley ierr = DMClone(dm, &dmFace);CHKERRQ(ierr); 1416113c68e6SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionFace);CHKERRQ(ierr); 1417113c68e6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1418113c68e6SMatthew G. Knepley ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr); 14199e5edeeeSMatthew G. Knepley for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1420113c68e6SMatthew G. Knepley ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr); 1421113c68e6SMatthew G. Knepley ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr); 1422113c68e6SMatthew G. Knepley ierr = PetscSectionDestroy(§ionFace);CHKERRQ(ierr); 1423113c68e6SMatthew G. Knepley ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr); 1424113c68e6SMatthew G. Knepley ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr); 1425113c68e6SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1426113c68e6SMatthew G. Knepley minradius = PETSC_MAX_REAL; 1427113c68e6SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 1428113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 1429113c68e6SMatthew G. Knepley PetscReal area; 14309ac3fadcSMatthew G. Knepley PetscInt ghost = -1, d; 1431113c68e6SMatthew G. Knepley 14329ac3fadcSMatthew G. Knepley if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);} 1433113c68e6SMatthew G. Knepley if (ghost >= 0) continue; 1434113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr); 1435113c68e6SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr); 1436113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] *= area; 1437113c68e6SMatthew G. Knepley /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */ 1438113c68e6SMatthew G. Knepley { 1439113c68e6SMatthew G. Knepley PetscFVCellGeom *cL, *cR; 1440113c68e6SMatthew G. Knepley const PetscInt *cells; 1441113c68e6SMatthew G. Knepley PetscReal *lcentroid, *rcentroid; 14420453c0cdSMatthew G. Knepley PetscReal l[3], r[3], v[3]; 1443113c68e6SMatthew G. Knepley 1444113c68e6SMatthew G. Knepley ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr); 1445113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr); 1446113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr); 1447113c68e6SMatthew G. Knepley lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid; 1448113c68e6SMatthew G. Knepley rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid; 1449f170fd80SMatthew G. Knepley ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr); 1450f170fd80SMatthew G. Knepley ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr); 14510453c0cdSMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, l, r, v); 1452113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) { 1453113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d]; 1454113c68e6SMatthew G. Knepley } 1455113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) { 1456113c68e6SMatthew G. Knepley if (dim == 2) SETERRQ5(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed, normal (%g,%g) v (%g,%g)", f, (double) fg->normal[0], (double) fg->normal[1], (double) v[0], (double) v[1]); 1457113c68e6SMatthew G. Knepley if (dim == 3) SETERRQ7(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed, normal (%g,%g,%g) v (%g,%g,%g)", f, (double) fg->normal[0], (double) fg->normal[1], (double) fg->normal[2], (double) v[0], (double) v[1], (double) v[2]); 1458113c68e6SMatthew G. Knepley SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f); 1459113c68e6SMatthew G. Knepley } 1460113c68e6SMatthew G. Knepley if (cells[0] < cEndInterior) { 1461113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v); 1462113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 1463113c68e6SMatthew G. Knepley } 1464113c68e6SMatthew G. Knepley if (cells[1] < cEndInterior) { 1465113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v); 1466113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 1467113c68e6SMatthew G. Knepley } 1468113c68e6SMatthew G. Knepley } 1469113c68e6SMatthew G. Knepley } 1470a9b180a6SBarry Smith ierr = MPI_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 1471113c68e6SMatthew G. Knepley ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr); 1472113c68e6SMatthew G. Knepley /* Compute centroids of ghost cells */ 1473113c68e6SMatthew G. Knepley for (c = cEndInterior; c < cEnd; ++c) { 1474113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 1475113c68e6SMatthew G. Knepley const PetscInt *cone, *support; 1476113c68e6SMatthew G. Knepley PetscInt coneSize, supportSize, s; 1477113c68e6SMatthew G. Knepley 1478113c68e6SMatthew G. Knepley ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr); 1479113c68e6SMatthew G. Knepley if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize); 1480113c68e6SMatthew G. Knepley ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr); 1481113c68e6SMatthew G. Knepley ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr); 1482113c68e6SMatthew G. Knepley if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 1", cone[0], supportSize); 1483113c68e6SMatthew G. Knepley ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr); 1484113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr); 1485113c68e6SMatthew G. Knepley for (s = 0; s < 2; ++s) { 1486113c68e6SMatthew G. Knepley /* Reflect ghost centroid across plane of face */ 1487113c68e6SMatthew G. Knepley if (support[s] == c) { 1488113c68e6SMatthew G. Knepley const PetscFVCellGeom *ci; 1489113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 1490113c68e6SMatthew G. Knepley PetscReal c2f[3], a; 1491113c68e6SMatthew G. Knepley 1492113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr); 1493113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */ 1494113c68e6SMatthew G. Knepley a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal); 1495113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr); 1496113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid); 1497113c68e6SMatthew G. Knepley cg->volume = ci->volume; 1498113c68e6SMatthew G. Knepley } 1499113c68e6SMatthew G. Knepley } 1500113c68e6SMatthew G. Knepley } 1501113c68e6SMatthew G. Knepley ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr); 1502113c68e6SMatthew G. Knepley ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1503113c68e6SMatthew G. Knepley ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 1504113c68e6SMatthew G. Knepley ierr = DMDestroy(&dmFace);CHKERRQ(ierr); 1505113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 1506113c68e6SMatthew G. Knepley } 1507113c68e6SMatthew G. Knepley 1508113c68e6SMatthew G. Knepley #undef __FUNCT__ 1509113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius" 1510113c68e6SMatthew G. Knepley /*@C 1511113c68e6SMatthew G. Knepley DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face 1512113c68e6SMatthew G. Knepley 1513113c68e6SMatthew G. Knepley Not collective 1514113c68e6SMatthew G. Knepley 1515113c68e6SMatthew G. Knepley Input Argument: 1516113c68e6SMatthew G. Knepley . dm - the DM 1517113c68e6SMatthew G. Knepley 1518113c68e6SMatthew G. Knepley Output Argument: 1519113c68e6SMatthew G. Knepley . minradius - the minium cell radius 1520113c68e6SMatthew G. Knepley 1521113c68e6SMatthew G. Knepley Level: developer 1522113c68e6SMatthew G. Knepley 1523113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates() 1524113c68e6SMatthew G. Knepley @*/ 1525113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) 1526113c68e6SMatthew G. Knepley { 1527113c68e6SMatthew G. Knepley PetscFunctionBegin; 1528113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1529113c68e6SMatthew G. Knepley PetscValidPointer(minradius,2); 1530113c68e6SMatthew G. Knepley *minradius = ((DM_Plex*) dm->data)->minradius; 1531113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 1532113c68e6SMatthew G. Knepley } 1533113c68e6SMatthew G. Knepley 1534113c68e6SMatthew G. Knepley #undef __FUNCT__ 1535113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius" 1536113c68e6SMatthew G. Knepley /*@C 1537113c68e6SMatthew G. Knepley DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face 1538113c68e6SMatthew G. Knepley 1539113c68e6SMatthew G. Knepley Logically collective 1540113c68e6SMatthew G. Knepley 1541113c68e6SMatthew G. Knepley Input Arguments: 1542113c68e6SMatthew G. Knepley + dm - the DM 1543113c68e6SMatthew G. Knepley - minradius - the minium cell radius 1544113c68e6SMatthew G. Knepley 1545113c68e6SMatthew G. Knepley Level: developer 1546113c68e6SMatthew G. Knepley 1547113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates() 1548113c68e6SMatthew G. Knepley @*/ 1549113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) 1550113c68e6SMatthew G. Knepley { 1551113c68e6SMatthew G. Knepley PetscFunctionBegin; 1552113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1553113c68e6SMatthew G. Knepley ((DM_Plex*) dm->data)->minradius = minradius; 1554113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 1555113c68e6SMatthew G. Knepley } 1556856ac710SMatthew G. Knepley 1557856ac710SMatthew G. Knepley #undef __FUNCT__ 1558856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal" 1559856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 1560856ac710SMatthew G. Knepley { 1561856ac710SMatthew G. Knepley DMLabel ghostLabel; 1562856ac710SMatthew G. Knepley PetscScalar *dx, *grad, **gref; 1563856ac710SMatthew G. Knepley PetscInt dim, cStart, cEnd, c, cEndInterior, maxNumFaces; 1564856ac710SMatthew G. Knepley PetscErrorCode ierr; 1565856ac710SMatthew G. Knepley 1566856ac710SMatthew G. Knepley PetscFunctionBegin; 1567856ac710SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1568856ac710SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1569856ac710SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1570856ac710SMatthew G. Knepley ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr); 1571856ac710SMatthew G. Knepley ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr); 1572856ac710SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1573856ac710SMatthew G. Knepley ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr); 1574856ac710SMatthew G. Knepley for (c = cStart; c < cEndInterior; c++) { 1575856ac710SMatthew G. Knepley const PetscInt *faces; 1576856ac710SMatthew G. Knepley PetscInt numFaces, usedFaces, f, d; 1577856ac710SMatthew G. Knepley const PetscFVCellGeom *cg; 1578856ac710SMatthew G. Knepley PetscBool boundary; 1579856ac710SMatthew G. Knepley PetscInt ghost; 1580856ac710SMatthew G. Knepley 1581856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1582856ac710SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr); 1583856ac710SMatthew G. Knepley ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr); 1584856ac710SMatthew G. Knepley if (numFaces < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cell %D has only %D faces, not enough for gradient reconstruction", c, numFaces); 1585856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 1586856ac710SMatthew G. Knepley const PetscFVCellGeom *cg1; 1587856ac710SMatthew G. Knepley PetscFVFaceGeom *fg; 1588856ac710SMatthew G. Knepley const PetscInt *fcells; 1589856ac710SMatthew G. Knepley PetscInt ncell, side; 1590856ac710SMatthew G. Knepley 1591856ac710SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 1592856ac710SMatthew G. Knepley ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 1593856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 1594856ac710SMatthew G. Knepley ierr = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr); 1595856ac710SMatthew G. Knepley side = (c != fcells[0]); /* c is on left=0 or right=1 of face */ 1596856ac710SMatthew G. Knepley ncell = fcells[!side]; /* the neighbor */ 1597856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr); 1598856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr); 1599856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d]; 1600856ac710SMatthew G. Knepley gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */ 1601856ac710SMatthew G. Knepley } 1602856ac710SMatthew G. Knepley if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?"); 1603856ac710SMatthew G. Knepley ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr); 1604856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 1605856ac710SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 1606856ac710SMatthew G. Knepley ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 1607856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 1608856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d]; 1609856ac710SMatthew G. Knepley ++usedFaces; 1610856ac710SMatthew G. Knepley } 1611856ac710SMatthew G. Knepley } 1612856ac710SMatthew G. Knepley ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr); 1613856ac710SMatthew G. Knepley PetscFunctionReturn(0); 1614856ac710SMatthew G. Knepley } 1615856ac710SMatthew G. Knepley 1616856ac710SMatthew G. Knepley #undef __FUNCT__ 1617856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM" 1618856ac710SMatthew G. Knepley /*@ 1619856ac710SMatthew G. Knepley DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data 1620856ac710SMatthew G. Knepley 1621856ac710SMatthew G. Knepley Collective on DM 1622856ac710SMatthew G. Knepley 1623856ac710SMatthew G. Knepley Input Arguments: 1624856ac710SMatthew G. Knepley + dm - The DM 1625856ac710SMatthew G. Knepley . fvm - The PetscFV 1626856ac710SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexGetFaceGeometryFVM() 1627856ac710SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexGetCellGeometryFVM() 1628856ac710SMatthew G. Knepley 1629856ac710SMatthew G. Knepley Output Parameters: 1630856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted 1631856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data 1632856ac710SMatthew G. Knepley 1633856ac710SMatthew G. Knepley Level: developer 1634856ac710SMatthew G. Knepley 1635856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM() 1636856ac710SMatthew G. Knepley @*/ 1637856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) 1638856ac710SMatthew G. Knepley { 1639856ac710SMatthew G. Knepley DM dmFace, dmCell; 1640856ac710SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 1641856ac710SMatthew G. Knepley PetscSection sectionGrad; 1642856ac710SMatthew G. Knepley PetscInt dim, pdim, cStart, cEnd, cEndInterior, c; 1643856ac710SMatthew G. Knepley PetscErrorCode ierr; 1644856ac710SMatthew G. Knepley 1645856ac710SMatthew G. Knepley PetscFunctionBegin; 1646856ac710SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1647856ac710SMatthew G. Knepley ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr); 1648856ac710SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1649856ac710SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1650856ac710SMatthew G. Knepley /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */ 1651856ac710SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 1652856ac710SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 1653856ac710SMatthew G. Knepley ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr); 1654856ac710SMatthew G. Knepley ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr); 1655856ac710SMatthew G. Knepley ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr); 1656856ac710SMatthew G. Knepley ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr); 1657856ac710SMatthew G. Knepley ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr); 1658856ac710SMatthew G. Knepley /* Create storage for gradients */ 1659856ac710SMatthew G. Knepley ierr = DMClone(dm, dmGrad);CHKERRQ(ierr); 1660856ac710SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionGrad);CHKERRQ(ierr); 1661856ac710SMatthew G. Knepley ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr); 1662856ac710SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);} 1663856ac710SMatthew G. Knepley ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr); 1664856ac710SMatthew G. Knepley ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr); 1665856ac710SMatthew G. Knepley ierr = PetscSectionDestroy(§ionGrad);CHKERRQ(ierr); 1666856ac710SMatthew G. Knepley PetscFunctionReturn(0); 1667856ac710SMatthew G. Knepley } 1668