1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2ccd2543fSMatthew G Knepley 3ccd2543fSMatthew G Knepley #undef __FUNCT__ 4fea14342SMatthew G. Knepley #define __FUNCT__ "DMPlexGetLineIntersection_2D_Internal" 5fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection) 6fea14342SMatthew G. Knepley { 7fea14342SMatthew G. Knepley const PetscReal p0_x = segmentA[0*2+0]; 8fea14342SMatthew G. Knepley const PetscReal p0_y = segmentA[0*2+1]; 9fea14342SMatthew G. Knepley const PetscReal p1_x = segmentA[1*2+0]; 10fea14342SMatthew G. Knepley const PetscReal p1_y = segmentA[1*2+1]; 11fea14342SMatthew G. Knepley const PetscReal p2_x = segmentB[0*2+0]; 12fea14342SMatthew G. Knepley const PetscReal p2_y = segmentB[0*2+1]; 13fea14342SMatthew G. Knepley const PetscReal p3_x = segmentB[1*2+0]; 14fea14342SMatthew G. Knepley const PetscReal p3_y = segmentB[1*2+1]; 15fea14342SMatthew G. Knepley const PetscReal s1_x = p1_x - p0_x; 16fea14342SMatthew G. Knepley const PetscReal s1_y = p1_y - p0_y; 17fea14342SMatthew G. Knepley const PetscReal s2_x = p3_x - p2_x; 18fea14342SMatthew G. Knepley const PetscReal s2_y = p3_y - p2_y; 19fea14342SMatthew G. Knepley const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y); 20fea14342SMatthew G. Knepley 21fea14342SMatthew G. Knepley PetscFunctionBegin; 22fea14342SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 23fea14342SMatthew G. Knepley /* Non-parallel lines */ 24fea14342SMatthew G. Knepley if (denom != 0.0) { 25fea14342SMatthew G. Knepley const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom; 26fea14342SMatthew G. Knepley const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom; 27fea14342SMatthew G. Knepley 28fea14342SMatthew G. Knepley if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { 29fea14342SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 30fea14342SMatthew G. Knepley if (intersection) { 31fea14342SMatthew G. Knepley intersection[0] = p0_x + (t * s1_x); 32fea14342SMatthew G. Knepley intersection[1] = p0_y + (t * s1_y); 33fea14342SMatthew G. Knepley } 34fea14342SMatthew G. Knepley } 35fea14342SMatthew G. Knepley } 36fea14342SMatthew G. Knepley PetscFunctionReturn(0); 37fea14342SMatthew G. Knepley } 38fea14342SMatthew G. Knepley 39fea14342SMatthew G. Knepley #undef __FUNCT__ 40ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal" 41ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 42ccd2543fSMatthew G Knepley { 43ccd2543fSMatthew G Knepley const PetscInt embedDim = 2; 44f5ebc837SMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 45ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 46ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 47ccd2543fSMatthew G Knepley PetscReal v0[2], J[4], invJ[4], detJ; 48ccd2543fSMatthew G Knepley PetscReal xi, eta; 49ccd2543fSMatthew G Knepley PetscErrorCode ierr; 50ccd2543fSMatthew G Knepley 51ccd2543fSMatthew G Knepley PetscFunctionBegin; 528e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 53ccd2543fSMatthew G Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]); 54ccd2543fSMatthew G Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]); 55ccd2543fSMatthew G Knepley 56f5ebc837SMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c; 57ccd2543fSMatthew G Knepley else *cell = -1; 58ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 59ccd2543fSMatthew G Knepley } 60ccd2543fSMatthew G Knepley 61ccd2543fSMatthew G Knepley #undef __FUNCT__ 62*62a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Simplex_2D_Internal" 63*62a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[]) 64*62a38674SMatthew G. Knepley { 65*62a38674SMatthew G. Knepley const PetscInt embedDim = 2; 66*62a38674SMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 67*62a38674SMatthew G. Knepley PetscReal x = PetscRealPart(point[0]); 68*62a38674SMatthew G. Knepley PetscReal y = PetscRealPart(point[1]); 69*62a38674SMatthew G. Knepley PetscReal v0[2], J[4], invJ[4], detJ; 70*62a38674SMatthew G. Knepley PetscReal xi, eta, r; 71*62a38674SMatthew G. Knepley PetscErrorCode ierr; 72*62a38674SMatthew G. Knepley 73*62a38674SMatthew G. Knepley PetscFunctionBegin; 74*62a38674SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 75*62a38674SMatthew G. Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]); 76*62a38674SMatthew G. Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]); 77*62a38674SMatthew G. Knepley 78*62a38674SMatthew G. Knepley xi = PetscMax(xi, 0.0); 79*62a38674SMatthew G. Knepley eta = PetscMax(eta, 0.0); 80*62a38674SMatthew G. Knepley r = (xi + eta)/2.0; 81*62a38674SMatthew G. Knepley if (xi + eta > 2.0) { 82*62a38674SMatthew G. Knepley r = (xi + eta)/2.0; 83*62a38674SMatthew G. Knepley xi /= r; 84*62a38674SMatthew G. Knepley eta /= r; 85*62a38674SMatthew G. Knepley } 86*62a38674SMatthew G. Knepley cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0]; 87*62a38674SMatthew G. Knepley cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1]; 88*62a38674SMatthew G. Knepley PetscFunctionReturn(0); 89*62a38674SMatthew G. Knepley } 90*62a38674SMatthew G. Knepley 91*62a38674SMatthew G. Knepley #undef __FUNCT__ 92ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal" 93ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 94ccd2543fSMatthew G Knepley { 95ccd2543fSMatthew G Knepley PetscSection coordSection; 96ccd2543fSMatthew G Knepley Vec coordsLocal; 97a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 98ccd2543fSMatthew G Knepley const PetscInt faces[8] = {0, 1, 1, 2, 2, 3, 3, 0}; 99ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 100ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 101ccd2543fSMatthew G Knepley PetscInt crossings = 0, f; 102ccd2543fSMatthew G Knepley PetscErrorCode ierr; 103ccd2543fSMatthew G Knepley 104ccd2543fSMatthew G Knepley PetscFunctionBegin; 105ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 10669d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 107ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 108ccd2543fSMatthew G Knepley for (f = 0; f < 4; ++f) { 109ccd2543fSMatthew G Knepley PetscReal x_i = PetscRealPart(coords[faces[2*f+0]*2+0]); 110ccd2543fSMatthew G Knepley PetscReal y_i = PetscRealPart(coords[faces[2*f+0]*2+1]); 111ccd2543fSMatthew G Knepley PetscReal x_j = PetscRealPart(coords[faces[2*f+1]*2+0]); 112ccd2543fSMatthew G Knepley PetscReal y_j = PetscRealPart(coords[faces[2*f+1]*2+1]); 113ccd2543fSMatthew G Knepley PetscReal slope = (y_j - y_i) / (x_j - x_i); 114ccd2543fSMatthew G Knepley PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE; 115ccd2543fSMatthew G Knepley PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE; 116ccd2543fSMatthew G Knepley PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE; 117ccd2543fSMatthew G Knepley if ((cond1 || cond2) && above) ++crossings; 118ccd2543fSMatthew G Knepley } 119ccd2543fSMatthew G Knepley if (crossings % 2) *cell = c; 120ccd2543fSMatthew G Knepley else *cell = -1; 121ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 122ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 123ccd2543fSMatthew G Knepley } 124ccd2543fSMatthew G Knepley 125ccd2543fSMatthew G Knepley #undef __FUNCT__ 126ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal" 127ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 128ccd2543fSMatthew G Knepley { 129ccd2543fSMatthew G Knepley const PetscInt embedDim = 3; 130ccd2543fSMatthew G Knepley PetscReal v0[3], J[9], invJ[9], detJ; 131ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 132ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 133ccd2543fSMatthew G Knepley PetscReal z = PetscRealPart(point[2]); 134ccd2543fSMatthew G Knepley PetscReal xi, eta, zeta; 135ccd2543fSMatthew G Knepley PetscErrorCode ierr; 136ccd2543fSMatthew G Knepley 137ccd2543fSMatthew G Knepley PetscFunctionBegin; 1388e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 139ccd2543fSMatthew G Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]); 140ccd2543fSMatthew G Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]); 141ccd2543fSMatthew G Knepley zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]); 142ccd2543fSMatthew G Knepley 143ccd2543fSMatthew G Knepley if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c; 144ccd2543fSMatthew G Knepley else *cell = -1; 145ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 146ccd2543fSMatthew G Knepley } 147ccd2543fSMatthew G Knepley 148ccd2543fSMatthew G Knepley #undef __FUNCT__ 149ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal" 150ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 151ccd2543fSMatthew G Knepley { 152ccd2543fSMatthew G Knepley PetscSection coordSection; 153ccd2543fSMatthew G Knepley Vec coordsLocal; 1547c1f9639SMatthew G Knepley PetscScalar *coords; 155fb150da6SMatthew G. Knepley const PetscInt faces[24] = {0, 3, 2, 1, 5, 4, 7, 6, 3, 0, 4, 5, 156fb150da6SMatthew G. Knepley 1, 2, 6, 7, 3, 5, 6, 2, 0, 1, 7, 4}; 157ccd2543fSMatthew G Knepley PetscBool found = PETSC_TRUE; 158ccd2543fSMatthew G Knepley PetscInt f; 159ccd2543fSMatthew G Knepley PetscErrorCode ierr; 160ccd2543fSMatthew G Knepley 161ccd2543fSMatthew G Knepley PetscFunctionBegin; 162ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 16369d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 164ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 165ccd2543fSMatthew G Knepley for (f = 0; f < 6; ++f) { 166ccd2543fSMatthew G Knepley /* Check the point is under plane */ 167ccd2543fSMatthew G Knepley /* Get face normal */ 168ccd2543fSMatthew G Knepley PetscReal v_i[3]; 169ccd2543fSMatthew G Knepley PetscReal v_j[3]; 170ccd2543fSMatthew G Knepley PetscReal normal[3]; 171ccd2543fSMatthew G Knepley PetscReal pp[3]; 172ccd2543fSMatthew G Knepley PetscReal dot; 173ccd2543fSMatthew G Knepley 174ccd2543fSMatthew G Knepley v_i[0] = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]); 175ccd2543fSMatthew G Knepley v_i[1] = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]); 176ccd2543fSMatthew G Knepley v_i[2] = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]); 177ccd2543fSMatthew G Knepley v_j[0] = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]); 178ccd2543fSMatthew G Knepley v_j[1] = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]); 179ccd2543fSMatthew G Knepley v_j[2] = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]); 180ccd2543fSMatthew G Knepley normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1]; 181ccd2543fSMatthew G Knepley normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2]; 182ccd2543fSMatthew G Knepley normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0]; 183ccd2543fSMatthew G Knepley pp[0] = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]); 184ccd2543fSMatthew G Knepley pp[1] = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]); 185ccd2543fSMatthew G Knepley pp[2] = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]); 186ccd2543fSMatthew G Knepley dot = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2]; 187ccd2543fSMatthew G Knepley 188ccd2543fSMatthew G Knepley /* Check that projected point is in face (2D location problem) */ 189ccd2543fSMatthew G Knepley if (dot < 0.0) { 190ccd2543fSMatthew G Knepley found = PETSC_FALSE; 191ccd2543fSMatthew G Knepley break; 192ccd2543fSMatthew G Knepley } 193ccd2543fSMatthew G Knepley } 194ccd2543fSMatthew G Knepley if (found) *cell = c; 195ccd2543fSMatthew G Knepley else *cell = -1; 196ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 197ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 198ccd2543fSMatthew G Knepley } 199ccd2543fSMatthew G Knepley 200ccd2543fSMatthew G Knepley #undef __FUNCT__ 201c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal" 202c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[]) 203c4eade1cSMatthew G. Knepley { 204c4eade1cSMatthew G. Knepley PetscInt d; 205c4eade1cSMatthew G. Knepley 206c4eade1cSMatthew G. Knepley PetscFunctionBegin; 207c4eade1cSMatthew G. Knepley box->dim = dim; 208c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]); 209c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 210c4eade1cSMatthew G. Knepley } 211c4eade1cSMatthew G. Knepley 212c4eade1cSMatthew G. Knepley #undef __FUNCT__ 213c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate" 214c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box) 215c4eade1cSMatthew G. Knepley { 216c4eade1cSMatthew G. Knepley PetscErrorCode ierr; 217c4eade1cSMatthew G. Knepley 218c4eade1cSMatthew G. Knepley PetscFunctionBegin; 219c4eade1cSMatthew G. Knepley ierr = PetscMalloc1(1, box);CHKERRQ(ierr); 220c4eade1cSMatthew G. Knepley ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr); 221c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 222c4eade1cSMatthew G. Knepley } 223c4eade1cSMatthew G. Knepley 224c4eade1cSMatthew G. Knepley #undef __FUNCT__ 225c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge" 226c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[]) 227c4eade1cSMatthew G. Knepley { 228c4eade1cSMatthew G. Knepley PetscInt d; 229c4eade1cSMatthew G. Knepley 230c4eade1cSMatthew G. Knepley PetscFunctionBegin; 231c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 232c4eade1cSMatthew G. Knepley box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d])); 233c4eade1cSMatthew G. Knepley box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d])); 234c4eade1cSMatthew G. Knepley } 235c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 236c4eade1cSMatthew G. Knepley } 237c4eade1cSMatthew G. Knepley 238c4eade1cSMatthew G. Knepley #undef __FUNCT__ 239c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid" 240*62a38674SMatthew G. Knepley /* 241*62a38674SMatthew G. Knepley PetscGridHashSetGrid - Divide the grid into boxes 242*62a38674SMatthew G. Knepley 243*62a38674SMatthew G. Knepley Not collective 244*62a38674SMatthew G. Knepley 245*62a38674SMatthew G. Knepley Input Parameters: 246*62a38674SMatthew G. Knepley + box - The grid hash object 247*62a38674SMatthew G. Knepley . n - The number of boxes in each dimension, or PETSC_DETERMINE 248*62a38674SMatthew G. Knepley - h - The box size in each dimension, only used if n[d] == PETSC_DETERMINE 249*62a38674SMatthew G. Knepley 250*62a38674SMatthew G. Knepley Level: developer 251*62a38674SMatthew G. Knepley 252*62a38674SMatthew G. Knepley .seealso: PetscGridHashCreate() 253*62a38674SMatthew G. Knepley */ 254c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[]) 255c4eade1cSMatthew G. Knepley { 256c4eade1cSMatthew G. Knepley PetscInt d; 257c4eade1cSMatthew G. Knepley 258c4eade1cSMatthew G. Knepley PetscFunctionBegin; 259c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 260c4eade1cSMatthew G. Knepley box->extent[d] = box->upper[d] - box->lower[d]; 261c4eade1cSMatthew G. Knepley if (n[d] == PETSC_DETERMINE) { 262c4eade1cSMatthew G. Knepley box->h[d] = h[d]; 263c4eade1cSMatthew G. Knepley box->n[d] = PetscCeilReal(box->extent[d]/h[d]); 264c4eade1cSMatthew G. Knepley } else { 265c4eade1cSMatthew G. Knepley box->n[d] = n[d]; 266c4eade1cSMatthew G. Knepley box->h[d] = box->extent[d]/n[d]; 267c4eade1cSMatthew G. Knepley } 268c4eade1cSMatthew G. Knepley } 269c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 270c4eade1cSMatthew G. Knepley } 271c4eade1cSMatthew G. Knepley 272c4eade1cSMatthew G. Knepley #undef __FUNCT__ 273c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox" 274*62a38674SMatthew G. Knepley /* 275*62a38674SMatthew G. Knepley PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point 276*62a38674SMatthew G. Knepley 277*62a38674SMatthew G. Knepley Not collective 278*62a38674SMatthew G. Knepley 279*62a38674SMatthew G. Knepley Input Parameters: 280*62a38674SMatthew G. Knepley + box - The grid hash object 281*62a38674SMatthew G. Knepley . numPoints - The number of input points 282*62a38674SMatthew G. Knepley - points - The input point coordinates 283*62a38674SMatthew G. Knepley 284*62a38674SMatthew G. Knepley Output Parameters: 285*62a38674SMatthew G. Knepley + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 286*62a38674SMatthew G. Knepley - boxes - An array of numPoints integers expressing the enclosing box as single number, or NULL 287*62a38674SMatthew G. Knepley 288*62a38674SMatthew G. Knepley Level: developer 289*62a38674SMatthew G. Knepley 290*62a38674SMatthew G. Knepley .seealso: PetscGridHashCreate() 291*62a38674SMatthew G. Knepley */ 2921c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[]) 293c4eade1cSMatthew G. Knepley { 294c4eade1cSMatthew G. Knepley const PetscReal *lower = box->lower; 295c4eade1cSMatthew G. Knepley const PetscReal *upper = box->upper; 296c4eade1cSMatthew G. Knepley const PetscReal *h = box->h; 297c4eade1cSMatthew G. Knepley const PetscInt *n = box->n; 298c4eade1cSMatthew G. Knepley const PetscInt dim = box->dim; 299c4eade1cSMatthew G. Knepley PetscInt d, p; 300c4eade1cSMatthew G. Knepley 301c4eade1cSMatthew G. Knepley PetscFunctionBegin; 302c4eade1cSMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 303c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) { 3041c6dfc3eSMatthew G. Knepley PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]); 305c4eade1cSMatthew G. Knepley 3061c6dfc3eSMatthew G. Knepley if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1; 307c4eade1cSMatthew 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", 3081c6dfc3eSMatthew 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); 309c4eade1cSMatthew G. Knepley dboxes[p*dim+d] = dbox; 310c4eade1cSMatthew G. Knepley } 311c4eade1cSMatthew G. Knepley if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1]; 312c4eade1cSMatthew G. Knepley } 313c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 314c4eade1cSMatthew G. Knepley } 315c4eade1cSMatthew G. Knepley 316c4eade1cSMatthew G. Knepley #undef __FUNCT__ 317c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy" 318c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box) 319c4eade1cSMatthew G. Knepley { 320c4eade1cSMatthew G. Knepley PetscErrorCode ierr; 321c4eade1cSMatthew G. Knepley 322c4eade1cSMatthew G. Knepley PetscFunctionBegin; 323c4eade1cSMatthew G. Knepley if (*box) { 324c4eade1cSMatthew G. Knepley ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr); 325c4eade1cSMatthew G. Knepley ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr); 326c4eade1cSMatthew G. Knepley ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr); 327c4eade1cSMatthew G. Knepley } 328c4eade1cSMatthew G. Knepley ierr = PetscFree(*box);CHKERRQ(ierr); 329c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 330c4eade1cSMatthew G. Knepley } 331c4eade1cSMatthew G. Knepley 332cafe43deSMatthew G. Knepley #undef __FUNCT__ 333cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexLocatePoint_Internal" 334cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell) 335cafe43deSMatthew G. Knepley { 336cafe43deSMatthew G. Knepley PetscInt coneSize; 337cafe43deSMatthew G. Knepley PetscErrorCode ierr; 338cafe43deSMatthew G. Knepley 339cafe43deSMatthew G. Knepley PetscFunctionBegin; 340cafe43deSMatthew G. Knepley switch (dim) { 341cafe43deSMatthew G. Knepley case 2: 342cafe43deSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr); 343cafe43deSMatthew G. Knepley switch (coneSize) { 344cafe43deSMatthew G. Knepley case 3: 345cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 346cafe43deSMatthew G. Knepley break; 347cafe43deSMatthew G. Knepley case 4: 348cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 349cafe43deSMatthew G. Knepley break; 350cafe43deSMatthew G. Knepley default: 351cafe43deSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize); 352cafe43deSMatthew G. Knepley } 353cafe43deSMatthew G. Knepley break; 354cafe43deSMatthew G. Knepley case 3: 355cafe43deSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr); 356cafe43deSMatthew G. Knepley switch (coneSize) { 357cafe43deSMatthew G. Knepley case 4: 358cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 359cafe43deSMatthew G. Knepley break; 360cafe43deSMatthew G. Knepley case 6: 361cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 362cafe43deSMatthew G. Knepley break; 363cafe43deSMatthew G. Knepley default: 364cafe43deSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize); 365cafe43deSMatthew G. Knepley } 366cafe43deSMatthew G. Knepley break; 367cafe43deSMatthew G. Knepley default: 368cafe43deSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim); 369cafe43deSMatthew G. Knepley } 370cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 371cafe43deSMatthew G. Knepley } 372cafe43deSMatthew G. Knepley 373cafe43deSMatthew G. Knepley #undef __FUNCT__ 374*62a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Internal" 375*62a38674SMatthew G. Knepley /* 376*62a38674SMatthew G. Knepley DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point 377*62a38674SMatthew G. Knepley */ 378*62a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[]) 379*62a38674SMatthew G. Knepley { 380*62a38674SMatthew G. Knepley PetscInt coneSize; 381*62a38674SMatthew G. Knepley PetscErrorCode ierr; 382*62a38674SMatthew G. Knepley 383*62a38674SMatthew G. Knepley PetscFunctionBegin; 384*62a38674SMatthew G. Knepley switch (dim) { 385*62a38674SMatthew G. Knepley case 2: 386*62a38674SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 387*62a38674SMatthew G. Knepley switch (coneSize) { 388*62a38674SMatthew G. Knepley case 3: 389*62a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr); 390*62a38674SMatthew G. Knepley break; 391*62a38674SMatthew G. Knepley #if 0 392*62a38674SMatthew G. Knepley case 4: 393*62a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr); 394*62a38674SMatthew G. Knepley break; 395*62a38674SMatthew G. Knepley #endif 396*62a38674SMatthew G. Knepley default: 397*62a38674SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize); 398*62a38674SMatthew G. Knepley } 399*62a38674SMatthew G. Knepley break; 400*62a38674SMatthew G. Knepley #if 0 401*62a38674SMatthew G. Knepley case 3: 402*62a38674SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 403*62a38674SMatthew G. Knepley switch (coneSize) { 404*62a38674SMatthew G. Knepley case 4: 405*62a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr); 406*62a38674SMatthew G. Knepley break; 407*62a38674SMatthew G. Knepley case 6: 408*62a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr); 409*62a38674SMatthew G. Knepley break; 410*62a38674SMatthew G. Knepley default: 411*62a38674SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize); 412*62a38674SMatthew G. Knepley } 413*62a38674SMatthew G. Knepley break; 414*62a38674SMatthew G. Knepley #endif 415*62a38674SMatthew G. Knepley default: 416*62a38674SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for mesh dimension %D", dim); 417*62a38674SMatthew G. Knepley } 418*62a38674SMatthew G. Knepley PetscFunctionReturn(0); 419*62a38674SMatthew G. Knepley } 420*62a38674SMatthew G. Knepley 421*62a38674SMatthew G. Knepley #undef __FUNCT__ 422cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGridHash_Internal" 423*62a38674SMatthew G. Knepley /* 424*62a38674SMatthew G. Knepley DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex 425*62a38674SMatthew G. Knepley 426*62a38674SMatthew G. Knepley Collective on DM 427*62a38674SMatthew G. Knepley 428*62a38674SMatthew G. Knepley Input Parameter: 429*62a38674SMatthew G. Knepley . dm - The Plex 430*62a38674SMatthew G. Knepley 431*62a38674SMatthew G. Knepley Output Parameter: 432*62a38674SMatthew G. Knepley . localBox - The grid hash object 433*62a38674SMatthew G. Knepley 434*62a38674SMatthew G. Knepley Level: developer 435*62a38674SMatthew G. Knepley 436*62a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox() 437*62a38674SMatthew G. Knepley */ 438cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox) 439cafe43deSMatthew G. Knepley { 440cafe43deSMatthew G. Knepley MPI_Comm comm; 441cafe43deSMatthew G. Knepley PetscGridHash lbox; 442cafe43deSMatthew G. Knepley Vec coordinates; 443cafe43deSMatthew G. Knepley PetscSection coordSection; 444cafe43deSMatthew G. Knepley Vec coordsLocal; 445cafe43deSMatthew G. Knepley const PetscScalar *coords; 446722d0f5cSMatthew G. Knepley PetscInt *dboxes, *boxes; 447cafe43deSMatthew G. Knepley PetscInt n[3] = {10, 10, 10}; 4481d0c6c94SMatthew G. Knepley PetscInt dim, N, cStart, cEnd, cMax, c, i; 449cafe43deSMatthew G. Knepley PetscErrorCode ierr; 450cafe43deSMatthew G. Knepley 451cafe43deSMatthew G. Knepley PetscFunctionBegin; 452cafe43deSMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 453cafe43deSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 454cafe43deSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 4555b3353d8SMatthew G. Knepley if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D"); 456cafe43deSMatthew G. Knepley ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr); 457cafe43deSMatthew G. Knepley ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr); 458cafe43deSMatthew G. Knepley ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr); 459cafe43deSMatthew G. Knepley for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);} 460cafe43deSMatthew G. Knepley ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr); 461cafe43deSMatthew G. Knepley ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr); 462cafe43deSMatthew G. Knepley #if 0 463cafe43deSMatthew G. Knepley /* Could define a custom reduction to merge these */ 464b2566f29SBarry Smith ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr); 465b2566f29SBarry Smith ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr); 466cafe43deSMatthew G. Knepley #endif 467cafe43deSMatthew G. Knepley /* Is there a reason to snap the local bounding box to a division of the global box? */ 468cafe43deSMatthew G. Knepley /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */ 469cafe43deSMatthew G. Knepley /* Create label */ 470cafe43deSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 4711d0c6c94SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 4721d0c6c94SMatthew G. Knepley if (cMax >= 0) cEnd = PetscMin(cEnd, cMax); 473cafe43deSMatthew G. Knepley ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr); 474cafe43deSMatthew G. Knepley ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr); 475722d0f5cSMatthew G. Knepley /* Compute boxes which overlap each cell: http://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */ 476cafe43deSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 477cafe43deSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 47838353de4SMatthew G. Knepley ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr); 479cafe43deSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 480cafe43deSMatthew G. Knepley const PetscReal *h = lbox->h; 481cafe43deSMatthew G. Knepley PetscScalar *ccoords = NULL; 48238353de4SMatthew G. Knepley PetscInt csize = 0; 483cafe43deSMatthew G. Knepley PetscScalar point[3]; 484cafe43deSMatthew G. Knepley PetscInt dlim[6], d, e, i, j, k; 485cafe43deSMatthew G. Knepley 486cafe43deSMatthew G. Knepley /* Find boxes enclosing each vertex */ 48738353de4SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr); 48838353de4SMatthew G. Knepley ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr); 489722d0f5cSMatthew G. Knepley /* Mark cells containing the vertices */ 49038353de4SMatthew G. Knepley for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);} 491cafe43deSMatthew G. Knepley /* Get grid of boxes containing these */ 492cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];} 4932291669eSMatthew G. Knepley for (d = dim; d < 3; ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;} 494cafe43deSMatthew G. Knepley for (e = 1; e < dim+1; ++e) { 495cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) { 496cafe43deSMatthew G. Knepley dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]); 497cafe43deSMatthew G. Knepley dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]); 498cafe43deSMatthew G. Knepley } 499cafe43deSMatthew G. Knepley } 500fea14342SMatthew G. Knepley /* Check for intersection of box with cell */ 501cafe43deSMatthew 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]) { 502cafe43deSMatthew 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]) { 503cafe43deSMatthew 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]) { 504cafe43deSMatthew G. Knepley const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i; 505cafe43deSMatthew G. Knepley PetscScalar cpoint[3]; 506fea14342SMatthew G. Knepley PetscInt cell, edge, ii, jj, kk; 507cafe43deSMatthew G. Knepley 508fea14342SMatthew G. Knepley /* Check whether cell contains any vertex of these subboxes TODO vectorize this */ 509cafe43deSMatthew G. Knepley for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) { 510cafe43deSMatthew G. Knepley for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) { 511cafe43deSMatthew G. Knepley for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) { 512cafe43deSMatthew G. Knepley 513cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr); 514cafe43deSMatthew G. Knepley if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;} 515cafe43deSMatthew G. Knepley } 516cafe43deSMatthew G. Knepley } 517cafe43deSMatthew G. Knepley } 518fea14342SMatthew G. Knepley /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */ 519fea14342SMatthew G. Knepley for (edge = 0; edge < dim+1; ++edge) { 520fea14342SMatthew G. Knepley PetscReal segA[6], segB[6]; 521fea14342SMatthew G. Knepley 522fea14342SMatthew G. Knepley for (d = 0; d < dim; ++d) {segA[d] = PetscRealPart(ccoords[edge*dim+d]); segA[dim+d] = PetscRealPart(ccoords[((edge+1)%(dim+1))*dim+d]);} 523fea14342SMatthew G. Knepley for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) { 5249a128ed2SMatthew G. Knepley if (dim > 2) {segB[2] = PetscRealPart(point[2]); 5259a128ed2SMatthew G. Knepley segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];} 526fea14342SMatthew G. Knepley for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) { 5279a128ed2SMatthew G. Knepley if (dim > 1) {segB[1] = PetscRealPart(point[1]); 5289a128ed2SMatthew G. Knepley segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];} 529fea14342SMatthew G. Knepley for (ii = 0; ii < 2; ++ii) { 530fea14342SMatthew G. Knepley PetscBool intersects; 531fea14342SMatthew G. Knepley 5329a128ed2SMatthew G. Knepley segB[0] = PetscRealPart(point[0]); 5339a128ed2SMatthew G. Knepley segB[dim+0] = PetscRealPart(point[0]) + ii*h[0]; 534fea14342SMatthew G. Knepley ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr); 535fea14342SMatthew G. Knepley if (intersects) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;} 536cafe43deSMatthew G. Knepley } 537cafe43deSMatthew G. Knepley } 538cafe43deSMatthew G. Knepley } 539cafe43deSMatthew G. Knepley } 540fea14342SMatthew G. Knepley } 541fea14342SMatthew G. Knepley } 542fea14342SMatthew G. Knepley } 543fea14342SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr); 544fea14342SMatthew G. Knepley } 545722d0f5cSMatthew G. Knepley ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr); 546cafe43deSMatthew G. Knepley ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr); 547cafe43deSMatthew G. Knepley ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr); 548cafe43deSMatthew G. Knepley *localBox = lbox; 549cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 550cafe43deSMatthew G. Knepley } 551cafe43deSMatthew G. Knepley 552cafe43deSMatthew G. Knepley #undef __FUNCT__ 553ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex" 554*62a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF) 555ccd2543fSMatthew G Knepley { 556cafe43deSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 557953fc75cSMatthew G. Knepley PetscBool hash = mesh->useHashLocation; 5583a93e3b7SToby Isaac PetscInt bs, numPoints, p, numFound, *found = NULL; 5591318edbeSMatthew G. Knepley PetscInt dim, cStart, cEnd, cMax, numCells, c; 560cafe43deSMatthew G. Knepley const PetscInt *boxCells; 5613a93e3b7SToby Isaac PetscSFNode *cells; 562ccd2543fSMatthew G Knepley PetscScalar *a; 5633a93e3b7SToby Isaac PetscMPIInt result; 564ccd2543fSMatthew G Knepley PetscErrorCode ierr; 565ccd2543fSMatthew G Knepley 566ccd2543fSMatthew G Knepley PetscFunctionBegin; 567*62a38674SMatthew G. Knepley if (ltype == DM_POINTLOCATION_NEAREST && !hash) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Nearest point location only supported with grid hashing."); 568cafe43deSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 569cafe43deSMatthew G. Knepley ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr); 5703a93e3b7SToby Isaac ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr); 5713a93e3b7SToby Isaac if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported"); 572cafe43deSMatthew 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); 573ccd2543fSMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 574ccd2543fSMatthew G Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 575ccd2543fSMatthew G Knepley if (cMax >= 0) cEnd = PetscMin(cEnd, cMax); 576ccd2543fSMatthew G Knepley ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr); 577ccd2543fSMatthew G Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 578ccd2543fSMatthew G Knepley numPoints /= bs; 579785e854fSJed Brown ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr); 580953fc75cSMatthew G. Knepley if (hash) { 581ac6ec2abSMatthew G. Knepley if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);} 582cafe43deSMatthew G. Knepley /* Designate the local box for each point */ 583cafe43deSMatthew G. Knepley /* Send points to correct process */ 584cafe43deSMatthew G. Knepley /* Search cells that lie in each subbox */ 585cafe43deSMatthew G. Knepley /* Should we bin points before doing search? */ 586cafe43deSMatthew G. Knepley ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr); 587953fc75cSMatthew G. Knepley } 5883a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; ++p) { 589ccd2543fSMatthew G Knepley const PetscScalar *point = &a[p*bs]; 590953fc75cSMatthew G. Knepley PetscInt dbin[3], bin, cell = -1, cellOffset; 591ccd2543fSMatthew G Knepley 5923a93e3b7SToby Isaac cells[p].rank = -1; 5933a93e3b7SToby Isaac cells[p].index = -1; 594953fc75cSMatthew G. Knepley if (hash) { 595cafe43deSMatthew G. Knepley ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr); 596cafe43deSMatthew G. Knepley /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */ 597cafe43deSMatthew G. Knepley ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr); 598cafe43deSMatthew G. Knepley ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr); 599cafe43deSMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 600cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr); 6013a93e3b7SToby Isaac if (cell >= 0) { 6023a93e3b7SToby Isaac cells[p].rank = 0; 6033a93e3b7SToby Isaac cells[p].index = cell; 6043a93e3b7SToby Isaac numFound++; 6053a93e3b7SToby Isaac break; 606ccd2543fSMatthew G Knepley } 6073a93e3b7SToby Isaac } 608953fc75cSMatthew G. Knepley } else { 609953fc75cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 610953fc75cSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr); 6113a93e3b7SToby Isaac if (cell >= 0) { 6123a93e3b7SToby Isaac cells[p].rank = 0; 6133a93e3b7SToby Isaac cells[p].index = cell; 6143a93e3b7SToby Isaac numFound++; 6153a93e3b7SToby Isaac break; 616953fc75cSMatthew G. Knepley } 617953fc75cSMatthew G. Knepley } 6183a93e3b7SToby Isaac } 619ccd2543fSMatthew G Knepley } 620953fc75cSMatthew G. Knepley if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);} 621*62a38674SMatthew G. Knepley if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) { 622*62a38674SMatthew G. Knepley for (p = 0; p < numPoints; p++) { 623*62a38674SMatthew G. Knepley const PetscScalar *point = &a[p*bs]; 624*62a38674SMatthew G. Knepley PetscReal cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL; 625*62a38674SMatthew G. Knepley PetscInt dbin[3], bin, cell = -1, cellOffset, d; 626*62a38674SMatthew G. Knepley 627*62a38674SMatthew G. Knepley if (cells[p].rank < 0) { 628*62a38674SMatthew G. Knepley ++numFound; 629*62a38674SMatthew G. Knepley ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr); 630*62a38674SMatthew G. Knepley ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr); 631*62a38674SMatthew G. Knepley ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr); 632*62a38674SMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 633*62a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr); 634*62a38674SMatthew G. Knepley for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - point[d]; 635*62a38674SMatthew G. Knepley dist = DMPlex_NormD_Internal(dim, diff); 636*62a38674SMatthew G. Knepley if (dist < distMax) { 637*62a38674SMatthew G. Knepley for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d]; 638*62a38674SMatthew G. Knepley cells[p].rank = 0; 639*62a38674SMatthew G. Knepley cells[p].index = boxCells[c]; 640*62a38674SMatthew G. Knepley distMax = dist; 641*62a38674SMatthew G. Knepley break; 642*62a38674SMatthew G. Knepley } 643*62a38674SMatthew G. Knepley } 644*62a38674SMatthew G. Knepley } 645*62a38674SMatthew G. Knepley } 646*62a38674SMatthew G. Knepley } 647*62a38674SMatthew G. Knepley /* This code is only be relevant when interfaced to parallel point location */ 648cafe43deSMatthew G. Knepley /* Check for highest numbered proc that claims a point (do we care?) */ 6493a93e3b7SToby Isaac if (numFound < numPoints) { 650*62a38674SMatthew G. Knepley if (ltype == DM_POINTLOCATION_NEAREST) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Nearest point location does not support parallel point location."); 6513a93e3b7SToby Isaac ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr); 6523a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; p++) { 6533a93e3b7SToby Isaac if (cells[p].rank >= 0 && cells[p].index >= 0) { 6543a93e3b7SToby Isaac if (numFound < p) { 6553a93e3b7SToby Isaac cells[numFound] = cells[p]; 6563a93e3b7SToby Isaac } 6573a93e3b7SToby Isaac found[numFound++] = p; 6583a93e3b7SToby Isaac } 6593a93e3b7SToby Isaac } 6603a93e3b7SToby Isaac } 661*62a38674SMatthew G. Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 6623a93e3b7SToby Isaac ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr); 663ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 664ccd2543fSMatthew G Knepley } 665ccd2543fSMatthew G Knepley 666ccd2543fSMatthew G Knepley #undef __FUNCT__ 66717fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D_Internal" 66817fe8556SMatthew G. Knepley /* 66917fe8556SMatthew G. Knepley DMPlexComputeProjection2Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 2D 67017fe8556SMatthew G. Knepley */ 6713beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D_Internal(PetscScalar coords[], PetscReal R[]) 67217fe8556SMatthew G. Knepley { 67317fe8556SMatthew G. Knepley const PetscReal x = PetscRealPart(coords[2] - coords[0]); 67417fe8556SMatthew G. Knepley const PetscReal y = PetscRealPart(coords[3] - coords[1]); 6758b49ba18SBarry Smith const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r; 67617fe8556SMatthew G. Knepley 67717fe8556SMatthew G. Knepley PetscFunctionBegin; 6781c99cf0cSGeoffrey Irving R[0] = c; R[1] = -s; 6791c99cf0cSGeoffrey Irving R[2] = s; R[3] = c; 68017fe8556SMatthew G. Knepley coords[0] = 0.0; 6817f07f362SMatthew G. Knepley coords[1] = r; 68217fe8556SMatthew G. Knepley PetscFunctionReturn(0); 68317fe8556SMatthew G. Knepley } 68417fe8556SMatthew G. Knepley 68517fe8556SMatthew G. Knepley #undef __FUNCT__ 68628dbe442SToby Isaac #define __FUNCT__ "DMPlexComputeProjection3Dto1D_Internal" 68728dbe442SToby Isaac /* 68828dbe442SToby Isaac DMPlexComputeProjection3Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 3D 68928dbe442SToby Isaac 69028dbe442SToby Isaac This uses the basis completion described by Frisvad, 69128dbe442SToby Isaac 69228dbe442SToby Isaac http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html 69328dbe442SToby Isaac DOI:10.1080/2165347X.2012.689606 69428dbe442SToby Isaac */ 6953beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D_Internal(PetscScalar coords[], PetscReal R[]) 69628dbe442SToby Isaac { 69728dbe442SToby Isaac PetscReal x = PetscRealPart(coords[3] - coords[0]); 69828dbe442SToby Isaac PetscReal y = PetscRealPart(coords[4] - coords[1]); 69928dbe442SToby Isaac PetscReal z = PetscRealPart(coords[5] - coords[2]); 70028dbe442SToby Isaac PetscReal r = PetscSqrtReal(x*x + y*y + z*z); 70128dbe442SToby Isaac PetscReal rinv = 1. / r; 70228dbe442SToby Isaac PetscFunctionBegin; 70328dbe442SToby Isaac 70428dbe442SToby Isaac x *= rinv; y *= rinv; z *= rinv; 70528dbe442SToby Isaac if (x > 0.) { 70628dbe442SToby Isaac PetscReal inv1pX = 1./ (1. + x); 70728dbe442SToby Isaac 70828dbe442SToby Isaac R[0] = x; R[1] = -y; R[2] = -z; 70928dbe442SToby Isaac R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] = -y*z*inv1pX; 71028dbe442SToby Isaac R[6] = z; R[7] = -y*z*inv1pX; R[8] = 1. - z*z*inv1pX; 71128dbe442SToby Isaac } 71228dbe442SToby Isaac else { 71328dbe442SToby Isaac PetscReal inv1mX = 1./ (1. - x); 71428dbe442SToby Isaac 71528dbe442SToby Isaac R[0] = x; R[1] = z; R[2] = y; 71628dbe442SToby Isaac R[3] = y; R[4] = -y*z*inv1mX; R[5] = 1. - y*y*inv1mX; 71728dbe442SToby Isaac R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] = -y*z*inv1mX; 71828dbe442SToby Isaac } 71928dbe442SToby Isaac coords[0] = 0.0; 72028dbe442SToby Isaac coords[1] = r; 72128dbe442SToby Isaac PetscFunctionReturn(0); 72228dbe442SToby Isaac } 72328dbe442SToby Isaac 72428dbe442SToby Isaac #undef __FUNCT__ 725ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D_Internal" 726ccd2543fSMatthew G Knepley /* 727ccd2543fSMatthew G Knepley DMPlexComputeProjection3Dto2D_Internal - Rewrite coordinates to be the 2D projection of the 3D 728ccd2543fSMatthew G Knepley */ 7293beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D_Internal(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) 730ccd2543fSMatthew G Knepley { 7311ee9d5ecSMatthew G. Knepley PetscReal x1[3], x2[3], n[3], norm; 73299dec3a6SMatthew G. Knepley PetscReal x1p[3], x2p[3], xnp[3]; 7334a217a95SMatthew G. Knepley PetscReal sqrtz, alpha; 734ccd2543fSMatthew G Knepley const PetscInt dim = 3; 73599dec3a6SMatthew G. Knepley PetscInt d, e, p; 736ccd2543fSMatthew G Knepley 737ccd2543fSMatthew G Knepley PetscFunctionBegin; 738ccd2543fSMatthew G Knepley /* 0) Calculate normal vector */ 739ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 7401ee9d5ecSMatthew G. Knepley x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]); 7411ee9d5ecSMatthew G. Knepley x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]); 742ccd2543fSMatthew G Knepley } 743ccd2543fSMatthew G Knepley n[0] = x1[1]*x2[2] - x1[2]*x2[1]; 744ccd2543fSMatthew G Knepley n[1] = x1[2]*x2[0] - x1[0]*x2[2]; 745ccd2543fSMatthew G Knepley n[2] = x1[0]*x2[1] - x1[1]*x2[0]; 7468b49ba18SBarry Smith norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]); 747ccd2543fSMatthew G Knepley n[0] /= norm; 748ccd2543fSMatthew G Knepley n[1] /= norm; 749ccd2543fSMatthew G Knepley n[2] /= norm; 750ccd2543fSMatthew G Knepley /* 1) Take the normal vector and rotate until it is \hat z 751ccd2543fSMatthew G Knepley 752ccd2543fSMatthew G Knepley Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then 753ccd2543fSMatthew G Knepley 754ccd2543fSMatthew G Knepley R = / alpha nx nz alpha ny nz -1/alpha \ 755ccd2543fSMatthew G Knepley | -alpha ny alpha nx 0 | 756ccd2543fSMatthew G Knepley \ nx ny nz / 757ccd2543fSMatthew G Knepley 758ccd2543fSMatthew G Knepley will rotate the normal vector to \hat z 759ccd2543fSMatthew G Knepley */ 7608b49ba18SBarry Smith sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]); 76173868372SMatthew G. Knepley /* Check for n = z */ 76273868372SMatthew G. Knepley if (sqrtz < 1.0e-10) { 7637df32b8bSSanderA const PetscInt s = PetscSign(n[2]); 7647df32b8bSSanderA /* If nz < 0, rotate 180 degrees around x-axis */ 76599dec3a6SMatthew G. Knepley for (p = 3; p < coordSize/3; ++p) { 76699dec3a6SMatthew G. Knepley coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]); 7677df32b8bSSanderA coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s; 76873868372SMatthew G. Knepley } 76999dec3a6SMatthew G. Knepley coords[0] = 0.0; 77099dec3a6SMatthew G. Knepley coords[1] = 0.0; 7717df32b8bSSanderA coords[2] = x1[0]; 7727df32b8bSSanderA coords[3] = x1[1] * s; 7737df32b8bSSanderA coords[4] = x2[0]; 7747df32b8bSSanderA coords[5] = x2[1] * s; 7757df32b8bSSanderA R[0] = 1.0; R[1] = 0.0; R[2] = 0.0; 7767df32b8bSSanderA R[3] = 0.0; R[4] = 1.0 * s; R[5] = 0.0; 7777df32b8bSSanderA R[6] = 0.0; R[7] = 0.0; R[8] = 1.0 * s; 77873868372SMatthew G. Knepley PetscFunctionReturn(0); 77973868372SMatthew G. Knepley } 780da18b5e6SMatthew G Knepley alpha = 1.0/sqrtz; 781ccd2543fSMatthew G Knepley R[0] = alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz; 782ccd2543fSMatthew G Knepley R[3] = -alpha*n[1]; R[4] = alpha*n[0]; R[5] = 0.0; 783ccd2543fSMatthew G Knepley R[6] = n[0]; R[7] = n[1]; R[8] = n[2]; 784ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 785ccd2543fSMatthew G Knepley x1p[d] = 0.0; 786ccd2543fSMatthew G Knepley x2p[d] = 0.0; 787ccd2543fSMatthew G Knepley for (e = 0; e < dim; ++e) { 788ccd2543fSMatthew G Knepley x1p[d] += R[d*dim+e]*x1[e]; 789ccd2543fSMatthew G Knepley x2p[d] += R[d*dim+e]*x2[e]; 790ccd2543fSMatthew G Knepley } 791ccd2543fSMatthew G Knepley } 7928763be8eSMatthew G. Knepley if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 7938763be8eSMatthew G. Knepley if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 794ccd2543fSMatthew G Knepley /* 2) Project to (x, y) */ 79599dec3a6SMatthew G. Knepley for (p = 3; p < coordSize/3; ++p) { 79699dec3a6SMatthew G. Knepley for (d = 0; d < dim; ++d) { 79799dec3a6SMatthew G. Knepley xnp[d] = 0.0; 79899dec3a6SMatthew G. Knepley for (e = 0; e < dim; ++e) { 79999dec3a6SMatthew G. Knepley xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]); 80099dec3a6SMatthew G. Knepley } 80199dec3a6SMatthew G. Knepley if (d < dim-1) coords[p*2+d] = xnp[d]; 80299dec3a6SMatthew G. Knepley } 80399dec3a6SMatthew G. Knepley } 804ccd2543fSMatthew G Knepley coords[0] = 0.0; 805ccd2543fSMatthew G Knepley coords[1] = 0.0; 806ccd2543fSMatthew G Knepley coords[2] = x1p[0]; 807ccd2543fSMatthew G Knepley coords[3] = x1p[1]; 808ccd2543fSMatthew G Knepley coords[4] = x2p[0]; 809ccd2543fSMatthew G Knepley coords[5] = x2p[1]; 8107f07f362SMatthew G. Knepley /* Output R^T which rotates \hat z to the input normal */ 8117f07f362SMatthew G. Knepley for (d = 0; d < dim; ++d) { 8127f07f362SMatthew G. Knepley for (e = d+1; e < dim; ++e) { 8137f07f362SMatthew G. Knepley PetscReal tmp; 8147f07f362SMatthew G. Knepley 8157f07f362SMatthew G. Knepley tmp = R[d*dim+e]; 8167f07f362SMatthew G. Knepley R[d*dim+e] = R[e*dim+d]; 8177f07f362SMatthew G. Knepley R[e*dim+d] = tmp; 8187f07f362SMatthew G. Knepley } 8197f07f362SMatthew G. Knepley } 820ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 821ccd2543fSMatthew G Knepley } 822ccd2543fSMatthew G Knepley 823ccd2543fSMatthew G Knepley #undef __FUNCT__ 824834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal" 8256322fe33SJed Brown PETSC_UNUSED 826834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) 827834e62ceSMatthew G. Knepley { 828834e62ceSMatthew G. Knepley /* Signed volume is 1/2 the determinant 829834e62ceSMatthew G. Knepley 830834e62ceSMatthew G. Knepley | 1 1 1 | 831834e62ceSMatthew G. Knepley | x0 x1 x2 | 832834e62ceSMatthew G. Knepley | y0 y1 y2 | 833834e62ceSMatthew G. Knepley 834834e62ceSMatthew G. Knepley but if x0,y0 is the origin, we have 835834e62ceSMatthew G. Knepley 836834e62ceSMatthew G. Knepley | x1 x2 | 837834e62ceSMatthew G. Knepley | y1 y2 | 838834e62ceSMatthew G. Knepley */ 839834e62ceSMatthew G. Knepley const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1]; 840834e62ceSMatthew G. Knepley const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1]; 841834e62ceSMatthew G. Knepley PetscReal M[4], detM; 842834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; 84386623015SMatthew G. Knepley M[2] = y1; M[3] = y2; 844923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(&detM, M); 845834e62ceSMatthew G. Knepley *vol = 0.5*detM; 8463bc0b13bSBarry Smith (void)PetscLogFlops(5.0); 847834e62ceSMatthew G. Knepley } 848834e62ceSMatthew G. Knepley 849834e62ceSMatthew G. Knepley #undef __FUNCT__ 850834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal" 851834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[]) 852834e62ceSMatthew G. Knepley { 853923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(vol, coords); 854834e62ceSMatthew G. Knepley *vol *= 0.5; 855834e62ceSMatthew G. Knepley } 856834e62ceSMatthew G. Knepley 857834e62ceSMatthew G. Knepley #undef __FUNCT__ 858834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal" 8596322fe33SJed Brown PETSC_UNUSED 860834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) 861834e62ceSMatthew G. Knepley { 862834e62ceSMatthew G. Knepley /* Signed volume is 1/6th of the determinant 863834e62ceSMatthew G. Knepley 864834e62ceSMatthew G. Knepley | 1 1 1 1 | 865834e62ceSMatthew G. Knepley | x0 x1 x2 x3 | 866834e62ceSMatthew G. Knepley | y0 y1 y2 y3 | 867834e62ceSMatthew G. Knepley | z0 z1 z2 z3 | 868834e62ceSMatthew G. Knepley 869834e62ceSMatthew G. Knepley but if x0,y0,z0 is the origin, we have 870834e62ceSMatthew G. Knepley 871834e62ceSMatthew G. Knepley | x1 x2 x3 | 872834e62ceSMatthew G. Knepley | y1 y2 y3 | 873834e62ceSMatthew G. Knepley | z1 z2 z3 | 874834e62ceSMatthew G. Knepley */ 875834e62ceSMatthew G. Knepley const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2]; 876834e62ceSMatthew G. Knepley const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2]; 877834e62ceSMatthew G. Knepley const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2]; 878834e62ceSMatthew G. Knepley PetscReal M[9], detM; 879834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; M[2] = x3; 880834e62ceSMatthew G. Knepley M[3] = y1; M[4] = y2; M[5] = y3; 881834e62ceSMatthew G. Knepley M[6] = z1; M[7] = z2; M[8] = z3; 882923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(&detM, M); 883b7ad821dSMatthew G. Knepley *vol = -0.16666666666666666666666*detM; 8843bc0b13bSBarry Smith (void)PetscLogFlops(10.0); 885834e62ceSMatthew G. Knepley } 886834e62ceSMatthew G. Knepley 887834e62ceSMatthew G. Knepley #undef __FUNCT__ 8880ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal" 8890ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) 8900ec8681fSMatthew G. Knepley { 891923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(vol, coords); 892b7ad821dSMatthew G. Knepley *vol *= -0.16666666666666666666666; 8930ec8681fSMatthew G. Knepley } 8940ec8681fSMatthew G. Knepley 8950ec8681fSMatthew G. Knepley #undef __FUNCT__ 89617fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal" 89717fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 89817fe8556SMatthew G. Knepley { 89917fe8556SMatthew G. Knepley PetscSection coordSection; 90017fe8556SMatthew G. Knepley Vec coordinates; 901a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 9028bf5c034SToby Isaac PetscInt numCoords, d, pStart, pEnd, numSelfCoords = 0; 90317fe8556SMatthew G. Knepley PetscErrorCode ierr; 90417fe8556SMatthew G. Knepley 90517fe8556SMatthew G. Knepley PetscFunctionBegin; 90617fe8556SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 90769d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 9088bf5c034SToby Isaac ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr); 9098bf5c034SToby Isaac if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);} 91017fe8556SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 9118bf5c034SToby Isaac numCoords = numSelfCoords ? numSelfCoords : numCoords; 912adac9986SMatthew G. Knepley if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 9137f07f362SMatthew G. Knepley *detJ = 0.0; 91428dbe442SToby Isaac if (numCoords == 6) { 91528dbe442SToby Isaac const PetscInt dim = 3; 91628dbe442SToby Isaac PetscReal R[9], J0; 91728dbe442SToby Isaac 91828dbe442SToby Isaac if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 91928dbe442SToby Isaac ierr = DMPlexComputeProjection3Dto1D_Internal(coords, R);CHKERRQ(ierr); 92028dbe442SToby Isaac if (J) { 92128dbe442SToby Isaac J0 = 0.5*PetscRealPart(coords[1]); 92228dbe442SToby Isaac J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2]; 92328dbe442SToby Isaac J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5]; 92428dbe442SToby Isaac J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8]; 92528dbe442SToby Isaac DMPlex_Det3D_Internal(detJ, J); 92628dbe442SToby Isaac if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 927adac9986SMatthew G. Knepley } 92828dbe442SToby Isaac } else if (numCoords == 4) { 9297f07f362SMatthew G. Knepley const PetscInt dim = 2; 9307f07f362SMatthew G. Knepley PetscReal R[4], J0; 9317f07f362SMatthew G. Knepley 9327f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 9337f07f362SMatthew G. Knepley ierr = DMPlexComputeProjection2Dto1D_Internal(coords, R);CHKERRQ(ierr); 93417fe8556SMatthew G. Knepley if (J) { 9357f07f362SMatthew G. Knepley J0 = 0.5*PetscRealPart(coords[1]); 9367f07f362SMatthew G. Knepley J[0] = R[0]*J0; J[1] = R[1]; 9377f07f362SMatthew G. Knepley J[2] = R[2]*J0; J[3] = R[3]; 938923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 939923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 940adac9986SMatthew G. Knepley } 9417f07f362SMatthew G. Knepley } else if (numCoords == 2) { 9427f07f362SMatthew G. Knepley const PetscInt dim = 1; 9437f07f362SMatthew G. Knepley 9447f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 9457f07f362SMatthew G. Knepley if (J) { 9467f07f362SMatthew G. Knepley J[0] = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0])); 94717fe8556SMatthew G. Knepley *detJ = J[0]; 9483bc0b13bSBarry Smith ierr = PetscLogFlops(2.0);CHKERRQ(ierr); 9493bc0b13bSBarry Smith if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);} 950adac9986SMatthew G. Knepley } 951796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords); 95217fe8556SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 95317fe8556SMatthew G. Knepley PetscFunctionReturn(0); 95417fe8556SMatthew G. Knepley } 95517fe8556SMatthew G. Knepley 95617fe8556SMatthew G. Knepley #undef __FUNCT__ 957ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal" 958ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 959ccd2543fSMatthew G Knepley { 960ccd2543fSMatthew G Knepley PetscSection coordSection; 961ccd2543fSMatthew G Knepley Vec coordinates; 962a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 9637f07f362SMatthew G. Knepley PetscInt numCoords, d, f, g; 964ccd2543fSMatthew G Knepley PetscErrorCode ierr; 965ccd2543fSMatthew G Knepley 966ccd2543fSMatthew G Knepley PetscFunctionBegin; 967ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 96869d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 969ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 9707f07f362SMatthew G. Knepley *detJ = 0.0; 971ccd2543fSMatthew G Knepley if (numCoords == 9) { 9727f07f362SMatthew G. Knepley const PetscInt dim = 3; 9737f07f362SMatthew 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}; 9747f07f362SMatthew G. Knepley 9757f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 97699dec3a6SMatthew G. Knepley ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr); 9777f07f362SMatthew G. Knepley if (J) { 978b7ad821dSMatthew G. Knepley const PetscInt pdim = 2; 979b7ad821dSMatthew G. Knepley 980b7ad821dSMatthew G. Knepley for (d = 0; d < pdim; d++) { 981b7ad821dSMatthew G. Knepley for (f = 0; f < pdim; f++) { 982b7ad821dSMatthew G. Knepley J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 983ccd2543fSMatthew G Knepley } 9847f07f362SMatthew G. Knepley } 9853bc0b13bSBarry Smith ierr = PetscLogFlops(8.0);CHKERRQ(ierr); 986923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 9877f07f362SMatthew G. Knepley for (d = 0; d < dim; d++) { 9887f07f362SMatthew G. Knepley for (f = 0; f < dim; f++) { 9897f07f362SMatthew G. Knepley J[d*dim+f] = 0.0; 9907f07f362SMatthew G. Knepley for (g = 0; g < dim; g++) { 9917f07f362SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 9927f07f362SMatthew G. Knepley } 9937f07f362SMatthew G. Knepley } 9947f07f362SMatthew G. Knepley } 9953bc0b13bSBarry Smith ierr = PetscLogFlops(18.0);CHKERRQ(ierr); 9967f07f362SMatthew G. Knepley } 997923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 9987f07f362SMatthew G. Knepley } else if (numCoords == 6) { 9997f07f362SMatthew G. Knepley const PetscInt dim = 2; 10007f07f362SMatthew G. Knepley 10017f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1002ccd2543fSMatthew G Knepley if (J) { 1003ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1004ccd2543fSMatthew G Knepley for (f = 0; f < dim; f++) { 1005ccd2543fSMatthew G Knepley J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d])); 1006ccd2543fSMatthew G Knepley } 1007ccd2543fSMatthew G Knepley } 10083bc0b13bSBarry Smith ierr = PetscLogFlops(8.0);CHKERRQ(ierr); 1009923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1010ccd2543fSMatthew G Knepley } 1011923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 1012796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords); 1013ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 1014ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1015ccd2543fSMatthew G Knepley } 1016ccd2543fSMatthew G Knepley 1017ccd2543fSMatthew G Knepley #undef __FUNCT__ 1018ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal" 1019ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1020ccd2543fSMatthew G Knepley { 1021ccd2543fSMatthew G Knepley PetscSection coordSection; 1022ccd2543fSMatthew G Knepley Vec coordinates; 1023a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 10240d29256aSToby Isaac PetscInt numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd; 1025ccd2543fSMatthew G Knepley PetscErrorCode ierr; 1026ccd2543fSMatthew G Knepley 1027ccd2543fSMatthew G Knepley PetscFunctionBegin; 1028ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 102969d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 10300d29256aSToby Isaac ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr); 10310d29256aSToby Isaac if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);} 103299dec3a6SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 103371f58de1SToby Isaac numCoords = numSelfCoords ? numSelfCoords : numCoords; 10347f07f362SMatthew G. Knepley *detJ = 0.0; 103599dec3a6SMatthew G. Knepley if (numCoords == 12) { 103699dec3a6SMatthew G. Knepley const PetscInt dim = 3; 103799dec3a6SMatthew 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}; 103899dec3a6SMatthew G. Knepley 103999dec3a6SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 104099dec3a6SMatthew G. Knepley ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr); 104199dec3a6SMatthew G. Knepley if (J) { 104299dec3a6SMatthew G. Knepley const PetscInt pdim = 2; 104399dec3a6SMatthew G. Knepley 104499dec3a6SMatthew G. Knepley for (d = 0; d < pdim; d++) { 104599dec3a6SMatthew G. Knepley J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 104699dec3a6SMatthew G. Knepley J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 104799dec3a6SMatthew G. Knepley } 10483bc0b13bSBarry Smith ierr = PetscLogFlops(8.0);CHKERRQ(ierr); 1049923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 105099dec3a6SMatthew G. Knepley for (d = 0; d < dim; d++) { 105199dec3a6SMatthew G. Knepley for (f = 0; f < dim; f++) { 105299dec3a6SMatthew G. Knepley J[d*dim+f] = 0.0; 105399dec3a6SMatthew G. Knepley for (g = 0; g < dim; g++) { 105499dec3a6SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 105599dec3a6SMatthew G. Knepley } 105699dec3a6SMatthew G. Knepley } 105799dec3a6SMatthew G. Knepley } 10583bc0b13bSBarry Smith ierr = PetscLogFlops(18.0);CHKERRQ(ierr); 105999dec3a6SMatthew G. Knepley } 1060923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 106171f58de1SToby Isaac } else if (numCoords == 8) { 106299dec3a6SMatthew G. Knepley const PetscInt dim = 2; 106399dec3a6SMatthew G. Knepley 10647f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1065ccd2543fSMatthew G Knepley if (J) { 1066ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 106799dec3a6SMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 106899dec3a6SMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 1069ccd2543fSMatthew G Knepley } 10703bc0b13bSBarry Smith ierr = PetscLogFlops(8.0);CHKERRQ(ierr); 1071923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1072ccd2543fSMatthew G Knepley } 1073923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 1074796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords); 107599dec3a6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 1076ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1077ccd2543fSMatthew G Knepley } 1078ccd2543fSMatthew G Knepley 1079ccd2543fSMatthew G Knepley #undef __FUNCT__ 1080ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal" 1081ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1082ccd2543fSMatthew G Knepley { 1083ccd2543fSMatthew G Knepley PetscSection coordSection; 1084ccd2543fSMatthew G Knepley Vec coordinates; 1085a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1086ccd2543fSMatthew G Knepley const PetscInt dim = 3; 108799dec3a6SMatthew G. Knepley PetscInt d; 1088ccd2543fSMatthew G Knepley PetscErrorCode ierr; 1089ccd2543fSMatthew G Knepley 1090ccd2543fSMatthew G Knepley PetscFunctionBegin; 1091ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 109269d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1093ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 10947f07f362SMatthew G. Knepley *detJ = 0.0; 10957f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1096ccd2543fSMatthew G Knepley if (J) { 1097ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1098f0df753eSMatthew G. Knepley /* I orient with outward face normals */ 1099f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d])); 1100f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 1101f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 1102ccd2543fSMatthew G Knepley } 11033bc0b13bSBarry Smith ierr = PetscLogFlops(18.0);CHKERRQ(ierr); 1104923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1105ccd2543fSMatthew G Knepley } 1106923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 1107ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 1108ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1109ccd2543fSMatthew G Knepley } 1110ccd2543fSMatthew G Knepley 1111ccd2543fSMatthew G Knepley #undef __FUNCT__ 1112ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal" 1113ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1114ccd2543fSMatthew G Knepley { 1115ccd2543fSMatthew G Knepley PetscSection coordSection; 1116ccd2543fSMatthew G Knepley Vec coordinates; 1117a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1118ccd2543fSMatthew G Knepley const PetscInt dim = 3; 1119ccd2543fSMatthew G Knepley PetscInt d; 1120ccd2543fSMatthew G Knepley PetscErrorCode ierr; 1121ccd2543fSMatthew G Knepley 1122ccd2543fSMatthew G Knepley PetscFunctionBegin; 1123ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 112469d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1125ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 11267f07f362SMatthew G. Knepley *detJ = 0.0; 11277f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1128ccd2543fSMatthew G Knepley if (J) { 1129ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1130f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 1131f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 1132f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d])); 1133ccd2543fSMatthew G Knepley } 11343bc0b13bSBarry Smith ierr = PetscLogFlops(18.0);CHKERRQ(ierr); 1135923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1136ccd2543fSMatthew G Knepley } 1137923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 1138ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 1139ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1140ccd2543fSMatthew G Knepley } 1141ccd2543fSMatthew G Knepley 1142ccd2543fSMatthew G Knepley #undef __FUNCT__ 11438e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM" 1144ccd2543fSMatthew G Knepley /*@C 11458e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell 1146ccd2543fSMatthew G Knepley 1147ccd2543fSMatthew G Knepley Collective on DM 1148ccd2543fSMatthew G Knepley 1149ccd2543fSMatthew G Knepley Input Arguments: 1150ccd2543fSMatthew G Knepley + dm - the DM 1151ccd2543fSMatthew G Knepley - cell - the cell 1152ccd2543fSMatthew G Knepley 1153ccd2543fSMatthew G Knepley Output Arguments: 1154ccd2543fSMatthew G Knepley + v0 - the translation part of this affine transform 1155ccd2543fSMatthew G Knepley . J - the Jacobian of the transform from the reference element 1156ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian 1157ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant 1158ccd2543fSMatthew G Knepley 1159ccd2543fSMatthew G Knepley Level: advanced 1160ccd2543fSMatthew G Knepley 1161ccd2543fSMatthew G Knepley Fortran Notes: 1162ccd2543fSMatthew G Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 1163ccd2543fSMatthew G Knepley include petsc.h90 in your code. 1164ccd2543fSMatthew G Knepley 11658e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec() 1166ccd2543fSMatthew G Knepley @*/ 11678e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 1168ccd2543fSMatthew G Knepley { 116949dc4407SMatthew G. Knepley PetscInt depth, dim, coneSize; 1170ccd2543fSMatthew G Knepley PetscErrorCode ierr; 1171ccd2543fSMatthew G Knepley 1172ccd2543fSMatthew G Knepley PetscFunctionBegin; 1173139a35ccSMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 1174ccd2543fSMatthew G Knepley ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 117549dc4407SMatthew G. Knepley if (depth == 1) { 11768e0841e0SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 11778e0841e0SMatthew G. Knepley } else { 11788e0841e0SMatthew G. Knepley DMLabel depth; 11798e0841e0SMatthew G. Knepley 11808e0841e0SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 11818e0841e0SMatthew G. Knepley ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr); 11828e0841e0SMatthew G. Knepley } 1183ccd2543fSMatthew G Knepley switch (dim) { 118417fe8556SMatthew G. Knepley case 1: 118517fe8556SMatthew G. Knepley ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 118617fe8556SMatthew G. Knepley break; 1187ccd2543fSMatthew G Knepley case 2: 1188ccd2543fSMatthew G Knepley switch (coneSize) { 1189ccd2543fSMatthew G Knepley case 3: 1190ccd2543fSMatthew G Knepley ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 1191ccd2543fSMatthew G Knepley break; 1192ccd2543fSMatthew G Knepley case 4: 1193ccd2543fSMatthew G Knepley ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 1194ccd2543fSMatthew G Knepley break; 1195ccd2543fSMatthew G Knepley default: 11968e0841e0SMatthew G. Knepley SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 1197ccd2543fSMatthew G Knepley } 1198ccd2543fSMatthew G Knepley break; 1199ccd2543fSMatthew G Knepley case 3: 1200ccd2543fSMatthew G Knepley switch (coneSize) { 1201ccd2543fSMatthew G Knepley case 4: 1202ccd2543fSMatthew G Knepley ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 1203ccd2543fSMatthew G Knepley break; 12048e0841e0SMatthew G. Knepley case 6: /* Faces */ 12058e0841e0SMatthew G. Knepley case 8: /* Vertices */ 1206ccd2543fSMatthew G Knepley ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 1207ccd2543fSMatthew G Knepley break; 1208ccd2543fSMatthew G Knepley default: 12098e0841e0SMatthew G. Knepley SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 1210ccd2543fSMatthew G Knepley } 1211ccd2543fSMatthew G Knepley break; 1212ccd2543fSMatthew G Knepley default: 1213ccd2543fSMatthew G Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 1214ccd2543fSMatthew G Knepley } 12158e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 12168e0841e0SMatthew G. Knepley } 12178e0841e0SMatthew G. Knepley 12188e0841e0SMatthew G. Knepley #undef __FUNCT__ 12198e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal" 12208e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 12218e0841e0SMatthew G. Knepley { 12228e0841e0SMatthew G. Knepley PetscQuadrature quad; 12238e0841e0SMatthew G. Knepley PetscSection coordSection; 12248e0841e0SMatthew G. Knepley Vec coordinates; 12258e0841e0SMatthew G. Knepley PetscScalar *coords = NULL; 12268e0841e0SMatthew G. Knepley const PetscReal *quadPoints; 12278e0841e0SMatthew G. Knepley PetscReal *basisDer; 12288e0841e0SMatthew G. Knepley PetscInt dim, cdim, pdim, qdim, Nq, numCoords, d, q; 12298e0841e0SMatthew G. Knepley PetscErrorCode ierr; 12308e0841e0SMatthew G. Knepley 12318e0841e0SMatthew G. Knepley PetscFunctionBegin; 12328e0841e0SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 12338e0841e0SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 12348e0841e0SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 12358e0841e0SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 12368e0841e0SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr); 12378e0841e0SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 1238954b1791SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr); 12398e0841e0SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr); 12408e0841e0SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 12418e0841e0SMatthew G. Knepley *detJ = 0.0; 12428e0841e0SMatthew G. Knepley if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim); 12438e0841e0SMatthew 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); 12448e0841e0SMatthew G. Knepley if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);} 12458e0841e0SMatthew G. Knepley if (J) { 12460790e268SMatthew G. Knepley ierr = PetscMemzero(J, Nq*cdim*dim*sizeof(PetscReal));CHKERRQ(ierr); 12478e0841e0SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 12488e0841e0SMatthew G. Knepley PetscInt i, j, k, c, r; 12498e0841e0SMatthew G. Knepley 12508e0841e0SMatthew G. Knepley /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */ 12518e0841e0SMatthew G. Knepley for (k = 0; k < pdim; ++k) 12528e0841e0SMatthew G. Knepley for (j = 0; j < dim; ++j) 12538e0841e0SMatthew G. Knepley for (i = 0; i < cdim; ++i) 125471d6e60fSMatthew G. Knepley J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]); 12553bc0b13bSBarry Smith ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr); 12568e0841e0SMatthew G. Knepley if (cdim > dim) { 12578e0841e0SMatthew G. Knepley for (c = dim; c < cdim; ++c) 12588e0841e0SMatthew G. Knepley for (r = 0; r < cdim; ++r) 12598e0841e0SMatthew G. Knepley J[r*cdim+c] = r == c ? 1.0 : 0.0; 12608e0841e0SMatthew G. Knepley } 12618e0841e0SMatthew G. Knepley switch (cdim) { 12628e0841e0SMatthew G. Knepley case 3: 12638e0841e0SMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 12648e0841e0SMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 126517fe8556SMatthew G. Knepley break; 126649dc4407SMatthew G. Knepley case 2: 12678e0841e0SMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 12688e0841e0SMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 126949dc4407SMatthew G. Knepley break; 12708e0841e0SMatthew G. Knepley case 1: 12718e0841e0SMatthew G. Knepley *detJ = J[0]; 12728e0841e0SMatthew G. Knepley if (invJ) invJ[0] = 1.0/J[0]; 127349dc4407SMatthew G. Knepley } 127449dc4407SMatthew G. Knepley } 12758e0841e0SMatthew G. Knepley } 12768e0841e0SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 12778e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 12788e0841e0SMatthew G. Knepley } 12798e0841e0SMatthew G. Knepley 12808e0841e0SMatthew G. Knepley #undef __FUNCT__ 12818e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM" 12828e0841e0SMatthew G. Knepley /*@C 12838e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell 12848e0841e0SMatthew G. Knepley 12858e0841e0SMatthew G. Knepley Collective on DM 12868e0841e0SMatthew G. Knepley 12878e0841e0SMatthew G. Knepley Input Arguments: 12888e0841e0SMatthew G. Knepley + dm - the DM 12898e0841e0SMatthew G. Knepley . cell - the cell 12908e0841e0SMatthew G. Knepley - fe - the finite element containing the quadrature 12918e0841e0SMatthew G. Knepley 12928e0841e0SMatthew G. Knepley Output Arguments: 12938e0841e0SMatthew G. Knepley + v0 - the translation part of this transform 12948e0841e0SMatthew G. Knepley . J - the Jacobian of the transform from the reference element at each quadrature point 12958e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point 12968e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point 12978e0841e0SMatthew G. Knepley 12988e0841e0SMatthew G. Knepley Level: advanced 12998e0841e0SMatthew G. Knepley 13008e0841e0SMatthew G. Knepley Fortran Notes: 13018e0841e0SMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 13028e0841e0SMatthew G. Knepley include petsc.h90 in your code. 13038e0841e0SMatthew G. Knepley 13048e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 13058e0841e0SMatthew G. Knepley @*/ 13068e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 13078e0841e0SMatthew G. Knepley { 13088e0841e0SMatthew G. Knepley PetscErrorCode ierr; 13098e0841e0SMatthew G. Knepley 13108e0841e0SMatthew G. Knepley PetscFunctionBegin; 13118e0841e0SMatthew G. Knepley if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);} 13128e0841e0SMatthew G. Knepley else {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);} 1313ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1314ccd2543fSMatthew G Knepley } 1315834e62ceSMatthew G. Knepley 1316834e62ceSMatthew G. Knepley #undef __FUNCT__ 1317cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal" 1318011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1319cc08537eSMatthew G. Knepley { 1320cc08537eSMatthew G. Knepley PetscSection coordSection; 1321cc08537eSMatthew G. Knepley Vec coordinates; 1322a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 132306e2781eSMatthew G. Knepley PetscScalar tmp[2]; 1324cc08537eSMatthew G. Knepley PetscInt coordSize; 1325cc08537eSMatthew G. Knepley PetscErrorCode ierr; 1326cc08537eSMatthew G. Knepley 1327cc08537eSMatthew G. Knepley PetscFunctionBegin; 1328cc08537eSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 132969d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1330cc08537eSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1331011ea5d8SMatthew G. Knepley if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now"); 13322e17dfb7SMatthew G. Knepley ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr); 1333cc08537eSMatthew G. Knepley if (centroid) { 133406e2781eSMatthew G. Knepley centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]); 133506e2781eSMatthew G. Knepley centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]); 1336cc08537eSMatthew G. Knepley } 1337cc08537eSMatthew G. Knepley if (normal) { 1338a60a936bSMatthew G. Knepley PetscReal norm; 1339a60a936bSMatthew G. Knepley 134006e2781eSMatthew G. Knepley normal[0] = -PetscRealPart(coords[1] - tmp[1]); 134106e2781eSMatthew G. Knepley normal[1] = PetscRealPart(coords[0] - tmp[0]); 1342a60a936bSMatthew G. Knepley norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]); 1343a60a936bSMatthew G. Knepley normal[0] /= norm; 1344a60a936bSMatthew G. Knepley normal[1] /= norm; 1345cc08537eSMatthew G. Knepley } 1346cc08537eSMatthew G. Knepley if (vol) { 134706e2781eSMatthew G. Knepley *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1]))); 1348cc08537eSMatthew G. Knepley } 1349cc08537eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1350cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 1351cc08537eSMatthew G. Knepley } 1352cc08537eSMatthew G. Knepley 1353cc08537eSMatthew G. Knepley #undef __FUNCT__ 1354cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal" 1355cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */ 1356011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1357cc08537eSMatthew G. Knepley { 1358cc08537eSMatthew G. Knepley PetscSection coordSection; 1359cc08537eSMatthew G. Knepley Vec coordinates; 1360cc08537eSMatthew G. Knepley PetscScalar *coords = NULL; 13610a1d6728SMatthew G. Knepley PetscReal vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9]; 13620a1d6728SMatthew G. Knepley PetscInt tdim = 2, coordSize, numCorners, p, d, e; 1363cc08537eSMatthew G. Knepley PetscErrorCode ierr; 1364cc08537eSMatthew G. Knepley 1365cc08537eSMatthew G. Knepley PetscFunctionBegin; 1366cc08537eSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 13670a1d6728SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr); 136869d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1369cc08537eSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 13700bce18caSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 1371ceee4971SMatthew G. Knepley if (dim > 2 && centroid) { 1372ceee4971SMatthew G. Knepley v0[0] = PetscRealPart(coords[0]); 1373ceee4971SMatthew G. Knepley v0[1] = PetscRealPart(coords[1]); 1374ceee4971SMatthew G. Knepley v0[2] = PetscRealPart(coords[2]); 1375ceee4971SMatthew G. Knepley } 1376011ea5d8SMatthew G. Knepley if (normal) { 1377011ea5d8SMatthew G. Knepley if (dim > 2) { 13781ee9d5ecSMatthew G. Knepley const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]); 13791ee9d5ecSMatthew G. Knepley const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]); 13801ee9d5ecSMatthew G. Knepley const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]); 13810a1d6728SMatthew G. Knepley PetscReal norm; 13820a1d6728SMatthew G. Knepley 13830a1d6728SMatthew G. Knepley normal[0] = y0*z1 - z0*y1; 13840a1d6728SMatthew G. Knepley normal[1] = z0*x1 - x0*z1; 13850a1d6728SMatthew G. Knepley normal[2] = x0*y1 - y0*x1; 13868b49ba18SBarry Smith norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]); 13870a1d6728SMatthew G. Knepley normal[0] /= norm; 13880a1d6728SMatthew G. Knepley normal[1] /= norm; 13890a1d6728SMatthew G. Knepley normal[2] /= norm; 1390011ea5d8SMatthew G. Knepley } else { 1391011ea5d8SMatthew G. Knepley for (d = 0; d < dim; ++d) normal[d] = 0.0; 1392011ea5d8SMatthew G. Knepley } 1393011ea5d8SMatthew G. Knepley } 139499dec3a6SMatthew G. Knepley if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D_Internal(coordSize, coords, R);CHKERRQ(ierr);} 13950a1d6728SMatthew G. Knepley for (p = 0; p < numCorners; ++p) { 13960a1d6728SMatthew G. Knepley /* Need to do this copy to get types right */ 13970a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 13981ee9d5ecSMatthew G. Knepley ctmp[d] = PetscRealPart(coords[p*tdim+d]); 13991ee9d5ecSMatthew G. Knepley ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]); 14000a1d6728SMatthew G. Knepley } 14010a1d6728SMatthew G. Knepley Volume_Triangle_Origin_Internal(&vtmp, ctmp); 14020a1d6728SMatthew G. Knepley vsum += vtmp; 14030a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 14040a1d6728SMatthew G. Knepley csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp; 14050a1d6728SMatthew G. Knepley } 14060a1d6728SMatthew G. Knepley } 14070a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 14080a1d6728SMatthew G. Knepley csum[d] /= (tdim+1)*vsum; 14090a1d6728SMatthew G. Knepley } 14100a1d6728SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1411ee6bbdb2SSatish Balay if (vol) *vol = PetscAbsReal(vsum); 14120a1d6728SMatthew G. Knepley if (centroid) { 14130a1d6728SMatthew G. Knepley if (dim > 2) { 14140a1d6728SMatthew G. Knepley for (d = 0; d < dim; ++d) { 14150a1d6728SMatthew G. Knepley centroid[d] = v0[d]; 14160a1d6728SMatthew G. Knepley for (e = 0; e < dim; ++e) { 14170a1d6728SMatthew G. Knepley centroid[d] += R[d*dim+e]*csum[e]; 14180a1d6728SMatthew G. Knepley } 14190a1d6728SMatthew G. Knepley } 14200a1d6728SMatthew G. Knepley } else for (d = 0; d < dim; ++d) centroid[d] = csum[d]; 14210a1d6728SMatthew G. Knepley } 1422cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 1423cc08537eSMatthew G. Knepley } 1424cc08537eSMatthew G. Knepley 1425cc08537eSMatthew G. Knepley #undef __FUNCT__ 14260ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal" 14270ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */ 1428011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 14290ec8681fSMatthew G. Knepley { 14300ec8681fSMatthew G. Knepley PetscSection coordSection; 14310ec8681fSMatthew G. Knepley Vec coordinates; 14320ec8681fSMatthew G. Knepley PetscScalar *coords = NULL; 143386623015SMatthew G. Knepley PetscReal vsum = 0.0, vtmp, coordsTmp[3*3]; 1434a7df9edeSMatthew G. Knepley const PetscInt *faces, *facesO; 14350ec8681fSMatthew G. Knepley PetscInt numFaces, f, coordSize, numCorners, p, d; 14360ec8681fSMatthew G. Knepley PetscErrorCode ierr; 14370ec8681fSMatthew G. Knepley 14380ec8681fSMatthew G. Knepley PetscFunctionBegin; 1439f6dae198SJed Brown if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim); 14400ec8681fSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 144169d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 14420ec8681fSMatthew G. Knepley 1443d9a81ebdSMatthew G. Knepley if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0; 14440ec8681fSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr); 14450ec8681fSMatthew G. Knepley ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr); 1446a7df9edeSMatthew G. Knepley ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr); 14470ec8681fSMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 1448011ea5d8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 14490ec8681fSMatthew G. Knepley numCorners = coordSize/dim; 14500ec8681fSMatthew G. Knepley switch (numCorners) { 14510ec8681fSMatthew G. Knepley case 3: 14520ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 14531ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 14541ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 14551ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]); 14560ec8681fSMatthew G. Knepley } 14570ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1458a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 14590ec8681fSMatthew G. Knepley vsum += vtmp; 14604f25033aSJed Brown if (centroid) { /* Centroid of OABC = (a+b+c)/4 */ 14610ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 14621ee9d5ecSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 14630ec8681fSMatthew G. Knepley } 14640ec8681fSMatthew G. Knepley } 14650ec8681fSMatthew G. Knepley break; 14660ec8681fSMatthew G. Knepley case 4: 14670ec8681fSMatthew G. Knepley /* DO FOR PYRAMID */ 14680ec8681fSMatthew G. Knepley /* First tet */ 14690ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 14701ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 14711ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 14721ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 14730ec8681fSMatthew G. Knepley } 14740ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1475a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 14760ec8681fSMatthew G. Knepley vsum += vtmp; 14770ec8681fSMatthew G. Knepley if (centroid) { 14780ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 14790ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 14800ec8681fSMatthew G. Knepley } 14810ec8681fSMatthew G. Knepley } 14820ec8681fSMatthew G. Knepley /* Second tet */ 14830ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 14841ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]); 14851ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]); 14861ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 14870ec8681fSMatthew G. Knepley } 14880ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1489a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 14900ec8681fSMatthew G. Knepley vsum += vtmp; 14910ec8681fSMatthew G. Knepley if (centroid) { 14920ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 14930ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 14940ec8681fSMatthew G. Knepley } 14950ec8681fSMatthew G. Knepley } 14960ec8681fSMatthew G. Knepley break; 14970ec8681fSMatthew G. Knepley default: 1498796f034aSJed Brown SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners); 14990ec8681fSMatthew G. Knepley } 15004f25033aSJed Brown ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 15010ec8681fSMatthew G. Knepley } 15028763be8eSMatthew G. Knepley if (vol) *vol = PetscAbsReal(vsum); 15030ec8681fSMatthew G. Knepley if (normal) for (d = 0; d < dim; ++d) normal[d] = 0.0; 1504d9a81ebdSMatthew G. Knepley if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4); 15050ec8681fSMatthew G. Knepley PetscFunctionReturn(0); 15060ec8681fSMatthew G. Knepley } 15070ec8681fSMatthew G. Knepley 15080ec8681fSMatthew G. Knepley #undef __FUNCT__ 1509834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM" 1510834e62ceSMatthew G. Knepley /*@C 1511834e62ceSMatthew G. Knepley DMPlexComputeCellGeometryFVM - Compute the volume for a given cell 1512834e62ceSMatthew G. Knepley 1513834e62ceSMatthew G. Knepley Collective on DM 1514834e62ceSMatthew G. Knepley 1515834e62ceSMatthew G. Knepley Input Arguments: 1516834e62ceSMatthew G. Knepley + dm - the DM 1517834e62ceSMatthew G. Knepley - cell - the cell 1518834e62ceSMatthew G. Knepley 1519834e62ceSMatthew G. Knepley Output Arguments: 1520834e62ceSMatthew G. Knepley + volume - the cell volume 1521cc08537eSMatthew G. Knepley . centroid - the cell centroid 1522cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate 1523834e62ceSMatthew G. Knepley 1524834e62ceSMatthew G. Knepley Level: advanced 1525834e62ceSMatthew G. Knepley 1526834e62ceSMatthew G. Knepley Fortran Notes: 1527834e62ceSMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 1528834e62ceSMatthew G. Knepley include petsc.h90 in your code. 1529834e62ceSMatthew G. Knepley 153069d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 1531834e62ceSMatthew G. Knepley @*/ 1532cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1533834e62ceSMatthew G. Knepley { 15340ec8681fSMatthew G. Knepley PetscInt depth, dim; 1535834e62ceSMatthew G. Knepley PetscErrorCode ierr; 1536834e62ceSMatthew G. Knepley 1537834e62ceSMatthew G. Knepley PetscFunctionBegin; 1538834e62ceSMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 1539c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1540834e62ceSMatthew G. Knepley if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated"); 1541834e62ceSMatthew G. Knepley /* We need to keep a pointer to the depth label */ 1542c58f1c22SToby Isaac ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr); 1543834e62ceSMatthew G. Knepley /* Cone size is now the number of faces */ 1544011ea5d8SMatthew G. Knepley switch (depth) { 1545cc08537eSMatthew G. Knepley case 1: 1546011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1547cc08537eSMatthew G. Knepley break; 1548834e62ceSMatthew G. Knepley case 2: 1549011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1550834e62ceSMatthew G. Knepley break; 1551834e62ceSMatthew G. Knepley case 3: 1552011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1553834e62ceSMatthew G. Knepley break; 1554834e62ceSMatthew G. Knepley default: 1555834e62ceSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 1556834e62ceSMatthew G. Knepley } 1557834e62ceSMatthew G. Knepley PetscFunctionReturn(0); 1558834e62ceSMatthew G. Knepley } 1559113c68e6SMatthew G. Knepley 1560113c68e6SMatthew G. Knepley #undef __FUNCT__ 1561c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM" 1562c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */ 1563c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom) 1564c0d900a5SMatthew G. Knepley { 1565c0d900a5SMatthew G. Knepley DM dmCell; 1566c0d900a5SMatthew G. Knepley Vec coordinates; 1567c0d900a5SMatthew G. Knepley PetscSection coordSection, sectionCell; 1568c0d900a5SMatthew G. Knepley PetscScalar *cgeom; 1569c0d900a5SMatthew G. Knepley PetscInt cStart, cEnd, cMax, c; 1570c0d900a5SMatthew G. Knepley PetscErrorCode ierr; 1571c0d900a5SMatthew G. Knepley 1572c0d900a5SMatthew G. Knepley PetscFunctionBegin; 1573c0d900a5SMatthew G. Knepley ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1574c0d900a5SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1575c0d900a5SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1576c0d900a5SMatthew G. Knepley ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1577c0d900a5SMatthew G. Knepley ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1578c0d900a5SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1579c0d900a5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1580c0d900a5SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 1581c0d900a5SMatthew G. Knepley cEnd = cMax < 0 ? cEnd : cMax; 1582c0d900a5SMatthew G. Knepley ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 1583c0d900a5SMatthew G. Knepley /* TODO This needs to be multiplied by Nq for non-affine */ 15849e5edeeeSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1585c0d900a5SMatthew G. Knepley ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1586c0d900a5SMatthew G. Knepley ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1587c0d900a5SMatthew G. Knepley ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1588c0d900a5SMatthew G. Knepley ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 1589c0d900a5SMatthew G. Knepley ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1590c0d900a5SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1591c0d900a5SMatthew G. Knepley PetscFECellGeom *cg; 1592c0d900a5SMatthew G. Knepley 1593c0d900a5SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1594c0d900a5SMatthew G. Knepley ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1595c0d900a5SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr); 1596c0d900a5SMatthew G. Knepley if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c); 1597c0d900a5SMatthew G. Knepley } 1598c0d900a5SMatthew G. Knepley ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1599c0d900a5SMatthew G. Knepley ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 1600c0d900a5SMatthew G. Knepley PetscFunctionReturn(0); 1601c0d900a5SMatthew G. Knepley } 1602c0d900a5SMatthew G. Knepley 1603c0d900a5SMatthew G. Knepley #undef __FUNCT__ 1604113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM" 1605891a9168SMatthew G. Knepley /*@ 1606891a9168SMatthew G. Knepley DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method 1607891a9168SMatthew G. Knepley 1608891a9168SMatthew G. Knepley Input Parameter: 1609891a9168SMatthew G. Knepley . dm - The DM 1610891a9168SMatthew G. Knepley 1611891a9168SMatthew G. Knepley Output Parameters: 1612891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data 1613891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data 1614891a9168SMatthew G. Knepley 1615891a9168SMatthew G. Knepley Level: developer 1616891a9168SMatthew G. Knepley 1617891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM() 1618891a9168SMatthew G. Knepley @*/ 1619113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) 1620113c68e6SMatthew G. Knepley { 1621113c68e6SMatthew G. Knepley DM dmFace, dmCell; 1622113c68e6SMatthew G. Knepley DMLabel ghostLabel; 1623113c68e6SMatthew G. Knepley PetscSection sectionFace, sectionCell; 1624113c68e6SMatthew G. Knepley PetscSection coordSection; 1625113c68e6SMatthew G. Knepley Vec coordinates; 1626113c68e6SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 1627113c68e6SMatthew G. Knepley PetscReal minradius, gminradius; 1628113c68e6SMatthew G. Knepley PetscInt dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f; 1629113c68e6SMatthew G. Knepley PetscErrorCode ierr; 1630113c68e6SMatthew G. Knepley 1631113c68e6SMatthew G. Knepley PetscFunctionBegin; 1632113c68e6SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1633113c68e6SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1634113c68e6SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1635113c68e6SMatthew G. Knepley /* Make cell centroids and volumes */ 1636113c68e6SMatthew G. Knepley ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1637113c68e6SMatthew G. Knepley ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1638113c68e6SMatthew G. Knepley ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1639113c68e6SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1640113c68e6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1641113c68e6SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1642113c68e6SMatthew G. Knepley ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 16439e5edeeeSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1644113c68e6SMatthew G. Knepley ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1645113c68e6SMatthew G. Knepley ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1646113c68e6SMatthew G. Knepley ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1647113c68e6SMatthew G. Knepley ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 164806348e87SToby Isaac if (cEndInterior < 0) { 164906348e87SToby Isaac cEndInterior = cEnd; 165006348e87SToby Isaac } 1651113c68e6SMatthew G. Knepley ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1652113c68e6SMatthew G. Knepley for (c = cStart; c < cEndInterior; ++c) { 1653113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 1654113c68e6SMatthew G. Knepley 1655113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1656113c68e6SMatthew G. Knepley ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1657113c68e6SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr); 1658113c68e6SMatthew G. Knepley } 1659113c68e6SMatthew G. Knepley /* Compute face normals and minimum cell radius */ 1660113c68e6SMatthew G. Knepley ierr = DMClone(dm, &dmFace);CHKERRQ(ierr); 1661113c68e6SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionFace);CHKERRQ(ierr); 1662113c68e6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1663113c68e6SMatthew G. Knepley ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr); 16649e5edeeeSMatthew G. Knepley for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1665113c68e6SMatthew G. Knepley ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr); 1666113c68e6SMatthew G. Knepley ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr); 1667113c68e6SMatthew G. Knepley ierr = PetscSectionDestroy(§ionFace);CHKERRQ(ierr); 1668113c68e6SMatthew G. Knepley ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr); 1669113c68e6SMatthew G. Knepley ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr); 1670c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1671113c68e6SMatthew G. Knepley minradius = PETSC_MAX_REAL; 1672113c68e6SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 1673113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 1674113c68e6SMatthew G. Knepley PetscReal area; 167550d63984SToby Isaac PetscInt ghost = -1, d, numChildren; 1676113c68e6SMatthew G. Knepley 16779ac3fadcSMatthew G. Knepley if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);} 167850d63984SToby Isaac ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr); 167950d63984SToby Isaac if (ghost >= 0 || numChildren) continue; 1680113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr); 1681113c68e6SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr); 1682113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] *= area; 1683113c68e6SMatthew G. Knepley /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */ 1684113c68e6SMatthew G. Knepley { 1685113c68e6SMatthew G. Knepley PetscFVCellGeom *cL, *cR; 168606348e87SToby Isaac PetscInt ncells; 1687113c68e6SMatthew G. Knepley const PetscInt *cells; 1688113c68e6SMatthew G. Knepley PetscReal *lcentroid, *rcentroid; 16890453c0cdSMatthew G. Knepley PetscReal l[3], r[3], v[3]; 1690113c68e6SMatthew G. Knepley 1691113c68e6SMatthew G. Knepley ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr); 169206348e87SToby Isaac ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr); 1693113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr); 1694113c68e6SMatthew G. Knepley lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid; 169506348e87SToby Isaac if (ncells > 1) { 169606348e87SToby Isaac ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr); 1697113c68e6SMatthew G. Knepley rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid; 169806348e87SToby Isaac } 169906348e87SToby Isaac else { 170006348e87SToby Isaac rcentroid = fg->centroid; 170106348e87SToby Isaac } 17022e17dfb7SMatthew G. Knepley ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr); 17032e17dfb7SMatthew G. Knepley ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr); 17040453c0cdSMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, l, r, v); 1705113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) { 1706113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d]; 1707113c68e6SMatthew G. Knepley } 1708113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) { 1709113c68e6SMatthew 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]); 1710113c68e6SMatthew 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]); 1711113c68e6SMatthew G. Knepley SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f); 1712113c68e6SMatthew G. Knepley } 1713113c68e6SMatthew G. Knepley if (cells[0] < cEndInterior) { 1714113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v); 1715113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 1716113c68e6SMatthew G. Knepley } 171706348e87SToby Isaac if (ncells > 1 && cells[1] < cEndInterior) { 1718113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v); 1719113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 1720113c68e6SMatthew G. Knepley } 1721113c68e6SMatthew G. Knepley } 1722113c68e6SMatthew G. Knepley } 1723b2566f29SBarry Smith ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 1724113c68e6SMatthew G. Knepley ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr); 1725113c68e6SMatthew G. Knepley /* Compute centroids of ghost cells */ 1726113c68e6SMatthew G. Knepley for (c = cEndInterior; c < cEnd; ++c) { 1727113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 1728113c68e6SMatthew G. Knepley const PetscInt *cone, *support; 1729113c68e6SMatthew G. Knepley PetscInt coneSize, supportSize, s; 1730113c68e6SMatthew G. Knepley 1731113c68e6SMatthew G. Knepley ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr); 1732113c68e6SMatthew G. Knepley if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize); 1733113c68e6SMatthew G. Knepley ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr); 1734113c68e6SMatthew G. Knepley ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr); 173550d63984SToby Isaac if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize); 1736113c68e6SMatthew G. Knepley ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr); 1737113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr); 1738113c68e6SMatthew G. Knepley for (s = 0; s < 2; ++s) { 1739113c68e6SMatthew G. Knepley /* Reflect ghost centroid across plane of face */ 1740113c68e6SMatthew G. Knepley if (support[s] == c) { 1741640bce14SSatish Balay PetscFVCellGeom *ci; 1742113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 1743113c68e6SMatthew G. Knepley PetscReal c2f[3], a; 1744113c68e6SMatthew G. Knepley 1745113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr); 1746113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */ 1747113c68e6SMatthew G. Knepley a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal); 1748113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr); 1749113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid); 1750113c68e6SMatthew G. Knepley cg->volume = ci->volume; 1751113c68e6SMatthew G. Knepley } 1752113c68e6SMatthew G. Knepley } 1753113c68e6SMatthew G. Knepley } 1754113c68e6SMatthew G. Knepley ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr); 1755113c68e6SMatthew G. Knepley ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1756113c68e6SMatthew G. Knepley ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 1757113c68e6SMatthew G. Knepley ierr = DMDestroy(&dmFace);CHKERRQ(ierr); 1758113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 1759113c68e6SMatthew G. Knepley } 1760113c68e6SMatthew G. Knepley 1761113c68e6SMatthew G. Knepley #undef __FUNCT__ 1762113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius" 1763113c68e6SMatthew G. Knepley /*@C 1764113c68e6SMatthew G. Knepley DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face 1765113c68e6SMatthew G. Knepley 1766113c68e6SMatthew G. Knepley Not collective 1767113c68e6SMatthew G. Knepley 1768113c68e6SMatthew G. Knepley Input Argument: 1769113c68e6SMatthew G. Knepley . dm - the DM 1770113c68e6SMatthew G. Knepley 1771113c68e6SMatthew G. Knepley Output Argument: 1772113c68e6SMatthew G. Knepley . minradius - the minium cell radius 1773113c68e6SMatthew G. Knepley 1774113c68e6SMatthew G. Knepley Level: developer 1775113c68e6SMatthew G. Knepley 1776113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates() 1777113c68e6SMatthew G. Knepley @*/ 1778113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) 1779113c68e6SMatthew G. Knepley { 1780113c68e6SMatthew G. Knepley PetscFunctionBegin; 1781113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1782113c68e6SMatthew G. Knepley PetscValidPointer(minradius,2); 1783113c68e6SMatthew G. Knepley *minradius = ((DM_Plex*) dm->data)->minradius; 1784113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 1785113c68e6SMatthew G. Knepley } 1786113c68e6SMatthew G. Knepley 1787113c68e6SMatthew G. Knepley #undef __FUNCT__ 1788113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius" 1789113c68e6SMatthew G. Knepley /*@C 1790113c68e6SMatthew G. Knepley DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face 1791113c68e6SMatthew G. Knepley 1792113c68e6SMatthew G. Knepley Logically collective 1793113c68e6SMatthew G. Knepley 1794113c68e6SMatthew G. Knepley Input Arguments: 1795113c68e6SMatthew G. Knepley + dm - the DM 1796113c68e6SMatthew G. Knepley - minradius - the minium cell radius 1797113c68e6SMatthew G. Knepley 1798113c68e6SMatthew G. Knepley Level: developer 1799113c68e6SMatthew G. Knepley 1800113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates() 1801113c68e6SMatthew G. Knepley @*/ 1802113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) 1803113c68e6SMatthew G. Knepley { 1804113c68e6SMatthew G. Knepley PetscFunctionBegin; 1805113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1806113c68e6SMatthew G. Knepley ((DM_Plex*) dm->data)->minradius = minradius; 1807113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 1808113c68e6SMatthew G. Knepley } 1809856ac710SMatthew G. Knepley 1810856ac710SMatthew G. Knepley #undef __FUNCT__ 1811856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal" 1812856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 1813856ac710SMatthew G. Knepley { 1814856ac710SMatthew G. Knepley DMLabel ghostLabel; 1815856ac710SMatthew G. Knepley PetscScalar *dx, *grad, **gref; 1816856ac710SMatthew G. Knepley PetscInt dim, cStart, cEnd, c, cEndInterior, maxNumFaces; 1817856ac710SMatthew G. Knepley PetscErrorCode ierr; 1818856ac710SMatthew G. Knepley 1819856ac710SMatthew G. Knepley PetscFunctionBegin; 1820856ac710SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1821856ac710SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1822856ac710SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1823856ac710SMatthew G. Knepley ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr); 1824856ac710SMatthew G. Knepley ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr); 1825c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1826856ac710SMatthew G. Knepley ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr); 1827856ac710SMatthew G. Knepley for (c = cStart; c < cEndInterior; c++) { 1828856ac710SMatthew G. Knepley const PetscInt *faces; 1829856ac710SMatthew G. Knepley PetscInt numFaces, usedFaces, f, d; 1830640bce14SSatish Balay PetscFVCellGeom *cg; 1831856ac710SMatthew G. Knepley PetscBool boundary; 1832856ac710SMatthew G. Knepley PetscInt ghost; 1833856ac710SMatthew G. Knepley 1834856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1835856ac710SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr); 1836856ac710SMatthew G. Knepley ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr); 1837856ac710SMatthew 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); 1838856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 1839640bce14SSatish Balay PetscFVCellGeom *cg1; 1840856ac710SMatthew G. Knepley PetscFVFaceGeom *fg; 1841856ac710SMatthew G. Knepley const PetscInt *fcells; 1842856ac710SMatthew G. Knepley PetscInt ncell, side; 1843856ac710SMatthew G. Knepley 1844856ac710SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 1845a6ba4734SToby Isaac ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 1846856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 1847856ac710SMatthew G. Knepley ierr = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr); 1848856ac710SMatthew G. Knepley side = (c != fcells[0]); /* c is on left=0 or right=1 of face */ 1849856ac710SMatthew G. Knepley ncell = fcells[!side]; /* the neighbor */ 1850856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr); 1851856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr); 1852856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d]; 1853856ac710SMatthew G. Knepley gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */ 1854856ac710SMatthew G. Knepley } 1855856ac710SMatthew G. Knepley if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?"); 1856856ac710SMatthew G. Knepley ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr); 1857856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 1858856ac710SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 1859a6ba4734SToby Isaac ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 1860856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 1861856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d]; 1862856ac710SMatthew G. Knepley ++usedFaces; 1863856ac710SMatthew G. Knepley } 1864856ac710SMatthew G. Knepley } 1865856ac710SMatthew G. Knepley ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr); 1866856ac710SMatthew G. Knepley PetscFunctionReturn(0); 1867856ac710SMatthew G. Knepley } 1868856ac710SMatthew G. Knepley 1869856ac710SMatthew G. Knepley #undef __FUNCT__ 1870b81db932SToby Isaac #define __FUNCT__ "BuildGradientReconstruction_Internal_Tree" 1871b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 1872b81db932SToby Isaac { 1873b81db932SToby Isaac DMLabel ghostLabel; 1874b81db932SToby Isaac PetscScalar *dx, *grad, **gref; 1875b81db932SToby Isaac PetscInt dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0; 1876b81db932SToby Isaac PetscSection neighSec; 1877b81db932SToby Isaac PetscInt (*neighbors)[2]; 1878b81db932SToby Isaac PetscInt *counter; 1879b81db932SToby Isaac PetscErrorCode ierr; 1880b81db932SToby Isaac 1881b81db932SToby Isaac PetscFunctionBegin; 1882b81db932SToby Isaac ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1883b81db932SToby Isaac ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1884b81db932SToby Isaac ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 18855bc680faSToby Isaac if (cEndInterior < 0) { 18865bc680faSToby Isaac cEndInterior = cEnd; 18875bc680faSToby Isaac } 1888b81db932SToby Isaac ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr); 1889b81db932SToby Isaac ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr); 1890b81db932SToby Isaac ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1891c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1892b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 1893b81db932SToby Isaac const PetscInt *fcells; 1894b81db932SToby Isaac PetscBool boundary; 18955bc680faSToby Isaac PetscInt ghost = -1; 1896b81db932SToby Isaac PetscInt numChildren, numCells, c; 1897b81db932SToby Isaac 189806348e87SToby Isaac if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);} 1899a6ba4734SToby Isaac ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr); 1900b81db932SToby Isaac ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr); 1901b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 1902b81db932SToby Isaac ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr); 190306348e87SToby Isaac if (numCells == 2) { 1904b81db932SToby Isaac ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr); 1905b81db932SToby Isaac for (c = 0; c < 2; c++) { 1906b81db932SToby Isaac PetscInt cell = fcells[c]; 1907b81db932SToby Isaac 1908e6885bbbSToby Isaac if (cell >= cStart && cell < cEndInterior) { 1909b81db932SToby Isaac ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr); 1910b81db932SToby Isaac } 1911b81db932SToby Isaac } 1912b81db932SToby Isaac } 191306348e87SToby Isaac } 1914b81db932SToby Isaac ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr); 1915b81db932SToby Isaac ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr); 1916b81db932SToby Isaac ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr); 1917b81db932SToby Isaac nStart = 0; 1918b81db932SToby Isaac ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr); 1919b81db932SToby Isaac ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr); 1920b81db932SToby Isaac ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr); 1921b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 1922b81db932SToby Isaac const PetscInt *fcells; 1923b81db932SToby Isaac PetscBool boundary; 19245bc680faSToby Isaac PetscInt ghost = -1; 1925b81db932SToby Isaac PetscInt numChildren, numCells, c; 1926b81db932SToby Isaac 192706348e87SToby Isaac if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);} 1928a6ba4734SToby Isaac ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr); 1929b81db932SToby Isaac ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr); 1930b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 1931b81db932SToby Isaac ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr); 193206348e87SToby Isaac if (numCells == 2) { 1933b81db932SToby Isaac ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr); 1934b81db932SToby Isaac for (c = 0; c < 2; c++) { 1935b81db932SToby Isaac PetscInt cell = fcells[c], off; 1936b81db932SToby Isaac 1937e6885bbbSToby Isaac if (cell >= cStart && cell < cEndInterior) { 1938b81db932SToby Isaac ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr); 1939b81db932SToby Isaac off += counter[cell - cStart]++; 1940b81db932SToby Isaac neighbors[off][0] = f; 1941b81db932SToby Isaac neighbors[off][1] = fcells[1 - c]; 1942b81db932SToby Isaac } 1943b81db932SToby Isaac } 1944b81db932SToby Isaac } 194506348e87SToby Isaac } 1946b81db932SToby Isaac ierr = PetscFree(counter);CHKERRQ(ierr); 1947b81db932SToby Isaac ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr); 1948b81db932SToby Isaac for (c = cStart; c < cEndInterior; c++) { 1949317218b9SToby Isaac PetscInt numFaces, f, d, off, ghost = -1; 1950640bce14SSatish Balay PetscFVCellGeom *cg; 1951b81db932SToby Isaac 1952b81db932SToby Isaac ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1953b81db932SToby Isaac ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr); 1954b81db932SToby Isaac ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr); 1955317218b9SToby Isaac if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);} 1956317218b9SToby Isaac if (ghost < 0 && numFaces < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cell %D has only %D faces, not enough for gradient reconstruction", c, numFaces); 1957b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 1958640bce14SSatish Balay PetscFVCellGeom *cg1; 1959b81db932SToby Isaac PetscFVFaceGeom *fg; 1960b81db932SToby Isaac const PetscInt *fcells; 1961b81db932SToby Isaac PetscInt ncell, side, nface; 1962b81db932SToby Isaac 1963b81db932SToby Isaac nface = neighbors[off + f][0]; 1964b81db932SToby Isaac ncell = neighbors[off + f][1]; 1965b81db932SToby Isaac ierr = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr); 1966b81db932SToby Isaac side = (c != fcells[0]); 1967b81db932SToby Isaac ierr = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr); 1968b81db932SToby Isaac ierr = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr); 1969b81db932SToby Isaac for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d]; 1970b81db932SToby Isaac gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */ 1971b81db932SToby Isaac } 1972b81db932SToby Isaac ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr); 1973b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 1974b81db932SToby Isaac for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d]; 1975b81db932SToby Isaac } 1976b81db932SToby Isaac } 1977b81db932SToby Isaac ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr); 19785fe94518SToby Isaac ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr); 1979b81db932SToby Isaac ierr = PetscFree(neighbors);CHKERRQ(ierr); 1980b81db932SToby Isaac PetscFunctionReturn(0); 1981b81db932SToby Isaac } 1982b81db932SToby Isaac 1983b81db932SToby Isaac #undef __FUNCT__ 1984856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM" 1985856ac710SMatthew G. Knepley /*@ 1986856ac710SMatthew G. Knepley DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data 1987856ac710SMatthew G. Knepley 1988856ac710SMatthew G. Knepley Collective on DM 1989856ac710SMatthew G. Knepley 1990856ac710SMatthew G. Knepley Input Arguments: 1991856ac710SMatthew G. Knepley + dm - The DM 1992856ac710SMatthew G. Knepley . fvm - The PetscFV 1993856ac710SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexGetFaceGeometryFVM() 1994856ac710SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexGetCellGeometryFVM() 1995856ac710SMatthew G. Knepley 1996856ac710SMatthew G. Knepley Output Parameters: 1997856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted 1998856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data 1999856ac710SMatthew G. Knepley 2000856ac710SMatthew G. Knepley Level: developer 2001856ac710SMatthew G. Knepley 2002856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM() 2003856ac710SMatthew G. Knepley @*/ 2004856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) 2005856ac710SMatthew G. Knepley { 2006856ac710SMatthew G. Knepley DM dmFace, dmCell; 2007856ac710SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 2008b81db932SToby Isaac PetscSection sectionGrad, parentSection; 2009856ac710SMatthew G. Knepley PetscInt dim, pdim, cStart, cEnd, cEndInterior, c; 2010856ac710SMatthew G. Knepley PetscErrorCode ierr; 2011856ac710SMatthew G. Knepley 2012856ac710SMatthew G. Knepley PetscFunctionBegin; 2013856ac710SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2014856ac710SMatthew G. Knepley ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr); 2015856ac710SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2016856ac710SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2017856ac710SMatthew G. Knepley /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */ 2018856ac710SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 2019856ac710SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 2020856ac710SMatthew G. Knepley ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr); 2021856ac710SMatthew G. Knepley ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr); 2022b81db932SToby Isaac ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr); 2023b81db932SToby Isaac if (!parentSection) { 2024856ac710SMatthew G. Knepley ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr); 2025b5a3613cSMatthew G. Knepley } else { 2026b81db932SToby Isaac ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr); 2027b81db932SToby Isaac } 2028856ac710SMatthew G. Knepley ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr); 2029856ac710SMatthew G. Knepley ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr); 2030856ac710SMatthew G. Knepley /* Create storage for gradients */ 2031856ac710SMatthew G. Knepley ierr = DMClone(dm, dmGrad);CHKERRQ(ierr); 2032856ac710SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionGrad);CHKERRQ(ierr); 2033856ac710SMatthew G. Knepley ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr); 2034856ac710SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);} 2035856ac710SMatthew G. Knepley ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr); 2036856ac710SMatthew G. Knepley ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr); 2037856ac710SMatthew G. Knepley ierr = PetscSectionDestroy(§ionGrad);CHKERRQ(ierr); 2038856ac710SMatthew G. Knepley PetscFunctionReturn(0); 2039856ac710SMatthew G. Knepley } 2040