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> 4af74b616SDave May #include <petsctime.h> 5ccd2543fSMatthew G Knepley 63985bb02SVaclav Hapla /*@ 73985bb02SVaclav Hapla DMPlexFindVertices - Try to find DAG points based on their coordinates. 83985bb02SVaclav Hapla 93985bb02SVaclav Hapla Not Collective (provided DMGetCoordinatesLocalSetUp() has been called already) 103985bb02SVaclav Hapla 113985bb02SVaclav Hapla Input Parameters: 123985bb02SVaclav Hapla + dm - The DMPlex object 13d3e1f4ccSVaclav Hapla . coordinates - The Vec of coordinates of the sought points 143985bb02SVaclav Hapla - eps - The tolerance or PETSC_DEFAULT 153985bb02SVaclav Hapla 163985bb02SVaclav Hapla Output Parameters: 17d3e1f4ccSVaclav Hapla . points - The IS of found DAG points or -1 183985bb02SVaclav Hapla 193985bb02SVaclav Hapla Level: intermediate 203985bb02SVaclav Hapla 213985bb02SVaclav Hapla Notes: 22d3e1f4ccSVaclav Hapla The length of Vec coordinates must be npoints * dim where dim is the spatial dimension returned by DMGetCoordinateDim() and npoints is the number of sought points. 233985bb02SVaclav Hapla 24d3e1f4ccSVaclav Hapla The output IS is living on PETSC_COMM_SELF and its length is npoints. 25d3e1f4ccSVaclav Hapla Each rank does the search independently. 26d3e1f4ccSVaclav Hapla If this rank's local DMPlex portion contains the DAG point corresponding to the i-th tuple of coordinates, the i-th entry of the output IS is set to that DAG point, otherwise to -1. 273985bb02SVaclav Hapla 28d3e1f4ccSVaclav Hapla The output IS must be destroyed by user. 293985bb02SVaclav Hapla 303985bb02SVaclav Hapla The tolerance is interpreted as the maximum Euclidean (L2) distance of the sought point from the specified coordinates. 313985bb02SVaclav Hapla 32d3e1f4ccSVaclav Hapla Complexity of this function is currently O(mn) with m number of vertices to find and n number of vertices in the local mesh. This could probably be improved if needed. 33335ef845SVaclav Hapla 3437900f7dSMatthew G. Knepley .seealso: DMPlexCreate(), DMGetCoordinatesLocal() 353985bb02SVaclav Hapla @*/ 36d3e1f4ccSVaclav Hapla PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points) 373985bb02SVaclav Hapla { 3837900f7dSMatthew G. Knepley PetscInt c, cdim, i, j, o, p, vStart, vEnd; 39d3e1f4ccSVaclav Hapla PetscInt npoints; 40d3e1f4ccSVaclav Hapla const PetscScalar *coord; 413985bb02SVaclav Hapla Vec allCoordsVec; 423985bb02SVaclav Hapla const PetscScalar *allCoords; 43d3e1f4ccSVaclav Hapla PetscInt *dagPoints; 443985bb02SVaclav Hapla 453985bb02SVaclav Hapla PetscFunctionBegin; 463985bb02SVaclav Hapla if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON; 479566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 48d3e1f4ccSVaclav Hapla { 49d3e1f4ccSVaclav Hapla PetscInt n; 50d3e1f4ccSVaclav Hapla 519566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(coordinates, &n)); 522c71b3e2SJacob Faibussowitsch PetscCheckFalse(n % cdim,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Given coordinates Vec has local length %D not divisible by coordinate dimension %D of given DM", n, cdim); 53d3e1f4ccSVaclav Hapla npoints = n / cdim; 54d3e1f4ccSVaclav Hapla } 559566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &allCoordsVec)); 569566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(allCoordsVec, &allCoords)); 579566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coord)); 589566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 5976bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 60335ef845SVaclav Hapla /* check coordinate section is consistent with DM dimension */ 61335ef845SVaclav Hapla PetscSection cs; 62335ef845SVaclav Hapla PetscInt ndof; 63335ef845SVaclav Hapla 649566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &cs)); 653985bb02SVaclav Hapla for (p = vStart; p < vEnd; p++) { 669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cs, p, &ndof)); 672c71b3e2SJacob Faibussowitsch PetscCheckFalse(ndof != cdim,PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %D: ndof = %D != %D = cdim", p, ndof, cdim); 68335ef845SVaclav Hapla } 69335ef845SVaclav Hapla } 709566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npoints, &dagPoints)); 71eca9f518SVaclav Hapla if (eps == 0.0) { 7237900f7dSMatthew G. Knepley for (i=0,j=0; i < npoints; i++,j+=cdim) { 73eca9f518SVaclav Hapla dagPoints[i] = -1; 7437900f7dSMatthew G. Knepley for (p = vStart,o=0; p < vEnd; p++,o+=cdim) { 7537900f7dSMatthew G. Knepley for (c = 0; c < cdim; c++) { 76d3e1f4ccSVaclav Hapla if (coord[j+c] != allCoords[o+c]) break; 77eca9f518SVaclav Hapla } 7837900f7dSMatthew G. Knepley if (c == cdim) { 79eca9f518SVaclav Hapla dagPoints[i] = p; 80eca9f518SVaclav Hapla break; 81eca9f518SVaclav Hapla } 82eca9f518SVaclav Hapla } 83eca9f518SVaclav Hapla } 84d3e1f4ccSVaclav Hapla } else { 8537900f7dSMatthew G. Knepley for (i=0,j=0; i < npoints; i++,j+=cdim) { 86d3e1f4ccSVaclav Hapla PetscReal norm; 87d3e1f4ccSVaclav Hapla 88335ef845SVaclav Hapla dagPoints[i] = -1; 8937900f7dSMatthew G. Knepley for (p = vStart,o=0; p < vEnd; p++,o+=cdim) { 903985bb02SVaclav Hapla norm = 0.0; 9137900f7dSMatthew G. Knepley for (c = 0; c < cdim; c++) { 92d3e1f4ccSVaclav Hapla norm += PetscRealPart(PetscSqr(coord[j+c] - allCoords[o+c])); 933985bb02SVaclav Hapla } 943985bb02SVaclav Hapla norm = PetscSqrtReal(norm); 953985bb02SVaclav Hapla if (norm <= eps) { 963985bb02SVaclav Hapla dagPoints[i] = p; 973985bb02SVaclav Hapla break; 983985bb02SVaclav Hapla } 993985bb02SVaclav Hapla } 1003985bb02SVaclav Hapla } 101d3e1f4ccSVaclav Hapla } 1029566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(allCoordsVec, &allCoords)); 1039566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coord)); 1049566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points)); 1053985bb02SVaclav Hapla PetscFunctionReturn(0); 1063985bb02SVaclav Hapla } 1073985bb02SVaclav Hapla 108fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection) 109fea14342SMatthew G. Knepley { 110fea14342SMatthew G. Knepley const PetscReal p0_x = segmentA[0*2+0]; 111fea14342SMatthew G. Knepley const PetscReal p0_y = segmentA[0*2+1]; 112fea14342SMatthew G. Knepley const PetscReal p1_x = segmentA[1*2+0]; 113fea14342SMatthew G. Knepley const PetscReal p1_y = segmentA[1*2+1]; 114fea14342SMatthew G. Knepley const PetscReal p2_x = segmentB[0*2+0]; 115fea14342SMatthew G. Knepley const PetscReal p2_y = segmentB[0*2+1]; 116fea14342SMatthew G. Knepley const PetscReal p3_x = segmentB[1*2+0]; 117fea14342SMatthew G. Knepley const PetscReal p3_y = segmentB[1*2+1]; 118fea14342SMatthew G. Knepley const PetscReal s1_x = p1_x - p0_x; 119fea14342SMatthew G. Knepley const PetscReal s1_y = p1_y - p0_y; 120fea14342SMatthew G. Knepley const PetscReal s2_x = p3_x - p2_x; 121fea14342SMatthew G. Knepley const PetscReal s2_y = p3_y - p2_y; 122fea14342SMatthew G. Knepley const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y); 123fea14342SMatthew G. Knepley 124fea14342SMatthew G. Knepley PetscFunctionBegin; 125fea14342SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 126fea14342SMatthew G. Knepley /* Non-parallel lines */ 127fea14342SMatthew G. Knepley if (denom != 0.0) { 128fea14342SMatthew G. Knepley const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom; 129fea14342SMatthew G. Knepley const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom; 130fea14342SMatthew G. Knepley 131fea14342SMatthew G. Knepley if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { 132fea14342SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 133fea14342SMatthew G. Knepley if (intersection) { 134fea14342SMatthew G. Knepley intersection[0] = p0_x + (t * s1_x); 135fea14342SMatthew G. Knepley intersection[1] = p0_y + (t * s1_y); 136fea14342SMatthew G. Knepley } 137fea14342SMatthew G. Knepley } 138fea14342SMatthew G. Knepley } 139fea14342SMatthew G. Knepley PetscFunctionReturn(0); 140fea14342SMatthew G. Knepley } 141fea14342SMatthew G. Knepley 142ddce0771SMatthew G. Knepley /* The plane is segmentB x segmentC: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */ 143ddce0771SMatthew G. Knepley static PetscErrorCode DMPlexGetLinePlaneIntersection_3D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], const PetscReal segmentC[], PetscReal intersection[], PetscBool *hasIntersection) 144ddce0771SMatthew G. Knepley { 145ddce0771SMatthew G. Knepley const PetscReal p0_x = segmentA[0*3+0]; 146ddce0771SMatthew G. Knepley const PetscReal p0_y = segmentA[0*3+1]; 147ddce0771SMatthew G. Knepley const PetscReal p0_z = segmentA[0*3+2]; 148ddce0771SMatthew G. Knepley const PetscReal p1_x = segmentA[1*3+0]; 149ddce0771SMatthew G. Knepley const PetscReal p1_y = segmentA[1*3+1]; 150ddce0771SMatthew G. Knepley const PetscReal p1_z = segmentA[1*3+2]; 151ddce0771SMatthew G. Knepley const PetscReal q0_x = segmentB[0*3+0]; 152ddce0771SMatthew G. Knepley const PetscReal q0_y = segmentB[0*3+1]; 153ddce0771SMatthew G. Knepley const PetscReal q0_z = segmentB[0*3+2]; 154ddce0771SMatthew G. Knepley const PetscReal q1_x = segmentB[1*3+0]; 155ddce0771SMatthew G. Knepley const PetscReal q1_y = segmentB[1*3+1]; 156ddce0771SMatthew G. Knepley const PetscReal q1_z = segmentB[1*3+2]; 157ddce0771SMatthew G. Knepley const PetscReal r0_x = segmentC[0*3+0]; 158ddce0771SMatthew G. Knepley const PetscReal r0_y = segmentC[0*3+1]; 159ddce0771SMatthew G. Knepley const PetscReal r0_z = segmentC[0*3+2]; 160ddce0771SMatthew G. Knepley const PetscReal r1_x = segmentC[1*3+0]; 161ddce0771SMatthew G. Knepley const PetscReal r1_y = segmentC[1*3+1]; 162ddce0771SMatthew G. Knepley const PetscReal r1_z = segmentC[1*3+2]; 163ddce0771SMatthew G. Knepley const PetscReal s0_x = p1_x - p0_x; 164ddce0771SMatthew G. Knepley const PetscReal s0_y = p1_y - p0_y; 165ddce0771SMatthew G. Knepley const PetscReal s0_z = p1_z - p0_z; 166ddce0771SMatthew G. Knepley const PetscReal s1_x = q1_x - q0_x; 167ddce0771SMatthew G. Knepley const PetscReal s1_y = q1_y - q0_y; 168ddce0771SMatthew G. Knepley const PetscReal s1_z = q1_z - q0_z; 169ddce0771SMatthew G. Knepley const PetscReal s2_x = r1_x - r0_x; 170ddce0771SMatthew G. Knepley const PetscReal s2_y = r1_y - r0_y; 171ddce0771SMatthew G. Knepley const PetscReal s2_z = r1_z - r0_z; 172ddce0771SMatthew G. Knepley const PetscReal s3_x = s1_y*s2_z - s1_z*s2_y; /* s1 x s2 */ 173ddce0771SMatthew G. Knepley const PetscReal s3_y = s1_z*s2_x - s1_x*s2_z; 174ddce0771SMatthew G. Knepley const PetscReal s3_z = s1_x*s2_y - s1_y*s2_x; 175ddce0771SMatthew G. Knepley const PetscReal s4_x = s0_y*s2_z - s0_z*s2_y; /* s0 x s2 */ 176ddce0771SMatthew G. Knepley const PetscReal s4_y = s0_z*s2_x - s0_x*s2_z; 177ddce0771SMatthew G. Knepley const PetscReal s4_z = s0_x*s2_y - s0_y*s2_x; 178ddce0771SMatthew G. Knepley const PetscReal s5_x = s1_y*s0_z - s1_z*s0_y; /* s1 x s0 */ 179ddce0771SMatthew G. Knepley const PetscReal s5_y = s1_z*s0_x - s1_x*s0_z; 180ddce0771SMatthew G. Knepley const PetscReal s5_z = s1_x*s0_y - s1_y*s0_x; 181ddce0771SMatthew G. Knepley const PetscReal denom = -(s0_x*s3_x + s0_y*s3_y + s0_z*s3_z); /* -s0 . (s1 x s2) */ 182ddce0771SMatthew G. Knepley 183ddce0771SMatthew G. Knepley PetscFunctionBegin; 184ddce0771SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 185ddce0771SMatthew G. Knepley /* Line not parallel to plane */ 186ddce0771SMatthew G. Knepley if (denom != 0.0) { 187ddce0771SMatthew G. Knepley const PetscReal t = (s3_x * (p0_x - q0_x) + s3_y * (p0_y - q0_y) + s3_z * (p0_z - q0_z)) / denom; 188ddce0771SMatthew G. Knepley const PetscReal u = (s4_x * (p0_x - q0_x) + s4_y * (p0_y - q0_y) + s4_z * (p0_z - q0_z)) / denom; 189ddce0771SMatthew G. Knepley const PetscReal v = (s5_x * (p0_x - q0_x) + s5_y * (p0_y - q0_y) + s5_z * (p0_z - q0_z)) / denom; 190ddce0771SMatthew G. Knepley 191ddce0771SMatthew G. Knepley if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && v >= 0 && v <= 1) { 192ddce0771SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 193ddce0771SMatthew G. Knepley if (intersection) { 194ddce0771SMatthew G. Knepley intersection[0] = p0_x + (t * s0_x); 195ddce0771SMatthew G. Knepley intersection[1] = p0_y + (t * s0_y); 196ddce0771SMatthew G. Knepley intersection[2] = p0_z + (t * s0_z); 197ddce0771SMatthew G. Knepley } 198ddce0771SMatthew G. Knepley } 199ddce0771SMatthew G. Knepley } 200ddce0771SMatthew G. Knepley PetscFunctionReturn(0); 201ddce0771SMatthew G. Knepley } 202ddce0771SMatthew G. Knepley 20314bbb9f0SLawrence Mitchell static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 20414bbb9f0SLawrence Mitchell { 20514bbb9f0SLawrence Mitchell const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 20614bbb9f0SLawrence Mitchell const PetscReal x = PetscRealPart(point[0]); 20714bbb9f0SLawrence Mitchell PetscReal v0, J, invJ, detJ; 20814bbb9f0SLawrence Mitchell PetscReal xi; 20914bbb9f0SLawrence Mitchell 21014bbb9f0SLawrence Mitchell PetscFunctionBegin; 2119566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ)); 21214bbb9f0SLawrence Mitchell xi = invJ*(x - v0); 21314bbb9f0SLawrence Mitchell 21414bbb9f0SLawrence Mitchell if ((xi >= -eps) && (xi <= 2.+eps)) *cell = c; 21514bbb9f0SLawrence Mitchell else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 21614bbb9f0SLawrence Mitchell PetscFunctionReturn(0); 21714bbb9f0SLawrence Mitchell } 21814bbb9f0SLawrence Mitchell 219ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 220ccd2543fSMatthew G Knepley { 221ccd2543fSMatthew G Knepley const PetscInt embedDim = 2; 222f5ebc837SMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 223ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 224ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 225ccd2543fSMatthew G Knepley PetscReal v0[2], J[4], invJ[4], detJ; 226ccd2543fSMatthew G Knepley PetscReal xi, eta; 227ccd2543fSMatthew G Knepley 228ccd2543fSMatthew G Knepley PetscFunctionBegin; 2299566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 230ccd2543fSMatthew G Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]); 231ccd2543fSMatthew G Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]); 232ccd2543fSMatthew G Knepley 233f5ebc837SMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c; 234c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 235ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 236ccd2543fSMatthew G Knepley } 237ccd2543fSMatthew G Knepley 23862a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[]) 23962a38674SMatthew G. Knepley { 24062a38674SMatthew G. Knepley const PetscInt embedDim = 2; 24162a38674SMatthew G. Knepley PetscReal x = PetscRealPart(point[0]); 24262a38674SMatthew G. Knepley PetscReal y = PetscRealPart(point[1]); 24362a38674SMatthew G. Knepley PetscReal v0[2], J[4], invJ[4], detJ; 24462a38674SMatthew G. Knepley PetscReal xi, eta, r; 24562a38674SMatthew G. Knepley 24662a38674SMatthew G. Knepley PetscFunctionBegin; 2479566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 24862a38674SMatthew G. Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]); 24962a38674SMatthew G. Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]); 25062a38674SMatthew G. Knepley 25162a38674SMatthew G. Knepley xi = PetscMax(xi, 0.0); 25262a38674SMatthew G. Knepley eta = PetscMax(eta, 0.0); 25362a38674SMatthew G. Knepley if (xi + eta > 2.0) { 25462a38674SMatthew G. Knepley r = (xi + eta)/2.0; 25562a38674SMatthew G. Knepley xi /= r; 25662a38674SMatthew G. Knepley eta /= r; 25762a38674SMatthew G. Knepley } 25862a38674SMatthew G. Knepley cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0]; 25962a38674SMatthew G. Knepley cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1]; 26062a38674SMatthew G. Knepley PetscFunctionReturn(0); 26162a38674SMatthew G. Knepley } 26262a38674SMatthew G. Knepley 263ba2698f1SMatthew G. Knepley static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 264ccd2543fSMatthew G Knepley { 265ccd2543fSMatthew G Knepley PetscSection coordSection; 266ccd2543fSMatthew G Knepley Vec coordsLocal; 267a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 268ccd2543fSMatthew G Knepley const PetscInt faces[8] = {0, 1, 1, 2, 2, 3, 3, 0}; 269ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 270ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 271ccd2543fSMatthew G Knepley PetscInt crossings = 0, f; 272ccd2543fSMatthew G Knepley 273ccd2543fSMatthew G Knepley PetscFunctionBegin; 2749566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal)); 2759566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 2769566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords)); 277ccd2543fSMatthew G Knepley for (f = 0; f < 4; ++f) { 278ccd2543fSMatthew G Knepley PetscReal x_i = PetscRealPart(coords[faces[2*f+0]*2+0]); 279ccd2543fSMatthew G Knepley PetscReal y_i = PetscRealPart(coords[faces[2*f+0]*2+1]); 280ccd2543fSMatthew G Knepley PetscReal x_j = PetscRealPart(coords[faces[2*f+1]*2+0]); 281ccd2543fSMatthew G Knepley PetscReal y_j = PetscRealPart(coords[faces[2*f+1]*2+1]); 282ccd2543fSMatthew G Knepley PetscReal slope = (y_j - y_i) / (x_j - x_i); 283ccd2543fSMatthew G Knepley PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE; 284ccd2543fSMatthew G Knepley PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE; 285ccd2543fSMatthew G Knepley PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE; 286ccd2543fSMatthew G Knepley if ((cond1 || cond2) && above) ++crossings; 287ccd2543fSMatthew G Knepley } 288ccd2543fSMatthew G Knepley if (crossings % 2) *cell = c; 289c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 2909566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords)); 291ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 292ccd2543fSMatthew G Knepley } 293ccd2543fSMatthew G Knepley 294ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 295ccd2543fSMatthew G Knepley { 296ccd2543fSMatthew G Knepley const PetscInt embedDim = 3; 29737900f7dSMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 298ccd2543fSMatthew G Knepley PetscReal v0[3], J[9], invJ[9], detJ; 299ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 300ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 301ccd2543fSMatthew G Knepley PetscReal z = PetscRealPart(point[2]); 302ccd2543fSMatthew G Knepley PetscReal xi, eta, zeta; 303ccd2543fSMatthew G Knepley 304ccd2543fSMatthew G Knepley PetscFunctionBegin; 3059566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 306ccd2543fSMatthew G Knepley xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]); 307ccd2543fSMatthew G Knepley eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]); 308ccd2543fSMatthew G Knepley zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]); 309ccd2543fSMatthew G Knepley 31037900f7dSMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0+eps)) *cell = c; 311c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 312ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 313ccd2543fSMatthew G Knepley } 314ccd2543fSMatthew G Knepley 315ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 316ccd2543fSMatthew G Knepley { 317ccd2543fSMatthew G Knepley PetscSection coordSection; 318ccd2543fSMatthew G Knepley Vec coordsLocal; 319872a9804SMatthew G. Knepley PetscScalar *coords = NULL; 320fb150da6SMatthew G. Knepley const PetscInt faces[24] = {0, 3, 2, 1, 5, 4, 7, 6, 3, 0, 4, 5, 321fb150da6SMatthew G. Knepley 1, 2, 6, 7, 3, 5, 6, 2, 0, 1, 7, 4}; 322ccd2543fSMatthew G Knepley PetscBool found = PETSC_TRUE; 323ccd2543fSMatthew G Knepley PetscInt f; 324ccd2543fSMatthew G Knepley 325ccd2543fSMatthew G Knepley PetscFunctionBegin; 3269566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal)); 3279566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 3289566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords)); 329ccd2543fSMatthew G Knepley for (f = 0; f < 6; ++f) { 330ccd2543fSMatthew G Knepley /* Check the point is under plane */ 331ccd2543fSMatthew G Knepley /* Get face normal */ 332ccd2543fSMatthew G Knepley PetscReal v_i[3]; 333ccd2543fSMatthew G Knepley PetscReal v_j[3]; 334ccd2543fSMatthew G Knepley PetscReal normal[3]; 335ccd2543fSMatthew G Knepley PetscReal pp[3]; 336ccd2543fSMatthew G Knepley PetscReal dot; 337ccd2543fSMatthew G Knepley 338ccd2543fSMatthew G Knepley v_i[0] = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]); 339ccd2543fSMatthew G Knepley v_i[1] = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]); 340ccd2543fSMatthew G Knepley v_i[2] = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]); 341ccd2543fSMatthew G Knepley v_j[0] = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]); 342ccd2543fSMatthew G Knepley v_j[1] = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]); 343ccd2543fSMatthew G Knepley v_j[2] = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]); 344ccd2543fSMatthew G Knepley normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1]; 345ccd2543fSMatthew G Knepley normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2]; 346ccd2543fSMatthew G Knepley normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0]; 347ccd2543fSMatthew G Knepley pp[0] = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]); 348ccd2543fSMatthew G Knepley pp[1] = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]); 349ccd2543fSMatthew G Knepley pp[2] = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]); 350ccd2543fSMatthew G Knepley dot = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2]; 351ccd2543fSMatthew G Knepley 352ccd2543fSMatthew G Knepley /* Check that projected point is in face (2D location problem) */ 353ccd2543fSMatthew G Knepley if (dot < 0.0) { 354ccd2543fSMatthew G Knepley found = PETSC_FALSE; 355ccd2543fSMatthew G Knepley break; 356ccd2543fSMatthew G Knepley } 357ccd2543fSMatthew G Knepley } 358ccd2543fSMatthew G Knepley if (found) *cell = c; 359c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 3609566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords)); 361ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 362ccd2543fSMatthew G Knepley } 363ccd2543fSMatthew G Knepley 364c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[]) 365c4eade1cSMatthew G. Knepley { 366c4eade1cSMatthew G. Knepley PetscInt d; 367c4eade1cSMatthew G. Knepley 368c4eade1cSMatthew G. Knepley PetscFunctionBegin; 369c4eade1cSMatthew G. Knepley box->dim = dim; 370c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]); 371c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 372c4eade1cSMatthew G. Knepley } 373c4eade1cSMatthew G. Knepley 374c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box) 375c4eade1cSMatthew G. Knepley { 376c4eade1cSMatthew G. Knepley PetscFunctionBegin; 3779566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(1, box)); 3789566063dSJacob Faibussowitsch PetscCall(PetscGridHashInitialize_Internal(*box, dim, point)); 379c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 380c4eade1cSMatthew G. Knepley } 381c4eade1cSMatthew G. Knepley 382c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[]) 383c4eade1cSMatthew G. Knepley { 384c4eade1cSMatthew G. Knepley PetscInt d; 385c4eade1cSMatthew G. Knepley 386c4eade1cSMatthew G. Knepley PetscFunctionBegin; 387c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 388c4eade1cSMatthew G. Knepley box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d])); 389c4eade1cSMatthew G. Knepley box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d])); 390c4eade1cSMatthew G. Knepley } 391c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 392c4eade1cSMatthew G. Knepley } 393c4eade1cSMatthew G. Knepley 39462a38674SMatthew G. Knepley /* 39562a38674SMatthew G. Knepley PetscGridHashSetGrid - Divide the grid into boxes 39662a38674SMatthew G. Knepley 39762a38674SMatthew G. Knepley Not collective 39862a38674SMatthew G. Knepley 39962a38674SMatthew G. Knepley Input Parameters: 40062a38674SMatthew G. Knepley + box - The grid hash object 40162a38674SMatthew G. Knepley . n - The number of boxes in each dimension, or PETSC_DETERMINE 40262a38674SMatthew G. Knepley - h - The box size in each dimension, only used if n[d] == PETSC_DETERMINE 40362a38674SMatthew G. Knepley 40462a38674SMatthew G. Knepley Level: developer 40562a38674SMatthew G. Knepley 40662a38674SMatthew G. Knepley .seealso: PetscGridHashCreate() 40762a38674SMatthew G. Knepley */ 408c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[]) 409c4eade1cSMatthew G. Knepley { 410c4eade1cSMatthew G. Knepley PetscInt d; 411c4eade1cSMatthew G. Knepley 412c4eade1cSMatthew G. Knepley PetscFunctionBegin; 413c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 414c4eade1cSMatthew G. Knepley box->extent[d] = box->upper[d] - box->lower[d]; 415c4eade1cSMatthew G. Knepley if (n[d] == PETSC_DETERMINE) { 416c4eade1cSMatthew G. Knepley box->h[d] = h[d]; 417c4eade1cSMatthew G. Knepley box->n[d] = PetscCeilReal(box->extent[d]/h[d]); 418c4eade1cSMatthew G. Knepley } else { 419c4eade1cSMatthew G. Knepley box->n[d] = n[d]; 420c4eade1cSMatthew G. Knepley box->h[d] = box->extent[d]/n[d]; 421c4eade1cSMatthew G. Knepley } 422c4eade1cSMatthew G. Knepley } 423c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 424c4eade1cSMatthew G. Knepley } 425c4eade1cSMatthew G. Knepley 42662a38674SMatthew G. Knepley /* 42762a38674SMatthew G. Knepley PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point 42862a38674SMatthew G. Knepley 42962a38674SMatthew G. Knepley Not collective 43062a38674SMatthew G. Knepley 43162a38674SMatthew G. Knepley Input Parameters: 43262a38674SMatthew G. Knepley + box - The grid hash object 43362a38674SMatthew G. Knepley . numPoints - The number of input points 43462a38674SMatthew G. Knepley - points - The input point coordinates 43562a38674SMatthew G. Knepley 43662a38674SMatthew G. Knepley Output Parameters: 43762a38674SMatthew G. Knepley + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 43862a38674SMatthew G. Knepley - boxes - An array of numPoints integers expressing the enclosing box as single number, or NULL 43962a38674SMatthew G. Knepley 44062a38674SMatthew G. Knepley Level: developer 44162a38674SMatthew G. Knepley 44262a38674SMatthew G. Knepley .seealso: PetscGridHashCreate() 44362a38674SMatthew G. Knepley */ 4441c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[]) 445c4eade1cSMatthew G. Knepley { 446c4eade1cSMatthew G. Knepley const PetscReal *lower = box->lower; 447c4eade1cSMatthew G. Knepley const PetscReal *upper = box->upper; 448c4eade1cSMatthew G. Knepley const PetscReal *h = box->h; 449c4eade1cSMatthew G. Knepley const PetscInt *n = box->n; 450c4eade1cSMatthew G. Knepley const PetscInt dim = box->dim; 451c4eade1cSMatthew G. Knepley PetscInt d, p; 452c4eade1cSMatthew G. Knepley 453c4eade1cSMatthew G. Knepley PetscFunctionBegin; 454c4eade1cSMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 455c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) { 4561c6dfc3eSMatthew G. Knepley PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]); 457c4eade1cSMatthew G. Knepley 4581c6dfc3eSMatthew G. Knepley if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1; 4592a705cacSMatthew G. Knepley if (dbox == -1 && PetscAbsReal(PetscRealPart(points[p*dim+d]) - lower[d]) < 1.0e-9) dbox = 0; 4602c71b3e2SJacob Faibussowitsch PetscCheckFalse(dbox < 0 || dbox >= n[d],PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %d (%g, %g, %g) is outside of our bounding box", 461087ef6b2SMatthew G. Knepley p, (double) PetscRealPart(points[p*dim+0]), dim > 1 ? (double) PetscRealPart(points[p*dim+1]) : 0.0, dim > 2 ? (double) PetscRealPart(points[p*dim+2]) : 0.0); 462c4eade1cSMatthew G. Knepley dboxes[p*dim+d] = dbox; 463c4eade1cSMatthew G. Knepley } 464ddce0771SMatthew G. Knepley if (boxes) for (d = dim-2, boxes[p] = dboxes[p*dim+dim-1]; d >= 0; --d) boxes[p] = boxes[p]*n[d] + dboxes[p*dim+d]; 465c4eade1cSMatthew G. Knepley } 466c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 467c4eade1cSMatthew G. Knepley } 468c4eade1cSMatthew G. Knepley 469af74b616SDave May /* 470af74b616SDave May PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point 471af74b616SDave May 472af74b616SDave May Not collective 473af74b616SDave May 474af74b616SDave May Input Parameters: 475af74b616SDave May + box - The grid hash object 476af74b616SDave May . numPoints - The number of input points 477af74b616SDave May - points - The input point coordinates 478af74b616SDave May 479af74b616SDave May Output Parameters: 480af74b616SDave May + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 481af74b616SDave May . boxes - An array of numPoints integers expressing the enclosing box as single number, or NULL 482af74b616SDave May - found - Flag indicating if point was located within a box 483af74b616SDave May 484af74b616SDave May Level: developer 485af74b616SDave May 486af74b616SDave May .seealso: PetscGridHashGetEnclosingBox() 487af74b616SDave May */ 488af74b616SDave May PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[],PetscBool *found) 489af74b616SDave May { 490af74b616SDave May const PetscReal *lower = box->lower; 491af74b616SDave May const PetscReal *upper = box->upper; 492af74b616SDave May const PetscReal *h = box->h; 493af74b616SDave May const PetscInt *n = box->n; 494af74b616SDave May const PetscInt dim = box->dim; 495af74b616SDave May PetscInt d, p; 496af74b616SDave May 497af74b616SDave May PetscFunctionBegin; 498af74b616SDave May *found = PETSC_FALSE; 499af74b616SDave May for (p = 0; p < numPoints; ++p) { 500af74b616SDave May for (d = 0; d < dim; ++d) { 501af74b616SDave May PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]); 502af74b616SDave May 503af74b616SDave May if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1; 504af74b616SDave May if (dbox < 0 || dbox >= n[d]) { 505af74b616SDave May PetscFunctionReturn(0); 506af74b616SDave May } 507af74b616SDave May dboxes[p*dim+d] = dbox; 508af74b616SDave May } 509ddce0771SMatthew G. Knepley if (boxes) for (d = dim-2, boxes[p] = dboxes[p*dim+dim-1]; d >= 0; --d) boxes[p] = boxes[p]*n[d] + dboxes[p*dim+d]; 510af74b616SDave May } 511af74b616SDave May *found = PETSC_TRUE; 512af74b616SDave May PetscFunctionReturn(0); 513af74b616SDave May } 514af74b616SDave May 515c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box) 516c4eade1cSMatthew G. Knepley { 517c4eade1cSMatthew G. Knepley PetscFunctionBegin; 518c4eade1cSMatthew G. Knepley if (*box) { 5199566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&(*box)->cellSection)); 5209566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*box)->cells)); 5219566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&(*box)->cellsSparse)); 522c4eade1cSMatthew G. Knepley } 5239566063dSJacob Faibussowitsch PetscCall(PetscFree(*box)); 524c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 525c4eade1cSMatthew G. Knepley } 526c4eade1cSMatthew G. Knepley 527cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell) 528cafe43deSMatthew G. Knepley { 529ba2698f1SMatthew G. Knepley DMPolytopeType ct; 530cafe43deSMatthew G. Knepley 531cafe43deSMatthew G. Knepley PetscFunctionBegin; 5329566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cellStart, &ct)); 533ba2698f1SMatthew G. Knepley switch (ct) { 53414bbb9f0SLawrence Mitchell case DM_POLYTOPE_SEGMENT: 5359566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell));break; 536ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 5379566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell));break; 538ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 5399566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell));break; 540ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 5419566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell));break; 542ba2698f1SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 5439566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell));break; 54498921bdaSJacob Faibussowitsch default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %D with type %s", cellStart, DMPolytopeTypes[ct]); 545cafe43deSMatthew G. Knepley } 546cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 547cafe43deSMatthew G. Knepley } 548cafe43deSMatthew G. Knepley 54962a38674SMatthew G. Knepley /* 55062a38674SMatthew G. Knepley DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point 55162a38674SMatthew G. Knepley */ 55262a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[]) 55362a38674SMatthew G. Knepley { 554ba2698f1SMatthew G. Knepley DMPolytopeType ct; 55562a38674SMatthew G. Knepley 55662a38674SMatthew G. Knepley PetscFunctionBegin; 5579566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 558ba2698f1SMatthew G. Knepley switch (ct) { 559ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 5609566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint));break; 56162a38674SMatthew G. Knepley #if 0 562ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 5639566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint));break; 564ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 5659566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint));break; 566ba2698f1SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 5679566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint));break; 56862a38674SMatthew G. Knepley #endif 56998921bdaSJacob Faibussowitsch default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %D with type %s", cell, DMPolytopeTypes[ct]); 57062a38674SMatthew G. Knepley } 57162a38674SMatthew G. Knepley PetscFunctionReturn(0); 57262a38674SMatthew G. Knepley } 57362a38674SMatthew G. Knepley 57462a38674SMatthew G. Knepley /* 57562a38674SMatthew G. Knepley DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex 57662a38674SMatthew G. Knepley 577d083f849SBarry Smith Collective on dm 57862a38674SMatthew G. Knepley 57962a38674SMatthew G. Knepley Input Parameter: 58062a38674SMatthew G. Knepley . dm - The Plex 58162a38674SMatthew G. Knepley 58262a38674SMatthew G. Knepley Output Parameter: 58362a38674SMatthew G. Knepley . localBox - The grid hash object 58462a38674SMatthew G. Knepley 58562a38674SMatthew G. Knepley Level: developer 58662a38674SMatthew G. Knepley 58762a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox() 58862a38674SMatthew G. Knepley */ 589cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox) 590cafe43deSMatthew G. Knepley { 591ddce0771SMatthew G. Knepley const PetscInt debug = 0; 592cafe43deSMatthew G. Knepley MPI_Comm comm; 593cafe43deSMatthew G. Knepley PetscGridHash lbox; 594cafe43deSMatthew G. Knepley Vec coordinates; 595cafe43deSMatthew G. Knepley PetscSection coordSection; 596cafe43deSMatthew G. Knepley Vec coordsLocal; 597cafe43deSMatthew G. Knepley const PetscScalar *coords; 598ddce0771SMatthew G. Knepley PetscScalar *edgeCoords; 599722d0f5cSMatthew G. Knepley PetscInt *dboxes, *boxes; 600ddce0771SMatthew G. Knepley PetscInt n[3] = {2, 2, 2}; 601ddce0771SMatthew G. Knepley PetscInt dim, N, maxConeSize, cStart, cEnd, c, eStart, eEnd, i; 602ddce0771SMatthew G. Knepley PetscBool flg; 603cafe43deSMatthew G. Knepley 604cafe43deSMatthew G. Knepley PetscFunctionBegin; 6059566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject) dm, &comm)); 6069566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 6079566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dim)); 6089566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL)); 6099566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 6109566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(coordinates, &N)); 6119566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coords)); 6129566063dSJacob Faibussowitsch PetscCall(PetscGridHashCreate(comm, dim, coords, &lbox)); 6139566063dSJacob Faibussowitsch for (i = 0; i < N; i += dim) PetscCall(PetscGridHashEnlarge(lbox, &coords[i])); 6149566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coords)); 615ddce0771SMatthew G. Knepley c = dim; 6169566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject) dm)->prefix, "-dm_plex_hash_box_faces", n, &c, &flg)); 617ddce0771SMatthew G. Knepley if (flg) {for (i = c; i < dim; ++i) n[i] = n[c-1];} 618ddce0771SMatthew G. Knepley else {for (i = 0; i < dim; ++i) n[i] = PetscMax(2, PetscFloorReal(PetscPowReal((PetscReal) (cEnd - cStart), 1.0/dim) * 0.8));} 6199566063dSJacob Faibussowitsch PetscCall(PetscGridHashSetGrid(lbox, n, NULL)); 620cafe43deSMatthew G. Knepley #if 0 621cafe43deSMatthew G. Knepley /* Could define a custom reduction to merge these */ 6221c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm)); 6231c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm)); 624cafe43deSMatthew G. Knepley #endif 625cafe43deSMatthew G. Knepley /* Is there a reason to snap the local bounding box to a division of the global box? */ 626cafe43deSMatthew G. Knepley /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */ 627cafe43deSMatthew G. Knepley /* Create label */ 6289566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd)); 629b26b5bf9SMatthew G. Knepley if (dim < 2) eStart = eEnd = -1; 6309566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse)); 6319566063dSJacob Faibussowitsch PetscCall(DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd)); 632a8d69d7bSBarry Smith /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */ 6339566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal)); 6349566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 6359566063dSJacob Faibussowitsch PetscCall(PetscCalloc3(16 * dim, &dboxes, 16, &boxes, PetscPowInt(maxConeSize, dim) * dim, &edgeCoords)); 636cafe43deSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 637cafe43deSMatthew G. Knepley const PetscReal *h = lbox->h; 638cafe43deSMatthew G. Knepley PetscScalar *ccoords = NULL; 63938353de4SMatthew G. Knepley PetscInt csize = 0; 640ddce0771SMatthew G. Knepley PetscInt *closure = NULL; 641ddce0771SMatthew G. Knepley PetscInt Ncl, cl, Ne = 0; 642cafe43deSMatthew G. Knepley PetscScalar point[3]; 643cafe43deSMatthew G. Knepley PetscInt dlim[6], d, e, i, j, k; 644cafe43deSMatthew G. Knepley 645ddce0771SMatthew G. Knepley /* Get all edges in cell */ 6469566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure)); 647ddce0771SMatthew G. Knepley for (cl = 0; cl < Ncl*2; ++cl) { 648ddce0771SMatthew G. Knepley if ((closure[cl] >= eStart) && (closure[cl] < eEnd)) { 649ddce0771SMatthew G. Knepley PetscScalar *ecoords = &edgeCoords[Ne*dim*2]; 650ddce0771SMatthew G. Knepley PetscInt ecsize = dim*2; 651ddce0771SMatthew G. Knepley 6529566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, closure[cl], &ecsize, &ecoords)); 6532c71b3e2SJacob Faibussowitsch PetscCheckFalse(ecsize != dim*2,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Got %D coords for edge, instead of %D", ecsize, dim*2); 654ddce0771SMatthew G. Knepley ++Ne; 655ddce0771SMatthew G. Knepley } 656ddce0771SMatthew G. Knepley } 6579566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure)); 658cafe43deSMatthew G. Knepley /* Find boxes enclosing each vertex */ 6599566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords)); 6609566063dSJacob Faibussowitsch PetscCall(PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes)); 661722d0f5cSMatthew G. Knepley /* Mark cells containing the vertices */ 662ddce0771SMatthew G. Knepley for (e = 0; e < csize/dim; ++e) { 6639566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %D has vertex in box %D (%D, %D, %D)\n", c, boxes[e], dboxes[e*dim+0], dim > 1 ? dboxes[e*dim+1] : -1, dim > 2 ? dboxes[e*dim+2] : -1)); 6649566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(lbox->cellsSparse, c, boxes[e])); 665ddce0771SMatthew G. Knepley } 666cafe43deSMatthew G. Knepley /* Get grid of boxes containing these */ 667cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];} 6682291669eSMatthew G. Knepley for (d = dim; d < 3; ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;} 669cafe43deSMatthew G. Knepley for (e = 1; e < dim+1; ++e) { 670cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) { 671cafe43deSMatthew G. Knepley dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]); 672cafe43deSMatthew G. Knepley dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]); 673cafe43deSMatthew G. Knepley } 674cafe43deSMatthew G. Knepley } 675fea14342SMatthew G. Knepley /* Check for intersection of box with cell */ 676cafe43deSMatthew 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]) { 677cafe43deSMatthew 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]) { 678cafe43deSMatthew 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]) { 679cafe43deSMatthew G. Knepley const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i; 680cafe43deSMatthew G. Knepley PetscScalar cpoint[3]; 681fea14342SMatthew G. Knepley PetscInt cell, edge, ii, jj, kk; 682cafe43deSMatthew G. Knepley 6839566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Box %D: (%.2g, %.2g, %.2g) -- (%.2g, %.2g, %.2g)\n", box, point[0], point[1], point[2], point[0] + h[0], point[1] + h[1], point[2] + h[2])); 684ddce0771SMatthew G. Knepley /* Check whether cell contains any vertex of this subbox TODO vectorize this */ 685cafe43deSMatthew G. Knepley for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) { 686cafe43deSMatthew G. Knepley for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) { 687cafe43deSMatthew G. Knepley for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) { 688cafe43deSMatthew G. Knepley 6899566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell)); 6900b6bfacdSStefano Zampini if (cell >= 0) { 6919566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %D contains vertex (%.2g, %.2g, %.2g) of box %D\n", c, cpoint[0], cpoint[1], cpoint[2], box)); 6929566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 6930b6bfacdSStefano Zampini jj = kk = 2; 6940b6bfacdSStefano Zampini break; 6950b6bfacdSStefano Zampini } 696cafe43deSMatthew G. Knepley } 697cafe43deSMatthew G. Knepley } 698cafe43deSMatthew G. Knepley } 699ddce0771SMatthew G. Knepley /* Check whether cell edge intersects any face of these subboxes TODO vectorize this */ 700ddce0771SMatthew G. Knepley for (edge = 0; edge < Ne; ++edge) { 701a5cae605SSatish Balay PetscReal segA[6] = {0.,0.,0.,0.,0.,0.}; 702a5cae605SSatish Balay PetscReal segB[6] = {0.,0.,0.,0.,0.,0.}; 703a5cae605SSatish Balay PetscReal segC[6] = {0.,0.,0.,0.,0.,0.}; 704fea14342SMatthew G. Knepley 7052c71b3e2SJacob Faibussowitsch PetscCheckFalse(dim > 3,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected dim %d > 3",dim); 706ddce0771SMatthew G. Knepley for (d = 0; d < dim*2; ++d) segA[d] = PetscRealPart(edgeCoords[edge*dim*2+d]); 707ddce0771SMatthew G. Knepley /* 1D: (x) -- (x+h) 0 -- 1 708ddce0771SMatthew G. Knepley 2D: (x, y) -- (x, y+h) (0, 0) -- (0, 1) 709ddce0771SMatthew G. Knepley (x+h, y) -- (x+h, y+h) (1, 0) -- (1, 1) 710ddce0771SMatthew G. Knepley (x, y) -- (x+h, y) (0, 0) -- (1, 0) 711ddce0771SMatthew G. Knepley (x, y+h) -- (x+h, y+h) (0, 1) -- (1, 1) 712ddce0771SMatthew G. Knepley 3D: (x, y, z) -- (x, y+h, z), (x, y, z) -- (x, y, z+h) (0, 0, 0) -- (0, 1, 0), (0, 0, 0) -- (0, 0, 1) 713ddce0771SMatthew G. Knepley (x+h, y, z) -- (x+h, y+h, z), (x+h, y, z) -- (x+h, y, z+h) (1, 0, 0) -- (1, 1, 0), (1, 0, 0) -- (1, 0, 1) 714ddce0771SMatthew G. Knepley (x, y, z) -- (x+h, y, z), (x, y, z) -- (x, y, z+h) (0, 0, 0) -- (1, 0, 0), (0, 0, 0) -- (0, 0, 1) 715ddce0771SMatthew G. Knepley (x, y+h, z) -- (x+h, y+h, z), (x, y+h, z) -- (x, y+h, z+h) (0, 1, 0) -- (1, 1, 0), (0, 1, 0) -- (0, 1, 1) 716ddce0771SMatthew G. Knepley (x, y, z) -- (x+h, y, z), (x, y, z) -- (x, y+h, z) (0, 0, 0) -- (1, 0, 0), (0, 0, 0) -- (0, 1, 0) 717ddce0771SMatthew G. Knepley (x, y, z+h) -- (x+h, y, z+h), (x, y, z+h) -- (x, y+h, z+h) (0, 0, 1) -- (1, 0, 1), (0, 0, 1) -- (0, 1, 1) 718ddce0771SMatthew G. Knepley */ 719ddce0771SMatthew G. Knepley /* Loop over faces with normal in direction d */ 720ddce0771SMatthew G. Knepley for (d = 0; d < dim; ++d) { 721ddce0771SMatthew G. Knepley PetscBool intersects = PETSC_FALSE; 722ddce0771SMatthew G. Knepley PetscInt e = (d+1)%dim; 723ddce0771SMatthew G. Knepley PetscInt f = (d+2)%dim; 724ddce0771SMatthew G. Knepley 725ddce0771SMatthew G. Knepley /* There are two faces in each dimension */ 726ddce0771SMatthew G. Knepley for (ii = 0; ii < 2; ++ii) { 727ddce0771SMatthew G. Knepley segB[d] = PetscRealPart(point[d] + ii*h[d]); 728ddce0771SMatthew G. Knepley segB[dim+d] = PetscRealPart(point[d] + ii*h[d]); 729ddce0771SMatthew G. Knepley segC[d] = PetscRealPart(point[d] + ii*h[d]); 730ddce0771SMatthew G. Knepley segC[dim+d] = PetscRealPart(point[d] + ii*h[d]); 731ddce0771SMatthew G. Knepley if (dim > 1) { 732ddce0771SMatthew G. Knepley segB[e] = PetscRealPart(point[e] + 0*h[e]); 733ddce0771SMatthew G. Knepley segB[dim+e] = PetscRealPart(point[e] + 1*h[e]); 734ddce0771SMatthew G. Knepley segC[e] = PetscRealPart(point[e] + 0*h[e]); 735ddce0771SMatthew G. Knepley segC[dim+e] = PetscRealPart(point[e] + 0*h[e]); 736ddce0771SMatthew G. Knepley } 737ddce0771SMatthew G. Knepley if (dim > 2) { 738ddce0771SMatthew G. Knepley segB[f] = PetscRealPart(point[f] + 0*h[f]); 739ddce0771SMatthew G. Knepley segB[dim+f] = PetscRealPart(point[f] + 0*h[f]); 740ddce0771SMatthew G. Knepley segC[f] = PetscRealPart(point[f] + 0*h[f]); 741ddce0771SMatthew G. Knepley segC[dim+f] = PetscRealPart(point[f] + 1*h[f]); 742ddce0771SMatthew G. Knepley } 743ddce0771SMatthew G. Knepley if (dim == 2) { 7449566063dSJacob Faibussowitsch PetscCall(DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects)); 745ddce0771SMatthew G. Knepley } else if (dim == 3) { 7469566063dSJacob Faibussowitsch PetscCall(DMPlexGetLinePlaneIntersection_3D_Internal(segA, segB, segC, NULL, &intersects)); 747ddce0771SMatthew G. Knepley } 748ddce0771SMatthew G. Knepley if (intersects) { 7499566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %D edge %D (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g) intersects box %D, face (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g) (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g)\n", c, edge, segA[0], segA[1], segA[2], segA[3], segA[4], segA[5], box, segB[0], segB[1], segB[2], segB[3], segB[4], segB[5], segC[0], segC[1], segC[2], segC[3], segC[4], segC[5])); 7509566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); edge = Ne; break; 751ddce0771SMatthew G. Knepley } 752ddce0771SMatthew G. Knepley } 753ddce0771SMatthew G. Knepley } 754cafe43deSMatthew G. Knepley } 755fea14342SMatthew G. Knepley } 756fea14342SMatthew G. Knepley } 757fea14342SMatthew G. Knepley } 7589566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords)); 759fea14342SMatthew G. Knepley } 7609566063dSJacob Faibussowitsch PetscCall(PetscFree3(dboxes, boxes, edgeCoords)); 7619566063dSJacob Faibussowitsch if (debug) PetscCall(DMLabelView(lbox->cellsSparse, PETSC_VIEWER_STDOUT_SELF)); 7629566063dSJacob Faibussowitsch PetscCall(DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells)); 7639566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lbox->cellsSparse)); 764cafe43deSMatthew G. Knepley *localBox = lbox; 765cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 766cafe43deSMatthew G. Knepley } 767cafe43deSMatthew G. Knepley 76862a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF) 769ccd2543fSMatthew G Knepley { 770ddce0771SMatthew G. Knepley const PetscInt debug = 0; 771cafe43deSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 772af74b616SDave May PetscBool hash = mesh->useHashLocation, reuse = PETSC_FALSE; 7733a93e3b7SToby Isaac PetscInt bs, numPoints, p, numFound, *found = NULL; 774412e9a14SMatthew G. Knepley PetscInt dim, cStart, cEnd, numCells, c, d; 775cafe43deSMatthew G. Knepley const PetscInt *boxCells; 7763a93e3b7SToby Isaac PetscSFNode *cells; 777ccd2543fSMatthew G Knepley PetscScalar *a; 7783a93e3b7SToby Isaac PetscMPIInt result; 779af74b616SDave May PetscLogDouble t0,t1; 7809cb35068SDave May PetscReal gmin[3],gmax[3]; 7819cb35068SDave May PetscInt terminating_query_type[] = { 0, 0, 0 }; 782ccd2543fSMatthew G Knepley 783ccd2543fSMatthew G Knepley PetscFunctionBegin; 7849566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_LocatePoints,0,0,0,0)); 7859566063dSJacob Faibussowitsch PetscCall(PetscTime(&t0)); 7862c71b3e2SJacob Faibussowitsch PetscCheckFalse(ltype == DM_POINTLOCATION_NEAREST && !hash,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Nearest point location only supported with grid hashing. Use -dm_plex_hash_location to enable it."); 7879566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dim)); 7889566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(v, &bs)); 7899566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result)); 7902c71b3e2SJacob Faibussowitsch PetscCheckFalse(result != MPI_IDENT && result != MPI_CONGRUENT,PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported"); 7912c71b3e2SJacob Faibussowitsch PetscCheckFalse(bs != dim,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Block size for point vector %D must be the mesh coordinate dimension %D", bs, dim); 7929566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 7939566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(v, &numPoints)); 7949566063dSJacob Faibussowitsch PetscCall(VecGetArray(v, &a)); 795ccd2543fSMatthew G Knepley numPoints /= bs; 796af74b616SDave May { 797af74b616SDave May const PetscSFNode *sf_cells; 798af74b616SDave May 7999566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(cellSF,NULL,NULL,NULL,&sf_cells)); 800af74b616SDave May if (sf_cells) { 8019566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm,"[DMLocatePoints_Plex] Re-using existing StarForest node list\n")); 802af74b616SDave May cells = (PetscSFNode*)sf_cells; 803af74b616SDave May reuse = PETSC_TRUE; 804af74b616SDave May } else { 8059566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm,"[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n")); 8069566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints, &cells)); 807af74b616SDave May /* initialize cells if created */ 808af74b616SDave May for (p=0; p<numPoints; p++) { 809af74b616SDave May cells[p].rank = 0; 810af74b616SDave May cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 811af74b616SDave May } 812af74b616SDave May } 813af74b616SDave May } 8149cb35068SDave May /* define domain bounding box */ 8159cb35068SDave May { 8169cb35068SDave May Vec coorglobal; 8179cb35068SDave May 8189566063dSJacob Faibussowitsch PetscCall(DMGetCoordinates(dm,&coorglobal)); 8199566063dSJacob Faibussowitsch PetscCall(VecStrideMaxAll(coorglobal,NULL,gmax)); 8209566063dSJacob Faibussowitsch PetscCall(VecStrideMinAll(coorglobal,NULL,gmin)); 8219cb35068SDave May } 822953fc75cSMatthew G. Knepley if (hash) { 8239566063dSJacob Faibussowitsch if (!mesh->lbox) {PetscCall(PetscInfo(dm, "Initializing grid hashing"));PetscCall(DMPlexComputeGridHash_Internal(dm, &mesh->lbox));} 824cafe43deSMatthew G. Knepley /* Designate the local box for each point */ 825cafe43deSMatthew G. Knepley /* Send points to correct process */ 826cafe43deSMatthew G. Knepley /* Search cells that lie in each subbox */ 827cafe43deSMatthew G. Knepley /* Should we bin points before doing search? */ 8289566063dSJacob Faibussowitsch PetscCall(ISGetIndices(mesh->lbox->cells, &boxCells)); 829953fc75cSMatthew G. Knepley } 8303a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; ++p) { 831ccd2543fSMatthew G Knepley const PetscScalar *point = &a[p*bs]; 832e56f9228SJed Brown PetscInt dbin[3] = {-1,-1,-1}, bin, cell = -1, cellOffset; 8339cb35068SDave May PetscBool point_outside_domain = PETSC_FALSE; 834ccd2543fSMatthew G Knepley 8359cb35068SDave May /* check bounding box of domain */ 8369cb35068SDave May for (d=0; d<dim; d++) { 837a5f152d1SDave May if (PetscRealPart(point[d]) < gmin[d]) { point_outside_domain = PETSC_TRUE; break; } 838a5f152d1SDave May if (PetscRealPart(point[d]) > gmax[d]) { point_outside_domain = PETSC_TRUE; break; } 8399cb35068SDave May } 8409cb35068SDave May if (point_outside_domain) { 841e9b685f5SMatthew G. Knepley cells[p].rank = 0; 842e9b685f5SMatthew G. Knepley cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 8439cb35068SDave May terminating_query_type[0]++; 8449cb35068SDave May continue; 8459cb35068SDave May } 846ccd2543fSMatthew G Knepley 847af74b616SDave May /* check initial values in cells[].index - abort early if found */ 848af74b616SDave May if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) { 849af74b616SDave May c = cells[p].index; 8503a93e3b7SToby Isaac cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 8519566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell)); 852af74b616SDave May if (cell >= 0) { 853af74b616SDave May cells[p].rank = 0; 854af74b616SDave May cells[p].index = cell; 855af74b616SDave May numFound++; 856af74b616SDave May } 857af74b616SDave May } 8589cb35068SDave May if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) { 8599cb35068SDave May terminating_query_type[1]++; 8609cb35068SDave May continue; 8619cb35068SDave May } 862af74b616SDave May 863953fc75cSMatthew G. Knepley if (hash) { 864af74b616SDave May PetscBool found_box; 865af74b616SDave May 8669566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Checking point %D (%.2g, %.2g, %.2g)\n", p, point[0], point[1], point[2])); 867af74b616SDave May /* allow for case that point is outside box - abort early */ 8689566063dSJacob Faibussowitsch PetscCall(PetscGridHashGetEnclosingBoxQuery(mesh->lbox, 1, point, dbin, &bin,&found_box)); 869af74b616SDave May if (found_box) { 8709566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Found point in box %D (%D, %D, %D)\n", bin, dbin[0], dbin[1], dbin[2])); 871cafe43deSMatthew G. Knepley /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */ 8729566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells)); 8739566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset)); 874cafe43deSMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 8759566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Checking for point in cell %D\n", boxCells[c])); 8769566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell)); 8773a93e3b7SToby Isaac if (cell >= 0) { 8789566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " FOUND in cell %D\n", cell)); 8793a93e3b7SToby Isaac cells[p].rank = 0; 8803a93e3b7SToby Isaac cells[p].index = cell; 8813a93e3b7SToby Isaac numFound++; 8829cb35068SDave May terminating_query_type[2]++; 8833a93e3b7SToby Isaac break; 884ccd2543fSMatthew G Knepley } 8853a93e3b7SToby Isaac } 886af74b616SDave May } 887953fc75cSMatthew G. Knepley } else { 888953fc75cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 8899566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell)); 8903a93e3b7SToby Isaac if (cell >= 0) { 8913a93e3b7SToby Isaac cells[p].rank = 0; 8923a93e3b7SToby Isaac cells[p].index = cell; 8933a93e3b7SToby Isaac numFound++; 8949cb35068SDave May terminating_query_type[2]++; 8953a93e3b7SToby Isaac break; 896953fc75cSMatthew G. Knepley } 897953fc75cSMatthew G. Knepley } 8983a93e3b7SToby Isaac } 899ccd2543fSMatthew G Knepley } 9009566063dSJacob Faibussowitsch if (hash) PetscCall(ISRestoreIndices(mesh->lbox->cells, &boxCells)); 90162a38674SMatthew G. Knepley if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) { 90262a38674SMatthew G. Knepley for (p = 0; p < numPoints; p++) { 90362a38674SMatthew G. Knepley const PetscScalar *point = &a[p*bs]; 904d92c4b9fSToby Isaac PetscReal cpoint[3], diff[3], best[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL}, dist, distMax = PETSC_MAX_REAL; 905d92c4b9fSToby Isaac PetscInt dbin[3] = {-1,-1,-1}, bin, cellOffset, d, bestc = -1; 90662a38674SMatthew G. Knepley 907e9b685f5SMatthew G. Knepley if (cells[p].index < 0) { 9089566063dSJacob Faibussowitsch PetscCall(PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin)); 9099566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells)); 9109566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset)); 91162a38674SMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 9129566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint)); 913b716b415SMatthew G. Knepley for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]); 91462a38674SMatthew G. Knepley dist = DMPlex_NormD_Internal(dim, diff); 91562a38674SMatthew G. Knepley if (dist < distMax) { 916d92c4b9fSToby Isaac for (d = 0; d < dim; ++d) best[d] = cpoint[d]; 917d92c4b9fSToby Isaac bestc = boxCells[c]; 91862a38674SMatthew G. Knepley distMax = dist; 91962a38674SMatthew G. Knepley } 92062a38674SMatthew G. Knepley } 921d92c4b9fSToby Isaac if (distMax < PETSC_MAX_REAL) { 922d92c4b9fSToby Isaac ++numFound; 923d92c4b9fSToby Isaac cells[p].rank = 0; 924d92c4b9fSToby Isaac cells[p].index = bestc; 925d92c4b9fSToby Isaac for (d = 0; d < dim; ++d) a[p*bs+d] = best[d]; 926d92c4b9fSToby Isaac } 92762a38674SMatthew G. Knepley } 92862a38674SMatthew G. Knepley } 92962a38674SMatthew G. Knepley } 93062a38674SMatthew G. Knepley /* This code is only be relevant when interfaced to parallel point location */ 931cafe43deSMatthew G. Knepley /* Check for highest numbered proc that claims a point (do we care?) */ 9322d1fa6caSMatthew G. Knepley if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) { 9339566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numFound,&found)); 9343a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; p++) { 9353a93e3b7SToby Isaac if (cells[p].rank >= 0 && cells[p].index >= 0) { 9363a93e3b7SToby Isaac if (numFound < p) { 9373a93e3b7SToby Isaac cells[numFound] = cells[p]; 9383a93e3b7SToby Isaac } 9393a93e3b7SToby Isaac found[numFound++] = p; 9403a93e3b7SToby Isaac } 9413a93e3b7SToby Isaac } 9423a93e3b7SToby Isaac } 9439566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(v, &a)); 944af74b616SDave May if (!reuse) { 9459566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER)); 946af74b616SDave May } 9479566063dSJacob Faibussowitsch PetscCall(PetscTime(&t1)); 9489cb35068SDave May if (hash) { 9499566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm,"[DMLocatePoints_Plex] terminating_query_type : %D [outside domain] : %D [inside initial cell] : %D [hash]\n",terminating_query_type[0],terminating_query_type[1],terminating_query_type[2])); 9509cb35068SDave May } else { 9519566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm,"[DMLocatePoints_Plex] terminating_query_type : %D [outside domain] : %D [inside initial cell] : %D [brute-force]\n",terminating_query_type[0],terminating_query_type[1],terminating_query_type[2])); 9529cb35068SDave May } 9539566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm,"[DMLocatePoints_Plex] npoints %D : time(rank0) %1.2e (sec): points/sec %1.4e\n",numPoints,t1-t0,(double)((double)numPoints/(t1-t0)))); 9549566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_LocatePoints,0,0,0,0)); 955ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 956ccd2543fSMatthew G Knepley } 957ccd2543fSMatthew G Knepley 958741bfc07SMatthew G. Knepley /*@C 959741bfc07SMatthew G. Knepley DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates 960741bfc07SMatthew G. Knepley 961741bfc07SMatthew G. Knepley Not collective 962741bfc07SMatthew G. Knepley 9636b867d5aSJose E. Roman Input/Output Parameter: 9646b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x 965741bfc07SMatthew G. Knepley 9666b867d5aSJose E. Roman Output Parameter: 9676b867d5aSJose E. Roman . R - The rotation which accomplishes the projection 968741bfc07SMatthew G. Knepley 969741bfc07SMatthew G. Knepley Level: developer 970741bfc07SMatthew G. Knepley 971741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D() 972741bfc07SMatthew G. Knepley @*/ 973741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[]) 97417fe8556SMatthew G. Knepley { 97517fe8556SMatthew G. Knepley const PetscReal x = PetscRealPart(coords[2] - coords[0]); 97617fe8556SMatthew G. Knepley const PetscReal y = PetscRealPart(coords[3] - coords[1]); 9778b49ba18SBarry Smith const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r; 97817fe8556SMatthew G. Knepley 97917fe8556SMatthew G. Knepley PetscFunctionBegin; 9801c99cf0cSGeoffrey Irving R[0] = c; R[1] = -s; 9811c99cf0cSGeoffrey Irving R[2] = s; R[3] = c; 98217fe8556SMatthew G. Knepley coords[0] = 0.0; 9837f07f362SMatthew G. Knepley coords[1] = r; 98417fe8556SMatthew G. Knepley PetscFunctionReturn(0); 98517fe8556SMatthew G. Knepley } 98617fe8556SMatthew G. Knepley 987741bfc07SMatthew G. Knepley /*@C 988741bfc07SMatthew G. Knepley DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates 98928dbe442SToby Isaac 990741bfc07SMatthew G. Knepley Not collective 99128dbe442SToby Isaac 9926b867d5aSJose E. Roman Input/Output Parameter: 9936b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z 994741bfc07SMatthew G. Knepley 9956b867d5aSJose E. Roman Output Parameter: 9966b867d5aSJose E. Roman . R - The rotation which accomplishes the projection 997741bfc07SMatthew G. Knepley 998741bfc07SMatthew 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 999741bfc07SMatthew G. Knepley 1000741bfc07SMatthew G. Knepley Level: developer 1001741bfc07SMatthew G. Knepley 1002741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D() 1003741bfc07SMatthew G. Knepley @*/ 1004741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[]) 100528dbe442SToby Isaac { 100628dbe442SToby Isaac PetscReal x = PetscRealPart(coords[3] - coords[0]); 100728dbe442SToby Isaac PetscReal y = PetscRealPart(coords[4] - coords[1]); 100828dbe442SToby Isaac PetscReal z = PetscRealPart(coords[5] - coords[2]); 100928dbe442SToby Isaac PetscReal r = PetscSqrtReal(x*x + y*y + z*z); 101028dbe442SToby Isaac PetscReal rinv = 1. / r; 101128dbe442SToby Isaac PetscFunctionBegin; 101228dbe442SToby Isaac 101328dbe442SToby Isaac x *= rinv; y *= rinv; z *= rinv; 101428dbe442SToby Isaac if (x > 0.) { 101528dbe442SToby Isaac PetscReal inv1pX = 1./ (1. + x); 101628dbe442SToby Isaac 101728dbe442SToby Isaac R[0] = x; R[1] = -y; R[2] = -z; 101828dbe442SToby Isaac R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] = -y*z*inv1pX; 101928dbe442SToby Isaac R[6] = z; R[7] = -y*z*inv1pX; R[8] = 1. - z*z*inv1pX; 102028dbe442SToby Isaac } 102128dbe442SToby Isaac else { 102228dbe442SToby Isaac PetscReal inv1mX = 1./ (1. - x); 102328dbe442SToby Isaac 102428dbe442SToby Isaac R[0] = x; R[1] = z; R[2] = y; 102528dbe442SToby Isaac R[3] = y; R[4] = -y*z*inv1mX; R[5] = 1. - y*y*inv1mX; 102628dbe442SToby Isaac R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] = -y*z*inv1mX; 102728dbe442SToby Isaac } 102828dbe442SToby Isaac coords[0] = 0.0; 102928dbe442SToby Isaac coords[1] = r; 103028dbe442SToby Isaac PetscFunctionReturn(0); 103128dbe442SToby Isaac } 103228dbe442SToby Isaac 1033741bfc07SMatthew G. Knepley /*@ 1034c871b86eSJed Brown DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the 1035c871b86eSJed Brown plane. The normal is defined by positive orientation of the first 3 points. 1036741bfc07SMatthew G. Knepley 1037741bfc07SMatthew G. Knepley Not collective 1038741bfc07SMatthew G. Knepley 1039741bfc07SMatthew G. Knepley Input Parameter: 10406b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points) 1041741bfc07SMatthew G. Knepley 10426b867d5aSJose E. Roman Input/Output Parameter: 10436b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first 10446b867d5aSJose E. Roman 2*coordSize/3 entries contain interlaced 2D points, with the rest undefined 10456b867d5aSJose E. Roman 10466b867d5aSJose E. Roman Output Parameter: 10476b867d5aSJose E. Roman . R - 3x3 row-major rotation matrix whose columns are the tangent basis [t1, t2, n]. Multiplying by R^T transforms from original frame to tangent frame. 1048741bfc07SMatthew G. Knepley 1049741bfc07SMatthew G. Knepley Level: developer 1050741bfc07SMatthew G. Knepley 1051741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D() 1052741bfc07SMatthew G. Knepley @*/ 1053741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) 1054ccd2543fSMatthew G Knepley { 1055c871b86eSJed Brown PetscReal x1[3], x2[3], n[3], c[3], norm; 1056ccd2543fSMatthew G Knepley const PetscInt dim = 3; 1057c871b86eSJed Brown PetscInt d, p; 1058ccd2543fSMatthew G Knepley 1059ccd2543fSMatthew G Knepley PetscFunctionBegin; 1060ccd2543fSMatthew G Knepley /* 0) Calculate normal vector */ 1061ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 10621ee9d5ecSMatthew G. Knepley x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]); 10631ee9d5ecSMatthew G. Knepley x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]); 1064ccd2543fSMatthew G Knepley } 1065c871b86eSJed Brown // n = x1 \otimes x2 1066ccd2543fSMatthew G Knepley n[0] = x1[1]*x2[2] - x1[2]*x2[1]; 1067ccd2543fSMatthew G Knepley n[1] = x1[2]*x2[0] - x1[0]*x2[2]; 1068ccd2543fSMatthew G Knepley n[2] = x1[0]*x2[1] - x1[1]*x2[0]; 10698b49ba18SBarry Smith norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]); 1070c871b86eSJed Brown for (d = 0; d < dim; d++) n[d] /= norm; 1071c871b86eSJed Brown norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]); 1072c871b86eSJed Brown for (d = 0; d < dim; d++) x1[d] /= norm; 1073c871b86eSJed Brown // x2 = n \otimes x1 1074c871b86eSJed Brown x2[0] = n[1] * x1[2] - n[2] * x1[1]; 1075c871b86eSJed Brown x2[1] = n[2] * x1[0] - n[0] * x1[2]; 1076c871b86eSJed Brown x2[2] = n[0] * x1[1] - n[1] * x1[0]; 1077c871b86eSJed Brown for (d=0; d<dim; d++) { 1078c871b86eSJed Brown R[d * dim + 0] = x1[d]; 1079c871b86eSJed Brown R[d * dim + 1] = x2[d]; 1080c871b86eSJed Brown R[d * dim + 2] = n[d]; 1081c871b86eSJed Brown c[d] = PetscRealPart(coords[0*dim + d]); 108273868372SMatthew G. Knepley } 1083c871b86eSJed Brown for (p=0; p<coordSize/dim; p++) { 1084c871b86eSJed Brown PetscReal y[3]; 1085c871b86eSJed Brown for (d=0; d<dim; d++) y[d] = PetscRealPart(coords[p*dim + d]) - c[d]; 1086c871b86eSJed Brown for (d=0; d<2; d++) coords[p*2+d] = R[0*dim + d] * y[0] + R[1*dim + d] * y[1] + R[2*dim + d] * y[2]; 10877f07f362SMatthew G. Knepley } 1088ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1089ccd2543fSMatthew G Knepley } 1090ccd2543fSMatthew G Knepley 10916322fe33SJed Brown PETSC_UNUSED 10929fbee547SJacob Faibussowitsch static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) 1093834e62ceSMatthew G. Knepley { 1094834e62ceSMatthew G. Knepley /* Signed volume is 1/2 the determinant 1095834e62ceSMatthew G. Knepley 1096834e62ceSMatthew G. Knepley | 1 1 1 | 1097834e62ceSMatthew G. Knepley | x0 x1 x2 | 1098834e62ceSMatthew G. Knepley | y0 y1 y2 | 1099834e62ceSMatthew G. Knepley 1100834e62ceSMatthew G. Knepley but if x0,y0 is the origin, we have 1101834e62ceSMatthew G. Knepley 1102834e62ceSMatthew G. Knepley | x1 x2 | 1103834e62ceSMatthew G. Knepley | y1 y2 | 1104834e62ceSMatthew G. Knepley */ 1105834e62ceSMatthew G. Knepley const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1]; 1106834e62ceSMatthew G. Knepley const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1]; 1107834e62ceSMatthew G. Knepley PetscReal M[4], detM; 1108834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; 110986623015SMatthew G. Knepley M[2] = y1; M[3] = y2; 1110923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(&detM, M); 1111834e62ceSMatthew G. Knepley *vol = 0.5*detM; 11123bc0b13bSBarry Smith (void)PetscLogFlops(5.0); 1113834e62ceSMatthew G. Knepley } 1114834e62ceSMatthew G. Knepley 11156322fe33SJed Brown PETSC_UNUSED 11169fbee547SJacob Faibussowitsch static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) 1117834e62ceSMatthew G. Knepley { 1118834e62ceSMatthew G. Knepley /* Signed volume is 1/6th of the determinant 1119834e62ceSMatthew G. Knepley 1120834e62ceSMatthew G. Knepley | 1 1 1 1 | 1121834e62ceSMatthew G. Knepley | x0 x1 x2 x3 | 1122834e62ceSMatthew G. Knepley | y0 y1 y2 y3 | 1123834e62ceSMatthew G. Knepley | z0 z1 z2 z3 | 1124834e62ceSMatthew G. Knepley 1125834e62ceSMatthew G. Knepley but if x0,y0,z0 is the origin, we have 1126834e62ceSMatthew G. Knepley 1127834e62ceSMatthew G. Knepley | x1 x2 x3 | 1128834e62ceSMatthew G. Knepley | y1 y2 y3 | 1129834e62ceSMatthew G. Knepley | z1 z2 z3 | 1130834e62ceSMatthew G. Knepley */ 1131834e62ceSMatthew G. Knepley const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2]; 1132834e62ceSMatthew G. Knepley const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2]; 1133834e62ceSMatthew G. Knepley const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2]; 11340a3da2c2SToby Isaac const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.); 1135834e62ceSMatthew G. Knepley PetscReal M[9], detM; 1136834e62ceSMatthew G. Knepley M[0] = x1; M[1] = x2; M[2] = x3; 1137834e62ceSMatthew G. Knepley M[3] = y1; M[4] = y2; M[5] = y3; 1138834e62ceSMatthew G. Knepley M[6] = z1; M[7] = z2; M[8] = z3; 1139923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(&detM, M); 11400a3da2c2SToby Isaac *vol = -onesixth*detM; 11413bc0b13bSBarry Smith (void)PetscLogFlops(10.0); 1142834e62ceSMatthew G. Knepley } 1143834e62ceSMatthew G. Knepley 11449fbee547SJacob Faibussowitsch static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) 11450ec8681fSMatthew G. Knepley { 11460a3da2c2SToby Isaac const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.); 1147923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(vol, coords); 11480a3da2c2SToby Isaac *vol *= -onesixth; 11490ec8681fSMatthew G. Knepley } 11500ec8681fSMatthew G. Knepley 1151cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1152cb92db44SToby Isaac { 1153cb92db44SToby Isaac PetscSection coordSection; 1154cb92db44SToby Isaac Vec coordinates; 1155cb92db44SToby Isaac const PetscScalar *coords; 1156cb92db44SToby Isaac PetscInt dim, d, off; 1157cb92db44SToby Isaac 1158cb92db44SToby Isaac PetscFunctionBegin; 11599566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 11609566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 11619566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection,e,&dim)); 1162cb92db44SToby Isaac if (!dim) PetscFunctionReturn(0); 11639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection,e,&off)); 11649566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates,&coords)); 1165cb92db44SToby Isaac if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);} 11669566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates,&coords)); 1167cb92db44SToby Isaac *detJ = 1.; 1168cb92db44SToby Isaac if (J) { 1169cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) J[d] = 0.; 1170cb92db44SToby Isaac for (d = 0; d < dim; d++) J[d * dim + d] = 1.; 1171cb92db44SToby Isaac if (invJ) { 1172cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) invJ[d] = 0.; 1173cb92db44SToby Isaac for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.; 1174cb92db44SToby Isaac } 1175cb92db44SToby Isaac } 1176cb92db44SToby Isaac PetscFunctionReturn(0); 1177cb92db44SToby Isaac } 1178cb92db44SToby Isaac 117917fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 118017fe8556SMatthew G. Knepley { 118117fe8556SMatthew G. Knepley PetscSection coordSection; 118217fe8556SMatthew G. Knepley Vec coordinates; 1183a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 11848bf5c034SToby Isaac PetscInt numCoords, d, pStart, pEnd, numSelfCoords = 0; 118517fe8556SMatthew G. Knepley 118617fe8556SMatthew G. Knepley PetscFunctionBegin; 11879566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 11889566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 11899566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(coordSection,&pStart,&pEnd)); 11909566063dSJacob Faibussowitsch if (e >= pStart && e < pEnd) PetscCall(PetscSectionGetDof(coordSection,e,&numSelfCoords)); 11919566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords)); 11928bf5c034SToby Isaac numCoords = numSelfCoords ? numSelfCoords : numCoords; 11932c71b3e2SJacob Faibussowitsch PetscCheckFalse(invJ && !J,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 11947f07f362SMatthew G. Knepley *detJ = 0.0; 119528dbe442SToby Isaac if (numCoords == 6) { 119628dbe442SToby Isaac const PetscInt dim = 3; 119728dbe442SToby Isaac PetscReal R[9], J0; 119828dbe442SToby Isaac 119928dbe442SToby Isaac if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 12009566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto1D(coords, R)); 120128dbe442SToby Isaac if (J) { 120228dbe442SToby Isaac J0 = 0.5*PetscRealPart(coords[1]); 120328dbe442SToby Isaac J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2]; 120428dbe442SToby Isaac J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5]; 120528dbe442SToby Isaac J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8]; 120628dbe442SToby Isaac DMPlex_Det3D_Internal(detJ, J); 120728dbe442SToby Isaac if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 1208adac9986SMatthew G. Knepley } 120928dbe442SToby Isaac } else if (numCoords == 4) { 12107f07f362SMatthew G. Knepley const PetscInt dim = 2; 12117f07f362SMatthew G. Knepley PetscReal R[4], J0; 12127f07f362SMatthew G. Knepley 12137f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 12149566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection2Dto1D(coords, R)); 121517fe8556SMatthew G. Knepley if (J) { 12167f07f362SMatthew G. Knepley J0 = 0.5*PetscRealPart(coords[1]); 12177f07f362SMatthew G. Knepley J[0] = R[0]*J0; J[1] = R[1]; 12187f07f362SMatthew G. Knepley J[2] = R[2]*J0; J[3] = R[3]; 1219923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1220923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 1221adac9986SMatthew G. Knepley } 12227f07f362SMatthew G. Knepley } else if (numCoords == 2) { 12237f07f362SMatthew G. Knepley const PetscInt dim = 1; 12247f07f362SMatthew G. Knepley 12257f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 12267f07f362SMatthew G. Knepley if (J) { 12277f07f362SMatthew G. Knepley J[0] = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0])); 122817fe8556SMatthew G. Knepley *detJ = J[0]; 12299566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0)); 12309566063dSJacob Faibussowitsch if (invJ) {invJ[0] = 1.0/J[0]; PetscCall(PetscLogFlops(1.0));} 1231adac9986SMatthew G. Knepley } 123298921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords); 12339566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords)); 123417fe8556SMatthew G. Knepley PetscFunctionReturn(0); 123517fe8556SMatthew G. Knepley } 123617fe8556SMatthew G. Knepley 1237ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1238ccd2543fSMatthew G Knepley { 1239ccd2543fSMatthew G Knepley PetscSection coordSection; 1240ccd2543fSMatthew G Knepley Vec coordinates; 1241a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 124203d7ed2eSStefano Zampini PetscInt numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd; 1243ccd2543fSMatthew G Knepley 1244ccd2543fSMatthew G Knepley PetscFunctionBegin; 12459566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 12469566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 12479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(coordSection,&pStart,&pEnd)); 12489566063dSJacob Faibussowitsch if (e >= pStart && e < pEnd) PetscCall(PetscSectionGetDof(coordSection,e,&numSelfCoords)); 12499566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords)); 125003d7ed2eSStefano Zampini numCoords = numSelfCoords ? numSelfCoords : numCoords; 12517f07f362SMatthew G. Knepley *detJ = 0.0; 1252ccd2543fSMatthew G Knepley if (numCoords == 9) { 12537f07f362SMatthew G. Knepley const PetscInt dim = 3; 12547f07f362SMatthew 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}; 12557f07f362SMatthew G. Knepley 12567f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 12579566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R)); 12587f07f362SMatthew G. Knepley if (J) { 1259b7ad821dSMatthew G. Knepley const PetscInt pdim = 2; 1260b7ad821dSMatthew G. Knepley 1261b7ad821dSMatthew G. Knepley for (d = 0; d < pdim; d++) { 1262b7ad821dSMatthew G. Knepley for (f = 0; f < pdim; f++) { 1263b7ad821dSMatthew G. Knepley J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 1264ccd2543fSMatthew G Knepley } 12657f07f362SMatthew G. Knepley } 12669566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1267923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 12687f07f362SMatthew G. Knepley for (d = 0; d < dim; d++) { 12697f07f362SMatthew G. Knepley for (f = 0; f < dim; f++) { 12707f07f362SMatthew G. Knepley J[d*dim+f] = 0.0; 12717f07f362SMatthew G. Knepley for (g = 0; g < dim; g++) { 12727f07f362SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 12737f07f362SMatthew G. Knepley } 12747f07f362SMatthew G. Knepley } 12757f07f362SMatthew G. Knepley } 12769566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 12777f07f362SMatthew G. Knepley } 1278923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 12797f07f362SMatthew G. Knepley } else if (numCoords == 6) { 12807f07f362SMatthew G. Knepley const PetscInt dim = 2; 12817f07f362SMatthew G. Knepley 12827f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1283ccd2543fSMatthew G Knepley if (J) { 1284ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1285ccd2543fSMatthew G Knepley for (f = 0; f < dim; f++) { 1286ccd2543fSMatthew G Knepley J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d])); 1287ccd2543fSMatthew G Knepley } 1288ccd2543fSMatthew G Knepley } 12899566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1290923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1291ccd2543fSMatthew G Knepley } 1292923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 129398921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords); 12949566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords)); 1295ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1296ccd2543fSMatthew G Knepley } 1297ccd2543fSMatthew G Knepley 1298412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1299ccd2543fSMatthew G Knepley { 1300ccd2543fSMatthew G Knepley PetscSection coordSection; 1301ccd2543fSMatthew G Knepley Vec coordinates; 1302a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 13030d29256aSToby Isaac PetscInt numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd; 1304ccd2543fSMatthew G Knepley 1305ccd2543fSMatthew G Knepley PetscFunctionBegin; 13069566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 13079566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 13089566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(coordSection,&pStart,&pEnd)); 13099566063dSJacob Faibussowitsch if (e >= pStart && e < pEnd) PetscCall(PetscSectionGetDof(coordSection,e,&numSelfCoords)); 13109566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords)); 131171f58de1SToby Isaac numCoords = numSelfCoords ? numSelfCoords : numCoords; 1312dfccc68fSToby Isaac if (!Nq) { 1313412e9a14SMatthew G. Knepley PetscInt vorder[4] = {0, 1, 2, 3}; 1314412e9a14SMatthew G. Knepley 1315412e9a14SMatthew G. Knepley if (isTensor) {vorder[2] = 3; vorder[3] = 2;} 13167f07f362SMatthew G. Knepley *detJ = 0.0; 131799dec3a6SMatthew G. Knepley if (numCoords == 12) { 131899dec3a6SMatthew G. Knepley const PetscInt dim = 3; 131999dec3a6SMatthew 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}; 132099dec3a6SMatthew G. Knepley 1321dfccc68fSToby Isaac if (v) {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);} 13229566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R)); 132399dec3a6SMatthew G. Knepley if (J) { 132499dec3a6SMatthew G. Knepley const PetscInt pdim = 2; 132599dec3a6SMatthew G. Knepley 132699dec3a6SMatthew G. Knepley for (d = 0; d < pdim; d++) { 1327412e9a14SMatthew G. Knepley J0[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*pdim+d]) - PetscRealPart(coords[vorder[0]*pdim+d])); 1328412e9a14SMatthew G. Knepley J0[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[2]*pdim+d]) - PetscRealPart(coords[vorder[1]*pdim+d])); 132999dec3a6SMatthew G. Knepley } 13309566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1331923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 133299dec3a6SMatthew G. Knepley for (d = 0; d < dim; d++) { 133399dec3a6SMatthew G. Knepley for (f = 0; f < dim; f++) { 133499dec3a6SMatthew G. Knepley J[d*dim+f] = 0.0; 133599dec3a6SMatthew G. Knepley for (g = 0; g < dim; g++) { 133699dec3a6SMatthew G. Knepley J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 133799dec3a6SMatthew G. Knepley } 133899dec3a6SMatthew G. Knepley } 133999dec3a6SMatthew G. Knepley } 13409566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 134199dec3a6SMatthew G. Knepley } 1342923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 134371f58de1SToby Isaac } else if (numCoords == 8) { 134499dec3a6SMatthew G. Knepley const PetscInt dim = 2; 134599dec3a6SMatthew G. Knepley 1346dfccc68fSToby Isaac if (v) {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);} 1347ccd2543fSMatthew G Knepley if (J) { 1348ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1349412e9a14SMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d])); 1350412e9a14SMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[3]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d])); 1351ccd2543fSMatthew G Knepley } 13529566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1353923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1354ccd2543fSMatthew G Knepley } 1355923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 135698921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords); 1357dfccc68fSToby Isaac } else { 1358dfccc68fSToby Isaac const PetscInt Nv = 4; 1359dfccc68fSToby Isaac const PetscInt dimR = 2; 1360412e9a14SMatthew G. Knepley PetscInt zToPlex[4] = {0, 1, 3, 2}; 1361dfccc68fSToby Isaac PetscReal zOrder[12]; 1362dfccc68fSToby Isaac PetscReal zCoeff[12]; 1363dfccc68fSToby Isaac PetscInt i, j, k, l, dim; 1364dfccc68fSToby Isaac 1365412e9a14SMatthew G. Knepley if (isTensor) {zToPlex[2] = 2; zToPlex[3] = 3;} 1366dfccc68fSToby Isaac if (numCoords == 12) { 1367dfccc68fSToby Isaac dim = 3; 1368dfccc68fSToby Isaac } else if (numCoords == 8) { 1369dfccc68fSToby Isaac dim = 2; 137098921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords); 1371dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 1372dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 1373dfccc68fSToby Isaac 1374dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1375dfccc68fSToby Isaac zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); 1376dfccc68fSToby Isaac } 1377dfccc68fSToby Isaac } 1378dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 13792df84da0SMatthew G. Knepley /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta): 13802df84da0SMatthew G. Knepley \phi^0 = (1 - xi - eta + xi eta) --> 1 = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3) 13812df84da0SMatthew G. Knepley \phi^1 = (1 + xi - eta - xi eta) --> xi = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3) 13822df84da0SMatthew G. Knepley \phi^2 = (1 - xi + eta - xi eta) --> eta = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3) 13832df84da0SMatthew G. Knepley \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3) 13842df84da0SMatthew G. Knepley */ 1385dfccc68fSToby Isaac zCoeff[dim * 0 + j] = 0.25 * ( zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1386dfccc68fSToby Isaac zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1387dfccc68fSToby Isaac zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1388dfccc68fSToby Isaac zCoeff[dim * 3 + j] = 0.25 * ( zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1389dfccc68fSToby Isaac } 1390dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1391dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1]; 1392dfccc68fSToby Isaac 1393dfccc68fSToby Isaac if (v) { 1394dfccc68fSToby Isaac PetscReal extPoint[4]; 1395dfccc68fSToby Isaac 1396dfccc68fSToby Isaac extPoint[0] = 1.; 1397dfccc68fSToby Isaac extPoint[1] = xi; 1398dfccc68fSToby Isaac extPoint[2] = eta; 1399dfccc68fSToby Isaac extPoint[3] = xi * eta; 1400dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1401dfccc68fSToby Isaac PetscReal val = 0.; 1402dfccc68fSToby Isaac 1403dfccc68fSToby Isaac for (k = 0; k < Nv; k++) { 1404dfccc68fSToby Isaac val += extPoint[k] * zCoeff[dim * k + j]; 1405dfccc68fSToby Isaac } 1406dfccc68fSToby Isaac v[i * dim + j] = val; 1407dfccc68fSToby Isaac } 1408dfccc68fSToby Isaac } 1409dfccc68fSToby Isaac if (J) { 1410dfccc68fSToby Isaac PetscReal extJ[8]; 1411dfccc68fSToby Isaac 1412dfccc68fSToby Isaac extJ[0] = 0.; 1413dfccc68fSToby Isaac extJ[1] = 0.; 1414dfccc68fSToby Isaac extJ[2] = 1.; 1415dfccc68fSToby Isaac extJ[3] = 0.; 1416dfccc68fSToby Isaac extJ[4] = 0.; 1417dfccc68fSToby Isaac extJ[5] = 1.; 1418dfccc68fSToby Isaac extJ[6] = eta; 1419dfccc68fSToby Isaac extJ[7] = xi; 1420dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1421dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 1422dfccc68fSToby Isaac PetscReal val = 0.; 1423dfccc68fSToby Isaac 1424dfccc68fSToby Isaac for (l = 0; l < Nv; l++) { 1425dfccc68fSToby Isaac val += zCoeff[dim * l + j] * extJ[dimR * l + k]; 1426dfccc68fSToby Isaac } 1427dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 1428dfccc68fSToby Isaac } 1429dfccc68fSToby Isaac } 1430dfccc68fSToby Isaac if (dim == 3) { /* put the cross product in the third component of the Jacobian */ 1431dfccc68fSToby Isaac PetscReal x, y, z; 1432dfccc68fSToby Isaac PetscReal *iJ = &J[i * dim * dim]; 1433dfccc68fSToby Isaac PetscReal norm; 1434dfccc68fSToby Isaac 1435dfccc68fSToby Isaac x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0]; 1436dfccc68fSToby Isaac y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1]; 1437dfccc68fSToby Isaac z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0]; 1438dfccc68fSToby Isaac norm = PetscSqrtReal(x * x + y * y + z * z); 1439dfccc68fSToby Isaac iJ[2] = x / norm; 1440dfccc68fSToby Isaac iJ[5] = y / norm; 1441dfccc68fSToby Isaac iJ[8] = z / norm; 1442dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 1443dfccc68fSToby Isaac if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);} 1444dfccc68fSToby Isaac } else { 1445dfccc68fSToby Isaac DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]); 1446dfccc68fSToby Isaac if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);} 1447dfccc68fSToby Isaac } 1448dfccc68fSToby Isaac } 1449dfccc68fSToby Isaac } 1450dfccc68fSToby Isaac } 14519566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords)); 1452ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1453ccd2543fSMatthew G Knepley } 1454ccd2543fSMatthew G Knepley 1455ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1456ccd2543fSMatthew G Knepley { 1457ccd2543fSMatthew G Knepley PetscSection coordSection; 1458ccd2543fSMatthew G Knepley Vec coordinates; 1459a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1460ccd2543fSMatthew G Knepley const PetscInt dim = 3; 146199dec3a6SMatthew G. Knepley PetscInt d; 1462ccd2543fSMatthew G Knepley 1463ccd2543fSMatthew G Knepley PetscFunctionBegin; 14649566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 14659566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 14669566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords)); 14677f07f362SMatthew G. Knepley *detJ = 0.0; 14687f07f362SMatthew G. Knepley if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 1469ccd2543fSMatthew G Knepley if (J) { 1470ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1471f0df753eSMatthew G. Knepley /* I orient with outward face normals */ 1472f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d])); 1473f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 1474f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 1475ccd2543fSMatthew G Knepley } 14769566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 1477923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1478ccd2543fSMatthew G Knepley } 1479923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 14809566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords)); 1481ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1482ccd2543fSMatthew G Knepley } 1483ccd2543fSMatthew G Knepley 1484dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1485ccd2543fSMatthew G Knepley { 1486ccd2543fSMatthew G Knepley PetscSection coordSection; 1487ccd2543fSMatthew G Knepley Vec coordinates; 1488a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1489ccd2543fSMatthew G Knepley const PetscInt dim = 3; 1490ccd2543fSMatthew G Knepley PetscInt d; 1491ccd2543fSMatthew G Knepley 1492ccd2543fSMatthew G Knepley PetscFunctionBegin; 14939566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 14949566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 14959566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords)); 1496dfccc68fSToby Isaac if (!Nq) { 14977f07f362SMatthew G. Knepley *detJ = 0.0; 1498dfccc68fSToby Isaac if (v) {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);} 1499ccd2543fSMatthew G Knepley if (J) { 1500ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1501f0df753eSMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 1502f0df753eSMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 1503f0df753eSMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d])); 1504ccd2543fSMatthew G Knepley } 15059566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 1506923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1507ccd2543fSMatthew G Knepley } 1508923591dfSMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 1509dfccc68fSToby Isaac } else { 1510dfccc68fSToby Isaac const PetscInt Nv = 8; 1511dfccc68fSToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 1512dfccc68fSToby Isaac const PetscInt dim = 3; 1513dfccc68fSToby Isaac const PetscInt dimR = 3; 1514dfccc68fSToby Isaac PetscReal zOrder[24]; 1515dfccc68fSToby Isaac PetscReal zCoeff[24]; 1516dfccc68fSToby Isaac PetscInt i, j, k, l; 1517dfccc68fSToby Isaac 1518dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 1519dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 1520dfccc68fSToby Isaac 1521dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1522dfccc68fSToby Isaac zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); 1523dfccc68fSToby Isaac } 1524dfccc68fSToby Isaac } 1525dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1526dfccc68fSToby 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]); 1527dfccc68fSToby 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]); 1528dfccc68fSToby 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]); 1529dfccc68fSToby 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]); 1530dfccc68fSToby 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]); 1531dfccc68fSToby 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]); 1532dfccc68fSToby 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]); 1533dfccc68fSToby 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]); 1534dfccc68fSToby Isaac } 1535dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1536dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2]; 1537dfccc68fSToby Isaac 1538dfccc68fSToby Isaac if (v) { 153991d2b7ceSToby Isaac PetscReal extPoint[8]; 1540dfccc68fSToby Isaac 1541dfccc68fSToby Isaac extPoint[0] = 1.; 1542dfccc68fSToby Isaac extPoint[1] = xi; 1543dfccc68fSToby Isaac extPoint[2] = eta; 1544dfccc68fSToby Isaac extPoint[3] = xi * eta; 1545dfccc68fSToby Isaac extPoint[4] = theta; 1546dfccc68fSToby Isaac extPoint[5] = theta * xi; 1547dfccc68fSToby Isaac extPoint[6] = theta * eta; 1548dfccc68fSToby Isaac extPoint[7] = theta * eta * xi; 1549dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1550dfccc68fSToby Isaac PetscReal val = 0.; 1551dfccc68fSToby Isaac 1552dfccc68fSToby Isaac for (k = 0; k < Nv; k++) { 1553dfccc68fSToby Isaac val += extPoint[k] * zCoeff[dim * k + j]; 1554dfccc68fSToby Isaac } 1555dfccc68fSToby Isaac v[i * dim + j] = val; 1556dfccc68fSToby Isaac } 1557dfccc68fSToby Isaac } 1558dfccc68fSToby Isaac if (J) { 1559dfccc68fSToby Isaac PetscReal extJ[24]; 1560dfccc68fSToby Isaac 1561dfccc68fSToby Isaac extJ[0] = 0. ; extJ[1] = 0. ; extJ[2] = 0. ; 1562dfccc68fSToby Isaac extJ[3] = 1. ; extJ[4] = 0. ; extJ[5] = 0. ; 1563dfccc68fSToby Isaac extJ[6] = 0. ; extJ[7] = 1. ; extJ[8] = 0. ; 1564dfccc68fSToby Isaac extJ[9] = eta ; extJ[10] = xi ; extJ[11] = 0. ; 1565dfccc68fSToby Isaac extJ[12] = 0. ; extJ[13] = 0. ; extJ[14] = 1. ; 1566dfccc68fSToby Isaac extJ[15] = theta ; extJ[16] = 0. ; extJ[17] = xi ; 1567dfccc68fSToby Isaac extJ[18] = 0. ; extJ[19] = theta ; extJ[20] = eta ; 1568dfccc68fSToby Isaac extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi; 1569dfccc68fSToby Isaac 1570dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1571dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 1572dfccc68fSToby Isaac PetscReal val = 0.; 1573dfccc68fSToby Isaac 1574dfccc68fSToby Isaac for (l = 0; l < Nv; l++) { 1575dfccc68fSToby Isaac val += zCoeff[dim * l + j] * extJ[dimR * l + k]; 1576dfccc68fSToby Isaac } 1577dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 1578dfccc68fSToby Isaac } 1579dfccc68fSToby Isaac } 1580dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 1581dfccc68fSToby Isaac if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);} 1582dfccc68fSToby Isaac } 1583dfccc68fSToby Isaac } 1584dfccc68fSToby Isaac } 15859566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords)); 1586ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1587ccd2543fSMatthew G Knepley } 1588ccd2543fSMatthew G Knepley 15892df84da0SMatthew G. Knepley static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 15902df84da0SMatthew G. Knepley { 15912df84da0SMatthew G. Knepley PetscSection coordSection; 15922df84da0SMatthew G. Knepley Vec coordinates; 15932df84da0SMatthew G. Knepley PetscScalar *coords = NULL; 15942df84da0SMatthew G. Knepley const PetscInt dim = 3; 15952df84da0SMatthew G. Knepley PetscInt d; 15962df84da0SMatthew G. Knepley 15972df84da0SMatthew G. Knepley PetscFunctionBegin; 15989566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 15999566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 16009566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords)); 16012df84da0SMatthew G. Knepley if (!Nq) { 16022df84da0SMatthew G. Knepley /* Assume that the map to the reference is affine */ 16032df84da0SMatthew G. Knepley *detJ = 0.0; 16042df84da0SMatthew G. Knepley if (v) {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);} 16052df84da0SMatthew G. Knepley if (J) { 16062df84da0SMatthew G. Knepley for (d = 0; d < dim; d++) { 16072df84da0SMatthew G. Knepley J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d])); 16082df84da0SMatthew G. Knepley J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 16092df84da0SMatthew G. Knepley J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d])); 16102df84da0SMatthew G. Knepley } 16119566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 16122df84da0SMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 16132df84da0SMatthew G. Knepley } 16142df84da0SMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 16152df84da0SMatthew G. Knepley } else { 16162df84da0SMatthew G. Knepley const PetscInt dim = 3; 16172df84da0SMatthew G. Knepley const PetscInt dimR = 3; 16182df84da0SMatthew G. Knepley const PetscInt Nv = 6; 16192df84da0SMatthew G. Knepley PetscReal verts[18]; 16202df84da0SMatthew G. Knepley PetscReal coeff[18]; 16212df84da0SMatthew G. Knepley PetscInt i, j, k, l; 16222df84da0SMatthew G. Knepley 16232df84da0SMatthew G. Knepley for (i = 0; i < Nv; ++i) for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]); 16242df84da0SMatthew G. Knepley for (j = 0; j < dim; ++j) { 16252df84da0SMatthew G. Knepley /* Check for triangle, 16262df84da0SMatthew G. Knepley phi^0 = -1/2 (xi + eta) chi^0 = delta(-1, -1) x(xi) = \sum_k x_k phi^k(xi) = \sum_k chi^k(x) phi^k(xi) 16272df84da0SMatthew G. Knepley phi^1 = 1/2 (1 + xi) chi^1 = delta( 1, -1) y(xi) = \sum_k y_k phi^k(xi) = \sum_k chi^k(y) phi^k(xi) 16282df84da0SMatthew G. Knepley phi^2 = 1/2 (1 + eta) chi^2 = delta(-1, 1) 16292df84da0SMatthew G. Knepley 16302df84da0SMatthew G. Knepley phi^0 + phi^1 + phi^2 = 1 coef_1 = 1/2 ( chi^1 + chi^2) 16312df84da0SMatthew G. Knepley -phi^0 + phi^1 - phi^2 = xi coef_xi = 1/2 (-chi^0 + chi^1) 16322df84da0SMatthew G. Knepley -phi^0 - phi^1 + phi^2 = eta coef_eta = 1/2 (-chi^0 + chi^2) 16332df84da0SMatthew G. Knepley 16342df84da0SMatthew G. Knepley < chi_0 chi_1 chi_2> A / 1 1 1 \ / phi_0 \ <chi> I <phi>^T so we need the inverse transpose 16352df84da0SMatthew G. Knepley | -1 1 -1 | | phi_1 | = 16362df84da0SMatthew G. Knepley \ -1 -1 1 / \ phi_2 / 16372df84da0SMatthew G. Knepley 16382df84da0SMatthew G. Knepley Check phi^0: 1/2 (phi^0 chi^1 + phi^0 chi^2 + phi^0 chi^0 - phi^0 chi^1 + phi^0 chi^0 - phi^0 chi^2) = phi^0 chi^0 16392df84da0SMatthew G. Knepley */ 16402df84da0SMatthew G. Knepley /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta): 16412df84da0SMatthew G. Knepley \phi^0 = 1/4 ( -xi - eta + xi zeta + eta zeta) --> / 1 1 1 1 1 1 \ 1 16422df84da0SMatthew G. Knepley \phi^1 = 1/4 (1 + eta - zeta - eta zeta) --> | -1 1 -1 -1 -1 1 | eta 16432df84da0SMatthew G. Knepley \phi^2 = 1/4 (1 + xi - zeta - xi zeta) --> | -1 -1 1 -1 1 -1 | xi 16442df84da0SMatthew G. Knepley \phi^3 = 1/4 ( -xi - eta - xi zeta - eta zeta) --> | -1 -1 -1 1 1 1 | zeta 16452df84da0SMatthew G. Knepley \phi^4 = 1/4 (1 + xi + zeta + xi zeta) --> | 1 1 -1 -1 1 -1 | xi zeta 16462df84da0SMatthew G. Knepley \phi^5 = 1/4 (1 + eta + zeta + eta zeta) --> \ 1 -1 1 -1 -1 1 / eta zeta 16472df84da0SMatthew G. Knepley 1/4 / 0 1 1 0 1 1 \ 16482df84da0SMatthew G. Knepley | -1 1 0 -1 0 1 | 16492df84da0SMatthew G. Knepley | -1 0 1 -1 1 0 | 16502df84da0SMatthew G. Knepley | 0 -1 -1 0 1 1 | 16512df84da0SMatthew G. Knepley | 1 0 -1 -1 1 0 | 16522df84da0SMatthew G. Knepley \ 1 -1 0 -1 0 1 / 16532df84da0SMatthew G. Knepley */ 16542df84da0SMatthew G. Knepley coeff[dim * 0 + j] = (1./4.) * ( verts[dim * 1 + j] + verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]); 16552df84da0SMatthew G. Knepley coeff[dim * 1 + j] = (1./4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]); 16562df84da0SMatthew G. Knepley coeff[dim * 2 + j] = (1./4.) * (-verts[dim * 0 + j] + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]); 16572df84da0SMatthew G. Knepley coeff[dim * 3 + j] = (1./4.) * ( - verts[dim * 1 + j] - verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]); 16582df84da0SMatthew G. Knepley coeff[dim * 4 + j] = (1./4.) * ( verts[dim * 0 + j] - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]); 16592df84da0SMatthew G. Knepley coeff[dim * 5 + j] = (1./4.) * ( verts[dim * 0 + j] - verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]); 16602df84da0SMatthew G. Knepley /* For reference prism: 16612df84da0SMatthew G. Knepley {0, 0, 0} 16622df84da0SMatthew G. Knepley {0, 1, 0} 16632df84da0SMatthew G. Knepley {1, 0, 0} 16642df84da0SMatthew G. Knepley {0, 0, 1} 16652df84da0SMatthew G. Knepley {0, 0, 0} 16662df84da0SMatthew G. Knepley {0, 0, 0} 16672df84da0SMatthew G. Knepley */ 16682df84da0SMatthew G. Knepley } 16692df84da0SMatthew G. Knepley for (i = 0; i < Nq; ++i) { 16702df84da0SMatthew G. Knepley const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2]; 16712df84da0SMatthew G. Knepley 16722df84da0SMatthew G. Knepley if (v) { 16732df84da0SMatthew G. Knepley PetscReal extPoint[6]; 16742df84da0SMatthew G. Knepley PetscInt c; 16752df84da0SMatthew G. Knepley 16762df84da0SMatthew G. Knepley extPoint[0] = 1.; 16772df84da0SMatthew G. Knepley extPoint[1] = eta; 16782df84da0SMatthew G. Knepley extPoint[2] = xi; 16792df84da0SMatthew G. Knepley extPoint[3] = zeta; 16802df84da0SMatthew G. Knepley extPoint[4] = xi * zeta; 16812df84da0SMatthew G. Knepley extPoint[5] = eta * zeta; 16822df84da0SMatthew G. Knepley for (c = 0; c < dim; ++c) { 16832df84da0SMatthew G. Knepley PetscReal val = 0.; 16842df84da0SMatthew G. Knepley 16852df84da0SMatthew G. Knepley for (k = 0; k < Nv; ++k) { 16862df84da0SMatthew G. Knepley val += extPoint[k] * coeff[k*dim + c]; 16872df84da0SMatthew G. Knepley } 16882df84da0SMatthew G. Knepley v[i*dim + c] = val; 16892df84da0SMatthew G. Knepley } 16902df84da0SMatthew G. Knepley } 16912df84da0SMatthew G. Knepley if (J) { 16922df84da0SMatthew G. Knepley PetscReal extJ[18]; 16932df84da0SMatthew G. Knepley 16942df84da0SMatthew G. Knepley extJ[0] = 0. ; extJ[1] = 0. ; extJ[2] = 0. ; 16952df84da0SMatthew G. Knepley extJ[3] = 0. ; extJ[4] = 1. ; extJ[5] = 0. ; 16962df84da0SMatthew G. Knepley extJ[6] = 1. ; extJ[7] = 0. ; extJ[8] = 0. ; 16972df84da0SMatthew G. Knepley extJ[9] = 0. ; extJ[10] = 0. ; extJ[11] = 1. ; 16982df84da0SMatthew G. Knepley extJ[12] = zeta; extJ[13] = 0. ; extJ[14] = xi ; 16992df84da0SMatthew G. Knepley extJ[15] = 0. ; extJ[16] = zeta; extJ[17] = eta; 17002df84da0SMatthew G. Knepley 17012df84da0SMatthew G. Knepley for (j = 0; j < dim; j++) { 17022df84da0SMatthew G. Knepley for (k = 0; k < dimR; k++) { 17032df84da0SMatthew G. Knepley PetscReal val = 0.; 17042df84da0SMatthew G. Knepley 17052df84da0SMatthew G. Knepley for (l = 0; l < Nv; l++) { 17062df84da0SMatthew G. Knepley val += coeff[dim * l + j] * extJ[dimR * l + k]; 17072df84da0SMatthew G. Knepley } 17082df84da0SMatthew G. Knepley J[i * dim * dim + dim * j + k] = val; 17092df84da0SMatthew G. Knepley } 17102df84da0SMatthew G. Knepley } 17112df84da0SMatthew G. Knepley DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 17122df84da0SMatthew G. Knepley if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);} 17132df84da0SMatthew G. Knepley } 17142df84da0SMatthew G. Knepley } 17152df84da0SMatthew G. Knepley } 17169566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords)); 17172df84da0SMatthew G. Knepley PetscFunctionReturn(0); 17182df84da0SMatthew G. Knepley } 17192df84da0SMatthew G. Knepley 1720dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 1721dfccc68fSToby Isaac { 1722ba2698f1SMatthew G. Knepley DMPolytopeType ct; 1723dfccc68fSToby Isaac PetscInt depth, dim, coordDim, coneSize, i; 1724dfccc68fSToby Isaac PetscInt Nq = 0; 1725dfccc68fSToby Isaac const PetscReal *points = NULL; 1726dfccc68fSToby Isaac DMLabel depthLabel; 1727c330f8ffSToby Isaac PetscReal xi0[3] = {-1.,-1.,-1.}, v0[3], J0[9], detJ0; 1728dfccc68fSToby Isaac PetscBool isAffine = PETSC_TRUE; 1729dfccc68fSToby Isaac 1730dfccc68fSToby Isaac PetscFunctionBegin; 17319566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 17329566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 17339566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dm, &depthLabel)); 17349566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(depthLabel, cell, &dim)); 1735dfccc68fSToby Isaac if (depth == 1 && dim == 1) { 17369566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 1737dfccc68fSToby Isaac } 17389566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &coordDim)); 17392c71b3e2SJacob Faibussowitsch PetscCheckFalse(coordDim > 3,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim); 17409566063dSJacob Faibussowitsch if (quad) PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL)); 17419566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 1742ba2698f1SMatthew G. Knepley switch (ct) { 1743ba2698f1SMatthew G. Knepley case DM_POLYTOPE_POINT: 17449566063dSJacob Faibussowitsch PetscCall(DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ)); 1745dfccc68fSToby Isaac isAffine = PETSC_FALSE; 1746dfccc68fSToby Isaac break; 1747ba2698f1SMatthew G. Knepley case DM_POLYTOPE_SEGMENT: 1748412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 17499566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 17509566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ)); 1751dfccc68fSToby Isaac break; 1752ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 17539566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 17549566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ)); 1755dfccc68fSToby Isaac break; 1756ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 17579566063dSJacob Faibussowitsch PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ)); 1758412e9a14SMatthew G. Knepley isAffine = PETSC_FALSE; 1759412e9a14SMatthew G. Knepley break; 1760412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 17619566063dSJacob Faibussowitsch PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ)); 1762dfccc68fSToby Isaac isAffine = PETSC_FALSE; 1763dfccc68fSToby Isaac break; 1764ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 17659566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 17669566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ)); 1767dfccc68fSToby Isaac break; 1768ba2698f1SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 17699566063dSJacob Faibussowitsch PetscCall(DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ)); 1770dfccc68fSToby Isaac isAffine = PETSC_FALSE; 1771dfccc68fSToby Isaac break; 17722df84da0SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM: 17739566063dSJacob Faibussowitsch PetscCall(DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ)); 17742df84da0SMatthew G. Knepley isAffine = PETSC_FALSE; 17752df84da0SMatthew G. Knepley break; 17762df84da0SMatthew G. Knepley default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No element geometry for cell %" PetscInt_FMT " with type %s", cell, DMPolytopeTypes[PetscMax(0, PetscMin(ct, DM_NUM_POLYTOPES))]); 1777dfccc68fSToby Isaac } 17787318780aSToby Isaac if (isAffine && Nq) { 1779dfccc68fSToby Isaac if (v) { 1780dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1781c330f8ffSToby Isaac CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]); 1782dfccc68fSToby Isaac } 1783dfccc68fSToby Isaac } 17847318780aSToby Isaac if (detJ) { 17857318780aSToby Isaac for (i = 0; i < Nq; i++) { 17867318780aSToby Isaac detJ[i] = detJ0; 1787dfccc68fSToby Isaac } 17887318780aSToby Isaac } 17897318780aSToby Isaac if (J) { 17907318780aSToby Isaac PetscInt k; 17917318780aSToby Isaac 17927318780aSToby Isaac for (i = 0, k = 0; i < Nq; i++) { 1793dfccc68fSToby Isaac PetscInt j; 1794dfccc68fSToby Isaac 17957318780aSToby Isaac for (j = 0; j < coordDim * coordDim; j++, k++) { 17967318780aSToby Isaac J[k] = J0[j]; 17977318780aSToby Isaac } 17987318780aSToby Isaac } 17997318780aSToby Isaac } 18007318780aSToby Isaac if (invJ) { 18017318780aSToby Isaac PetscInt k; 18027318780aSToby Isaac switch (coordDim) { 18037318780aSToby Isaac case 0: 18047318780aSToby Isaac break; 18057318780aSToby Isaac case 1: 18067318780aSToby Isaac invJ[0] = 1./J0[0]; 18077318780aSToby Isaac break; 18087318780aSToby Isaac case 2: 18097318780aSToby Isaac DMPlex_Invert2D_Internal(invJ, J0, detJ0); 18107318780aSToby Isaac break; 18117318780aSToby Isaac case 3: 18127318780aSToby Isaac DMPlex_Invert3D_Internal(invJ, J0, detJ0); 18137318780aSToby Isaac break; 18147318780aSToby Isaac } 18157318780aSToby Isaac for (i = 1, k = coordDim * coordDim; i < Nq; i++) { 18167318780aSToby Isaac PetscInt j; 18177318780aSToby Isaac 18187318780aSToby Isaac for (j = 0; j < coordDim * coordDim; j++, k++) { 18197318780aSToby Isaac invJ[k] = invJ[j]; 18207318780aSToby Isaac } 1821dfccc68fSToby Isaac } 1822dfccc68fSToby Isaac } 1823dfccc68fSToby Isaac } 1824dfccc68fSToby Isaac PetscFunctionReturn(0); 1825dfccc68fSToby Isaac } 1826dfccc68fSToby Isaac 1827ccd2543fSMatthew G Knepley /*@C 18288e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell 1829ccd2543fSMatthew G Knepley 1830d083f849SBarry Smith Collective on dm 1831ccd2543fSMatthew G Knepley 18324165533cSJose E. Roman Input Parameters: 1833ccd2543fSMatthew G Knepley + dm - the DM 1834ccd2543fSMatthew G Knepley - cell - the cell 1835ccd2543fSMatthew G Knepley 18364165533cSJose E. Roman Output Parameters: 1837*9b172b3aSMatthew Knepley + v0 - the translation part of this affine transform, meaning the translation to the origin (not the first vertex of the reference cell) 1838ccd2543fSMatthew G Knepley . J - the Jacobian of the transform from the reference element 1839ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian 1840ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant 1841ccd2543fSMatthew G Knepley 1842ccd2543fSMatthew G Knepley Level: advanced 1843ccd2543fSMatthew G Knepley 1844ccd2543fSMatthew G Knepley Fortran Notes: 1845ccd2543fSMatthew G Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 1846ccd2543fSMatthew G Knepley include petsc.h90 in your code. 1847ccd2543fSMatthew G Knepley 1848e8964c0aSStefano Zampini .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinates() 1849ccd2543fSMatthew G Knepley @*/ 18508e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 1851ccd2543fSMatthew G Knepley { 1852ccd2543fSMatthew G Knepley PetscFunctionBegin; 18539566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ)); 18548e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 18558e0841e0SMatthew G. Knepley } 18568e0841e0SMatthew G. Knepley 1857dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 18588e0841e0SMatthew G. Knepley { 1859dfccc68fSToby Isaac PetscQuadrature feQuad; 18608e0841e0SMatthew G. Knepley PetscSection coordSection; 18618e0841e0SMatthew G. Knepley Vec coordinates; 18628e0841e0SMatthew G. Knepley PetscScalar *coords = NULL; 18638e0841e0SMatthew G. Knepley const PetscReal *quadPoints; 1864ef0bb6c7SMatthew G. Knepley PetscTabulation T; 1865f960e424SToby Isaac PetscInt dim, cdim, pdim, qdim, Nq, numCoords, q; 18668e0841e0SMatthew G. Knepley 18678e0841e0SMatthew G. Knepley PetscFunctionBegin; 18689566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 18699566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 18709566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords)); 18719566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 18729566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 1873dfccc68fSToby Isaac if (!quad) { /* use the first point of the first functional of the dual space */ 1874dfccc68fSToby Isaac PetscDualSpace dsp; 1875dfccc68fSToby Isaac 18769566063dSJacob Faibussowitsch PetscCall(PetscFEGetDualSpace(fe, &dsp)); 18779566063dSJacob Faibussowitsch PetscCall(PetscDualSpaceGetFunctional(dsp, 0, &quad)); 18789566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL)); 1879dfccc68fSToby Isaac Nq = 1; 1880dfccc68fSToby Isaac } else { 18819566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL)); 1882dfccc68fSToby Isaac } 18839566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 18849566063dSJacob Faibussowitsch PetscCall(PetscFEGetQuadrature(fe, &feQuad)); 1885dfccc68fSToby Isaac if (feQuad == quad) { 18869566063dSJacob Faibussowitsch PetscCall(PetscFEGetCellTabulation(fe, J ? 1 : 0, &T)); 18872c71b3e2SJacob Faibussowitsch PetscCheckFalse(numCoords != pdim*cdim,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "There are %d coordinates for point %d != %d*%d", numCoords, point, pdim, cdim); 1888dfccc68fSToby Isaac } else { 18899566063dSJacob Faibussowitsch PetscCall(PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T)); 1890dfccc68fSToby Isaac } 18912c71b3e2SJacob Faibussowitsch PetscCheckFalse(qdim != dim,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim); 1892ef0bb6c7SMatthew G. Knepley { 1893ef0bb6c7SMatthew G. Knepley const PetscReal *basis = T->T[0]; 1894ef0bb6c7SMatthew G. Knepley const PetscReal *basisDer = T->T[1]; 1895ef0bb6c7SMatthew G. Knepley PetscReal detJt; 1896ef0bb6c7SMatthew G. Knepley 1897a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG) 18982c71b3e2SJacob Faibussowitsch PetscCheckFalse(Nq != T->Np,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %D != %D", Nq, T->Np); 18992c71b3e2SJacob Faibussowitsch PetscCheckFalse(pdim != T->Nb,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %D != %D", pdim, T->Nb); 19002c71b3e2SJacob Faibussowitsch PetscCheckFalse(dim != T->Nc,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %D != %D", dim, T->Nc); 19012c71b3e2SJacob Faibussowitsch PetscCheckFalse(cdim != T->cdim,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %D != %D", cdim, T->cdim); 1902a2a9e04cSMatthew G. Knepley #endif 1903dfccc68fSToby Isaac if (v) { 19049566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(v, Nq*cdim)); 1905f960e424SToby Isaac for (q = 0; q < Nq; ++q) { 1906f960e424SToby Isaac PetscInt i, k; 1907f960e424SToby Isaac 1908301b184aSMatthew G. Knepley for (k = 0; k < pdim; ++k) { 1909301b184aSMatthew G. Knepley const PetscInt vertex = k/cdim; 1910301b184aSMatthew G. Knepley for (i = 0; i < cdim; ++i) { 1911301b184aSMatthew G. Knepley v[q*cdim + i] += basis[(q*pdim + k)*cdim + i] * PetscRealPart(coords[vertex*cdim + i]); 1912301b184aSMatthew G. Knepley } 1913301b184aSMatthew G. Knepley } 19149566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0*pdim*cdim)); 1915f960e424SToby Isaac } 1916f960e424SToby Isaac } 19178e0841e0SMatthew G. Knepley if (J) { 19189566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(J, Nq*cdim*cdim)); 19198e0841e0SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 19208e0841e0SMatthew G. Knepley PetscInt i, j, k, c, r; 19218e0841e0SMatthew G. Knepley 19228e0841e0SMatthew G. Knepley /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */ 1923301b184aSMatthew G. Knepley for (k = 0; k < pdim; ++k) { 1924301b184aSMatthew G. Knepley const PetscInt vertex = k/cdim; 1925301b184aSMatthew G. Knepley for (j = 0; j < dim; ++j) { 1926301b184aSMatthew G. Knepley for (i = 0; i < cdim; ++i) { 1927301b184aSMatthew G. Knepley J[(q*cdim + i)*cdim + j] += basisDer[((q*pdim + k)*cdim + i)*dim + j] * PetscRealPart(coords[vertex*cdim + i]); 1928301b184aSMatthew G. Knepley } 1929301b184aSMatthew G. Knepley } 1930301b184aSMatthew G. Knepley } 19319566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0*pdim*dim*cdim)); 19328e0841e0SMatthew G. Knepley if (cdim > dim) { 19338e0841e0SMatthew G. Knepley for (c = dim; c < cdim; ++c) 19348e0841e0SMatthew G. Knepley for (r = 0; r < cdim; ++r) 19358e0841e0SMatthew G. Knepley J[r*cdim+c] = r == c ? 1.0 : 0.0; 19368e0841e0SMatthew G. Knepley } 1937f960e424SToby Isaac if (!detJ && !invJ) continue; 1938a63b72c6SToby Isaac detJt = 0.; 19398e0841e0SMatthew G. Knepley switch (cdim) { 19408e0841e0SMatthew G. Knepley case 3: 1941037dc194SToby Isaac DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]); 1942037dc194SToby Isaac if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);} 194317fe8556SMatthew G. Knepley break; 194449dc4407SMatthew G. Knepley case 2: 19459f328543SToby Isaac DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]); 1946037dc194SToby Isaac if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);} 194749dc4407SMatthew G. Knepley break; 19488e0841e0SMatthew G. Knepley case 1: 1949037dc194SToby Isaac detJt = J[q*cdim*dim]; 1950037dc194SToby Isaac if (invJ) invJ[q*cdim*dim] = 1.0/detJt; 195149dc4407SMatthew G. Knepley } 1952f960e424SToby Isaac if (detJ) detJ[q] = detJt; 195349dc4407SMatthew G. Knepley } 19542c71b3e2SJacob Faibussowitsch } else PetscCheckFalse(detJ || invJ,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ"); 195549dc4407SMatthew G. Knepley } 19569566063dSJacob Faibussowitsch if (feQuad != quad) PetscCall(PetscTabulationDestroy(&T)); 19579566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords)); 19588e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 19598e0841e0SMatthew G. Knepley } 19608e0841e0SMatthew G. Knepley 19618e0841e0SMatthew G. Knepley /*@C 19628e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell 19638e0841e0SMatthew G. Knepley 1964d083f849SBarry Smith Collective on dm 19658e0841e0SMatthew G. Knepley 19664165533cSJose E. Roman Input Parameters: 19678e0841e0SMatthew G. Knepley + dm - the DM 19688e0841e0SMatthew G. Knepley . cell - the cell 1969dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated. If quad == NULL, geometry will be 1970dfccc68fSToby Isaac evaluated at the first vertex of the reference element 19718e0841e0SMatthew G. Knepley 19724165533cSJose E. Roman Output Parameters: 1973dfccc68fSToby Isaac + v - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element 19748e0841e0SMatthew G. Knepley . J - the Jacobian of the transform from the reference element at each quadrature point 19758e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point 19768e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point 19778e0841e0SMatthew G. Knepley 19788e0841e0SMatthew G. Knepley Level: advanced 19798e0841e0SMatthew G. Knepley 19808e0841e0SMatthew G. Knepley Fortran Notes: 19818e0841e0SMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 19828e0841e0SMatthew G. Knepley include petsc.h90 in your code. 19838e0841e0SMatthew G. Knepley 1984e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates() 19858e0841e0SMatthew G. Knepley @*/ 1986dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 19878e0841e0SMatthew G. Knepley { 1988bb4a5db5SMatthew G. Knepley DM cdm; 1989dfccc68fSToby Isaac PetscFE fe = NULL; 19908e0841e0SMatthew G. Knepley 19918e0841e0SMatthew G. Knepley PetscFunctionBegin; 1992dadcf809SJacob Faibussowitsch PetscValidRealPointer(detJ, 7); 19939566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 1994bb4a5db5SMatthew G. Knepley if (cdm) { 1995dfccc68fSToby Isaac PetscClassId id; 1996dfccc68fSToby Isaac PetscInt numFields; 1997e5e52638SMatthew G. Knepley PetscDS prob; 1998dfccc68fSToby Isaac PetscObject disc; 1999dfccc68fSToby Isaac 20009566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(cdm, &numFields)); 2001dfccc68fSToby Isaac if (numFields) { 20029566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &prob)); 20039566063dSJacob Faibussowitsch PetscCall(PetscDSGetDiscretization(prob,0,&disc)); 20049566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc,&id)); 2005dfccc68fSToby Isaac if (id == PETSCFE_CLASSID) { 2006dfccc68fSToby Isaac fe = (PetscFE) disc; 2007dfccc68fSToby Isaac } 2008dfccc68fSToby Isaac } 2009dfccc68fSToby Isaac } 20109566063dSJacob Faibussowitsch if (!fe) PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ)); 20119566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ)); 2012ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 2013ccd2543fSMatthew G Knepley } 2014834e62ceSMatthew G. Knepley 20159bf2564aSMatt McGurn static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 20169bf2564aSMatt McGurn { 20179bf2564aSMatt McGurn PetscSection coordSection; 20189bf2564aSMatt McGurn Vec coordinates; 20199bf2564aSMatt McGurn const PetscScalar *coords = NULL; 20209bf2564aSMatt McGurn PetscInt d, dof, off; 20219bf2564aSMatt McGurn 20229bf2564aSMatt McGurn PetscFunctionBegin; 20239566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 20249566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 20259566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coords)); 20269bf2564aSMatt McGurn 20279bf2564aSMatt McGurn /* for a point the centroid is just the coord */ 20289bf2564aSMatt McGurn if (centroid) { 20299566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, cell, &dof)); 20309566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, cell, &off)); 20319bf2564aSMatt McGurn for (d = 0; d < dof; d++){ 20329bf2564aSMatt McGurn centroid[d] = PetscRealPart(coords[off + d]); 20339bf2564aSMatt McGurn } 20349bf2564aSMatt McGurn } 20359bf2564aSMatt McGurn if (normal) { 20369bf2564aSMatt McGurn const PetscInt *support, *cones; 20379bf2564aSMatt McGurn PetscInt supportSize; 20389bf2564aSMatt McGurn PetscReal norm, sign; 20399bf2564aSMatt McGurn 20409bf2564aSMatt McGurn /* compute the norm based upon the support centroids */ 20419566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, cell, &supportSize)); 20429566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, cell, &support)); 20439566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL)); 20449bf2564aSMatt McGurn 20459bf2564aSMatt McGurn /* Take the normal from the centroid of the support to the vertex*/ 20469566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, cell, &dof)); 20479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, cell, &off)); 20489bf2564aSMatt McGurn for (d = 0; d < dof; d++){ 20499bf2564aSMatt McGurn normal[d] -= PetscRealPart(coords[off + d]); 20509bf2564aSMatt McGurn } 20519bf2564aSMatt McGurn 20529bf2564aSMatt McGurn /* Determine the sign of the normal based upon its location in the support */ 20539566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, support[0], &cones)); 20549bf2564aSMatt McGurn sign = cones[0] == cell ? 1.0 : -1.0; 20559bf2564aSMatt McGurn 20569bf2564aSMatt McGurn norm = DMPlex_NormD_Internal(dim, normal); 20579bf2564aSMatt McGurn for (d = 0; d < dim; ++d) normal[d] /= (norm*sign); 20589bf2564aSMatt McGurn } 20599bf2564aSMatt McGurn if (vol) { 20609bf2564aSMatt McGurn *vol = 1.0; 20619bf2564aSMatt McGurn } 20629566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coords)); 20639bf2564aSMatt McGurn PetscFunctionReturn(0); 20649bf2564aSMatt McGurn } 20659bf2564aSMatt McGurn 2066011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2067cc08537eSMatthew G. Knepley { 2068cc08537eSMatthew G. Knepley PetscSection coordSection; 2069cc08537eSMatthew G. Knepley Vec coordinates; 2070a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 207106e2781eSMatthew G. Knepley PetscScalar tmp[2]; 2072714b99b6SMatthew G. Knepley PetscInt coordSize, d; 2073cc08537eSMatthew G. Knepley 2074cc08537eSMatthew G. Knepley PetscFunctionBegin; 20759566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 20769566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 20779566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords)); 20789566063dSJacob Faibussowitsch PetscCall(DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp)); 2079cc08537eSMatthew G. Knepley if (centroid) { 2080714b99b6SMatthew G. Knepley for (d = 0; d < dim; ++d) centroid[d] = 0.5*PetscRealPart(coords[d] + tmp[d]); 2081cc08537eSMatthew G. Knepley } 2082cc08537eSMatthew G. Knepley if (normal) { 2083a60a936bSMatthew G. Knepley PetscReal norm; 2084a60a936bSMatthew G. Knepley 20852c71b3e2SJacob Faibussowitsch PetscCheckFalse(dim != 2,PETSC_COMM_SELF, PETSC_ERR_SUP, "We only support 2D edges right now"); 208606e2781eSMatthew G. Knepley normal[0] = -PetscRealPart(coords[1] - tmp[1]); 208706e2781eSMatthew G. Knepley normal[1] = PetscRealPart(coords[0] - tmp[0]); 2088714b99b6SMatthew G. Knepley norm = DMPlex_NormD_Internal(dim, normal); 2089714b99b6SMatthew G. Knepley for (d = 0; d < dim; ++d) normal[d] /= norm; 2090cc08537eSMatthew G. Knepley } 2091cc08537eSMatthew G. Knepley if (vol) { 2092714b99b6SMatthew G. Knepley *vol = 0.0; 2093714b99b6SMatthew G. Knepley for (d = 0; d < dim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - tmp[d])); 2094714b99b6SMatthew G. Knepley *vol = PetscSqrtReal(*vol); 2095cc08537eSMatthew G. Knepley } 20969566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords)); 2097cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 2098cc08537eSMatthew G. Knepley } 2099cc08537eSMatthew G. Knepley 2100cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */ 2101011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2102cc08537eSMatthew G. Knepley { 2103412e9a14SMatthew G. Knepley DMPolytopeType ct; 2104cc08537eSMatthew G. Knepley PetscSection coordSection; 2105cc08537eSMatthew G. Knepley Vec coordinates; 2106cc08537eSMatthew G. Knepley PetscScalar *coords = NULL; 2107793a2a13SMatthew G. Knepley PetscInt fv[4] = {0, 1, 2, 3}; 21084f99dae5SMatthew G. Knepley PetscInt cdim, coordSize, numCorners, p, d; 2109cc08537eSMatthew G. Knepley 2110cc08537eSMatthew G. Knepley PetscFunctionBegin; 2111793a2a13SMatthew G. Knepley /* Must check for hybrid cells because prisms have a different orientation scheme */ 21129566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2113412e9a14SMatthew G. Knepley switch (ct) { 21144f99dae5SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: fv[2] = 3; fv[3] = 2;break; 2115412e9a14SMatthew G. Knepley default: break; 2116412e9a14SMatthew G. Knepley } 21179566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 21189566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &numCorners)); 21199566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 21209566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords)); 21219566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 21223f27a4e6SJed Brown { 21233f27a4e6SJed Brown PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm; 2124793a2a13SMatthew G. Knepley 21253f27a4e6SJed Brown for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]); 21264f99dae5SMatthew G. Knepley for (p = 0; p < numCorners-2; ++p) { 21273f27a4e6SJed Brown PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.}; 21283f27a4e6SJed Brown for (d = 0; d < cdim; d++) { 21293f27a4e6SJed Brown e0[d] = PetscRealPart(coords[cdim*fv[p+1]+d]) - origin[d]; 21303f27a4e6SJed Brown e1[d] = PetscRealPart(coords[cdim*fv[p+2]+d]) - origin[d]; 21313f27a4e6SJed Brown } 21323f27a4e6SJed Brown const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1]; 21333f27a4e6SJed Brown const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2]; 21343f27a4e6SJed Brown const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0]; 21353f27a4e6SJed Brown const PetscReal a = PetscSqrtReal(dx*dx + dy*dy + dz*dz); 21364f99dae5SMatthew G. Knepley 21374f99dae5SMatthew G. Knepley n[0] += dx; 21384f99dae5SMatthew G. Knepley n[1] += dy; 21394f99dae5SMatthew G. Knepley n[2] += dz; 21403f27a4e6SJed Brown for (d = 0; d < cdim; d++) { 21413f27a4e6SJed Brown c[d] += a * PetscRealPart(origin[d] + coords[cdim*fv[p+1]+d] + coords[cdim*fv[p+2]+d]) / 3.; 21423f27a4e6SJed Brown } 2143ceee4971SMatthew G. Knepley } 21444f99dae5SMatthew G. Knepley norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]); 21454f99dae5SMatthew G. Knepley n[0] /= norm; 21464f99dae5SMatthew G. Knepley n[1] /= norm; 21474f99dae5SMatthew G. Knepley n[2] /= norm; 21484f99dae5SMatthew G. Knepley c[0] /= norm; 21494f99dae5SMatthew G. Knepley c[1] /= norm; 21504f99dae5SMatthew G. Knepley c[2] /= norm; 21514f99dae5SMatthew G. Knepley if (vol) *vol = 0.5*norm; 21524f99dae5SMatthew G. Knepley if (centroid) for (d = 0; d < cdim; ++d) centroid[d] = c[d]; 21534f99dae5SMatthew G. Knepley if (normal) for (d = 0; d < cdim; ++d) normal[d] = n[d]; 21540a1d6728SMatthew G. Knepley } 21559566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords)); 2156cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 2157cc08537eSMatthew G. Knepley } 2158cc08537eSMatthew G. Knepley 21590ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */ 2160011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 21610ec8681fSMatthew G. Knepley { 2162412e9a14SMatthew G. Knepley DMPolytopeType ct; 21630ec8681fSMatthew G. Knepley PetscSection coordSection; 21640ec8681fSMatthew G. Knepley Vec coordinates; 21650ec8681fSMatthew G. Knepley PetscScalar *coords = NULL; 21663f27a4e6SJed Brown PetscReal vsum = 0.0, vtmp, coordsTmp[3*3], origin[3]; 2167a7df9edeSMatthew G. Knepley const PetscInt *faces, *facesO; 2168793a2a13SMatthew G. Knepley PetscBool isHybrid = PETSC_FALSE; 2169412e9a14SMatthew G. Knepley PetscInt numFaces, f, coordSize, p, d; 21700ec8681fSMatthew G. Knepley 21710ec8681fSMatthew G. Knepley PetscFunctionBegin; 21722c71b3e2SJacob Faibussowitsch PetscCheckFalse(dim > 3,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim); 2173793a2a13SMatthew G. Knepley /* Must check for hybrid cells because prisms have a different orientation scheme */ 21749566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2175412e9a14SMatthew G. Knepley switch (ct) { 2176412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 2177412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 2178412e9a14SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM_TENSOR: 2179412e9a14SMatthew G. Knepley case DM_POLYTOPE_QUAD_PRISM_TENSOR: 2180412e9a14SMatthew G. Knepley isHybrid = PETSC_TRUE; 2181412e9a14SMatthew G. Knepley default: break; 2182412e9a14SMatthew G. Knepley } 2183793a2a13SMatthew G. Knepley 21849566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 21859566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 21860ec8681fSMatthew G. Knepley 2187d9a81ebdSMatthew G. Knepley if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0; 21889566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &numFaces)); 21899566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, cell, &faces)); 21909566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientation(dm, cell, &facesO)); 21910ec8681fSMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 2192793a2a13SMatthew G. Knepley PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */ 2193ba2698f1SMatthew G. Knepley DMPolytopeType ct; 2194793a2a13SMatthew G. Knepley 21959566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords)); 21963f27a4e6SJed Brown // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and 21973f27a4e6SJed Brown // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex 21983f27a4e6SJed Brown // so that all tetrahedra have positive volume. 21993f27a4e6SJed Brown if (f == 0) for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]); 22009566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, faces[f], &ct)); 2201ba2698f1SMatthew G. Knepley switch (ct) { 2202ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 22030ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 22043f27a4e6SJed Brown coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]) - origin[d]; 22053f27a4e6SJed Brown coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]) - origin[d]; 22063f27a4e6SJed Brown coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]) - origin[d]; 22070ec8681fSMatthew G. Knepley } 22080ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 2209793a2a13SMatthew G. Knepley if (facesO[f] < 0 || flip) vtmp = -vtmp; 22100ec8681fSMatthew G. Knepley vsum += vtmp; 22114f25033aSJed Brown if (centroid) { /* Centroid of OABC = (a+b+c)/4 */ 22120ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 22131ee9d5ecSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 22140ec8681fSMatthew G. Knepley } 22150ec8681fSMatthew G. Knepley } 22160ec8681fSMatthew G. Knepley break; 2217ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 2218412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 2219793a2a13SMatthew G. Knepley { 2220793a2a13SMatthew G. Knepley PetscInt fv[4] = {0, 1, 2, 3}; 2221793a2a13SMatthew G. Knepley 2222793a2a13SMatthew G. Knepley /* Side faces for hybrid cells are are stored as tensor products */ 2223793a2a13SMatthew G. Knepley if (isHybrid && f > 1) {fv[2] = 3; fv[3] = 2;} 22240ec8681fSMatthew G. Knepley /* DO FOR PYRAMID */ 22250ec8681fSMatthew G. Knepley /* First tet */ 22260ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 22273f27a4e6SJed Brown coordsTmp[0*dim+d] = PetscRealPart(coords[fv[0]*dim+d]) - origin[d]; 22283f27a4e6SJed Brown coordsTmp[1*dim+d] = PetscRealPart(coords[fv[1]*dim+d]) - origin[d]; 22293f27a4e6SJed Brown coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]) - origin[d]; 22300ec8681fSMatthew G. Knepley } 22310ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 2232793a2a13SMatthew G. Knepley if (facesO[f] < 0 || flip) vtmp = -vtmp; 22330ec8681fSMatthew G. Knepley vsum += vtmp; 22340ec8681fSMatthew G. Knepley if (centroid) { 22350ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 22360ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 22370ec8681fSMatthew G. Knepley } 22380ec8681fSMatthew G. Knepley } 22390ec8681fSMatthew G. Knepley /* Second tet */ 22400ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 22413f27a4e6SJed Brown coordsTmp[0*dim+d] = PetscRealPart(coords[fv[1]*dim+d]) - origin[d]; 22423f27a4e6SJed Brown coordsTmp[1*dim+d] = PetscRealPart(coords[fv[2]*dim+d]) - origin[d]; 22433f27a4e6SJed Brown coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]) - origin[d]; 22440ec8681fSMatthew G. Knepley } 22450ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 2246793a2a13SMatthew G. Knepley if (facesO[f] < 0 || flip) vtmp = -vtmp; 22470ec8681fSMatthew G. Knepley vsum += vtmp; 22480ec8681fSMatthew G. Knepley if (centroid) { 22490ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 22500ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 22510ec8681fSMatthew G. Knepley } 22520ec8681fSMatthew G. Knepley } 22530ec8681fSMatthew G. Knepley break; 2254793a2a13SMatthew G. Knepley } 22550ec8681fSMatthew G. Knepley default: 225698921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %D of type %s", faces[f], DMPolytopeTypes[ct]); 22570ec8681fSMatthew G. Knepley } 22589566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords)); 22590ec8681fSMatthew G. Knepley } 22608763be8eSMatthew G. Knepley if (vol) *vol = PetscAbsReal(vsum); 22610ec8681fSMatthew G. Knepley if (normal) for (d = 0; d < dim; ++d) normal[d] = 0.0; 22623f27a4e6SJed Brown if (centroid) for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum*4) + origin[d]; 22633f27a4e6SJed Brown ; 22640ec8681fSMatthew G. Knepley PetscFunctionReturn(0); 22650ec8681fSMatthew G. Knepley } 22660ec8681fSMatthew G. Knepley 2267834e62ceSMatthew G. Knepley /*@C 2268834e62ceSMatthew G. Knepley DMPlexComputeCellGeometryFVM - Compute the volume for a given cell 2269834e62ceSMatthew G. Knepley 2270d083f849SBarry Smith Collective on dm 2271834e62ceSMatthew G. Knepley 22724165533cSJose E. Roman Input Parameters: 2273834e62ceSMatthew G. Knepley + dm - the DM 2274834e62ceSMatthew G. Knepley - cell - the cell 2275834e62ceSMatthew G. Knepley 22764165533cSJose E. Roman Output Parameters: 2277834e62ceSMatthew G. Knepley + volume - the cell volume 2278cc08537eSMatthew G. Knepley . centroid - the cell centroid 2279cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate 2280834e62ceSMatthew G. Knepley 2281834e62ceSMatthew G. Knepley Level: advanced 2282834e62ceSMatthew G. Knepley 2283834e62ceSMatthew G. Knepley Fortran Notes: 2284834e62ceSMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 2285834e62ceSMatthew G. Knepley include petsc.h90 in your code. 2286834e62ceSMatthew G. Knepley 2287e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates() 2288834e62ceSMatthew G. Knepley @*/ 2289cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2290834e62ceSMatthew G. Knepley { 22910ec8681fSMatthew G. Knepley PetscInt depth, dim; 2292834e62ceSMatthew G. Knepley 2293834e62ceSMatthew G. Knepley PetscFunctionBegin; 22949566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 22959566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 22962c71b3e2SJacob Faibussowitsch PetscCheckFalse(depth != dim,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated"); 22979566063dSJacob Faibussowitsch PetscCall(DMPlexGetPointDepth(dm, cell, &depth)); 2298011ea5d8SMatthew G. Knepley switch (depth) { 22999bf2564aSMatt McGurn case 0: 23009566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal)); 23019bf2564aSMatt McGurn break; 2302cc08537eSMatthew G. Knepley case 1: 23039566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal)); 2304cc08537eSMatthew G. Knepley break; 2305834e62ceSMatthew G. Knepley case 2: 23069566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal)); 2307834e62ceSMatthew G. Knepley break; 2308834e62ceSMatthew G. Knepley case 3: 23099566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal)); 2310834e62ceSMatthew G. Knepley break; 2311834e62ceSMatthew G. Knepley default: 231298921bdaSJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D (depth %D) for element geometry computation", dim, depth); 2313834e62ceSMatthew G. Knepley } 2314834e62ceSMatthew G. Knepley PetscFunctionReturn(0); 2315834e62ceSMatthew G. Knepley } 2316113c68e6SMatthew G. Knepley 2317c501906fSMatthew G. Knepley /*@ 2318c501906fSMatthew G. Knepley DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh 2319c501906fSMatthew G. Knepley 2320c501906fSMatthew G. Knepley Collective on dm 2321c501906fSMatthew G. Knepley 2322c501906fSMatthew G. Knepley Input Parameter: 2323c501906fSMatthew G. Knepley . dm - The DMPlex 2324c501906fSMatthew G. Knepley 2325c501906fSMatthew G. Knepley Output Parameter: 2326c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell 2327c501906fSMatthew G. Knepley 2328c501906fSMatthew G. Knepley Level: beginner 2329c501906fSMatthew G. Knepley 2330c501906fSMatthew G. Knepley @*/ 2331c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom) 2332c0d900a5SMatthew G. Knepley { 2333c0d900a5SMatthew G. Knepley DM dmCell; 2334c0d900a5SMatthew G. Knepley Vec coordinates; 2335c0d900a5SMatthew G. Knepley PetscSection coordSection, sectionCell; 2336c0d900a5SMatthew G. Knepley PetscScalar *cgeom; 2337412e9a14SMatthew G. Knepley PetscInt cStart, cEnd, c; 2338c0d900a5SMatthew G. Knepley 2339c0d900a5SMatthew G. Knepley PetscFunctionBegin; 23409566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmCell)); 23419566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 23429566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 23439566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection)); 23449566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmCell, coordinates)); 23459566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell)); 23469566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 23479566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd)); 2348c0d900a5SMatthew G. Knepley /* TODO This needs to be multiplied by Nq for non-affine */ 23499566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFEGeom))/sizeof(PetscScalar)))); 23509566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionCell)); 23519566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmCell, sectionCell)); 23529566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionCell)); 23539566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmCell, cellgeom)); 23549566063dSJacob Faibussowitsch PetscCall(VecGetArray(*cellgeom, &cgeom)); 2355c0d900a5SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 2356cf0b7c11SKarl Rupp PetscFEGeom *cg; 2357c0d900a5SMatthew G. Knepley 23589566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg)); 23599566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(cg, 1)); 23609566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ)); 23612c71b3e2SJacob Faibussowitsch PetscCheckFalse(*cg->detJ <= 0.0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D", (double) *cg->detJ, c); 2362c0d900a5SMatthew G. Knepley } 2363c0d900a5SMatthew G. Knepley PetscFunctionReturn(0); 2364c0d900a5SMatthew G. Knepley } 2365c0d900a5SMatthew G. Knepley 2366891a9168SMatthew G. Knepley /*@ 2367891a9168SMatthew G. Knepley DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method 2368891a9168SMatthew G. Knepley 2369891a9168SMatthew G. Knepley Input Parameter: 2370891a9168SMatthew G. Knepley . dm - The DM 2371891a9168SMatthew G. Knepley 2372891a9168SMatthew G. Knepley Output Parameters: 2373891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data 2374a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data 2375891a9168SMatthew G. Knepley 2376891a9168SMatthew G. Knepley Level: developer 2377891a9168SMatthew G. Knepley 2378891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM() 2379891a9168SMatthew G. Knepley @*/ 2380113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) 2381113c68e6SMatthew G. Knepley { 2382113c68e6SMatthew G. Knepley DM dmFace, dmCell; 2383113c68e6SMatthew G. Knepley DMLabel ghostLabel; 2384113c68e6SMatthew G. Knepley PetscSection sectionFace, sectionCell; 2385113c68e6SMatthew G. Knepley PetscSection coordSection; 2386113c68e6SMatthew G. Knepley Vec coordinates; 2387113c68e6SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 2388113c68e6SMatthew G. Knepley PetscReal minradius, gminradius; 2389113c68e6SMatthew G. Knepley PetscInt dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f; 2390113c68e6SMatthew G. Knepley 2391113c68e6SMatthew G. Knepley PetscFunctionBegin; 23929566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 23939566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 23949566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 2395113c68e6SMatthew G. Knepley /* Make cell centroids and volumes */ 23969566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmCell)); 23979566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection)); 23989566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmCell, coordinates)); 23999566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell)); 24009566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 24019566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 24029566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd)); 24039566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)))); 24049566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionCell)); 24059566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmCell, sectionCell)); 24069566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionCell)); 24079566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmCell, cellgeom)); 2408485ad865SMatthew G. Knepley if (cEndInterior < 0) cEndInterior = cEnd; 24099566063dSJacob Faibussowitsch PetscCall(VecGetArray(*cellgeom, &cgeom)); 2410113c68e6SMatthew G. Knepley for (c = cStart; c < cEndInterior; ++c) { 2411113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 2412113c68e6SMatthew G. Knepley 24139566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg)); 24149566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(cg, 1)); 24159566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL)); 2416113c68e6SMatthew G. Knepley } 2417113c68e6SMatthew G. Knepley /* Compute face normals and minimum cell radius */ 24189566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmFace)); 24199566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionFace)); 24209566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 24219566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionFace, fStart, fEnd)); 24229566063dSJacob Faibussowitsch for (f = fStart; f < fEnd; ++f) PetscCall(PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)))); 24239566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionFace)); 24249566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmFace, sectionFace)); 24259566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionFace)); 24269566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmFace, facegeom)); 24279566063dSJacob Faibussowitsch PetscCall(VecGetArray(*facegeom, &fgeom)); 24289566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 2429113c68e6SMatthew G. Knepley minradius = PETSC_MAX_REAL; 2430113c68e6SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 2431113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 2432113c68e6SMatthew G. Knepley PetscReal area; 2433412e9a14SMatthew G. Knepley const PetscInt *cells; 2434412e9a14SMatthew G. Knepley PetscInt ncells, ghost = -1, d, numChildren; 2435113c68e6SMatthew G. Knepley 24369566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 24379566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm,f,&numChildren,NULL)); 24389566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &cells)); 24399566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &ncells)); 2440412e9a14SMatthew G. Knepley /* It is possible to get a face with no support when using partition overlap */ 2441412e9a14SMatthew G. Knepley if (!ncells || ghost >= 0 || numChildren) continue; 24429566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, f, fgeom, &fg)); 24439566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal)); 2444113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] *= area; 2445113c68e6SMatthew G. Knepley /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */ 2446113c68e6SMatthew G. Knepley { 2447113c68e6SMatthew G. Knepley PetscFVCellGeom *cL, *cR; 2448113c68e6SMatthew G. Knepley PetscReal *lcentroid, *rcentroid; 24490453c0cdSMatthew G. Knepley PetscReal l[3], r[3], v[3]; 2450113c68e6SMatthew G. Knepley 24519566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL)); 2452113c68e6SMatthew G. Knepley lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid; 245306348e87SToby Isaac if (ncells > 1) { 24549566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR)); 2455113c68e6SMatthew G. Knepley rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid; 245606348e87SToby Isaac } 245706348e87SToby Isaac else { 245806348e87SToby Isaac rcentroid = fg->centroid; 245906348e87SToby Isaac } 24609566063dSJacob Faibussowitsch PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l)); 24619566063dSJacob Faibussowitsch PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r)); 24620453c0cdSMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, l, r, v); 2463113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) { 2464113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d]; 2465113c68e6SMatthew G. Knepley } 2466113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) { 24672c71b3e2SJacob Faibussowitsch PetscCheckFalse(dim == 2,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]); 24682c71b3e2SJacob Faibussowitsch PetscCheckFalse(dim == 3,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]); 246998921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f); 2470113c68e6SMatthew G. Knepley } 2471113c68e6SMatthew G. Knepley if (cells[0] < cEndInterior) { 2472113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v); 2473113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2474113c68e6SMatthew G. Knepley } 247506348e87SToby Isaac if (ncells > 1 && cells[1] < cEndInterior) { 2476113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v); 2477113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2478113c68e6SMatthew G. Knepley } 2479113c68e6SMatthew G. Knepley } 2480113c68e6SMatthew G. Knepley } 24811c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm))); 24829566063dSJacob Faibussowitsch PetscCall(DMPlexSetMinRadius(dm, gminradius)); 2483113c68e6SMatthew G. Knepley /* Compute centroids of ghost cells */ 2484113c68e6SMatthew G. Knepley for (c = cEndInterior; c < cEnd; ++c) { 2485113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 2486113c68e6SMatthew G. Knepley const PetscInt *cone, *support; 2487113c68e6SMatthew G. Knepley PetscInt coneSize, supportSize, s; 2488113c68e6SMatthew G. Knepley 24899566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dmCell, c, &coneSize)); 24902c71b3e2SJacob Faibussowitsch PetscCheckFalse(coneSize != 1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize); 24919566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dmCell, c, &cone)); 24929566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dmCell, cone[0], &supportSize)); 24932c71b3e2SJacob Faibussowitsch PetscCheckFalse(supportSize != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize); 24949566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dmCell, cone[0], &support)); 24959566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg)); 2496113c68e6SMatthew G. Knepley for (s = 0; s < 2; ++s) { 2497113c68e6SMatthew G. Knepley /* Reflect ghost centroid across plane of face */ 2498113c68e6SMatthew G. Knepley if (support[s] == c) { 2499640bce14SSatish Balay PetscFVCellGeom *ci; 2500113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 2501113c68e6SMatthew G. Knepley PetscReal c2f[3], a; 2502113c68e6SMatthew G. Knepley 25039566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci)); 2504113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */ 2505113c68e6SMatthew G. Knepley a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal); 25069566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg)); 2507113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid); 2508113c68e6SMatthew G. Knepley cg->volume = ci->volume; 2509113c68e6SMatthew G. Knepley } 2510113c68e6SMatthew G. Knepley } 2511113c68e6SMatthew G. Knepley } 25129566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(*facegeom, &fgeom)); 25139566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(*cellgeom, &cgeom)); 25149566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmCell)); 25159566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmFace)); 2516113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 2517113c68e6SMatthew G. Knepley } 2518113c68e6SMatthew G. Knepley 2519113c68e6SMatthew G. Knepley /*@C 2520113c68e6SMatthew G. Knepley DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face 2521113c68e6SMatthew G. Knepley 2522113c68e6SMatthew G. Knepley Not collective 2523113c68e6SMatthew G. Knepley 25244165533cSJose E. Roman Input Parameter: 2525113c68e6SMatthew G. Knepley . dm - the DM 2526113c68e6SMatthew G. Knepley 25274165533cSJose E. Roman Output Parameter: 2528a5b23f4aSJose E. Roman . minradius - the minimum cell radius 2529113c68e6SMatthew G. Knepley 2530113c68e6SMatthew G. Knepley Level: developer 2531113c68e6SMatthew G. Knepley 2532113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates() 2533113c68e6SMatthew G. Knepley @*/ 2534113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) 2535113c68e6SMatthew G. Knepley { 2536113c68e6SMatthew G. Knepley PetscFunctionBegin; 2537113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2538dadcf809SJacob Faibussowitsch PetscValidRealPointer(minradius,2); 2539113c68e6SMatthew G. Knepley *minradius = ((DM_Plex*) dm->data)->minradius; 2540113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 2541113c68e6SMatthew G. Knepley } 2542113c68e6SMatthew G. Knepley 2543113c68e6SMatthew G. Knepley /*@C 2544113c68e6SMatthew G. Knepley DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face 2545113c68e6SMatthew G. Knepley 2546113c68e6SMatthew G. Knepley Logically collective 2547113c68e6SMatthew G. Knepley 25484165533cSJose E. Roman Input Parameters: 2549113c68e6SMatthew G. Knepley + dm - the DM 2550a5b23f4aSJose E. Roman - minradius - the minimum cell radius 2551113c68e6SMatthew G. Knepley 2552113c68e6SMatthew G. Knepley Level: developer 2553113c68e6SMatthew G. Knepley 2554113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates() 2555113c68e6SMatthew G. Knepley @*/ 2556113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) 2557113c68e6SMatthew G. Knepley { 2558113c68e6SMatthew G. Knepley PetscFunctionBegin; 2559113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2560113c68e6SMatthew G. Knepley ((DM_Plex*) dm->data)->minradius = minradius; 2561113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 2562113c68e6SMatthew G. Knepley } 2563856ac710SMatthew G. Knepley 2564856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 2565856ac710SMatthew G. Knepley { 2566856ac710SMatthew G. Knepley DMLabel ghostLabel; 2567856ac710SMatthew G. Knepley PetscScalar *dx, *grad, **gref; 2568856ac710SMatthew G. Knepley PetscInt dim, cStart, cEnd, c, cEndInterior, maxNumFaces; 2569856ac710SMatthew G. Knepley 2570856ac710SMatthew G. Knepley PetscFunctionBegin; 25719566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 25729566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 25739566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 2574089217ebSMatthew G. Knepley cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior; 25759566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxNumFaces, NULL)); 25769566063dSJacob Faibussowitsch PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces)); 25779566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 25789566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref)); 2579856ac710SMatthew G. Knepley for (c = cStart; c < cEndInterior; c++) { 2580856ac710SMatthew G. Knepley const PetscInt *faces; 2581856ac710SMatthew G. Knepley PetscInt numFaces, usedFaces, f, d; 2582640bce14SSatish Balay PetscFVCellGeom *cg; 2583856ac710SMatthew G. Knepley PetscBool boundary; 2584856ac710SMatthew G. Knepley PetscInt ghost; 2585856ac710SMatthew G. Knepley 2586a79418b7SMatt McGurn // do not attempt to compute a gradient reconstruction stencil in a ghost cell. It will never be used 2587a79418b7SMatt McGurn PetscCall(DMLabelGetValue(ghostLabel, c, &ghost)); 2588a79418b7SMatt McGurn if (ghost >= 0) continue; 2589a79418b7SMatt McGurn 25909566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg)); 25919566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, c, &numFaces)); 25929566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, c, &faces)); 25932c71b3e2SJacob Faibussowitsch PetscCheckFalse(numFaces < dim,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cell %D has only %D faces, not enough for gradient reconstruction", c, numFaces); 2594856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 2595640bce14SSatish Balay PetscFVCellGeom *cg1; 2596856ac710SMatthew G. Knepley PetscFVFaceGeom *fg; 2597856ac710SMatthew G. Knepley const PetscInt *fcells; 2598856ac710SMatthew G. Knepley PetscInt ncell, side; 2599856ac710SMatthew G. Knepley 26009566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost)); 26019566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary)); 2602856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 26039566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, faces[f], &fcells)); 2604856ac710SMatthew G. Knepley side = (c != fcells[0]); /* c is on left=0 or right=1 of face */ 2605856ac710SMatthew G. Knepley ncell = fcells[!side]; /* the neighbor */ 26069566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg)); 26079566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1)); 2608856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d]; 2609856ac710SMatthew G. Knepley gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */ 2610856ac710SMatthew G. Knepley } 261128b400f6SJacob Faibussowitsch PetscCheck(usedFaces,PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?"); 26129566063dSJacob Faibussowitsch PetscCall(PetscFVComputeGradient(fvm, usedFaces, dx, grad)); 2613856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 26149566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost)); 26159566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary)); 2616856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 2617856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d]; 2618856ac710SMatthew G. Knepley ++usedFaces; 2619856ac710SMatthew G. Knepley } 2620856ac710SMatthew G. Knepley } 26219566063dSJacob Faibussowitsch PetscCall(PetscFree3(dx, grad, gref)); 2622856ac710SMatthew G. Knepley PetscFunctionReturn(0); 2623856ac710SMatthew G. Knepley } 2624856ac710SMatthew G. Knepley 2625b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 2626b81db932SToby Isaac { 2627b81db932SToby Isaac DMLabel ghostLabel; 2628b81db932SToby Isaac PetscScalar *dx, *grad, **gref; 2629b81db932SToby Isaac PetscInt dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0; 2630b81db932SToby Isaac PetscSection neighSec; 2631b81db932SToby Isaac PetscInt (*neighbors)[2]; 2632b81db932SToby Isaac PetscInt *counter; 2633b81db932SToby Isaac 2634b81db932SToby Isaac PetscFunctionBegin; 26359566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 26369566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 26379566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 2638485ad865SMatthew G. Knepley if (cEndInterior < 0) cEndInterior = cEnd; 26399566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec)); 26409566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(neighSec,cStart,cEndInterior)); 26419566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 26429566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 2643b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 2644b81db932SToby Isaac const PetscInt *fcells; 2645b81db932SToby Isaac PetscBool boundary; 26465bc680faSToby Isaac PetscInt ghost = -1; 2647b81db932SToby Isaac PetscInt numChildren, numCells, c; 2648b81db932SToby Isaac 26499566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 26509566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, f, &boundary)); 26519566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 2652b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 26539566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &numCells)); 265406348e87SToby Isaac if (numCells == 2) { 26559566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &fcells)); 2656b81db932SToby Isaac for (c = 0; c < 2; c++) { 2657b81db932SToby Isaac PetscInt cell = fcells[c]; 2658b81db932SToby Isaac 2659e6885bbbSToby Isaac if (cell >= cStart && cell < cEndInterior) { 26609566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(neighSec,cell,1)); 2661b81db932SToby Isaac } 2662b81db932SToby Isaac } 2663b81db932SToby Isaac } 266406348e87SToby Isaac } 26659566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(neighSec)); 26669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetMaxDof(neighSec,&maxNumFaces)); 26679566063dSJacob Faibussowitsch PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces)); 2668b81db932SToby Isaac nStart = 0; 26699566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(neighSec,&nEnd)); 26709566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((nEnd-nStart),&neighbors)); 26719566063dSJacob Faibussowitsch PetscCall(PetscCalloc1((cEndInterior-cStart),&counter)); 2672b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 2673b81db932SToby Isaac const PetscInt *fcells; 2674b81db932SToby Isaac PetscBool boundary; 26755bc680faSToby Isaac PetscInt ghost = -1; 2676b81db932SToby Isaac PetscInt numChildren, numCells, c; 2677b81db932SToby Isaac 26789566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 26799566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, f, &boundary)); 26809566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 2681b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 26829566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &numCells)); 268306348e87SToby Isaac if (numCells == 2) { 26849566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &fcells)); 2685b81db932SToby Isaac for (c = 0; c < 2; c++) { 2686b81db932SToby Isaac PetscInt cell = fcells[c], off; 2687b81db932SToby Isaac 2688e6885bbbSToby Isaac if (cell >= cStart && cell < cEndInterior) { 26899566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(neighSec,cell,&off)); 2690b81db932SToby Isaac off += counter[cell - cStart]++; 2691b81db932SToby Isaac neighbors[off][0] = f; 2692b81db932SToby Isaac neighbors[off][1] = fcells[1 - c]; 2693b81db932SToby Isaac } 2694b81db932SToby Isaac } 2695b81db932SToby Isaac } 269606348e87SToby Isaac } 26979566063dSJacob Faibussowitsch PetscCall(PetscFree(counter)); 26989566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref)); 2699b81db932SToby Isaac for (c = cStart; c < cEndInterior; c++) { 2700317218b9SToby Isaac PetscInt numFaces, f, d, off, ghost = -1; 2701640bce14SSatish Balay PetscFVCellGeom *cg; 2702b81db932SToby Isaac 27039566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg)); 27049566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(neighSec, c, &numFaces)); 27059566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(neighSec, c, &off)); 2706a79418b7SMatt McGurn 2707a79418b7SMatt McGurn // do not attempt to compute a gradient reconstruction stencil in a ghost cell. It will never be used 27089566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, c, &ghost)); 2709a79418b7SMatt McGurn if (ghost >= 0) continue; 2710a79418b7SMatt McGurn 2711a79418b7SMatt McGurn PetscCheckFalse(numFaces < dim,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cell %D has only %D faces, not enough for gradient reconstruction", c, numFaces); 2712b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 2713640bce14SSatish Balay PetscFVCellGeom *cg1; 2714b81db932SToby Isaac PetscFVFaceGeom *fg; 2715b81db932SToby Isaac const PetscInt *fcells; 2716b81db932SToby Isaac PetscInt ncell, side, nface; 2717b81db932SToby Isaac 2718b81db932SToby Isaac nface = neighbors[off + f][0]; 2719b81db932SToby Isaac ncell = neighbors[off + f][1]; 27209566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm,nface,&fcells)); 2721b81db932SToby Isaac side = (c != fcells[0]); 27229566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, nface, fgeom, &fg)); 27239566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1)); 2724b81db932SToby Isaac for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d]; 2725b81db932SToby Isaac gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */ 2726b81db932SToby Isaac } 27279566063dSJacob Faibussowitsch PetscCall(PetscFVComputeGradient(fvm, numFaces, dx, grad)); 2728b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 2729b81db932SToby Isaac for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d]; 2730b81db932SToby Isaac } 2731b81db932SToby Isaac } 27329566063dSJacob Faibussowitsch PetscCall(PetscFree3(dx, grad, gref)); 27339566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&neighSec)); 27349566063dSJacob Faibussowitsch PetscCall(PetscFree(neighbors)); 2735b81db932SToby Isaac PetscFunctionReturn(0); 2736b81db932SToby Isaac } 2737b81db932SToby Isaac 2738856ac710SMatthew G. Knepley /*@ 2739856ac710SMatthew G. Knepley DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data 2740856ac710SMatthew G. Knepley 2741d083f849SBarry Smith Collective on dm 2742856ac710SMatthew G. Knepley 27434165533cSJose E. Roman Input Parameters: 2744856ac710SMatthew G. Knepley + dm - The DM 2745856ac710SMatthew G. Knepley . fvm - The PetscFV 27468f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM() 2747856ac710SMatthew G. Knepley 27486b867d5aSJose E. Roman Input/Output Parameter: 27496b867d5aSJose E. Roman . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM(); on output 27506b867d5aSJose E. Roman the geometric factors for gradient calculation are inserted 27516b867d5aSJose E. Roman 27526b867d5aSJose E. Roman Output Parameter: 27536b867d5aSJose E. Roman . dmGrad - The DM describing the layout of gradient data 2754856ac710SMatthew G. Knepley 2755856ac710SMatthew G. Knepley Level: developer 2756856ac710SMatthew G. Knepley 2757856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM() 2758856ac710SMatthew G. Knepley @*/ 2759856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) 2760856ac710SMatthew G. Knepley { 2761856ac710SMatthew G. Knepley DM dmFace, dmCell; 2762856ac710SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 2763b81db932SToby Isaac PetscSection sectionGrad, parentSection; 2764856ac710SMatthew G. Knepley PetscInt dim, pdim, cStart, cEnd, cEndInterior, c; 2765856ac710SMatthew G. Knepley 2766856ac710SMatthew G. Knepley PetscFunctionBegin; 27679566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 27689566063dSJacob Faibussowitsch PetscCall(PetscFVGetNumComponents(fvm, &pdim)); 27699566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 27709566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 2771856ac710SMatthew G. Knepley /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */ 27729566063dSJacob Faibussowitsch PetscCall(VecGetDM(faceGeometry, &dmFace)); 27739566063dSJacob Faibussowitsch PetscCall(VecGetDM(cellGeometry, &dmCell)); 27749566063dSJacob Faibussowitsch PetscCall(VecGetArray(faceGeometry, &fgeom)); 27759566063dSJacob Faibussowitsch PetscCall(VecGetArray(cellGeometry, &cgeom)); 27769566063dSJacob Faibussowitsch PetscCall(DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL)); 2777b81db932SToby Isaac if (!parentSection) { 27789566063dSJacob Faibussowitsch PetscCall(BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom)); 2779b5a3613cSMatthew G. Knepley } else { 27809566063dSJacob Faibussowitsch PetscCall(BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom)); 2781b81db932SToby Isaac } 27829566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(faceGeometry, &fgeom)); 27839566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(cellGeometry, &cgeom)); 2784856ac710SMatthew G. Knepley /* Create storage for gradients */ 27859566063dSJacob Faibussowitsch PetscCall(DMClone(dm, dmGrad)); 27869566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionGrad)); 27879566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionGrad, cStart, cEnd)); 27889566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionGrad, c, pdim*dim)); 27899566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionGrad)); 27909566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(*dmGrad, sectionGrad)); 27919566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionGrad)); 2792856ac710SMatthew G. Knepley PetscFunctionReturn(0); 2793856ac710SMatthew G. Knepley } 2794b27d5b9eSToby Isaac 2795c501906fSMatthew G. Knepley /*@ 2796c501906fSMatthew G. Knepley DMPlexGetDataFVM - Retrieve precomputed cell geometry 2797c501906fSMatthew G. Knepley 2798d083f849SBarry Smith Collective on dm 2799c501906fSMatthew G. Knepley 28004165533cSJose E. Roman Input Parameters: 2801c501906fSMatthew G. Knepley + dm - The DM 28026b867d5aSJose E. Roman - fv - The PetscFV 2803c501906fSMatthew G. Knepley 2804c501906fSMatthew G. Knepley Output Parameters: 2805c501906fSMatthew G. Knepley + cellGeometry - The cell geometry 2806c501906fSMatthew G. Knepley . faceGeometry - The face geometry 28076b867d5aSJose E. Roman - gradDM - The gradient matrices 2808c501906fSMatthew G. Knepley 2809c501906fSMatthew G. Knepley Level: developer 2810c501906fSMatthew G. Knepley 2811c501906fSMatthew G. Knepley .seealso: DMPlexComputeGeometryFVM() 2812c501906fSMatthew G. Knepley @*/ 2813b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM) 2814b27d5b9eSToby Isaac { 2815b27d5b9eSToby Isaac PetscObject cellgeomobj, facegeomobj; 2816b27d5b9eSToby Isaac 2817b27d5b9eSToby Isaac PetscFunctionBegin; 28189566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj)); 2819b27d5b9eSToby Isaac if (!cellgeomobj) { 2820b27d5b9eSToby Isaac Vec cellgeomInt, facegeomInt; 2821b27d5b9eSToby Isaac 28229566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt)); 28239566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt)); 28249566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt)); 28259566063dSJacob Faibussowitsch PetscCall(VecDestroy(&cellgeomInt)); 28269566063dSJacob Faibussowitsch PetscCall(VecDestroy(&facegeomInt)); 28279566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj)); 2828b27d5b9eSToby Isaac } 28299566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj)); 2830b27d5b9eSToby Isaac if (cellgeom) *cellgeom = (Vec) cellgeomobj; 2831b27d5b9eSToby Isaac if (facegeom) *facegeom = (Vec) facegeomobj; 2832b27d5b9eSToby Isaac if (gradDM) { 2833b27d5b9eSToby Isaac PetscObject gradobj; 2834b27d5b9eSToby Isaac PetscBool computeGradients; 2835b27d5b9eSToby Isaac 28369566063dSJacob Faibussowitsch PetscCall(PetscFVGetComputeGradients(fv,&computeGradients)); 2837b27d5b9eSToby Isaac if (!computeGradients) { 2838b27d5b9eSToby Isaac *gradDM = NULL; 2839b27d5b9eSToby Isaac PetscFunctionReturn(0); 2840b27d5b9eSToby Isaac } 28419566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj)); 2842b27d5b9eSToby Isaac if (!gradobj) { 2843b27d5b9eSToby Isaac DM dmGradInt; 2844b27d5b9eSToby Isaac 28459566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt)); 28469566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt)); 28479566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmGradInt)); 28489566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj)); 2849b27d5b9eSToby Isaac } 2850b27d5b9eSToby Isaac *gradDM = (DM) gradobj; 2851b27d5b9eSToby Isaac } 2852b27d5b9eSToby Isaac PetscFunctionReturn(0); 2853b27d5b9eSToby Isaac } 2854d6143a4eSToby Isaac 28559d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess) 28569d150b73SToby Isaac { 28579d150b73SToby Isaac PetscInt l, m; 28589d150b73SToby Isaac 2859cd345991SToby Isaac PetscFunctionBeginHot; 28609d150b73SToby Isaac if (dimC == dimR && dimR <= 3) { 28619d150b73SToby Isaac /* invert Jacobian, multiply */ 28629d150b73SToby Isaac PetscScalar det, idet; 28639d150b73SToby Isaac 28649d150b73SToby Isaac switch (dimR) { 28659d150b73SToby Isaac case 1: 28669d150b73SToby Isaac invJ[0] = 1./ J[0]; 28679d150b73SToby Isaac break; 28689d150b73SToby Isaac case 2: 28699d150b73SToby Isaac det = J[0] * J[3] - J[1] * J[2]; 28709d150b73SToby Isaac idet = 1./det; 28719d150b73SToby Isaac invJ[0] = J[3] * idet; 28729d150b73SToby Isaac invJ[1] = -J[1] * idet; 28739d150b73SToby Isaac invJ[2] = -J[2] * idet; 28749d150b73SToby Isaac invJ[3] = J[0] * idet; 28759d150b73SToby Isaac break; 28769d150b73SToby Isaac case 3: 28779d150b73SToby Isaac { 28789d150b73SToby Isaac invJ[0] = J[4] * J[8] - J[5] * J[7]; 28799d150b73SToby Isaac invJ[1] = J[2] * J[7] - J[1] * J[8]; 28809d150b73SToby Isaac invJ[2] = J[1] * J[5] - J[2] * J[4]; 28819d150b73SToby Isaac det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6]; 28829d150b73SToby Isaac idet = 1./det; 28839d150b73SToby Isaac invJ[0] *= idet; 28849d150b73SToby Isaac invJ[1] *= idet; 28859d150b73SToby Isaac invJ[2] *= idet; 28869d150b73SToby Isaac invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]); 28879d150b73SToby Isaac invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]); 28889d150b73SToby Isaac invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]); 28899d150b73SToby Isaac invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]); 28909d150b73SToby Isaac invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]); 28919d150b73SToby Isaac invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]); 28929d150b73SToby Isaac } 28939d150b73SToby Isaac break; 28949d150b73SToby Isaac } 28959d150b73SToby Isaac for (l = 0; l < dimR; l++) { 28969d150b73SToby Isaac for (m = 0; m < dimC; m++) { 2897c6e120d1SToby Isaac guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m]; 28989d150b73SToby Isaac } 28999d150b73SToby Isaac } 29009d150b73SToby Isaac } else { 29019d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX) 29029d150b73SToby Isaac char transpose = 'C'; 29039d150b73SToby Isaac #else 29049d150b73SToby Isaac char transpose = 'T'; 29059d150b73SToby Isaac #endif 29069d150b73SToby Isaac PetscBLASInt m = dimR; 29079d150b73SToby Isaac PetscBLASInt n = dimC; 29089d150b73SToby Isaac PetscBLASInt one = 1; 29099d150b73SToby Isaac PetscBLASInt worksize = dimR * dimC, info; 29109d150b73SToby Isaac 29119d150b73SToby Isaac for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];} 29129d150b73SToby Isaac 29139d150b73SToby Isaac PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info)); 29142c71b3e2SJacob Faibussowitsch PetscCheckFalse(info != 0,PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS"); 29159d150b73SToby Isaac 2916c6e120d1SToby Isaac for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);} 29179d150b73SToby Isaac } 29189d150b73SToby Isaac PetscFunctionReturn(0); 29199d150b73SToby Isaac } 29209d150b73SToby Isaac 29219d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 29229d150b73SToby Isaac { 2923c0cbe899SToby Isaac PetscInt coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR); 29249d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 29259d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg; 29269d150b73SToby Isaac PetscScalar *J, *invJ, *work; 29279d150b73SToby Isaac 29289d150b73SToby Isaac PetscFunctionBegin; 29299d150b73SToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 29309566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 29312c71b3e2SJacob Faibussowitsch PetscCheckFalse(coordSize < dimC * numV,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize); 29329566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData)); 29339566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J)); 29349d150b73SToby Isaac cellCoords = &cellData[0]; 29359d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 29369d150b73SToby Isaac extJ = &cellData[2 * coordSize]; 29379d150b73SToby Isaac resNeg = &cellData[2 * coordSize + dimR]; 29389d150b73SToby Isaac invJ = &J[dimR * dimC]; 29399d150b73SToby Isaac work = &J[2 * dimR * dimC]; 29409d150b73SToby Isaac if (dimR == 2) { 29419d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 29429d150b73SToby Isaac 29439d150b73SToby Isaac for (i = 0; i < 4; i++) { 29449d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 29459d150b73SToby Isaac 29469d150b73SToby Isaac for (j = 0; j < dimC; j++) { 29479d150b73SToby Isaac cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 29489d150b73SToby Isaac } 29499d150b73SToby Isaac } 29509d150b73SToby Isaac } else if (dimR == 3) { 29519d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 29529d150b73SToby Isaac 29539d150b73SToby Isaac for (i = 0; i < 8; i++) { 29549d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 29559d150b73SToby Isaac 29569d150b73SToby Isaac for (j = 0; j < dimC; j++) { 29579d150b73SToby Isaac cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 29589d150b73SToby Isaac } 29599d150b73SToby Isaac } 29609d150b73SToby Isaac } else { 29619d150b73SToby Isaac for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);} 29629d150b73SToby Isaac } 29639d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 29649d150b73SToby Isaac for (i = 0; i < dimR; i++) { 29659d150b73SToby Isaac PetscReal *swap; 29669d150b73SToby Isaac 29679d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 29689d150b73SToby Isaac for (k = 0; k < dimC; k++) { 29699d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 29709d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 29719d150b73SToby Isaac } 29729d150b73SToby Isaac } 29739d150b73SToby Isaac 29749d150b73SToby Isaac if (i < dimR - 1) { 29759d150b73SToby Isaac swap = cellCoeffs; 29769d150b73SToby Isaac cellCoeffs = cellCoords; 29779d150b73SToby Isaac cellCoords = swap; 29789d150b73SToby Isaac } 29799d150b73SToby Isaac } 29809566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(refCoords,numPoints * dimR)); 29819d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 29829d150b73SToby Isaac for (i = 0; i < maxIts; i++) { 29839d150b73SToby Isaac PetscReal *guess = &refCoords[dimR * j]; 29849d150b73SToby Isaac 29859d150b73SToby Isaac /* compute -residual and Jacobian */ 29869d150b73SToby Isaac for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];} 29879d150b73SToby Isaac for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;} 29889d150b73SToby Isaac for (k = 0; k < numV; k++) { 29899d150b73SToby Isaac PetscReal extCoord = 1.; 29909d150b73SToby Isaac for (l = 0; l < dimR; l++) { 29919d150b73SToby Isaac PetscReal coord = guess[l]; 29929d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 29939d150b73SToby Isaac 29949d150b73SToby Isaac extCoord *= dep * coord + !dep; 29959d150b73SToby Isaac extJ[l] = dep; 29969d150b73SToby Isaac 29979d150b73SToby Isaac for (m = 0; m < dimR; m++) { 29989d150b73SToby Isaac PetscReal coord = guess[m]; 29999d150b73SToby Isaac PetscInt dep = ((k & (1 << m)) >> m) && (m != l); 30009d150b73SToby Isaac PetscReal mult = dep * coord + !dep; 30019d150b73SToby Isaac 30029d150b73SToby Isaac extJ[l] *= mult; 30039d150b73SToby Isaac } 30049d150b73SToby Isaac } 30059d150b73SToby Isaac for (l = 0; l < dimC; l++) { 30069d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 30079d150b73SToby Isaac 30089d150b73SToby Isaac resNeg[l] -= coeff * extCoord; 30099d150b73SToby Isaac for (m = 0; m < dimR; m++) { 30109d150b73SToby Isaac J[dimR * l + m] += coeff * extJ[m]; 30119d150b73SToby Isaac } 30129d150b73SToby Isaac } 30139d150b73SToby Isaac } 301476bd3646SJed Brown if (0 && PetscDefined(USE_DEBUG)) { 30150611203eSToby Isaac PetscReal maxAbs = 0.; 30160611203eSToby Isaac 30170611203eSToby Isaac for (l = 0; l < dimC; l++) { 30180611203eSToby Isaac maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l])); 30190611203eSToby Isaac } 30209566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs)); 30210611203eSToby Isaac } 30229d150b73SToby Isaac 30239566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess)); 30249d150b73SToby Isaac } 30259d150b73SToby Isaac } 30269566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J)); 30279566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData)); 30289566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 30299d150b73SToby Isaac PetscFunctionReturn(0); 30309d150b73SToby Isaac } 30319d150b73SToby Isaac 30329d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 30339d150b73SToby Isaac { 30349d150b73SToby Isaac PetscInt coordSize, i, j, k, l, numV = (1 << dimR); 30359d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 30369d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs; 30379d150b73SToby Isaac 30389d150b73SToby Isaac PetscFunctionBegin; 30399d150b73SToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 30409566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 30412c71b3e2SJacob Faibussowitsch PetscCheckFalse(coordSize < dimC * numV,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize); 30429566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData)); 30439d150b73SToby Isaac cellCoords = &cellData[0]; 30449d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 30459d150b73SToby Isaac if (dimR == 2) { 30469d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 30479d150b73SToby Isaac 30489d150b73SToby Isaac for (i = 0; i < 4; i++) { 30499d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 30509d150b73SToby Isaac 30519d150b73SToby Isaac for (j = 0; j < dimC; j++) { 30529d150b73SToby Isaac cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 30539d150b73SToby Isaac } 30549d150b73SToby Isaac } 30559d150b73SToby Isaac } else if (dimR == 3) { 30569d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 30579d150b73SToby Isaac 30589d150b73SToby Isaac for (i = 0; i < 8; i++) { 30599d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 30609d150b73SToby Isaac 30619d150b73SToby Isaac for (j = 0; j < dimC; j++) { 30629d150b73SToby Isaac cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 30639d150b73SToby Isaac } 30649d150b73SToby Isaac } 30659d150b73SToby Isaac } else { 30669d150b73SToby Isaac for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);} 30679d150b73SToby Isaac } 30689d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 30699d150b73SToby Isaac for (i = 0; i < dimR; i++) { 30709d150b73SToby Isaac PetscReal *swap; 30719d150b73SToby Isaac 30729d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 30739d150b73SToby Isaac for (k = 0; k < dimC; k++) { 30749d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 30759d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 30769d150b73SToby Isaac } 30779d150b73SToby Isaac } 30789d150b73SToby Isaac 30799d150b73SToby Isaac if (i < dimR - 1) { 30809d150b73SToby Isaac swap = cellCoeffs; 30819d150b73SToby Isaac cellCoeffs = cellCoords; 30829d150b73SToby Isaac cellCoords = swap; 30839d150b73SToby Isaac } 30849d150b73SToby Isaac } 30859566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(realCoords,numPoints * dimC)); 30869d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 30879d150b73SToby Isaac const PetscReal *guess = &refCoords[dimR * j]; 30889d150b73SToby Isaac PetscReal *mapped = &realCoords[dimC * j]; 30899d150b73SToby Isaac 30909d150b73SToby Isaac for (k = 0; k < numV; k++) { 30919d150b73SToby Isaac PetscReal extCoord = 1.; 30929d150b73SToby Isaac for (l = 0; l < dimR; l++) { 30939d150b73SToby Isaac PetscReal coord = guess[l]; 30949d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 30959d150b73SToby Isaac 30969d150b73SToby Isaac extCoord *= dep * coord + !dep; 30979d150b73SToby Isaac } 30989d150b73SToby Isaac for (l = 0; l < dimC; l++) { 30999d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 31009d150b73SToby Isaac 31019d150b73SToby Isaac mapped[l] += coeff * extCoord; 31029d150b73SToby Isaac } 31039d150b73SToby Isaac } 31049d150b73SToby Isaac } 31059566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData)); 31069566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 31079d150b73SToby Isaac PetscFunctionReturn(0); 31089d150b73SToby Isaac } 31099d150b73SToby Isaac 31109c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */ 31119c3cf19fSMatthew G. Knepley static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR) 31129d150b73SToby Isaac { 31139c3cf19fSMatthew G. Knepley PetscInt numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize; 3114c6e120d1SToby Isaac PetscScalar *nodes = NULL; 3115c6e120d1SToby Isaac PetscReal *invV, *modes; 3116c6e120d1SToby Isaac PetscReal *B, *D, *resNeg; 3117c6e120d1SToby Isaac PetscScalar *J, *invJ, *work; 31189d150b73SToby Isaac 31199d150b73SToby Isaac PetscFunctionBegin; 31209566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 31219566063dSJacob Faibussowitsch PetscCall(PetscFEGetNumComponents(fe, &numComp)); 31222c71b3e2SJacob Faibussowitsch PetscCheckFalse(numComp != Nc,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc); 31239566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 31249d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 31259566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm,pdim,MPIU_REAL,&modes)); 31269d150b73SToby Isaac invV = fe->invV; 3127012b7cc6SMatthew G. Knepley for (i = 0; i < pdim; ++i) { 3128012b7cc6SMatthew G. Knepley modes[i] = 0.; 3129012b7cc6SMatthew G. Knepley for (j = 0; j < pdim; ++j) { 3130012b7cc6SMatthew G. Knepley modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); 31319d150b73SToby Isaac } 31329d150b73SToby Isaac } 31339566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B)); 31349c3cf19fSMatthew G. Knepley D = &B[pdim*Nc]; 31359c3cf19fSMatthew G. Knepley resNeg = &D[pdim*Nc * dimR]; 31369566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J)); 31379c3cf19fSMatthew G. Knepley invJ = &J[Nc * dimR]; 31389c3cf19fSMatthew G. Knepley work = &invJ[Nc * dimR]; 31399d150b73SToby Isaac for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;} 31409d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 31419b1f03cbSToby 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 */ 31429d150b73SToby Isaac PetscReal *guess = &refCoords[j * dimR]; 31439566063dSJacob Faibussowitsch PetscCall(PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL)); 31449c3cf19fSMatthew G. Knepley for (k = 0; k < Nc; k++) {resNeg[k] = realCoords[j * Nc + k];} 31459c3cf19fSMatthew G. Knepley for (k = 0; k < Nc * dimR; k++) {J[k] = 0.;} 31469c3cf19fSMatthew G. Knepley for (k = 0; k < pdim; k++) { 31479c3cf19fSMatthew G. Knepley for (l = 0; l < Nc; l++) { 3148012b7cc6SMatthew G. Knepley resNeg[l] -= modes[k] * B[k * Nc + l]; 31499d150b73SToby Isaac for (m = 0; m < dimR; m++) { 3150012b7cc6SMatthew G. Knepley J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m]; 31519d150b73SToby Isaac } 31529d150b73SToby Isaac } 31539d150b73SToby Isaac } 315476bd3646SJed Brown if (0 && PetscDefined(USE_DEBUG)) { 31550611203eSToby Isaac PetscReal maxAbs = 0.; 31560611203eSToby Isaac 31579c3cf19fSMatthew G. Knepley for (l = 0; l < Nc; l++) { 31580611203eSToby Isaac maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l])); 31590611203eSToby Isaac } 31609566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs)); 31610611203eSToby Isaac } 31629566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(Nc,dimR,J,invJ,work,resNeg,guess)); 31639d150b73SToby Isaac } 31649d150b73SToby Isaac } 31659566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J)); 31669566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B)); 31679566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes)); 31689566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 31699d150b73SToby Isaac PetscFunctionReturn(0); 31709d150b73SToby Isaac } 31719d150b73SToby Isaac 31729c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */ 31739c3cf19fSMatthew G. Knepley static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR) 31749d150b73SToby Isaac { 31759c3cf19fSMatthew G. Knepley PetscInt numComp, pdim, i, j, k, l, coordSize; 3176c6e120d1SToby Isaac PetscScalar *nodes = NULL; 3177c6e120d1SToby Isaac PetscReal *invV, *modes; 31789d150b73SToby Isaac PetscReal *B; 31799d150b73SToby Isaac 31809d150b73SToby Isaac PetscFunctionBegin; 31819566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 31829566063dSJacob Faibussowitsch PetscCall(PetscFEGetNumComponents(fe, &numComp)); 31832c71b3e2SJacob Faibussowitsch PetscCheckFalse(numComp != Nc,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc); 31849566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 31859d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 31869566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm,pdim,MPIU_REAL,&modes)); 31879d150b73SToby Isaac invV = fe->invV; 3188012b7cc6SMatthew G. Knepley for (i = 0; i < pdim; ++i) { 3189012b7cc6SMatthew G. Knepley modes[i] = 0.; 3190012b7cc6SMatthew G. Knepley for (j = 0; j < pdim; ++j) { 3191012b7cc6SMatthew G. Knepley modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); 31929d150b73SToby Isaac } 31939d150b73SToby Isaac } 31949566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B)); 31959566063dSJacob Faibussowitsch PetscCall(PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL)); 31969c3cf19fSMatthew G. Knepley for (i = 0; i < numPoints * Nc; i++) {realCoords[i] = 0.;} 31979d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 31989c3cf19fSMatthew G. Knepley PetscReal *mapped = &realCoords[j * Nc]; 31999d150b73SToby Isaac 32009c3cf19fSMatthew G. Knepley for (k = 0; k < pdim; k++) { 32019c3cf19fSMatthew G. Knepley for (l = 0; l < Nc; l++) { 320240cf36b3SToby Isaac mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l]; 32039d150b73SToby Isaac } 32049d150b73SToby Isaac } 32059d150b73SToby Isaac } 32069566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B)); 32079566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes)); 32089566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 32099d150b73SToby Isaac PetscFunctionReturn(0); 32109d150b73SToby Isaac } 32119d150b73SToby Isaac 3212d6143a4eSToby Isaac /*@ 3213d6143a4eSToby Isaac DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element 3214d6143a4eSToby Isaac map. This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not 3215d6143a4eSToby Isaac extend uniquely outside the reference cell (e.g, most non-affine maps) 3216d6143a4eSToby Isaac 3217d6143a4eSToby Isaac Not collective 3218d6143a4eSToby Isaac 3219d6143a4eSToby Isaac Input Parameters: 3220d6143a4eSToby Isaac + dm - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or 3221d6143a4eSToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 3222d6143a4eSToby Isaac as a multilinear map for tensor-product elements 3223d6143a4eSToby Isaac . cell - the cell whose map is used. 3224d6143a4eSToby Isaac . numPoints - the number of points to locate 32251b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim()) 3226d6143a4eSToby Isaac 3227d6143a4eSToby Isaac Output Parameters: 3228d6143a4eSToby Isaac . refCoords - (numPoints x dimension) array of reference coordinates (see DMGetDimension()) 32291b266c99SBarry Smith 32301b266c99SBarry Smith Level: intermediate 323173c9229bSMatthew Knepley 323273c9229bSMatthew Knepley .seealso: DMPlexReferenceToCoordinates() 3233d6143a4eSToby Isaac @*/ 3234d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[]) 3235d6143a4eSToby Isaac { 3236485ad865SMatthew G. Knepley PetscInt dimC, dimR, depth, cStart, cEnd, i; 32379d150b73SToby Isaac DM coordDM = NULL; 32389d150b73SToby Isaac Vec coords; 32399d150b73SToby Isaac PetscFE fe = NULL; 32409d150b73SToby Isaac 3241d6143a4eSToby Isaac PetscFunctionBegin; 32429d150b73SToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 32439566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm,&dimR)); 32449566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm,&dimC)); 32459d150b73SToby Isaac if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0); 32469566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm,&depth)); 32479566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm,&coords)); 32489566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm,&coordDM)); 32499d150b73SToby Isaac if (coordDM) { 32509d150b73SToby Isaac PetscInt coordFields; 32519d150b73SToby Isaac 32529566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(coordDM,&coordFields)); 32539d150b73SToby Isaac if (coordFields) { 32549d150b73SToby Isaac PetscClassId id; 32559d150b73SToby Isaac PetscObject disc; 32569d150b73SToby Isaac 32579566063dSJacob Faibussowitsch PetscCall(DMGetField(coordDM,0,NULL,&disc)); 32589566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc,&id)); 32599d150b73SToby Isaac if (id == PETSCFE_CLASSID) { 32609d150b73SToby Isaac fe = (PetscFE) disc; 32619d150b73SToby Isaac } 32629d150b73SToby Isaac } 32639d150b73SToby Isaac } 32649566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 32652c71b3e2SJacob Faibussowitsch PetscCheckFalse(cell < cStart || cell >= cEnd,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd); 32669d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 32679d150b73SToby Isaac PetscInt coneSize; 32689d150b73SToby Isaac PetscBool isSimplex, isTensor; 32699d150b73SToby Isaac 32709566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm,cell,&coneSize)); 32719d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 32729d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 32739d150b73SToby Isaac if (isSimplex) { 32749d150b73SToby Isaac PetscReal detJ, *v0, *J, *invJ; 32759d150b73SToby Isaac 32769566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 32779d150b73SToby Isaac J = &v0[dimC]; 32789d150b73SToby Isaac invJ = &J[dimC * dimC]; 32799566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ)); 32809d150b73SToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */ 3281c330f8ffSToby Isaac const PetscReal x0[3] = {-1.,-1.,-1.}; 3282c330f8ffSToby Isaac 3283c330f8ffSToby Isaac CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]); 32849d150b73SToby Isaac } 32859566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 32869d150b73SToby Isaac } else if (isTensor) { 32879566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR)); 328898921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize); 32899d150b73SToby Isaac } else { 32909566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR)); 32919d150b73SToby Isaac } 32929d150b73SToby Isaac PetscFunctionReturn(0); 32939d150b73SToby Isaac } 32949d150b73SToby Isaac 32959d150b73SToby Isaac /*@ 32969d150b73SToby Isaac DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map. 32979d150b73SToby Isaac 32989d150b73SToby Isaac Not collective 32999d150b73SToby Isaac 33009d150b73SToby Isaac Input Parameters: 33019d150b73SToby Isaac + dm - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or 33029d150b73SToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 33039d150b73SToby Isaac as a multilinear map for tensor-product elements 33049d150b73SToby Isaac . cell - the cell whose map is used. 33059d150b73SToby Isaac . numPoints - the number of points to locate 3306a2b725a8SWilliam Gropp - refCoords - (numPoints x dimension) array of reference coordinates (see DMGetDimension()) 33079d150b73SToby Isaac 33089d150b73SToby Isaac Output Parameters: 33099d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim()) 33101b266c99SBarry Smith 33111b266c99SBarry Smith Level: intermediate 331273c9229bSMatthew Knepley 331373c9229bSMatthew Knepley .seealso: DMPlexCoordinatesToReference() 33149d150b73SToby Isaac @*/ 33159d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[]) 33169d150b73SToby Isaac { 3317485ad865SMatthew G. Knepley PetscInt dimC, dimR, depth, cStart, cEnd, i; 33189d150b73SToby Isaac DM coordDM = NULL; 33199d150b73SToby Isaac Vec coords; 33209d150b73SToby Isaac PetscFE fe = NULL; 33219d150b73SToby Isaac 33229d150b73SToby Isaac PetscFunctionBegin; 33239d150b73SToby Isaac PetscValidHeaderSpecific(dm,DM_CLASSID,1); 33249566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm,&dimR)); 33259566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm,&dimC)); 33269d150b73SToby Isaac if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0); 33279566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm,&depth)); 33289566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm,&coords)); 33299566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm,&coordDM)); 33309d150b73SToby Isaac if (coordDM) { 33319d150b73SToby Isaac PetscInt coordFields; 33329d150b73SToby Isaac 33339566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(coordDM,&coordFields)); 33349d150b73SToby Isaac if (coordFields) { 33359d150b73SToby Isaac PetscClassId id; 33369d150b73SToby Isaac PetscObject disc; 33379d150b73SToby Isaac 33389566063dSJacob Faibussowitsch PetscCall(DMGetField(coordDM,0,NULL,&disc)); 33399566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc,&id)); 33409d150b73SToby Isaac if (id == PETSCFE_CLASSID) { 33419d150b73SToby Isaac fe = (PetscFE) disc; 33429d150b73SToby Isaac } 33439d150b73SToby Isaac } 33449d150b73SToby Isaac } 33459566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 33462c71b3e2SJacob Faibussowitsch PetscCheckFalse(cell < cStart || cell >= cEnd,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd); 33479d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 33489d150b73SToby Isaac PetscInt coneSize; 33499d150b73SToby Isaac PetscBool isSimplex, isTensor; 33509d150b73SToby Isaac 33519566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm,cell,&coneSize)); 33529d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 33539d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 33549d150b73SToby Isaac if (isSimplex) { 33559d150b73SToby Isaac PetscReal detJ, *v0, *J; 33569d150b73SToby Isaac 33579566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 33589d150b73SToby Isaac J = &v0[dimC]; 33599566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ)); 3360c330f8ffSToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */ 3361c330f8ffSToby Isaac const PetscReal xi0[3] = {-1.,-1.,-1.}; 3362c330f8ffSToby Isaac 3363c330f8ffSToby Isaac CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]); 33649d150b73SToby Isaac } 33659566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 33669d150b73SToby Isaac } else if (isTensor) { 33679566063dSJacob Faibussowitsch PetscCall(DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR)); 336898921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize); 33699d150b73SToby Isaac } else { 33709566063dSJacob Faibussowitsch PetscCall(DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR)); 33719d150b73SToby Isaac } 3372d6143a4eSToby Isaac PetscFunctionReturn(0); 3373d6143a4eSToby Isaac } 33740139fca9SMatthew G. Knepley 33750139fca9SMatthew G. Knepley /*@C 33760139fca9SMatthew G. Knepley DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates. 33770139fca9SMatthew G. Knepley 33780139fca9SMatthew G. Knepley Not collective 33790139fca9SMatthew G. Knepley 33800139fca9SMatthew G. Knepley Input Parameters: 33810139fca9SMatthew G. Knepley + dm - The DM 33820139fca9SMatthew G. Knepley . time - The time 33830139fca9SMatthew G. Knepley - func - The function transforming current coordinates to new coordaintes 33840139fca9SMatthew G. Knepley 33850139fca9SMatthew G. Knepley Calling sequence of func: 33860139fca9SMatthew G. Knepley $ func(PetscInt dim, PetscInt Nf, PetscInt NfAux, 33870139fca9SMatthew G. Knepley $ const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 33880139fca9SMatthew G. Knepley $ const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 33890139fca9SMatthew G. Knepley $ PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]); 33900139fca9SMatthew G. Knepley 33910139fca9SMatthew G. Knepley + dim - The spatial dimension 33920139fca9SMatthew G. Knepley . Nf - The number of input fields (here 1) 33930139fca9SMatthew G. Knepley . NfAux - The number of input auxiliary fields 33940139fca9SMatthew G. Knepley . uOff - The offset of the coordinates in u[] (here 0) 33950139fca9SMatthew G. Knepley . uOff_x - The offset of the coordinates in u_x[] (here 0) 33960139fca9SMatthew G. Knepley . u - The coordinate values at this point in space 33970139fca9SMatthew G. Knepley . u_t - The coordinate time derivative at this point in space (here NULL) 33980139fca9SMatthew G. Knepley . u_x - The coordinate derivatives at this point in space 33990139fca9SMatthew G. Knepley . aOff - The offset of each auxiliary field in u[] 34000139fca9SMatthew G. Knepley . aOff_x - The offset of each auxiliary field in u_x[] 34010139fca9SMatthew G. Knepley . a - The auxiliary field values at this point in space 34020139fca9SMatthew G. Knepley . a_t - The auxiliary field time derivative at this point in space (or NULL) 34030139fca9SMatthew G. Knepley . a_x - The auxiliary field derivatives at this point in space 34040139fca9SMatthew G. Knepley . t - The current time 34050139fca9SMatthew G. Knepley . x - The coordinates of this point (here not used) 34060139fca9SMatthew G. Knepley . numConstants - The number of constants 34070139fca9SMatthew G. Knepley . constants - The value of each constant 34080139fca9SMatthew G. Knepley - f - The new coordinates at this point in space 34090139fca9SMatthew G. Knepley 34100139fca9SMatthew G. Knepley Level: intermediate 34110139fca9SMatthew G. Knepley 34120139fca9SMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMProjectFieldLocal(), DMProjectFieldLabelLocal() 34130139fca9SMatthew G. Knepley @*/ 34140139fca9SMatthew G. Knepley PetscErrorCode DMPlexRemapGeometry(DM dm, PetscReal time, 34150139fca9SMatthew G. Knepley void (*func)(PetscInt, PetscInt, PetscInt, 34160139fca9SMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 34170139fca9SMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 34180139fca9SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) 34190139fca9SMatthew G. Knepley { 34200139fca9SMatthew G. Knepley DM cdm; 34218bf1a49fSMatthew G. Knepley DMField cf; 34220139fca9SMatthew G. Knepley Vec lCoords, tmpCoords; 34230139fca9SMatthew G. Knepley 34240139fca9SMatthew G. Knepley PetscFunctionBegin; 34259566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 34269566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &lCoords)); 34279566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(cdm, &tmpCoords)); 34289566063dSJacob Faibussowitsch PetscCall(VecCopy(lCoords, tmpCoords)); 34298bf1a49fSMatthew G. Knepley /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */ 34309566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateField(dm, &cf)); 34318bf1a49fSMatthew G. Knepley cdm->coordinateField = cf; 34329566063dSJacob Faibussowitsch PetscCall(DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords)); 34338bf1a49fSMatthew G. Knepley cdm->coordinateField = NULL; 34349566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(cdm, &tmpCoords)); 34359566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, lCoords)); 34360139fca9SMatthew G. Knepley PetscFunctionReturn(0); 34370139fca9SMatthew G. Knepley } 34380139fca9SMatthew G. Knepley 34390139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z, 34400139fca9SMatthew G. Knepley / 1 0 m_0 \ 34410139fca9SMatthew G. Knepley | 0 1 m_1 | 34420139fca9SMatthew G. Knepley \ 0 0 1 / 34430139fca9SMatthew G. Knepley */ 34440139fca9SMatthew G. Knepley static void f0_shear(PetscInt dim, PetscInt Nf, PetscInt NfAux, 34450139fca9SMatthew G. Knepley const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 34460139fca9SMatthew G. Knepley const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 34470139fca9SMatthew G. Knepley PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar coords[]) 34480139fca9SMatthew G. Knepley { 34490139fca9SMatthew G. Knepley const PetscInt Nc = uOff[1]-uOff[0]; 3450c1f1bd54SMatthew G. Knepley const PetscInt ax = (PetscInt) PetscRealPart(constants[0]); 34510139fca9SMatthew G. Knepley PetscInt c; 34520139fca9SMatthew G. Knepley 34530139fca9SMatthew G. Knepley for (c = 0; c < Nc; ++c) { 34540139fca9SMatthew G. Knepley coords[c] = u[c] + constants[c+1]*u[ax]; 34550139fca9SMatthew G. Knepley } 34560139fca9SMatthew G. Knepley } 34570139fca9SMatthew G. Knepley 34580139fca9SMatthew G. Knepley /*@C 34590139fca9SMatthew G. Knepley DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates. 34600139fca9SMatthew G. Knepley 34610139fca9SMatthew G. Knepley Not collective 34620139fca9SMatthew G. Knepley 34630139fca9SMatthew G. Knepley Input Parameters: 34640139fca9SMatthew G. Knepley + dm - The DM 34653ee9839eSMatthew G. Knepley . direction - The shear coordinate direction, e.g. 0 is the x-axis 34660139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction 34670139fca9SMatthew G. Knepley 34680139fca9SMatthew G. Knepley Level: intermediate 34690139fca9SMatthew G. Knepley 34700139fca9SMatthew G. Knepley .seealso: DMPlexRemapGeometry() 34710139fca9SMatthew G. Knepley @*/ 34723ee9839eSMatthew G. Knepley PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[]) 34730139fca9SMatthew G. Knepley { 34740139fca9SMatthew G. Knepley DM cdm; 34750139fca9SMatthew G. Knepley PetscDS cds; 34760139fca9SMatthew G. Knepley PetscObject obj; 34770139fca9SMatthew G. Knepley PetscClassId id; 34780139fca9SMatthew G. Knepley PetscScalar *moduli; 34793ee9839eSMatthew G. Knepley const PetscInt dir = (PetscInt) direction; 34800139fca9SMatthew G. Knepley PetscInt dE, d, e; 34810139fca9SMatthew G. Knepley 34820139fca9SMatthew G. Knepley PetscFunctionBegin; 34839566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 34849566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dE)); 34859566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(dE+1, &moduli)); 34860139fca9SMatthew G. Knepley moduli[0] = dir; 3487cdaaecf7SMatthew G. Knepley for (d = 0, e = 0; d < dE; ++d) moduli[d+1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0); 34889566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &cds)); 34899566063dSJacob Faibussowitsch PetscCall(PetscDSGetDiscretization(cds, 0, &obj)); 34909566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(obj, &id)); 34910139fca9SMatthew G. Knepley if (id != PETSCFE_CLASSID) { 34920139fca9SMatthew G. Knepley Vec lCoords; 34930139fca9SMatthew G. Knepley PetscSection cSection; 34940139fca9SMatthew G. Knepley PetscScalar *coords; 34950139fca9SMatthew G. Knepley PetscInt vStart, vEnd, v; 34960139fca9SMatthew G. Knepley 34979566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 34989566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &cSection)); 34999566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &lCoords)); 35009566063dSJacob Faibussowitsch PetscCall(VecGetArray(lCoords, &coords)); 35010139fca9SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 35020139fca9SMatthew G. Knepley PetscReal ds; 35030139fca9SMatthew G. Knepley PetscInt off, c; 35040139fca9SMatthew G. Knepley 35059566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cSection, v, &off)); 35060139fca9SMatthew G. Knepley ds = PetscRealPart(coords[off+dir]); 35070139fca9SMatthew G. Knepley for (c = 0; c < dE; ++c) coords[off+c] += moduli[c]*ds; 35080139fca9SMatthew G. Knepley } 35099566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(lCoords, &coords)); 35100139fca9SMatthew G. Knepley } else { 35119566063dSJacob Faibussowitsch PetscCall(PetscDSSetConstants(cds, dE+1, moduli)); 35129566063dSJacob Faibussowitsch PetscCall(DMPlexRemapGeometry(dm, 0.0, f0_shear)); 35130139fca9SMatthew G. Knepley } 35149566063dSJacob Faibussowitsch PetscCall(PetscFree(moduli)); 35150139fca9SMatthew G. Knepley PetscFunctionReturn(0); 35160139fca9SMatthew G. Knepley } 3517