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; 284cafe43deSMatthew G. Knepley PetscInt *dboxes; 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); 310cafe43deSMatthew G. Knepley /* Compute boxes which overlap each cell */ 311cafe43deSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 312cafe43deSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 313cafe43deSMatthew G. Knepley ierr = PetscCalloc1((dim+1) * dim, &dboxes);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 = PetscGridHashGetEnclosingBox(lbox, dim+1, ccoords, dboxes, NULL);CHKERRQ(ierr); 323cafe43deSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr); 324cafe43deSMatthew G. Knepley /* Get grid of boxes containing these */ 325cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];} 3262291669eSMatthew G. Knepley for (d = dim; d < 3; ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;} 327cafe43deSMatthew G. Knepley for (e = 1; e < dim+1; ++e) { 328cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) { 329cafe43deSMatthew G. Knepley dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]); 330cafe43deSMatthew G. Knepley dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]); 331cafe43deSMatthew G. Knepley } 332cafe43deSMatthew G. Knepley } 333cafe43deSMatthew G. Knepley /* Check whether cell contains any vertex of these subboxes TODO vectorize this */ 334cafe43deSMatthew 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]) { 335cafe43deSMatthew 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]) { 336cafe43deSMatthew 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]) { 337cafe43deSMatthew G. Knepley const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i; 338cafe43deSMatthew G. Knepley PetscScalar cpoint[3]; 339cafe43deSMatthew G. Knepley PetscInt cell, ii, jj, kk; 340cafe43deSMatthew G. Knepley 341cafe43deSMatthew G. Knepley for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) { 342cafe43deSMatthew G. Knepley for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) { 343cafe43deSMatthew G. Knepley for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) { 344cafe43deSMatthew G. Knepley 345cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr); 346cafe43deSMatthew G. Knepley if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;} 347cafe43deSMatthew G. Knepley } 348cafe43deSMatthew G. Knepley } 349cafe43deSMatthew G. Knepley } 350cafe43deSMatthew G. Knepley } 351cafe43deSMatthew G. Knepley } 352cafe43deSMatthew G. Knepley } 353cafe43deSMatthew G. Knepley } 354cafe43deSMatthew G. Knepley ierr = PetscFree(dboxes);CHKERRQ(ierr); 355cafe43deSMatthew G. Knepley ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr); 356cafe43deSMatthew G. Knepley ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr); 357cafe43deSMatthew G. Knepley *localBox = lbox; 358cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 359cafe43deSMatthew G. Knepley } 360cafe43deSMatthew G. Knepley 361cafe43deSMatthew G. Knepley #undef __FUNCT__ 362ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex" 363ccd2543fSMatthew G Knepley /* 364ccd2543fSMatthew G Knepley Need to implement using the guess 365ccd2543fSMatthew G Knepley */ 366ccd2543fSMatthew G Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, IS *cellIS) 367ccd2543fSMatthew G Knepley { 368cafe43deSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 369ccd2543fSMatthew G Knepley PetscInt bs, numPoints, p; 3701318edbeSMatthew G. Knepley PetscInt dim, cStart, cEnd, cMax, numCells, c; 371cafe43deSMatthew G. Knepley const PetscInt *boxCells; 372ccd2543fSMatthew G Knepley PetscInt *cells; 373ccd2543fSMatthew G Knepley PetscScalar *a; 374ccd2543fSMatthew G Knepley PetscErrorCode ierr; 375ccd2543fSMatthew G Knepley 376ccd2543fSMatthew G Knepley PetscFunctionBegin; 377cafe43deSMatthew G. Knepley if (!mesh->lbox) {ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);} 378cafe43deSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 379cafe43deSMatthew G. Knepley ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr); 380cafe43deSMatthew 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); 381ccd2543fSMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 382ccd2543fSMatthew G Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 383ccd2543fSMatthew G Knepley if (cMax >= 0) cEnd = PetscMin(cEnd, cMax); 384ccd2543fSMatthew G Knepley ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr); 385ccd2543fSMatthew G Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 386ccd2543fSMatthew G Knepley numPoints /= bs; 387785e854fSJed Brown ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr); 388cafe43deSMatthew G. Knepley /* Designate the local box for each point */ 389cafe43deSMatthew G. Knepley /* Send points to correct process */ 390cafe43deSMatthew G. Knepley /* Search cells that lie in each subbox */ 391cafe43deSMatthew G. Knepley /* Should we bin points before doing search? */ 392cafe43deSMatthew G. Knepley ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr); 393ccd2543fSMatthew G Knepley for (p = 0; p < numPoints; ++p) { 394ccd2543fSMatthew G Knepley const PetscScalar *point = &a[p*bs]; 3951318edbeSMatthew G. Knepley PetscInt dbin[3], bin, cell, cellOffset; 396ccd2543fSMatthew G Knepley 397cafe43deSMatthew G. Knepley ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr); 398cafe43deSMatthew G. Knepley /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */ 399cafe43deSMatthew G. Knepley ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr); 400cafe43deSMatthew G. Knepley ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr); 401cafe43deSMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 402cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr); 403ccd2543fSMatthew G Knepley if (cell >= 0) break; 404ccd2543fSMatthew G Knepley } 405*4f6087d8SMatthew G. Knepley if (cell < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D not found in mesh", p); 406ccd2543fSMatthew G Knepley cells[p] = cell; 407ccd2543fSMatthew G Knepley } 408cafe43deSMatthew G. Knepley ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr); 409cafe43deSMatthew G. Knepley /* Check for highest numbered proc that claims a point (do we care?) */ 410ccd2543fSMatthew G Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 411ccd2543fSMatthew G Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, cells, PETSC_OWN_POINTER, cellIS);CHKERRQ(ierr); 412ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 413ccd2543fSMatthew G Knepley } 414ccd2543fSMatthew G Knepley 415ccd2543fSMatthew G Knepley #undef __FUNCT__ 41617fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D_Internal" 41717fe8556SMatthew G. Knepley /* 41817fe8556SMatthew G. Knepley DMPlexComputeProjection2Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 2D 41917fe8556SMatthew G. Knepley */ 4203beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D_Internal(PetscScalar coords[], PetscReal R[]) 42117fe8556SMatthew G. Knepley { 42217fe8556SMatthew G. Knepley const PetscReal x = PetscRealPart(coords[2] - coords[0]); 42317fe8556SMatthew G. Knepley const PetscReal y = PetscRealPart(coords[3] - coords[1]); 4248b49ba18SBarry Smith const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r; 42517fe8556SMatthew G. Knepley 42617fe8556SMatthew G. Knepley PetscFunctionBegin; 4271c99cf0cSGeoffrey Irving R[0] = c; R[1] = -s; 4281c99cf0cSGeoffrey Irving R[2] = s; R[3] = c; 42917fe8556SMatthew G. Knepley coords[0] = 0.0; 4307f07f362SMatthew G. Knepley coords[1] = r; 43117fe8556SMatthew G. Knepley PetscFunctionReturn(0); 43217fe8556SMatthew G. Knepley } 43317fe8556SMatthew G. Knepley 43417fe8556SMatthew G. Knepley #undef __FUNCT__ 43528dbe442SToby Isaac #define __FUNCT__ "DMPlexComputeProjection3Dto1D_Internal" 43628dbe442SToby Isaac /* 43728dbe442SToby Isaac DMPlexComputeProjection3Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 3D 43828dbe442SToby Isaac 43928dbe442SToby Isaac This uses the basis completion described by Frisvad, 44028dbe442SToby Isaac 44128dbe442SToby Isaac http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html 44228dbe442SToby Isaac DOI:10.1080/2165347X.2012.689606 44328dbe442SToby Isaac */ 4443beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D_Internal(PetscScalar coords[], PetscReal R[]) 44528dbe442SToby Isaac { 44628dbe442SToby Isaac PetscReal x = PetscRealPart(coords[3] - coords[0]); 44728dbe442SToby Isaac PetscReal y = PetscRealPart(coords[4] - coords[1]); 44828dbe442SToby Isaac PetscReal z = PetscRealPart(coords[5] - coords[2]); 44928dbe442SToby Isaac PetscReal r = PetscSqrtReal(x*x + y*y + z*z); 45028dbe442SToby Isaac PetscReal rinv = 1. / r; 45128dbe442SToby Isaac PetscFunctionBegin; 45228dbe442SToby Isaac 45328dbe442SToby Isaac x *= rinv; y *= rinv; z *= rinv; 45428dbe442SToby Isaac if (x > 0.) { 45528dbe442SToby Isaac PetscReal inv1pX = 1./ (1. + x); 45628dbe442SToby Isaac 45728dbe442SToby Isaac R[0] = x; R[1] = -y; R[2] = -z; 45828dbe442SToby Isaac R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] = -y*z*inv1pX; 45928dbe442SToby Isaac R[6] = z; R[7] = -y*z*inv1pX; R[8] = 1. - z*z*inv1pX; 46028dbe442SToby Isaac } 46128dbe442SToby Isaac else { 46228dbe442SToby Isaac PetscReal inv1mX = 1./ (1. - x); 46328dbe442SToby Isaac 46428dbe442SToby Isaac R[0] = x; R[1] = z; R[2] = y; 46528dbe442SToby Isaac R[3] = y; R[4] = -y*z*inv1mX; R[5] = 1. - y*y*inv1mX; 46628dbe442SToby Isaac R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] = -y*z*inv1mX; 46728dbe442SToby Isaac } 46828dbe442SToby Isaac coords[0] = 0.0; 46928dbe442SToby Isaac coords[1] = r; 47028dbe442SToby Isaac PetscFunctionReturn(0); 47128dbe442SToby Isaac } 47228dbe442SToby Isaac 47328dbe442SToby Isaac #undef __FUNCT__ 474ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D_Internal" 475ccd2543fSMatthew G Knepley /* 476ccd2543fSMatthew G Knepley DMPlexComputeProjection3Dto2D_Internal - Rewrite coordinates to be the 2D projection of the 3D 477ccd2543fSMatthew G Knepley */ 4783beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D_Internal(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) 479ccd2543fSMatthew G Knepley { 4801ee9d5ecSMatthew G. Knepley PetscReal x1[3], x2[3], n[3], norm; 48199dec3a6SMatthew G. Knepley PetscReal x1p[3], x2p[3], xnp[3]; 4824a217a95SMatthew G. Knepley PetscReal sqrtz, alpha; 483ccd2543fSMatthew G Knepley const PetscInt dim = 3; 48499dec3a6SMatthew G. Knepley PetscInt d, e, p; 485ccd2543fSMatthew G Knepley 486ccd2543fSMatthew G Knepley PetscFunctionBegin; 487ccd2543fSMatthew G Knepley /* 0) Calculate normal vector */ 488ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 4891ee9d5ecSMatthew G. Knepley x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]); 4901ee9d5ecSMatthew G. Knepley x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]); 491ccd2543fSMatthew G Knepley } 492ccd2543fSMatthew G Knepley n[0] = x1[1]*x2[2] - x1[2]*x2[1]; 493ccd2543fSMatthew G Knepley n[1] = x1[2]*x2[0] - x1[0]*x2[2]; 494ccd2543fSMatthew G Knepley n[2] = x1[0]*x2[1] - x1[1]*x2[0]; 4958b49ba18SBarry Smith norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]); 496ccd2543fSMatthew G Knepley n[0] /= norm; 497ccd2543fSMatthew G Knepley n[1] /= norm; 498ccd2543fSMatthew G Knepley n[2] /= norm; 499ccd2543fSMatthew G Knepley /* 1) Take the normal vector and rotate until it is \hat z 500ccd2543fSMatthew G Knepley 501ccd2543fSMatthew G Knepley Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then 502ccd2543fSMatthew G Knepley 503ccd2543fSMatthew G Knepley R = / alpha nx nz alpha ny nz -1/alpha \ 504ccd2543fSMatthew G Knepley | -alpha ny alpha nx 0 | 505ccd2543fSMatthew G Knepley \ nx ny nz / 506ccd2543fSMatthew G Knepley 507ccd2543fSMatthew G Knepley will rotate the normal vector to \hat z 508ccd2543fSMatthew G Knepley */ 5098b49ba18SBarry Smith sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]); 51073868372SMatthew G. Knepley /* Check for n = z */ 51173868372SMatthew G. Knepley if (sqrtz < 1.0e-10) { 5121ee9d5ecSMatthew G. Knepley if (n[2] < 0.0) { 51399dec3a6SMatthew G. Knepley if (coordSize > 9) { 51499dec3a6SMatthew G. Knepley coords[2] = PetscRealPart(coords[3*dim+0] - coords[0*dim+0]); 51508b58242SMatthew G. Knepley coords[3] = PetscRealPart(coords[3*dim+1] - coords[0*dim+1]); 51699dec3a6SMatthew G. Knepley coords[4] = x2[0]; 51799dec3a6SMatthew G. Knepley coords[5] = x2[1]; 51899dec3a6SMatthew G. Knepley coords[6] = x1[0]; 51999dec3a6SMatthew G. Knepley coords[7] = x1[1]; 52099dec3a6SMatthew G. Knepley } else { 52173868372SMatthew G. Knepley coords[2] = x2[0]; 52273868372SMatthew G. Knepley coords[3] = x2[1]; 52373868372SMatthew G. Knepley coords[4] = x1[0]; 52473868372SMatthew G. Knepley coords[5] = x1[1]; 52599dec3a6SMatthew G. Knepley } 526b7ad821dSMatthew G. Knepley R[0] = 1.0; R[1] = 0.0; R[2] = 0.0; 527b7ad821dSMatthew G. Knepley R[3] = 0.0; R[4] = 1.0; R[5] = 0.0; 528b7ad821dSMatthew G. Knepley R[6] = 0.0; R[7] = 0.0; R[8] = -1.0; 52973868372SMatthew G. Knepley } else { 53099dec3a6SMatthew G. Knepley for (p = 3; p < coordSize/3; ++p) { 53199dec3a6SMatthew G. Knepley coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]); 53299dec3a6SMatthew G. Knepley coords[p*2+1] = PetscRealPart(coords[p*dim+1] - coords[0*dim+1]); 53399dec3a6SMatthew G. Knepley } 53473868372SMatthew G. Knepley coords[2] = x1[0]; 53573868372SMatthew G. Knepley coords[3] = x1[1]; 53673868372SMatthew G. Knepley coords[4] = x2[0]; 53773868372SMatthew G. Knepley coords[5] = x2[1]; 538b7ad821dSMatthew G. Knepley R[0] = 1.0; R[1] = 0.0; R[2] = 0.0; 539b7ad821dSMatthew G. Knepley R[3] = 0.0; R[4] = 1.0; R[5] = 0.0; 540b7ad821dSMatthew G. Knepley R[6] = 0.0; R[7] = 0.0; R[8] = 1.0; 54173868372SMatthew G. Knepley } 54299dec3a6SMatthew G. Knepley coords[0] = 0.0; 54399dec3a6SMatthew G. Knepley coords[1] = 0.0; 54473868372SMatthew G. Knepley PetscFunctionReturn(0); 54573868372SMatthew G. Knepley } 546da18b5e6SMatthew G Knepley alpha = 1.0/sqrtz; 547ccd2543fSMatthew G Knepley R[0] = alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz; 548ccd2543fSMatthew G Knepley R[3] = -alpha*n[1]; R[4] = alpha*n[0]; R[5] = 0.0; 549ccd2543fSMatthew G Knepley R[6] = n[0]; R[7] = n[1]; R[8] = n[2]; 550ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 551ccd2543fSMatthew G Knepley x1p[d] = 0.0; 552ccd2543fSMatthew G Knepley x2p[d] = 0.0; 553ccd2543fSMatthew G Knepley for (e = 0; e < dim; ++e) { 554ccd2543fSMatthew G Knepley x1p[d] += R[d*dim+e]*x1[e]; 555ccd2543fSMatthew G Knepley x2p[d] += R[d*dim+e]*x2[e]; 556ccd2543fSMatthew G Knepley } 557ccd2543fSMatthew G Knepley } 5588763be8eSMatthew G. Knepley if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 5598763be8eSMatthew G. Knepley if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 560ccd2543fSMatthew G Knepley /* 2) Project to (x, y) */ 56199dec3a6SMatthew G. Knepley for (p = 3; p < coordSize/3; ++p) { 56299dec3a6SMatthew G. Knepley for (d = 0; d < dim; ++d) { 56399dec3a6SMatthew G. Knepley xnp[d] = 0.0; 56499dec3a6SMatthew G. Knepley for (e = 0; e < dim; ++e) { 56599dec3a6SMatthew G. Knepley xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]); 56699dec3a6SMatthew G. Knepley } 56799dec3a6SMatthew G. Knepley if (d < dim-1) coords[p*2+d] = xnp[d]; 56899dec3a6SMatthew G. Knepley } 56999dec3a6SMatthew G. Knepley } 570ccd2543fSMatthew G Knepley coords[0] = 0.0; 571ccd2543fSMatthew G Knepley coords[1] = 0.0; 572ccd2543fSMatthew G Knepley coords[2] = x1p[0]; 573ccd2543fSMatthew G Knepley coords[3] = x1p[1]; 574ccd2543fSMatthew G Knepley coords[4] = x2p[0]; 575ccd2543fSMatthew G Knepley coords[5] = x2p[1]; 5767f07f362SMatthew G. Knepley /* Output R^T which rotates \hat z to the input normal */ 5777f07f362SMatthew G. Knepley for (d = 0; d < dim; ++d) { 5787f07f362SMatthew G. Knepley for (e = d+1; e < dim; ++e) { 5797f07f362SMatthew G. Knepley PetscReal tmp; 5807f07f362SMatthew G. Knepley 5817f07f362SMatthew G. Knepley tmp = R[d*dim+e]; 5827f07f362SMatthew G. Knepley R[d*dim+e] = R[e*dim+d]; 5837f07f362SMatthew G. Knepley R[e*dim+d] = tmp; 5847f07f362SMatthew G. Knepley } 5857f07f362SMatthew G. Knepley } 586ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 587ccd2543fSMatthew G Knepley } 588ccd2543fSMatthew G Knepley 589ccd2543fSMatthew G Knepley #undef __FUNCT__ 590834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal" 5916322fe33SJed Brown PETSC_UNUSED 592834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) 593834e62ceSMatthew G. Knepley { 594834e62ceSMatthew G. Knepley /* Signed volume is 1/2 the determinant 595834e62ceSMatthew G. Knepley 596834e62ceSMatthew G. Knepley | 1 1 1 | 597834e62ceSMatthew G. Knepley | x0 x1 x2 | 598834e62ceSMatthew G. Knepley | y0 y1 y2 | 599834e62ceSMatthew G. Knepley 600834e62ceSMatthew G. Knepley but if x0,y0 is the origin, we have 601834e62ceSMatthew G. Knepley 602834e62ceSMatthew G. Knepley | x1 x2 | 603834e62ceSMatthew G. Knepley | y1 y2 | 604834e62ceSMatthew G. Knepley */ 605834e62ceSMatthew G. Knepley const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1]; 606834e62ceSMatthew G. Knepley const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1]; 607834e62ceSMatthew G. Knepley PetscReal M[4], detM; 608834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; 60986623015SMatthew G. Knepley M[2] = y1; M[3] = y2; 610923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(&detM, M); 611834e62ceSMatthew G. Knepley *vol = 0.5*detM; 612834e62ceSMatthew G. Knepley PetscLogFlops(5.0); 613834e62ceSMatthew G. Knepley } 614834e62ceSMatthew G. Knepley 615834e62ceSMatthew G. Knepley #undef __FUNCT__ 616834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal" 617834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[]) 618834e62ceSMatthew G. Knepley { 619923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(vol, coords); 620834e62ceSMatthew G. Knepley *vol *= 0.5; 621834e62ceSMatthew G. Knepley } 622834e62ceSMatthew G. Knepley 623834e62ceSMatthew G. Knepley #undef __FUNCT__ 624834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal" 6256322fe33SJed Brown PETSC_UNUSED 626834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) 627834e62ceSMatthew G. Knepley { 628834e62ceSMatthew G. Knepley /* Signed volume is 1/6th of the determinant 629834e62ceSMatthew G. Knepley 630834e62ceSMatthew G. Knepley | 1 1 1 1 | 631834e62ceSMatthew G. Knepley | x0 x1 x2 x3 | 632834e62ceSMatthew G. Knepley | y0 y1 y2 y3 | 633834e62ceSMatthew G. Knepley | z0 z1 z2 z3 | 634834e62ceSMatthew G. Knepley 635834e62ceSMatthew G. Knepley but if x0,y0,z0 is the origin, we have 636834e62ceSMatthew G. Knepley 637834e62ceSMatthew G. Knepley | x1 x2 x3 | 638834e62ceSMatthew G. Knepley | y1 y2 y3 | 639834e62ceSMatthew G. Knepley | z1 z2 z3 | 640834e62ceSMatthew G. Knepley */ 641834e62ceSMatthew G. Knepley const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2]; 642834e62ceSMatthew G. Knepley const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2]; 643834e62ceSMatthew G. Knepley const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2]; 644834e62ceSMatthew G. Knepley PetscReal M[9], detM; 645834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; M[2] = x3; 646834e62ceSMatthew G. Knepley M[3] = y1; M[4] = y2; M[5] = y3; 647834e62ceSMatthew G. Knepley M[6] = z1; M[7] = z2; M[8] = z3; 648923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(&detM, M); 649b7ad821dSMatthew G. Knepley *vol = -0.16666666666666666666666*detM; 650834e62ceSMatthew G. Knepley PetscLogFlops(10.0); 651834e62ceSMatthew G. Knepley } 652834e62ceSMatthew G. Knepley 653834e62ceSMatthew G. Knepley #undef __FUNCT__ 6540ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal" 6550ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) 6560ec8681fSMatthew G. Knepley { 657923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(vol, coords); 658b7ad821dSMatthew G. Knepley *vol *= -0.16666666666666666666666; 6590ec8681fSMatthew G. Knepley } 6600ec8681fSMatthew G. Knepley 6610ec8681fSMatthew G. Knepley #undef __FUNCT__ 66217fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal" 66317fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 66417fe8556SMatthew G. Knepley { 66517fe8556SMatthew G. Knepley PetscSection coordSection; 66617fe8556SMatthew G. Knepley Vec coordinates; 667a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 6687f07f362SMatthew G. Knepley PetscInt numCoords, d; 66917fe8556SMatthew G. Knepley PetscErrorCode ierr; 67017fe8556SMatthew G. Knepley 67117fe8556SMatthew G. Knepley PetscFunctionBegin; 67217fe8556SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 67369d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 67417fe8556SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 6757f07f362SMatthew G. Knepley *detJ = 0.0; 67628dbe442SToby Isaac if (numCoords == 6) { 67728dbe442SToby Isaac const PetscInt dim = 3; 67828dbe442SToby Isaac PetscReal R[9], J0; 67928dbe442SToby Isaac 68028dbe442SToby Isaac if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 68128dbe442SToby Isaac ierr = DMPlexComputeProjection3Dto1D_Internal(coords, R);CHKERRQ(ierr); 68228dbe442SToby Isaac if (J) { 68328dbe442SToby Isaac J0 = 0.5*PetscRealPart(coords[1]); 68428dbe442SToby Isaac J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2]; 68528dbe442SToby Isaac J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5]; 68628dbe442SToby Isaac J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8]; 68728dbe442SToby Isaac DMPlex_Det3D_Internal(detJ, J); 68828dbe442SToby Isaac } 68928dbe442SToby Isaac if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 69028dbe442SToby Isaac } else if (numCoords == 4) { 6917f07f362SMatthew G. Knepley const PetscInt dim = 2; 6927f07f362SMatthew G. Knepley PetscReal R[4], J0; 6937f07f362SMatthew G. Knepley 6947f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 6957f07f362SMatthew G. Knepley ierr = DMPlexComputeProjection2Dto1D_Internal(coords, R);CHKERRQ(ierr); 69617fe8556SMatthew G. Knepley if (J) { 6977f07f362SMatthew G. Knepley J0 = 0.5*PetscRealPart(coords[1]); 6987f07f362SMatthew G. Knepley J[0] = R[0]*J0; J[1] = R[1]; 6997f07f362SMatthew G. Knepley J[2] = R[2]*J0; J[3] = R[3]; 700923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 70117fe8556SMatthew G. Knepley } 702923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 7037f07f362SMatthew G. Knepley } else if (numCoords == 2) { 7047f07f362SMatthew G. Knepley const PetscInt dim = 1; 7057f07f362SMatthew G. Knepley 7067f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 7077f07f362SMatthew G. Knepley if (J) { 7087f07f362SMatthew G. Knepley J[0] = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0])); 70917fe8556SMatthew G. Knepley *detJ = J[0]; 71017fe8556SMatthew G. Knepley PetscLogFlops(2.0); 71117fe8556SMatthew G. Knepley } 7127f07f362SMatthew G. Knepley if (invJ) {invJ[0] = 1.0/J[0]; PetscLogFlops(1.0);} 713796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords); 71417fe8556SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 71517fe8556SMatthew G. Knepley PetscFunctionReturn(0); 71617fe8556SMatthew G. Knepley } 71717fe8556SMatthew G. Knepley 71817fe8556SMatthew G. Knepley #undef __FUNCT__ 719ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal" 720ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 721ccd2543fSMatthew G Knepley { 722ccd2543fSMatthew G Knepley PetscSection coordSection; 723ccd2543fSMatthew G Knepley Vec coordinates; 724a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 7257f07f362SMatthew G. Knepley PetscInt numCoords, d, f, g; 726ccd2543fSMatthew G Knepley PetscErrorCode ierr; 727ccd2543fSMatthew G Knepley 728ccd2543fSMatthew G Knepley PetscFunctionBegin; 729ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 73069d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 731ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 7327f07f362SMatthew G. Knepley *detJ = 0.0; 733ccd2543fSMatthew G Knepley if (numCoords == 9) { 7347f07f362SMatthew G. Knepley const PetscInt dim = 3; 7357f07f362SMatthew 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}; 7367f07f362SMatthew G. Knepley 7377f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 73899dec3a6SMatthew G. Knepley ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr); 7397f07f362SMatthew G. Knepley if (J) { 740b7ad821dSMatthew G. Knepley const PetscInt pdim = 2; 741b7ad821dSMatthew G. Knepley 742b7ad821dSMatthew G. Knepley for (d = 0; d < pdim; d++) { 743b7ad821dSMatthew G. Knepley for (f = 0; f < pdim; f++) { 744b7ad821dSMatthew G. Knepley J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 745ccd2543fSMatthew G Knepley } 7467f07f362SMatthew G. Knepley } 7477f07f362SMatthew G. Knepley PetscLogFlops(8.0); 748923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 7497f07f362SMatthew G. Knepley for (d = 0; d < dim; d++) { 7507f07f362SMatthew G. Knepley for (f = 0; f < dim; f++) { 7517f07f362SMatthew G. Knepley J[d*dim+f] = 0.0; 7527f07f362SMatthew G. Knepley for (g = 0; g < dim; g++) { 7537f07f362SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 7547f07f362SMatthew G. Knepley } 7557f07f362SMatthew G. Knepley } 7567f07f362SMatthew G. Knepley } 7577f07f362SMatthew G. Knepley PetscLogFlops(18.0); 7587f07f362SMatthew G. Knepley } 759923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 7607f07f362SMatthew G. Knepley } else if (numCoords == 6) { 7617f07f362SMatthew G. Knepley const PetscInt dim = 2; 7627f07f362SMatthew G. Knepley 7637f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 764ccd2543fSMatthew G Knepley if (J) { 765ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 766ccd2543fSMatthew G Knepley for (f = 0; f < dim; f++) { 767ccd2543fSMatthew G Knepley J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d])); 768ccd2543fSMatthew G Knepley } 769ccd2543fSMatthew G Knepley } 7707f07f362SMatthew G. Knepley PetscLogFlops(8.0); 771923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 772ccd2543fSMatthew G Knepley } 773923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 774796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords); 775ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 776ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 777ccd2543fSMatthew G Knepley } 778ccd2543fSMatthew G Knepley 779ccd2543fSMatthew G Knepley #undef __FUNCT__ 780ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal" 781ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 782ccd2543fSMatthew G Knepley { 783ccd2543fSMatthew G Knepley PetscSection coordSection; 784ccd2543fSMatthew G Knepley Vec coordinates; 785a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 78699dec3a6SMatthew G. Knepley PetscInt numCoords, d, f, g; 787ccd2543fSMatthew G Knepley PetscErrorCode ierr; 788ccd2543fSMatthew G Knepley 789ccd2543fSMatthew G Knepley PetscFunctionBegin; 790ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 79169d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 79299dec3a6SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 7937f07f362SMatthew G. Knepley *detJ = 0.0; 79499dec3a6SMatthew G. Knepley if (numCoords == 12) { 79599dec3a6SMatthew G. Knepley const PetscInt dim = 3; 79699dec3a6SMatthew 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}; 79799dec3a6SMatthew G. Knepley 79899dec3a6SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 79999dec3a6SMatthew G. Knepley ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr); 80099dec3a6SMatthew G. Knepley if (J) { 80199dec3a6SMatthew G. Knepley const PetscInt pdim = 2; 80299dec3a6SMatthew G. Knepley 80399dec3a6SMatthew G. Knepley for (d = 0; d < pdim; d++) { 80499dec3a6SMatthew G. Knepley J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 80599dec3a6SMatthew G. Knepley J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 80699dec3a6SMatthew G. Knepley } 80799dec3a6SMatthew G. Knepley PetscLogFlops(8.0); 808923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 80999dec3a6SMatthew G. Knepley for (d = 0; d < dim; d++) { 81099dec3a6SMatthew G. Knepley for (f = 0; f < dim; f++) { 81199dec3a6SMatthew G. Knepley J[d*dim+f] = 0.0; 81299dec3a6SMatthew G. Knepley for (g = 0; g < dim; g++) { 81399dec3a6SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 81499dec3a6SMatthew G. Knepley } 81599dec3a6SMatthew G. Knepley } 81699dec3a6SMatthew G. Knepley } 81799dec3a6SMatthew G. Knepley PetscLogFlops(18.0); 81899dec3a6SMatthew G. Knepley } 819923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 82097f1a218SMatthew G. Knepley } else if ((numCoords == 8) || (numCoords == 16)) { 82199dec3a6SMatthew G. Knepley const PetscInt dim = 2; 82299dec3a6SMatthew G. Knepley 8237f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 824ccd2543fSMatthew G Knepley if (J) { 825ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 82699dec3a6SMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 82799dec3a6SMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 828ccd2543fSMatthew G Knepley } 8297f07f362SMatthew G. Knepley PetscLogFlops(8.0); 830923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 831ccd2543fSMatthew G Knepley } 832923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 833796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords); 83499dec3a6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 835ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 836ccd2543fSMatthew G Knepley } 837ccd2543fSMatthew G Knepley 838ccd2543fSMatthew G Knepley #undef __FUNCT__ 839ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal" 840ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 841ccd2543fSMatthew G Knepley { 842ccd2543fSMatthew G Knepley PetscSection coordSection; 843ccd2543fSMatthew G Knepley Vec coordinates; 844a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 845ccd2543fSMatthew G Knepley const PetscInt dim = 3; 84699dec3a6SMatthew G. Knepley PetscInt d; 847ccd2543fSMatthew G Knepley PetscErrorCode ierr; 848ccd2543fSMatthew G Knepley 849ccd2543fSMatthew G Knepley PetscFunctionBegin; 850ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 85169d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 852ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 8537f07f362SMatthew G. Knepley *detJ = 0.0; 8547f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 855ccd2543fSMatthew G Knepley if (J) { 856ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 857f0df753eSMatthew G. Knepley /* I orient with outward face normals */ 858f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d])); 859f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 860f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 861ccd2543fSMatthew G Knepley } 8627f07f362SMatthew G. Knepley PetscLogFlops(18.0); 863923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 864ccd2543fSMatthew G Knepley } 865923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 866ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 867ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 868ccd2543fSMatthew G Knepley } 869ccd2543fSMatthew G Knepley 870ccd2543fSMatthew G Knepley #undef __FUNCT__ 871ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal" 872ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 873ccd2543fSMatthew G Knepley { 874ccd2543fSMatthew G Knepley PetscSection coordSection; 875ccd2543fSMatthew G Knepley Vec coordinates; 876a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 877ccd2543fSMatthew G Knepley const PetscInt dim = 3; 878ccd2543fSMatthew G Knepley PetscInt d; 879ccd2543fSMatthew G Knepley PetscErrorCode ierr; 880ccd2543fSMatthew G Knepley 881ccd2543fSMatthew G Knepley PetscFunctionBegin; 882ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 88369d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 884ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 8857f07f362SMatthew G. Knepley *detJ = 0.0; 8867f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 887ccd2543fSMatthew G Knepley if (J) { 888ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 889f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 890f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 891f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d])); 892ccd2543fSMatthew G Knepley } 8937f07f362SMatthew G. Knepley PetscLogFlops(18.0); 894923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 895ccd2543fSMatthew G Knepley } 896923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 897ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 898ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 899ccd2543fSMatthew G Knepley } 900ccd2543fSMatthew G Knepley 901ccd2543fSMatthew G Knepley #undef __FUNCT__ 9028e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM" 903ccd2543fSMatthew G Knepley /*@C 9048e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell 905ccd2543fSMatthew G Knepley 906ccd2543fSMatthew G Knepley Collective on DM 907ccd2543fSMatthew G Knepley 908ccd2543fSMatthew G Knepley Input Arguments: 909ccd2543fSMatthew G Knepley + dm - the DM 910ccd2543fSMatthew G Knepley - cell - the cell 911ccd2543fSMatthew G Knepley 912ccd2543fSMatthew G Knepley Output Arguments: 913ccd2543fSMatthew G Knepley + v0 - the translation part of this affine transform 914ccd2543fSMatthew G Knepley . J - the Jacobian of the transform from the reference element 915ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian 916ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant 917ccd2543fSMatthew G Knepley 918ccd2543fSMatthew G Knepley Level: advanced 919ccd2543fSMatthew G Knepley 920ccd2543fSMatthew G Knepley Fortran Notes: 921ccd2543fSMatthew G Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 922ccd2543fSMatthew G Knepley include petsc.h90 in your code. 923ccd2543fSMatthew G Knepley 9248e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec() 925ccd2543fSMatthew G Knepley @*/ 9268e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 927ccd2543fSMatthew G Knepley { 92849dc4407SMatthew G. Knepley PetscInt depth, dim, coneSize; 929ccd2543fSMatthew G Knepley PetscErrorCode ierr; 930ccd2543fSMatthew G Knepley 931ccd2543fSMatthew G Knepley PetscFunctionBegin; 932139a35ccSMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 933ccd2543fSMatthew G Knepley ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 93449dc4407SMatthew G. Knepley if (depth == 1) { 9358e0841e0SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 9368e0841e0SMatthew G. Knepley } else { 9378e0841e0SMatthew G. Knepley DMLabel depth; 9388e0841e0SMatthew G. Knepley 9398e0841e0SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 9408e0841e0SMatthew G. Knepley ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr); 9418e0841e0SMatthew G. Knepley } 942ccd2543fSMatthew G Knepley switch (dim) { 94317fe8556SMatthew G. Knepley case 1: 94417fe8556SMatthew G. Knepley ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 94517fe8556SMatthew G. Knepley break; 946ccd2543fSMatthew G Knepley case 2: 947ccd2543fSMatthew G Knepley switch (coneSize) { 948ccd2543fSMatthew G Knepley case 3: 949ccd2543fSMatthew G Knepley ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 950ccd2543fSMatthew G Knepley break; 951ccd2543fSMatthew G Knepley case 4: 952ccd2543fSMatthew G Knepley ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 953ccd2543fSMatthew G Knepley break; 954ccd2543fSMatthew G Knepley default: 9558e0841e0SMatthew G. Knepley SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 956ccd2543fSMatthew G Knepley } 957ccd2543fSMatthew G Knepley break; 958ccd2543fSMatthew G Knepley case 3: 959ccd2543fSMatthew G Knepley switch (coneSize) { 960ccd2543fSMatthew G Knepley case 4: 961ccd2543fSMatthew G Knepley ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 962ccd2543fSMatthew G Knepley break; 9638e0841e0SMatthew G. Knepley case 6: /* Faces */ 9648e0841e0SMatthew G. Knepley case 8: /* Vertices */ 965ccd2543fSMatthew G Knepley ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 966ccd2543fSMatthew G Knepley break; 967ccd2543fSMatthew G Knepley default: 9688e0841e0SMatthew G. Knepley SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 969ccd2543fSMatthew G Knepley } 970ccd2543fSMatthew G Knepley break; 971ccd2543fSMatthew G Knepley default: 972ccd2543fSMatthew G Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 973ccd2543fSMatthew G Knepley } 9748e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 9758e0841e0SMatthew G. Knepley } 9768e0841e0SMatthew G. Knepley 9778e0841e0SMatthew G. Knepley #undef __FUNCT__ 9788e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal" 9798e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 9808e0841e0SMatthew G. Knepley { 9818e0841e0SMatthew G. Knepley PetscQuadrature quad; 9828e0841e0SMatthew G. Knepley PetscSection coordSection; 9838e0841e0SMatthew G. Knepley Vec coordinates; 9848e0841e0SMatthew G. Knepley PetscScalar *coords = NULL; 9858e0841e0SMatthew G. Knepley const PetscReal *quadPoints; 9868e0841e0SMatthew G. Knepley PetscReal *basisDer; 9878e0841e0SMatthew G. Knepley PetscInt dim, cdim, pdim, qdim, Nq, numCoords, d, q; 9888e0841e0SMatthew G. Knepley PetscErrorCode ierr; 9898e0841e0SMatthew G. Knepley 9908e0841e0SMatthew G. Knepley PetscFunctionBegin; 9918e0841e0SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 9928e0841e0SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 9938e0841e0SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 9948e0841e0SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 9958e0841e0SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr); 9968e0841e0SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 997954b1791SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr); 9988e0841e0SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr); 9998e0841e0SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 10008e0841e0SMatthew G. Knepley *detJ = 0.0; 10018e0841e0SMatthew G. Knepley if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim); 10028e0841e0SMatthew 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); 10038e0841e0SMatthew G. Knepley if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);} 10048e0841e0SMatthew G. Knepley if (J) { 10058e0841e0SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 10068e0841e0SMatthew G. Knepley PetscInt i, j, k, c, r; 10078e0841e0SMatthew G. Knepley 10088e0841e0SMatthew G. Knepley /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */ 10098e0841e0SMatthew G. Knepley for (k = 0; k < pdim; ++k) 10108e0841e0SMatthew G. Knepley for (j = 0; j < dim; ++j) 10118e0841e0SMatthew G. Knepley for (i = 0; i < cdim; ++i) 101271d6e60fSMatthew G. Knepley J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]); 10138e0841e0SMatthew G. Knepley PetscLogFlops(2.0*pdim*dim*cdim); 10148e0841e0SMatthew G. Knepley if (cdim > dim) { 10158e0841e0SMatthew G. Knepley for (c = dim; c < cdim; ++c) 10168e0841e0SMatthew G. Knepley for (r = 0; r < cdim; ++r) 10178e0841e0SMatthew G. Knepley J[r*cdim+c] = r == c ? 1.0 : 0.0; 10188e0841e0SMatthew G. Knepley } 10198e0841e0SMatthew G. Knepley switch (cdim) { 10208e0841e0SMatthew G. Knepley case 3: 10218e0841e0SMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 10228e0841e0SMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 102317fe8556SMatthew G. Knepley break; 102449dc4407SMatthew G. Knepley case 2: 10258e0841e0SMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 10268e0841e0SMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 102749dc4407SMatthew G. Knepley break; 10288e0841e0SMatthew G. Knepley case 1: 10298e0841e0SMatthew G. Knepley *detJ = J[0]; 10308e0841e0SMatthew G. Knepley if (invJ) invJ[0] = 1.0/J[0]; 103149dc4407SMatthew G. Knepley } 103249dc4407SMatthew G. Knepley } 10338e0841e0SMatthew G. Knepley } 10348e0841e0SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 10358e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 10368e0841e0SMatthew G. Knepley } 10378e0841e0SMatthew G. Knepley 10388e0841e0SMatthew G. Knepley #undef __FUNCT__ 10398e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM" 10408e0841e0SMatthew G. Knepley /*@C 10418e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell 10428e0841e0SMatthew G. Knepley 10438e0841e0SMatthew G. Knepley Collective on DM 10448e0841e0SMatthew G. Knepley 10458e0841e0SMatthew G. Knepley Input Arguments: 10468e0841e0SMatthew G. Knepley + dm - the DM 10478e0841e0SMatthew G. Knepley . cell - the cell 10488e0841e0SMatthew G. Knepley - fe - the finite element containing the quadrature 10498e0841e0SMatthew G. Knepley 10508e0841e0SMatthew G. Knepley Output Arguments: 10518e0841e0SMatthew G. Knepley + v0 - the translation part of this transform 10528e0841e0SMatthew G. Knepley . J - the Jacobian of the transform from the reference element at each quadrature point 10538e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point 10548e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point 10558e0841e0SMatthew G. Knepley 10568e0841e0SMatthew G. Knepley Level: advanced 10578e0841e0SMatthew G. Knepley 10588e0841e0SMatthew G. Knepley Fortran Notes: 10598e0841e0SMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 10608e0841e0SMatthew G. Knepley include petsc.h90 in your code. 10618e0841e0SMatthew G. Knepley 10628e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 10638e0841e0SMatthew G. Knepley @*/ 10648e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 10658e0841e0SMatthew G. Knepley { 10668e0841e0SMatthew G. Knepley PetscErrorCode ierr; 10678e0841e0SMatthew G. Knepley 10688e0841e0SMatthew G. Knepley PetscFunctionBegin; 10698e0841e0SMatthew G. Knepley if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);} 10708e0841e0SMatthew G. Knepley else {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);} 1071ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1072ccd2543fSMatthew G Knepley } 1073834e62ceSMatthew G. Knepley 1074834e62ceSMatthew G. Knepley #undef __FUNCT__ 1075cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal" 1076011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1077cc08537eSMatthew G. Knepley { 1078cc08537eSMatthew G. Knepley PetscSection coordSection; 1079cc08537eSMatthew G. Knepley Vec coordinates; 1080a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 108106e2781eSMatthew G. Knepley PetscScalar tmp[2]; 1082cc08537eSMatthew G. Knepley PetscInt coordSize; 1083cc08537eSMatthew G. Knepley PetscErrorCode ierr; 1084cc08537eSMatthew G. Knepley 1085cc08537eSMatthew G. Knepley PetscFunctionBegin; 1086cc08537eSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 108769d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1088cc08537eSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1089011ea5d8SMatthew G. Knepley if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now"); 109006e2781eSMatthew G. Knepley ierr = DMPlexLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr); 1091cc08537eSMatthew G. Knepley if (centroid) { 109206e2781eSMatthew G. Knepley centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]); 109306e2781eSMatthew G. Knepley centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]); 1094cc08537eSMatthew G. Knepley } 1095cc08537eSMatthew G. Knepley if (normal) { 1096a60a936bSMatthew G. Knepley PetscReal norm; 1097a60a936bSMatthew G. Knepley 109806e2781eSMatthew G. Knepley normal[0] = -PetscRealPart(coords[1] - tmp[1]); 109906e2781eSMatthew G. Knepley normal[1] = PetscRealPart(coords[0] - tmp[0]); 1100a60a936bSMatthew G. Knepley norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]); 1101a60a936bSMatthew G. Knepley normal[0] /= norm; 1102a60a936bSMatthew G. Knepley normal[1] /= norm; 1103cc08537eSMatthew G. Knepley } 1104cc08537eSMatthew G. Knepley if (vol) { 110506e2781eSMatthew G. Knepley *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1]))); 1106cc08537eSMatthew G. Knepley } 1107cc08537eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1108cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 1109cc08537eSMatthew G. Knepley } 1110cc08537eSMatthew G. Knepley 1111cc08537eSMatthew G. Knepley #undef __FUNCT__ 1112cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal" 1113cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */ 1114011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1115cc08537eSMatthew G. Knepley { 1116cc08537eSMatthew G. Knepley PetscSection coordSection; 1117cc08537eSMatthew G. Knepley Vec coordinates; 1118cc08537eSMatthew G. Knepley PetscScalar *coords = NULL; 11190a1d6728SMatthew G. Knepley PetscReal vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9]; 11200a1d6728SMatthew G. Knepley PetscInt tdim = 2, coordSize, numCorners, p, d, e; 1121cc08537eSMatthew G. Knepley PetscErrorCode ierr; 1122cc08537eSMatthew G. Knepley 1123cc08537eSMatthew G. Knepley PetscFunctionBegin; 1124cc08537eSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 11250a1d6728SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr); 112669d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1127cc08537eSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 11280bce18caSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 1129011ea5d8SMatthew G. Knepley if (normal) { 1130011ea5d8SMatthew G. Knepley if (dim > 2) { 11311ee9d5ecSMatthew G. Knepley const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]); 11321ee9d5ecSMatthew G. Knepley const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]); 11331ee9d5ecSMatthew G. Knepley const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]); 11340a1d6728SMatthew G. Knepley PetscReal norm; 11350a1d6728SMatthew G. Knepley 11361ee9d5ecSMatthew G. Knepley v0[0] = PetscRealPart(coords[0]); 11371ee9d5ecSMatthew G. Knepley v0[1] = PetscRealPart(coords[1]); 11381ee9d5ecSMatthew G. Knepley v0[2] = PetscRealPart(coords[2]); 11390a1d6728SMatthew G. Knepley normal[0] = y0*z1 - z0*y1; 11400a1d6728SMatthew G. Knepley normal[1] = z0*x1 - x0*z1; 11410a1d6728SMatthew G. Knepley normal[2] = x0*y1 - y0*x1; 11428b49ba18SBarry Smith norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]); 11430a1d6728SMatthew G. Knepley normal[0] /= norm; 11440a1d6728SMatthew G. Knepley normal[1] /= norm; 11450a1d6728SMatthew G. Knepley normal[2] /= norm; 1146011ea5d8SMatthew G. Knepley } else { 1147011ea5d8SMatthew G. Knepley for (d = 0; d < dim; ++d) normal[d] = 0.0; 1148011ea5d8SMatthew G. Knepley } 1149011ea5d8SMatthew G. Knepley } 115099dec3a6SMatthew G. Knepley if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D_Internal(coordSize, coords, R);CHKERRQ(ierr);} 11510a1d6728SMatthew G. Knepley for (p = 0; p < numCorners; ++p) { 11520a1d6728SMatthew G. Knepley /* Need to do this copy to get types right */ 11530a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 11541ee9d5ecSMatthew G. Knepley ctmp[d] = PetscRealPart(coords[p*tdim+d]); 11551ee9d5ecSMatthew G. Knepley ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]); 11560a1d6728SMatthew G. Knepley } 11570a1d6728SMatthew G. Knepley Volume_Triangle_Origin_Internal(&vtmp, ctmp); 11580a1d6728SMatthew G. Knepley vsum += vtmp; 11590a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 11600a1d6728SMatthew G. Knepley csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp; 11610a1d6728SMatthew G. Knepley } 11620a1d6728SMatthew G. Knepley } 11630a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 11640a1d6728SMatthew G. Knepley csum[d] /= (tdim+1)*vsum; 11650a1d6728SMatthew G. Knepley } 11660a1d6728SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1167ee6bbdb2SSatish Balay if (vol) *vol = PetscAbsReal(vsum); 11680a1d6728SMatthew G. Knepley if (centroid) { 11690a1d6728SMatthew G. Knepley if (dim > 2) { 11700a1d6728SMatthew G. Knepley for (d = 0; d < dim; ++d) { 11710a1d6728SMatthew G. Knepley centroid[d] = v0[d]; 11720a1d6728SMatthew G. Knepley for (e = 0; e < dim; ++e) { 11730a1d6728SMatthew G. Knepley centroid[d] += R[d*dim+e]*csum[e]; 11740a1d6728SMatthew G. Knepley } 11750a1d6728SMatthew G. Knepley } 11760a1d6728SMatthew G. Knepley } else for (d = 0; d < dim; ++d) centroid[d] = csum[d]; 11770a1d6728SMatthew G. Knepley } 1178cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 1179cc08537eSMatthew G. Knepley } 1180cc08537eSMatthew G. Knepley 1181cc08537eSMatthew G. Knepley #undef __FUNCT__ 11820ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal" 11830ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */ 1184011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 11850ec8681fSMatthew G. Knepley { 11860ec8681fSMatthew G. Knepley PetscSection coordSection; 11870ec8681fSMatthew G. Knepley Vec coordinates; 11880ec8681fSMatthew G. Knepley PetscScalar *coords = NULL; 118986623015SMatthew G. Knepley PetscReal vsum = 0.0, vtmp, coordsTmp[3*3]; 1190a7df9edeSMatthew G. Knepley const PetscInt *faces, *facesO; 11910ec8681fSMatthew G. Knepley PetscInt numFaces, f, coordSize, numCorners, p, d; 11920ec8681fSMatthew G. Knepley PetscErrorCode ierr; 11930ec8681fSMatthew G. Knepley 11940ec8681fSMatthew G. Knepley PetscFunctionBegin; 1195f6dae198SJed Brown if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim); 11960ec8681fSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 119769d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 11980ec8681fSMatthew G. Knepley 1199d9a81ebdSMatthew G. Knepley if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0; 12000ec8681fSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr); 12010ec8681fSMatthew G. Knepley ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr); 1202a7df9edeSMatthew G. Knepley ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr); 12030ec8681fSMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 1204011ea5d8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 12050ec8681fSMatthew G. Knepley numCorners = coordSize/dim; 12060ec8681fSMatthew G. Knepley switch (numCorners) { 12070ec8681fSMatthew G. Knepley case 3: 12080ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12091ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 12101ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 12111ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]); 12120ec8681fSMatthew G. Knepley } 12130ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1214a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 12150ec8681fSMatthew G. Knepley vsum += vtmp; 12164f25033aSJed Brown if (centroid) { /* Centroid of OABC = (a+b+c)/4 */ 12170ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12181ee9d5ecSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 12190ec8681fSMatthew G. Knepley } 12200ec8681fSMatthew G. Knepley } 12210ec8681fSMatthew G. Knepley break; 12220ec8681fSMatthew G. Knepley case 4: 12230ec8681fSMatthew G. Knepley /* DO FOR PYRAMID */ 12240ec8681fSMatthew G. Knepley /* First tet */ 12250ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12261ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 12271ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 12281ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 12290ec8681fSMatthew G. Knepley } 12300ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1231a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 12320ec8681fSMatthew G. Knepley vsum += vtmp; 12330ec8681fSMatthew G. Knepley if (centroid) { 12340ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12350ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 12360ec8681fSMatthew G. Knepley } 12370ec8681fSMatthew G. Knepley } 12380ec8681fSMatthew G. Knepley /* Second tet */ 12390ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12401ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]); 12411ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]); 12421ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 12430ec8681fSMatthew G. Knepley } 12440ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1245a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 12460ec8681fSMatthew G. Knepley vsum += vtmp; 12470ec8681fSMatthew G. Knepley if (centroid) { 12480ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 12490ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 12500ec8681fSMatthew G. Knepley } 12510ec8681fSMatthew G. Knepley } 12520ec8681fSMatthew G. Knepley break; 12530ec8681fSMatthew G. Knepley default: 1254796f034aSJed Brown SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners); 12550ec8681fSMatthew G. Knepley } 12564f25033aSJed Brown ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 12570ec8681fSMatthew G. Knepley } 12588763be8eSMatthew G. Knepley if (vol) *vol = PetscAbsReal(vsum); 12590ec8681fSMatthew G. Knepley if (normal) for (d = 0; d < dim; ++d) normal[d] = 0.0; 1260d9a81ebdSMatthew G. Knepley if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4); 12610ec8681fSMatthew G. Knepley PetscFunctionReturn(0); 12620ec8681fSMatthew G. Knepley } 12630ec8681fSMatthew G. Knepley 12640ec8681fSMatthew G. Knepley #undef __FUNCT__ 1265834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM" 1266834e62ceSMatthew G. Knepley /*@C 1267834e62ceSMatthew G. Knepley DMPlexComputeCellGeometryFVM - Compute the volume for a given cell 1268834e62ceSMatthew G. Knepley 1269834e62ceSMatthew G. Knepley Collective on DM 1270834e62ceSMatthew G. Knepley 1271834e62ceSMatthew G. Knepley Input Arguments: 1272834e62ceSMatthew G. Knepley + dm - the DM 1273834e62ceSMatthew G. Knepley - cell - the cell 1274834e62ceSMatthew G. Knepley 1275834e62ceSMatthew G. Knepley Output Arguments: 1276834e62ceSMatthew G. Knepley + volume - the cell volume 1277cc08537eSMatthew G. Knepley . centroid - the cell centroid 1278cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate 1279834e62ceSMatthew G. Knepley 1280834e62ceSMatthew G. Knepley Level: advanced 1281834e62ceSMatthew G. Knepley 1282834e62ceSMatthew G. Knepley Fortran Notes: 1283834e62ceSMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 1284834e62ceSMatthew G. Knepley include petsc.h90 in your code. 1285834e62ceSMatthew G. Knepley 128669d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 1287834e62ceSMatthew G. Knepley @*/ 1288cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1289834e62ceSMatthew G. Knepley { 12900ec8681fSMatthew G. Knepley PetscInt depth, dim; 1291834e62ceSMatthew G. Knepley PetscErrorCode ierr; 1292834e62ceSMatthew G. Knepley 1293834e62ceSMatthew G. Knepley PetscFunctionBegin; 1294834e62ceSMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 1295c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1296834e62ceSMatthew G. Knepley if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated"); 1297834e62ceSMatthew G. Knepley /* We need to keep a pointer to the depth label */ 1298011ea5d8SMatthew G. Knepley ierr = DMPlexGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr); 1299834e62ceSMatthew G. Knepley /* Cone size is now the number of faces */ 1300011ea5d8SMatthew G. Knepley switch (depth) { 1301cc08537eSMatthew G. Knepley case 1: 1302011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1303cc08537eSMatthew G. Knepley break; 1304834e62ceSMatthew G. Knepley case 2: 1305011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1306834e62ceSMatthew G. Knepley break; 1307834e62ceSMatthew G. Knepley case 3: 1308011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1309834e62ceSMatthew G. Knepley break; 1310834e62ceSMatthew G. Knepley default: 1311834e62ceSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 1312834e62ceSMatthew G. Knepley } 1313834e62ceSMatthew G. Knepley PetscFunctionReturn(0); 1314834e62ceSMatthew G. Knepley } 1315113c68e6SMatthew G. Knepley 1316113c68e6SMatthew G. Knepley #undef __FUNCT__ 1317c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM" 1318c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */ 1319c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom) 1320c0d900a5SMatthew G. Knepley { 1321c0d900a5SMatthew G. Knepley DM dmCell; 1322c0d900a5SMatthew G. Knepley Vec coordinates; 1323c0d900a5SMatthew G. Knepley PetscSection coordSection, sectionCell; 1324c0d900a5SMatthew G. Knepley PetscScalar *cgeom; 1325c0d900a5SMatthew G. Knepley PetscInt cStart, cEnd, cMax, c; 1326c0d900a5SMatthew G. Knepley PetscErrorCode ierr; 1327c0d900a5SMatthew G. Knepley 1328c0d900a5SMatthew G. Knepley PetscFunctionBegin; 1329c0d900a5SMatthew G. Knepley ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1330c0d900a5SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1331c0d900a5SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1332c0d900a5SMatthew G. Knepley ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1333c0d900a5SMatthew G. Knepley ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1334c0d900a5SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1335c0d900a5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1336c0d900a5SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 1337c0d900a5SMatthew G. Knepley cEnd = cMax < 0 ? cEnd : cMax; 1338c0d900a5SMatthew G. Knepley ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 1339c0d900a5SMatthew G. Knepley /* TODO This needs to be multiplied by Nq for non-affine */ 13409e5edeeeSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1341c0d900a5SMatthew G. Knepley ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1342c0d900a5SMatthew G. Knepley ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1343c0d900a5SMatthew G. Knepley ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1344c0d900a5SMatthew G. Knepley ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 1345c0d900a5SMatthew G. Knepley ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1346c0d900a5SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1347c0d900a5SMatthew G. Knepley PetscFECellGeom *cg; 1348c0d900a5SMatthew G. Knepley 1349c0d900a5SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1350c0d900a5SMatthew G. Knepley ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1351c0d900a5SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr); 1352c0d900a5SMatthew G. Knepley if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c); 1353c0d900a5SMatthew G. Knepley } 1354c0d900a5SMatthew G. Knepley ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1355c0d900a5SMatthew G. Knepley ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 1356c0d900a5SMatthew G. Knepley PetscFunctionReturn(0); 1357c0d900a5SMatthew G. Knepley } 1358c0d900a5SMatthew G. Knepley 1359c0d900a5SMatthew G. Knepley #undef __FUNCT__ 1360113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM" 1361891a9168SMatthew G. Knepley /*@ 1362891a9168SMatthew G. Knepley DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method 1363891a9168SMatthew G. Knepley 1364891a9168SMatthew G. Knepley Input Parameter: 1365891a9168SMatthew G. Knepley . dm - The DM 1366891a9168SMatthew G. Knepley 1367891a9168SMatthew G. Knepley Output Parameters: 1368891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data 1369891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data 1370891a9168SMatthew G. Knepley 1371891a9168SMatthew G. Knepley Level: developer 1372891a9168SMatthew G. Knepley 1373891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM() 1374891a9168SMatthew G. Knepley @*/ 1375113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) 1376113c68e6SMatthew G. Knepley { 1377113c68e6SMatthew G. Knepley DM dmFace, dmCell; 1378113c68e6SMatthew G. Knepley DMLabel ghostLabel; 1379113c68e6SMatthew G. Knepley PetscSection sectionFace, sectionCell; 1380113c68e6SMatthew G. Knepley PetscSection coordSection; 1381113c68e6SMatthew G. Knepley Vec coordinates; 1382113c68e6SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 1383113c68e6SMatthew G. Knepley PetscReal minradius, gminradius; 1384113c68e6SMatthew G. Knepley PetscInt dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f; 1385113c68e6SMatthew G. Knepley PetscErrorCode ierr; 1386113c68e6SMatthew G. Knepley 1387113c68e6SMatthew G. Knepley PetscFunctionBegin; 1388113c68e6SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1389113c68e6SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1390113c68e6SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1391113c68e6SMatthew G. Knepley /* Make cell centroids and volumes */ 1392113c68e6SMatthew G. Knepley ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1393113c68e6SMatthew G. Knepley ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1394113c68e6SMatthew G. Knepley ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1395113c68e6SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1396113c68e6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1397113c68e6SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1398113c68e6SMatthew G. Knepley ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 13999e5edeeeSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1400113c68e6SMatthew G. Knepley ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1401113c68e6SMatthew G. Knepley ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1402113c68e6SMatthew G. Knepley ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1403113c68e6SMatthew G. Knepley ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 1404113c68e6SMatthew G. Knepley ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1405113c68e6SMatthew G. Knepley for (c = cStart; c < cEndInterior; ++c) { 1406113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 1407113c68e6SMatthew G. Knepley 1408113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1409113c68e6SMatthew G. Knepley ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1410113c68e6SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr); 1411113c68e6SMatthew G. Knepley } 1412113c68e6SMatthew G. Knepley /* Compute face normals and minimum cell radius */ 1413113c68e6SMatthew G. Knepley ierr = DMClone(dm, &dmFace);CHKERRQ(ierr); 1414113c68e6SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionFace);CHKERRQ(ierr); 1415113c68e6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1416113c68e6SMatthew G. Knepley ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr); 14179e5edeeeSMatthew G. Knepley for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1418113c68e6SMatthew G. Knepley ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr); 1419113c68e6SMatthew G. Knepley ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr); 1420113c68e6SMatthew G. Knepley ierr = PetscSectionDestroy(§ionFace);CHKERRQ(ierr); 1421113c68e6SMatthew G. Knepley ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr); 1422113c68e6SMatthew G. Knepley ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr); 1423113c68e6SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1424113c68e6SMatthew G. Knepley minradius = PETSC_MAX_REAL; 1425113c68e6SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 1426113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 1427113c68e6SMatthew G. Knepley PetscReal area; 14289ac3fadcSMatthew G. Knepley PetscInt ghost = -1, d; 1429113c68e6SMatthew G. Knepley 14309ac3fadcSMatthew G. Knepley if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);} 1431113c68e6SMatthew G. Knepley if (ghost >= 0) continue; 1432113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr); 1433113c68e6SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr); 1434113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] *= area; 1435113c68e6SMatthew G. Knepley /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */ 1436113c68e6SMatthew G. Knepley { 1437113c68e6SMatthew G. Knepley PetscFVCellGeom *cL, *cR; 1438113c68e6SMatthew G. Knepley const PetscInt *cells; 1439113c68e6SMatthew G. Knepley PetscReal *lcentroid, *rcentroid; 14400453c0cdSMatthew G. Knepley PetscReal l[3], r[3], v[3]; 1441113c68e6SMatthew G. Knepley 1442113c68e6SMatthew G. Knepley ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr); 1443113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr); 1444113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr); 1445113c68e6SMatthew G. Knepley lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid; 1446113c68e6SMatthew G. Knepley rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid; 1447f170fd80SMatthew G. Knepley ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr); 1448f170fd80SMatthew G. Knepley ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr); 14490453c0cdSMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, l, r, v); 1450113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) { 1451113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d]; 1452113c68e6SMatthew G. Knepley } 1453113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) { 1454113c68e6SMatthew 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]); 1455113c68e6SMatthew 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]); 1456113c68e6SMatthew G. Knepley SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f); 1457113c68e6SMatthew G. Knepley } 1458113c68e6SMatthew G. Knepley if (cells[0] < cEndInterior) { 1459113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v); 1460113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 1461113c68e6SMatthew G. Knepley } 1462113c68e6SMatthew G. Knepley if (cells[1] < cEndInterior) { 1463113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v); 1464113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 1465113c68e6SMatthew G. Knepley } 1466113c68e6SMatthew G. Knepley } 1467113c68e6SMatthew G. Knepley } 1468a9b180a6SBarry Smith ierr = MPI_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 1469113c68e6SMatthew G. Knepley ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr); 1470113c68e6SMatthew G. Knepley /* Compute centroids of ghost cells */ 1471113c68e6SMatthew G. Knepley for (c = cEndInterior; c < cEnd; ++c) { 1472113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 1473113c68e6SMatthew G. Knepley const PetscInt *cone, *support; 1474113c68e6SMatthew G. Knepley PetscInt coneSize, supportSize, s; 1475113c68e6SMatthew G. Knepley 1476113c68e6SMatthew G. Knepley ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr); 1477113c68e6SMatthew G. Knepley if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize); 1478113c68e6SMatthew G. Knepley ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr); 1479113c68e6SMatthew G. Knepley ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr); 1480113c68e6SMatthew G. Knepley if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 1", cone[0], supportSize); 1481113c68e6SMatthew G. Knepley ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr); 1482113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr); 1483113c68e6SMatthew G. Knepley for (s = 0; s < 2; ++s) { 1484113c68e6SMatthew G. Knepley /* Reflect ghost centroid across plane of face */ 1485113c68e6SMatthew G. Knepley if (support[s] == c) { 1486113c68e6SMatthew G. Knepley const PetscFVCellGeom *ci; 1487113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 1488113c68e6SMatthew G. Knepley PetscReal c2f[3], a; 1489113c68e6SMatthew G. Knepley 1490113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr); 1491113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */ 1492113c68e6SMatthew G. Knepley a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal); 1493113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr); 1494113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid); 1495113c68e6SMatthew G. Knepley cg->volume = ci->volume; 1496113c68e6SMatthew G. Knepley } 1497113c68e6SMatthew G. Knepley } 1498113c68e6SMatthew G. Knepley } 1499113c68e6SMatthew G. Knepley ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr); 1500113c68e6SMatthew G. Knepley ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1501113c68e6SMatthew G. Knepley ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 1502113c68e6SMatthew G. Knepley ierr = DMDestroy(&dmFace);CHKERRQ(ierr); 1503113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 1504113c68e6SMatthew G. Knepley } 1505113c68e6SMatthew G. Knepley 1506113c68e6SMatthew G. Knepley #undef __FUNCT__ 1507113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius" 1508113c68e6SMatthew G. Knepley /*@C 1509113c68e6SMatthew G. Knepley DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face 1510113c68e6SMatthew G. Knepley 1511113c68e6SMatthew G. Knepley Not collective 1512113c68e6SMatthew G. Knepley 1513113c68e6SMatthew G. Knepley Input Argument: 1514113c68e6SMatthew G. Knepley . dm - the DM 1515113c68e6SMatthew G. Knepley 1516113c68e6SMatthew G. Knepley Output Argument: 1517113c68e6SMatthew G. Knepley . minradius - the minium cell radius 1518113c68e6SMatthew G. Knepley 1519113c68e6SMatthew G. Knepley Level: developer 1520113c68e6SMatthew G. Knepley 1521113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates() 1522113c68e6SMatthew G. Knepley @*/ 1523113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) 1524113c68e6SMatthew G. Knepley { 1525113c68e6SMatthew G. Knepley PetscFunctionBegin; 1526113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1527113c68e6SMatthew G. Knepley PetscValidPointer(minradius,2); 1528113c68e6SMatthew G. Knepley *minradius = ((DM_Plex*) dm->data)->minradius; 1529113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 1530113c68e6SMatthew G. Knepley } 1531113c68e6SMatthew G. Knepley 1532113c68e6SMatthew G. Knepley #undef __FUNCT__ 1533113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius" 1534113c68e6SMatthew G. Knepley /*@C 1535113c68e6SMatthew G. Knepley DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face 1536113c68e6SMatthew G. Knepley 1537113c68e6SMatthew G. Knepley Logically collective 1538113c68e6SMatthew G. Knepley 1539113c68e6SMatthew G. Knepley Input Arguments: 1540113c68e6SMatthew G. Knepley + dm - the DM 1541113c68e6SMatthew G. Knepley - minradius - the minium cell radius 1542113c68e6SMatthew G. Knepley 1543113c68e6SMatthew G. Knepley Level: developer 1544113c68e6SMatthew G. Knepley 1545113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates() 1546113c68e6SMatthew G. Knepley @*/ 1547113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) 1548113c68e6SMatthew G. Knepley { 1549113c68e6SMatthew G. Knepley PetscFunctionBegin; 1550113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1551113c68e6SMatthew G. Knepley ((DM_Plex*) dm->data)->minradius = minradius; 1552113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 1553113c68e6SMatthew G. Knepley } 1554856ac710SMatthew G. Knepley 1555856ac710SMatthew G. Knepley #undef __FUNCT__ 1556856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal" 1557856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 1558856ac710SMatthew G. Knepley { 1559856ac710SMatthew G. Knepley DMLabel ghostLabel; 1560856ac710SMatthew G. Knepley PetscScalar *dx, *grad, **gref; 1561856ac710SMatthew G. Knepley PetscInt dim, cStart, cEnd, c, cEndInterior, maxNumFaces; 1562856ac710SMatthew G. Knepley PetscErrorCode ierr; 1563856ac710SMatthew G. Knepley 1564856ac710SMatthew G. Knepley PetscFunctionBegin; 1565856ac710SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1566856ac710SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1567856ac710SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1568856ac710SMatthew G. Knepley ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr); 1569856ac710SMatthew G. Knepley ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr); 1570856ac710SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1571856ac710SMatthew G. Knepley ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr); 1572856ac710SMatthew G. Knepley for (c = cStart; c < cEndInterior; c++) { 1573856ac710SMatthew G. Knepley const PetscInt *faces; 1574856ac710SMatthew G. Knepley PetscInt numFaces, usedFaces, f, d; 1575856ac710SMatthew G. Knepley const PetscFVCellGeom *cg; 1576856ac710SMatthew G. Knepley PetscBool boundary; 1577856ac710SMatthew G. Knepley PetscInt ghost; 1578856ac710SMatthew G. Knepley 1579856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1580856ac710SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr); 1581856ac710SMatthew G. Knepley ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr); 1582856ac710SMatthew 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); 1583856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 1584856ac710SMatthew G. Knepley const PetscFVCellGeom *cg1; 1585856ac710SMatthew G. Knepley PetscFVFaceGeom *fg; 1586856ac710SMatthew G. Knepley const PetscInt *fcells; 1587856ac710SMatthew G. Knepley PetscInt ncell, side; 1588856ac710SMatthew G. Knepley 1589856ac710SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 1590856ac710SMatthew G. Knepley ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 1591856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 1592856ac710SMatthew G. Knepley ierr = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr); 1593856ac710SMatthew G. Knepley side = (c != fcells[0]); /* c is on left=0 or right=1 of face */ 1594856ac710SMatthew G. Knepley ncell = fcells[!side]; /* the neighbor */ 1595856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr); 1596856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr); 1597856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d]; 1598856ac710SMatthew G. Knepley gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */ 1599856ac710SMatthew G. Knepley } 1600856ac710SMatthew G. Knepley if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?"); 1601856ac710SMatthew G. Knepley ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr); 1602856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 1603856ac710SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 1604856ac710SMatthew G. Knepley ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 1605856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 1606856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d]; 1607856ac710SMatthew G. Knepley ++usedFaces; 1608856ac710SMatthew G. Knepley } 1609856ac710SMatthew G. Knepley } 1610856ac710SMatthew G. Knepley ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr); 1611856ac710SMatthew G. Knepley PetscFunctionReturn(0); 1612856ac710SMatthew G. Knepley } 1613856ac710SMatthew G. Knepley 1614856ac710SMatthew G. Knepley #undef __FUNCT__ 1615856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM" 1616856ac710SMatthew G. Knepley /*@ 1617856ac710SMatthew G. Knepley DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data 1618856ac710SMatthew G. Knepley 1619856ac710SMatthew G. Knepley Collective on DM 1620856ac710SMatthew G. Knepley 1621856ac710SMatthew G. Knepley Input Arguments: 1622856ac710SMatthew G. Knepley + dm - The DM 1623856ac710SMatthew G. Knepley . fvm - The PetscFV 1624856ac710SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexGetFaceGeometryFVM() 1625856ac710SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexGetCellGeometryFVM() 1626856ac710SMatthew G. Knepley 1627856ac710SMatthew G. Knepley Output Parameters: 1628856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted 1629856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data 1630856ac710SMatthew G. Knepley 1631856ac710SMatthew G. Knepley Level: developer 1632856ac710SMatthew G. Knepley 1633856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM() 1634856ac710SMatthew G. Knepley @*/ 1635856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) 1636856ac710SMatthew G. Knepley { 1637856ac710SMatthew G. Knepley DM dmFace, dmCell; 1638856ac710SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 1639856ac710SMatthew G. Knepley PetscSection sectionGrad; 1640856ac710SMatthew G. Knepley PetscInt dim, pdim, cStart, cEnd, cEndInterior, c; 1641856ac710SMatthew G. Knepley PetscErrorCode ierr; 1642856ac710SMatthew G. Knepley 1643856ac710SMatthew G. Knepley PetscFunctionBegin; 1644856ac710SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1645856ac710SMatthew G. Knepley ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr); 1646856ac710SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1647856ac710SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1648856ac710SMatthew G. Knepley /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */ 1649856ac710SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 1650856ac710SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 1651856ac710SMatthew G. Knepley ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr); 1652856ac710SMatthew G. Knepley ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr); 1653856ac710SMatthew G. Knepley ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr); 1654856ac710SMatthew G. Knepley ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr); 1655856ac710SMatthew G. Knepley ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr); 1656856ac710SMatthew G. Knepley /* Create storage for gradients */ 1657856ac710SMatthew G. Knepley ierr = DMClone(dm, dmGrad);CHKERRQ(ierr); 1658856ac710SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionGrad);CHKERRQ(ierr); 1659856ac710SMatthew G. Knepley ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr); 1660856ac710SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);} 1661856ac710SMatthew G. Knepley ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr); 1662856ac710SMatthew G. Knepley ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr); 1663856ac710SMatthew G. Knepley ierr = PetscSectionDestroy(§ionGrad);CHKERRQ(ierr); 1664856ac710SMatthew G. Knepley PetscFunctionReturn(0); 1665856ac710SMatthew G. Knepley } 1666