1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 29d150b73SToby Isaac #include <petsc/private/petscfeimpl.h> /*I "petscfe.h" I*/ 39d150b73SToby Isaac #include <petscblaslapack.h> 4ccd2543fSMatthew G Knepley 5ccd2543fSMatthew G Knepley #undef __FUNCT__ 6fea14342SMatthew G. Knepley #define __FUNCT__ "DMPlexGetLineIntersection_2D_Internal" 7fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection) 8fea14342SMatthew G. Knepley { 9fea14342SMatthew G. Knepley const PetscReal p0_x = segmentA[0*2+0]; 10fea14342SMatthew G. Knepley const PetscReal p0_y = segmentA[0*2+1]; 11fea14342SMatthew G. Knepley const PetscReal p1_x = segmentA[1*2+0]; 12fea14342SMatthew G. Knepley const PetscReal p1_y = segmentA[1*2+1]; 13fea14342SMatthew G. Knepley const PetscReal p2_x = segmentB[0*2+0]; 14fea14342SMatthew G. Knepley const PetscReal p2_y = segmentB[0*2+1]; 15fea14342SMatthew G. Knepley const PetscReal p3_x = segmentB[1*2+0]; 16fea14342SMatthew G. Knepley const PetscReal p3_y = segmentB[1*2+1]; 17fea14342SMatthew G. Knepley const PetscReal s1_x = p1_x - p0_x; 18fea14342SMatthew G. Knepley const PetscReal s1_y = p1_y - p0_y; 19fea14342SMatthew G. Knepley const PetscReal s2_x = p3_x - p2_x; 20fea14342SMatthew G. Knepley const PetscReal s2_y = p3_y - p2_y; 21fea14342SMatthew G. Knepley const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y); 22fea14342SMatthew G. Knepley 23fea14342SMatthew G. Knepley PetscFunctionBegin; 24fea14342SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 25fea14342SMatthew G. Knepley /* Non-parallel lines */ 26fea14342SMatthew G. Knepley if (denom != 0.0) { 27fea14342SMatthew G. Knepley const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom; 28fea14342SMatthew G. Knepley const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom; 29fea14342SMatthew G. Knepley 30fea14342SMatthew G. Knepley if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { 31fea14342SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 32fea14342SMatthew G. Knepley if (intersection) { 33fea14342SMatthew G. Knepley intersection[0] = p0_x + (t * s1_x); 34fea14342SMatthew G. Knepley intersection[1] = p0_y + (t * s1_y); 35fea14342SMatthew G. Knepley } 36fea14342SMatthew G. Knepley } 37fea14342SMatthew G. Knepley } 38fea14342SMatthew G. Knepley PetscFunctionReturn(0); 39fea14342SMatthew G. Knepley } 40fea14342SMatthew G. Knepley 41fea14342SMatthew G. Knepley #undef __FUNCT__ 42ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal" 43ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 44ccd2543fSMatthew G Knepley { 45ccd2543fSMatthew G Knepley const PetscInt embedDim = 2; 46f5ebc837SMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 47ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 48ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 49ccd2543fSMatthew G Knepley PetscReal v0[2], J[4], invJ[4], detJ; 50ccd2543fSMatthew G Knepley PetscReal xi, eta; 51ccd2543fSMatthew G Knepley PetscErrorCode ierr; 52ccd2543fSMatthew G Knepley 53ccd2543fSMatthew G Knepley PetscFunctionBegin; 548e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 55ccd2543fSMatthew G Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]); 56ccd2543fSMatthew G Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]); 57ccd2543fSMatthew G Knepley 58f5ebc837SMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c; 59c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 60ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 61ccd2543fSMatthew G Knepley } 62ccd2543fSMatthew G Knepley 63ccd2543fSMatthew G Knepley #undef __FUNCT__ 6462a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Simplex_2D_Internal" 6562a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[]) 6662a38674SMatthew G. Knepley { 6762a38674SMatthew G. Knepley const PetscInt embedDim = 2; 6862a38674SMatthew G. Knepley PetscReal x = PetscRealPart(point[0]); 6962a38674SMatthew G. Knepley PetscReal y = PetscRealPart(point[1]); 7062a38674SMatthew G. Knepley PetscReal v0[2], J[4], invJ[4], detJ; 7162a38674SMatthew G. Knepley PetscReal xi, eta, r; 7262a38674SMatthew G. Knepley PetscErrorCode ierr; 7362a38674SMatthew G. Knepley 7462a38674SMatthew G. Knepley PetscFunctionBegin; 7562a38674SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 7662a38674SMatthew G. Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]); 7762a38674SMatthew G. Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]); 7862a38674SMatthew G. Knepley 7962a38674SMatthew G. Knepley xi = PetscMax(xi, 0.0); 8062a38674SMatthew G. Knepley eta = PetscMax(eta, 0.0); 8162a38674SMatthew G. Knepley r = (xi + eta)/2.0; 8262a38674SMatthew G. Knepley if (xi + eta > 2.0) { 8362a38674SMatthew G. Knepley r = (xi + eta)/2.0; 8462a38674SMatthew G. Knepley xi /= r; 8562a38674SMatthew G. Knepley eta /= r; 8662a38674SMatthew G. Knepley } 8762a38674SMatthew G. Knepley cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0]; 8862a38674SMatthew G. Knepley cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1]; 8962a38674SMatthew G. Knepley PetscFunctionReturn(0); 9062a38674SMatthew G. Knepley } 9162a38674SMatthew G. Knepley 9262a38674SMatthew G. Knepley #undef __FUNCT__ 93ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal" 94ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 95ccd2543fSMatthew G Knepley { 96ccd2543fSMatthew G Knepley PetscSection coordSection; 97ccd2543fSMatthew G Knepley Vec coordsLocal; 98a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 99ccd2543fSMatthew G Knepley const PetscInt faces[8] = {0, 1, 1, 2, 2, 3, 3, 0}; 100ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 101ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 102ccd2543fSMatthew G Knepley PetscInt crossings = 0, f; 103ccd2543fSMatthew G Knepley PetscErrorCode ierr; 104ccd2543fSMatthew G Knepley 105ccd2543fSMatthew G Knepley PetscFunctionBegin; 106ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 10769d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 108ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 109ccd2543fSMatthew G Knepley for (f = 0; f < 4; ++f) { 110ccd2543fSMatthew G Knepley PetscReal x_i = PetscRealPart(coords[faces[2*f+0]*2+0]); 111ccd2543fSMatthew G Knepley PetscReal y_i = PetscRealPart(coords[faces[2*f+0]*2+1]); 112ccd2543fSMatthew G Knepley PetscReal x_j = PetscRealPart(coords[faces[2*f+1]*2+0]); 113ccd2543fSMatthew G Knepley PetscReal y_j = PetscRealPart(coords[faces[2*f+1]*2+1]); 114ccd2543fSMatthew G Knepley PetscReal slope = (y_j - y_i) / (x_j - x_i); 115ccd2543fSMatthew G Knepley PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE; 116ccd2543fSMatthew G Knepley PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE; 117ccd2543fSMatthew G Knepley PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE; 118ccd2543fSMatthew G Knepley if ((cond1 || cond2) && above) ++crossings; 119ccd2543fSMatthew G Knepley } 120ccd2543fSMatthew G Knepley if (crossings % 2) *cell = c; 121c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 122ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 123ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 124ccd2543fSMatthew G Knepley } 125ccd2543fSMatthew G Knepley 126ccd2543fSMatthew G Knepley #undef __FUNCT__ 127ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal" 128ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 129ccd2543fSMatthew G Knepley { 130ccd2543fSMatthew G Knepley const PetscInt embedDim = 3; 131ccd2543fSMatthew G Knepley PetscReal v0[3], J[9], invJ[9], detJ; 132ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 133ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 134ccd2543fSMatthew G Knepley PetscReal z = PetscRealPart(point[2]); 135ccd2543fSMatthew G Knepley PetscReal xi, eta, zeta; 136ccd2543fSMatthew G Knepley PetscErrorCode ierr; 137ccd2543fSMatthew G Knepley 138ccd2543fSMatthew G Knepley PetscFunctionBegin; 1398e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 140ccd2543fSMatthew G Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]); 141ccd2543fSMatthew G Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]); 142ccd2543fSMatthew G Knepley zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]); 143ccd2543fSMatthew G Knepley 144ccd2543fSMatthew G Knepley if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c; 145c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 146ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 147ccd2543fSMatthew G Knepley } 148ccd2543fSMatthew G Knepley 149ccd2543fSMatthew G Knepley #undef __FUNCT__ 150ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal" 151ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 152ccd2543fSMatthew G Knepley { 153ccd2543fSMatthew G Knepley PetscSection coordSection; 154ccd2543fSMatthew G Knepley Vec coordsLocal; 1557c1f9639SMatthew G Knepley PetscScalar *coords; 156fb150da6SMatthew G. Knepley const PetscInt faces[24] = {0, 3, 2, 1, 5, 4, 7, 6, 3, 0, 4, 5, 157fb150da6SMatthew G. Knepley 1, 2, 6, 7, 3, 5, 6, 2, 0, 1, 7, 4}; 158ccd2543fSMatthew G Knepley PetscBool found = PETSC_TRUE; 159ccd2543fSMatthew G Knepley PetscInt f; 160ccd2543fSMatthew G Knepley PetscErrorCode ierr; 161ccd2543fSMatthew G Knepley 162ccd2543fSMatthew G Knepley PetscFunctionBegin; 163ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 16469d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 165ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 166ccd2543fSMatthew G Knepley for (f = 0; f < 6; ++f) { 167ccd2543fSMatthew G Knepley /* Check the point is under plane */ 168ccd2543fSMatthew G Knepley /* Get face normal */ 169ccd2543fSMatthew G Knepley PetscReal v_i[3]; 170ccd2543fSMatthew G Knepley PetscReal v_j[3]; 171ccd2543fSMatthew G Knepley PetscReal normal[3]; 172ccd2543fSMatthew G Knepley PetscReal pp[3]; 173ccd2543fSMatthew G Knepley PetscReal dot; 174ccd2543fSMatthew G Knepley 175ccd2543fSMatthew G Knepley v_i[0] = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]); 176ccd2543fSMatthew G Knepley v_i[1] = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]); 177ccd2543fSMatthew G Knepley v_i[2] = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]); 178ccd2543fSMatthew G Knepley v_j[0] = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]); 179ccd2543fSMatthew G Knepley v_j[1] = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]); 180ccd2543fSMatthew G Knepley v_j[2] = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]); 181ccd2543fSMatthew G Knepley normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1]; 182ccd2543fSMatthew G Knepley normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2]; 183ccd2543fSMatthew G Knepley normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0]; 184ccd2543fSMatthew G Knepley pp[0] = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]); 185ccd2543fSMatthew G Knepley pp[1] = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]); 186ccd2543fSMatthew G Knepley pp[2] = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]); 187ccd2543fSMatthew G Knepley dot = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2]; 188ccd2543fSMatthew G Knepley 189ccd2543fSMatthew G Knepley /* Check that projected point is in face (2D location problem) */ 190ccd2543fSMatthew G Knepley if (dot < 0.0) { 191ccd2543fSMatthew G Knepley found = PETSC_FALSE; 192ccd2543fSMatthew G Knepley break; 193ccd2543fSMatthew G Knepley } 194ccd2543fSMatthew G Knepley } 195ccd2543fSMatthew G Knepley if (found) *cell = c; 196c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 197ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 198ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 199ccd2543fSMatthew G Knepley } 200ccd2543fSMatthew G Knepley 201ccd2543fSMatthew G Knepley #undef __FUNCT__ 202c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal" 203c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[]) 204c4eade1cSMatthew G. Knepley { 205c4eade1cSMatthew G. Knepley PetscInt d; 206c4eade1cSMatthew G. Knepley 207c4eade1cSMatthew G. Knepley PetscFunctionBegin; 208c4eade1cSMatthew G. Knepley box->dim = dim; 209c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]); 210c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 211c4eade1cSMatthew G. Knepley } 212c4eade1cSMatthew G. Knepley 213c4eade1cSMatthew G. Knepley #undef __FUNCT__ 214c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate" 215c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box) 216c4eade1cSMatthew G. Knepley { 217c4eade1cSMatthew G. Knepley PetscErrorCode ierr; 218c4eade1cSMatthew G. Knepley 219c4eade1cSMatthew G. Knepley PetscFunctionBegin; 220c4eade1cSMatthew G. Knepley ierr = PetscMalloc1(1, box);CHKERRQ(ierr); 221c4eade1cSMatthew G. Knepley ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr); 222c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 223c4eade1cSMatthew G. Knepley } 224c4eade1cSMatthew G. Knepley 225c4eade1cSMatthew G. Knepley #undef __FUNCT__ 226c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge" 227c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[]) 228c4eade1cSMatthew G. Knepley { 229c4eade1cSMatthew G. Knepley PetscInt d; 230c4eade1cSMatthew G. Knepley 231c4eade1cSMatthew G. Knepley PetscFunctionBegin; 232c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 233c4eade1cSMatthew G. Knepley box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d])); 234c4eade1cSMatthew G. Knepley box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d])); 235c4eade1cSMatthew G. Knepley } 236c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 237c4eade1cSMatthew G. Knepley } 238c4eade1cSMatthew G. Knepley 239c4eade1cSMatthew G. Knepley #undef __FUNCT__ 240c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid" 24162a38674SMatthew G. Knepley /* 24262a38674SMatthew G. Knepley PetscGridHashSetGrid - Divide the grid into boxes 24362a38674SMatthew G. Knepley 24462a38674SMatthew G. Knepley Not collective 24562a38674SMatthew G. Knepley 24662a38674SMatthew G. Knepley Input Parameters: 24762a38674SMatthew G. Knepley + box - The grid hash object 24862a38674SMatthew G. Knepley . n - The number of boxes in each dimension, or PETSC_DETERMINE 24962a38674SMatthew G. Knepley - h - The box size in each dimension, only used if n[d] == PETSC_DETERMINE 25062a38674SMatthew G. Knepley 25162a38674SMatthew G. Knepley Level: developer 25262a38674SMatthew G. Knepley 25362a38674SMatthew G. Knepley .seealso: PetscGridHashCreate() 25462a38674SMatthew G. Knepley */ 255c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[]) 256c4eade1cSMatthew G. Knepley { 257c4eade1cSMatthew G. Knepley PetscInt d; 258c4eade1cSMatthew G. Knepley 259c4eade1cSMatthew G. Knepley PetscFunctionBegin; 260c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 261c4eade1cSMatthew G. Knepley box->extent[d] = box->upper[d] - box->lower[d]; 262c4eade1cSMatthew G. Knepley if (n[d] == PETSC_DETERMINE) { 263c4eade1cSMatthew G. Knepley box->h[d] = h[d]; 264c4eade1cSMatthew G. Knepley box->n[d] = PetscCeilReal(box->extent[d]/h[d]); 265c4eade1cSMatthew G. Knepley } else { 266c4eade1cSMatthew G. Knepley box->n[d] = n[d]; 267c4eade1cSMatthew G. Knepley box->h[d] = box->extent[d]/n[d]; 268c4eade1cSMatthew G. Knepley } 269c4eade1cSMatthew G. Knepley } 270c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 271c4eade1cSMatthew G. Knepley } 272c4eade1cSMatthew G. Knepley 273c4eade1cSMatthew G. Knepley #undef __FUNCT__ 274c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox" 27562a38674SMatthew G. Knepley /* 27662a38674SMatthew G. Knepley PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point 27762a38674SMatthew G. Knepley 27862a38674SMatthew G. Knepley Not collective 27962a38674SMatthew G. Knepley 28062a38674SMatthew G. Knepley Input Parameters: 28162a38674SMatthew G. Knepley + box - The grid hash object 28262a38674SMatthew G. Knepley . numPoints - The number of input points 28362a38674SMatthew G. Knepley - points - The input point coordinates 28462a38674SMatthew G. Knepley 28562a38674SMatthew G. Knepley Output Parameters: 28662a38674SMatthew G. Knepley + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 28762a38674SMatthew G. Knepley - boxes - An array of numPoints integers expressing the enclosing box as single number, or NULL 28862a38674SMatthew G. Knepley 28962a38674SMatthew G. Knepley Level: developer 29062a38674SMatthew G. Knepley 29162a38674SMatthew G. Knepley .seealso: PetscGridHashCreate() 29262a38674SMatthew G. Knepley */ 2931c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[]) 294c4eade1cSMatthew G. Knepley { 295c4eade1cSMatthew G. Knepley const PetscReal *lower = box->lower; 296c4eade1cSMatthew G. Knepley const PetscReal *upper = box->upper; 297c4eade1cSMatthew G. Knepley const PetscReal *h = box->h; 298c4eade1cSMatthew G. Knepley const PetscInt *n = box->n; 299c4eade1cSMatthew G. Knepley const PetscInt dim = box->dim; 300c4eade1cSMatthew G. Knepley PetscInt d, p; 301c4eade1cSMatthew G. Knepley 302c4eade1cSMatthew G. Knepley PetscFunctionBegin; 303c4eade1cSMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 304c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) { 3051c6dfc3eSMatthew G. Knepley PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]); 306c4eade1cSMatthew G. Knepley 3071c6dfc3eSMatthew G. Knepley if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1; 308c4eade1cSMatthew 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", 3091c6dfc3eSMatthew 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); 310c4eade1cSMatthew G. Knepley dboxes[p*dim+d] = dbox; 311c4eade1cSMatthew G. Knepley } 312c4eade1cSMatthew G. Knepley if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1]; 313c4eade1cSMatthew G. Knepley } 314c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 315c4eade1cSMatthew G. Knepley } 316c4eade1cSMatthew G. Knepley 317c4eade1cSMatthew G. Knepley #undef __FUNCT__ 318c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy" 319c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box) 320c4eade1cSMatthew G. Knepley { 321c4eade1cSMatthew G. Knepley PetscErrorCode ierr; 322c4eade1cSMatthew G. Knepley 323c4eade1cSMatthew G. Knepley PetscFunctionBegin; 324c4eade1cSMatthew G. Knepley if (*box) { 325c4eade1cSMatthew G. Knepley ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr); 326c4eade1cSMatthew G. Knepley ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr); 327c4eade1cSMatthew G. Knepley ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr); 328c4eade1cSMatthew G. Knepley } 329c4eade1cSMatthew G. Knepley ierr = PetscFree(*box);CHKERRQ(ierr); 330c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 331c4eade1cSMatthew G. Knepley } 332c4eade1cSMatthew G. Knepley 333cafe43deSMatthew G. Knepley #undef __FUNCT__ 334cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexLocatePoint_Internal" 335cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell) 336cafe43deSMatthew G. Knepley { 337cafe43deSMatthew G. Knepley PetscInt coneSize; 338cafe43deSMatthew G. Knepley PetscErrorCode ierr; 339cafe43deSMatthew G. Knepley 340cafe43deSMatthew G. Knepley PetscFunctionBegin; 341cafe43deSMatthew G. Knepley switch (dim) { 342cafe43deSMatthew G. Knepley case 2: 343cafe43deSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr); 344cafe43deSMatthew G. Knepley switch (coneSize) { 345cafe43deSMatthew G. Knepley case 3: 346cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 347cafe43deSMatthew G. Knepley break; 348cafe43deSMatthew G. Knepley case 4: 349cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 350cafe43deSMatthew G. Knepley break; 351cafe43deSMatthew G. Knepley default: 352cafe43deSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize); 353cafe43deSMatthew G. Knepley } 354cafe43deSMatthew G. Knepley break; 355cafe43deSMatthew G. Knepley case 3: 356cafe43deSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr); 357cafe43deSMatthew G. Knepley switch (coneSize) { 358cafe43deSMatthew G. Knepley case 4: 359cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 360cafe43deSMatthew G. Knepley break; 361cafe43deSMatthew G. Knepley case 6: 362cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 363cafe43deSMatthew G. Knepley break; 364cafe43deSMatthew G. Knepley default: 365cafe43deSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize); 366cafe43deSMatthew G. Knepley } 367cafe43deSMatthew G. Knepley break; 368cafe43deSMatthew G. Knepley default: 369cafe43deSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim); 370cafe43deSMatthew G. Knepley } 371cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 372cafe43deSMatthew G. Knepley } 373cafe43deSMatthew G. Knepley 374cafe43deSMatthew G. Knepley #undef __FUNCT__ 37562a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Internal" 37662a38674SMatthew G. Knepley /* 37762a38674SMatthew G. Knepley DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point 37862a38674SMatthew G. Knepley */ 37962a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[]) 38062a38674SMatthew G. Knepley { 38162a38674SMatthew G. Knepley PetscInt coneSize; 38262a38674SMatthew G. Knepley PetscErrorCode ierr; 38362a38674SMatthew G. Knepley 38462a38674SMatthew G. Knepley PetscFunctionBegin; 38562a38674SMatthew G. Knepley switch (dim) { 38662a38674SMatthew G. Knepley case 2: 38762a38674SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 38862a38674SMatthew G. Knepley switch (coneSize) { 38962a38674SMatthew G. Knepley case 3: 39062a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr); 39162a38674SMatthew G. Knepley break; 39262a38674SMatthew G. Knepley #if 0 39362a38674SMatthew G. Knepley case 4: 39462a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr); 39562a38674SMatthew G. Knepley break; 39662a38674SMatthew G. Knepley #endif 39762a38674SMatthew G. Knepley default: 39862a38674SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize); 39962a38674SMatthew G. Knepley } 40062a38674SMatthew G. Knepley break; 40162a38674SMatthew G. Knepley #if 0 40262a38674SMatthew G. Knepley case 3: 40362a38674SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 40462a38674SMatthew G. Knepley switch (coneSize) { 40562a38674SMatthew G. Knepley case 4: 40662a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr); 40762a38674SMatthew G. Knepley break; 40862a38674SMatthew G. Knepley case 6: 40962a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr); 41062a38674SMatthew G. Knepley break; 41162a38674SMatthew G. Knepley default: 41262a38674SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize); 41362a38674SMatthew G. Knepley } 41462a38674SMatthew G. Knepley break; 41562a38674SMatthew G. Knepley #endif 41662a38674SMatthew G. Knepley default: 41762a38674SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for mesh dimension %D", dim); 41862a38674SMatthew G. Knepley } 41962a38674SMatthew G. Knepley PetscFunctionReturn(0); 42062a38674SMatthew G. Knepley } 42162a38674SMatthew G. Knepley 42262a38674SMatthew G. Knepley #undef __FUNCT__ 423cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGridHash_Internal" 42462a38674SMatthew G. Knepley /* 42562a38674SMatthew G. Knepley DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex 42662a38674SMatthew G. Knepley 42762a38674SMatthew G. Knepley Collective on DM 42862a38674SMatthew G. Knepley 42962a38674SMatthew G. Knepley Input Parameter: 43062a38674SMatthew G. Knepley . dm - The Plex 43162a38674SMatthew G. Knepley 43262a38674SMatthew G. Knepley Output Parameter: 43362a38674SMatthew G. Knepley . localBox - The grid hash object 43462a38674SMatthew G. Knepley 43562a38674SMatthew G. Knepley Level: developer 43662a38674SMatthew G. Knepley 43762a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox() 43862a38674SMatthew G. Knepley */ 439cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox) 440cafe43deSMatthew G. Knepley { 441cafe43deSMatthew G. Knepley MPI_Comm comm; 442cafe43deSMatthew G. Knepley PetscGridHash lbox; 443cafe43deSMatthew G. Knepley Vec coordinates; 444cafe43deSMatthew G. Knepley PetscSection coordSection; 445cafe43deSMatthew G. Knepley Vec coordsLocal; 446cafe43deSMatthew G. Knepley const PetscScalar *coords; 447722d0f5cSMatthew G. Knepley PetscInt *dboxes, *boxes; 448cafe43deSMatthew G. Knepley PetscInt n[3] = {10, 10, 10}; 4491d0c6c94SMatthew G. Knepley PetscInt dim, N, cStart, cEnd, cMax, c, i; 450cafe43deSMatthew G. Knepley PetscErrorCode ierr; 451cafe43deSMatthew G. Knepley 452cafe43deSMatthew G. Knepley PetscFunctionBegin; 453cafe43deSMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 454cafe43deSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 455cafe43deSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 4565b3353d8SMatthew G. Knepley if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D"); 457cafe43deSMatthew G. Knepley ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr); 458cafe43deSMatthew G. Knepley ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr); 459cafe43deSMatthew G. Knepley ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr); 460cafe43deSMatthew G. Knepley for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);} 461cafe43deSMatthew G. Knepley ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr); 462cafe43deSMatthew G. Knepley ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr); 463cafe43deSMatthew G. Knepley #if 0 464cafe43deSMatthew G. Knepley /* Could define a custom reduction to merge these */ 465b2566f29SBarry Smith ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr); 466b2566f29SBarry Smith ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr); 467cafe43deSMatthew G. Knepley #endif 468cafe43deSMatthew G. Knepley /* Is there a reason to snap the local bounding box to a division of the global box? */ 469cafe43deSMatthew G. Knepley /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */ 470cafe43deSMatthew G. Knepley /* Create label */ 471cafe43deSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 4721d0c6c94SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 4731d0c6c94SMatthew G. Knepley if (cMax >= 0) cEnd = PetscMin(cEnd, cMax); 474cafe43deSMatthew G. Knepley ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr); 475cafe43deSMatthew G. Knepley ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr); 476722d0f5cSMatthew G. Knepley /* Compute boxes which overlap each cell: http://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */ 477cafe43deSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 478cafe43deSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 47938353de4SMatthew G. Knepley ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr); 480cafe43deSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 481cafe43deSMatthew G. Knepley const PetscReal *h = lbox->h; 482cafe43deSMatthew G. Knepley PetscScalar *ccoords = NULL; 48338353de4SMatthew G. Knepley PetscInt csize = 0; 484cafe43deSMatthew G. Knepley PetscScalar point[3]; 485cafe43deSMatthew G. Knepley PetscInt dlim[6], d, e, i, j, k; 486cafe43deSMatthew G. Knepley 487cafe43deSMatthew G. Knepley /* Find boxes enclosing each vertex */ 48838353de4SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr); 48938353de4SMatthew G. Knepley ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr); 490722d0f5cSMatthew G. Knepley /* Mark cells containing the vertices */ 49138353de4SMatthew G. Knepley for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);} 492cafe43deSMatthew G. Knepley /* Get grid of boxes containing these */ 493cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];} 4942291669eSMatthew G. Knepley for (d = dim; d < 3; ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;} 495cafe43deSMatthew G. Knepley for (e = 1; e < dim+1; ++e) { 496cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) { 497cafe43deSMatthew G. Knepley dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]); 498cafe43deSMatthew G. Knepley dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]); 499cafe43deSMatthew G. Knepley } 500cafe43deSMatthew G. Knepley } 501fea14342SMatthew G. Knepley /* Check for intersection of box with cell */ 502cafe43deSMatthew 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]) { 503cafe43deSMatthew 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]) { 504cafe43deSMatthew 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]) { 505cafe43deSMatthew G. Knepley const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i; 506cafe43deSMatthew G. Knepley PetscScalar cpoint[3]; 507fea14342SMatthew G. Knepley PetscInt cell, edge, ii, jj, kk; 508cafe43deSMatthew G. Knepley 509fea14342SMatthew G. Knepley /* Check whether cell contains any vertex of these subboxes TODO vectorize this */ 510cafe43deSMatthew G. Knepley for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) { 511cafe43deSMatthew G. Knepley for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) { 512cafe43deSMatthew G. Knepley for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) { 513cafe43deSMatthew G. Knepley 514cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr); 515cafe43deSMatthew G. Knepley if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;} 516cafe43deSMatthew G. Knepley } 517cafe43deSMatthew G. Knepley } 518cafe43deSMatthew G. Knepley } 519fea14342SMatthew G. Knepley /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */ 520fea14342SMatthew G. Knepley for (edge = 0; edge < dim+1; ++edge) { 521fea14342SMatthew G. Knepley PetscReal segA[6], segB[6]; 522fea14342SMatthew G. Knepley 523fea14342SMatthew 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]);} 524fea14342SMatthew G. Knepley for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) { 5259a128ed2SMatthew G. Knepley if (dim > 2) {segB[2] = PetscRealPart(point[2]); 5269a128ed2SMatthew G. Knepley segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];} 527fea14342SMatthew G. Knepley for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) { 5289a128ed2SMatthew G. Knepley if (dim > 1) {segB[1] = PetscRealPart(point[1]); 5299a128ed2SMatthew G. Knepley segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];} 530fea14342SMatthew G. Knepley for (ii = 0; ii < 2; ++ii) { 531fea14342SMatthew G. Knepley PetscBool intersects; 532fea14342SMatthew G. Knepley 5339a128ed2SMatthew G. Knepley segB[0] = PetscRealPart(point[0]); 5349a128ed2SMatthew G. Knepley segB[dim+0] = PetscRealPart(point[0]) + ii*h[0]; 535fea14342SMatthew G. Knepley ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr); 536fea14342SMatthew G. Knepley if (intersects) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;} 537cafe43deSMatthew G. Knepley } 538cafe43deSMatthew G. Knepley } 539cafe43deSMatthew G. Knepley } 540cafe43deSMatthew G. Knepley } 541fea14342SMatthew G. Knepley } 542fea14342SMatthew G. Knepley } 543fea14342SMatthew G. Knepley } 544fea14342SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr); 545fea14342SMatthew G. Knepley } 546722d0f5cSMatthew G. Knepley ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr); 547cafe43deSMatthew G. Knepley ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr); 548cafe43deSMatthew G. Knepley ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr); 549cafe43deSMatthew G. Knepley *localBox = lbox; 550cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 551cafe43deSMatthew G. Knepley } 552cafe43deSMatthew G. Knepley 553cafe43deSMatthew G. Knepley #undef __FUNCT__ 554ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex" 55562a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF) 556ccd2543fSMatthew G Knepley { 557cafe43deSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 558953fc75cSMatthew G. Knepley PetscBool hash = mesh->useHashLocation; 5593a93e3b7SToby Isaac PetscInt bs, numPoints, p, numFound, *found = NULL; 5601318edbeSMatthew G. Knepley PetscInt dim, cStart, cEnd, cMax, numCells, c; 561cafe43deSMatthew G. Knepley const PetscInt *boxCells; 5623a93e3b7SToby Isaac PetscSFNode *cells; 563ccd2543fSMatthew G Knepley PetscScalar *a; 5643a93e3b7SToby Isaac PetscMPIInt result; 565ccd2543fSMatthew G Knepley PetscErrorCode ierr; 566ccd2543fSMatthew G Knepley 567ccd2543fSMatthew G Knepley PetscFunctionBegin; 568080342d1SMatthew G. Knepley if (ltype == DM_POINTLOCATION_NEAREST && !hash) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Nearest point location only supported with grid hashing. Use -dm_plex_hash_location to enable it."); 569cafe43deSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 570cafe43deSMatthew G. Knepley ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr); 5713a93e3b7SToby Isaac ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr); 5723a93e3b7SToby Isaac if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported"); 573cafe43deSMatthew 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); 574ccd2543fSMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 575ccd2543fSMatthew G Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 576ccd2543fSMatthew G Knepley if (cMax >= 0) cEnd = PetscMin(cEnd, cMax); 577ccd2543fSMatthew G Knepley ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr); 578ccd2543fSMatthew G Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 579ccd2543fSMatthew G Knepley numPoints /= bs; 580785e854fSJed Brown ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr); 581953fc75cSMatthew G. Knepley if (hash) { 582ac6ec2abSMatthew G. Knepley if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);} 583cafe43deSMatthew G. Knepley /* Designate the local box for each point */ 584cafe43deSMatthew G. Knepley /* Send points to correct process */ 585cafe43deSMatthew G. Knepley /* Search cells that lie in each subbox */ 586cafe43deSMatthew G. Knepley /* Should we bin points before doing search? */ 587cafe43deSMatthew G. Knepley ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr); 588953fc75cSMatthew G. Knepley } 5893a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; ++p) { 590ccd2543fSMatthew G Knepley const PetscScalar *point = &a[p*bs]; 591953fc75cSMatthew G. Knepley PetscInt dbin[3], bin, cell = -1, cellOffset; 592ccd2543fSMatthew G Knepley 593e9b685f5SMatthew G. Knepley cells[p].rank = 0; 594e9b685f5SMatthew G. Knepley cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 595953fc75cSMatthew G. Knepley if (hash) { 596cafe43deSMatthew G. Knepley ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr); 597cafe43deSMatthew G. Knepley /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */ 598cafe43deSMatthew G. Knepley ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr); 599cafe43deSMatthew G. Knepley ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr); 600cafe43deSMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 601cafe43deSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr); 6023a93e3b7SToby Isaac if (cell >= 0) { 6033a93e3b7SToby Isaac cells[p].rank = 0; 6043a93e3b7SToby Isaac cells[p].index = cell; 6053a93e3b7SToby Isaac numFound++; 6063a93e3b7SToby Isaac break; 607ccd2543fSMatthew G Knepley } 6083a93e3b7SToby Isaac } 609953fc75cSMatthew G. Knepley } else { 610953fc75cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 611953fc75cSMatthew G. Knepley ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr); 6123a93e3b7SToby Isaac if (cell >= 0) { 6133a93e3b7SToby Isaac cells[p].rank = 0; 6143a93e3b7SToby Isaac cells[p].index = cell; 6153a93e3b7SToby Isaac numFound++; 6163a93e3b7SToby Isaac break; 617953fc75cSMatthew G. Knepley } 618953fc75cSMatthew G. Knepley } 6193a93e3b7SToby Isaac } 620ccd2543fSMatthew G Knepley } 621953fc75cSMatthew G. Knepley if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);} 62262a38674SMatthew G. Knepley if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) { 62362a38674SMatthew G. Knepley for (p = 0; p < numPoints; p++) { 62462a38674SMatthew G. Knepley const PetscScalar *point = &a[p*bs]; 62562a38674SMatthew G. Knepley PetscReal cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL; 626b716b415SMatthew G. Knepley PetscInt dbin[3], bin, cellOffset, d; 62762a38674SMatthew G. Knepley 628e9b685f5SMatthew G. Knepley if (cells[p].index < 0) { 62962a38674SMatthew G. Knepley ++numFound; 63062a38674SMatthew G. Knepley ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr); 63162a38674SMatthew G. Knepley ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr); 63262a38674SMatthew G. Knepley ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr); 63362a38674SMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 63462a38674SMatthew G. Knepley ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr); 635b716b415SMatthew G. Knepley for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]); 63662a38674SMatthew G. Knepley dist = DMPlex_NormD_Internal(dim, diff); 63762a38674SMatthew G. Knepley if (dist < distMax) { 63862a38674SMatthew G. Knepley for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d]; 63962a38674SMatthew G. Knepley cells[p].rank = 0; 64062a38674SMatthew G. Knepley cells[p].index = boxCells[c]; 64162a38674SMatthew G. Knepley distMax = dist; 64262a38674SMatthew G. Knepley break; 64362a38674SMatthew G. Knepley } 64462a38674SMatthew G. Knepley } 64562a38674SMatthew G. Knepley } 64662a38674SMatthew G. Knepley } 64762a38674SMatthew G. Knepley } 64862a38674SMatthew G. Knepley /* This code is only be relevant when interfaced to parallel point location */ 649cafe43deSMatthew G. Knepley /* Check for highest numbered proc that claims a point (do we care?) */ 6502d1fa6caSMatthew G. Knepley if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) { 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 } 66162a38674SMatthew 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__ 667741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D" 668741bfc07SMatthew G. Knepley /*@C 669741bfc07SMatthew G. Knepley DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates 670741bfc07SMatthew G. Knepley 671741bfc07SMatthew G. Knepley Not collective 672741bfc07SMatthew G. Knepley 673741bfc07SMatthew G. Knepley Input Parameter: 674741bfc07SMatthew G. Knepley . coords - The coordinates of a segment 675741bfc07SMatthew G. Knepley 676741bfc07SMatthew G. Knepley Output Parameters: 677741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x 678741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection 679741bfc07SMatthew G. Knepley 680741bfc07SMatthew G. Knepley Level: developer 681741bfc07SMatthew G. Knepley 682741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D() 683741bfc07SMatthew G. Knepley @*/ 684741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[]) 68517fe8556SMatthew G. Knepley { 68617fe8556SMatthew G. Knepley const PetscReal x = PetscRealPart(coords[2] - coords[0]); 68717fe8556SMatthew G. Knepley const PetscReal y = PetscRealPart(coords[3] - coords[1]); 6888b49ba18SBarry Smith const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r; 68917fe8556SMatthew G. Knepley 69017fe8556SMatthew G. Knepley PetscFunctionBegin; 6911c99cf0cSGeoffrey Irving R[0] = c; R[1] = -s; 6921c99cf0cSGeoffrey Irving R[2] = s; R[3] = c; 69317fe8556SMatthew G. Knepley coords[0] = 0.0; 6947f07f362SMatthew G. Knepley coords[1] = r; 69517fe8556SMatthew G. Knepley PetscFunctionReturn(0); 69617fe8556SMatthew G. Knepley } 69717fe8556SMatthew G. Knepley 69817fe8556SMatthew G. Knepley #undef __FUNCT__ 699741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto1D" 700741bfc07SMatthew G. Knepley /*@C 701741bfc07SMatthew G. Knepley DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates 70228dbe442SToby Isaac 703741bfc07SMatthew G. Knepley Not collective 70428dbe442SToby Isaac 705741bfc07SMatthew G. Knepley Input Parameter: 706741bfc07SMatthew G. Knepley . coords - The coordinates of a segment 707741bfc07SMatthew G. Knepley 708741bfc07SMatthew G. Knepley Output Parameters: 709741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x and z 710741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection 711741bfc07SMatthew G. Knepley 712741bfc07SMatthew G. Knepley Note: This uses the basis completion described by Frisvad in http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html, DOI:10.1080/2165347X.2012.689606 713741bfc07SMatthew G. Knepley 714741bfc07SMatthew G. Knepley Level: developer 715741bfc07SMatthew G. Knepley 716741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D() 717741bfc07SMatthew G. Knepley @*/ 718741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[]) 71928dbe442SToby Isaac { 72028dbe442SToby Isaac PetscReal x = PetscRealPart(coords[3] - coords[0]); 72128dbe442SToby Isaac PetscReal y = PetscRealPart(coords[4] - coords[1]); 72228dbe442SToby Isaac PetscReal z = PetscRealPart(coords[5] - coords[2]); 72328dbe442SToby Isaac PetscReal r = PetscSqrtReal(x*x + y*y + z*z); 72428dbe442SToby Isaac PetscReal rinv = 1. / r; 72528dbe442SToby Isaac PetscFunctionBegin; 72628dbe442SToby Isaac 72728dbe442SToby Isaac x *= rinv; y *= rinv; z *= rinv; 72828dbe442SToby Isaac if (x > 0.) { 72928dbe442SToby Isaac PetscReal inv1pX = 1./ (1. + x); 73028dbe442SToby Isaac 73128dbe442SToby Isaac R[0] = x; R[1] = -y; R[2] = -z; 73228dbe442SToby Isaac R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] = -y*z*inv1pX; 73328dbe442SToby Isaac R[6] = z; R[7] = -y*z*inv1pX; R[8] = 1. - z*z*inv1pX; 73428dbe442SToby Isaac } 73528dbe442SToby Isaac else { 73628dbe442SToby Isaac PetscReal inv1mX = 1./ (1. - x); 73728dbe442SToby Isaac 73828dbe442SToby Isaac R[0] = x; R[1] = z; R[2] = y; 73928dbe442SToby Isaac R[3] = y; R[4] = -y*z*inv1mX; R[5] = 1. - y*y*inv1mX; 74028dbe442SToby Isaac R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] = -y*z*inv1mX; 74128dbe442SToby Isaac } 74228dbe442SToby Isaac coords[0] = 0.0; 74328dbe442SToby Isaac coords[1] = r; 74428dbe442SToby Isaac PetscFunctionReturn(0); 74528dbe442SToby Isaac } 74628dbe442SToby Isaac 74728dbe442SToby Isaac #undef __FUNCT__ 748741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D" 749741bfc07SMatthew G. Knepley /*@ 750741bfc07SMatthew G. Knepley DMPlexComputeProjection3Dto2D - Rewrite coordinates to be the 2D projection of the 3D coordinates 751741bfc07SMatthew G. Knepley 752741bfc07SMatthew G. Knepley Not collective 753741bfc07SMatthew G. Knepley 754741bfc07SMatthew G. Knepley Input Parameter: 755741bfc07SMatthew G. Knepley . coords - The coordinates of a segment 756741bfc07SMatthew G. Knepley 757741bfc07SMatthew G. Knepley Output Parameters: 758741bfc07SMatthew G. Knepley + coords - The new y- and z-coordinates, and 0 for x 759741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection 760741bfc07SMatthew G. Knepley 761741bfc07SMatthew G. Knepley Level: developer 762741bfc07SMatthew G. Knepley 763741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D() 764741bfc07SMatthew G. Knepley @*/ 765741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) 766ccd2543fSMatthew G Knepley { 7671ee9d5ecSMatthew G. Knepley PetscReal x1[3], x2[3], n[3], norm; 76899dec3a6SMatthew G. Knepley PetscReal x1p[3], x2p[3], xnp[3]; 7694a217a95SMatthew G. Knepley PetscReal sqrtz, alpha; 770ccd2543fSMatthew G Knepley const PetscInt dim = 3; 77199dec3a6SMatthew G. Knepley PetscInt d, e, p; 772ccd2543fSMatthew G Knepley 773ccd2543fSMatthew G Knepley PetscFunctionBegin; 774ccd2543fSMatthew G Knepley /* 0) Calculate normal vector */ 775ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 7761ee9d5ecSMatthew G. Knepley x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]); 7771ee9d5ecSMatthew G. Knepley x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]); 778ccd2543fSMatthew G Knepley } 779ccd2543fSMatthew G Knepley n[0] = x1[1]*x2[2] - x1[2]*x2[1]; 780ccd2543fSMatthew G Knepley n[1] = x1[2]*x2[0] - x1[0]*x2[2]; 781ccd2543fSMatthew G Knepley n[2] = x1[0]*x2[1] - x1[1]*x2[0]; 7828b49ba18SBarry Smith norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]); 783ccd2543fSMatthew G Knepley n[0] /= norm; 784ccd2543fSMatthew G Knepley n[1] /= norm; 785ccd2543fSMatthew G Knepley n[2] /= norm; 786ccd2543fSMatthew G Knepley /* 1) Take the normal vector and rotate until it is \hat z 787ccd2543fSMatthew G Knepley 788ccd2543fSMatthew G Knepley Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then 789ccd2543fSMatthew G Knepley 790ccd2543fSMatthew G Knepley R = / alpha nx nz alpha ny nz -1/alpha \ 791ccd2543fSMatthew G Knepley | -alpha ny alpha nx 0 | 792ccd2543fSMatthew G Knepley \ nx ny nz / 793ccd2543fSMatthew G Knepley 794ccd2543fSMatthew G Knepley will rotate the normal vector to \hat z 795ccd2543fSMatthew G Knepley */ 7968b49ba18SBarry Smith sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]); 79773868372SMatthew G. Knepley /* Check for n = z */ 79873868372SMatthew G. Knepley if (sqrtz < 1.0e-10) { 7997df32b8bSSanderA const PetscInt s = PetscSign(n[2]); 8007df32b8bSSanderA /* If nz < 0, rotate 180 degrees around x-axis */ 80199dec3a6SMatthew G. Knepley for (p = 3; p < coordSize/3; ++p) { 80299dec3a6SMatthew G. Knepley coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]); 8037df32b8bSSanderA coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s; 80473868372SMatthew G. Knepley } 80599dec3a6SMatthew G. Knepley coords[0] = 0.0; 80699dec3a6SMatthew G. Knepley coords[1] = 0.0; 8077df32b8bSSanderA coords[2] = x1[0]; 8087df32b8bSSanderA coords[3] = x1[1] * s; 8097df32b8bSSanderA coords[4] = x2[0]; 8107df32b8bSSanderA coords[5] = x2[1] * s; 8117df32b8bSSanderA R[0] = 1.0; R[1] = 0.0; R[2] = 0.0; 8127df32b8bSSanderA R[3] = 0.0; R[4] = 1.0 * s; R[5] = 0.0; 8137df32b8bSSanderA R[6] = 0.0; R[7] = 0.0; R[8] = 1.0 * s; 81473868372SMatthew G. Knepley PetscFunctionReturn(0); 81573868372SMatthew G. Knepley } 816da18b5e6SMatthew G Knepley alpha = 1.0/sqrtz; 817ccd2543fSMatthew G Knepley R[0] = alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz; 818ccd2543fSMatthew G Knepley R[3] = -alpha*n[1]; R[4] = alpha*n[0]; R[5] = 0.0; 819ccd2543fSMatthew G Knepley R[6] = n[0]; R[7] = n[1]; R[8] = n[2]; 820ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 821ccd2543fSMatthew G Knepley x1p[d] = 0.0; 822ccd2543fSMatthew G Knepley x2p[d] = 0.0; 823ccd2543fSMatthew G Knepley for (e = 0; e < dim; ++e) { 824ccd2543fSMatthew G Knepley x1p[d] += R[d*dim+e]*x1[e]; 825ccd2543fSMatthew G Knepley x2p[d] += R[d*dim+e]*x2[e]; 826ccd2543fSMatthew G Knepley } 827ccd2543fSMatthew G Knepley } 82848120919SToby Isaac if (PetscAbsReal(x1p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 82948120919SToby Isaac if (PetscAbsReal(x2p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 830ccd2543fSMatthew G Knepley /* 2) Project to (x, y) */ 83199dec3a6SMatthew G. Knepley for (p = 3; p < coordSize/3; ++p) { 83299dec3a6SMatthew G. Knepley for (d = 0; d < dim; ++d) { 83399dec3a6SMatthew G. Knepley xnp[d] = 0.0; 83499dec3a6SMatthew G. Knepley for (e = 0; e < dim; ++e) { 83599dec3a6SMatthew G. Knepley xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]); 83699dec3a6SMatthew G. Knepley } 83799dec3a6SMatthew G. Knepley if (d < dim-1) coords[p*2+d] = xnp[d]; 83899dec3a6SMatthew G. Knepley } 83999dec3a6SMatthew G. Knepley } 840ccd2543fSMatthew G Knepley coords[0] = 0.0; 841ccd2543fSMatthew G Knepley coords[1] = 0.0; 842ccd2543fSMatthew G Knepley coords[2] = x1p[0]; 843ccd2543fSMatthew G Knepley coords[3] = x1p[1]; 844ccd2543fSMatthew G Knepley coords[4] = x2p[0]; 845ccd2543fSMatthew G Knepley coords[5] = x2p[1]; 8467f07f362SMatthew G. Knepley /* Output R^T which rotates \hat z to the input normal */ 8477f07f362SMatthew G. Knepley for (d = 0; d < dim; ++d) { 8487f07f362SMatthew G. Knepley for (e = d+1; e < dim; ++e) { 8497f07f362SMatthew G. Knepley PetscReal tmp; 8507f07f362SMatthew G. Knepley 8517f07f362SMatthew G. Knepley tmp = R[d*dim+e]; 8527f07f362SMatthew G. Knepley R[d*dim+e] = R[e*dim+d]; 8537f07f362SMatthew G. Knepley R[e*dim+d] = tmp; 8547f07f362SMatthew G. Knepley } 8557f07f362SMatthew G. Knepley } 856ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 857ccd2543fSMatthew G Knepley } 858ccd2543fSMatthew G Knepley 859ccd2543fSMatthew G Knepley #undef __FUNCT__ 860834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal" 8616322fe33SJed Brown PETSC_UNUSED 862834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) 863834e62ceSMatthew G. Knepley { 864834e62ceSMatthew G. Knepley /* Signed volume is 1/2 the determinant 865834e62ceSMatthew G. Knepley 866834e62ceSMatthew G. Knepley | 1 1 1 | 867834e62ceSMatthew G. Knepley | x0 x1 x2 | 868834e62ceSMatthew G. Knepley | y0 y1 y2 | 869834e62ceSMatthew G. Knepley 870834e62ceSMatthew G. Knepley but if x0,y0 is the origin, we have 871834e62ceSMatthew G. Knepley 872834e62ceSMatthew G. Knepley | x1 x2 | 873834e62ceSMatthew G. Knepley | y1 y2 | 874834e62ceSMatthew G. Knepley */ 875834e62ceSMatthew G. Knepley const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1]; 876834e62ceSMatthew G. Knepley const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1]; 877834e62ceSMatthew G. Knepley PetscReal M[4], detM; 878834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; 87986623015SMatthew G. Knepley M[2] = y1; M[3] = y2; 880923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(&detM, M); 881834e62ceSMatthew G. Knepley *vol = 0.5*detM; 8823bc0b13bSBarry Smith (void)PetscLogFlops(5.0); 883834e62ceSMatthew G. Knepley } 884834e62ceSMatthew G. Knepley 885834e62ceSMatthew G. Knepley #undef __FUNCT__ 886834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal" 887834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[]) 888834e62ceSMatthew G. Knepley { 889923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(vol, coords); 890834e62ceSMatthew G. Knepley *vol *= 0.5; 891834e62ceSMatthew G. Knepley } 892834e62ceSMatthew G. Knepley 893834e62ceSMatthew G. Knepley #undef __FUNCT__ 894834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal" 8956322fe33SJed Brown PETSC_UNUSED 896834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) 897834e62ceSMatthew G. Knepley { 898834e62ceSMatthew G. Knepley /* Signed volume is 1/6th of the determinant 899834e62ceSMatthew G. Knepley 900834e62ceSMatthew G. Knepley | 1 1 1 1 | 901834e62ceSMatthew G. Knepley | x0 x1 x2 x3 | 902834e62ceSMatthew G. Knepley | y0 y1 y2 y3 | 903834e62ceSMatthew G. Knepley | z0 z1 z2 z3 | 904834e62ceSMatthew G. Knepley 905834e62ceSMatthew G. Knepley but if x0,y0,z0 is the origin, we have 906834e62ceSMatthew G. Knepley 907834e62ceSMatthew G. Knepley | x1 x2 x3 | 908834e62ceSMatthew G. Knepley | y1 y2 y3 | 909834e62ceSMatthew G. Knepley | z1 z2 z3 | 910834e62ceSMatthew G. Knepley */ 911834e62ceSMatthew G. Knepley const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2]; 912834e62ceSMatthew G. Knepley const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2]; 913834e62ceSMatthew G. Knepley const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2]; 914834e62ceSMatthew G. Knepley PetscReal M[9], detM; 915834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; M[2] = x3; 916834e62ceSMatthew G. Knepley M[3] = y1; M[4] = y2; M[5] = y3; 917834e62ceSMatthew G. Knepley M[6] = z1; M[7] = z2; M[8] = z3; 918923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(&detM, M); 919b7ad821dSMatthew G. Knepley *vol = -0.16666666666666666666666*detM; 9203bc0b13bSBarry Smith (void)PetscLogFlops(10.0); 921834e62ceSMatthew G. Knepley } 922834e62ceSMatthew G. Knepley 923834e62ceSMatthew G. Knepley #undef __FUNCT__ 9240ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal" 9250ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) 9260ec8681fSMatthew G. Knepley { 927923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(vol, coords); 928b7ad821dSMatthew G. Knepley *vol *= -0.16666666666666666666666; 9290ec8681fSMatthew G. Knepley } 9300ec8681fSMatthew G. Knepley 9310ec8681fSMatthew G. Knepley #undef __FUNCT__ 932cb92db44SToby Isaac #define __FUNCT__ "DMPlexComputePointGeometry_Internal" 933cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 934cb92db44SToby Isaac { 935cb92db44SToby Isaac PetscSection coordSection; 936cb92db44SToby Isaac Vec coordinates; 937cb92db44SToby Isaac const PetscScalar *coords; 938cb92db44SToby Isaac PetscInt dim, d, off; 939cb92db44SToby Isaac PetscErrorCode ierr; 940cb92db44SToby Isaac 941cb92db44SToby Isaac PetscFunctionBegin; 942cb92db44SToby Isaac ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 943cb92db44SToby Isaac ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 944cb92db44SToby Isaac ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr); 945cb92db44SToby Isaac if (!dim) PetscFunctionReturn(0); 946cb92db44SToby Isaac ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr); 947cb92db44SToby Isaac ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr); 948cb92db44SToby Isaac if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);} 949cb92db44SToby Isaac ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr); 950cb92db44SToby Isaac *detJ = 1.; 951cb92db44SToby Isaac if (J) { 952cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) J[d] = 0.; 953cb92db44SToby Isaac for (d = 0; d < dim; d++) J[d * dim + d] = 1.; 954cb92db44SToby Isaac if (invJ) { 955cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) invJ[d] = 0.; 956cb92db44SToby Isaac for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.; 957cb92db44SToby Isaac } 958cb92db44SToby Isaac } 959cb92db44SToby Isaac PetscFunctionReturn(0); 960cb92db44SToby Isaac } 961cb92db44SToby Isaac 962cb92db44SToby Isaac #undef __FUNCT__ 96317fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal" 96417fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 96517fe8556SMatthew G. Knepley { 96617fe8556SMatthew G. Knepley PetscSection coordSection; 96717fe8556SMatthew G. Knepley Vec coordinates; 968a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 9698bf5c034SToby Isaac PetscInt numCoords, d, pStart, pEnd, numSelfCoords = 0; 97017fe8556SMatthew G. Knepley PetscErrorCode ierr; 97117fe8556SMatthew G. Knepley 97217fe8556SMatthew G. Knepley PetscFunctionBegin; 97317fe8556SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 97469d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 9758bf5c034SToby Isaac ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr); 9768bf5c034SToby Isaac if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);} 97717fe8556SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 9788bf5c034SToby Isaac numCoords = numSelfCoords ? numSelfCoords : numCoords; 979adac9986SMatthew G. Knepley if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 9807f07f362SMatthew G. Knepley *detJ = 0.0; 98128dbe442SToby Isaac if (numCoords == 6) { 98228dbe442SToby Isaac const PetscInt dim = 3; 98328dbe442SToby Isaac PetscReal R[9], J0; 98428dbe442SToby Isaac 98528dbe442SToby Isaac if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 986741bfc07SMatthew G. Knepley ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr); 98728dbe442SToby Isaac if (J) { 98828dbe442SToby Isaac J0 = 0.5*PetscRealPart(coords[1]); 98928dbe442SToby Isaac J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2]; 99028dbe442SToby Isaac J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5]; 99128dbe442SToby Isaac J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8]; 99228dbe442SToby Isaac DMPlex_Det3D_Internal(detJ, J); 99328dbe442SToby Isaac if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 994adac9986SMatthew G. Knepley } 99528dbe442SToby Isaac } else if (numCoords == 4) { 9967f07f362SMatthew G. Knepley const PetscInt dim = 2; 9977f07f362SMatthew G. Knepley PetscReal R[4], J0; 9987f07f362SMatthew G. Knepley 9997f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1000741bfc07SMatthew G. Knepley ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr); 100117fe8556SMatthew G. Knepley if (J) { 10027f07f362SMatthew G. Knepley J0 = 0.5*PetscRealPart(coords[1]); 10037f07f362SMatthew G. Knepley J[0] = R[0]*J0; J[1] = R[1]; 10047f07f362SMatthew G. Knepley J[2] = R[2]*J0; J[3] = R[3]; 1005923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1006923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 1007adac9986SMatthew G. Knepley } 10087f07f362SMatthew G. Knepley } else if (numCoords == 2) { 10097f07f362SMatthew G. Knepley const PetscInt dim = 1; 10107f07f362SMatthew G. Knepley 10117f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 10127f07f362SMatthew G. Knepley if (J) { 10137f07f362SMatthew G. Knepley J[0] = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0])); 101417fe8556SMatthew G. Knepley *detJ = J[0]; 10153bc0b13bSBarry Smith ierr = PetscLogFlops(2.0);CHKERRQ(ierr); 10163bc0b13bSBarry Smith if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);} 1017adac9986SMatthew G. Knepley } 1018796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords); 101917fe8556SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 102017fe8556SMatthew G. Knepley PetscFunctionReturn(0); 102117fe8556SMatthew G. Knepley } 102217fe8556SMatthew G. Knepley 102317fe8556SMatthew G. Knepley #undef __FUNCT__ 1024ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal" 1025ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1026ccd2543fSMatthew G Knepley { 1027ccd2543fSMatthew G Knepley PetscSection coordSection; 1028ccd2543fSMatthew G Knepley Vec coordinates; 1029a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 10307f07f362SMatthew G. Knepley PetscInt numCoords, d, f, g; 1031ccd2543fSMatthew G Knepley PetscErrorCode ierr; 1032ccd2543fSMatthew G Knepley 1033ccd2543fSMatthew G Knepley PetscFunctionBegin; 1034ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 103569d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1036ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 10377f07f362SMatthew G. Knepley *detJ = 0.0; 1038ccd2543fSMatthew G Knepley if (numCoords == 9) { 10397f07f362SMatthew G. Knepley const PetscInt dim = 3; 10407f07f362SMatthew 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}; 10417f07f362SMatthew G. Knepley 10427f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1043741bfc07SMatthew G. Knepley ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr); 10447f07f362SMatthew G. Knepley if (J) { 1045b7ad821dSMatthew G. Knepley const PetscInt pdim = 2; 1046b7ad821dSMatthew G. Knepley 1047b7ad821dSMatthew G. Knepley for (d = 0; d < pdim; d++) { 1048b7ad821dSMatthew G. Knepley for (f = 0; f < pdim; f++) { 1049b7ad821dSMatthew G. Knepley J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 1050ccd2543fSMatthew G Knepley } 10517f07f362SMatthew G. Knepley } 10523bc0b13bSBarry Smith ierr = PetscLogFlops(8.0);CHKERRQ(ierr); 1053923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 10547f07f362SMatthew G. Knepley for (d = 0; d < dim; d++) { 10557f07f362SMatthew G. Knepley for (f = 0; f < dim; f++) { 10567f07f362SMatthew G. Knepley J[d*dim+f] = 0.0; 10577f07f362SMatthew G. Knepley for (g = 0; g < dim; g++) { 10587f07f362SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 10597f07f362SMatthew G. Knepley } 10607f07f362SMatthew G. Knepley } 10617f07f362SMatthew G. Knepley } 10623bc0b13bSBarry Smith ierr = PetscLogFlops(18.0);CHKERRQ(ierr); 10637f07f362SMatthew G. Knepley } 1064923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 10657f07f362SMatthew G. Knepley } else if (numCoords == 6) { 10667f07f362SMatthew G. Knepley const PetscInt dim = 2; 10677f07f362SMatthew G. Knepley 10687f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1069ccd2543fSMatthew G Knepley if (J) { 1070ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1071ccd2543fSMatthew G Knepley for (f = 0; f < dim; f++) { 1072ccd2543fSMatthew G Knepley J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d])); 1073ccd2543fSMatthew G Knepley } 1074ccd2543fSMatthew G Knepley } 10753bc0b13bSBarry Smith ierr = PetscLogFlops(8.0);CHKERRQ(ierr); 1076923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1077ccd2543fSMatthew G Knepley } 1078923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 1079796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords); 1080ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 1081ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1082ccd2543fSMatthew G Knepley } 1083ccd2543fSMatthew G Knepley 1084ccd2543fSMatthew G Knepley #undef __FUNCT__ 1085ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal" 1086dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1087ccd2543fSMatthew G Knepley { 1088ccd2543fSMatthew G Knepley PetscSection coordSection; 1089ccd2543fSMatthew G Knepley Vec coordinates; 1090a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 10910d29256aSToby Isaac PetscInt numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd; 1092ccd2543fSMatthew G Knepley PetscErrorCode ierr; 1093ccd2543fSMatthew G Knepley 1094ccd2543fSMatthew G Knepley PetscFunctionBegin; 1095ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 109669d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 10970d29256aSToby Isaac ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr); 10980d29256aSToby Isaac if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);} 109999dec3a6SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 110071f58de1SToby Isaac numCoords = numSelfCoords ? numSelfCoords : numCoords; 1101dfccc68fSToby Isaac if (!Nq) { 11027f07f362SMatthew G. Knepley *detJ = 0.0; 110399dec3a6SMatthew G. Knepley if (numCoords == 12) { 110499dec3a6SMatthew G. Knepley const PetscInt dim = 3; 110599dec3a6SMatthew 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}; 110699dec3a6SMatthew G. Knepley 1107dfccc68fSToby Isaac if (v) {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);} 1108741bfc07SMatthew G. Knepley ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr); 110999dec3a6SMatthew G. Knepley if (J) { 111099dec3a6SMatthew G. Knepley const PetscInt pdim = 2; 111199dec3a6SMatthew G. Knepley 111299dec3a6SMatthew G. Knepley for (d = 0; d < pdim; d++) { 111399dec3a6SMatthew G. Knepley J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 111499dec3a6SMatthew G. Knepley J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 111599dec3a6SMatthew G. Knepley } 11163bc0b13bSBarry Smith ierr = PetscLogFlops(8.0);CHKERRQ(ierr); 1117923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 111899dec3a6SMatthew G. Knepley for (d = 0; d < dim; d++) { 111999dec3a6SMatthew G. Knepley for (f = 0; f < dim; f++) { 112099dec3a6SMatthew G. Knepley J[d*dim+f] = 0.0; 112199dec3a6SMatthew G. Knepley for (g = 0; g < dim; g++) { 112299dec3a6SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 112399dec3a6SMatthew G. Knepley } 112499dec3a6SMatthew G. Knepley } 112599dec3a6SMatthew G. Knepley } 11263bc0b13bSBarry Smith ierr = PetscLogFlops(18.0);CHKERRQ(ierr); 112799dec3a6SMatthew G. Knepley } 1128923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 112971f58de1SToby Isaac } else if (numCoords == 8) { 113099dec3a6SMatthew G. Knepley const PetscInt dim = 2; 113199dec3a6SMatthew G. Knepley 1132dfccc68fSToby Isaac if (v) {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);} 1133ccd2543fSMatthew G Knepley if (J) { 1134ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 113599dec3a6SMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 113699dec3a6SMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 1137ccd2543fSMatthew G Knepley } 11383bc0b13bSBarry Smith ierr = PetscLogFlops(8.0);CHKERRQ(ierr); 1139923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1140ccd2543fSMatthew G Knepley } 1141923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 1142796f034aSJed Brown } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords); 1143dfccc68fSToby Isaac } else { 1144dfccc68fSToby Isaac const PetscInt Nv = 4; 1145dfccc68fSToby Isaac const PetscInt dimR = 2; 1146dfccc68fSToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 1147dfccc68fSToby Isaac PetscReal zOrder[12]; 1148dfccc68fSToby Isaac PetscReal zCoeff[12]; 1149dfccc68fSToby Isaac PetscInt i, j, k, l, dim; 1150dfccc68fSToby Isaac 1151dfccc68fSToby Isaac if (numCoords == 12) { 1152dfccc68fSToby Isaac dim = 3; 1153dfccc68fSToby Isaac } else if (numCoords == 8) { 1154dfccc68fSToby Isaac dim = 2; 1155dfccc68fSToby Isaac } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords); 1156dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 1157dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 1158dfccc68fSToby Isaac 1159dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1160dfccc68fSToby Isaac zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); 1161dfccc68fSToby Isaac } 1162dfccc68fSToby Isaac } 1163dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1164dfccc68fSToby Isaac zCoeff[dim * 0 + j] = 0.25 * ( zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1165dfccc68fSToby Isaac zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1166dfccc68fSToby Isaac zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1167dfccc68fSToby Isaac zCoeff[dim * 3 + j] = 0.25 * ( zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1168dfccc68fSToby Isaac } 1169dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1170dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1]; 1171dfccc68fSToby Isaac 1172dfccc68fSToby Isaac if (v) { 1173dfccc68fSToby Isaac PetscReal extPoint[4]; 1174dfccc68fSToby Isaac 1175dfccc68fSToby Isaac extPoint[0] = 1.; 1176dfccc68fSToby Isaac extPoint[1] = xi; 1177dfccc68fSToby Isaac extPoint[2] = eta; 1178dfccc68fSToby Isaac extPoint[3] = xi * eta; 1179dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1180dfccc68fSToby Isaac PetscReal val = 0.; 1181dfccc68fSToby Isaac 1182dfccc68fSToby Isaac for (k = 0; k < Nv; k++) { 1183dfccc68fSToby Isaac val += extPoint[k] * zCoeff[dim * k + j]; 1184dfccc68fSToby Isaac } 1185dfccc68fSToby Isaac v[i * dim + j] = val; 1186dfccc68fSToby Isaac } 1187dfccc68fSToby Isaac } 1188dfccc68fSToby Isaac if (J) { 1189dfccc68fSToby Isaac PetscReal extJ[8]; 1190dfccc68fSToby Isaac 1191dfccc68fSToby Isaac extJ[0] = 0.; 1192dfccc68fSToby Isaac extJ[1] = 0.; 1193dfccc68fSToby Isaac extJ[2] = 1.; 1194dfccc68fSToby Isaac extJ[3] = 0.; 1195dfccc68fSToby Isaac extJ[4] = 0.; 1196dfccc68fSToby Isaac extJ[5] = 1.; 1197dfccc68fSToby Isaac extJ[6] = eta; 1198dfccc68fSToby Isaac extJ[7] = xi; 1199dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1200dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 1201dfccc68fSToby Isaac PetscReal val = 0.; 1202dfccc68fSToby Isaac 1203dfccc68fSToby Isaac for (l = 0; l < Nv; l++) { 1204dfccc68fSToby Isaac val += zCoeff[dim * l + j] * extJ[dimR * l + k]; 1205dfccc68fSToby Isaac } 1206dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 1207dfccc68fSToby Isaac } 1208dfccc68fSToby Isaac } 1209dfccc68fSToby Isaac if (dim == 3) { /* put the cross product in the third component of the Jacobian */ 1210dfccc68fSToby Isaac PetscReal x, y, z; 1211dfccc68fSToby Isaac PetscReal *iJ = &J[i * dim * dim]; 1212dfccc68fSToby Isaac PetscReal norm; 1213dfccc68fSToby Isaac 1214dfccc68fSToby Isaac x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0]; 1215dfccc68fSToby Isaac y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1]; 1216dfccc68fSToby Isaac z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0]; 1217dfccc68fSToby Isaac norm = PetscSqrtReal(x * x + y * y + z * z); 1218dfccc68fSToby Isaac iJ[2] = x / norm; 1219dfccc68fSToby Isaac iJ[5] = y / norm; 1220dfccc68fSToby Isaac iJ[8] = z / norm; 1221dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 1222dfccc68fSToby Isaac if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);} 1223dfccc68fSToby Isaac } else { 1224dfccc68fSToby Isaac DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]); 1225dfccc68fSToby Isaac if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);} 1226dfccc68fSToby Isaac } 1227dfccc68fSToby Isaac } 1228dfccc68fSToby Isaac } 1229dfccc68fSToby Isaac } 123099dec3a6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 1231ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1232ccd2543fSMatthew G Knepley } 1233ccd2543fSMatthew G Knepley 1234ccd2543fSMatthew G Knepley #undef __FUNCT__ 1235ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal" 1236ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1237ccd2543fSMatthew G Knepley { 1238ccd2543fSMatthew G Knepley PetscSection coordSection; 1239ccd2543fSMatthew G Knepley Vec coordinates; 1240a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1241ccd2543fSMatthew G Knepley const PetscInt dim = 3; 124299dec3a6SMatthew G. Knepley PetscInt d; 1243ccd2543fSMatthew G Knepley PetscErrorCode ierr; 1244ccd2543fSMatthew G Knepley 1245ccd2543fSMatthew G Knepley PetscFunctionBegin; 1246ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 124769d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1248ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 12497f07f362SMatthew G. Knepley *detJ = 0.0; 12507f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1251ccd2543fSMatthew G Knepley if (J) { 1252ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1253f0df753eSMatthew G. Knepley /* I orient with outward face normals */ 1254f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d])); 1255f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 1256f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 1257ccd2543fSMatthew G Knepley } 12583bc0b13bSBarry Smith ierr = PetscLogFlops(18.0);CHKERRQ(ierr); 1259923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1260ccd2543fSMatthew G Knepley } 1261923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 1262ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 1263ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1264ccd2543fSMatthew G Knepley } 1265ccd2543fSMatthew G Knepley 1266ccd2543fSMatthew G Knepley #undef __FUNCT__ 1267ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal" 1268dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1269ccd2543fSMatthew G Knepley { 1270ccd2543fSMatthew G Knepley PetscSection coordSection; 1271ccd2543fSMatthew G Knepley Vec coordinates; 1272a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1273ccd2543fSMatthew G Knepley const PetscInt dim = 3; 1274ccd2543fSMatthew G Knepley PetscInt d; 1275ccd2543fSMatthew G Knepley PetscErrorCode ierr; 1276ccd2543fSMatthew G Knepley 1277ccd2543fSMatthew G Knepley PetscFunctionBegin; 1278ccd2543fSMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 127969d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1280ccd2543fSMatthew G Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 1281dfccc68fSToby Isaac if (!Nq) { 12827f07f362SMatthew G. Knepley *detJ = 0.0; 1283dfccc68fSToby Isaac if (v) {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);} 1284ccd2543fSMatthew G Knepley if (J) { 1285ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1286f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 1287f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 1288f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d])); 1289ccd2543fSMatthew G Knepley } 12903bc0b13bSBarry Smith ierr = PetscLogFlops(18.0);CHKERRQ(ierr); 1291923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1292ccd2543fSMatthew G Knepley } 1293923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 1294dfccc68fSToby Isaac } else { 1295dfccc68fSToby Isaac const PetscInt Nv = 8; 1296dfccc68fSToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 1297dfccc68fSToby Isaac const PetscInt dim = 3; 1298dfccc68fSToby Isaac const PetscInt dimR = 3; 1299dfccc68fSToby Isaac PetscReal zOrder[24]; 1300dfccc68fSToby Isaac PetscReal zCoeff[24]; 1301dfccc68fSToby Isaac PetscInt i, j, k, l; 1302dfccc68fSToby Isaac 1303dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 1304dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 1305dfccc68fSToby Isaac 1306dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1307dfccc68fSToby Isaac zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); 1308dfccc68fSToby Isaac } 1309dfccc68fSToby Isaac } 1310dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1311dfccc68fSToby Isaac zCoeff[dim * 0 + j] = 0.125 * ( zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j] + zOrder[dim * 4 + j] + zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]); 1312dfccc68fSToby Isaac zCoeff[dim * 1 + j] = 0.125 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j] - zOrder[dim * 4 + j] + zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]); 1313dfccc68fSToby Isaac zCoeff[dim * 2 + j] = 0.125 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j] - zOrder[dim * 4 + j] - zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]); 1314dfccc68fSToby Isaac zCoeff[dim * 3 + j] = 0.125 * ( zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j] + zOrder[dim * 4 + j] - zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]); 1315dfccc68fSToby Isaac zCoeff[dim * 4 + j] = 0.125 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] - zOrder[dim * 3 + j] + zOrder[dim * 4 + j] + zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]); 1316dfccc68fSToby Isaac zCoeff[dim * 5 + j] = 0.125 * (+ zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] - zOrder[dim * 3 + j] - zOrder[dim * 4 + j] + zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]); 1317dfccc68fSToby Isaac zCoeff[dim * 6 + j] = 0.125 * (+ zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] - zOrder[dim * 3 + j] - zOrder[dim * 4 + j] - zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]); 1318dfccc68fSToby Isaac zCoeff[dim * 7 + j] = 0.125 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] - zOrder[dim * 3 + j] + zOrder[dim * 4 + j] - zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]); 1319dfccc68fSToby Isaac } 1320dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1321dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2]; 1322dfccc68fSToby Isaac 1323dfccc68fSToby Isaac if (v) { 1324*91d2b7ceSToby Isaac PetscReal extPoint[8]; 1325dfccc68fSToby Isaac 1326dfccc68fSToby Isaac extPoint[0] = 1.; 1327dfccc68fSToby Isaac extPoint[1] = xi; 1328dfccc68fSToby Isaac extPoint[2] = eta; 1329dfccc68fSToby Isaac extPoint[3] = xi * eta; 1330dfccc68fSToby Isaac extPoint[4] = theta; 1331dfccc68fSToby Isaac extPoint[5] = theta * xi; 1332dfccc68fSToby Isaac extPoint[6] = theta * eta; 1333dfccc68fSToby Isaac extPoint[7] = theta * eta * xi; 1334dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1335dfccc68fSToby Isaac PetscReal val = 0.; 1336dfccc68fSToby Isaac 1337dfccc68fSToby Isaac for (k = 0; k < Nv; k++) { 1338dfccc68fSToby Isaac val += extPoint[k] * zCoeff[dim * k + j]; 1339dfccc68fSToby Isaac } 1340dfccc68fSToby Isaac v[i * dim + j] = val; 1341dfccc68fSToby Isaac } 1342dfccc68fSToby Isaac } 1343dfccc68fSToby Isaac if (J) { 1344dfccc68fSToby Isaac PetscReal extJ[24]; 1345dfccc68fSToby Isaac 1346dfccc68fSToby Isaac extJ[0] = 0. ; extJ[1] = 0. ; extJ[2] = 0. ; 1347dfccc68fSToby Isaac extJ[3] = 1. ; extJ[4] = 0. ; extJ[5] = 0. ; 1348dfccc68fSToby Isaac extJ[6] = 0. ; extJ[7] = 1. ; extJ[8] = 0. ; 1349dfccc68fSToby Isaac extJ[9] = eta ; extJ[10] = xi ; extJ[11] = 0. ; 1350dfccc68fSToby Isaac extJ[12] = 0. ; extJ[13] = 0. ; extJ[14] = 1. ; 1351dfccc68fSToby Isaac extJ[15] = theta ; extJ[16] = 0. ; extJ[17] = xi ; 1352dfccc68fSToby Isaac extJ[18] = 0. ; extJ[19] = theta ; extJ[20] = eta ; 1353dfccc68fSToby Isaac extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi; 1354dfccc68fSToby Isaac 1355dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1356dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 1357dfccc68fSToby Isaac PetscReal val = 0.; 1358dfccc68fSToby Isaac 1359dfccc68fSToby Isaac for (l = 0; l < Nv; l++) { 1360dfccc68fSToby Isaac val += zCoeff[dim * l + j] * extJ[dimR * l + k]; 1361dfccc68fSToby Isaac } 1362dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 1363dfccc68fSToby Isaac } 1364dfccc68fSToby Isaac } 1365dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 1366dfccc68fSToby Isaac if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);} 1367dfccc68fSToby Isaac } 1368dfccc68fSToby Isaac } 1369dfccc68fSToby Isaac } 1370ccd2543fSMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 1371ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1372ccd2543fSMatthew G Knepley } 1373ccd2543fSMatthew G Knepley 1374ccd2543fSMatthew G Knepley #undef __FUNCT__ 1375dfccc68fSToby Isaac #define __FUNCT__ "DMPlexComputeCellGeometryFEM_Implicit" 1376dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 1377dfccc68fSToby Isaac { 1378dfccc68fSToby Isaac PetscInt depth, dim, coordDim, coneSize, i; 1379dfccc68fSToby Isaac PetscInt Nq = 0; 1380dfccc68fSToby Isaac const PetscReal *points = NULL; 1381dfccc68fSToby Isaac DMLabel depthLabel; 1382dfccc68fSToby Isaac PetscReal v0[3]; 1383dfccc68fSToby Isaac PetscBool isAffine = PETSC_TRUE; 1384dfccc68fSToby Isaac PetscErrorCode ierr; 1385dfccc68fSToby Isaac 1386dfccc68fSToby Isaac PetscFunctionBegin; 1387dfccc68fSToby Isaac ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 1388dfccc68fSToby Isaac ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 1389dfccc68fSToby Isaac ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1390dfccc68fSToby Isaac ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr); 1391dfccc68fSToby Isaac if (depth == 1 && dim == 1) { 1392dfccc68fSToby Isaac ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1393dfccc68fSToby Isaac } 1394dfccc68fSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 1395dfccc68fSToby Isaac if (coordDim > 3) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim); 1396dfccc68fSToby Isaac if (quad) {ierr = PetscQuadratureGetData(quad, NULL, &Nq, &points, NULL);CHKERRQ(ierr);} 1397dfccc68fSToby Isaac switch (dim) { 1398dfccc68fSToby Isaac case 0: 1399dfccc68fSToby Isaac ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr); 1400dfccc68fSToby Isaac isAffine = PETSC_FALSE; 1401dfccc68fSToby Isaac break; 1402dfccc68fSToby Isaac case 1: 1403dfccc68fSToby Isaac ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 1404dfccc68fSToby Isaac break; 1405dfccc68fSToby Isaac case 2: 1406dfccc68fSToby Isaac switch (coneSize) { 1407dfccc68fSToby Isaac case 3: 1408dfccc68fSToby Isaac ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 1409dfccc68fSToby Isaac break; 1410dfccc68fSToby Isaac case 4: 1411dfccc68fSToby Isaac ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr); 1412dfccc68fSToby Isaac isAffine = PETSC_FALSE; 1413dfccc68fSToby Isaac break; 1414dfccc68fSToby Isaac default: 1415dfccc68fSToby Isaac SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 1416dfccc68fSToby Isaac } 1417dfccc68fSToby Isaac break; 1418dfccc68fSToby Isaac case 3: 1419dfccc68fSToby Isaac switch (coneSize) { 1420dfccc68fSToby Isaac case 4: 1421dfccc68fSToby Isaac ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 1422dfccc68fSToby Isaac break; 1423dfccc68fSToby Isaac case 6: /* Faces */ 1424dfccc68fSToby Isaac case 8: /* Vertices */ 1425dfccc68fSToby Isaac ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr); 1426dfccc68fSToby Isaac isAffine = PETSC_FALSE; 1427dfccc68fSToby Isaac break; 1428dfccc68fSToby Isaac default: 1429dfccc68fSToby Isaac SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 1430dfccc68fSToby Isaac } 1431dfccc68fSToby Isaac break; 1432dfccc68fSToby Isaac default: 1433dfccc68fSToby Isaac SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 1434dfccc68fSToby Isaac } 1435dfccc68fSToby Isaac if (isAffine) { 1436dfccc68fSToby Isaac if (v) { 1437dfccc68fSToby Isaac if (!Nq) { 1438dfccc68fSToby Isaac for (i = 0; i < coordDim; i++) {v[i] = v0[i];} 1439dfccc68fSToby Isaac } else { 1440dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1441dfccc68fSToby Isaac CoordinatesRefToReal(coordDim,dim,v0, J, &points[dim * i], &v[coordDim * i]); 1442dfccc68fSToby Isaac } 1443dfccc68fSToby Isaac } 1444dfccc68fSToby Isaac } 1445dfccc68fSToby Isaac for (i = 1; i < Nq; i++) { 1446dfccc68fSToby Isaac PetscInt j; 1447dfccc68fSToby Isaac 1448*91d2b7ceSToby Isaac if (detJ) {detJ[i] = detJ[0];} 1449dfccc68fSToby Isaac for (j = 0; j < coordDim * coordDim; j++) { 1450dfccc68fSToby Isaac if (J) {J [coordDim * coordDim * i + j] = J[j];} 1451dfccc68fSToby Isaac if (invJ) {invJ[coordDim * coordDim * i + j] = invJ[j];} 1452dfccc68fSToby Isaac } 1453dfccc68fSToby Isaac } 1454dfccc68fSToby Isaac } 1455dfccc68fSToby Isaac PetscFunctionReturn(0); 1456dfccc68fSToby Isaac } 1457dfccc68fSToby Isaac 1458dfccc68fSToby Isaac #undef __FUNCT__ 14598e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM" 1460ccd2543fSMatthew G Knepley /*@C 14618e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell 1462ccd2543fSMatthew G Knepley 1463ccd2543fSMatthew G Knepley Collective on DM 1464ccd2543fSMatthew G Knepley 1465ccd2543fSMatthew G Knepley Input Arguments: 1466ccd2543fSMatthew G Knepley + dm - the DM 1467ccd2543fSMatthew G Knepley - cell - the cell 1468ccd2543fSMatthew G Knepley 1469ccd2543fSMatthew G Knepley Output Arguments: 1470ccd2543fSMatthew G Knepley + v0 - the translation part of this affine transform 1471ccd2543fSMatthew G Knepley . J - the Jacobian of the transform from the reference element 1472ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian 1473ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant 1474ccd2543fSMatthew G Knepley 1475ccd2543fSMatthew G Knepley Level: advanced 1476ccd2543fSMatthew G Knepley 1477ccd2543fSMatthew G Knepley Fortran Notes: 1478ccd2543fSMatthew G Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 1479ccd2543fSMatthew G Knepley include petsc.h90 in your code. 1480ccd2543fSMatthew G Knepley 14818e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec() 1482ccd2543fSMatthew G Knepley @*/ 14838e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 1484ccd2543fSMatthew G Knepley { 1485ccd2543fSMatthew G Knepley PetscErrorCode ierr; 1486ccd2543fSMatthew G Knepley 1487ccd2543fSMatthew G Knepley PetscFunctionBegin; 1488dfccc68fSToby Isaac ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr); 14898e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 14908e0841e0SMatthew G. Knepley } 14918e0841e0SMatthew G. Knepley 14928e0841e0SMatthew G. Knepley #undef __FUNCT__ 1493dfccc68fSToby Isaac #define __FUNCT__ "DMPlexComputeCellGeometryFEM_FE" 1494dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 14958e0841e0SMatthew G. Knepley { 1496dfccc68fSToby Isaac PetscQuadrature feQuad; 14978e0841e0SMatthew G. Knepley PetscSection coordSection; 14988e0841e0SMatthew G. Knepley Vec coordinates; 14998e0841e0SMatthew G. Knepley PetscScalar *coords = NULL; 15008e0841e0SMatthew G. Knepley const PetscReal *quadPoints; 1501f960e424SToby Isaac PetscReal *basisDer, *basis, detJt; 1502f960e424SToby Isaac PetscInt dim, cdim, pdim, qdim, Nq, numCoords, q; 15038e0841e0SMatthew G. Knepley PetscErrorCode ierr; 15048e0841e0SMatthew G. Knepley 15058e0841e0SMatthew G. Knepley PetscFunctionBegin; 15068e0841e0SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 15078e0841e0SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 15088e0841e0SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 15098e0841e0SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 15108e0841e0SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr); 1511dfccc68fSToby Isaac if (!quad) { /* use the first point of the first functional of the dual space */ 1512dfccc68fSToby Isaac PetscDualSpace dsp; 1513dfccc68fSToby Isaac 1514dfccc68fSToby Isaac ierr = PetscFEGetDualSpace(fe, &dsp);CHKERRQ(ierr); 1515dfccc68fSToby Isaac ierr = PetscDualSpaceGetFunctional(dsp, 0, &quad);CHKERRQ(ierr); 15168e0841e0SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr); 1517dfccc68fSToby Isaac Nq = 1; 1518dfccc68fSToby Isaac } else { 1519dfccc68fSToby Isaac ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr); 1520dfccc68fSToby Isaac } 1521*91d2b7ceSToby Isaac ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr); 1522dfccc68fSToby Isaac ierr = PetscFEGetQuadrature(fe, &feQuad);CHKERRQ(ierr); 1523dfccc68fSToby Isaac if (feQuad == quad) { 1524f960e424SToby Isaac ierr = PetscFEGetDefaultTabulation(fe, &basis, &basisDer, NULL);CHKERRQ(ierr); 15258e0841e0SMatthew 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); 1526dfccc68fSToby Isaac } else { 1527dfccc68fSToby Isaac ierr = PetscFEGetTabulation(fe, Nq, quadPoints, &basis, &basisDer, NULL);CHKERRQ(ierr); 1528dfccc68fSToby Isaac } 1529dfccc68fSToby Isaac if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim); 1530dfccc68fSToby Isaac if (v) { 1531dfccc68fSToby Isaac ierr = PetscMemzero(v, Nq*cdim*sizeof(PetscReal));CHKERRQ(ierr); 1532f960e424SToby Isaac for (q = 0; q < Nq; ++q) { 1533f960e424SToby Isaac PetscInt i, k; 1534f960e424SToby Isaac 1535f960e424SToby Isaac for (k = 0; k < pdim; ++k) 1536f960e424SToby Isaac for (i = 0; i < cdim; ++i) 1537dfccc68fSToby Isaac v[q*cdim + i] += basis[q*pdim + k] * PetscRealPart(coords[k*cdim + i]); 1538f960e424SToby Isaac ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr); 1539f960e424SToby Isaac } 1540f960e424SToby Isaac } 15418e0841e0SMatthew G. Knepley if (J) { 1542dfccc68fSToby Isaac ierr = PetscMemzero(J, Nq*cdim*cdim*sizeof(PetscReal));CHKERRQ(ierr); 15438e0841e0SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 15448e0841e0SMatthew G. Knepley PetscInt i, j, k, c, r; 15458e0841e0SMatthew G. Knepley 15468e0841e0SMatthew G. Knepley /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */ 15478e0841e0SMatthew G. Knepley for (k = 0; k < pdim; ++k) 15488e0841e0SMatthew G. Knepley for (j = 0; j < dim; ++j) 15498e0841e0SMatthew G. Knepley for (i = 0; i < cdim; ++i) 1550dfccc68fSToby Isaac J[(q*cdim + i)*cdim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]); 15513bc0b13bSBarry Smith ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr); 15528e0841e0SMatthew G. Knepley if (cdim > dim) { 15538e0841e0SMatthew G. Knepley for (c = dim; c < cdim; ++c) 15548e0841e0SMatthew G. Knepley for (r = 0; r < cdim; ++r) 15558e0841e0SMatthew G. Knepley J[r*cdim+c] = r == c ? 1.0 : 0.0; 15568e0841e0SMatthew G. Knepley } 1557f960e424SToby Isaac if (!detJ && !invJ) continue; 1558a63b72c6SToby Isaac detJt = 0.; 15598e0841e0SMatthew G. Knepley switch (cdim) { 15608e0841e0SMatthew G. Knepley case 3: 1561037dc194SToby Isaac DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]); 1562037dc194SToby Isaac if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);} 156317fe8556SMatthew G. Knepley break; 156449dc4407SMatthew G. Knepley case 2: 15659f328543SToby Isaac DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]); 1566037dc194SToby Isaac if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);} 156749dc4407SMatthew G. Knepley break; 15688e0841e0SMatthew G. Knepley case 1: 1569037dc194SToby Isaac detJt = J[q*cdim*dim]; 1570037dc194SToby Isaac if (invJ) invJ[q*cdim*dim] = 1.0/detJt; 157149dc4407SMatthew G. Knepley } 1572f960e424SToby Isaac if (detJ) detJ[q] = detJt; 157349dc4407SMatthew G. Knepley } 157449dc4407SMatthew G. Knepley } 1575037dc194SToby Isaac else if (detJ || invJ) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ"); 1576dfccc68fSToby Isaac if (feQuad != quad) { 1577dfccc68fSToby Isaac ierr = PetscFERestoreTabulation(fe, Nq, quadPoints, &basis, &basisDer, NULL);CHKERRQ(ierr); 1578dfccc68fSToby Isaac } 15798e0841e0SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 15808e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 15818e0841e0SMatthew G. Knepley } 15828e0841e0SMatthew G. Knepley 15838e0841e0SMatthew G. Knepley #undef __FUNCT__ 15848e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM" 15858e0841e0SMatthew G. Knepley /*@C 15868e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell 15878e0841e0SMatthew G. Knepley 15888e0841e0SMatthew G. Knepley Collective on DM 15898e0841e0SMatthew G. Knepley 15908e0841e0SMatthew G. Knepley Input Arguments: 15918e0841e0SMatthew G. Knepley + dm - the DM 15928e0841e0SMatthew G. Knepley . cell - the cell 1593dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated. If quad == NULL, geometry will be 1594dfccc68fSToby Isaac evaluated at the first vertex of the reference element 15958e0841e0SMatthew G. Knepley 15968e0841e0SMatthew G. Knepley Output Arguments: 1597dfccc68fSToby Isaac + v - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element 15988e0841e0SMatthew G. Knepley . J - the Jacobian of the transform from the reference element at each quadrature point 15998e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point 16008e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point 16018e0841e0SMatthew G. Knepley 16028e0841e0SMatthew G. Knepley Level: advanced 16038e0841e0SMatthew G. Knepley 16048e0841e0SMatthew G. Knepley Fortran Notes: 16058e0841e0SMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 16068e0841e0SMatthew G. Knepley include petsc.h90 in your code. 16078e0841e0SMatthew G. Knepley 16088e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 16098e0841e0SMatthew G. Knepley @*/ 1610dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 16118e0841e0SMatthew G. Knepley { 1612dfccc68fSToby Isaac PetscFE fe = NULL; 16138e0841e0SMatthew G. Knepley PetscErrorCode ierr; 16148e0841e0SMatthew G. Knepley 16158e0841e0SMatthew G. Knepley PetscFunctionBegin; 1616dfccc68fSToby Isaac if (dm->coordinateDM) { 1617dfccc68fSToby Isaac PetscClassId id; 1618dfccc68fSToby Isaac PetscInt numFields; 1619dfccc68fSToby Isaac PetscDS prob = dm->coordinateDM->prob; 1620dfccc68fSToby Isaac PetscObject disc; 1621dfccc68fSToby Isaac 1622dfccc68fSToby Isaac ierr = PetscDSGetNumFields(prob, &numFields);CHKERRQ(ierr); 1623dfccc68fSToby Isaac if (numFields) { 1624dfccc68fSToby Isaac ierr = PetscDSGetDiscretization(prob,0,&disc);CHKERRQ(ierr); 1625dfccc68fSToby Isaac ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr); 1626dfccc68fSToby Isaac if (id == PETSCFE_CLASSID) { 1627dfccc68fSToby Isaac fe = (PetscFE) disc; 1628dfccc68fSToby Isaac } 1629dfccc68fSToby Isaac } 1630dfccc68fSToby Isaac } 1631dfccc68fSToby Isaac if (!fe) {ierr = DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);} 1632dfccc68fSToby Isaac else {ierr = DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);} 1633ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1634ccd2543fSMatthew G Knepley } 1635834e62ceSMatthew G. Knepley 1636834e62ceSMatthew G. Knepley #undef __FUNCT__ 1637cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal" 1638011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1639cc08537eSMatthew G. Knepley { 1640cc08537eSMatthew G. Knepley PetscSection coordSection; 1641cc08537eSMatthew G. Knepley Vec coordinates; 1642a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 164306e2781eSMatthew G. Knepley PetscScalar tmp[2]; 1644cc08537eSMatthew G. Knepley PetscInt coordSize; 1645cc08537eSMatthew G. Knepley PetscErrorCode ierr; 1646cc08537eSMatthew G. Knepley 1647cc08537eSMatthew G. Knepley PetscFunctionBegin; 1648cc08537eSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 164969d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1650cc08537eSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1651011ea5d8SMatthew G. Knepley if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now"); 16522e17dfb7SMatthew G. Knepley ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr); 1653cc08537eSMatthew G. Knepley if (centroid) { 165406e2781eSMatthew G. Knepley centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]); 165506e2781eSMatthew G. Knepley centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]); 1656cc08537eSMatthew G. Knepley } 1657cc08537eSMatthew G. Knepley if (normal) { 1658a60a936bSMatthew G. Knepley PetscReal norm; 1659a60a936bSMatthew G. Knepley 166006e2781eSMatthew G. Knepley normal[0] = -PetscRealPart(coords[1] - tmp[1]); 166106e2781eSMatthew G. Knepley normal[1] = PetscRealPart(coords[0] - tmp[0]); 1662a60a936bSMatthew G. Knepley norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]); 1663a60a936bSMatthew G. Knepley normal[0] /= norm; 1664a60a936bSMatthew G. Knepley normal[1] /= norm; 1665cc08537eSMatthew G. Knepley } 1666cc08537eSMatthew G. Knepley if (vol) { 166706e2781eSMatthew G. Knepley *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1]))); 1668cc08537eSMatthew G. Knepley } 1669cc08537eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1670cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 1671cc08537eSMatthew G. Knepley } 1672cc08537eSMatthew G. Knepley 1673cc08537eSMatthew G. Knepley #undef __FUNCT__ 1674cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal" 1675cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */ 1676011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1677cc08537eSMatthew G. Knepley { 1678cc08537eSMatthew G. Knepley PetscSection coordSection; 1679cc08537eSMatthew G. Knepley Vec coordinates; 1680cc08537eSMatthew G. Knepley PetscScalar *coords = NULL; 16810a1d6728SMatthew G. Knepley PetscReal vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9]; 16820a1d6728SMatthew G. Knepley PetscInt tdim = 2, coordSize, numCorners, p, d, e; 1683cc08537eSMatthew G. Knepley PetscErrorCode ierr; 1684cc08537eSMatthew G. Knepley 1685cc08537eSMatthew G. Knepley PetscFunctionBegin; 1686cc08537eSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 16870a1d6728SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr); 168869d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1689cc08537eSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 16900bce18caSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 1691ceee4971SMatthew G. Knepley if (dim > 2 && centroid) { 1692ceee4971SMatthew G. Knepley v0[0] = PetscRealPart(coords[0]); 1693ceee4971SMatthew G. Knepley v0[1] = PetscRealPart(coords[1]); 1694ceee4971SMatthew G. Knepley v0[2] = PetscRealPart(coords[2]); 1695ceee4971SMatthew G. Knepley } 1696011ea5d8SMatthew G. Knepley if (normal) { 1697011ea5d8SMatthew G. Knepley if (dim > 2) { 16981ee9d5ecSMatthew G. Knepley const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]); 16991ee9d5ecSMatthew G. Knepley const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]); 17001ee9d5ecSMatthew G. Knepley const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]); 17010a1d6728SMatthew G. Knepley PetscReal norm; 17020a1d6728SMatthew G. Knepley 17030a1d6728SMatthew G. Knepley normal[0] = y0*z1 - z0*y1; 17040a1d6728SMatthew G. Knepley normal[1] = z0*x1 - x0*z1; 17050a1d6728SMatthew G. Knepley normal[2] = x0*y1 - y0*x1; 17068b49ba18SBarry Smith norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]); 17070a1d6728SMatthew G. Knepley normal[0] /= norm; 17080a1d6728SMatthew G. Knepley normal[1] /= norm; 17090a1d6728SMatthew G. Knepley normal[2] /= norm; 1710011ea5d8SMatthew G. Knepley } else { 1711011ea5d8SMatthew G. Knepley for (d = 0; d < dim; ++d) normal[d] = 0.0; 1712011ea5d8SMatthew G. Knepley } 1713011ea5d8SMatthew G. Knepley } 1714741bfc07SMatthew G. Knepley if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);} 17150a1d6728SMatthew G. Knepley for (p = 0; p < numCorners; ++p) { 17160a1d6728SMatthew G. Knepley /* Need to do this copy to get types right */ 17170a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 17181ee9d5ecSMatthew G. Knepley ctmp[d] = PetscRealPart(coords[p*tdim+d]); 17191ee9d5ecSMatthew G. Knepley ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]); 17200a1d6728SMatthew G. Knepley } 17210a1d6728SMatthew G. Knepley Volume_Triangle_Origin_Internal(&vtmp, ctmp); 17220a1d6728SMatthew G. Knepley vsum += vtmp; 17230a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 17240a1d6728SMatthew G. Knepley csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp; 17250a1d6728SMatthew G. Knepley } 17260a1d6728SMatthew G. Knepley } 17270a1d6728SMatthew G. Knepley for (d = 0; d < tdim; ++d) { 17280a1d6728SMatthew G. Knepley csum[d] /= (tdim+1)*vsum; 17290a1d6728SMatthew G. Knepley } 17300a1d6728SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1731ee6bbdb2SSatish Balay if (vol) *vol = PetscAbsReal(vsum); 17320a1d6728SMatthew G. Knepley if (centroid) { 17330a1d6728SMatthew G. Knepley if (dim > 2) { 17340a1d6728SMatthew G. Knepley for (d = 0; d < dim; ++d) { 17350a1d6728SMatthew G. Knepley centroid[d] = v0[d]; 17360a1d6728SMatthew G. Knepley for (e = 0; e < dim; ++e) { 17370a1d6728SMatthew G. Knepley centroid[d] += R[d*dim+e]*csum[e]; 17380a1d6728SMatthew G. Knepley } 17390a1d6728SMatthew G. Knepley } 17400a1d6728SMatthew G. Knepley } else for (d = 0; d < dim; ++d) centroid[d] = csum[d]; 17410a1d6728SMatthew G. Knepley } 1742cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 1743cc08537eSMatthew G. Knepley } 1744cc08537eSMatthew G. Knepley 1745cc08537eSMatthew G. Knepley #undef __FUNCT__ 17460ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal" 17470ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */ 1748011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 17490ec8681fSMatthew G. Knepley { 17500ec8681fSMatthew G. Knepley PetscSection coordSection; 17510ec8681fSMatthew G. Knepley Vec coordinates; 17520ec8681fSMatthew G. Knepley PetscScalar *coords = NULL; 175386623015SMatthew G. Knepley PetscReal vsum = 0.0, vtmp, coordsTmp[3*3]; 1754a7df9edeSMatthew G. Knepley const PetscInt *faces, *facesO; 17550ec8681fSMatthew G. Knepley PetscInt numFaces, f, coordSize, numCorners, p, d; 17560ec8681fSMatthew G. Knepley PetscErrorCode ierr; 17570ec8681fSMatthew G. Knepley 17580ec8681fSMatthew G. Knepley PetscFunctionBegin; 1759f6dae198SJed Brown if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim); 17600ec8681fSMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 176169d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 17620ec8681fSMatthew G. Knepley 1763d9a81ebdSMatthew G. Knepley if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0; 17640ec8681fSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr); 17650ec8681fSMatthew G. Knepley ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr); 1766a7df9edeSMatthew G. Knepley ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr); 17670ec8681fSMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 1768011ea5d8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 17690ec8681fSMatthew G. Knepley numCorners = coordSize/dim; 17700ec8681fSMatthew G. Knepley switch (numCorners) { 17710ec8681fSMatthew G. Knepley case 3: 17720ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 17731ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 17741ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 17751ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]); 17760ec8681fSMatthew G. Knepley } 17770ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1778a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 17790ec8681fSMatthew G. Knepley vsum += vtmp; 17804f25033aSJed Brown if (centroid) { /* Centroid of OABC = (a+b+c)/4 */ 17810ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 17821ee9d5ecSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 17830ec8681fSMatthew G. Knepley } 17840ec8681fSMatthew G. Knepley } 17850ec8681fSMatthew G. Knepley break; 17860ec8681fSMatthew G. Knepley case 4: 17870ec8681fSMatthew G. Knepley /* DO FOR PYRAMID */ 17880ec8681fSMatthew G. Knepley /* First tet */ 17890ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 17901ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 17911ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 17921ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 17930ec8681fSMatthew G. Knepley } 17940ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1795a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 17960ec8681fSMatthew G. Knepley vsum += vtmp; 17970ec8681fSMatthew G. Knepley if (centroid) { 17980ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 17990ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 18000ec8681fSMatthew G. Knepley } 18010ec8681fSMatthew G. Knepley } 18020ec8681fSMatthew G. Knepley /* Second tet */ 18030ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 18041ee9d5ecSMatthew G. Knepley coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]); 18051ee9d5ecSMatthew G. Knepley coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]); 18061ee9d5ecSMatthew G. Knepley coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 18070ec8681fSMatthew G. Knepley } 18080ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1809a7df9edeSMatthew G. Knepley if (facesO[f] < 0) vtmp = -vtmp; 18100ec8681fSMatthew G. Knepley vsum += vtmp; 18110ec8681fSMatthew G. Knepley if (centroid) { 18120ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 18130ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 18140ec8681fSMatthew G. Knepley } 18150ec8681fSMatthew G. Knepley } 18160ec8681fSMatthew G. Knepley break; 18170ec8681fSMatthew G. Knepley default: 1818796f034aSJed Brown SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners); 18190ec8681fSMatthew G. Knepley } 18204f25033aSJed Brown ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 18210ec8681fSMatthew G. Knepley } 18228763be8eSMatthew G. Knepley if (vol) *vol = PetscAbsReal(vsum); 18230ec8681fSMatthew G. Knepley if (normal) for (d = 0; d < dim; ++d) normal[d] = 0.0; 1824d9a81ebdSMatthew G. Knepley if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4); 18250ec8681fSMatthew G. Knepley PetscFunctionReturn(0); 18260ec8681fSMatthew G. Knepley } 18270ec8681fSMatthew G. Knepley 18280ec8681fSMatthew G. Knepley #undef __FUNCT__ 1829834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM" 1830834e62ceSMatthew G. Knepley /*@C 1831834e62ceSMatthew G. Knepley DMPlexComputeCellGeometryFVM - Compute the volume for a given cell 1832834e62ceSMatthew G. Knepley 1833834e62ceSMatthew G. Knepley Collective on DM 1834834e62ceSMatthew G. Knepley 1835834e62ceSMatthew G. Knepley Input Arguments: 1836834e62ceSMatthew G. Knepley + dm - the DM 1837834e62ceSMatthew G. Knepley - cell - the cell 1838834e62ceSMatthew G. Knepley 1839834e62ceSMatthew G. Knepley Output Arguments: 1840834e62ceSMatthew G. Knepley + volume - the cell volume 1841cc08537eSMatthew G. Knepley . centroid - the cell centroid 1842cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate 1843834e62ceSMatthew G. Knepley 1844834e62ceSMatthew G. Knepley Level: advanced 1845834e62ceSMatthew G. Knepley 1846834e62ceSMatthew G. Knepley Fortran Notes: 1847834e62ceSMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 1848834e62ceSMatthew G. Knepley include petsc.h90 in your code. 1849834e62ceSMatthew G. Knepley 185069d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 1851834e62ceSMatthew G. Knepley @*/ 1852cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1853834e62ceSMatthew G. Knepley { 18540ec8681fSMatthew G. Knepley PetscInt depth, dim; 1855834e62ceSMatthew G. Knepley PetscErrorCode ierr; 1856834e62ceSMatthew G. Knepley 1857834e62ceSMatthew G. Knepley PetscFunctionBegin; 1858834e62ceSMatthew G. Knepley ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 1859c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1860834e62ceSMatthew G. Knepley if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated"); 1861834e62ceSMatthew G. Knepley /* We need to keep a pointer to the depth label */ 1862c58f1c22SToby Isaac ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr); 1863834e62ceSMatthew G. Knepley /* Cone size is now the number of faces */ 1864011ea5d8SMatthew G. Knepley switch (depth) { 1865cc08537eSMatthew G. Knepley case 1: 1866011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1867cc08537eSMatthew G. Knepley break; 1868834e62ceSMatthew G. Knepley case 2: 1869011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1870834e62ceSMatthew G. Knepley break; 1871834e62ceSMatthew G. Knepley case 3: 1872011ea5d8SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1873834e62ceSMatthew G. Knepley break; 1874834e62ceSMatthew G. Knepley default: 1875834e62ceSMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 1876834e62ceSMatthew G. Knepley } 1877834e62ceSMatthew G. Knepley PetscFunctionReturn(0); 1878834e62ceSMatthew G. Knepley } 1879113c68e6SMatthew G. Knepley 1880113c68e6SMatthew G. Knepley #undef __FUNCT__ 1881c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM" 1882c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */ 1883c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom) 1884c0d900a5SMatthew G. Knepley { 1885c0d900a5SMatthew G. Knepley DM dmCell; 1886c0d900a5SMatthew G. Knepley Vec coordinates; 1887c0d900a5SMatthew G. Knepley PetscSection coordSection, sectionCell; 1888c0d900a5SMatthew G. Knepley PetscScalar *cgeom; 1889c0d900a5SMatthew G. Knepley PetscInt cStart, cEnd, cMax, c; 1890c0d900a5SMatthew G. Knepley PetscErrorCode ierr; 1891c0d900a5SMatthew G. Knepley 1892c0d900a5SMatthew G. Knepley PetscFunctionBegin; 1893c0d900a5SMatthew G. Knepley ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1894c0d900a5SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1895c0d900a5SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1896c0d900a5SMatthew G. Knepley ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1897c0d900a5SMatthew G. Knepley ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1898c0d900a5SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1899c0d900a5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1900c0d900a5SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 1901c0d900a5SMatthew G. Knepley cEnd = cMax < 0 ? cEnd : cMax; 1902c0d900a5SMatthew G. Knepley ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 1903c0d900a5SMatthew G. Knepley /* TODO This needs to be multiplied by Nq for non-affine */ 19049e5edeeeSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1905c0d900a5SMatthew G. Knepley ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1906c0d900a5SMatthew G. Knepley ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1907c0d900a5SMatthew G. Knepley ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1908c0d900a5SMatthew G. Knepley ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 1909c0d900a5SMatthew G. Knepley ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1910c0d900a5SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1911c0d900a5SMatthew G. Knepley PetscFECellGeom *cg; 1912c0d900a5SMatthew G. Knepley 1913c0d900a5SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1914c0d900a5SMatthew G. Knepley ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1915c0d900a5SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr); 1916c0d900a5SMatthew G. Knepley if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c); 1917c0d900a5SMatthew G. Knepley } 1918c0d900a5SMatthew G. Knepley ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1919c0d900a5SMatthew G. Knepley ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 1920c0d900a5SMatthew G. Knepley PetscFunctionReturn(0); 1921c0d900a5SMatthew G. Knepley } 1922c0d900a5SMatthew G. Knepley 1923c0d900a5SMatthew G. Knepley #undef __FUNCT__ 1924113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM" 1925891a9168SMatthew G. Knepley /*@ 1926891a9168SMatthew G. Knepley DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method 1927891a9168SMatthew G. Knepley 1928891a9168SMatthew G. Knepley Input Parameter: 1929891a9168SMatthew G. Knepley . dm - The DM 1930891a9168SMatthew G. Knepley 1931891a9168SMatthew G. Knepley Output Parameters: 1932891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data 1933891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data 1934891a9168SMatthew G. Knepley 1935891a9168SMatthew G. Knepley Level: developer 1936891a9168SMatthew G. Knepley 1937891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM() 1938891a9168SMatthew G. Knepley @*/ 1939113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) 1940113c68e6SMatthew G. Knepley { 1941113c68e6SMatthew G. Knepley DM dmFace, dmCell; 1942113c68e6SMatthew G. Knepley DMLabel ghostLabel; 1943113c68e6SMatthew G. Knepley PetscSection sectionFace, sectionCell; 1944113c68e6SMatthew G. Knepley PetscSection coordSection; 1945113c68e6SMatthew G. Knepley Vec coordinates; 1946113c68e6SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 1947113c68e6SMatthew G. Knepley PetscReal minradius, gminradius; 1948113c68e6SMatthew G. Knepley PetscInt dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f; 1949113c68e6SMatthew G. Knepley PetscErrorCode ierr; 1950113c68e6SMatthew G. Knepley 1951113c68e6SMatthew G. Knepley PetscFunctionBegin; 1952113c68e6SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1953113c68e6SMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1954113c68e6SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1955113c68e6SMatthew G. Knepley /* Make cell centroids and volumes */ 1956113c68e6SMatthew G. Knepley ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1957113c68e6SMatthew G. Knepley ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1958113c68e6SMatthew G. Knepley ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1959113c68e6SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1960113c68e6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1961113c68e6SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1962113c68e6SMatthew G. Knepley ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 19639e5edeeeSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1964113c68e6SMatthew G. Knepley ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1965113c68e6SMatthew G. Knepley ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1966113c68e6SMatthew G. Knepley ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1967113c68e6SMatthew G. Knepley ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 196806348e87SToby Isaac if (cEndInterior < 0) { 196906348e87SToby Isaac cEndInterior = cEnd; 197006348e87SToby Isaac } 1971113c68e6SMatthew G. Knepley ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1972113c68e6SMatthew G. Knepley for (c = cStart; c < cEndInterior; ++c) { 1973113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 1974113c68e6SMatthew G. Knepley 1975113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1976113c68e6SMatthew G. Knepley ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1977113c68e6SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr); 1978113c68e6SMatthew G. Knepley } 1979113c68e6SMatthew G. Knepley /* Compute face normals and minimum cell radius */ 1980113c68e6SMatthew G. Knepley ierr = DMClone(dm, &dmFace);CHKERRQ(ierr); 1981113c68e6SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionFace);CHKERRQ(ierr); 1982113c68e6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1983113c68e6SMatthew G. Knepley ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr); 19849e5edeeeSMatthew G. Knepley for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1985113c68e6SMatthew G. Knepley ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr); 1986113c68e6SMatthew G. Knepley ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr); 1987113c68e6SMatthew G. Knepley ierr = PetscSectionDestroy(§ionFace);CHKERRQ(ierr); 1988113c68e6SMatthew G. Knepley ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr); 1989113c68e6SMatthew G. Knepley ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr); 1990c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1991113c68e6SMatthew G. Knepley minradius = PETSC_MAX_REAL; 1992113c68e6SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 1993113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 1994113c68e6SMatthew G. Knepley PetscReal area; 199550d63984SToby Isaac PetscInt ghost = -1, d, numChildren; 1996113c68e6SMatthew G. Knepley 19979ac3fadcSMatthew G. Knepley if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);} 199850d63984SToby Isaac ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr); 199950d63984SToby Isaac if (ghost >= 0 || numChildren) continue; 2000113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr); 2001113c68e6SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr); 2002113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] *= area; 2003113c68e6SMatthew G. Knepley /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */ 2004113c68e6SMatthew G. Knepley { 2005113c68e6SMatthew G. Knepley PetscFVCellGeom *cL, *cR; 200606348e87SToby Isaac PetscInt ncells; 2007113c68e6SMatthew G. Knepley const PetscInt *cells; 2008113c68e6SMatthew G. Knepley PetscReal *lcentroid, *rcentroid; 20090453c0cdSMatthew G. Knepley PetscReal l[3], r[3], v[3]; 2010113c68e6SMatthew G. Knepley 2011113c68e6SMatthew G. Knepley ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr); 201206348e87SToby Isaac ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr); 2013113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr); 2014113c68e6SMatthew G. Knepley lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid; 201506348e87SToby Isaac if (ncells > 1) { 201606348e87SToby Isaac ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr); 2017113c68e6SMatthew G. Knepley rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid; 201806348e87SToby Isaac } 201906348e87SToby Isaac else { 202006348e87SToby Isaac rcentroid = fg->centroid; 202106348e87SToby Isaac } 20222e17dfb7SMatthew G. Knepley ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr); 20232e17dfb7SMatthew G. Knepley ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr); 20240453c0cdSMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, l, r, v); 2025113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) { 2026113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d]; 2027113c68e6SMatthew G. Knepley } 2028113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) { 2029113c68e6SMatthew 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]); 2030113c68e6SMatthew 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]); 2031113c68e6SMatthew G. Knepley SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f); 2032113c68e6SMatthew G. Knepley } 2033113c68e6SMatthew G. Knepley if (cells[0] < cEndInterior) { 2034113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v); 2035113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2036113c68e6SMatthew G. Knepley } 203706348e87SToby Isaac if (ncells > 1 && cells[1] < cEndInterior) { 2038113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v); 2039113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2040113c68e6SMatthew G. Knepley } 2041113c68e6SMatthew G. Knepley } 2042113c68e6SMatthew G. Knepley } 2043b2566f29SBarry Smith ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 2044113c68e6SMatthew G. Knepley ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr); 2045113c68e6SMatthew G. Knepley /* Compute centroids of ghost cells */ 2046113c68e6SMatthew G. Knepley for (c = cEndInterior; c < cEnd; ++c) { 2047113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 2048113c68e6SMatthew G. Knepley const PetscInt *cone, *support; 2049113c68e6SMatthew G. Knepley PetscInt coneSize, supportSize, s; 2050113c68e6SMatthew G. Knepley 2051113c68e6SMatthew G. Knepley ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr); 2052113c68e6SMatthew G. Knepley if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize); 2053113c68e6SMatthew G. Knepley ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr); 2054113c68e6SMatthew G. Knepley ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr); 205550d63984SToby Isaac if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize); 2056113c68e6SMatthew G. Knepley ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr); 2057113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr); 2058113c68e6SMatthew G. Knepley for (s = 0; s < 2; ++s) { 2059113c68e6SMatthew G. Knepley /* Reflect ghost centroid across plane of face */ 2060113c68e6SMatthew G. Knepley if (support[s] == c) { 2061640bce14SSatish Balay PetscFVCellGeom *ci; 2062113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 2063113c68e6SMatthew G. Knepley PetscReal c2f[3], a; 2064113c68e6SMatthew G. Knepley 2065113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr); 2066113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */ 2067113c68e6SMatthew G. Knepley a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal); 2068113c68e6SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr); 2069113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid); 2070113c68e6SMatthew G. Knepley cg->volume = ci->volume; 2071113c68e6SMatthew G. Knepley } 2072113c68e6SMatthew G. Knepley } 2073113c68e6SMatthew G. Knepley } 2074113c68e6SMatthew G. Knepley ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr); 2075113c68e6SMatthew G. Knepley ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 2076113c68e6SMatthew G. Knepley ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 2077113c68e6SMatthew G. Knepley ierr = DMDestroy(&dmFace);CHKERRQ(ierr); 2078113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 2079113c68e6SMatthew G. Knepley } 2080113c68e6SMatthew G. Knepley 2081113c68e6SMatthew G. Knepley #undef __FUNCT__ 2082113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius" 2083113c68e6SMatthew G. Knepley /*@C 2084113c68e6SMatthew G. Knepley DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face 2085113c68e6SMatthew G. Knepley 2086113c68e6SMatthew G. Knepley Not collective 2087113c68e6SMatthew G. Knepley 2088113c68e6SMatthew G. Knepley Input Argument: 2089113c68e6SMatthew G. Knepley . dm - the DM 2090113c68e6SMatthew G. Knepley 2091113c68e6SMatthew G. Knepley Output Argument: 2092113c68e6SMatthew G. Knepley . minradius - the minium cell radius 2093113c68e6SMatthew G. Knepley 2094113c68e6SMatthew G. Knepley Level: developer 2095113c68e6SMatthew G. Knepley 2096113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates() 2097113c68e6SMatthew G. Knepley @*/ 2098113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) 2099113c68e6SMatthew G. Knepley { 2100113c68e6SMatthew G. Knepley PetscFunctionBegin; 2101113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2102113c68e6SMatthew G. Knepley PetscValidPointer(minradius,2); 2103113c68e6SMatthew G. Knepley *minradius = ((DM_Plex*) dm->data)->minradius; 2104113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 2105113c68e6SMatthew G. Knepley } 2106113c68e6SMatthew G. Knepley 2107113c68e6SMatthew G. Knepley #undef __FUNCT__ 2108113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius" 2109113c68e6SMatthew G. Knepley /*@C 2110113c68e6SMatthew G. Knepley DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face 2111113c68e6SMatthew G. Knepley 2112113c68e6SMatthew G. Knepley Logically collective 2113113c68e6SMatthew G. Knepley 2114113c68e6SMatthew G. Knepley Input Arguments: 2115113c68e6SMatthew G. Knepley + dm - the DM 2116113c68e6SMatthew G. Knepley - minradius - the minium cell radius 2117113c68e6SMatthew G. Knepley 2118113c68e6SMatthew G. Knepley Level: developer 2119113c68e6SMatthew G. Knepley 2120113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates() 2121113c68e6SMatthew G. Knepley @*/ 2122113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) 2123113c68e6SMatthew G. Knepley { 2124113c68e6SMatthew G. Knepley PetscFunctionBegin; 2125113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2126113c68e6SMatthew G. Knepley ((DM_Plex*) dm->data)->minradius = minradius; 2127113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 2128113c68e6SMatthew G. Knepley } 2129856ac710SMatthew G. Knepley 2130856ac710SMatthew G. Knepley #undef __FUNCT__ 2131856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal" 2132856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 2133856ac710SMatthew G. Knepley { 2134856ac710SMatthew G. Knepley DMLabel ghostLabel; 2135856ac710SMatthew G. Knepley PetscScalar *dx, *grad, **gref; 2136856ac710SMatthew G. Knepley PetscInt dim, cStart, cEnd, c, cEndInterior, maxNumFaces; 2137856ac710SMatthew G. Knepley PetscErrorCode ierr; 2138856ac710SMatthew G. Knepley 2139856ac710SMatthew G. Knepley PetscFunctionBegin; 2140856ac710SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2141856ac710SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2142856ac710SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2143856ac710SMatthew G. Knepley ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr); 2144856ac710SMatthew G. Knepley ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr); 2145c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 2146856ac710SMatthew G. Knepley ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr); 2147856ac710SMatthew G. Knepley for (c = cStart; c < cEndInterior; c++) { 2148856ac710SMatthew G. Knepley const PetscInt *faces; 2149856ac710SMatthew G. Knepley PetscInt numFaces, usedFaces, f, d; 2150640bce14SSatish Balay PetscFVCellGeom *cg; 2151856ac710SMatthew G. Knepley PetscBool boundary; 2152856ac710SMatthew G. Knepley PetscInt ghost; 2153856ac710SMatthew G. Knepley 2154856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 2155856ac710SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr); 2156856ac710SMatthew G. Knepley ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr); 2157856ac710SMatthew 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); 2158856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 2159640bce14SSatish Balay PetscFVCellGeom *cg1; 2160856ac710SMatthew G. Knepley PetscFVFaceGeom *fg; 2161856ac710SMatthew G. Knepley const PetscInt *fcells; 2162856ac710SMatthew G. Knepley PetscInt ncell, side; 2163856ac710SMatthew G. Knepley 2164856ac710SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 2165a6ba4734SToby Isaac ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 2166856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 2167856ac710SMatthew G. Knepley ierr = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr); 2168856ac710SMatthew G. Knepley side = (c != fcells[0]); /* c is on left=0 or right=1 of face */ 2169856ac710SMatthew G. Knepley ncell = fcells[!side]; /* the neighbor */ 2170856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr); 2171856ac710SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr); 2172856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d]; 2173856ac710SMatthew G. Knepley gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */ 2174856ac710SMatthew G. Knepley } 2175856ac710SMatthew G. Knepley if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?"); 2176856ac710SMatthew G. Knepley ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr); 2177856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 2178856ac710SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 2179a6ba4734SToby Isaac ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 2180856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 2181856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d]; 2182856ac710SMatthew G. Knepley ++usedFaces; 2183856ac710SMatthew G. Knepley } 2184856ac710SMatthew G. Knepley } 2185856ac710SMatthew G. Knepley ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr); 2186856ac710SMatthew G. Knepley PetscFunctionReturn(0); 2187856ac710SMatthew G. Knepley } 2188856ac710SMatthew G. Knepley 2189856ac710SMatthew G. Knepley #undef __FUNCT__ 2190b81db932SToby Isaac #define __FUNCT__ "BuildGradientReconstruction_Internal_Tree" 2191b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 2192b81db932SToby Isaac { 2193b81db932SToby Isaac DMLabel ghostLabel; 2194b81db932SToby Isaac PetscScalar *dx, *grad, **gref; 2195b81db932SToby Isaac PetscInt dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0; 2196b81db932SToby Isaac PetscSection neighSec; 2197b81db932SToby Isaac PetscInt (*neighbors)[2]; 2198b81db932SToby Isaac PetscInt *counter; 2199b81db932SToby Isaac PetscErrorCode ierr; 2200b81db932SToby Isaac 2201b81db932SToby Isaac PetscFunctionBegin; 2202b81db932SToby Isaac ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2203b81db932SToby Isaac ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2204b81db932SToby Isaac ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 22055bc680faSToby Isaac if (cEndInterior < 0) { 22065bc680faSToby Isaac cEndInterior = cEnd; 22075bc680faSToby Isaac } 2208b81db932SToby Isaac ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr); 2209b81db932SToby Isaac ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr); 2210b81db932SToby Isaac ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 2211c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 2212b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 2213b81db932SToby Isaac const PetscInt *fcells; 2214b81db932SToby Isaac PetscBool boundary; 22155bc680faSToby Isaac PetscInt ghost = -1; 2216b81db932SToby Isaac PetscInt numChildren, numCells, c; 2217b81db932SToby Isaac 221806348e87SToby Isaac if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);} 2219a6ba4734SToby Isaac ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr); 2220b81db932SToby Isaac ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr); 2221b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 2222b81db932SToby Isaac ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr); 222306348e87SToby Isaac if (numCells == 2) { 2224b81db932SToby Isaac ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr); 2225b81db932SToby Isaac for (c = 0; c < 2; c++) { 2226b81db932SToby Isaac PetscInt cell = fcells[c]; 2227b81db932SToby Isaac 2228e6885bbbSToby Isaac if (cell >= cStart && cell < cEndInterior) { 2229b81db932SToby Isaac ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr); 2230b81db932SToby Isaac } 2231b81db932SToby Isaac } 2232b81db932SToby Isaac } 223306348e87SToby Isaac } 2234b81db932SToby Isaac ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr); 2235b81db932SToby Isaac ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr); 2236b81db932SToby Isaac ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr); 2237b81db932SToby Isaac nStart = 0; 2238b81db932SToby Isaac ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr); 2239b81db932SToby Isaac ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr); 2240b81db932SToby Isaac ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr); 2241b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 2242b81db932SToby Isaac const PetscInt *fcells; 2243b81db932SToby Isaac PetscBool boundary; 22445bc680faSToby Isaac PetscInt ghost = -1; 2245b81db932SToby Isaac PetscInt numChildren, numCells, c; 2246b81db932SToby Isaac 224706348e87SToby Isaac if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);} 2248a6ba4734SToby Isaac ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr); 2249b81db932SToby Isaac ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr); 2250b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 2251b81db932SToby Isaac ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr); 225206348e87SToby Isaac if (numCells == 2) { 2253b81db932SToby Isaac ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr); 2254b81db932SToby Isaac for (c = 0; c < 2; c++) { 2255b81db932SToby Isaac PetscInt cell = fcells[c], off; 2256b81db932SToby Isaac 2257e6885bbbSToby Isaac if (cell >= cStart && cell < cEndInterior) { 2258b81db932SToby Isaac ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr); 2259b81db932SToby Isaac off += counter[cell - cStart]++; 2260b81db932SToby Isaac neighbors[off][0] = f; 2261b81db932SToby Isaac neighbors[off][1] = fcells[1 - c]; 2262b81db932SToby Isaac } 2263b81db932SToby Isaac } 2264b81db932SToby Isaac } 226506348e87SToby Isaac } 2266b81db932SToby Isaac ierr = PetscFree(counter);CHKERRQ(ierr); 2267b81db932SToby Isaac ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr); 2268b81db932SToby Isaac for (c = cStart; c < cEndInterior; c++) { 2269317218b9SToby Isaac PetscInt numFaces, f, d, off, ghost = -1; 2270640bce14SSatish Balay PetscFVCellGeom *cg; 2271b81db932SToby Isaac 2272b81db932SToby Isaac ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 2273b81db932SToby Isaac ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr); 2274b81db932SToby Isaac ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr); 2275317218b9SToby Isaac if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);} 2276317218b9SToby 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); 2277b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 2278640bce14SSatish Balay PetscFVCellGeom *cg1; 2279b81db932SToby Isaac PetscFVFaceGeom *fg; 2280b81db932SToby Isaac const PetscInt *fcells; 2281b81db932SToby Isaac PetscInt ncell, side, nface; 2282b81db932SToby Isaac 2283b81db932SToby Isaac nface = neighbors[off + f][0]; 2284b81db932SToby Isaac ncell = neighbors[off + f][1]; 2285b81db932SToby Isaac ierr = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr); 2286b81db932SToby Isaac side = (c != fcells[0]); 2287b81db932SToby Isaac ierr = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr); 2288b81db932SToby Isaac ierr = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr); 2289b81db932SToby Isaac for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d]; 2290b81db932SToby Isaac gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */ 2291b81db932SToby Isaac } 2292b81db932SToby Isaac ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr); 2293b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 2294b81db932SToby Isaac for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d]; 2295b81db932SToby Isaac } 2296b81db932SToby Isaac } 2297b81db932SToby Isaac ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr); 22985fe94518SToby Isaac ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr); 2299b81db932SToby Isaac ierr = PetscFree(neighbors);CHKERRQ(ierr); 2300b81db932SToby Isaac PetscFunctionReturn(0); 2301b81db932SToby Isaac } 2302b81db932SToby Isaac 2303b81db932SToby Isaac #undef __FUNCT__ 2304856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM" 2305856ac710SMatthew G. Knepley /*@ 2306856ac710SMatthew G. Knepley DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data 2307856ac710SMatthew G. Knepley 2308856ac710SMatthew G. Knepley Collective on DM 2309856ac710SMatthew G. Knepley 2310856ac710SMatthew G. Knepley Input Arguments: 2311856ac710SMatthew G. Knepley + dm - The DM 2312856ac710SMatthew G. Knepley . fvm - The PetscFV 23138f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM() 23148f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM() 2315856ac710SMatthew G. Knepley 2316856ac710SMatthew G. Knepley Output Parameters: 2317856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted 2318856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data 2319856ac710SMatthew G. Knepley 2320856ac710SMatthew G. Knepley Level: developer 2321856ac710SMatthew G. Knepley 2322856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM() 2323856ac710SMatthew G. Knepley @*/ 2324856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) 2325856ac710SMatthew G. Knepley { 2326856ac710SMatthew G. Knepley DM dmFace, dmCell; 2327856ac710SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 2328b81db932SToby Isaac PetscSection sectionGrad, parentSection; 2329856ac710SMatthew G. Knepley PetscInt dim, pdim, cStart, cEnd, cEndInterior, c; 2330856ac710SMatthew G. Knepley PetscErrorCode ierr; 2331856ac710SMatthew G. Knepley 2332856ac710SMatthew G. Knepley PetscFunctionBegin; 2333856ac710SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2334856ac710SMatthew G. Knepley ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr); 2335856ac710SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2336856ac710SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2337856ac710SMatthew G. Knepley /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */ 2338856ac710SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 2339856ac710SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 2340856ac710SMatthew G. Knepley ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr); 2341856ac710SMatthew G. Knepley ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr); 2342b81db932SToby Isaac ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr); 2343b81db932SToby Isaac if (!parentSection) { 2344856ac710SMatthew G. Knepley ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr); 2345b5a3613cSMatthew G. Knepley } else { 2346b81db932SToby Isaac ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr); 2347b81db932SToby Isaac } 2348856ac710SMatthew G. Knepley ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr); 2349856ac710SMatthew G. Knepley ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr); 2350856ac710SMatthew G. Knepley /* Create storage for gradients */ 2351856ac710SMatthew G. Knepley ierr = DMClone(dm, dmGrad);CHKERRQ(ierr); 2352856ac710SMatthew G. Knepley ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionGrad);CHKERRQ(ierr); 2353856ac710SMatthew G. Knepley ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr); 2354856ac710SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);} 2355856ac710SMatthew G. Knepley ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr); 2356856ac710SMatthew G. Knepley ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr); 2357856ac710SMatthew G. Knepley ierr = PetscSectionDestroy(§ionGrad);CHKERRQ(ierr); 2358856ac710SMatthew G. Knepley PetscFunctionReturn(0); 2359856ac710SMatthew G. Knepley } 2360b27d5b9eSToby Isaac 2361b27d5b9eSToby Isaac #undef __FUNCT__ 2362b27d5b9eSToby Isaac #define __FUNCT__ "DMPlexGetDataFVM" 2363b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM) 2364b27d5b9eSToby Isaac { 2365b27d5b9eSToby Isaac PetscObject cellgeomobj, facegeomobj; 2366b27d5b9eSToby Isaac PetscErrorCode ierr; 2367b27d5b9eSToby Isaac 2368b27d5b9eSToby Isaac PetscFunctionBegin; 2369b27d5b9eSToby Isaac ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr); 2370b27d5b9eSToby Isaac if (!cellgeomobj) { 2371b27d5b9eSToby Isaac Vec cellgeomInt, facegeomInt; 2372b27d5b9eSToby Isaac 2373b27d5b9eSToby Isaac ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr); 2374b27d5b9eSToby Isaac ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr); 2375b27d5b9eSToby Isaac ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr); 2376b27d5b9eSToby Isaac ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr); 2377b27d5b9eSToby Isaac ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr); 2378b27d5b9eSToby Isaac ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr); 2379b27d5b9eSToby Isaac } 2380b27d5b9eSToby Isaac ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr); 2381b27d5b9eSToby Isaac if (cellgeom) *cellgeom = (Vec) cellgeomobj; 2382b27d5b9eSToby Isaac if (facegeom) *facegeom = (Vec) facegeomobj; 2383b27d5b9eSToby Isaac if (gradDM) { 2384b27d5b9eSToby Isaac PetscObject gradobj; 2385b27d5b9eSToby Isaac PetscBool computeGradients; 2386b27d5b9eSToby Isaac 2387b27d5b9eSToby Isaac ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr); 2388b27d5b9eSToby Isaac if (!computeGradients) { 2389b27d5b9eSToby Isaac *gradDM = NULL; 2390b27d5b9eSToby Isaac PetscFunctionReturn(0); 2391b27d5b9eSToby Isaac } 2392b27d5b9eSToby Isaac ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr); 2393b27d5b9eSToby Isaac if (!gradobj) { 2394b27d5b9eSToby Isaac DM dmGradInt; 2395b27d5b9eSToby Isaac 2396b27d5b9eSToby Isaac ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr); 2397b27d5b9eSToby Isaac ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr); 2398b27d5b9eSToby Isaac ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr); 2399b27d5b9eSToby Isaac ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr); 2400b27d5b9eSToby Isaac } 2401b27d5b9eSToby Isaac *gradDM = (DM) gradobj; 2402b27d5b9eSToby Isaac } 2403b27d5b9eSToby Isaac PetscFunctionReturn(0); 2404b27d5b9eSToby Isaac } 2405d6143a4eSToby Isaac 2406d6143a4eSToby Isaac #undef __FUNCT__ 24079d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_NewtonUpdate" 24089d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess) 24099d150b73SToby Isaac { 24109d150b73SToby Isaac PetscInt l, m; 24119d150b73SToby Isaac 2412cd345991SToby Isaac PetscFunctionBeginHot; 24139d150b73SToby Isaac if (dimC == dimR && dimR <= 3) { 24149d150b73SToby Isaac /* invert Jacobian, multiply */ 24159d150b73SToby Isaac PetscScalar det, idet; 24169d150b73SToby Isaac 24179d150b73SToby Isaac switch (dimR) { 24189d150b73SToby Isaac case 1: 24199d150b73SToby Isaac invJ[0] = 1./ J[0]; 24209d150b73SToby Isaac break; 24219d150b73SToby Isaac case 2: 24229d150b73SToby Isaac det = J[0] * J[3] - J[1] * J[2]; 24239d150b73SToby Isaac idet = 1./det; 24249d150b73SToby Isaac invJ[0] = J[3] * idet; 24259d150b73SToby Isaac invJ[1] = -J[1] * idet; 24269d150b73SToby Isaac invJ[2] = -J[2] * idet; 24279d150b73SToby Isaac invJ[3] = J[0] * idet; 24289d150b73SToby Isaac break; 24299d150b73SToby Isaac case 3: 24309d150b73SToby Isaac { 24319d150b73SToby Isaac invJ[0] = J[4] * J[8] - J[5] * J[7]; 24329d150b73SToby Isaac invJ[1] = J[2] * J[7] - J[1] * J[8]; 24339d150b73SToby Isaac invJ[2] = J[1] * J[5] - J[2] * J[4]; 24349d150b73SToby Isaac det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6]; 24359d150b73SToby Isaac idet = 1./det; 24369d150b73SToby Isaac invJ[0] *= idet; 24379d150b73SToby Isaac invJ[1] *= idet; 24389d150b73SToby Isaac invJ[2] *= idet; 24399d150b73SToby Isaac invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]); 24409d150b73SToby Isaac invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]); 24419d150b73SToby Isaac invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]); 24429d150b73SToby Isaac invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]); 24439d150b73SToby Isaac invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]); 24449d150b73SToby Isaac invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]); 24459d150b73SToby Isaac } 24469d150b73SToby Isaac break; 24479d150b73SToby Isaac } 24489d150b73SToby Isaac for (l = 0; l < dimR; l++) { 24499d150b73SToby Isaac for (m = 0; m < dimC; m++) { 2450c6e120d1SToby Isaac guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m]; 24519d150b73SToby Isaac } 24529d150b73SToby Isaac } 24539d150b73SToby Isaac } else { 24549d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX) 24559d150b73SToby Isaac char transpose = 'C'; 24569d150b73SToby Isaac #else 24579d150b73SToby Isaac char transpose = 'T'; 24589d150b73SToby Isaac #endif 24599d150b73SToby Isaac PetscBLASInt m = dimR; 24609d150b73SToby Isaac PetscBLASInt n = dimC; 24619d150b73SToby Isaac PetscBLASInt one = 1; 24629d150b73SToby Isaac PetscBLASInt worksize = dimR * dimC, info; 24639d150b73SToby Isaac 24649d150b73SToby Isaac for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];} 24659d150b73SToby Isaac 24669d150b73SToby Isaac PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info)); 24679d150b73SToby Isaac if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS"); 24689d150b73SToby Isaac 2469c6e120d1SToby Isaac for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);} 24709d150b73SToby Isaac } 24719d150b73SToby Isaac PetscFunctionReturn(0); 24729d150b73SToby Isaac } 24739d150b73SToby Isaac 24749d150b73SToby Isaac #undef __FUNCT__ 24759d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_Tensor" 24769d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 24779d150b73SToby Isaac { 2478c0cbe899SToby Isaac PetscInt coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR); 24799d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 24809d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg; 24819d150b73SToby Isaac PetscScalar *J, *invJ, *work; 24829d150b73SToby Isaac PetscErrorCode ierr; 24839d150b73SToby Isaac 24849d150b73SToby Isaac PetscFunctionBegin; 24859d150b73SToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 24869d150b73SToby Isaac ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr); 24879d150b73SToby Isaac if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);CHKERRQ(ierr); 24889d150b73SToby Isaac ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr); 24899d150b73SToby Isaac ierr = DMGetWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr); 24909d150b73SToby Isaac cellCoords = &cellData[0]; 24919d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 24929d150b73SToby Isaac extJ = &cellData[2 * coordSize]; 24939d150b73SToby Isaac resNeg = &cellData[2 * coordSize + dimR]; 24949d150b73SToby Isaac invJ = &J[dimR * dimC]; 24959d150b73SToby Isaac work = &J[2 * dimR * dimC]; 24969d150b73SToby Isaac if (dimR == 2) { 24979d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 24989d150b73SToby Isaac 24999d150b73SToby Isaac for (i = 0; i < 4; i++) { 25009d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 25019d150b73SToby Isaac 25029d150b73SToby Isaac for (j = 0; j < dimC; j++) { 25039d150b73SToby Isaac cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 25049d150b73SToby Isaac } 25059d150b73SToby Isaac } 25069d150b73SToby Isaac } else if (dimR == 3) { 25079d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 25089d150b73SToby Isaac 25099d150b73SToby Isaac for (i = 0; i < 8; i++) { 25109d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 25119d150b73SToby Isaac 25129d150b73SToby Isaac for (j = 0; j < dimC; j++) { 25139d150b73SToby Isaac cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 25149d150b73SToby Isaac } 25159d150b73SToby Isaac } 25169d150b73SToby Isaac } else { 25179d150b73SToby Isaac for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);} 25189d150b73SToby Isaac } 25199d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 25209d150b73SToby Isaac for (i = 0; i < dimR; i++) { 25219d150b73SToby Isaac PetscReal *swap; 25229d150b73SToby Isaac 25239d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 25249d150b73SToby Isaac for (k = 0; k < dimC; k++) { 25259d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 25269d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 25279d150b73SToby Isaac } 25289d150b73SToby Isaac } 25299d150b73SToby Isaac 25309d150b73SToby Isaac if (i < dimR - 1) { 25319d150b73SToby Isaac swap = cellCoeffs; 25329d150b73SToby Isaac cellCoeffs = cellCoords; 25339d150b73SToby Isaac cellCoords = swap; 25349d150b73SToby Isaac } 25359d150b73SToby Isaac } 25369d150b73SToby Isaac ierr = PetscMemzero(refCoords,numPoints * dimR * sizeof (PetscReal));CHKERRQ(ierr); 25379d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 25389d150b73SToby Isaac for (i = 0; i < maxIts; i++) { 25399d150b73SToby Isaac PetscReal *guess = &refCoords[dimR * j]; 25409d150b73SToby Isaac 25419d150b73SToby Isaac /* compute -residual and Jacobian */ 25429d150b73SToby Isaac for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];} 25439d150b73SToby Isaac for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;} 25449d150b73SToby Isaac for (k = 0; k < numV; k++) { 25459d150b73SToby Isaac PetscReal extCoord = 1.; 25469d150b73SToby Isaac for (l = 0; l < dimR; l++) { 25479d150b73SToby Isaac PetscReal coord = guess[l]; 25489d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 25499d150b73SToby Isaac 25509d150b73SToby Isaac extCoord *= dep * coord + !dep; 25519d150b73SToby Isaac extJ[l] = dep; 25529d150b73SToby Isaac 25539d150b73SToby Isaac for (m = 0; m < dimR; m++) { 25549d150b73SToby Isaac PetscReal coord = guess[m]; 25559d150b73SToby Isaac PetscInt dep = ((k & (1 << m)) >> m) && (m != l); 25569d150b73SToby Isaac PetscReal mult = dep * coord + !dep; 25579d150b73SToby Isaac 25589d150b73SToby Isaac extJ[l] *= mult; 25599d150b73SToby Isaac } 25609d150b73SToby Isaac } 25619d150b73SToby Isaac for (l = 0; l < dimC; l++) { 25629d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 25639d150b73SToby Isaac 25649d150b73SToby Isaac resNeg[l] -= coeff * extCoord; 25659d150b73SToby Isaac for (m = 0; m < dimR; m++) { 25669d150b73SToby Isaac J[dimR * l + m] += coeff * extJ[m]; 25679d150b73SToby Isaac } 25689d150b73SToby Isaac } 25699d150b73SToby Isaac } 25700611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG) 25710611203eSToby Isaac { 25720611203eSToby Isaac PetscReal maxAbs = 0.; 25730611203eSToby Isaac 25740611203eSToby Isaac for (l = 0; l < dimC; l++) { 25750611203eSToby Isaac maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l])); 25760611203eSToby Isaac } 25770611203eSToby Isaac ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr); 25780611203eSToby Isaac } 25790611203eSToby Isaac #endif 25809d150b73SToby Isaac 25819d150b73SToby Isaac ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr); 25829d150b73SToby Isaac } 25839d150b73SToby Isaac } 25849d150b73SToby Isaac ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr); 25859d150b73SToby Isaac ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr); 25869d150b73SToby Isaac ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr); 25879d150b73SToby Isaac PetscFunctionReturn(0); 25889d150b73SToby Isaac } 25899d150b73SToby Isaac 25909d150b73SToby Isaac #undef __FUNCT__ 25919d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates_Tensor" 25929d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 25939d150b73SToby Isaac { 25949d150b73SToby Isaac PetscInt coordSize, i, j, k, l, numV = (1 << dimR); 25959d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 25969d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs; 25979d150b73SToby Isaac PetscErrorCode ierr; 25989d150b73SToby Isaac 25999d150b73SToby Isaac PetscFunctionBegin; 26009d150b73SToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 26019d150b73SToby Isaac ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr); 26029d150b73SToby Isaac if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);CHKERRQ(ierr); 26039d150b73SToby Isaac ierr = DMGetWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr); 26049d150b73SToby Isaac cellCoords = &cellData[0]; 26059d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 26069d150b73SToby Isaac if (dimR == 2) { 26079d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 26089d150b73SToby Isaac 26099d150b73SToby Isaac for (i = 0; i < 4; i++) { 26109d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 26119d150b73SToby Isaac 26129d150b73SToby Isaac for (j = 0; j < dimC; j++) { 26139d150b73SToby Isaac cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 26149d150b73SToby Isaac } 26159d150b73SToby Isaac } 26169d150b73SToby Isaac } else if (dimR == 3) { 26179d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 26189d150b73SToby Isaac 26199d150b73SToby Isaac for (i = 0; i < 8; i++) { 26209d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 26219d150b73SToby Isaac 26229d150b73SToby Isaac for (j = 0; j < dimC; j++) { 26239d150b73SToby Isaac cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 26249d150b73SToby Isaac } 26259d150b73SToby Isaac } 26269d150b73SToby Isaac } else { 26279d150b73SToby Isaac for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);} 26289d150b73SToby Isaac } 26299d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 26309d150b73SToby Isaac for (i = 0; i < dimR; i++) { 26319d150b73SToby Isaac PetscReal *swap; 26329d150b73SToby Isaac 26339d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 26349d150b73SToby Isaac for (k = 0; k < dimC; k++) { 26359d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 26369d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 26379d150b73SToby Isaac } 26389d150b73SToby Isaac } 26399d150b73SToby Isaac 26409d150b73SToby Isaac if (i < dimR - 1) { 26419d150b73SToby Isaac swap = cellCoeffs; 26429d150b73SToby Isaac cellCoeffs = cellCoords; 26439d150b73SToby Isaac cellCoords = swap; 26449d150b73SToby Isaac } 26459d150b73SToby Isaac } 26469d150b73SToby Isaac ierr = PetscMemzero(realCoords,numPoints * dimC * sizeof (PetscReal));CHKERRQ(ierr); 26479d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 26489d150b73SToby Isaac const PetscReal *guess = &refCoords[dimR * j]; 26499d150b73SToby Isaac PetscReal *mapped = &realCoords[dimC * j]; 26509d150b73SToby Isaac 26519d150b73SToby Isaac for (k = 0; k < numV; k++) { 26529d150b73SToby Isaac PetscReal extCoord = 1.; 26539d150b73SToby Isaac for (l = 0; l < dimR; l++) { 26549d150b73SToby Isaac PetscReal coord = guess[l]; 26559d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 26569d150b73SToby Isaac 26579d150b73SToby Isaac extCoord *= dep * coord + !dep; 26589d150b73SToby Isaac } 26599d150b73SToby Isaac for (l = 0; l < dimC; l++) { 26609d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 26619d150b73SToby Isaac 26629d150b73SToby Isaac mapped[l] += coeff * extCoord; 26639d150b73SToby Isaac } 26649d150b73SToby Isaac } 26659d150b73SToby Isaac } 26669d150b73SToby Isaac ierr = DMRestoreWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr); 26679d150b73SToby Isaac ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr); 26689d150b73SToby Isaac PetscFunctionReturn(0); 26699d150b73SToby Isaac } 26709d150b73SToby Isaac 26719d150b73SToby Isaac #undef __FUNCT__ 26729d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_FE" 26739d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 26749d150b73SToby Isaac { 2675c0cbe899SToby Isaac PetscInt numComp, numDof, i, j, k, l, m, maxIter = 7, coordSize; 2676c6e120d1SToby Isaac PetscScalar *nodes = NULL; 2677c6e120d1SToby Isaac PetscReal *invV, *modes; 2678c6e120d1SToby Isaac PetscReal *B, *D, *resNeg; 2679c6e120d1SToby Isaac PetscScalar *J, *invJ, *work; 26809d150b73SToby Isaac PetscErrorCode ierr; 26819d150b73SToby Isaac 26829d150b73SToby Isaac PetscFunctionBegin; 26839d150b73SToby Isaac ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr); 26849d150b73SToby Isaac ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr); 26859d150b73SToby Isaac if (numComp != dimC) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,dimC); 26869d150b73SToby Isaac ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr); 26879d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 2688c6e120d1SToby Isaac ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr); 26899d150b73SToby Isaac invV = fe->invV; 26909d150b73SToby Isaac for (i = 0; i < numDof; i++) { 26919d150b73SToby Isaac for (j = 0; j < dimC; j++) { 26929d150b73SToby Isaac modes[i * dimC + j] = 0.; 26939d150b73SToby Isaac for (k = 0; k < numDof; k++) { 2694c6e120d1SToby Isaac modes[i * dimC + j] += invV[i * numDof + k] * PetscRealPart(nodes[k * dimC + j]); 26959d150b73SToby Isaac } 26969d150b73SToby Isaac } 26979d150b73SToby Isaac } 2698c6e120d1SToby Isaac ierr = DMGetWorkArray(dm,numDof + numDof * dimR + dimC,PETSC_REAL,&B);CHKERRQ(ierr); 2699c6e120d1SToby Isaac D = &B[numDof]; 2700c6e120d1SToby Isaac resNeg = &D[numDof * dimR]; 2701c6e120d1SToby Isaac ierr = DMGetWorkArray(dm,3 * dimC * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr); 27029d150b73SToby Isaac invJ = &J[dimC * dimR]; 27039d150b73SToby Isaac work = &invJ[dimC * dimR]; 27049d150b73SToby Isaac for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;} 27059d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 27069b1f03cbSToby Isaac for (i = 0; i < maxIter; i++) { /* we could batch this so that we're not making big B and D arrays all the time */ 27079d150b73SToby Isaac PetscReal *guess = &refCoords[j * dimR]; 27089d150b73SToby Isaac ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr); 27099d150b73SToby Isaac for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[j * dimC + k];} 27109d150b73SToby Isaac for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;} 27119d150b73SToby Isaac for (k = 0; k < numDof; k++) { 27129d150b73SToby Isaac for (l = 0; l < dimC; l++) { 27139d150b73SToby Isaac resNeg[l] -= modes[k * dimC + l] * B[k]; 27149d150b73SToby Isaac for (m = 0; m < dimR; m++) { 27159d150b73SToby Isaac J[l * dimR + m] += modes[k * dimC + l] * D[k * dimR + m]; 27169d150b73SToby Isaac } 27179d150b73SToby Isaac } 27189d150b73SToby Isaac } 27190611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG) 27200611203eSToby Isaac { 27210611203eSToby Isaac PetscReal maxAbs = 0.; 27220611203eSToby Isaac 27230611203eSToby Isaac for (l = 0; l < dimC; l++) { 27240611203eSToby Isaac maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l])); 27250611203eSToby Isaac } 27260611203eSToby Isaac ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr); 27270611203eSToby Isaac } 27280611203eSToby Isaac #endif 27299d150b73SToby Isaac ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr); 27309d150b73SToby Isaac } 27319d150b73SToby Isaac } 2732c6e120d1SToby Isaac ierr = DMRestoreWorkArray(dm,3 * dimC * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr); 2733c6e120d1SToby Isaac ierr = DMRestoreWorkArray(dm,numDof + numDof * dimR + dimC,PETSC_REAL,&B);CHKERRQ(ierr); 2734c6e120d1SToby Isaac ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr); 27359d150b73SToby Isaac ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr); 27369d150b73SToby Isaac PetscFunctionReturn(0); 27379d150b73SToby Isaac } 27389d150b73SToby Isaac 27399d150b73SToby Isaac #undef __FUNCT__ 27409d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates_FE" 27419d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 27429d150b73SToby Isaac { 27439d150b73SToby Isaac PetscInt numComp, numDof, i, j, k, l, coordSize; 2744c6e120d1SToby Isaac PetscScalar *nodes = NULL; 2745c6e120d1SToby Isaac PetscReal *invV, *modes; 27469d150b73SToby Isaac PetscReal *B; 27479d150b73SToby Isaac PetscErrorCode ierr; 27489d150b73SToby Isaac 27499d150b73SToby Isaac PetscFunctionBegin; 27509d150b73SToby Isaac ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr); 27519d150b73SToby Isaac ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr); 27529d150b73SToby Isaac if (numComp != dimC) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,dimC); 27539d150b73SToby Isaac ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr); 27549d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 2755c6e120d1SToby Isaac ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr); 27569d150b73SToby Isaac invV = fe->invV; 27579d150b73SToby Isaac for (i = 0; i < numDof; i++) { 27589d150b73SToby Isaac for (j = 0; j < dimC; j++) { 27599d150b73SToby Isaac modes[i * dimC + j] = 0.; 27609d150b73SToby Isaac for (k = 0; k < numDof; k++) { 2761c6e120d1SToby Isaac modes[i * dimC + j] += invV[i * numDof + k] * PetscRealPart(nodes[k * dimC + j]); 27629d150b73SToby Isaac } 27639d150b73SToby Isaac } 27649d150b73SToby Isaac } 27659b1f03cbSToby Isaac ierr = DMGetWorkArray(dm,numDof,PETSC_REAL,&B);CHKERRQ(ierr); 27669d150b73SToby Isaac for (i = 0; i < numPoints * dimC; i++) {realCoords[i] = 0.;} 27679d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 27689d150b73SToby Isaac const PetscReal *guess = &refCoords[j * dimR]; 27699d150b73SToby Isaac PetscReal *mapped = &realCoords[j * dimC]; 27709d150b73SToby Isaac 27719d150b73SToby Isaac ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, NULL, NULL);CHKERRQ(ierr); 27729d150b73SToby Isaac for (k = 0; k < numDof; k++) { 27739d150b73SToby Isaac for (l = 0; l < dimC; l++) { 27749d150b73SToby Isaac mapped[l] += modes[k * dimC + l] * B[k]; 27759d150b73SToby Isaac } 27769d150b73SToby Isaac } 27779d150b73SToby Isaac } 27789b1f03cbSToby Isaac ierr = DMRestoreWorkArray(dm,numDof,PETSC_REAL,&B);CHKERRQ(ierr); 2779c6e120d1SToby Isaac ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr); 27809d150b73SToby Isaac ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr); 27819d150b73SToby Isaac PetscFunctionReturn(0); 27829d150b73SToby Isaac } 27839d150b73SToby Isaac 27849d150b73SToby Isaac #undef __FUNCT__ 2785d6143a4eSToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference" 2786d6143a4eSToby Isaac /*@ 2787d6143a4eSToby Isaac DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element 2788d6143a4eSToby Isaac map. This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not 2789d6143a4eSToby Isaac extend uniquely outside the reference cell (e.g, most non-affine maps) 2790d6143a4eSToby Isaac 2791d6143a4eSToby Isaac Not collective 2792d6143a4eSToby Isaac 2793d6143a4eSToby Isaac Input Parameters: 2794d6143a4eSToby Isaac + dm - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or 2795d6143a4eSToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 2796d6143a4eSToby Isaac as a multilinear map for tensor-product elements 2797d6143a4eSToby Isaac . cell - the cell whose map is used. 2798d6143a4eSToby Isaac . numPoints - the number of points to locate 2799d6143a4eSToby Isaac + realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim()) 2800d6143a4eSToby Isaac 2801d6143a4eSToby Isaac Output Parameters: 2802d6143a4eSToby Isaac . refCoords - (numPoints x dimension) array of reference coordinates (see DMGetDimension()) 2803d6143a4eSToby Isaac @*/ 2804d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[]) 2805d6143a4eSToby Isaac { 28069d150b73SToby Isaac PetscInt dimC, dimR, depth, cStart, cEnd, cEndInterior, i; 28079d150b73SToby Isaac DM coordDM = NULL; 28089d150b73SToby Isaac Vec coords; 28099d150b73SToby Isaac PetscFE fe = NULL; 28109d150b73SToby Isaac PetscErrorCode ierr; 28119d150b73SToby Isaac 2812d6143a4eSToby Isaac PetscFunctionBegin; 28139d150b73SToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 28149d150b73SToby Isaac ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr); 28159d150b73SToby Isaac ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr); 28169d150b73SToby Isaac if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0); 28179d150b73SToby Isaac ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr); 28189d150b73SToby Isaac ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr); 28199d150b73SToby Isaac ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr); 28209d150b73SToby Isaac if (coordDM) { 28219d150b73SToby Isaac PetscInt coordFields; 28229d150b73SToby Isaac 28239d150b73SToby Isaac ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr); 28249d150b73SToby Isaac if (coordFields) { 28259d150b73SToby Isaac PetscClassId id; 28269d150b73SToby Isaac PetscObject disc; 28279d150b73SToby Isaac 28289d150b73SToby Isaac ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr); 28299d150b73SToby Isaac ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr); 28309d150b73SToby Isaac if (id == PETSCFE_CLASSID) { 28319d150b73SToby Isaac fe = (PetscFE) disc; 28329d150b73SToby Isaac } 28339d150b73SToby Isaac } 28349d150b73SToby Isaac } 28359d150b73SToby Isaac ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr); 28369d150b73SToby Isaac ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr); 28379d150b73SToby Isaac cEnd = cEndInterior > 0 ? cEndInterior : cEnd; 28389d150b73SToby Isaac if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);CHKERRQ(ierr); 28399d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 28409d150b73SToby Isaac PetscInt coneSize; 28419d150b73SToby Isaac PetscBool isSimplex, isTensor; 28429d150b73SToby Isaac 28439d150b73SToby Isaac ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr); 28449d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 28459d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 28469d150b73SToby Isaac if (isSimplex) { 28479d150b73SToby Isaac PetscReal detJ, *v0, *J, *invJ; 28489d150b73SToby Isaac 28499d150b73SToby Isaac ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr); 28509d150b73SToby Isaac J = &v0[dimC]; 28519d150b73SToby Isaac invJ = &J[dimC * dimC]; 28529d150b73SToby Isaac ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr); 28539d150b73SToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */ 28549d150b73SToby Isaac CoordinatesRealToRef(dimC, dimR, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]); 28559d150b73SToby Isaac } 28569d150b73SToby Isaac ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr); 28579d150b73SToby Isaac } else if (isTensor) { 28589d150b73SToby Isaac ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr); 28599d150b73SToby Isaac } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize); 28609d150b73SToby Isaac } else { 28619d150b73SToby Isaac ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr); 28629d150b73SToby Isaac } 28639d150b73SToby Isaac PetscFunctionReturn(0); 28649d150b73SToby Isaac } 28659d150b73SToby Isaac 28669d150b73SToby Isaac #undef __FUNCT__ 28679d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates" 28689d150b73SToby Isaac /*@ 28699d150b73SToby Isaac DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map. 28709d150b73SToby Isaac 28719d150b73SToby Isaac Not collective 28729d150b73SToby Isaac 28739d150b73SToby Isaac Input Parameters: 28749d150b73SToby Isaac + dm - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or 28759d150b73SToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 28769d150b73SToby Isaac as a multilinear map for tensor-product elements 28779d150b73SToby Isaac . cell - the cell whose map is used. 28789d150b73SToby Isaac . numPoints - the number of points to locate 28799d150b73SToby Isaac + refCoords - (numPoints x dimension) array of reference coordinates (see DMGetDimension()) 28809d150b73SToby Isaac 28819d150b73SToby Isaac Output Parameters: 28829d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim()) 28839d150b73SToby Isaac @*/ 28849d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[]) 28859d150b73SToby Isaac { 28869d150b73SToby Isaac PetscInt dimC, dimR, depth, cStart, cEnd, cEndInterior, i; 28879d150b73SToby Isaac DM coordDM = NULL; 28889d150b73SToby Isaac Vec coords; 28899d150b73SToby Isaac PetscFE fe = NULL; 28909d150b73SToby Isaac PetscErrorCode ierr; 28919d150b73SToby Isaac 28929d150b73SToby Isaac PetscFunctionBegin; 28939d150b73SToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 28949d150b73SToby Isaac ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr); 28959d150b73SToby Isaac ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr); 28969d150b73SToby Isaac if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0); 28979d150b73SToby Isaac ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr); 28989d150b73SToby Isaac ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr); 28999d150b73SToby Isaac ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr); 29009d150b73SToby Isaac if (coordDM) { 29019d150b73SToby Isaac PetscInt coordFields; 29029d150b73SToby Isaac 29039d150b73SToby Isaac ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr); 29049d150b73SToby Isaac if (coordFields) { 29059d150b73SToby Isaac PetscClassId id; 29069d150b73SToby Isaac PetscObject disc; 29079d150b73SToby Isaac 29089d150b73SToby Isaac ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr); 29099d150b73SToby Isaac ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr); 29109d150b73SToby Isaac if (id == PETSCFE_CLASSID) { 29119d150b73SToby Isaac fe = (PetscFE) disc; 29129d150b73SToby Isaac } 29139d150b73SToby Isaac } 29149d150b73SToby Isaac } 29159d150b73SToby Isaac ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr); 29169d150b73SToby Isaac ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr); 29179d150b73SToby Isaac cEnd = cEndInterior > 0 ? cEndInterior : cEnd; 29189d150b73SToby Isaac if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);CHKERRQ(ierr); 29199d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 29209d150b73SToby Isaac PetscInt coneSize; 29219d150b73SToby Isaac PetscBool isSimplex, isTensor; 29229d150b73SToby Isaac 29239d150b73SToby Isaac ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr); 29249d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 29259d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 29269d150b73SToby Isaac if (isSimplex) { 29279d150b73SToby Isaac PetscReal detJ, *v0, *J; 29289d150b73SToby Isaac 29299d150b73SToby Isaac ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr); 29309d150b73SToby Isaac J = &v0[dimC]; 29319d150b73SToby Isaac ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr); 29329d150b73SToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */ 29339d150b73SToby Isaac CoordinatesRefToReal(dimC, dimR, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]); 29349d150b73SToby Isaac } 29359d150b73SToby Isaac ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr); 29369d150b73SToby Isaac } else if (isTensor) { 29379d150b73SToby Isaac ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr); 29389d150b73SToby Isaac } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize); 29399d150b73SToby Isaac } else { 29409d150b73SToby Isaac ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr); 29419d150b73SToby Isaac } 2942d6143a4eSToby Isaac PetscFunctionReturn(0); 2943d6143a4eSToby Isaac } 2944