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 34db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMGetCoordinatesLocal()` 353985bb02SVaclav Hapla @*/ 369371c9d4SSatish Balay PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points) { 3737900f7dSMatthew G. Knepley PetscInt c, cdim, i, j, o, p, vStart, vEnd; 38d3e1f4ccSVaclav Hapla PetscInt npoints; 39d3e1f4ccSVaclav Hapla const PetscScalar *coord; 403985bb02SVaclav Hapla Vec allCoordsVec; 413985bb02SVaclav Hapla const PetscScalar *allCoords; 42d3e1f4ccSVaclav Hapla PetscInt *dagPoints; 433985bb02SVaclav Hapla 443985bb02SVaclav Hapla PetscFunctionBegin; 453985bb02SVaclav Hapla if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON; 469566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 47d3e1f4ccSVaclav Hapla { 48d3e1f4ccSVaclav Hapla PetscInt n; 49d3e1f4ccSVaclav Hapla 509566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(coordinates, &n)); 5163a3b9bcSJacob Faibussowitsch PetscCheck(n % cdim == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Given coordinates Vec has local length %" PetscInt_FMT " not divisible by coordinate dimension %" PetscInt_FMT " of given DM", n, cdim); 52d3e1f4ccSVaclav Hapla npoints = n / cdim; 53d3e1f4ccSVaclav Hapla } 549566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &allCoordsVec)); 559566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(allCoordsVec, &allCoords)); 569566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coord)); 579566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 5876bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 59335ef845SVaclav Hapla /* check coordinate section is consistent with DM dimension */ 60335ef845SVaclav Hapla PetscSection cs; 61335ef845SVaclav Hapla PetscInt ndof; 62335ef845SVaclav Hapla 639566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &cs)); 643985bb02SVaclav Hapla for (p = vStart; p < vEnd; p++) { 659566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cs, p, &ndof)); 6663a3b9bcSJacob Faibussowitsch PetscCheck(ndof == cdim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %" PetscInt_FMT ": ndof = %" PetscInt_FMT " != %" PetscInt_FMT " = cdim", p, ndof, cdim); 67335ef845SVaclav Hapla } 68335ef845SVaclav Hapla } 699566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npoints, &dagPoints)); 70eca9f518SVaclav Hapla if (eps == 0.0) { 7137900f7dSMatthew G. Knepley for (i = 0, j = 0; i < npoints; i++, j += cdim) { 72eca9f518SVaclav Hapla dagPoints[i] = -1; 7337900f7dSMatthew G. Knepley for (p = vStart, o = 0; p < vEnd; p++, o += cdim) { 7437900f7dSMatthew G. Knepley for (c = 0; c < cdim; c++) { 75d3e1f4ccSVaclav Hapla if (coord[j + c] != allCoords[o + c]) break; 76eca9f518SVaclav Hapla } 7737900f7dSMatthew G. Knepley if (c == cdim) { 78eca9f518SVaclav Hapla dagPoints[i] = p; 79eca9f518SVaclav Hapla break; 80eca9f518SVaclav Hapla } 81eca9f518SVaclav Hapla } 82eca9f518SVaclav Hapla } 83d3e1f4ccSVaclav Hapla } else { 8437900f7dSMatthew G. Knepley for (i = 0, j = 0; i < npoints; i++, j += cdim) { 85d3e1f4ccSVaclav Hapla PetscReal norm; 86d3e1f4ccSVaclav Hapla 87335ef845SVaclav Hapla dagPoints[i] = -1; 8837900f7dSMatthew G. Knepley for (p = vStart, o = 0; p < vEnd; p++, o += cdim) { 893985bb02SVaclav Hapla norm = 0.0; 909371c9d4SSatish Balay for (c = 0; c < cdim; c++) { norm += PetscRealPart(PetscSqr(coord[j + c] - allCoords[o + c])); } 913985bb02SVaclav Hapla norm = PetscSqrtReal(norm); 923985bb02SVaclav Hapla if (norm <= eps) { 933985bb02SVaclav Hapla dagPoints[i] = p; 943985bb02SVaclav Hapla break; 953985bb02SVaclav Hapla } 963985bb02SVaclav Hapla } 973985bb02SVaclav Hapla } 98d3e1f4ccSVaclav Hapla } 999566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(allCoordsVec, &allCoords)); 1009566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coord)); 1019566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points)); 1023985bb02SVaclav Hapla PetscFunctionReturn(0); 1033985bb02SVaclav Hapla } 1043985bb02SVaclav Hapla 1059371c9d4SSatish Balay static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection) { 106fea14342SMatthew G. Knepley const PetscReal p0_x = segmentA[0 * 2 + 0]; 107fea14342SMatthew G. Knepley const PetscReal p0_y = segmentA[0 * 2 + 1]; 108fea14342SMatthew G. Knepley const PetscReal p1_x = segmentA[1 * 2 + 0]; 109fea14342SMatthew G. Knepley const PetscReal p1_y = segmentA[1 * 2 + 1]; 110fea14342SMatthew G. Knepley const PetscReal p2_x = segmentB[0 * 2 + 0]; 111fea14342SMatthew G. Knepley const PetscReal p2_y = segmentB[0 * 2 + 1]; 112fea14342SMatthew G. Knepley const PetscReal p3_x = segmentB[1 * 2 + 0]; 113fea14342SMatthew G. Knepley const PetscReal p3_y = segmentB[1 * 2 + 1]; 114fea14342SMatthew G. Knepley const PetscReal s1_x = p1_x - p0_x; 115fea14342SMatthew G. Knepley const PetscReal s1_y = p1_y - p0_y; 116fea14342SMatthew G. Knepley const PetscReal s2_x = p3_x - p2_x; 117fea14342SMatthew G. Knepley const PetscReal s2_y = p3_y - p2_y; 118fea14342SMatthew G. Knepley const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y); 119fea14342SMatthew G. Knepley 120fea14342SMatthew G. Knepley PetscFunctionBegin; 121fea14342SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 122fea14342SMatthew G. Knepley /* Non-parallel lines */ 123fea14342SMatthew G. Knepley if (denom != 0.0) { 124fea14342SMatthew G. Knepley const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom; 125fea14342SMatthew G. Knepley const PetscReal t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom; 126fea14342SMatthew G. Knepley 127fea14342SMatthew G. Knepley if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { 128fea14342SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 129fea14342SMatthew G. Knepley if (intersection) { 130fea14342SMatthew G. Knepley intersection[0] = p0_x + (t * s1_x); 131fea14342SMatthew G. Knepley intersection[1] = p0_y + (t * s1_y); 132fea14342SMatthew G. Knepley } 133fea14342SMatthew G. Knepley } 134fea14342SMatthew G. Knepley } 135fea14342SMatthew G. Knepley PetscFunctionReturn(0); 136fea14342SMatthew G. Knepley } 137fea14342SMatthew G. Knepley 138ddce0771SMatthew G. Knepley /* The plane is segmentB x segmentC: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */ 1399371c9d4SSatish Balay static PetscErrorCode DMPlexGetLinePlaneIntersection_3D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], const PetscReal segmentC[], PetscReal intersection[], PetscBool *hasIntersection) { 140ddce0771SMatthew G. Knepley const PetscReal p0_x = segmentA[0 * 3 + 0]; 141ddce0771SMatthew G. Knepley const PetscReal p0_y = segmentA[0 * 3 + 1]; 142ddce0771SMatthew G. Knepley const PetscReal p0_z = segmentA[0 * 3 + 2]; 143ddce0771SMatthew G. Knepley const PetscReal p1_x = segmentA[1 * 3 + 0]; 144ddce0771SMatthew G. Knepley const PetscReal p1_y = segmentA[1 * 3 + 1]; 145ddce0771SMatthew G. Knepley const PetscReal p1_z = segmentA[1 * 3 + 2]; 146ddce0771SMatthew G. Knepley const PetscReal q0_x = segmentB[0 * 3 + 0]; 147ddce0771SMatthew G. Knepley const PetscReal q0_y = segmentB[0 * 3 + 1]; 148ddce0771SMatthew G. Knepley const PetscReal q0_z = segmentB[0 * 3 + 2]; 149ddce0771SMatthew G. Knepley const PetscReal q1_x = segmentB[1 * 3 + 0]; 150ddce0771SMatthew G. Knepley const PetscReal q1_y = segmentB[1 * 3 + 1]; 151ddce0771SMatthew G. Knepley const PetscReal q1_z = segmentB[1 * 3 + 2]; 152ddce0771SMatthew G. Knepley const PetscReal r0_x = segmentC[0 * 3 + 0]; 153ddce0771SMatthew G. Knepley const PetscReal r0_y = segmentC[0 * 3 + 1]; 154ddce0771SMatthew G. Knepley const PetscReal r0_z = segmentC[0 * 3 + 2]; 155ddce0771SMatthew G. Knepley const PetscReal r1_x = segmentC[1 * 3 + 0]; 156ddce0771SMatthew G. Knepley const PetscReal r1_y = segmentC[1 * 3 + 1]; 157ddce0771SMatthew G. Knepley const PetscReal r1_z = segmentC[1 * 3 + 2]; 158ddce0771SMatthew G. Knepley const PetscReal s0_x = p1_x - p0_x; 159ddce0771SMatthew G. Knepley const PetscReal s0_y = p1_y - p0_y; 160ddce0771SMatthew G. Knepley const PetscReal s0_z = p1_z - p0_z; 161ddce0771SMatthew G. Knepley const PetscReal s1_x = q1_x - q0_x; 162ddce0771SMatthew G. Knepley const PetscReal s1_y = q1_y - q0_y; 163ddce0771SMatthew G. Knepley const PetscReal s1_z = q1_z - q0_z; 164ddce0771SMatthew G. Knepley const PetscReal s2_x = r1_x - r0_x; 165ddce0771SMatthew G. Knepley const PetscReal s2_y = r1_y - r0_y; 166ddce0771SMatthew G. Knepley const PetscReal s2_z = r1_z - r0_z; 167ddce0771SMatthew G. Knepley const PetscReal s3_x = s1_y * s2_z - s1_z * s2_y; /* s1 x s2 */ 168ddce0771SMatthew G. Knepley const PetscReal s3_y = s1_z * s2_x - s1_x * s2_z; 169ddce0771SMatthew G. Knepley const PetscReal s3_z = s1_x * s2_y - s1_y * s2_x; 170ddce0771SMatthew G. Knepley const PetscReal s4_x = s0_y * s2_z - s0_z * s2_y; /* s0 x s2 */ 171ddce0771SMatthew G. Knepley const PetscReal s4_y = s0_z * s2_x - s0_x * s2_z; 172ddce0771SMatthew G. Knepley const PetscReal s4_z = s0_x * s2_y - s0_y * s2_x; 173ddce0771SMatthew G. Knepley const PetscReal s5_x = s1_y * s0_z - s1_z * s0_y; /* s1 x s0 */ 174ddce0771SMatthew G. Knepley const PetscReal s5_y = s1_z * s0_x - s1_x * s0_z; 175ddce0771SMatthew G. Knepley const PetscReal s5_z = s1_x * s0_y - s1_y * s0_x; 176ddce0771SMatthew G. Knepley const PetscReal denom = -(s0_x * s3_x + s0_y * s3_y + s0_z * s3_z); /* -s0 . (s1 x s2) */ 177ddce0771SMatthew G. Knepley 178ddce0771SMatthew G. Knepley PetscFunctionBegin; 179ddce0771SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 180ddce0771SMatthew G. Knepley /* Line not parallel to plane */ 181ddce0771SMatthew G. Knepley if (denom != 0.0) { 182ddce0771SMatthew G. Knepley const PetscReal t = (s3_x * (p0_x - q0_x) + s3_y * (p0_y - q0_y) + s3_z * (p0_z - q0_z)) / denom; 183ddce0771SMatthew G. Knepley const PetscReal u = (s4_x * (p0_x - q0_x) + s4_y * (p0_y - q0_y) + s4_z * (p0_z - q0_z)) / denom; 184ddce0771SMatthew G. Knepley const PetscReal v = (s5_x * (p0_x - q0_x) + s5_y * (p0_y - q0_y) + s5_z * (p0_z - q0_z)) / denom; 185ddce0771SMatthew G. Knepley 186ddce0771SMatthew G. Knepley if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && v >= 0 && v <= 1) { 187ddce0771SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 188ddce0771SMatthew G. Knepley if (intersection) { 189ddce0771SMatthew G. Knepley intersection[0] = p0_x + (t * s0_x); 190ddce0771SMatthew G. Knepley intersection[1] = p0_y + (t * s0_y); 191ddce0771SMatthew G. Knepley intersection[2] = p0_z + (t * s0_z); 192ddce0771SMatthew G. Knepley } 193ddce0771SMatthew G. Knepley } 194ddce0771SMatthew G. Knepley } 195ddce0771SMatthew G. Knepley PetscFunctionReturn(0); 196ddce0771SMatthew G. Knepley } 197ddce0771SMatthew G. Knepley 1989371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) { 19914bbb9f0SLawrence Mitchell const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 20014bbb9f0SLawrence Mitchell const PetscReal x = PetscRealPart(point[0]); 20114bbb9f0SLawrence Mitchell PetscReal v0, J, invJ, detJ; 20214bbb9f0SLawrence Mitchell PetscReal xi; 20314bbb9f0SLawrence Mitchell 20414bbb9f0SLawrence Mitchell PetscFunctionBegin; 2059566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ)); 20614bbb9f0SLawrence Mitchell xi = invJ * (x - v0); 20714bbb9f0SLawrence Mitchell 20814bbb9f0SLawrence Mitchell if ((xi >= -eps) && (xi <= 2. + eps)) *cell = c; 20914bbb9f0SLawrence Mitchell else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 21014bbb9f0SLawrence Mitchell PetscFunctionReturn(0); 21114bbb9f0SLawrence Mitchell } 21214bbb9f0SLawrence Mitchell 2139371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) { 214ccd2543fSMatthew G Knepley const PetscInt embedDim = 2; 215f5ebc837SMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 216ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 217ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 218ccd2543fSMatthew G Knepley PetscReal v0[2], J[4], invJ[4], detJ; 219ccd2543fSMatthew G Knepley PetscReal xi, eta; 220ccd2543fSMatthew G Knepley 221ccd2543fSMatthew G Knepley PetscFunctionBegin; 2229566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 223ccd2543fSMatthew G Knepley xi = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]); 224ccd2543fSMatthew G Knepley eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]); 225ccd2543fSMatthew G Knepley 226f5ebc837SMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0 + eps)) *cell = c; 227c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 228ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 229ccd2543fSMatthew G Knepley } 230ccd2543fSMatthew G Knepley 2319371c9d4SSatish Balay static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[]) { 23262a38674SMatthew G. Knepley const PetscInt embedDim = 2; 23362a38674SMatthew G. Knepley PetscReal x = PetscRealPart(point[0]); 23462a38674SMatthew G. Knepley PetscReal y = PetscRealPart(point[1]); 23562a38674SMatthew G. Knepley PetscReal v0[2], J[4], invJ[4], detJ; 23662a38674SMatthew G. Knepley PetscReal xi, eta, r; 23762a38674SMatthew G. Knepley 23862a38674SMatthew G. Knepley PetscFunctionBegin; 2399566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 24062a38674SMatthew G. Knepley xi = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]); 24162a38674SMatthew G. Knepley eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]); 24262a38674SMatthew G. Knepley 24362a38674SMatthew G. Knepley xi = PetscMax(xi, 0.0); 24462a38674SMatthew G. Knepley eta = PetscMax(eta, 0.0); 24562a38674SMatthew G. Knepley if (xi + eta > 2.0) { 24662a38674SMatthew G. Knepley r = (xi + eta) / 2.0; 24762a38674SMatthew G. Knepley xi /= r; 24862a38674SMatthew G. Knepley eta /= r; 24962a38674SMatthew G. Knepley } 25062a38674SMatthew G. Knepley cpoint[0] = J[0 * embedDim + 0] * xi + J[0 * embedDim + 1] * eta + v0[0]; 25162a38674SMatthew G. Knepley cpoint[1] = J[1 * embedDim + 0] * xi + J[1 * embedDim + 1] * eta + v0[1]; 25262a38674SMatthew G. Knepley PetscFunctionReturn(0); 25362a38674SMatthew G. Knepley } 25462a38674SMatthew G. Knepley 2559371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) { 256ccd2543fSMatthew G Knepley PetscSection coordSection; 257ccd2543fSMatthew G Knepley Vec coordsLocal; 258a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 259ccd2543fSMatthew G Knepley const PetscInt faces[8] = {0, 1, 1, 2, 2, 3, 3, 0}; 260ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 261ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 262ccd2543fSMatthew G Knepley PetscInt crossings = 0, f; 263ccd2543fSMatthew G Knepley 264ccd2543fSMatthew G Knepley PetscFunctionBegin; 2659566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal)); 2669566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 2679566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords)); 268ccd2543fSMatthew G Knepley for (f = 0; f < 4; ++f) { 269ccd2543fSMatthew G Knepley PetscReal x_i = PetscRealPart(coords[faces[2 * f + 0] * 2 + 0]); 270ccd2543fSMatthew G Knepley PetscReal y_i = PetscRealPart(coords[faces[2 * f + 0] * 2 + 1]); 271ccd2543fSMatthew G Knepley PetscReal x_j = PetscRealPart(coords[faces[2 * f + 1] * 2 + 0]); 272ccd2543fSMatthew G Knepley PetscReal y_j = PetscRealPart(coords[faces[2 * f + 1] * 2 + 1]); 273ccd2543fSMatthew G Knepley PetscReal slope = (y_j - y_i) / (x_j - x_i); 274ccd2543fSMatthew G Knepley PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE; 275ccd2543fSMatthew G Knepley PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE; 276ccd2543fSMatthew G Knepley PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE; 277ccd2543fSMatthew G Knepley if ((cond1 || cond2) && above) ++crossings; 278ccd2543fSMatthew G Knepley } 279ccd2543fSMatthew G Knepley if (crossings % 2) *cell = c; 280c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 2819566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords)); 282ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 283ccd2543fSMatthew G Knepley } 284ccd2543fSMatthew G Knepley 2859371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) { 286ccd2543fSMatthew G Knepley const PetscInt embedDim = 3; 28737900f7dSMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 288ccd2543fSMatthew G Knepley PetscReal v0[3], J[9], invJ[9], detJ; 289ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 290ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 291ccd2543fSMatthew G Knepley PetscReal z = PetscRealPart(point[2]); 292ccd2543fSMatthew G Knepley PetscReal xi, eta, zeta; 293ccd2543fSMatthew G Knepley 294ccd2543fSMatthew G Knepley PetscFunctionBegin; 2959566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 296ccd2543fSMatthew G Knepley xi = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]) + invJ[0 * embedDim + 2] * (z - v0[2]); 297ccd2543fSMatthew G Knepley eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]) + invJ[1 * embedDim + 2] * (z - v0[2]); 298ccd2543fSMatthew G Knepley zeta = invJ[2 * embedDim + 0] * (x - v0[0]) + invJ[2 * embedDim + 1] * (y - v0[1]) + invJ[2 * embedDim + 2] * (z - v0[2]); 299ccd2543fSMatthew G Knepley 30037900f7dSMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0 + eps)) *cell = c; 301c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 302ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 303ccd2543fSMatthew G Knepley } 304ccd2543fSMatthew G Knepley 3059371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) { 306ccd2543fSMatthew G Knepley PetscSection coordSection; 307ccd2543fSMatthew G Knepley Vec coordsLocal; 308872a9804SMatthew G. Knepley PetscScalar *coords = NULL; 3099371c9d4SSatish Balay const PetscInt faces[24] = {0, 3, 2, 1, 5, 4, 7, 6, 3, 0, 4, 5, 1, 2, 6, 7, 3, 5, 6, 2, 0, 1, 7, 4}; 310ccd2543fSMatthew G Knepley PetscBool found = PETSC_TRUE; 311ccd2543fSMatthew G Knepley PetscInt f; 312ccd2543fSMatthew G Knepley 313ccd2543fSMatthew G Knepley PetscFunctionBegin; 3149566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal)); 3159566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 3169566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords)); 317ccd2543fSMatthew G Knepley for (f = 0; f < 6; ++f) { 318ccd2543fSMatthew G Knepley /* Check the point is under plane */ 319ccd2543fSMatthew G Knepley /* Get face normal */ 320ccd2543fSMatthew G Knepley PetscReal v_i[3]; 321ccd2543fSMatthew G Knepley PetscReal v_j[3]; 322ccd2543fSMatthew G Knepley PetscReal normal[3]; 323ccd2543fSMatthew G Knepley PetscReal pp[3]; 324ccd2543fSMatthew G Knepley PetscReal dot; 325ccd2543fSMatthew G Knepley 326ccd2543fSMatthew G Knepley v_i[0] = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]); 327ccd2543fSMatthew G Knepley v_i[1] = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]); 328ccd2543fSMatthew G Knepley v_i[2] = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]); 329ccd2543fSMatthew G Knepley v_j[0] = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]); 330ccd2543fSMatthew G Knepley v_j[1] = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]); 331ccd2543fSMatthew G Knepley v_j[2] = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]); 332ccd2543fSMatthew G Knepley normal[0] = v_i[1] * v_j[2] - v_i[2] * v_j[1]; 333ccd2543fSMatthew G Knepley normal[1] = v_i[2] * v_j[0] - v_i[0] * v_j[2]; 334ccd2543fSMatthew G Knepley normal[2] = v_i[0] * v_j[1] - v_i[1] * v_j[0]; 335ccd2543fSMatthew G Knepley pp[0] = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 0] - point[0]); 336ccd2543fSMatthew G Knepley pp[1] = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 1] - point[1]); 337ccd2543fSMatthew G Knepley pp[2] = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 2] - point[2]); 338ccd2543fSMatthew G Knepley dot = normal[0] * pp[0] + normal[1] * pp[1] + normal[2] * pp[2]; 339ccd2543fSMatthew G Knepley 340ccd2543fSMatthew G Knepley /* Check that projected point is in face (2D location problem) */ 341ccd2543fSMatthew G Knepley if (dot < 0.0) { 342ccd2543fSMatthew G Knepley found = PETSC_FALSE; 343ccd2543fSMatthew G Knepley break; 344ccd2543fSMatthew G Knepley } 345ccd2543fSMatthew G Knepley } 346ccd2543fSMatthew G Knepley if (found) *cell = c; 347c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 3489566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords)); 349ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 350ccd2543fSMatthew G Knepley } 351ccd2543fSMatthew G Knepley 3529371c9d4SSatish Balay static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[]) { 353c4eade1cSMatthew G. Knepley PetscInt d; 354c4eade1cSMatthew G. Knepley 355c4eade1cSMatthew G. Knepley PetscFunctionBegin; 356c4eade1cSMatthew G. Knepley box->dim = dim; 357c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]); 358c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 359c4eade1cSMatthew G. Knepley } 360c4eade1cSMatthew G. Knepley 3619371c9d4SSatish Balay PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box) { 362c4eade1cSMatthew G. Knepley PetscFunctionBegin; 3639566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(1, box)); 3649566063dSJacob Faibussowitsch PetscCall(PetscGridHashInitialize_Internal(*box, dim, point)); 365c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 366c4eade1cSMatthew G. Knepley } 367c4eade1cSMatthew G. Knepley 3689371c9d4SSatish Balay PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[]) { 369c4eade1cSMatthew G. Knepley PetscInt d; 370c4eade1cSMatthew G. Knepley 371c4eade1cSMatthew G. Knepley PetscFunctionBegin; 372c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 373c4eade1cSMatthew G. Knepley box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d])); 374c4eade1cSMatthew G. Knepley box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d])); 375c4eade1cSMatthew G. Knepley } 376c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 377c4eade1cSMatthew G. Knepley } 378c4eade1cSMatthew G. Knepley 37962a38674SMatthew G. Knepley /* 38062a38674SMatthew G. Knepley PetscGridHashSetGrid - Divide the grid into boxes 38162a38674SMatthew G. Knepley 38262a38674SMatthew G. Knepley Not collective 38362a38674SMatthew G. Knepley 38462a38674SMatthew G. Knepley Input Parameters: 38562a38674SMatthew G. Knepley + box - The grid hash object 38662a38674SMatthew G. Knepley . n - The number of boxes in each dimension, or PETSC_DETERMINE 38762a38674SMatthew G. Knepley - h - The box size in each dimension, only used if n[d] == PETSC_DETERMINE 38862a38674SMatthew G. Knepley 38962a38674SMatthew G. Knepley Level: developer 39062a38674SMatthew G. Knepley 391db781477SPatrick Sanan .seealso: `PetscGridHashCreate()` 39262a38674SMatthew G. Knepley */ 3939371c9d4SSatish Balay PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[]) { 394c4eade1cSMatthew G. Knepley PetscInt d; 395c4eade1cSMatthew G. Knepley 396c4eade1cSMatthew G. Knepley PetscFunctionBegin; 397c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 398c4eade1cSMatthew G. Knepley box->extent[d] = box->upper[d] - box->lower[d]; 399c4eade1cSMatthew G. Knepley if (n[d] == PETSC_DETERMINE) { 400c4eade1cSMatthew G. Knepley box->h[d] = h[d]; 401c4eade1cSMatthew G. Knepley box->n[d] = PetscCeilReal(box->extent[d] / h[d]); 402c4eade1cSMatthew G. Knepley } else { 403c4eade1cSMatthew G. Knepley box->n[d] = n[d]; 404c4eade1cSMatthew G. Knepley box->h[d] = box->extent[d] / n[d]; 405c4eade1cSMatthew G. Knepley } 406c4eade1cSMatthew G. Knepley } 407c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 408c4eade1cSMatthew G. Knepley } 409c4eade1cSMatthew G. Knepley 41062a38674SMatthew G. Knepley /* 41162a38674SMatthew G. Knepley PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point 41262a38674SMatthew G. Knepley 41362a38674SMatthew G. Knepley Not collective 41462a38674SMatthew G. Knepley 41562a38674SMatthew G. Knepley Input Parameters: 41662a38674SMatthew G. Knepley + box - The grid hash object 41762a38674SMatthew G. Knepley . numPoints - The number of input points 41862a38674SMatthew G. Knepley - points - The input point coordinates 41962a38674SMatthew G. Knepley 42062a38674SMatthew G. Knepley Output Parameters: 42162a38674SMatthew G. Knepley + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 42262a38674SMatthew G. Knepley - boxes - An array of numPoints integers expressing the enclosing box as single number, or NULL 42362a38674SMatthew G. Knepley 42462a38674SMatthew G. Knepley Level: developer 42562a38674SMatthew G. Knepley 426f5867de0SMatthew G. Knepley Note: 427f5867de0SMatthew G. Knepley This only guarantees that a box contains a point, not that a cell does. 428f5867de0SMatthew G. Knepley 429db781477SPatrick Sanan .seealso: `PetscGridHashCreate()` 43062a38674SMatthew G. Knepley */ 4319371c9d4SSatish Balay PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[]) { 432c4eade1cSMatthew G. Knepley const PetscReal *lower = box->lower; 433c4eade1cSMatthew G. Knepley const PetscReal *upper = box->upper; 434c4eade1cSMatthew G. Knepley const PetscReal *h = box->h; 435c4eade1cSMatthew G. Knepley const PetscInt *n = box->n; 436c4eade1cSMatthew G. Knepley const PetscInt dim = box->dim; 437c4eade1cSMatthew G. Knepley PetscInt d, p; 438c4eade1cSMatthew G. Knepley 439c4eade1cSMatthew G. Knepley PetscFunctionBegin; 440c4eade1cSMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 441c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) { 4421c6dfc3eSMatthew G. Knepley PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]); 443c4eade1cSMatthew G. Knepley 4441c6dfc3eSMatthew G. Knepley if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1; 4452a705cacSMatthew G. Knepley if (dbox == -1 && PetscAbsReal(PetscRealPart(points[p * dim + d]) - lower[d]) < 1.0e-9) dbox = 0; 4469371c9d4SSatish Balay PetscCheck(dbox >= 0 && dbox<n[d], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %" PetscInt_FMT " (%g, %g, %g) is outside of our bounding box", 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); 447c4eade1cSMatthew G. Knepley dboxes[p * dim + d] = dbox; 448c4eade1cSMatthew G. Knepley } 4499371c9d4SSatish Balay if (boxes) 4509371c9d4SSatish Balay for (d = dim - 2, boxes[p] = dboxes[p * dim + dim - 1]; d >= 0; --d) boxes[p] = boxes[p] * n[d] + dboxes[p * dim + d]; 451c4eade1cSMatthew G. Knepley } 452c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 453c4eade1cSMatthew G. Knepley } 454c4eade1cSMatthew G. Knepley 455af74b616SDave May /* 456af74b616SDave May PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point 457af74b616SDave May 458af74b616SDave May Not collective 459af74b616SDave May 460af74b616SDave May Input Parameters: 461af74b616SDave May + box - The grid hash object 462f5867de0SMatthew G. Knepley . cellSection - The PetscSection mapping cells to boxes 463af74b616SDave May . numPoints - The number of input points 464af74b616SDave May - points - The input point coordinates 465af74b616SDave May 466af74b616SDave May Output Parameters: 467af74b616SDave May + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 468af74b616SDave May . boxes - An array of numPoints integers expressing the enclosing box as single number, or NULL 469af74b616SDave May - found - Flag indicating if point was located within a box 470af74b616SDave May 471af74b616SDave May Level: developer 472af74b616SDave May 473f5867de0SMatthew G. Knepley Note: 474f5867de0SMatthew G. Knepley This does an additional check that a cell actually contains the point, and found is PETSC_FALSE if no cell does. Thus, this function requires that the cellSection is already constructed. 475f5867de0SMatthew G. Knepley 476db781477SPatrick Sanan .seealso: `PetscGridHashGetEnclosingBox()` 477af74b616SDave May */ 4789371c9d4SSatish Balay PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscSection cellSection, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[], PetscBool *found) { 479af74b616SDave May const PetscReal *lower = box->lower; 480af74b616SDave May const PetscReal *upper = box->upper; 481af74b616SDave May const PetscReal *h = box->h; 482af74b616SDave May const PetscInt *n = box->n; 483af74b616SDave May const PetscInt dim = box->dim; 484f5867de0SMatthew G. Knepley PetscInt bStart, bEnd, d, p; 485af74b616SDave May 486af74b616SDave May PetscFunctionBegin; 487f5867de0SMatthew G. Knepley PetscValidHeaderSpecific(cellSection, PETSC_SECTION_CLASSID, 2); 488af74b616SDave May *found = PETSC_FALSE; 489f5867de0SMatthew G. Knepley PetscCall(PetscSectionGetChart(box->cellSection, &bStart, &bEnd)); 490af74b616SDave May for (p = 0; p < numPoints; ++p) { 491af74b616SDave May for (d = 0; d < dim; ++d) { 492af74b616SDave May PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]); 493af74b616SDave May 494af74b616SDave May if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1; 495f5867de0SMatthew G. Knepley if (dbox < 0 || dbox >= n[d]) PetscFunctionReturn(0); 496af74b616SDave May dboxes[p * dim + d] = dbox; 497af74b616SDave May } 4989371c9d4SSatish Balay if (boxes) 4999371c9d4SSatish Balay for (d = dim - 2, boxes[p] = dboxes[p * dim + dim - 1]; d >= 0; --d) boxes[p] = boxes[p] * n[d] + dboxes[p * dim + d]; 500f5867de0SMatthew G. Knepley // It is possible for a box to overlap no grid cells 501f5867de0SMatthew G. Knepley if (boxes[p] < bStart || boxes[p] >= bEnd) PetscFunctionReturn(0); 502af74b616SDave May } 503af74b616SDave May *found = PETSC_TRUE; 504af74b616SDave May PetscFunctionReturn(0); 505af74b616SDave May } 506af74b616SDave May 5079371c9d4SSatish Balay PetscErrorCode PetscGridHashDestroy(PetscGridHash *box) { 508c4eade1cSMatthew G. Knepley PetscFunctionBegin; 509c4eade1cSMatthew G. Knepley if (*box) { 5109566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&(*box)->cellSection)); 5119566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*box)->cells)); 5129566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&(*box)->cellsSparse)); 513c4eade1cSMatthew G. Knepley } 5149566063dSJacob Faibussowitsch PetscCall(PetscFree(*box)); 515c4eade1cSMatthew G. Knepley PetscFunctionReturn(0); 516c4eade1cSMatthew G. Knepley } 517c4eade1cSMatthew G. Knepley 5189371c9d4SSatish Balay PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell) { 519ba2698f1SMatthew G. Knepley DMPolytopeType ct; 520cafe43deSMatthew G. Knepley 521cafe43deSMatthew G. Knepley PetscFunctionBegin; 5229566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cellStart, &ct)); 523ba2698f1SMatthew G. Knepley switch (ct) { 5249371c9d4SSatish Balay case DM_POLYTOPE_SEGMENT: PetscCall(DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell)); break; 5259371c9d4SSatish Balay case DM_POLYTOPE_TRIANGLE: PetscCall(DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell)); break; 5269371c9d4SSatish Balay case DM_POLYTOPE_QUADRILATERAL: PetscCall(DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell)); break; 5279371c9d4SSatish Balay case DM_POLYTOPE_TETRAHEDRON: PetscCall(DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell)); break; 5289371c9d4SSatish Balay case DM_POLYTOPE_HEXAHEDRON: PetscCall(DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell)); break; 52963a3b9bcSJacob Faibussowitsch default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %" PetscInt_FMT " with type %s", cellStart, DMPolytopeTypes[ct]); 530cafe43deSMatthew G. Knepley } 531cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 532cafe43deSMatthew G. Knepley } 533cafe43deSMatthew G. Knepley 53462a38674SMatthew G. Knepley /* 53562a38674SMatthew G. Knepley DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point 53662a38674SMatthew G. Knepley */ 5379371c9d4SSatish Balay PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[]) { 538ba2698f1SMatthew G. Knepley DMPolytopeType ct; 53962a38674SMatthew G. Knepley 54062a38674SMatthew G. Knepley PetscFunctionBegin; 5419566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 542ba2698f1SMatthew G. Knepley switch (ct) { 5439371c9d4SSatish Balay case DM_POLYTOPE_TRIANGLE: PetscCall(DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint)); break; 54462a38674SMatthew G. Knepley #if 0 545ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 5469566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint));break; 547ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 5489566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint));break; 549ba2698f1SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 5509566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint));break; 55162a38674SMatthew G. Knepley #endif 55263a3b9bcSJacob Faibussowitsch default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %" PetscInt_FMT " with type %s", cell, DMPolytopeTypes[ct]); 55362a38674SMatthew G. Knepley } 55462a38674SMatthew G. Knepley PetscFunctionReturn(0); 55562a38674SMatthew G. Knepley } 55662a38674SMatthew G. Knepley 55762a38674SMatthew G. Knepley /* 55862a38674SMatthew G. Knepley DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex 55962a38674SMatthew G. Knepley 560d083f849SBarry Smith Collective on dm 56162a38674SMatthew G. Knepley 56262a38674SMatthew G. Knepley Input Parameter: 56362a38674SMatthew G. Knepley . dm - The Plex 56462a38674SMatthew G. Knepley 56562a38674SMatthew G. Knepley Output Parameter: 56662a38674SMatthew G. Knepley . localBox - The grid hash object 56762a38674SMatthew G. Knepley 56862a38674SMatthew G. Knepley Level: developer 56962a38674SMatthew G. Knepley 570db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`, `PetscGridHashGetEnclosingBox()` 57162a38674SMatthew G. Knepley */ 5729371c9d4SSatish Balay PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox) { 573f5867de0SMatthew G. Knepley PetscInt debug = ((DM_Plex *)dm->data)->printLocate; 574cafe43deSMatthew G. Knepley MPI_Comm comm; 575cafe43deSMatthew G. Knepley PetscGridHash lbox; 576cafe43deSMatthew G. Knepley Vec coordinates; 577cafe43deSMatthew G. Knepley PetscSection coordSection; 578cafe43deSMatthew G. Knepley Vec coordsLocal; 579cafe43deSMatthew G. Knepley const PetscScalar *coords; 580ddce0771SMatthew G. Knepley PetscScalar *edgeCoords; 581722d0f5cSMatthew G. Knepley PetscInt *dboxes, *boxes; 582ddce0771SMatthew G. Knepley PetscInt n[3] = {2, 2, 2}; 583ddce0771SMatthew G. Knepley PetscInt dim, N, maxConeSize, cStart, cEnd, c, eStart, eEnd, i; 584ddce0771SMatthew G. Knepley PetscBool flg; 585cafe43deSMatthew G. Knepley 586cafe43deSMatthew G. Knepley PetscFunctionBegin; 5879566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 5889566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 5899566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dim)); 5909566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL)); 5919566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 5929566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(coordinates, &N)); 5939566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coords)); 5949566063dSJacob Faibussowitsch PetscCall(PetscGridHashCreate(comm, dim, coords, &lbox)); 5959566063dSJacob Faibussowitsch for (i = 0; i < N; i += dim) PetscCall(PetscGridHashEnlarge(lbox, &coords[i])); 5969566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coords)); 597ddce0771SMatthew G. Knepley c = dim; 5989566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, "-dm_plex_hash_box_faces", n, &c, &flg)); 5999371c9d4SSatish Balay if (flg) { 6009371c9d4SSatish Balay for (i = c; i < dim; ++i) n[i] = n[c - 1]; 6019371c9d4SSatish Balay } else { 6029371c9d4SSatish Balay for (i = 0; i < dim; ++i) n[i] = PetscMax(2, PetscFloorReal(PetscPowReal((PetscReal)(cEnd - cStart), 1.0 / dim) * 0.8)); 6039371c9d4SSatish Balay } 6049566063dSJacob Faibussowitsch PetscCall(PetscGridHashSetGrid(lbox, n, NULL)); 6059371c9d4SSatish Balay if (debug) 6069371c9d4SSatish Balay PetscCall(PetscPrintf(PETSC_COMM_SELF, "GridHash:\n (%g, %g, %g) -- (%g, %g, %g)\n n %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n h %g %g %g\n", (double)lbox->lower[0], (double)lbox->lower[1], (double)lbox->lower[2], (double)lbox->upper[0], 6079371c9d4SSatish Balay (double)lbox->upper[1], (double)lbox->upper[2], n[0], n[1], n[2], (double)lbox->h[0], (double)lbox->h[1], (double)lbox->h[2])); 608cafe43deSMatthew G. Knepley #if 0 609cafe43deSMatthew G. Knepley /* Could define a custom reduction to merge these */ 6101c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm)); 6111c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm)); 612cafe43deSMatthew G. Knepley #endif 613cafe43deSMatthew G. Knepley /* Is there a reason to snap the local bounding box to a division of the global box? */ 614cafe43deSMatthew G. Knepley /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */ 615cafe43deSMatthew G. Knepley /* Create label */ 6169566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd)); 617b26b5bf9SMatthew G. Knepley if (dim < 2) eStart = eEnd = -1; 6189566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse)); 6199566063dSJacob Faibussowitsch PetscCall(DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd)); 620a8d69d7bSBarry Smith /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */ 6219566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal)); 6229566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 6239566063dSJacob Faibussowitsch PetscCall(PetscCalloc3(16 * dim, &dboxes, 16, &boxes, PetscPowInt(maxConeSize, dim) * dim, &edgeCoords)); 624cafe43deSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 625cafe43deSMatthew G. Knepley const PetscReal *h = lbox->h; 626cafe43deSMatthew G. Knepley PetscScalar *ccoords = NULL; 62738353de4SMatthew G. Knepley PetscInt csize = 0; 628ddce0771SMatthew G. Knepley PetscInt *closure = NULL; 629ddce0771SMatthew G. Knepley PetscInt Ncl, cl, Ne = 0; 630cafe43deSMatthew G. Knepley PetscScalar point[3]; 631cafe43deSMatthew G. Knepley PetscInt dlim[6], d, e, i, j, k; 632cafe43deSMatthew G. Knepley 633ddce0771SMatthew G. Knepley /* Get all edges in cell */ 6349566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure)); 635ddce0771SMatthew G. Knepley for (cl = 0; cl < Ncl * 2; ++cl) { 636ddce0771SMatthew G. Knepley if ((closure[cl] >= eStart) && (closure[cl] < eEnd)) { 637ddce0771SMatthew G. Knepley PetscScalar *ecoords = &edgeCoords[Ne * dim * 2]; 638ddce0771SMatthew G. Knepley PetscInt ecsize = dim * 2; 639ddce0771SMatthew G. Knepley 6409566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, closure[cl], &ecsize, &ecoords)); 64163a3b9bcSJacob Faibussowitsch PetscCheck(ecsize == dim * 2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Got %" PetscInt_FMT " coords for edge, instead of %" PetscInt_FMT, ecsize, dim * 2); 642ddce0771SMatthew G. Knepley ++Ne; 643ddce0771SMatthew G. Knepley } 644ddce0771SMatthew G. Knepley } 6459566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure)); 646cafe43deSMatthew G. Knepley /* Find boxes enclosing each vertex */ 6479566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords)); 6489566063dSJacob Faibussowitsch PetscCall(PetscGridHashGetEnclosingBox(lbox, csize / dim, ccoords, dboxes, boxes)); 649722d0f5cSMatthew G. Knepley /* Mark cells containing the vertices */ 650ddce0771SMatthew G. Knepley for (e = 0; e < csize / dim; ++e) { 6519371c9d4SSatish Balay if (debug) 6529371c9d4SSatish Balay PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " has vertex in box %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ")\n", c, boxes[e], dboxes[e * dim + 0], dim > 1 ? dboxes[e * dim + 1] : -1, dim > 2 ? dboxes[e * dim + 2] : -1)); 6539566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(lbox->cellsSparse, c, boxes[e])); 654ddce0771SMatthew G. Knepley } 655cafe43deSMatthew G. Knepley /* Get grid of boxes containing these */ 656cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) { dlim[d * 2 + 0] = dlim[d * 2 + 1] = dboxes[d]; } 6572291669eSMatthew G. Knepley for (d = dim; d < 3; ++d) { dlim[d * 2 + 0] = dlim[d * 2 + 1] = 0; } 658cafe43deSMatthew G. Knepley for (e = 1; e < dim + 1; ++e) { 659cafe43deSMatthew G. Knepley for (d = 0; d < dim; ++d) { 660cafe43deSMatthew G. Knepley dlim[d * 2 + 0] = PetscMin(dlim[d * 2 + 0], dboxes[e * dim + d]); 661cafe43deSMatthew G. Knepley dlim[d * 2 + 1] = PetscMax(dlim[d * 2 + 1], dboxes[e * dim + d]); 662cafe43deSMatthew G. Knepley } 663cafe43deSMatthew G. Knepley } 664fea14342SMatthew G. Knepley /* Check for intersection of box with cell */ 665cafe43deSMatthew 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]) { 666cafe43deSMatthew 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]) { 667cafe43deSMatthew 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]) { 668cafe43deSMatthew G. Knepley const PetscInt box = (k * lbox->n[1] + j) * lbox->n[0] + i; 669cafe43deSMatthew G. Knepley PetscScalar cpoint[3]; 670fea14342SMatthew G. Knepley PetscInt cell, edge, ii, jj, kk; 671cafe43deSMatthew G. Knepley 6729371c9d4SSatish Balay if (debug) 6739371c9d4SSatish Balay PetscCall(PetscPrintf(PETSC_COMM_SELF, "Box %" PetscInt_FMT ": (%.2g, %.2g, %.2g) -- (%.2g, %.2g, %.2g)\n", box, (double)PetscRealPart(point[0]), (double)PetscRealPart(point[1]), (double)PetscRealPart(point[2]), (double)PetscRealPart(point[0] + h[0]), (double)PetscRealPart(point[1] + h[1]), (double)PetscRealPart(point[2] + h[2]))); 674ddce0771SMatthew G. Knepley /* Check whether cell contains any vertex of this subbox TODO vectorize this */ 675cafe43deSMatthew G. Knepley for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) { 676cafe43deSMatthew G. Knepley for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) { 677cafe43deSMatthew G. Knepley for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) { 6789566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell)); 6790b6bfacdSStefano Zampini if (cell >= 0) { 6809371c9d4SSatish Balay if (debug) 6819371c9d4SSatish Balay PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " contains vertex (%.2g, %.2g, %.2g) of box %" PetscInt_FMT "\n", c, (double)PetscRealPart(cpoint[0]), (double)PetscRealPart(cpoint[1]), (double)PetscRealPart(cpoint[2]), box)); 6829566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 6830b6bfacdSStefano Zampini jj = kk = 2; 6840b6bfacdSStefano Zampini break; 6850b6bfacdSStefano Zampini } 686cafe43deSMatthew G. Knepley } 687cafe43deSMatthew G. Knepley } 688cafe43deSMatthew G. Knepley } 689ddce0771SMatthew G. Knepley /* Check whether cell edge intersects any face of these subboxes TODO vectorize this */ 690ddce0771SMatthew G. Knepley for (edge = 0; edge < Ne; ++edge) { 691a5cae605SSatish Balay PetscReal segA[6] = {0., 0., 0., 0., 0., 0.}; 692a5cae605SSatish Balay PetscReal segB[6] = {0., 0., 0., 0., 0., 0.}; 693a5cae605SSatish Balay PetscReal segC[6] = {0., 0., 0., 0., 0., 0.}; 694fea14342SMatthew G. Knepley 69563a3b9bcSJacob Faibussowitsch PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected dim %" PetscInt_FMT " > 3", dim); 696ddce0771SMatthew G. Knepley for (d = 0; d < dim * 2; ++d) segA[d] = PetscRealPart(edgeCoords[edge * dim * 2 + d]); 697ddce0771SMatthew G. Knepley /* 1D: (x) -- (x+h) 0 -- 1 698ddce0771SMatthew G. Knepley 2D: (x, y) -- (x, y+h) (0, 0) -- (0, 1) 699ddce0771SMatthew G. Knepley (x+h, y) -- (x+h, y+h) (1, 0) -- (1, 1) 700ddce0771SMatthew G. Knepley (x, y) -- (x+h, y) (0, 0) -- (1, 0) 701ddce0771SMatthew G. Knepley (x, y+h) -- (x+h, y+h) (0, 1) -- (1, 1) 702ddce0771SMatthew 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) 703ddce0771SMatthew 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) 704ddce0771SMatthew 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) 705ddce0771SMatthew 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) 706ddce0771SMatthew 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) 707ddce0771SMatthew 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) 708ddce0771SMatthew G. Knepley */ 709ddce0771SMatthew G. Knepley /* Loop over faces with normal in direction d */ 710ddce0771SMatthew G. Knepley for (d = 0; d < dim; ++d) { 711ddce0771SMatthew G. Knepley PetscBool intersects = PETSC_FALSE; 712ddce0771SMatthew G. Knepley PetscInt e = (d + 1) % dim; 713ddce0771SMatthew G. Knepley PetscInt f = (d + 2) % dim; 714ddce0771SMatthew G. Knepley 715ddce0771SMatthew G. Knepley /* There are two faces in each dimension */ 716ddce0771SMatthew G. Knepley for (ii = 0; ii < 2; ++ii) { 717ddce0771SMatthew G. Knepley segB[d] = PetscRealPart(point[d] + ii * h[d]); 718ddce0771SMatthew G. Knepley segB[dim + d] = PetscRealPart(point[d] + ii * h[d]); 719ddce0771SMatthew G. Knepley segC[d] = PetscRealPart(point[d] + ii * h[d]); 720ddce0771SMatthew G. Knepley segC[dim + d] = PetscRealPart(point[d] + ii * h[d]); 721ddce0771SMatthew G. Knepley if (dim > 1) { 722ddce0771SMatthew G. Knepley segB[e] = PetscRealPart(point[e] + 0 * h[e]); 723ddce0771SMatthew G. Knepley segB[dim + e] = PetscRealPart(point[e] + 1 * h[e]); 724ddce0771SMatthew G. Knepley segC[e] = PetscRealPart(point[e] + 0 * h[e]); 725ddce0771SMatthew G. Knepley segC[dim + e] = PetscRealPart(point[e] + 0 * h[e]); 726ddce0771SMatthew G. Knepley } 727ddce0771SMatthew G. Knepley if (dim > 2) { 728ddce0771SMatthew G. Knepley segB[f] = PetscRealPart(point[f] + 0 * h[f]); 729ddce0771SMatthew G. Knepley segB[dim + f] = PetscRealPart(point[f] + 0 * h[f]); 730ddce0771SMatthew G. Knepley segC[f] = PetscRealPart(point[f] + 0 * h[f]); 731ddce0771SMatthew G. Knepley segC[dim + f] = PetscRealPart(point[f] + 1 * h[f]); 732ddce0771SMatthew G. Knepley } 733ddce0771SMatthew G. Knepley if (dim == 2) { 7349566063dSJacob Faibussowitsch PetscCall(DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects)); 735ddce0771SMatthew G. Knepley } else if (dim == 3) { 7369566063dSJacob Faibussowitsch PetscCall(DMPlexGetLinePlaneIntersection_3D_Internal(segA, segB, segC, NULL, &intersects)); 737ddce0771SMatthew G. Knepley } 738ddce0771SMatthew G. Knepley if (intersects) { 7399371c9d4SSatish Balay if (debug) 7409371c9d4SSatish Balay PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " edge %" PetscInt_FMT " (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g) intersects box %" PetscInt_FMT ", face (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g) (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g)\n", c, edge, (double)segA[0], (double)segA[1], (double)segA[2], (double)segA[3], (double)segA[4], (double)segA[5], box, (double)segB[0], (double)segB[1], (double)segB[2], (double)segB[3], (double)segB[4], (double)segB[5], (double)segC[0], (double)segC[1], (double)segC[2], (double)segC[3], (double)segC[4], (double)segC[5])); 7419371c9d4SSatish Balay PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 7429371c9d4SSatish Balay edge = Ne; 7439371c9d4SSatish Balay break; 744ddce0771SMatthew G. Knepley } 745ddce0771SMatthew G. Knepley } 746ddce0771SMatthew G. Knepley } 747cafe43deSMatthew G. Knepley } 748fea14342SMatthew G. Knepley } 749fea14342SMatthew G. Knepley } 750fea14342SMatthew G. Knepley } 7519566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords)); 752fea14342SMatthew G. Knepley } 7539566063dSJacob Faibussowitsch PetscCall(PetscFree3(dboxes, boxes, edgeCoords)); 7549566063dSJacob Faibussowitsch if (debug) PetscCall(DMLabelView(lbox->cellsSparse, PETSC_VIEWER_STDOUT_SELF)); 7559566063dSJacob Faibussowitsch PetscCall(DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells)); 7569566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lbox->cellsSparse)); 757cafe43deSMatthew G. Knepley *localBox = lbox; 758cafe43deSMatthew G. Knepley PetscFunctionReturn(0); 759cafe43deSMatthew G. Knepley } 760cafe43deSMatthew G. Knepley 7619371c9d4SSatish Balay PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF) { 762f5867de0SMatthew G. Knepley PetscInt debug = ((DM_Plex *)dm->data)->printLocate; 763cafe43deSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 764af74b616SDave May PetscBool hash = mesh->useHashLocation, reuse = PETSC_FALSE; 7653a93e3b7SToby Isaac PetscInt bs, numPoints, p, numFound, *found = NULL; 766412e9a14SMatthew G. Knepley PetscInt dim, cStart, cEnd, numCells, c, d; 767cafe43deSMatthew G. Knepley const PetscInt *boxCells; 7683a93e3b7SToby Isaac PetscSFNode *cells; 769ccd2543fSMatthew G Knepley PetscScalar *a; 7703a93e3b7SToby Isaac PetscMPIInt result; 771af74b616SDave May PetscLogDouble t0, t1; 7729cb35068SDave May PetscReal gmin[3], gmax[3]; 7739cb35068SDave May PetscInt terminating_query_type[] = {0, 0, 0}; 774ccd2543fSMatthew G Knepley 775ccd2543fSMatthew G Knepley PetscFunctionBegin; 7769566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_LocatePoints, 0, 0, 0, 0)); 7779566063dSJacob Faibussowitsch PetscCall(PetscTime(&t0)); 7781dca8a05SBarry Smith PetscCheck(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."); 7799566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dim)); 7809566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(v, &bs)); 7819566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF), PETSC_COMM_SELF, &result)); 7821dca8a05SBarry Smith PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT, PetscObjectComm((PetscObject)cellSF), PETSC_ERR_SUP, "Trying parallel point location: only local point location supported"); 78363a3b9bcSJacob Faibussowitsch PetscCheck(bs == dim, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Block size for point vector %" PetscInt_FMT " must be the mesh coordinate dimension %" PetscInt_FMT, bs, dim); 7846858538eSMatthew G. Knepley PetscCall(DMGetCoordinatesLocalSetUp(dm)); 7859566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 7869566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(v, &numPoints)); 7879566063dSJacob Faibussowitsch PetscCall(VecGetArray(v, &a)); 788ccd2543fSMatthew G Knepley numPoints /= bs; 789af74b616SDave May { 790af74b616SDave May const PetscSFNode *sf_cells; 791af74b616SDave May 7929566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(cellSF, NULL, NULL, NULL, &sf_cells)); 793af74b616SDave May if (sf_cells) { 7949566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Re-using existing StarForest node list\n")); 795af74b616SDave May cells = (PetscSFNode *)sf_cells; 796af74b616SDave May reuse = PETSC_TRUE; 797af74b616SDave May } else { 7989566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n")); 7999566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints, &cells)); 800af74b616SDave May /* initialize cells if created */ 801af74b616SDave May for (p = 0; p < numPoints; p++) { 802af74b616SDave May cells[p].rank = 0; 803af74b616SDave May cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 804af74b616SDave May } 805af74b616SDave May } 806af74b616SDave May } 8079cb35068SDave May /* define domain bounding box */ 8089cb35068SDave May { 8099cb35068SDave May Vec coorglobal; 8109cb35068SDave May 8119566063dSJacob Faibussowitsch PetscCall(DMGetCoordinates(dm, &coorglobal)); 8129566063dSJacob Faibussowitsch PetscCall(VecStrideMaxAll(coorglobal, NULL, gmax)); 8139566063dSJacob Faibussowitsch PetscCall(VecStrideMinAll(coorglobal, NULL, gmin)); 8149cb35068SDave May } 815953fc75cSMatthew G. Knepley if (hash) { 8169371c9d4SSatish Balay if (!mesh->lbox) { 8179371c9d4SSatish Balay PetscCall(PetscInfo(dm, "Initializing grid hashing")); 8189371c9d4SSatish Balay PetscCall(DMPlexComputeGridHash_Internal(dm, &mesh->lbox)); 8199371c9d4SSatish Balay } 820cafe43deSMatthew G. Knepley /* Designate the local box for each point */ 821cafe43deSMatthew G. Knepley /* Send points to correct process */ 822cafe43deSMatthew G. Knepley /* Search cells that lie in each subbox */ 823cafe43deSMatthew G. Knepley /* Should we bin points before doing search? */ 8249566063dSJacob Faibussowitsch PetscCall(ISGetIndices(mesh->lbox->cells, &boxCells)); 825953fc75cSMatthew G. Knepley } 8263a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; ++p) { 827ccd2543fSMatthew G Knepley const PetscScalar *point = &a[p * bs]; 828e56f9228SJed Brown PetscInt dbin[3] = {-1, -1, -1}, bin, cell = -1, cellOffset; 8299cb35068SDave May PetscBool point_outside_domain = PETSC_FALSE; 830ccd2543fSMatthew G Knepley 8319cb35068SDave May /* check bounding box of domain */ 8329cb35068SDave May for (d = 0; d < dim; d++) { 8339371c9d4SSatish Balay if (PetscRealPart(point[d]) < gmin[d]) { 8349371c9d4SSatish Balay point_outside_domain = PETSC_TRUE; 8359371c9d4SSatish Balay break; 8369371c9d4SSatish Balay } 8379371c9d4SSatish Balay if (PetscRealPart(point[d]) > gmax[d]) { 8389371c9d4SSatish Balay point_outside_domain = PETSC_TRUE; 8399371c9d4SSatish Balay break; 8409371c9d4SSatish Balay } 8419cb35068SDave May } 8429cb35068SDave May if (point_outside_domain) { 843e9b685f5SMatthew G. Knepley cells[p].rank = 0; 844e9b685f5SMatthew G. Knepley cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 8459cb35068SDave May terminating_query_type[0]++; 8469cb35068SDave May continue; 8479cb35068SDave May } 848ccd2543fSMatthew G Knepley 849af74b616SDave May /* check initial values in cells[].index - abort early if found */ 850af74b616SDave May if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) { 851af74b616SDave May c = cells[p].index; 8523a93e3b7SToby Isaac cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 8539566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell)); 854af74b616SDave May if (cell >= 0) { 855af74b616SDave May cells[p].rank = 0; 856af74b616SDave May cells[p].index = cell; 857af74b616SDave May numFound++; 858af74b616SDave May } 859af74b616SDave May } 8609cb35068SDave May if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) { 8619cb35068SDave May terminating_query_type[1]++; 8629cb35068SDave May continue; 8639cb35068SDave May } 864af74b616SDave May 865953fc75cSMatthew G. Knepley if (hash) { 866af74b616SDave May PetscBool found_box; 867af74b616SDave May 86863a3b9bcSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Checking point %" PetscInt_FMT " (%.2g, %.2g, %.2g)\n", p, (double)PetscRealPart(point[0]), (double)PetscRealPart(point[1]), (double)PetscRealPart(point[2]))); 869af74b616SDave May /* allow for case that point is outside box - abort early */ 870f5867de0SMatthew G. Knepley PetscCall(PetscGridHashGetEnclosingBoxQuery(mesh->lbox, mesh->lbox->cellSection, 1, point, dbin, &bin, &found_box)); 871af74b616SDave May if (found_box) { 87263a3b9bcSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Found point in box %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ")\n", bin, dbin[0], dbin[1], dbin[2])); 873cafe43deSMatthew G. Knepley /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */ 8749566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells)); 8759566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset)); 876cafe43deSMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 87763a3b9bcSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Checking for point in cell %" PetscInt_FMT "\n", boxCells[c])); 8789566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell)); 8793a93e3b7SToby Isaac if (cell >= 0) { 88063a3b9bcSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " FOUND in cell %" PetscInt_FMT "\n", cell)); 8813a93e3b7SToby Isaac cells[p].rank = 0; 8823a93e3b7SToby Isaac cells[p].index = cell; 8833a93e3b7SToby Isaac numFound++; 8849cb35068SDave May terminating_query_type[2]++; 8853a93e3b7SToby Isaac break; 886ccd2543fSMatthew G Knepley } 8873a93e3b7SToby Isaac } 888af74b616SDave May } 889953fc75cSMatthew G. Knepley } else { 890953fc75cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 8919566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell)); 8923a93e3b7SToby Isaac if (cell >= 0) { 8933a93e3b7SToby Isaac cells[p].rank = 0; 8943a93e3b7SToby Isaac cells[p].index = cell; 8953a93e3b7SToby Isaac numFound++; 8969cb35068SDave May terminating_query_type[2]++; 8973a93e3b7SToby Isaac break; 898953fc75cSMatthew G. Knepley } 899953fc75cSMatthew G. Knepley } 9003a93e3b7SToby Isaac } 901ccd2543fSMatthew G Knepley } 9029566063dSJacob Faibussowitsch if (hash) PetscCall(ISRestoreIndices(mesh->lbox->cells, &boxCells)); 90362a38674SMatthew G. Knepley if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) { 90462a38674SMatthew G. Knepley for (p = 0; p < numPoints; p++) { 90562a38674SMatthew G. Knepley const PetscScalar *point = &a[p * bs]; 906d92c4b9fSToby Isaac PetscReal cpoint[3], diff[3], best[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL}, dist, distMax = PETSC_MAX_REAL; 907d92c4b9fSToby Isaac PetscInt dbin[3] = {-1, -1, -1}, bin, cellOffset, d, bestc = -1; 90862a38674SMatthew G. Knepley 909e9b685f5SMatthew G. Knepley if (cells[p].index < 0) { 9109566063dSJacob Faibussowitsch PetscCall(PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin)); 9119566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells)); 9129566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset)); 91362a38674SMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 9149566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint)); 915b716b415SMatthew G. Knepley for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]); 91662a38674SMatthew G. Knepley dist = DMPlex_NormD_Internal(dim, diff); 91762a38674SMatthew G. Knepley if (dist < distMax) { 918d92c4b9fSToby Isaac for (d = 0; d < dim; ++d) best[d] = cpoint[d]; 919d92c4b9fSToby Isaac bestc = boxCells[c]; 92062a38674SMatthew G. Knepley distMax = dist; 92162a38674SMatthew G. Knepley } 92262a38674SMatthew G. Knepley } 923d92c4b9fSToby Isaac if (distMax < PETSC_MAX_REAL) { 924d92c4b9fSToby Isaac ++numFound; 925d92c4b9fSToby Isaac cells[p].rank = 0; 926d92c4b9fSToby Isaac cells[p].index = bestc; 927d92c4b9fSToby Isaac for (d = 0; d < dim; ++d) a[p * bs + d] = best[d]; 928d92c4b9fSToby Isaac } 92962a38674SMatthew G. Knepley } 93062a38674SMatthew G. Knepley } 93162a38674SMatthew G. Knepley } 93262a38674SMatthew G. Knepley /* This code is only be relevant when interfaced to parallel point location */ 933cafe43deSMatthew G. Knepley /* Check for highest numbered proc that claims a point (do we care?) */ 9342d1fa6caSMatthew G. Knepley if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) { 9359566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numFound, &found)); 9363a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; p++) { 9373a93e3b7SToby Isaac if (cells[p].rank >= 0 && cells[p].index >= 0) { 9389371c9d4SSatish Balay if (numFound < p) { cells[numFound] = cells[p]; } 9393a93e3b7SToby Isaac found[numFound++] = p; 9403a93e3b7SToby Isaac } 9413a93e3b7SToby Isaac } 9423a93e3b7SToby Isaac } 9439566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(v, &a)); 944*48a46eb9SPierre Jolivet if (!reuse) PetscCall(PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER)); 9459566063dSJacob Faibussowitsch PetscCall(PetscTime(&t1)); 9469cb35068SDave May if (hash) { 94763a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] terminating_query_type : %" PetscInt_FMT " [outside domain] : %" PetscInt_FMT " [inside initial cell] : %" PetscInt_FMT " [hash]\n", terminating_query_type[0], terminating_query_type[1], terminating_query_type[2])); 9489cb35068SDave May } else { 94963a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] terminating_query_type : %" PetscInt_FMT " [outside domain] : %" PetscInt_FMT " [inside initial cell] : %" PetscInt_FMT " [brute-force]\n", terminating_query_type[0], terminating_query_type[1], terminating_query_type[2])); 9509cb35068SDave May } 95163a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] npoints %" PetscInt_FMT " : time(rank0) %1.2e (sec): points/sec %1.4e\n", numPoints, t1 - t0, (double)((double)numPoints / (t1 - t0)))); 9529566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_LocatePoints, 0, 0, 0, 0)); 953ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 954ccd2543fSMatthew G Knepley } 955ccd2543fSMatthew G Knepley 956741bfc07SMatthew G. Knepley /*@C 957741bfc07SMatthew G. Knepley DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates 958741bfc07SMatthew G. Knepley 959741bfc07SMatthew G. Knepley Not collective 960741bfc07SMatthew G. Knepley 9616b867d5aSJose E. Roman Input/Output Parameter: 9626b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x 963741bfc07SMatthew G. Knepley 9646b867d5aSJose E. Roman Output Parameter: 9656b867d5aSJose E. Roman . R - The rotation which accomplishes the projection 966741bfc07SMatthew G. Knepley 967741bfc07SMatthew G. Knepley Level: developer 968741bfc07SMatthew G. Knepley 969db781477SPatrick Sanan .seealso: `DMPlexComputeProjection3Dto1D()`, `DMPlexComputeProjection3Dto2D()` 970741bfc07SMatthew G. Knepley @*/ 9719371c9d4SSatish Balay PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[]) { 97217fe8556SMatthew G. Knepley const PetscReal x = PetscRealPart(coords[2] - coords[0]); 97317fe8556SMatthew G. Knepley const PetscReal y = PetscRealPart(coords[3] - coords[1]); 9748b49ba18SBarry Smith const PetscReal r = PetscSqrtReal(x * x + y * y), c = x / r, s = y / r; 97517fe8556SMatthew G. Knepley 97617fe8556SMatthew G. Knepley PetscFunctionBegin; 9779371c9d4SSatish Balay R[0] = c; 9789371c9d4SSatish Balay R[1] = -s; 9799371c9d4SSatish Balay R[2] = s; 9809371c9d4SSatish Balay R[3] = c; 98117fe8556SMatthew G. Knepley coords[0] = 0.0; 9827f07f362SMatthew G. Knepley coords[1] = r; 98317fe8556SMatthew G. Knepley PetscFunctionReturn(0); 98417fe8556SMatthew G. Knepley } 98517fe8556SMatthew G. Knepley 986741bfc07SMatthew G. Knepley /*@C 987741bfc07SMatthew G. Knepley DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates 98828dbe442SToby Isaac 989741bfc07SMatthew G. Knepley Not collective 99028dbe442SToby Isaac 9916b867d5aSJose E. Roman Input/Output Parameter: 9926b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z 993741bfc07SMatthew G. Knepley 9946b867d5aSJose E. Roman Output Parameter: 9956b867d5aSJose E. Roman . R - The rotation which accomplishes the projection 996741bfc07SMatthew G. Knepley 997741bfc07SMatthew 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 998741bfc07SMatthew G. Knepley 999741bfc07SMatthew G. Knepley Level: developer 1000741bfc07SMatthew G. Knepley 1001db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto2D()` 1002741bfc07SMatthew G. Knepley @*/ 10039371c9d4SSatish Balay PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[]) { 100428dbe442SToby Isaac PetscReal x = PetscRealPart(coords[3] - coords[0]); 100528dbe442SToby Isaac PetscReal y = PetscRealPart(coords[4] - coords[1]); 100628dbe442SToby Isaac PetscReal z = PetscRealPart(coords[5] - coords[2]); 100728dbe442SToby Isaac PetscReal r = PetscSqrtReal(x * x + y * y + z * z); 100828dbe442SToby Isaac PetscReal rinv = 1. / r; 100928dbe442SToby Isaac PetscFunctionBegin; 101028dbe442SToby Isaac 10119371c9d4SSatish Balay x *= rinv; 10129371c9d4SSatish Balay y *= rinv; 10139371c9d4SSatish Balay z *= rinv; 101428dbe442SToby Isaac if (x > 0.) { 101528dbe442SToby Isaac PetscReal inv1pX = 1. / (1. + x); 101628dbe442SToby Isaac 10179371c9d4SSatish Balay R[0] = x; 10189371c9d4SSatish Balay R[1] = -y; 10199371c9d4SSatish Balay R[2] = -z; 10209371c9d4SSatish Balay R[3] = y; 10219371c9d4SSatish Balay R[4] = 1. - y * y * inv1pX; 10229371c9d4SSatish Balay R[5] = -y * z * inv1pX; 10239371c9d4SSatish Balay R[6] = z; 10249371c9d4SSatish Balay R[7] = -y * z * inv1pX; 10259371c9d4SSatish Balay R[8] = 1. - z * z * inv1pX; 10269371c9d4SSatish Balay } else { 102728dbe442SToby Isaac PetscReal inv1mX = 1. / (1. - x); 102828dbe442SToby Isaac 10299371c9d4SSatish Balay R[0] = x; 10309371c9d4SSatish Balay R[1] = z; 10319371c9d4SSatish Balay R[2] = y; 10329371c9d4SSatish Balay R[3] = y; 10339371c9d4SSatish Balay R[4] = -y * z * inv1mX; 10349371c9d4SSatish Balay R[5] = 1. - y * y * inv1mX; 10359371c9d4SSatish Balay R[6] = z; 10369371c9d4SSatish Balay R[7] = 1. - z * z * inv1mX; 10379371c9d4SSatish Balay R[8] = -y * z * inv1mX; 103828dbe442SToby Isaac } 103928dbe442SToby Isaac coords[0] = 0.0; 104028dbe442SToby Isaac coords[1] = r; 104128dbe442SToby Isaac PetscFunctionReturn(0); 104228dbe442SToby Isaac } 104328dbe442SToby Isaac 1044741bfc07SMatthew G. Knepley /*@ 1045c871b86eSJed Brown DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the 1046c871b86eSJed Brown plane. The normal is defined by positive orientation of the first 3 points. 1047741bfc07SMatthew G. Knepley 1048741bfc07SMatthew G. Knepley Not collective 1049741bfc07SMatthew G. Knepley 1050741bfc07SMatthew G. Knepley Input Parameter: 10516b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points) 1052741bfc07SMatthew G. Knepley 10536b867d5aSJose E. Roman Input/Output Parameter: 10546b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first 10556b867d5aSJose E. Roman 2*coordSize/3 entries contain interlaced 2D points, with the rest undefined 10566b867d5aSJose E. Roman 10576b867d5aSJose E. Roman Output Parameter: 10586b867d5aSJose 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. 1059741bfc07SMatthew G. Knepley 1060741bfc07SMatthew G. Knepley Level: developer 1061741bfc07SMatthew G. Knepley 1062db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto1D()` 1063741bfc07SMatthew G. Knepley @*/ 10649371c9d4SSatish Balay PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) { 1065c871b86eSJed Brown PetscReal x1[3], x2[3], n[3], c[3], norm; 1066ccd2543fSMatthew G Knepley const PetscInt dim = 3; 1067c871b86eSJed Brown PetscInt d, p; 1068ccd2543fSMatthew G Knepley 1069ccd2543fSMatthew G Knepley PetscFunctionBegin; 1070ccd2543fSMatthew G Knepley /* 0) Calculate normal vector */ 1071ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 10721ee9d5ecSMatthew G. Knepley x1[d] = PetscRealPart(coords[1 * dim + d] - coords[0 * dim + d]); 10731ee9d5ecSMatthew G. Knepley x2[d] = PetscRealPart(coords[2 * dim + d] - coords[0 * dim + d]); 1074ccd2543fSMatthew G Knepley } 1075c871b86eSJed Brown // n = x1 \otimes x2 1076ccd2543fSMatthew G Knepley n[0] = x1[1] * x2[2] - x1[2] * x2[1]; 1077ccd2543fSMatthew G Knepley n[1] = x1[2] * x2[0] - x1[0] * x2[2]; 1078ccd2543fSMatthew G Knepley n[2] = x1[0] * x2[1] - x1[1] * x2[0]; 10798b49ba18SBarry Smith norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]); 1080c871b86eSJed Brown for (d = 0; d < dim; d++) n[d] /= norm; 1081c871b86eSJed Brown norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]); 1082c871b86eSJed Brown for (d = 0; d < dim; d++) x1[d] /= norm; 1083c871b86eSJed Brown // x2 = n \otimes x1 1084c871b86eSJed Brown x2[0] = n[1] * x1[2] - n[2] * x1[1]; 1085c871b86eSJed Brown x2[1] = n[2] * x1[0] - n[0] * x1[2]; 1086c871b86eSJed Brown x2[2] = n[0] * x1[1] - n[1] * x1[0]; 1087c871b86eSJed Brown for (d = 0; d < dim; d++) { 1088c871b86eSJed Brown R[d * dim + 0] = x1[d]; 1089c871b86eSJed Brown R[d * dim + 1] = x2[d]; 1090c871b86eSJed Brown R[d * dim + 2] = n[d]; 1091c871b86eSJed Brown c[d] = PetscRealPart(coords[0 * dim + d]); 109273868372SMatthew G. Knepley } 1093c871b86eSJed Brown for (p = 0; p < coordSize / dim; p++) { 1094c871b86eSJed Brown PetscReal y[3]; 1095c871b86eSJed Brown for (d = 0; d < dim; d++) y[d] = PetscRealPart(coords[p * dim + d]) - c[d]; 1096c871b86eSJed 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]; 10977f07f362SMatthew G. Knepley } 1098ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1099ccd2543fSMatthew G Knepley } 1100ccd2543fSMatthew G Knepley 11016322fe33SJed Brown PETSC_UNUSED 11029371c9d4SSatish Balay static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) { 1103834e62ceSMatthew G. Knepley /* Signed volume is 1/2 the determinant 1104834e62ceSMatthew G. Knepley 1105834e62ceSMatthew G. Knepley | 1 1 1 | 1106834e62ceSMatthew G. Knepley | x0 x1 x2 | 1107834e62ceSMatthew G. Knepley | y0 y1 y2 | 1108834e62ceSMatthew G. Knepley 1109834e62ceSMatthew G. Knepley but if x0,y0 is the origin, we have 1110834e62ceSMatthew G. Knepley 1111834e62ceSMatthew G. Knepley | x1 x2 | 1112834e62ceSMatthew G. Knepley | y1 y2 | 1113834e62ceSMatthew G. Knepley */ 1114834e62ceSMatthew G. Knepley const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1]; 1115834e62ceSMatthew G. Knepley const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1]; 1116834e62ceSMatthew G. Knepley PetscReal M[4], detM; 11179371c9d4SSatish Balay M[0] = x1; 11189371c9d4SSatish Balay M[1] = x2; 11199371c9d4SSatish Balay M[2] = y1; 11209371c9d4SSatish Balay M[3] = y2; 1121923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(&detM, M); 1122834e62ceSMatthew G. Knepley *vol = 0.5 * detM; 11233bc0b13bSBarry Smith (void)PetscLogFlops(5.0); 1124834e62ceSMatthew G. Knepley } 1125834e62ceSMatthew G. Knepley 11266322fe33SJed Brown PETSC_UNUSED 11279371c9d4SSatish Balay static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) { 1128834e62ceSMatthew G. Knepley /* Signed volume is 1/6th of the determinant 1129834e62ceSMatthew G. Knepley 1130834e62ceSMatthew G. Knepley | 1 1 1 1 | 1131834e62ceSMatthew G. Knepley | x0 x1 x2 x3 | 1132834e62ceSMatthew G. Knepley | y0 y1 y2 y3 | 1133834e62ceSMatthew G. Knepley | z0 z1 z2 z3 | 1134834e62ceSMatthew G. Knepley 1135834e62ceSMatthew G. Knepley but if x0,y0,z0 is the origin, we have 1136834e62ceSMatthew G. Knepley 1137834e62ceSMatthew G. Knepley | x1 x2 x3 | 1138834e62ceSMatthew G. Knepley | y1 y2 y3 | 1139834e62ceSMatthew G. Knepley | z1 z2 z3 | 1140834e62ceSMatthew G. Knepley */ 1141834e62ceSMatthew G. Knepley const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2]; 1142834e62ceSMatthew G. Knepley const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2]; 1143834e62ceSMatthew G. Knepley const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2]; 11440a3da2c2SToby Isaac const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.); 1145834e62ceSMatthew G. Knepley PetscReal M[9], detM; 11469371c9d4SSatish Balay M[0] = x1; 11479371c9d4SSatish Balay M[1] = x2; 11489371c9d4SSatish Balay M[2] = x3; 11499371c9d4SSatish Balay M[3] = y1; 11509371c9d4SSatish Balay M[4] = y2; 11519371c9d4SSatish Balay M[5] = y3; 11529371c9d4SSatish Balay M[6] = z1; 11539371c9d4SSatish Balay M[7] = z2; 11549371c9d4SSatish Balay M[8] = z3; 1155923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(&detM, M); 11560a3da2c2SToby Isaac *vol = -onesixth * detM; 11573bc0b13bSBarry Smith (void)PetscLogFlops(10.0); 1158834e62ceSMatthew G. Knepley } 1159834e62ceSMatthew G. Knepley 11609371c9d4SSatish Balay static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) { 11610a3da2c2SToby Isaac const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.); 1162923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(vol, coords); 11630a3da2c2SToby Isaac *vol *= -onesixth; 11640ec8681fSMatthew G. Knepley } 11650ec8681fSMatthew G. Knepley 11669371c9d4SSatish Balay static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) { 1167cb92db44SToby Isaac PetscSection coordSection; 1168cb92db44SToby Isaac Vec coordinates; 1169cb92db44SToby Isaac const PetscScalar *coords; 1170cb92db44SToby Isaac PetscInt dim, d, off; 1171cb92db44SToby Isaac 1172cb92db44SToby Isaac PetscFunctionBegin; 11739566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 11749566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 11759566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, e, &dim)); 1176cb92db44SToby Isaac if (!dim) PetscFunctionReturn(0); 11779566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, e, &off)); 11789566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coords)); 11799371c9d4SSatish Balay if (v0) { 11809371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]); 11819371c9d4SSatish Balay } 11829566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coords)); 1183cb92db44SToby Isaac *detJ = 1.; 1184cb92db44SToby Isaac if (J) { 1185cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) J[d] = 0.; 1186cb92db44SToby Isaac for (d = 0; d < dim; d++) J[d * dim + d] = 1.; 1187cb92db44SToby Isaac if (invJ) { 1188cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) invJ[d] = 0.; 1189cb92db44SToby Isaac for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.; 1190cb92db44SToby Isaac } 1191cb92db44SToby Isaac } 1192cb92db44SToby Isaac PetscFunctionReturn(0); 1193cb92db44SToby Isaac } 1194cb92db44SToby Isaac 11956858538eSMatthew G. Knepley /*@C 11966858538eSMatthew G. Knepley DMPlexGetCellCoordinates - Get coordinates for a cell, taking into account periodicity 11976858538eSMatthew G. Knepley 11986858538eSMatthew G. Knepley Not collective 11996858538eSMatthew G. Knepley 12006858538eSMatthew G. Knepley Input Parameters: 12016858538eSMatthew G. Knepley + dm - The DM 12026858538eSMatthew G. Knepley - cell - The cell number 12036858538eSMatthew G. Knepley 12046858538eSMatthew G. Knepley Output Parameters: 12056858538eSMatthew G. Knepley + isDG - Using cellwise coordinates 12066858538eSMatthew G. Knepley . Nc - The number of coordinates 12076858538eSMatthew G. Knepley . array - The coordinate array 12086858538eSMatthew G. Knepley - coords - The cell coordinates 12096858538eSMatthew G. Knepley 12106858538eSMatthew G. Knepley Level: developer 12116858538eSMatthew G. Knepley 12126858538eSMatthew G. Knepley .seealso: DMPlexRestoreCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal() 12136858538eSMatthew G. Knepley @*/ 12149371c9d4SSatish Balay PetscErrorCode DMPlexGetCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[]) { 12156858538eSMatthew G. Knepley DM cdm; 12166858538eSMatthew G. Knepley Vec coordinates; 12176858538eSMatthew G. Knepley PetscSection cs; 12186858538eSMatthew G. Knepley const PetscScalar *ccoords; 12196858538eSMatthew G. Knepley PetscInt pStart, pEnd; 12206858538eSMatthew G. Knepley 12216858538eSMatthew G. Knepley PetscFunctionBeginHot; 12226858538eSMatthew G. Knepley *isDG = PETSC_FALSE; 12236858538eSMatthew G. Knepley *Nc = 0; 12246858538eSMatthew G. Knepley *array = NULL; 12256858538eSMatthew G. Knepley *coords = NULL; 12266858538eSMatthew G. Knepley /* Check for cellwise coordinates */ 12276858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateSection(dm, &cs)); 12286858538eSMatthew G. Knepley if (!cs) goto cg; 12296858538eSMatthew G. Knepley /* Check that the cell exists in the cellwise section */ 12306858538eSMatthew G. Knepley PetscCall(PetscSectionGetChart(cs, &pStart, &pEnd)); 12316858538eSMatthew G. Knepley if (cell < pStart || cell >= pEnd) goto cg; 12326858538eSMatthew G. Knepley /* Check for cellwise coordinates for this cell */ 12336858538eSMatthew G. Knepley PetscCall(PetscSectionGetDof(cs, cell, Nc)); 12346858538eSMatthew G. Knepley if (!*Nc) goto cg; 12356858538eSMatthew G. Knepley /* Check for cellwise coordinates */ 12366858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocalNoncollective(dm, &coordinates)); 12376858538eSMatthew G. Knepley if (!coordinates) goto cg; 12386858538eSMatthew G. Knepley /* Get cellwise coordinates */ 12396858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 12406858538eSMatthew G. Knepley PetscCall(VecGetArrayRead(coordinates, array)); 12416858538eSMatthew G. Knepley PetscCall(DMPlexPointLocalRead(cdm, cell, *array, &ccoords)); 12426858538eSMatthew G. Knepley PetscCall(DMGetWorkArray(cdm, *Nc, MPIU_SCALAR, coords)); 12436858538eSMatthew G. Knepley PetscCall(PetscArraycpy(*coords, ccoords, *Nc)); 12446858538eSMatthew G. Knepley PetscCall(VecRestoreArrayRead(coordinates, array)); 12456858538eSMatthew G. Knepley *isDG = PETSC_TRUE; 12466858538eSMatthew G. Knepley PetscFunctionReturn(0); 12476858538eSMatthew G. Knepley cg: 12486858538eSMatthew G. Knepley /* Use continuous coordinates */ 12496858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 12506858538eSMatthew G. Knepley PetscCall(DMGetCoordinateSection(dm, &cs)); 12516858538eSMatthew G. Knepley PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates)); 12526858538eSMatthew G. Knepley PetscCall(DMPlexVecGetClosure(cdm, cs, coordinates, cell, Nc, coords)); 12536858538eSMatthew G. Knepley PetscFunctionReturn(0); 12546858538eSMatthew G. Knepley } 12556858538eSMatthew G. Knepley 12566858538eSMatthew G. Knepley /*@C 12576858538eSMatthew G. Knepley DMPlexRestoreCellCoordinates - Get coordinates for a cell, taking into account periodicity 12586858538eSMatthew G. Knepley 12596858538eSMatthew G. Knepley Not collective 12606858538eSMatthew G. Knepley 12616858538eSMatthew G. Knepley Input Parameters: 12626858538eSMatthew G. Knepley + dm - The DM 12636858538eSMatthew G. Knepley - cell - The cell number 12646858538eSMatthew G. Knepley 12656858538eSMatthew G. Knepley Output Parameters: 12666858538eSMatthew G. Knepley + isDG - Using cellwise coordinates 12676858538eSMatthew G. Knepley . Nc - The number of coordinates 12686858538eSMatthew G. Knepley . array - The coordinate array 12696858538eSMatthew G. Knepley - coords - The cell coordinates 12706858538eSMatthew G. Knepley 12716858538eSMatthew G. Knepley Level: developer 12726858538eSMatthew G. Knepley 12736858538eSMatthew G. Knepley .seealso: DMPlexGetCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal() 12746858538eSMatthew G. Knepley @*/ 12759371c9d4SSatish Balay PetscErrorCode DMPlexRestoreCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[]) { 12766858538eSMatthew G. Knepley DM cdm; 12776858538eSMatthew G. Knepley PetscSection cs; 12786858538eSMatthew G. Knepley Vec coordinates; 12796858538eSMatthew G. Knepley 12806858538eSMatthew G. Knepley PetscFunctionBeginHot; 12816858538eSMatthew G. Knepley if (*isDG) { 12826858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 12836858538eSMatthew G. Knepley PetscCall(DMRestoreWorkArray(cdm, *Nc, MPIU_SCALAR, coords)); 12846858538eSMatthew G. Knepley } else { 12856858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 12866858538eSMatthew G. Knepley PetscCall(DMGetCoordinateSection(dm, &cs)); 12876858538eSMatthew G. Knepley PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates)); 12886858538eSMatthew G. Knepley PetscCall(DMPlexVecRestoreClosure(cdm, cs, coordinates, cell, Nc, (PetscScalar **)coords)); 12896858538eSMatthew G. Knepley } 12906858538eSMatthew G. Knepley PetscFunctionReturn(0); 12916858538eSMatthew G. Knepley } 12926858538eSMatthew G. Knepley 12939371c9d4SSatish Balay static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) { 12946858538eSMatthew G. Knepley const PetscScalar *array; 1295a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 12966858538eSMatthew G. Knepley PetscInt numCoords, d; 12976858538eSMatthew G. Knepley PetscBool isDG; 129817fe8556SMatthew G. Knepley 129917fe8556SMatthew G. Knepley PetscFunctionBegin; 13006858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 130108401ef6SPierre Jolivet PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 13027f07f362SMatthew G. Knepley *detJ = 0.0; 130328dbe442SToby Isaac if (numCoords == 6) { 130428dbe442SToby Isaac const PetscInt dim = 3; 130528dbe442SToby Isaac PetscReal R[9], J0; 130628dbe442SToby Isaac 13079371c9d4SSatish Balay if (v0) { 13089371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 13099371c9d4SSatish Balay } 13109566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto1D(coords, R)); 131128dbe442SToby Isaac if (J) { 131228dbe442SToby Isaac J0 = 0.5 * PetscRealPart(coords[1]); 13139371c9d4SSatish Balay J[0] = R[0] * J0; 13149371c9d4SSatish Balay J[1] = R[1]; 13159371c9d4SSatish Balay J[2] = R[2]; 13169371c9d4SSatish Balay J[3] = R[3] * J0; 13179371c9d4SSatish Balay J[4] = R[4]; 13189371c9d4SSatish Balay J[5] = R[5]; 13199371c9d4SSatish Balay J[6] = R[6] * J0; 13209371c9d4SSatish Balay J[7] = R[7]; 13219371c9d4SSatish Balay J[8] = R[8]; 132228dbe442SToby Isaac DMPlex_Det3D_Internal(detJ, J); 132328dbe442SToby Isaac if (invJ) { DMPlex_Invert2D_Internal(invJ, J, *detJ); } 1324adac9986SMatthew G. Knepley } 132528dbe442SToby Isaac } else if (numCoords == 4) { 13267f07f362SMatthew G. Knepley const PetscInt dim = 2; 13277f07f362SMatthew G. Knepley PetscReal R[4], J0; 13287f07f362SMatthew G. Knepley 13299371c9d4SSatish Balay if (v0) { 13309371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 13319371c9d4SSatish Balay } 13329566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection2Dto1D(coords, R)); 133317fe8556SMatthew G. Knepley if (J) { 13347f07f362SMatthew G. Knepley J0 = 0.5 * PetscRealPart(coords[1]); 13359371c9d4SSatish Balay J[0] = R[0] * J0; 13369371c9d4SSatish Balay J[1] = R[1]; 13379371c9d4SSatish Balay J[2] = R[2] * J0; 13389371c9d4SSatish Balay J[3] = R[3]; 1339923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1340923591dfSMatthew G. Knepley if (invJ) { DMPlex_Invert2D_Internal(invJ, J, *detJ); } 1341adac9986SMatthew G. Knepley } 13427f07f362SMatthew G. Knepley } else if (numCoords == 2) { 13437f07f362SMatthew G. Knepley const PetscInt dim = 1; 13447f07f362SMatthew G. Knepley 13459371c9d4SSatish Balay if (v0) { 13469371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 13479371c9d4SSatish Balay } 13487f07f362SMatthew G. Knepley if (J) { 13497f07f362SMatthew G. Knepley J[0] = 0.5 * (PetscRealPart(coords[1]) - PetscRealPart(coords[0])); 135017fe8556SMatthew G. Knepley *detJ = J[0]; 13519566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0)); 13529371c9d4SSatish Balay if (invJ) { 13539371c9d4SSatish Balay invJ[0] = 1.0 / J[0]; 13549371c9d4SSatish Balay PetscCall(PetscLogFlops(1.0)); 13559371c9d4SSatish Balay } 1356adac9986SMatthew G. Knepley } 13576858538eSMatthew G. Knepley } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for segment %" PetscInt_FMT " is %" PetscInt_FMT " != 2 or 4 or 6", e, numCoords); 13586858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 135917fe8556SMatthew G. Knepley PetscFunctionReturn(0); 136017fe8556SMatthew G. Knepley } 136117fe8556SMatthew G. Knepley 13629371c9d4SSatish Balay static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) { 13636858538eSMatthew G. Knepley const PetscScalar *array; 1364a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 13656858538eSMatthew G. Knepley PetscInt numCoords, d; 13666858538eSMatthew G. Knepley PetscBool isDG; 1367ccd2543fSMatthew G Knepley 1368ccd2543fSMatthew G Knepley PetscFunctionBegin; 13696858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 13706858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 13717f07f362SMatthew G. Knepley *detJ = 0.0; 1372ccd2543fSMatthew G Knepley if (numCoords == 9) { 13737f07f362SMatthew G. Knepley const PetscInt dim = 3; 13747f07f362SMatthew 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}; 13757f07f362SMatthew G. Knepley 13769371c9d4SSatish Balay if (v0) { 13779371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 13789371c9d4SSatish Balay } 13799566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R)); 13807f07f362SMatthew G. Knepley if (J) { 1381b7ad821dSMatthew G. Knepley const PetscInt pdim = 2; 1382b7ad821dSMatthew G. Knepley 1383b7ad821dSMatthew G. Knepley for (d = 0; d < pdim; d++) { 13849371c9d4SSatish Balay for (PetscInt f = 0; f < pdim; f++) { J0[d * dim + f] = 0.5 * (PetscRealPart(coords[(f + 1) * pdim + d]) - PetscRealPart(coords[0 * pdim + d])); } 13857f07f362SMatthew G. Knepley } 13869566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1387923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 13887f07f362SMatthew G. Knepley for (d = 0; d < dim; d++) { 13896858538eSMatthew G. Knepley for (PetscInt f = 0; f < dim; f++) { 13907f07f362SMatthew G. Knepley J[d * dim + f] = 0.0; 13919371c9d4SSatish Balay for (PetscInt g = 0; g < dim; g++) { J[d * dim + f] += R[d * dim + g] * J0[g * dim + f]; } 13927f07f362SMatthew G. Knepley } 13937f07f362SMatthew G. Knepley } 13949566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 13957f07f362SMatthew G. Knepley } 1396923591dfSMatthew G. Knepley if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); } 13977f07f362SMatthew G. Knepley } else if (numCoords == 6) { 13987f07f362SMatthew G. Knepley const PetscInt dim = 2; 13997f07f362SMatthew G. Knepley 14009371c9d4SSatish Balay if (v0) { 14019371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 14029371c9d4SSatish Balay } 1403ccd2543fSMatthew G Knepley if (J) { 1404ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 14059371c9d4SSatish Balay for (PetscInt f = 0; f < dim; f++) { J[d * dim + f] = 0.5 * (PetscRealPart(coords[(f + 1) * dim + d]) - PetscRealPart(coords[0 * dim + d])); } 1406ccd2543fSMatthew G Knepley } 14079566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1408923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1409ccd2543fSMatthew G Knepley } 1410923591dfSMatthew G. Knepley if (invJ) { DMPlex_Invert2D_Internal(invJ, J, *detJ); } 141163a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %" PetscInt_FMT " != 6 or 9", numCoords); 14126858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 1413ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1414ccd2543fSMatthew G Knepley } 1415ccd2543fSMatthew G Knepley 14169371c9d4SSatish Balay static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) { 14176858538eSMatthew G. Knepley const PetscScalar *array; 1418a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 14196858538eSMatthew G. Knepley PetscInt numCoords, d; 14206858538eSMatthew G. Knepley PetscBool isDG; 1421ccd2543fSMatthew G Knepley 1422ccd2543fSMatthew G Knepley PetscFunctionBegin; 14236858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 14246858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 1425dfccc68fSToby Isaac if (!Nq) { 1426412e9a14SMatthew G. Knepley PetscInt vorder[4] = {0, 1, 2, 3}; 1427412e9a14SMatthew G. Knepley 14289371c9d4SSatish Balay if (isTensor) { 14299371c9d4SSatish Balay vorder[2] = 3; 14309371c9d4SSatish Balay vorder[3] = 2; 14319371c9d4SSatish Balay } 14327f07f362SMatthew G. Knepley *detJ = 0.0; 143399dec3a6SMatthew G. Knepley if (numCoords == 12) { 143499dec3a6SMatthew G. Knepley const PetscInt dim = 3; 143599dec3a6SMatthew 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}; 143699dec3a6SMatthew G. Knepley 14379371c9d4SSatish Balay if (v) { 14389371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 14399371c9d4SSatish Balay } 14409566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R)); 144199dec3a6SMatthew G. Knepley if (J) { 144299dec3a6SMatthew G. Knepley const PetscInt pdim = 2; 144399dec3a6SMatthew G. Knepley 144499dec3a6SMatthew G. Knepley for (d = 0; d < pdim; d++) { 1445412e9a14SMatthew G. Knepley J0[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * pdim + d]) - PetscRealPart(coords[vorder[0] * pdim + d])); 1446412e9a14SMatthew G. Knepley J0[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[2] * pdim + d]) - PetscRealPart(coords[vorder[1] * pdim + d])); 144799dec3a6SMatthew G. Knepley } 14489566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1449923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 145099dec3a6SMatthew G. Knepley for (d = 0; d < dim; d++) { 14516858538eSMatthew G. Knepley for (PetscInt f = 0; f < dim; f++) { 145299dec3a6SMatthew G. Knepley J[d * dim + f] = 0.0; 14539371c9d4SSatish Balay for (PetscInt g = 0; g < dim; g++) { J[d * dim + f] += R[d * dim + g] * J0[g * dim + f]; } 145499dec3a6SMatthew G. Knepley } 145599dec3a6SMatthew G. Knepley } 14569566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 145799dec3a6SMatthew G. Knepley } 1458923591dfSMatthew G. Knepley if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); } 145971f58de1SToby Isaac } else if (numCoords == 8) { 146099dec3a6SMatthew G. Knepley const PetscInt dim = 2; 146199dec3a6SMatthew G. Knepley 14629371c9d4SSatish Balay if (v) { 14639371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 14649371c9d4SSatish Balay } 1465ccd2543fSMatthew G Knepley if (J) { 1466ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1467412e9a14SMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d])); 1468412e9a14SMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[3] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d])); 1469ccd2543fSMatthew G Knepley } 14709566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1471923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1472ccd2543fSMatthew G Knepley } 1473923591dfSMatthew G. Knepley if (invJ) { DMPlex_Invert2D_Internal(invJ, J, *detJ); } 147463a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords); 1475dfccc68fSToby Isaac } else { 1476dfccc68fSToby Isaac const PetscInt Nv = 4; 1477dfccc68fSToby Isaac const PetscInt dimR = 2; 1478412e9a14SMatthew G. Knepley PetscInt zToPlex[4] = {0, 1, 3, 2}; 1479dfccc68fSToby Isaac PetscReal zOrder[12]; 1480dfccc68fSToby Isaac PetscReal zCoeff[12]; 1481dfccc68fSToby Isaac PetscInt i, j, k, l, dim; 1482dfccc68fSToby Isaac 14839371c9d4SSatish Balay if (isTensor) { 14849371c9d4SSatish Balay zToPlex[2] = 2; 14859371c9d4SSatish Balay zToPlex[3] = 3; 14869371c9d4SSatish Balay } 1487dfccc68fSToby Isaac if (numCoords == 12) { 1488dfccc68fSToby Isaac dim = 3; 1489dfccc68fSToby Isaac } else if (numCoords == 8) { 1490dfccc68fSToby Isaac dim = 2; 149163a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords); 1492dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 1493dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 1494dfccc68fSToby Isaac 14959371c9d4SSatish Balay for (j = 0; j < dim; j++) { zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); } 1496dfccc68fSToby Isaac } 1497dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 14982df84da0SMatthew G. Knepley /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta): 14992df84da0SMatthew G. Knepley \phi^0 = (1 - xi - eta + xi eta) --> 1 = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3) 15002df84da0SMatthew G. Knepley \phi^1 = (1 + xi - eta - xi eta) --> xi = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3) 15012df84da0SMatthew G. Knepley \phi^2 = (1 - xi + eta - xi eta) --> eta = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3) 15022df84da0SMatthew G. Knepley \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3) 15032df84da0SMatthew G. Knepley */ 1504dfccc68fSToby Isaac zCoeff[dim * 0 + j] = 0.25 * (zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1505dfccc68fSToby Isaac zCoeff[dim * 1 + j] = 0.25 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1506dfccc68fSToby Isaac zCoeff[dim * 2 + j] = 0.25 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1507dfccc68fSToby Isaac zCoeff[dim * 3 + j] = 0.25 * (zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1508dfccc68fSToby Isaac } 1509dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1510dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1]; 1511dfccc68fSToby Isaac 1512dfccc68fSToby Isaac if (v) { 1513dfccc68fSToby Isaac PetscReal extPoint[4]; 1514dfccc68fSToby Isaac 1515dfccc68fSToby Isaac extPoint[0] = 1.; 1516dfccc68fSToby Isaac extPoint[1] = xi; 1517dfccc68fSToby Isaac extPoint[2] = eta; 1518dfccc68fSToby Isaac extPoint[3] = xi * eta; 1519dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1520dfccc68fSToby Isaac PetscReal val = 0.; 1521dfccc68fSToby Isaac 15229371c9d4SSatish Balay for (k = 0; k < Nv; k++) { val += extPoint[k] * zCoeff[dim * k + j]; } 1523dfccc68fSToby Isaac v[i * dim + j] = val; 1524dfccc68fSToby Isaac } 1525dfccc68fSToby Isaac } 1526dfccc68fSToby Isaac if (J) { 1527dfccc68fSToby Isaac PetscReal extJ[8]; 1528dfccc68fSToby Isaac 1529dfccc68fSToby Isaac extJ[0] = 0.; 1530dfccc68fSToby Isaac extJ[1] = 0.; 1531dfccc68fSToby Isaac extJ[2] = 1.; 1532dfccc68fSToby Isaac extJ[3] = 0.; 1533dfccc68fSToby Isaac extJ[4] = 0.; 1534dfccc68fSToby Isaac extJ[5] = 1.; 1535dfccc68fSToby Isaac extJ[6] = eta; 1536dfccc68fSToby Isaac extJ[7] = xi; 1537dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1538dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 1539dfccc68fSToby Isaac PetscReal val = 0.; 1540dfccc68fSToby Isaac 15419371c9d4SSatish Balay for (l = 0; l < Nv; l++) { val += zCoeff[dim * l + j] * extJ[dimR * l + k]; } 1542dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 1543dfccc68fSToby Isaac } 1544dfccc68fSToby Isaac } 1545dfccc68fSToby Isaac if (dim == 3) { /* put the cross product in the third component of the Jacobian */ 1546dfccc68fSToby Isaac PetscReal x, y, z; 1547dfccc68fSToby Isaac PetscReal *iJ = &J[i * dim * dim]; 1548dfccc68fSToby Isaac PetscReal norm; 1549dfccc68fSToby Isaac 1550dfccc68fSToby Isaac x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0]; 1551dfccc68fSToby Isaac y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1]; 1552dfccc68fSToby Isaac z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0]; 1553dfccc68fSToby Isaac norm = PetscSqrtReal(x * x + y * y + z * z); 1554dfccc68fSToby Isaac iJ[2] = x / norm; 1555dfccc68fSToby Isaac iJ[5] = y / norm; 1556dfccc68fSToby Isaac iJ[8] = z / norm; 1557dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 1558dfccc68fSToby Isaac if (invJ) { DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); } 1559dfccc68fSToby Isaac } else { 1560dfccc68fSToby Isaac DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]); 1561dfccc68fSToby Isaac if (invJ) { DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); } 1562dfccc68fSToby Isaac } 1563dfccc68fSToby Isaac } 1564dfccc68fSToby Isaac } 1565dfccc68fSToby Isaac } 15666858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 1567ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1568ccd2543fSMatthew G Knepley } 1569ccd2543fSMatthew G Knepley 15709371c9d4SSatish Balay static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) { 15716858538eSMatthew G. Knepley const PetscScalar *array; 1572a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1573ccd2543fSMatthew G Knepley const PetscInt dim = 3; 15746858538eSMatthew G. Knepley PetscInt numCoords, d; 15756858538eSMatthew G. Knepley PetscBool isDG; 1576ccd2543fSMatthew G Knepley 1577ccd2543fSMatthew G Knepley PetscFunctionBegin; 15786858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 15796858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 15807f07f362SMatthew G. Knepley *detJ = 0.0; 15819371c9d4SSatish Balay if (v0) { 15829371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 15839371c9d4SSatish Balay } 1584ccd2543fSMatthew G Knepley if (J) { 1585ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1586f0df753eSMatthew G. Knepley /* I orient with outward face normals */ 1587f0df753eSMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1588f0df753eSMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1589f0df753eSMatthew G. Knepley J[d * dim + 2] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1590ccd2543fSMatthew G Knepley } 15919566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 1592923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1593ccd2543fSMatthew G Knepley } 1594923591dfSMatthew G. Knepley if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); } 15956858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 1596ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1597ccd2543fSMatthew G Knepley } 1598ccd2543fSMatthew G Knepley 15999371c9d4SSatish Balay static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) { 16006858538eSMatthew G. Knepley const PetscScalar *array; 1601a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1602ccd2543fSMatthew G Knepley const PetscInt dim = 3; 16036858538eSMatthew G. Knepley PetscInt numCoords, d; 16046858538eSMatthew G. Knepley PetscBool isDG; 1605ccd2543fSMatthew G Knepley 1606ccd2543fSMatthew G Knepley PetscFunctionBegin; 16076858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 16086858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 1609dfccc68fSToby Isaac if (!Nq) { 16107f07f362SMatthew G. Knepley *detJ = 0.0; 16119371c9d4SSatish Balay if (v) { 16129371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 16139371c9d4SSatish Balay } 1614ccd2543fSMatthew G Knepley if (J) { 1615ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1616f0df753eSMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1617f0df753eSMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1618f0df753eSMatthew G. Knepley J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1619ccd2543fSMatthew G Knepley } 16209566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 1621923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1622ccd2543fSMatthew G Knepley } 1623923591dfSMatthew G. Knepley if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); } 1624dfccc68fSToby Isaac } else { 1625dfccc68fSToby Isaac const PetscInt Nv = 8; 1626dfccc68fSToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 1627dfccc68fSToby Isaac const PetscInt dim = 3; 1628dfccc68fSToby Isaac const PetscInt dimR = 3; 1629dfccc68fSToby Isaac PetscReal zOrder[24]; 1630dfccc68fSToby Isaac PetscReal zCoeff[24]; 1631dfccc68fSToby Isaac PetscInt i, j, k, l; 1632dfccc68fSToby Isaac 1633dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 1634dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 1635dfccc68fSToby Isaac 16369371c9d4SSatish Balay for (j = 0; j < dim; j++) { zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); } 1637dfccc68fSToby Isaac } 1638dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1639dfccc68fSToby 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]); 1640dfccc68fSToby 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]); 1641dfccc68fSToby 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]); 1642dfccc68fSToby 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]); 1643dfccc68fSToby 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]); 1644dfccc68fSToby 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]); 1645dfccc68fSToby 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]); 1646dfccc68fSToby 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]); 1647dfccc68fSToby Isaac } 1648dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1649dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2]; 1650dfccc68fSToby Isaac 1651dfccc68fSToby Isaac if (v) { 165291d2b7ceSToby Isaac PetscReal extPoint[8]; 1653dfccc68fSToby Isaac 1654dfccc68fSToby Isaac extPoint[0] = 1.; 1655dfccc68fSToby Isaac extPoint[1] = xi; 1656dfccc68fSToby Isaac extPoint[2] = eta; 1657dfccc68fSToby Isaac extPoint[3] = xi * eta; 1658dfccc68fSToby Isaac extPoint[4] = theta; 1659dfccc68fSToby Isaac extPoint[5] = theta * xi; 1660dfccc68fSToby Isaac extPoint[6] = theta * eta; 1661dfccc68fSToby Isaac extPoint[7] = theta * eta * xi; 1662dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1663dfccc68fSToby Isaac PetscReal val = 0.; 1664dfccc68fSToby Isaac 16659371c9d4SSatish Balay for (k = 0; k < Nv; k++) { val += extPoint[k] * zCoeff[dim * k + j]; } 1666dfccc68fSToby Isaac v[i * dim + j] = val; 1667dfccc68fSToby Isaac } 1668dfccc68fSToby Isaac } 1669dfccc68fSToby Isaac if (J) { 1670dfccc68fSToby Isaac PetscReal extJ[24]; 1671dfccc68fSToby Isaac 16729371c9d4SSatish Balay extJ[0] = 0.; 16739371c9d4SSatish Balay extJ[1] = 0.; 16749371c9d4SSatish Balay extJ[2] = 0.; 16759371c9d4SSatish Balay extJ[3] = 1.; 16769371c9d4SSatish Balay extJ[4] = 0.; 16779371c9d4SSatish Balay extJ[5] = 0.; 16789371c9d4SSatish Balay extJ[6] = 0.; 16799371c9d4SSatish Balay extJ[7] = 1.; 16809371c9d4SSatish Balay extJ[8] = 0.; 16819371c9d4SSatish Balay extJ[9] = eta; 16829371c9d4SSatish Balay extJ[10] = xi; 16839371c9d4SSatish Balay extJ[11] = 0.; 16849371c9d4SSatish Balay extJ[12] = 0.; 16859371c9d4SSatish Balay extJ[13] = 0.; 16869371c9d4SSatish Balay extJ[14] = 1.; 16879371c9d4SSatish Balay extJ[15] = theta; 16889371c9d4SSatish Balay extJ[16] = 0.; 16899371c9d4SSatish Balay extJ[17] = xi; 16909371c9d4SSatish Balay extJ[18] = 0.; 16919371c9d4SSatish Balay extJ[19] = theta; 16929371c9d4SSatish Balay extJ[20] = eta; 16939371c9d4SSatish Balay extJ[21] = theta * eta; 16949371c9d4SSatish Balay extJ[22] = theta * xi; 16959371c9d4SSatish Balay extJ[23] = eta * xi; 1696dfccc68fSToby Isaac 1697dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1698dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 1699dfccc68fSToby Isaac PetscReal val = 0.; 1700dfccc68fSToby Isaac 17019371c9d4SSatish Balay for (l = 0; l < Nv; l++) { val += zCoeff[dim * l + j] * extJ[dimR * l + k]; } 1702dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 1703dfccc68fSToby Isaac } 1704dfccc68fSToby Isaac } 1705dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 1706dfccc68fSToby Isaac if (invJ) { DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); } 1707dfccc68fSToby Isaac } 1708dfccc68fSToby Isaac } 1709dfccc68fSToby Isaac } 17106858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 1711ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 1712ccd2543fSMatthew G Knepley } 1713ccd2543fSMatthew G Knepley 17149371c9d4SSatish Balay static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) { 17156858538eSMatthew G. Knepley const PetscScalar *array; 17162df84da0SMatthew G. Knepley PetscScalar *coords = NULL; 17172df84da0SMatthew G. Knepley const PetscInt dim = 3; 17186858538eSMatthew G. Knepley PetscInt numCoords, d; 17196858538eSMatthew G. Knepley PetscBool isDG; 17202df84da0SMatthew G. Knepley 17212df84da0SMatthew G. Knepley PetscFunctionBegin; 17226858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 17236858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 17242df84da0SMatthew G. Knepley if (!Nq) { 17252df84da0SMatthew G. Knepley /* Assume that the map to the reference is affine */ 17262df84da0SMatthew G. Knepley *detJ = 0.0; 17279371c9d4SSatish Balay if (v) { 17289371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 17299371c9d4SSatish Balay } 17302df84da0SMatthew G. Knepley if (J) { 17312df84da0SMatthew G. Knepley for (d = 0; d < dim; d++) { 17322df84da0SMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 17332df84da0SMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 17342df84da0SMatthew G. Knepley J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 17352df84da0SMatthew G. Knepley } 17369566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 17372df84da0SMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 17382df84da0SMatthew G. Knepley } 17392df84da0SMatthew G. Knepley if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); } 17402df84da0SMatthew G. Knepley } else { 17412df84da0SMatthew G. Knepley const PetscInt dim = 3; 17422df84da0SMatthew G. Knepley const PetscInt dimR = 3; 17432df84da0SMatthew G. Knepley const PetscInt Nv = 6; 17442df84da0SMatthew G. Knepley PetscReal verts[18]; 17452df84da0SMatthew G. Knepley PetscReal coeff[18]; 17462df84da0SMatthew G. Knepley PetscInt i, j, k, l; 17472df84da0SMatthew G. Knepley 17489371c9d4SSatish Balay for (i = 0; i < Nv; ++i) 17499371c9d4SSatish Balay for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]); 17502df84da0SMatthew G. Knepley for (j = 0; j < dim; ++j) { 17512df84da0SMatthew G. Knepley /* Check for triangle, 17522df84da0SMatthew 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) 17532df84da0SMatthew 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) 17542df84da0SMatthew G. Knepley phi^2 = 1/2 (1 + eta) chi^2 = delta(-1, 1) 17552df84da0SMatthew G. Knepley 17562df84da0SMatthew G. Knepley phi^0 + phi^1 + phi^2 = 1 coef_1 = 1/2 ( chi^1 + chi^2) 17572df84da0SMatthew G. Knepley -phi^0 + phi^1 - phi^2 = xi coef_xi = 1/2 (-chi^0 + chi^1) 17582df84da0SMatthew G. Knepley -phi^0 - phi^1 + phi^2 = eta coef_eta = 1/2 (-chi^0 + chi^2) 17592df84da0SMatthew G. Knepley 17602df84da0SMatthew G. Knepley < chi_0 chi_1 chi_2> A / 1 1 1 \ / phi_0 \ <chi> I <phi>^T so we need the inverse transpose 17612df84da0SMatthew G. Knepley | -1 1 -1 | | phi_1 | = 17622df84da0SMatthew G. Knepley \ -1 -1 1 / \ phi_2 / 17632df84da0SMatthew G. Knepley 17642df84da0SMatthew 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 17652df84da0SMatthew G. Knepley */ 17662df84da0SMatthew G. Knepley /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta): 17672df84da0SMatthew G. Knepley \phi^0 = 1/4 ( -xi - eta + xi zeta + eta zeta) --> / 1 1 1 1 1 1 \ 1 17682df84da0SMatthew G. Knepley \phi^1 = 1/4 (1 + eta - zeta - eta zeta) --> | -1 1 -1 -1 -1 1 | eta 17692df84da0SMatthew G. Knepley \phi^2 = 1/4 (1 + xi - zeta - xi zeta) --> | -1 -1 1 -1 1 -1 | xi 17702df84da0SMatthew G. Knepley \phi^3 = 1/4 ( -xi - eta - xi zeta - eta zeta) --> | -1 -1 -1 1 1 1 | zeta 17712df84da0SMatthew G. Knepley \phi^4 = 1/4 (1 + xi + zeta + xi zeta) --> | 1 1 -1 -1 1 -1 | xi zeta 17722df84da0SMatthew G. Knepley \phi^5 = 1/4 (1 + eta + zeta + eta zeta) --> \ 1 -1 1 -1 -1 1 / eta zeta 17732df84da0SMatthew G. Knepley 1/4 / 0 1 1 0 1 1 \ 17742df84da0SMatthew G. Knepley | -1 1 0 -1 0 1 | 17752df84da0SMatthew G. Knepley | -1 0 1 -1 1 0 | 17762df84da0SMatthew G. Knepley | 0 -1 -1 0 1 1 | 17772df84da0SMatthew G. Knepley | 1 0 -1 -1 1 0 | 17782df84da0SMatthew G. Knepley \ 1 -1 0 -1 0 1 / 17792df84da0SMatthew G. Knepley */ 17802df84da0SMatthew G. Knepley coeff[dim * 0 + j] = (1. / 4.) * (verts[dim * 1 + j] + verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]); 17812df84da0SMatthew G. Knepley coeff[dim * 1 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]); 17822df84da0SMatthew G. Knepley coeff[dim * 2 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]); 17832df84da0SMatthew G. Knepley coeff[dim * 3 + j] = (1. / 4.) * (-verts[dim * 1 + j] - verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]); 17842df84da0SMatthew G. Knepley coeff[dim * 4 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]); 17852df84da0SMatthew G. Knepley coeff[dim * 5 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]); 17862df84da0SMatthew G. Knepley /* For reference prism: 17872df84da0SMatthew G. Knepley {0, 0, 0} 17882df84da0SMatthew G. Knepley {0, 1, 0} 17892df84da0SMatthew G. Knepley {1, 0, 0} 17902df84da0SMatthew G. Knepley {0, 0, 1} 17912df84da0SMatthew G. Knepley {0, 0, 0} 17922df84da0SMatthew G. Knepley {0, 0, 0} 17932df84da0SMatthew G. Knepley */ 17942df84da0SMatthew G. Knepley } 17952df84da0SMatthew G. Knepley for (i = 0; i < Nq; ++i) { 17962df84da0SMatthew G. Knepley const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2]; 17972df84da0SMatthew G. Knepley 17982df84da0SMatthew G. Knepley if (v) { 17992df84da0SMatthew G. Knepley PetscReal extPoint[6]; 18002df84da0SMatthew G. Knepley PetscInt c; 18012df84da0SMatthew G. Knepley 18022df84da0SMatthew G. Knepley extPoint[0] = 1.; 18032df84da0SMatthew G. Knepley extPoint[1] = eta; 18042df84da0SMatthew G. Knepley extPoint[2] = xi; 18052df84da0SMatthew G. Knepley extPoint[3] = zeta; 18062df84da0SMatthew G. Knepley extPoint[4] = xi * zeta; 18072df84da0SMatthew G. Knepley extPoint[5] = eta * zeta; 18082df84da0SMatthew G. Knepley for (c = 0; c < dim; ++c) { 18092df84da0SMatthew G. Knepley PetscReal val = 0.; 18102df84da0SMatthew G. Knepley 18119371c9d4SSatish Balay for (k = 0; k < Nv; ++k) { val += extPoint[k] * coeff[k * dim + c]; } 18122df84da0SMatthew G. Knepley v[i * dim + c] = val; 18132df84da0SMatthew G. Knepley } 18142df84da0SMatthew G. Knepley } 18152df84da0SMatthew G. Knepley if (J) { 18162df84da0SMatthew G. Knepley PetscReal extJ[18]; 18172df84da0SMatthew G. Knepley 18189371c9d4SSatish Balay extJ[0] = 0.; 18199371c9d4SSatish Balay extJ[1] = 0.; 18209371c9d4SSatish Balay extJ[2] = 0.; 18219371c9d4SSatish Balay extJ[3] = 0.; 18229371c9d4SSatish Balay extJ[4] = 1.; 18239371c9d4SSatish Balay extJ[5] = 0.; 18249371c9d4SSatish Balay extJ[6] = 1.; 18259371c9d4SSatish Balay extJ[7] = 0.; 18269371c9d4SSatish Balay extJ[8] = 0.; 18279371c9d4SSatish Balay extJ[9] = 0.; 18289371c9d4SSatish Balay extJ[10] = 0.; 18299371c9d4SSatish Balay extJ[11] = 1.; 18309371c9d4SSatish Balay extJ[12] = zeta; 18319371c9d4SSatish Balay extJ[13] = 0.; 18329371c9d4SSatish Balay extJ[14] = xi; 18339371c9d4SSatish Balay extJ[15] = 0.; 18349371c9d4SSatish Balay extJ[16] = zeta; 18359371c9d4SSatish Balay extJ[17] = eta; 18362df84da0SMatthew G. Knepley 18372df84da0SMatthew G. Knepley for (j = 0; j < dim; j++) { 18382df84da0SMatthew G. Knepley for (k = 0; k < dimR; k++) { 18392df84da0SMatthew G. Knepley PetscReal val = 0.; 18402df84da0SMatthew G. Knepley 18419371c9d4SSatish Balay for (l = 0; l < Nv; l++) { val += coeff[dim * l + j] * extJ[dimR * l + k]; } 18422df84da0SMatthew G. Knepley J[i * dim * dim + dim * j + k] = val; 18432df84da0SMatthew G. Knepley } 18442df84da0SMatthew G. Knepley } 18452df84da0SMatthew G. Knepley DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 18462df84da0SMatthew G. Knepley if (invJ) { DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); } 18472df84da0SMatthew G. Knepley } 18482df84da0SMatthew G. Knepley } 18492df84da0SMatthew G. Knepley } 18506858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 18512df84da0SMatthew G. Knepley PetscFunctionReturn(0); 18522df84da0SMatthew G. Knepley } 18532df84da0SMatthew G. Knepley 18549371c9d4SSatish Balay static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) { 1855ba2698f1SMatthew G. Knepley DMPolytopeType ct; 1856dfccc68fSToby Isaac PetscInt depth, dim, coordDim, coneSize, i; 1857dfccc68fSToby Isaac PetscInt Nq = 0; 1858dfccc68fSToby Isaac const PetscReal *points = NULL; 1859dfccc68fSToby Isaac DMLabel depthLabel; 1860c330f8ffSToby Isaac PetscReal xi0[3] = {-1., -1., -1.}, v0[3], J0[9], detJ0; 1861dfccc68fSToby Isaac PetscBool isAffine = PETSC_TRUE; 1862dfccc68fSToby Isaac 1863dfccc68fSToby Isaac PetscFunctionBegin; 18649566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 18659566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 18669566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dm, &depthLabel)); 18679566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(depthLabel, cell, &dim)); 1868*48a46eb9SPierre Jolivet if (depth == 1 && dim == 1) PetscCall(DMGetDimension(dm, &dim)); 18699566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &coordDim)); 187063a3b9bcSJacob Faibussowitsch PetscCheck(coordDim <= 3, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %" PetscInt_FMT " > 3", coordDim); 18719566063dSJacob Faibussowitsch if (quad) PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL)); 18729566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 1873ba2698f1SMatthew G. Knepley switch (ct) { 1874ba2698f1SMatthew G. Knepley case DM_POLYTOPE_POINT: 18759566063dSJacob Faibussowitsch PetscCall(DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ)); 1876dfccc68fSToby Isaac isAffine = PETSC_FALSE; 1877dfccc68fSToby Isaac break; 1878ba2698f1SMatthew G. Knepley case DM_POLYTOPE_SEGMENT: 1879412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 18809566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 18819566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ)); 1882dfccc68fSToby Isaac break; 1883ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 18849566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 18859566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ)); 1886dfccc68fSToby Isaac break; 1887ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 18889566063dSJacob Faibussowitsch PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ)); 1889412e9a14SMatthew G. Knepley isAffine = PETSC_FALSE; 1890412e9a14SMatthew G. Knepley break; 1891412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 18929566063dSJacob Faibussowitsch PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ)); 1893dfccc68fSToby Isaac isAffine = PETSC_FALSE; 1894dfccc68fSToby Isaac break; 1895ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 18969566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 18979566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ)); 1898dfccc68fSToby Isaac break; 1899ba2698f1SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 19009566063dSJacob Faibussowitsch PetscCall(DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ)); 1901dfccc68fSToby Isaac isAffine = PETSC_FALSE; 1902dfccc68fSToby Isaac break; 19032df84da0SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM: 19049566063dSJacob Faibussowitsch PetscCall(DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ)); 19052df84da0SMatthew G. Knepley isAffine = PETSC_FALSE; 19062df84da0SMatthew G. Knepley break; 19072df84da0SMatthew 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))]); 1908dfccc68fSToby Isaac } 19097318780aSToby Isaac if (isAffine && Nq) { 1910dfccc68fSToby Isaac if (v) { 19119371c9d4SSatish Balay for (i = 0; i < Nq; i++) { CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]); } 1912dfccc68fSToby Isaac } 19137318780aSToby Isaac if (detJ) { 19149371c9d4SSatish Balay for (i = 0; i < Nq; i++) { detJ[i] = detJ0; } 19157318780aSToby Isaac } 19167318780aSToby Isaac if (J) { 19177318780aSToby Isaac PetscInt k; 19187318780aSToby Isaac 19197318780aSToby Isaac for (i = 0, k = 0; i < Nq; i++) { 1920dfccc68fSToby Isaac PetscInt j; 1921dfccc68fSToby Isaac 19229371c9d4SSatish Balay for (j = 0; j < coordDim * coordDim; j++, k++) { J[k] = J0[j]; } 19237318780aSToby Isaac } 19247318780aSToby Isaac } 19257318780aSToby Isaac if (invJ) { 19267318780aSToby Isaac PetscInt k; 19277318780aSToby Isaac switch (coordDim) { 19289371c9d4SSatish Balay case 0: break; 19299371c9d4SSatish Balay case 1: invJ[0] = 1. / J0[0]; break; 19309371c9d4SSatish Balay case 2: DMPlex_Invert2D_Internal(invJ, J0, detJ0); break; 19319371c9d4SSatish Balay case 3: DMPlex_Invert3D_Internal(invJ, J0, detJ0); break; 19327318780aSToby Isaac } 19337318780aSToby Isaac for (i = 1, k = coordDim * coordDim; i < Nq; i++) { 19347318780aSToby Isaac PetscInt j; 19357318780aSToby Isaac 19369371c9d4SSatish Balay for (j = 0; j < coordDim * coordDim; j++, k++) { invJ[k] = invJ[j]; } 1937dfccc68fSToby Isaac } 1938dfccc68fSToby Isaac } 1939dfccc68fSToby Isaac } 1940dfccc68fSToby Isaac PetscFunctionReturn(0); 1941dfccc68fSToby Isaac } 1942dfccc68fSToby Isaac 1943ccd2543fSMatthew G Knepley /*@C 19448e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell 1945ccd2543fSMatthew G Knepley 1946d083f849SBarry Smith Collective on dm 1947ccd2543fSMatthew G Knepley 19484165533cSJose E. Roman Input Parameters: 1949ccd2543fSMatthew G Knepley + dm - the DM 1950ccd2543fSMatthew G Knepley - cell - the cell 1951ccd2543fSMatthew G Knepley 19524165533cSJose E. Roman Output Parameters: 19539b172b3aSMatthew Knepley + v0 - the translation part of this affine transform, meaning the translation to the origin (not the first vertex of the reference cell) 1954ccd2543fSMatthew G Knepley . J - the Jacobian of the transform from the reference element 1955ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian 1956ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant 1957ccd2543fSMatthew G Knepley 1958ccd2543fSMatthew G Knepley Level: advanced 1959ccd2543fSMatthew G Knepley 1960ccd2543fSMatthew G Knepley Fortran Notes: 1961ccd2543fSMatthew G Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 1962ccd2543fSMatthew G Knepley include petsc.h90 in your code. 1963ccd2543fSMatthew G Knepley 1964db781477SPatrick Sanan .seealso: `DMPlexComputeCellGeometryFEM()`, `DMGetCoordinateSection()`, `DMGetCoordinates()` 1965ccd2543fSMatthew G Knepley @*/ 19669371c9d4SSatish Balay PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) { 1967ccd2543fSMatthew G Knepley PetscFunctionBegin; 19689566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, NULL, v0, J, invJ, detJ)); 19698e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 19708e0841e0SMatthew G. Knepley } 19718e0841e0SMatthew G. Knepley 19729371c9d4SSatish Balay static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) { 19736858538eSMatthew G. Knepley const PetscScalar *array; 19748e0841e0SMatthew G. Knepley PetscScalar *coords = NULL; 19756858538eSMatthew G. Knepley PetscInt numCoords; 19766858538eSMatthew G. Knepley PetscBool isDG; 19776858538eSMatthew G. Knepley PetscQuadrature feQuad; 19788e0841e0SMatthew G. Knepley const PetscReal *quadPoints; 1979ef0bb6c7SMatthew G. Knepley PetscTabulation T; 19806858538eSMatthew G. Knepley PetscInt dim, cdim, pdim, qdim, Nq, q; 19818e0841e0SMatthew G. Knepley 19828e0841e0SMatthew G. Knepley PetscFunctionBegin; 19839566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 19849566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 19856858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords)); 1986dfccc68fSToby Isaac if (!quad) { /* use the first point of the first functional of the dual space */ 1987dfccc68fSToby Isaac PetscDualSpace dsp; 1988dfccc68fSToby Isaac 19899566063dSJacob Faibussowitsch PetscCall(PetscFEGetDualSpace(fe, &dsp)); 19909566063dSJacob Faibussowitsch PetscCall(PetscDualSpaceGetFunctional(dsp, 0, &quad)); 19919566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL)); 1992dfccc68fSToby Isaac Nq = 1; 1993dfccc68fSToby Isaac } else { 19949566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL)); 1995dfccc68fSToby Isaac } 19969566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 19979566063dSJacob Faibussowitsch PetscCall(PetscFEGetQuadrature(fe, &feQuad)); 1998dfccc68fSToby Isaac if (feQuad == quad) { 19999566063dSJacob Faibussowitsch PetscCall(PetscFEGetCellTabulation(fe, J ? 1 : 0, &T)); 200063a3b9bcSJacob Faibussowitsch PetscCheck(numCoords == pdim * cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "There are %" PetscInt_FMT " coordinates for point %" PetscInt_FMT " != %" PetscInt_FMT "*%" PetscInt_FMT, numCoords, point, pdim, cdim); 2001dfccc68fSToby Isaac } else { 20029566063dSJacob Faibussowitsch PetscCall(PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T)); 2003dfccc68fSToby Isaac } 200463a3b9bcSJacob Faibussowitsch PetscCheck(qdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %" PetscInt_FMT " != quadrature dimension %" PetscInt_FMT, dim, qdim); 2005ef0bb6c7SMatthew G. Knepley { 2006ef0bb6c7SMatthew G. Knepley const PetscReal *basis = T->T[0]; 2007ef0bb6c7SMatthew G. Knepley const PetscReal *basisDer = T->T[1]; 2008ef0bb6c7SMatthew G. Knepley PetscReal detJt; 2009ef0bb6c7SMatthew G. Knepley 2010a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG) 201163a3b9bcSJacob Faibussowitsch PetscCheck(Nq == T->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %" PetscInt_FMT " != %" PetscInt_FMT, Nq, T->Np); 201263a3b9bcSJacob Faibussowitsch PetscCheck(pdim == T->Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %" PetscInt_FMT " != %" PetscInt_FMT, pdim, T->Nb); 201363a3b9bcSJacob Faibussowitsch PetscCheck(dim == T->Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %" PetscInt_FMT " != %" PetscInt_FMT, dim, T->Nc); 201463a3b9bcSJacob Faibussowitsch PetscCheck(cdim == T->cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %" PetscInt_FMT " != %" PetscInt_FMT, cdim, T->cdim); 2015a2a9e04cSMatthew G. Knepley #endif 2016dfccc68fSToby Isaac if (v) { 20179566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(v, Nq * cdim)); 2018f960e424SToby Isaac for (q = 0; q < Nq; ++q) { 2019f960e424SToby Isaac PetscInt i, k; 2020f960e424SToby Isaac 2021301b184aSMatthew G. Knepley for (k = 0; k < pdim; ++k) { 2022301b184aSMatthew G. Knepley const PetscInt vertex = k / cdim; 20239371c9d4SSatish Balay for (i = 0; i < cdim; ++i) { v[q * cdim + i] += basis[(q * pdim + k) * cdim + i] * PetscRealPart(coords[vertex * cdim + i]); } 2024301b184aSMatthew G. Knepley } 20259566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * pdim * cdim)); 2026f960e424SToby Isaac } 2027f960e424SToby Isaac } 20288e0841e0SMatthew G. Knepley if (J) { 20299566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(J, Nq * cdim * cdim)); 20308e0841e0SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 20318e0841e0SMatthew G. Knepley PetscInt i, j, k, c, r; 20328e0841e0SMatthew G. Knepley 20338e0841e0SMatthew G. Knepley /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */ 2034301b184aSMatthew G. Knepley for (k = 0; k < pdim; ++k) { 2035301b184aSMatthew G. Knepley const PetscInt vertex = k / cdim; 2036301b184aSMatthew G. Knepley for (j = 0; j < dim; ++j) { 20379371c9d4SSatish Balay for (i = 0; i < cdim; ++i) { J[(q * cdim + i) * cdim + j] += basisDer[((q * pdim + k) * cdim + i) * dim + j] * PetscRealPart(coords[vertex * cdim + i]); } 2038301b184aSMatthew G. Knepley } 2039301b184aSMatthew G. Knepley } 20409566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * pdim * dim * cdim)); 20418e0841e0SMatthew G. Knepley if (cdim > dim) { 20428e0841e0SMatthew G. Knepley for (c = dim; c < cdim; ++c) 20439371c9d4SSatish Balay for (r = 0; r < cdim; ++r) J[r * cdim + c] = r == c ? 1.0 : 0.0; 20448e0841e0SMatthew G. Knepley } 2045f960e424SToby Isaac if (!detJ && !invJ) continue; 2046a63b72c6SToby Isaac detJt = 0.; 20478e0841e0SMatthew G. Knepley switch (cdim) { 20488e0841e0SMatthew G. Knepley case 3: 2049037dc194SToby Isaac DMPlex_Det3D_Internal(&detJt, &J[q * cdim * dim]); 2050037dc194SToby Isaac if (invJ) { DMPlex_Invert3D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt); } 205117fe8556SMatthew G. Knepley break; 205249dc4407SMatthew G. Knepley case 2: 20539f328543SToby Isaac DMPlex_Det2D_Internal(&detJt, &J[q * cdim * dim]); 2054037dc194SToby Isaac if (invJ) { DMPlex_Invert2D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt); } 205549dc4407SMatthew G. Knepley break; 20568e0841e0SMatthew G. Knepley case 1: 2057037dc194SToby Isaac detJt = J[q * cdim * dim]; 2058037dc194SToby Isaac if (invJ) invJ[q * cdim * dim] = 1.0 / detJt; 205949dc4407SMatthew G. Knepley } 2060f960e424SToby Isaac if (detJ) detJ[q] = detJt; 206149dc4407SMatthew G. Knepley } 206208401ef6SPierre Jolivet } else PetscCheck(!detJ && !invJ, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ"); 206349dc4407SMatthew G. Knepley } 20649566063dSJacob Faibussowitsch if (feQuad != quad) PetscCall(PetscTabulationDestroy(&T)); 20656858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords)); 20668e0841e0SMatthew G. Knepley PetscFunctionReturn(0); 20678e0841e0SMatthew G. Knepley } 20688e0841e0SMatthew G. Knepley 20698e0841e0SMatthew G. Knepley /*@C 20708e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell 20718e0841e0SMatthew G. Knepley 2072d083f849SBarry Smith Collective on dm 20738e0841e0SMatthew G. Knepley 20744165533cSJose E. Roman Input Parameters: 20758e0841e0SMatthew G. Knepley + dm - the DM 20768e0841e0SMatthew G. Knepley . cell - the cell 2077dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated. If quad == NULL, geometry will be 2078dfccc68fSToby Isaac evaluated at the first vertex of the reference element 20798e0841e0SMatthew G. Knepley 20804165533cSJose E. Roman Output Parameters: 2081dfccc68fSToby Isaac + v - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element 20828e0841e0SMatthew G. Knepley . J - the Jacobian of the transform from the reference element at each quadrature point 20838e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point 20848e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point 20858e0841e0SMatthew G. Knepley 20868e0841e0SMatthew G. Knepley Level: advanced 20878e0841e0SMatthew G. Knepley 20888e0841e0SMatthew G. Knepley Fortran Notes: 20898e0841e0SMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 20908e0841e0SMatthew G. Knepley include petsc.h90 in your code. 20918e0841e0SMatthew G. Knepley 2092db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()` 20938e0841e0SMatthew G. Knepley @*/ 20949371c9d4SSatish Balay PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) { 2095bb4a5db5SMatthew G. Knepley DM cdm; 2096dfccc68fSToby Isaac PetscFE fe = NULL; 20978e0841e0SMatthew G. Knepley 20988e0841e0SMatthew G. Knepley PetscFunctionBegin; 2099dadcf809SJacob Faibussowitsch PetscValidRealPointer(detJ, 7); 21009566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 2101bb4a5db5SMatthew G. Knepley if (cdm) { 2102dfccc68fSToby Isaac PetscClassId id; 2103dfccc68fSToby Isaac PetscInt numFields; 2104e5e52638SMatthew G. Knepley PetscDS prob; 2105dfccc68fSToby Isaac PetscObject disc; 2106dfccc68fSToby Isaac 21079566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(cdm, &numFields)); 2108dfccc68fSToby Isaac if (numFields) { 21099566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &prob)); 21109566063dSJacob Faibussowitsch PetscCall(PetscDSGetDiscretization(prob, 0, &disc)); 21119566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 21129371c9d4SSatish Balay if (id == PETSCFE_CLASSID) { fe = (PetscFE)disc; } 2113dfccc68fSToby Isaac } 2114dfccc68fSToby Isaac } 21159566063dSJacob Faibussowitsch if (!fe) PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ)); 21169566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ)); 2117ccd2543fSMatthew G Knepley PetscFunctionReturn(0); 2118ccd2543fSMatthew G Knepley } 2119834e62ceSMatthew G. Knepley 21209371c9d4SSatish Balay static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) { 21219bf2564aSMatt McGurn PetscSection coordSection; 21229bf2564aSMatt McGurn Vec coordinates; 21239bf2564aSMatt McGurn const PetscScalar *coords = NULL; 21249bf2564aSMatt McGurn PetscInt d, dof, off; 21259bf2564aSMatt McGurn 21269bf2564aSMatt McGurn PetscFunctionBegin; 21279566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 21289566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 21299566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coords)); 21309bf2564aSMatt McGurn 21319bf2564aSMatt McGurn /* for a point the centroid is just the coord */ 21329bf2564aSMatt McGurn if (centroid) { 21339566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, cell, &dof)); 21349566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, cell, &off)); 21359371c9d4SSatish Balay for (d = 0; d < dof; d++) { centroid[d] = PetscRealPart(coords[off + d]); } 21369bf2564aSMatt McGurn } 21379bf2564aSMatt McGurn if (normal) { 21389bf2564aSMatt McGurn const PetscInt *support, *cones; 21399bf2564aSMatt McGurn PetscInt supportSize; 21409bf2564aSMatt McGurn PetscReal norm, sign; 21419bf2564aSMatt McGurn 21429bf2564aSMatt McGurn /* compute the norm based upon the support centroids */ 21439566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, cell, &supportSize)); 21449566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, cell, &support)); 21459566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL)); 21469bf2564aSMatt McGurn 21479bf2564aSMatt McGurn /* Take the normal from the centroid of the support to the vertex*/ 21489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, cell, &dof)); 21499566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, cell, &off)); 21509371c9d4SSatish Balay for (d = 0; d < dof; d++) { normal[d] -= PetscRealPart(coords[off + d]); } 21519bf2564aSMatt McGurn 21529bf2564aSMatt McGurn /* Determine the sign of the normal based upon its location in the support */ 21539566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, support[0], &cones)); 21549bf2564aSMatt McGurn sign = cones[0] == cell ? 1.0 : -1.0; 21559bf2564aSMatt McGurn 21569bf2564aSMatt McGurn norm = DMPlex_NormD_Internal(dim, normal); 21579bf2564aSMatt McGurn for (d = 0; d < dim; ++d) normal[d] /= (norm * sign); 21589bf2564aSMatt McGurn } 21599371c9d4SSatish Balay if (vol) { *vol = 1.0; } 21609566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coords)); 21619bf2564aSMatt McGurn PetscFunctionReturn(0); 21629bf2564aSMatt McGurn } 21639bf2564aSMatt McGurn 21649371c9d4SSatish Balay static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) { 21656858538eSMatthew G. Knepley const PetscScalar *array; 2166a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 2167714b99b6SMatthew G. Knepley PetscInt coordSize, d; 21686858538eSMatthew G. Knepley PetscBool isDG; 2169cc08537eSMatthew G. Knepley 2170cc08537eSMatthew G. Knepley PetscFunctionBegin; 21716858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 2172cc08537eSMatthew G. Knepley if (centroid) { 21736858538eSMatthew G. Knepley for (d = 0; d < dim; ++d) centroid[d] = 0.5 * PetscRealPart(coords[d] + coords[dim + d]); 2174cc08537eSMatthew G. Knepley } 2175cc08537eSMatthew G. Knepley if (normal) { 2176a60a936bSMatthew G. Knepley PetscReal norm; 2177a60a936bSMatthew G. Knepley 217808401ef6SPierre Jolivet PetscCheck(dim == 2, PETSC_COMM_SELF, PETSC_ERR_SUP, "We only support 2D edges right now"); 21796858538eSMatthew G. Knepley normal[0] = -PetscRealPart(coords[1] - coords[dim + 1]); 21806858538eSMatthew G. Knepley normal[1] = PetscRealPart(coords[0] - coords[dim + 0]); 2181714b99b6SMatthew G. Knepley norm = DMPlex_NormD_Internal(dim, normal); 2182714b99b6SMatthew G. Knepley for (d = 0; d < dim; ++d) normal[d] /= norm; 2183cc08537eSMatthew G. Knepley } 2184cc08537eSMatthew G. Knepley if (vol) { 2185714b99b6SMatthew G. Knepley *vol = 0.0; 21866858538eSMatthew G. Knepley for (d = 0; d < dim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - coords[dim + d])); 2187714b99b6SMatthew G. Knepley *vol = PetscSqrtReal(*vol); 2188cc08537eSMatthew G. Knepley } 21896858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 2190cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 2191cc08537eSMatthew G. Knepley } 2192cc08537eSMatthew G. Knepley 2193cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */ 21949371c9d4SSatish Balay static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) { 2195412e9a14SMatthew G. Knepley DMPolytopeType ct; 21966858538eSMatthew G. Knepley const PetscScalar *array; 2197cc08537eSMatthew G. Knepley PetscScalar *coords = NULL; 21986858538eSMatthew G. Knepley PetscInt coordSize; 21996858538eSMatthew G. Knepley PetscBool isDG; 2200793a2a13SMatthew G. Knepley PetscInt fv[4] = {0, 1, 2, 3}; 22016858538eSMatthew G. Knepley PetscInt cdim, numCorners, p, d; 2202cc08537eSMatthew G. Knepley 2203cc08537eSMatthew G. Knepley PetscFunctionBegin; 2204793a2a13SMatthew G. Knepley /* Must check for hybrid cells because prisms have a different orientation scheme */ 22059566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2206412e9a14SMatthew G. Knepley switch (ct) { 22079371c9d4SSatish Balay case DM_POLYTOPE_SEG_PRISM_TENSOR: 22089371c9d4SSatish Balay fv[2] = 3; 22099371c9d4SSatish Balay fv[3] = 2; 22109371c9d4SSatish Balay break; 2211412e9a14SMatthew G. Knepley default: break; 2212412e9a14SMatthew G. Knepley } 22139566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 22146858538eSMatthew G. Knepley PetscCall(DMPlexGetConeSize(dm, cell, &numCorners)); 22156858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 22163f27a4e6SJed Brown { 22173f27a4e6SJed Brown PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm; 2218793a2a13SMatthew G. Knepley 22193f27a4e6SJed Brown for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]); 22204f99dae5SMatthew G. Knepley for (p = 0; p < numCorners - 2; ++p) { 22213f27a4e6SJed Brown PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.}; 22223f27a4e6SJed Brown for (d = 0; d < cdim; d++) { 22233f27a4e6SJed Brown e0[d] = PetscRealPart(coords[cdim * fv[p + 1] + d]) - origin[d]; 22243f27a4e6SJed Brown e1[d] = PetscRealPart(coords[cdim * fv[p + 2] + d]) - origin[d]; 22253f27a4e6SJed Brown } 22263f27a4e6SJed Brown const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1]; 22273f27a4e6SJed Brown const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2]; 22283f27a4e6SJed Brown const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0]; 22293f27a4e6SJed Brown const PetscReal a = PetscSqrtReal(dx * dx + dy * dy + dz * dz); 22304f99dae5SMatthew G. Knepley 22314f99dae5SMatthew G. Knepley n[0] += dx; 22324f99dae5SMatthew G. Knepley n[1] += dy; 22334f99dae5SMatthew G. Knepley n[2] += dz; 22349371c9d4SSatish Balay for (d = 0; d < cdim; d++) { c[d] += a * PetscRealPart(origin[d] + coords[cdim * fv[p + 1] + d] + coords[cdim * fv[p + 2] + d]) / 3.; } 2235ceee4971SMatthew G. Knepley } 22364f99dae5SMatthew G. Knepley norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]); 22374f99dae5SMatthew G. Knepley n[0] /= norm; 22384f99dae5SMatthew G. Knepley n[1] /= norm; 22394f99dae5SMatthew G. Knepley n[2] /= norm; 22404f99dae5SMatthew G. Knepley c[0] /= norm; 22414f99dae5SMatthew G. Knepley c[1] /= norm; 22424f99dae5SMatthew G. Knepley c[2] /= norm; 22434f99dae5SMatthew G. Knepley if (vol) *vol = 0.5 * norm; 22449371c9d4SSatish Balay if (centroid) 22459371c9d4SSatish Balay for (d = 0; d < cdim; ++d) centroid[d] = c[d]; 22469371c9d4SSatish Balay if (normal) 22479371c9d4SSatish Balay for (d = 0; d < cdim; ++d) normal[d] = n[d]; 22480a1d6728SMatthew G. Knepley } 22496858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 2250cc08537eSMatthew G. Knepley PetscFunctionReturn(0); 2251cc08537eSMatthew G. Knepley } 2252cc08537eSMatthew G. Knepley 22530ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */ 22549371c9d4SSatish Balay static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) { 2255412e9a14SMatthew G. Knepley DMPolytopeType ct; 22566858538eSMatthew G. Knepley const PetscScalar *array; 22570ec8681fSMatthew G. Knepley PetscScalar *coords = NULL; 22586858538eSMatthew G. Knepley PetscInt coordSize; 22596858538eSMatthew G. Knepley PetscBool isDG; 22603f27a4e6SJed Brown PetscReal vsum = 0.0, vtmp, coordsTmp[3 * 3], origin[3]; 22616858538eSMatthew G. Knepley const PetscInt order[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; 22626858538eSMatthew G. Knepley const PetscInt *cone, *faceSizes, *faces; 22636858538eSMatthew G. Knepley const DMPolytopeType *faceTypes; 2264793a2a13SMatthew G. Knepley PetscBool isHybrid = PETSC_FALSE; 22656858538eSMatthew G. Knepley PetscInt numFaces, f, fOff = 0, p, d; 22660ec8681fSMatthew G. Knepley 22670ec8681fSMatthew G. Knepley PetscFunctionBegin; 226863a3b9bcSJacob Faibussowitsch PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No support for dim %" PetscInt_FMT " > 3", dim); 2269793a2a13SMatthew G. Knepley /* Must check for hybrid cells because prisms have a different orientation scheme */ 22709566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2271412e9a14SMatthew G. Knepley switch (ct) { 2272412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 2273412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 2274412e9a14SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM_TENSOR: 22759371c9d4SSatish Balay case DM_POLYTOPE_QUAD_PRISM_TENSOR: isHybrid = PETSC_TRUE; 2276412e9a14SMatthew G. Knepley default: break; 2277412e9a14SMatthew G. Knepley } 2278793a2a13SMatthew G. Knepley 22799371c9d4SSatish Balay if (centroid) 22809371c9d4SSatish Balay for (d = 0; d < dim; ++d) centroid[d] = 0.0; 22816858538eSMatthew G. Knepley PetscCall(DMPlexGetCone(dm, cell, &cone)); 22826858538eSMatthew G. Knepley 22836858538eSMatthew G. Knepley // Using the closure of faces for coordinates does not work in periodic geometries, so we index into the cell coordinates 22846858538eSMatthew G. Knepley PetscCall(DMPlexGetRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces)); 22856858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 22860ec8681fSMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 2287793a2a13SMatthew G. Knepley PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */ 2288793a2a13SMatthew G. Knepley 22893f27a4e6SJed Brown // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and 22903f27a4e6SJed Brown // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex 22913f27a4e6SJed Brown // so that all tetrahedra have positive volume. 22929371c9d4SSatish Balay if (f == 0) 22939371c9d4SSatish Balay for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]); 22946858538eSMatthew G. Knepley switch (faceTypes[f]) { 2295ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 22960ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 22976858538eSMatthew G. Knepley coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + 0] * dim + d]) - origin[d]; 22986858538eSMatthew G. Knepley coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + 1] * dim + d]) - origin[d]; 22996858538eSMatthew G. Knepley coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + 2] * dim + d]) - origin[d]; 23000ec8681fSMatthew G. Knepley } 23010ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 23026858538eSMatthew G. Knepley if (flip) vtmp = -vtmp; 23030ec8681fSMatthew G. Knepley vsum += vtmp; 23044f25033aSJed Brown if (centroid) { /* Centroid of OABC = (a+b+c)/4 */ 23050ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 23061ee9d5ecSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp; 23070ec8681fSMatthew G. Knepley } 23080ec8681fSMatthew G. Knepley } 23090ec8681fSMatthew G. Knepley break; 2310ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 23119371c9d4SSatish Balay case DM_POLYTOPE_SEG_PRISM_TENSOR: { 2312793a2a13SMatthew G. Knepley PetscInt fv[4] = {0, 1, 2, 3}; 2313793a2a13SMatthew G. Knepley 2314793a2a13SMatthew G. Knepley /* Side faces for hybrid cells are are stored as tensor products */ 23159371c9d4SSatish Balay if (isHybrid && f > 1) { 23169371c9d4SSatish Balay fv[2] = 3; 23179371c9d4SSatish Balay fv[3] = 2; 23189371c9d4SSatish Balay } 23190ec8681fSMatthew G. Knepley /* DO FOR PYRAMID */ 23200ec8681fSMatthew G. Knepley /* First tet */ 23210ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 23226858538eSMatthew G. Knepley coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[0]] * dim + d]) - origin[d]; 23236858538eSMatthew G. Knepley coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d]; 23246858538eSMatthew G. Knepley coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d]; 23250ec8681fSMatthew G. Knepley } 23260ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 23276858538eSMatthew G. Knepley if (flip) vtmp = -vtmp; 23280ec8681fSMatthew G. Knepley vsum += vtmp; 23290ec8681fSMatthew G. Knepley if (centroid) { 23300ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 23310ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp; 23320ec8681fSMatthew G. Knepley } 23330ec8681fSMatthew G. Knepley } 23340ec8681fSMatthew G. Knepley /* Second tet */ 23350ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 23366858538eSMatthew G. Knepley coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d]; 23376858538eSMatthew G. Knepley coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[2]] * dim + d]) - origin[d]; 23386858538eSMatthew G. Knepley coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d]; 23390ec8681fSMatthew G. Knepley } 23400ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 23416858538eSMatthew G. Knepley if (flip) vtmp = -vtmp; 23420ec8681fSMatthew G. Knepley vsum += vtmp; 23430ec8681fSMatthew G. Knepley if (centroid) { 23440ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 23450ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp; 23460ec8681fSMatthew G. Knepley } 23470ec8681fSMatthew G. Knepley } 23480ec8681fSMatthew G. Knepley break; 2349793a2a13SMatthew G. Knepley } 23509371c9d4SSatish Balay default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %" PetscInt_FMT " of type %s", cone[f], DMPolytopeTypes[ct]); 23510ec8681fSMatthew G. Knepley } 23526858538eSMatthew G. Knepley fOff += faceSizes[f]; 23530ec8681fSMatthew G. Knepley } 23546858538eSMatthew G. Knepley PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces)); 23556858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 23568763be8eSMatthew G. Knepley if (vol) *vol = PetscAbsReal(vsum); 23579371c9d4SSatish Balay if (normal) 23589371c9d4SSatish Balay for (d = 0; d < dim; ++d) normal[d] = 0.0; 23599371c9d4SSatish Balay if (centroid) 23609371c9d4SSatish Balay for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum * 4) + origin[d]; 23610ec8681fSMatthew G. Knepley PetscFunctionReturn(0); 23620ec8681fSMatthew G. Knepley } 23630ec8681fSMatthew G. Knepley 2364834e62ceSMatthew G. Knepley /*@C 2365834e62ceSMatthew G. Knepley DMPlexComputeCellGeometryFVM - Compute the volume for a given cell 2366834e62ceSMatthew G. Knepley 2367d083f849SBarry Smith Collective on dm 2368834e62ceSMatthew G. Knepley 23694165533cSJose E. Roman Input Parameters: 2370834e62ceSMatthew G. Knepley + dm - the DM 2371834e62ceSMatthew G. Knepley - cell - the cell 2372834e62ceSMatthew G. Knepley 23734165533cSJose E. Roman Output Parameters: 2374834e62ceSMatthew G. Knepley + volume - the cell volume 2375cc08537eSMatthew G. Knepley . centroid - the cell centroid 2376cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate 2377834e62ceSMatthew G. Knepley 2378834e62ceSMatthew G. Knepley Level: advanced 2379834e62ceSMatthew G. Knepley 2380834e62ceSMatthew G. Knepley Fortran Notes: 2381834e62ceSMatthew G. Knepley Since it returns arrays, this routine is only available in Fortran 90, and you must 2382834e62ceSMatthew G. Knepley include petsc.h90 in your code. 2383834e62ceSMatthew G. Knepley 2384db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()` 2385834e62ceSMatthew G. Knepley @*/ 23869371c9d4SSatish Balay PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) { 23870ec8681fSMatthew G. Knepley PetscInt depth, dim; 2388834e62ceSMatthew G. Knepley 2389834e62ceSMatthew G. Knepley PetscFunctionBegin; 23909566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 23919566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 239208401ef6SPierre Jolivet PetscCheck(depth == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated"); 23939566063dSJacob Faibussowitsch PetscCall(DMPlexGetPointDepth(dm, cell, &depth)); 2394011ea5d8SMatthew G. Knepley switch (depth) { 23959371c9d4SSatish Balay case 0: PetscCall(DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal)); break; 23969371c9d4SSatish Balay case 1: PetscCall(DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal)); break; 23979371c9d4SSatish Balay case 2: PetscCall(DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal)); break; 23989371c9d4SSatish Balay case 3: PetscCall(DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal)); break; 23999371c9d4SSatish Balay default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT " (depth %" PetscInt_FMT ") for element geometry computation", dim, depth); 2400834e62ceSMatthew G. Knepley } 2401834e62ceSMatthew G. Knepley PetscFunctionReturn(0); 2402834e62ceSMatthew G. Knepley } 2403113c68e6SMatthew G. Knepley 2404c501906fSMatthew G. Knepley /*@ 2405c501906fSMatthew G. Knepley DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh 2406c501906fSMatthew G. Knepley 2407c501906fSMatthew G. Knepley Collective on dm 2408c501906fSMatthew G. Knepley 2409c501906fSMatthew G. Knepley Input Parameter: 2410c501906fSMatthew G. Knepley . dm - The DMPlex 2411c501906fSMatthew G. Knepley 2412c501906fSMatthew G. Knepley Output Parameter: 2413c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell 2414c501906fSMatthew G. Knepley 2415c501906fSMatthew G. Knepley Level: beginner 2416c501906fSMatthew G. Knepley 2417c501906fSMatthew G. Knepley @*/ 24189371c9d4SSatish Balay PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom) { 2419c0d900a5SMatthew G. Knepley DM dmCell; 2420c0d900a5SMatthew G. Knepley Vec coordinates; 2421c0d900a5SMatthew G. Knepley PetscSection coordSection, sectionCell; 2422c0d900a5SMatthew G. Knepley PetscScalar *cgeom; 2423412e9a14SMatthew G. Knepley PetscInt cStart, cEnd, c; 2424c0d900a5SMatthew G. Knepley 2425c0d900a5SMatthew G. Knepley PetscFunctionBegin; 24269566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmCell)); 24279566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 24289566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 24299566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection)); 24309566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmCell, coordinates)); 24319566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionCell)); 24329566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 24339566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd)); 2434c0d900a5SMatthew G. Knepley /* TODO This needs to be multiplied by Nq for non-affine */ 24359566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFEGeom)) / sizeof(PetscScalar)))); 24369566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionCell)); 24379566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmCell, sectionCell)); 24389566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionCell)); 24399566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmCell, cellgeom)); 24409566063dSJacob Faibussowitsch PetscCall(VecGetArray(*cellgeom, &cgeom)); 2441c0d900a5SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 2442cf0b7c11SKarl Rupp PetscFEGeom *cg; 2443c0d900a5SMatthew G. Knepley 24449566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg)); 24459566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(cg, 1)); 24469566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ)); 244763a3b9bcSJacob Faibussowitsch PetscCheck(*cg->detJ > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %" PetscInt_FMT, (double)*cg->detJ, c); 2448c0d900a5SMatthew G. Knepley } 2449c0d900a5SMatthew G. Knepley PetscFunctionReturn(0); 2450c0d900a5SMatthew G. Knepley } 2451c0d900a5SMatthew G. Knepley 2452891a9168SMatthew G. Knepley /*@ 2453891a9168SMatthew G. Knepley DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method 2454891a9168SMatthew G. Knepley 2455891a9168SMatthew G. Knepley Input Parameter: 2456891a9168SMatthew G. Knepley . dm - The DM 2457891a9168SMatthew G. Knepley 2458891a9168SMatthew G. Knepley Output Parameters: 2459891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data 2460a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data 2461891a9168SMatthew G. Knepley 2462891a9168SMatthew G. Knepley Level: developer 2463891a9168SMatthew G. Knepley 2464db781477SPatrick Sanan .seealso: `PetscFVFaceGeom`, `PetscFVCellGeom`, `DMPlexComputeGeometryFEM()` 2465891a9168SMatthew G. Knepley @*/ 24669371c9d4SSatish Balay PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) { 2467113c68e6SMatthew G. Knepley DM dmFace, dmCell; 2468113c68e6SMatthew G. Knepley DMLabel ghostLabel; 2469113c68e6SMatthew G. Knepley PetscSection sectionFace, sectionCell; 2470113c68e6SMatthew G. Knepley PetscSection coordSection; 2471113c68e6SMatthew G. Knepley Vec coordinates; 2472113c68e6SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 2473113c68e6SMatthew G. Knepley PetscReal minradius, gminradius; 2474113c68e6SMatthew G. Knepley PetscInt dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f; 2475113c68e6SMatthew G. Knepley 2476113c68e6SMatthew G. Knepley PetscFunctionBegin; 24779566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 24789566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 24799566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 2480113c68e6SMatthew G. Knepley /* Make cell centroids and volumes */ 24819566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmCell)); 24829566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection)); 24839566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmCell, coordinates)); 24849566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionCell)); 24859566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 24869566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 24879566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd)); 24889566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVCellGeom)) / sizeof(PetscScalar)))); 24899566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionCell)); 24909566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmCell, sectionCell)); 24919566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionCell)); 24929566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmCell, cellgeom)); 2493485ad865SMatthew G. Knepley if (cEndInterior < 0) cEndInterior = cEnd; 24949566063dSJacob Faibussowitsch PetscCall(VecGetArray(*cellgeom, &cgeom)); 2495113c68e6SMatthew G. Knepley for (c = cStart; c < cEndInterior; ++c) { 2496113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 2497113c68e6SMatthew G. Knepley 24989566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg)); 24999566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(cg, 1)); 25009566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL)); 2501113c68e6SMatthew G. Knepley } 2502113c68e6SMatthew G. Knepley /* Compute face normals and minimum cell radius */ 25039566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmFace)); 25049566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionFace)); 25059566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 25069566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionFace, fStart, fEnd)); 25079566063dSJacob Faibussowitsch for (f = fStart; f < fEnd; ++f) PetscCall(PetscSectionSetDof(sectionFace, f, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVFaceGeom)) / sizeof(PetscScalar)))); 25089566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionFace)); 25099566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmFace, sectionFace)); 25109566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionFace)); 25119566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmFace, facegeom)); 25129566063dSJacob Faibussowitsch PetscCall(VecGetArray(*facegeom, &fgeom)); 25139566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 2514113c68e6SMatthew G. Knepley minradius = PETSC_MAX_REAL; 2515113c68e6SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 2516113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 2517113c68e6SMatthew G. Knepley PetscReal area; 2518412e9a14SMatthew G. Knepley const PetscInt *cells; 2519412e9a14SMatthew G. Knepley PetscInt ncells, ghost = -1, d, numChildren; 2520113c68e6SMatthew G. Knepley 25219566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 25229566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 25239566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &cells)); 25249566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &ncells)); 2525412e9a14SMatthew G. Knepley /* It is possible to get a face with no support when using partition overlap */ 2526412e9a14SMatthew G. Knepley if (!ncells || ghost >= 0 || numChildren) continue; 25279566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, f, fgeom, &fg)); 25289566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal)); 2529113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] *= area; 2530113c68e6SMatthew G. Knepley /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */ 2531113c68e6SMatthew G. Knepley { 2532113c68e6SMatthew G. Knepley PetscFVCellGeom *cL, *cR; 2533113c68e6SMatthew G. Knepley PetscReal *lcentroid, *rcentroid; 25340453c0cdSMatthew G. Knepley PetscReal l[3], r[3], v[3]; 2535113c68e6SMatthew G. Knepley 25369566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL)); 2537113c68e6SMatthew G. Knepley lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid; 253806348e87SToby Isaac if (ncells > 1) { 25399566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR)); 2540113c68e6SMatthew G. Knepley rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid; 25419371c9d4SSatish Balay } else { 254206348e87SToby Isaac rcentroid = fg->centroid; 254306348e87SToby Isaac } 25449566063dSJacob Faibussowitsch PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l)); 25459566063dSJacob Faibussowitsch PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r)); 25460453c0cdSMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, l, r, v); 2547113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) { 2548113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d]; 2549113c68e6SMatthew G. Knepley } 2550113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) { 255163a3b9bcSJacob Faibussowitsch PetscCheck(dim != 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " 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]); 255263a3b9bcSJacob Faibussowitsch PetscCheck(dim != 3, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " 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]); 255363a3b9bcSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " could not be fixed", f); 2554113c68e6SMatthew G. Knepley } 2555113c68e6SMatthew G. Knepley if (cells[0] < cEndInterior) { 2556113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v); 2557113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2558113c68e6SMatthew G. Knepley } 255906348e87SToby Isaac if (ncells > 1 && cells[1] < cEndInterior) { 2560113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v); 2561113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2562113c68e6SMatthew G. Knepley } 2563113c68e6SMatthew G. Knepley } 2564113c68e6SMatthew G. Knepley } 25651c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm))); 25669566063dSJacob Faibussowitsch PetscCall(DMPlexSetMinRadius(dm, gminradius)); 2567113c68e6SMatthew G. Knepley /* Compute centroids of ghost cells */ 2568113c68e6SMatthew G. Knepley for (c = cEndInterior; c < cEnd; ++c) { 2569113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 2570113c68e6SMatthew G. Knepley const PetscInt *cone, *support; 2571113c68e6SMatthew G. Knepley PetscInt coneSize, supportSize, s; 2572113c68e6SMatthew G. Knepley 25739566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dmCell, c, &coneSize)); 257463a3b9bcSJacob Faibussowitsch PetscCheck(coneSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %" PetscInt_FMT " has cone size %" PetscInt_FMT " != 1", c, coneSize); 25759566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dmCell, c, &cone)); 25769566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dmCell, cone[0], &supportSize)); 257763a3b9bcSJacob Faibussowitsch PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " has support size %" PetscInt_FMT " != 2", cone[0], supportSize); 25789566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dmCell, cone[0], &support)); 25799566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg)); 2580113c68e6SMatthew G. Knepley for (s = 0; s < 2; ++s) { 2581113c68e6SMatthew G. Knepley /* Reflect ghost centroid across plane of face */ 2582113c68e6SMatthew G. Knepley if (support[s] == c) { 2583640bce14SSatish Balay PetscFVCellGeom *ci; 2584113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 2585113c68e6SMatthew G. Knepley PetscReal c2f[3], a; 2586113c68e6SMatthew G. Knepley 25879566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, support[(s + 1) % 2], cgeom, &ci)); 2588113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */ 2589113c68e6SMatthew G. Knepley a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal) / DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal); 25909566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg)); 2591113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, 2 * a, fg->normal, ci->centroid, cg->centroid); 2592113c68e6SMatthew G. Knepley cg->volume = ci->volume; 2593113c68e6SMatthew G. Knepley } 2594113c68e6SMatthew G. Knepley } 2595113c68e6SMatthew G. Knepley } 25969566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(*facegeom, &fgeom)); 25979566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(*cellgeom, &cgeom)); 25989566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmCell)); 25999566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmFace)); 2600113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 2601113c68e6SMatthew G. Knepley } 2602113c68e6SMatthew G. Knepley 2603113c68e6SMatthew G. Knepley /*@C 2604113c68e6SMatthew G. Knepley DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face 2605113c68e6SMatthew G. Knepley 2606113c68e6SMatthew G. Knepley Not collective 2607113c68e6SMatthew G. Knepley 26084165533cSJose E. Roman Input Parameter: 2609113c68e6SMatthew G. Knepley . dm - the DM 2610113c68e6SMatthew G. Knepley 26114165533cSJose E. Roman Output Parameter: 2612a5b23f4aSJose E. Roman . minradius - the minimum cell radius 2613113c68e6SMatthew G. Knepley 2614113c68e6SMatthew G. Knepley Level: developer 2615113c68e6SMatthew G. Knepley 2616db781477SPatrick Sanan .seealso: `DMGetCoordinates()` 2617113c68e6SMatthew G. Knepley @*/ 26189371c9d4SSatish Balay PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) { 2619113c68e6SMatthew G. Knepley PetscFunctionBegin; 2620113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2621dadcf809SJacob Faibussowitsch PetscValidRealPointer(minradius, 2); 2622113c68e6SMatthew G. Knepley *minradius = ((DM_Plex *)dm->data)->minradius; 2623113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 2624113c68e6SMatthew G. Knepley } 2625113c68e6SMatthew G. Knepley 2626113c68e6SMatthew G. Knepley /*@C 2627113c68e6SMatthew G. Knepley DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face 2628113c68e6SMatthew G. Knepley 2629113c68e6SMatthew G. Knepley Logically collective 2630113c68e6SMatthew G. Knepley 26314165533cSJose E. Roman Input Parameters: 2632113c68e6SMatthew G. Knepley + dm - the DM 2633a5b23f4aSJose E. Roman - minradius - the minimum cell radius 2634113c68e6SMatthew G. Knepley 2635113c68e6SMatthew G. Knepley Level: developer 2636113c68e6SMatthew G. Knepley 2637db781477SPatrick Sanan .seealso: `DMSetCoordinates()` 2638113c68e6SMatthew G. Knepley @*/ 26399371c9d4SSatish Balay PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) { 2640113c68e6SMatthew G. Knepley PetscFunctionBegin; 2641113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2642113c68e6SMatthew G. Knepley ((DM_Plex *)dm->data)->minradius = minradius; 2643113c68e6SMatthew G. Knepley PetscFunctionReturn(0); 2644113c68e6SMatthew G. Knepley } 2645856ac710SMatthew G. Knepley 26469371c9d4SSatish Balay static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) { 2647856ac710SMatthew G. Knepley DMLabel ghostLabel; 2648856ac710SMatthew G. Knepley PetscScalar *dx, *grad, **gref; 2649856ac710SMatthew G. Knepley PetscInt dim, cStart, cEnd, c, cEndInterior, maxNumFaces; 2650856ac710SMatthew G. Knepley 2651856ac710SMatthew G. Knepley PetscFunctionBegin; 26529566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 26539566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 26549566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 2655089217ebSMatthew G. Knepley cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior; 26569566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxNumFaces, NULL)); 26579566063dSJacob Faibussowitsch PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces)); 26589566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 26599566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref)); 2660856ac710SMatthew G. Knepley for (c = cStart; c < cEndInterior; c++) { 2661856ac710SMatthew G. Knepley const PetscInt *faces; 2662856ac710SMatthew G. Knepley PetscInt numFaces, usedFaces, f, d; 2663640bce14SSatish Balay PetscFVCellGeom *cg; 2664856ac710SMatthew G. Knepley PetscBool boundary; 2665856ac710SMatthew G. Knepley PetscInt ghost; 2666856ac710SMatthew G. Knepley 2667a79418b7SMatt McGurn // do not attempt to compute a gradient reconstruction stencil in a ghost cell. It will never be used 2668a79418b7SMatt McGurn PetscCall(DMLabelGetValue(ghostLabel, c, &ghost)); 2669a79418b7SMatt McGurn if (ghost >= 0) continue; 2670a79418b7SMatt McGurn 26719566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg)); 26729566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, c, &numFaces)); 26739566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, c, &faces)); 267463a3b9bcSJacob Faibussowitsch PetscCheck(numFaces >= dim, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Cell %" PetscInt_FMT " has only %" PetscInt_FMT " faces, not enough for gradient reconstruction", c, numFaces); 2675856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 2676640bce14SSatish Balay PetscFVCellGeom *cg1; 2677856ac710SMatthew G. Knepley PetscFVFaceGeom *fg; 2678856ac710SMatthew G. Knepley const PetscInt *fcells; 2679856ac710SMatthew G. Knepley PetscInt ncell, side; 2680856ac710SMatthew G. Knepley 26819566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost)); 26829566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary)); 2683856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 26849566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, faces[f], &fcells)); 2685856ac710SMatthew G. Knepley side = (c != fcells[0]); /* c is on left=0 or right=1 of face */ 2686856ac710SMatthew G. Knepley ncell = fcells[!side]; /* the neighbor */ 26879566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg)); 26889566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1)); 2689856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) dx[usedFaces * dim + d] = cg1->centroid[d] - cg->centroid[d]; 2690856ac710SMatthew G. Knepley gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */ 2691856ac710SMatthew G. Knepley } 269228b400f6SJacob Faibussowitsch PetscCheck(usedFaces, PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?"); 26939566063dSJacob Faibussowitsch PetscCall(PetscFVComputeGradient(fvm, usedFaces, dx, grad)); 2694856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 26959566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost)); 26969566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary)); 2697856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 2698856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces * dim + d]; 2699856ac710SMatthew G. Knepley ++usedFaces; 2700856ac710SMatthew G. Knepley } 2701856ac710SMatthew G. Knepley } 27029566063dSJacob Faibussowitsch PetscCall(PetscFree3(dx, grad, gref)); 2703856ac710SMatthew G. Knepley PetscFunctionReturn(0); 2704856ac710SMatthew G. Knepley } 2705856ac710SMatthew G. Knepley 27069371c9d4SSatish Balay static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) { 2707b81db932SToby Isaac DMLabel ghostLabel; 2708b81db932SToby Isaac PetscScalar *dx, *grad, **gref; 2709b81db932SToby Isaac PetscInt dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0; 2710b81db932SToby Isaac PetscSection neighSec; 2711b81db932SToby Isaac PetscInt(*neighbors)[2]; 2712b81db932SToby Isaac PetscInt *counter; 2713b81db932SToby Isaac 2714b81db932SToby Isaac PetscFunctionBegin; 27159566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 27169566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 27179566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 2718485ad865SMatthew G. Knepley if (cEndInterior < 0) cEndInterior = cEnd; 27199566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &neighSec)); 27209566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(neighSec, cStart, cEndInterior)); 27219566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 27229566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 2723b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 2724b81db932SToby Isaac const PetscInt *fcells; 2725b81db932SToby Isaac PetscBool boundary; 27265bc680faSToby Isaac PetscInt ghost = -1; 2727b81db932SToby Isaac PetscInt numChildren, numCells, c; 2728b81db932SToby Isaac 27299566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 27309566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, f, &boundary)); 27319566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 2732b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 27339566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &numCells)); 273406348e87SToby Isaac if (numCells == 2) { 27359566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &fcells)); 2736b81db932SToby Isaac for (c = 0; c < 2; c++) { 2737b81db932SToby Isaac PetscInt cell = fcells[c]; 2738b81db932SToby Isaac 2739*48a46eb9SPierre Jolivet if (cell >= cStart && cell < cEndInterior) PetscCall(PetscSectionAddDof(neighSec, cell, 1)); 2740b81db932SToby Isaac } 2741b81db932SToby Isaac } 274206348e87SToby Isaac } 27439566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(neighSec)); 27449566063dSJacob Faibussowitsch PetscCall(PetscSectionGetMaxDof(neighSec, &maxNumFaces)); 27459566063dSJacob Faibussowitsch PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces)); 2746b81db932SToby Isaac nStart = 0; 27479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(neighSec, &nEnd)); 27489566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((nEnd - nStart), &neighbors)); 27499566063dSJacob Faibussowitsch PetscCall(PetscCalloc1((cEndInterior - cStart), &counter)); 2750b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 2751b81db932SToby Isaac const PetscInt *fcells; 2752b81db932SToby Isaac PetscBool boundary; 27535bc680faSToby Isaac PetscInt ghost = -1; 2754b81db932SToby Isaac PetscInt numChildren, numCells, c; 2755b81db932SToby Isaac 27569566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 27579566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, f, &boundary)); 27589566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 2759b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 27609566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &numCells)); 276106348e87SToby Isaac if (numCells == 2) { 27629566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &fcells)); 2763b81db932SToby Isaac for (c = 0; c < 2; c++) { 2764b81db932SToby Isaac PetscInt cell = fcells[c], off; 2765b81db932SToby Isaac 2766e6885bbbSToby Isaac if (cell >= cStart && cell < cEndInterior) { 27679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(neighSec, cell, &off)); 2768b81db932SToby Isaac off += counter[cell - cStart]++; 2769b81db932SToby Isaac neighbors[off][0] = f; 2770b81db932SToby Isaac neighbors[off][1] = fcells[1 - c]; 2771b81db932SToby Isaac } 2772b81db932SToby Isaac } 2773b81db932SToby Isaac } 277406348e87SToby Isaac } 27759566063dSJacob Faibussowitsch PetscCall(PetscFree(counter)); 27769566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref)); 2777b81db932SToby Isaac for (c = cStart; c < cEndInterior; c++) { 2778317218b9SToby Isaac PetscInt numFaces, f, d, off, ghost = -1; 2779640bce14SSatish Balay PetscFVCellGeom *cg; 2780b81db932SToby Isaac 27819566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg)); 27829566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(neighSec, c, &numFaces)); 27839566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(neighSec, c, &off)); 2784a79418b7SMatt McGurn 2785a79418b7SMatt McGurn // do not attempt to compute a gradient reconstruction stencil in a ghost cell. It will never be used 27869566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, c, &ghost)); 2787a79418b7SMatt McGurn if (ghost >= 0) continue; 2788a79418b7SMatt McGurn 278963a3b9bcSJacob Faibussowitsch PetscCheck(numFaces >= dim, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Cell %" PetscInt_FMT " has only %" PetscInt_FMT " faces, not enough for gradient reconstruction", c, numFaces); 2790b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 2791640bce14SSatish Balay PetscFVCellGeom *cg1; 2792b81db932SToby Isaac PetscFVFaceGeom *fg; 2793b81db932SToby Isaac const PetscInt *fcells; 2794b81db932SToby Isaac PetscInt ncell, side, nface; 2795b81db932SToby Isaac 2796b81db932SToby Isaac nface = neighbors[off + f][0]; 2797b81db932SToby Isaac ncell = neighbors[off + f][1]; 27989566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, nface, &fcells)); 2799b81db932SToby Isaac side = (c != fcells[0]); 28009566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, nface, fgeom, &fg)); 28019566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1)); 2802b81db932SToby Isaac for (d = 0; d < dim; ++d) dx[f * dim + d] = cg1->centroid[d] - cg->centroid[d]; 2803b81db932SToby Isaac gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */ 2804b81db932SToby Isaac } 28059566063dSJacob Faibussowitsch PetscCall(PetscFVComputeGradient(fvm, numFaces, dx, grad)); 2806b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 2807b81db932SToby Isaac for (d = 0; d < dim; ++d) gref[f][d] = grad[f * dim + d]; 2808b81db932SToby Isaac } 2809b81db932SToby Isaac } 28109566063dSJacob Faibussowitsch PetscCall(PetscFree3(dx, grad, gref)); 28119566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&neighSec)); 28129566063dSJacob Faibussowitsch PetscCall(PetscFree(neighbors)); 2813b81db932SToby Isaac PetscFunctionReturn(0); 2814b81db932SToby Isaac } 2815b81db932SToby Isaac 2816856ac710SMatthew G. Knepley /*@ 2817856ac710SMatthew G. Knepley DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data 2818856ac710SMatthew G. Knepley 2819d083f849SBarry Smith Collective on dm 2820856ac710SMatthew G. Knepley 28214165533cSJose E. Roman Input Parameters: 2822856ac710SMatthew G. Knepley + dm - The DM 2823856ac710SMatthew G. Knepley . fvm - The PetscFV 28248f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM() 2825856ac710SMatthew G. Knepley 28266b867d5aSJose E. Roman Input/Output Parameter: 28276b867d5aSJose E. Roman . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM(); on output 28286b867d5aSJose E. Roman the geometric factors for gradient calculation are inserted 28296b867d5aSJose E. Roman 28306b867d5aSJose E. Roman Output Parameter: 28316b867d5aSJose E. Roman . dmGrad - The DM describing the layout of gradient data 2832856ac710SMatthew G. Knepley 2833856ac710SMatthew G. Knepley Level: developer 2834856ac710SMatthew G. Knepley 2835db781477SPatrick Sanan .seealso: `DMPlexGetFaceGeometryFVM()`, `DMPlexGetCellGeometryFVM()` 2836856ac710SMatthew G. Knepley @*/ 28379371c9d4SSatish Balay PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) { 2838856ac710SMatthew G. Knepley DM dmFace, dmCell; 2839856ac710SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 2840b81db932SToby Isaac PetscSection sectionGrad, parentSection; 2841856ac710SMatthew G. Knepley PetscInt dim, pdim, cStart, cEnd, cEndInterior, c; 2842856ac710SMatthew G. Knepley 2843856ac710SMatthew G. Knepley PetscFunctionBegin; 28449566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 28459566063dSJacob Faibussowitsch PetscCall(PetscFVGetNumComponents(fvm, &pdim)); 28469566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 28479566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 2848856ac710SMatthew G. Knepley /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */ 28499566063dSJacob Faibussowitsch PetscCall(VecGetDM(faceGeometry, &dmFace)); 28509566063dSJacob Faibussowitsch PetscCall(VecGetDM(cellGeometry, &dmCell)); 28519566063dSJacob Faibussowitsch PetscCall(VecGetArray(faceGeometry, &fgeom)); 28529566063dSJacob Faibussowitsch PetscCall(VecGetArray(cellGeometry, &cgeom)); 28539566063dSJacob Faibussowitsch PetscCall(DMPlexGetTree(dm, &parentSection, NULL, NULL, NULL, NULL)); 2854b81db932SToby Isaac if (!parentSection) { 28559566063dSJacob Faibussowitsch PetscCall(BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom)); 2856b5a3613cSMatthew G. Knepley } else { 28579566063dSJacob Faibussowitsch PetscCall(BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom)); 2858b81db932SToby Isaac } 28599566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(faceGeometry, &fgeom)); 28609566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(cellGeometry, &cgeom)); 2861856ac710SMatthew G. Knepley /* Create storage for gradients */ 28629566063dSJacob Faibussowitsch PetscCall(DMClone(dm, dmGrad)); 28639566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionGrad)); 28649566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionGrad, cStart, cEnd)); 28659566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionGrad, c, pdim * dim)); 28669566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionGrad)); 28679566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(*dmGrad, sectionGrad)); 28689566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionGrad)); 2869856ac710SMatthew G. Knepley PetscFunctionReturn(0); 2870856ac710SMatthew G. Knepley } 2871b27d5b9eSToby Isaac 2872c501906fSMatthew G. Knepley /*@ 2873c501906fSMatthew G. Knepley DMPlexGetDataFVM - Retrieve precomputed cell geometry 2874c501906fSMatthew G. Knepley 2875d083f849SBarry Smith Collective on dm 2876c501906fSMatthew G. Knepley 28774165533cSJose E. Roman Input Parameters: 2878c501906fSMatthew G. Knepley + dm - The DM 28796b867d5aSJose E. Roman - fv - The PetscFV 2880c501906fSMatthew G. Knepley 2881c501906fSMatthew G. Knepley Output Parameters: 2882c501906fSMatthew G. Knepley + cellGeometry - The cell geometry 2883c501906fSMatthew G. Knepley . faceGeometry - The face geometry 28846b867d5aSJose E. Roman - gradDM - The gradient matrices 2885c501906fSMatthew G. Knepley 2886c501906fSMatthew G. Knepley Level: developer 2887c501906fSMatthew G. Knepley 2888db781477SPatrick Sanan .seealso: `DMPlexComputeGeometryFVM()` 2889c501906fSMatthew G. Knepley @*/ 28909371c9d4SSatish Balay PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM) { 2891b27d5b9eSToby Isaac PetscObject cellgeomobj, facegeomobj; 2892b27d5b9eSToby Isaac 2893b27d5b9eSToby Isaac PetscFunctionBegin; 28949566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj)); 2895b27d5b9eSToby Isaac if (!cellgeomobj) { 2896b27d5b9eSToby Isaac Vec cellgeomInt, facegeomInt; 2897b27d5b9eSToby Isaac 28989566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt)); 28999566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_cellgeom_fvm", (PetscObject)cellgeomInt)); 29009566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_facegeom_fvm", (PetscObject)facegeomInt)); 29019566063dSJacob Faibussowitsch PetscCall(VecDestroy(&cellgeomInt)); 29029566063dSJacob Faibussowitsch PetscCall(VecDestroy(&facegeomInt)); 29039566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj)); 2904b27d5b9eSToby Isaac } 29059566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_facegeom_fvm", &facegeomobj)); 2906b27d5b9eSToby Isaac if (cellgeom) *cellgeom = (Vec)cellgeomobj; 2907b27d5b9eSToby Isaac if (facegeom) *facegeom = (Vec)facegeomobj; 2908b27d5b9eSToby Isaac if (gradDM) { 2909b27d5b9eSToby Isaac PetscObject gradobj; 2910b27d5b9eSToby Isaac PetscBool computeGradients; 2911b27d5b9eSToby Isaac 29129566063dSJacob Faibussowitsch PetscCall(PetscFVGetComputeGradients(fv, &computeGradients)); 2913b27d5b9eSToby Isaac if (!computeGradients) { 2914b27d5b9eSToby Isaac *gradDM = NULL; 2915b27d5b9eSToby Isaac PetscFunctionReturn(0); 2916b27d5b9eSToby Isaac } 29179566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj)); 2918b27d5b9eSToby Isaac if (!gradobj) { 2919b27d5b9eSToby Isaac DM dmGradInt; 2920b27d5b9eSToby Isaac 29219566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGradientFVM(dm, fv, (Vec)facegeomobj, (Vec)cellgeomobj, &dmGradInt)); 29229566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt)); 29239566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmGradInt)); 29249566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj)); 2925b27d5b9eSToby Isaac } 2926b27d5b9eSToby Isaac *gradDM = (DM)gradobj; 2927b27d5b9eSToby Isaac } 2928b27d5b9eSToby Isaac PetscFunctionReturn(0); 2929b27d5b9eSToby Isaac } 2930d6143a4eSToby Isaac 29319371c9d4SSatish Balay static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess) { 29329d150b73SToby Isaac PetscInt l, m; 29339d150b73SToby Isaac 2934cd345991SToby Isaac PetscFunctionBeginHot; 29359d150b73SToby Isaac if (dimC == dimR && dimR <= 3) { 29369d150b73SToby Isaac /* invert Jacobian, multiply */ 29379d150b73SToby Isaac PetscScalar det, idet; 29389d150b73SToby Isaac 29399d150b73SToby Isaac switch (dimR) { 29409371c9d4SSatish Balay case 1: invJ[0] = 1. / J[0]; break; 29419d150b73SToby Isaac case 2: 29429d150b73SToby Isaac det = J[0] * J[3] - J[1] * J[2]; 29439d150b73SToby Isaac idet = 1. / det; 29449d150b73SToby Isaac invJ[0] = J[3] * idet; 29459d150b73SToby Isaac invJ[1] = -J[1] * idet; 29469d150b73SToby Isaac invJ[2] = -J[2] * idet; 29479d150b73SToby Isaac invJ[3] = J[0] * idet; 29489d150b73SToby Isaac break; 29499371c9d4SSatish Balay case 3: { 29509d150b73SToby Isaac invJ[0] = J[4] * J[8] - J[5] * J[7]; 29519d150b73SToby Isaac invJ[1] = J[2] * J[7] - J[1] * J[8]; 29529d150b73SToby Isaac invJ[2] = J[1] * J[5] - J[2] * J[4]; 29539d150b73SToby Isaac det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6]; 29549d150b73SToby Isaac idet = 1. / det; 29559d150b73SToby Isaac invJ[0] *= idet; 29569d150b73SToby Isaac invJ[1] *= idet; 29579d150b73SToby Isaac invJ[2] *= idet; 29589d150b73SToby Isaac invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]); 29599d150b73SToby Isaac invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]); 29609d150b73SToby Isaac invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]); 29619d150b73SToby Isaac invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]); 29629d150b73SToby Isaac invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]); 29639d150b73SToby Isaac invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]); 29649371c9d4SSatish Balay } break; 29659d150b73SToby Isaac } 29669d150b73SToby Isaac for (l = 0; l < dimR; l++) { 29679371c9d4SSatish Balay for (m = 0; m < dimC; m++) { guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m]; } 29689d150b73SToby Isaac } 29699d150b73SToby Isaac } else { 29709d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX) 29719d150b73SToby Isaac char transpose = 'C'; 29729d150b73SToby Isaac #else 29739d150b73SToby Isaac char transpose = 'T'; 29749d150b73SToby Isaac #endif 29759d150b73SToby Isaac PetscBLASInt m = dimR; 29769d150b73SToby Isaac PetscBLASInt n = dimC; 29779d150b73SToby Isaac PetscBLASInt one = 1; 29789d150b73SToby Isaac PetscBLASInt worksize = dimR * dimC, info; 29799d150b73SToby Isaac 29809d150b73SToby Isaac for (l = 0; l < dimC; l++) { invJ[l] = resNeg[l]; } 29819d150b73SToby Isaac 2982792fecdfSBarry Smith PetscCallBLAS("LAPACKgels", LAPACKgels_(&transpose, &m, &n, &one, J, &m, invJ, &n, work, &worksize, &info)); 298308401ef6SPierre Jolivet PetscCheck(info == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Bad argument to GELS"); 29849d150b73SToby Isaac 2985c6e120d1SToby Isaac for (l = 0; l < dimR; l++) { guess[l] += PetscRealPart(invJ[l]); } 29869d150b73SToby Isaac } 29879d150b73SToby Isaac PetscFunctionReturn(0); 29889d150b73SToby Isaac } 29899d150b73SToby Isaac 29909371c9d4SSatish Balay static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR) { 2991c0cbe899SToby Isaac PetscInt coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR); 29929d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 29939d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg; 29949d150b73SToby Isaac PetscScalar *J, *invJ, *work; 29959d150b73SToby Isaac 29969d150b73SToby Isaac PetscFunctionBegin; 29979d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 29989566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 29991dca8a05SBarry Smith PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize); 30009566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData)); 30019566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J)); 30029d150b73SToby Isaac cellCoords = &cellData[0]; 30039d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 30049d150b73SToby Isaac extJ = &cellData[2 * coordSize]; 30059d150b73SToby Isaac resNeg = &cellData[2 * coordSize + dimR]; 30069d150b73SToby Isaac invJ = &J[dimR * dimC]; 30079d150b73SToby Isaac work = &J[2 * dimR * dimC]; 30089d150b73SToby Isaac if (dimR == 2) { 30099d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 30109d150b73SToby Isaac 30119d150b73SToby Isaac for (i = 0; i < 4; i++) { 30129d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 30139d150b73SToby Isaac 30149371c9d4SSatish Balay for (j = 0; j < dimC; j++) { cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); } 30159d150b73SToby Isaac } 30169d150b73SToby Isaac } else if (dimR == 3) { 30179d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 30189d150b73SToby Isaac 30199d150b73SToby Isaac for (i = 0; i < 8; i++) { 30209d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 30219d150b73SToby Isaac 30229371c9d4SSatish Balay for (j = 0; j < dimC; j++) { cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); } 30239d150b73SToby Isaac } 30249d150b73SToby Isaac } else { 30259d150b73SToby Isaac for (i = 0; i < coordSize; i++) { cellCoords[i] = PetscRealPart(coordsScalar[i]); } 30269d150b73SToby Isaac } 30279d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 30289d150b73SToby Isaac for (i = 0; i < dimR; i++) { 30299d150b73SToby Isaac PetscReal *swap; 30309d150b73SToby Isaac 30319d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 30329d150b73SToby Isaac for (k = 0; k < dimC; k++) { 30339d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 30349d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 30359d150b73SToby Isaac } 30369d150b73SToby Isaac } 30379d150b73SToby Isaac 30389d150b73SToby Isaac if (i < dimR - 1) { 30399d150b73SToby Isaac swap = cellCoeffs; 30409d150b73SToby Isaac cellCoeffs = cellCoords; 30419d150b73SToby Isaac cellCoords = swap; 30429d150b73SToby Isaac } 30439d150b73SToby Isaac } 30449566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(refCoords, numPoints * dimR)); 30459d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 30469d150b73SToby Isaac for (i = 0; i < maxIts; i++) { 30479d150b73SToby Isaac PetscReal *guess = &refCoords[dimR * j]; 30489d150b73SToby Isaac 30499d150b73SToby Isaac /* compute -residual and Jacobian */ 30509d150b73SToby Isaac for (k = 0; k < dimC; k++) { resNeg[k] = realCoords[dimC * j + k]; } 30519d150b73SToby Isaac for (k = 0; k < dimC * dimR; k++) { J[k] = 0.; } 30529d150b73SToby Isaac for (k = 0; k < numV; k++) { 30539d150b73SToby Isaac PetscReal extCoord = 1.; 30549d150b73SToby Isaac for (l = 0; l < dimR; l++) { 30559d150b73SToby Isaac PetscReal coord = guess[l]; 30569d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 30579d150b73SToby Isaac 30589d150b73SToby Isaac extCoord *= dep * coord + !dep; 30599d150b73SToby Isaac extJ[l] = dep; 30609d150b73SToby Isaac 30619d150b73SToby Isaac for (m = 0; m < dimR; m++) { 30629d150b73SToby Isaac PetscReal coord = guess[m]; 30639d150b73SToby Isaac PetscInt dep = ((k & (1 << m)) >> m) && (m != l); 30649d150b73SToby Isaac PetscReal mult = dep * coord + !dep; 30659d150b73SToby Isaac 30669d150b73SToby Isaac extJ[l] *= mult; 30679d150b73SToby Isaac } 30689d150b73SToby Isaac } 30699d150b73SToby Isaac for (l = 0; l < dimC; l++) { 30709d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 30719d150b73SToby Isaac 30729d150b73SToby Isaac resNeg[l] -= coeff * extCoord; 30739371c9d4SSatish Balay for (m = 0; m < dimR; m++) { J[dimR * l + m] += coeff * extJ[m]; } 30749d150b73SToby Isaac } 30759d150b73SToby Isaac } 307676bd3646SJed Brown if (0 && PetscDefined(USE_DEBUG)) { 30770611203eSToby Isaac PetscReal maxAbs = 0.; 30780611203eSToby Isaac 30799371c9d4SSatish Balay for (l = 0; l < dimC; l++) { maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l])); } 308063a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs)); 30810611203eSToby Isaac } 30829d150b73SToby Isaac 30839566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(dimC, dimR, J, invJ, work, resNeg, guess)); 30849d150b73SToby Isaac } 30859d150b73SToby Isaac } 30869566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J)); 30879566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData)); 30889566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 30899d150b73SToby Isaac PetscFunctionReturn(0); 30909d150b73SToby Isaac } 30919d150b73SToby Isaac 30929371c9d4SSatish Balay static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR) { 30939d150b73SToby Isaac PetscInt coordSize, i, j, k, l, numV = (1 << dimR); 30949d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 30959d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs; 30969d150b73SToby Isaac 30979d150b73SToby Isaac PetscFunctionBegin; 30989d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 30999566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 31001dca8a05SBarry Smith PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize); 31019566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData)); 31029d150b73SToby Isaac cellCoords = &cellData[0]; 31039d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 31049d150b73SToby Isaac if (dimR == 2) { 31059d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 31069d150b73SToby Isaac 31079d150b73SToby Isaac for (i = 0; i < 4; i++) { 31089d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 31099d150b73SToby Isaac 31109371c9d4SSatish Balay for (j = 0; j < dimC; j++) { cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); } 31119d150b73SToby Isaac } 31129d150b73SToby Isaac } else if (dimR == 3) { 31139d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 31149d150b73SToby Isaac 31159d150b73SToby Isaac for (i = 0; i < 8; i++) { 31169d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 31179d150b73SToby Isaac 31189371c9d4SSatish Balay for (j = 0; j < dimC; j++) { cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); } 31199d150b73SToby Isaac } 31209d150b73SToby Isaac } else { 31219d150b73SToby Isaac for (i = 0; i < coordSize; i++) { cellCoords[i] = PetscRealPart(coordsScalar[i]); } 31229d150b73SToby Isaac } 31239d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 31249d150b73SToby Isaac for (i = 0; i < dimR; i++) { 31259d150b73SToby Isaac PetscReal *swap; 31269d150b73SToby Isaac 31279d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 31289d150b73SToby Isaac for (k = 0; k < dimC; k++) { 31299d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 31309d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 31319d150b73SToby Isaac } 31329d150b73SToby Isaac } 31339d150b73SToby Isaac 31349d150b73SToby Isaac if (i < dimR - 1) { 31359d150b73SToby Isaac swap = cellCoeffs; 31369d150b73SToby Isaac cellCoeffs = cellCoords; 31379d150b73SToby Isaac cellCoords = swap; 31389d150b73SToby Isaac } 31399d150b73SToby Isaac } 31409566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(realCoords, numPoints * dimC)); 31419d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 31429d150b73SToby Isaac const PetscReal *guess = &refCoords[dimR * j]; 31439d150b73SToby Isaac PetscReal *mapped = &realCoords[dimC * j]; 31449d150b73SToby Isaac 31459d150b73SToby Isaac for (k = 0; k < numV; k++) { 31469d150b73SToby Isaac PetscReal extCoord = 1.; 31479d150b73SToby Isaac for (l = 0; l < dimR; l++) { 31489d150b73SToby Isaac PetscReal coord = guess[l]; 31499d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 31509d150b73SToby Isaac 31519d150b73SToby Isaac extCoord *= dep * coord + !dep; 31529d150b73SToby Isaac } 31539d150b73SToby Isaac for (l = 0; l < dimC; l++) { 31549d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 31559d150b73SToby Isaac 31569d150b73SToby Isaac mapped[l] += coeff * extCoord; 31579d150b73SToby Isaac } 31589d150b73SToby Isaac } 31599d150b73SToby Isaac } 31609566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData)); 31619566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 31629d150b73SToby Isaac PetscFunctionReturn(0); 31639d150b73SToby Isaac } 31649d150b73SToby Isaac 31659c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */ 31669371c9d4SSatish Balay static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR) { 31679c3cf19fSMatthew G. Knepley PetscInt numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize; 3168c6e120d1SToby Isaac PetscScalar *nodes = NULL; 3169c6e120d1SToby Isaac PetscReal *invV, *modes; 3170c6e120d1SToby Isaac PetscReal *B, *D, *resNeg; 3171c6e120d1SToby Isaac PetscScalar *J, *invJ, *work; 31729d150b73SToby Isaac 31739d150b73SToby Isaac PetscFunctionBegin; 31749566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 31759566063dSJacob Faibussowitsch PetscCall(PetscFEGetNumComponents(fe, &numComp)); 317663a3b9bcSJacob Faibussowitsch PetscCheck(numComp == Nc, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "coordinate discretization must have as many components (%" PetscInt_FMT ") as embedding dimension (!= %" PetscInt_FMT ")", numComp, Nc); 31779566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 31789d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 31799566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes)); 31809d150b73SToby Isaac invV = fe->invV; 3181012b7cc6SMatthew G. Knepley for (i = 0; i < pdim; ++i) { 3182012b7cc6SMatthew G. Knepley modes[i] = 0.; 31839371c9d4SSatish Balay for (j = 0; j < pdim; ++j) { modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); } 31849d150b73SToby Isaac } 31859566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B)); 31869c3cf19fSMatthew G. Knepley D = &B[pdim * Nc]; 31879c3cf19fSMatthew G. Knepley resNeg = &D[pdim * Nc * dimR]; 31889566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J)); 31899c3cf19fSMatthew G. Knepley invJ = &J[Nc * dimR]; 31909c3cf19fSMatthew G. Knepley work = &invJ[Nc * dimR]; 31919d150b73SToby Isaac for (i = 0; i < numPoints * dimR; i++) { refCoords[i] = 0.; } 31929d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 31939b1f03cbSToby 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 */ 31949d150b73SToby Isaac PetscReal *guess = &refCoords[j * dimR]; 31959566063dSJacob Faibussowitsch PetscCall(PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL)); 31969c3cf19fSMatthew G. Knepley for (k = 0; k < Nc; k++) { resNeg[k] = realCoords[j * Nc + k]; } 31979c3cf19fSMatthew G. Knepley for (k = 0; k < Nc * dimR; k++) { J[k] = 0.; } 31989c3cf19fSMatthew G. Knepley for (k = 0; k < pdim; k++) { 31999c3cf19fSMatthew G. Knepley for (l = 0; l < Nc; l++) { 3200012b7cc6SMatthew G. Knepley resNeg[l] -= modes[k] * B[k * Nc + l]; 32019371c9d4SSatish Balay for (m = 0; m < dimR; m++) { J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m]; } 32029d150b73SToby Isaac } 32039d150b73SToby Isaac } 320476bd3646SJed Brown if (0 && PetscDefined(USE_DEBUG)) { 32050611203eSToby Isaac PetscReal maxAbs = 0.; 32060611203eSToby Isaac 32079371c9d4SSatish Balay for (l = 0; l < Nc; l++) { maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l])); } 320863a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs)); 32090611203eSToby Isaac } 32109566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(Nc, dimR, J, invJ, work, resNeg, guess)); 32119d150b73SToby Isaac } 32129d150b73SToby Isaac } 32139566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J)); 32149566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B)); 32159566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes)); 32169566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 32179d150b73SToby Isaac PetscFunctionReturn(0); 32189d150b73SToby Isaac } 32199d150b73SToby Isaac 32209c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */ 32219371c9d4SSatish Balay static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR) { 32229c3cf19fSMatthew G. Knepley PetscInt numComp, pdim, i, j, k, l, coordSize; 3223c6e120d1SToby Isaac PetscScalar *nodes = NULL; 3224c6e120d1SToby Isaac PetscReal *invV, *modes; 32259d150b73SToby Isaac PetscReal *B; 32269d150b73SToby Isaac 32279d150b73SToby Isaac PetscFunctionBegin; 32289566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 32299566063dSJacob Faibussowitsch PetscCall(PetscFEGetNumComponents(fe, &numComp)); 323063a3b9bcSJacob Faibussowitsch PetscCheck(numComp == Nc, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "coordinate discretization must have as many components (%" PetscInt_FMT ") as embedding dimension (!= %" PetscInt_FMT ")", numComp, Nc); 32319566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 32329d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 32339566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes)); 32349d150b73SToby Isaac invV = fe->invV; 3235012b7cc6SMatthew G. Knepley for (i = 0; i < pdim; ++i) { 3236012b7cc6SMatthew G. Knepley modes[i] = 0.; 32379371c9d4SSatish Balay for (j = 0; j < pdim; ++j) { modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); } 32389d150b73SToby Isaac } 32399566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B)); 32409566063dSJacob Faibussowitsch PetscCall(PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL)); 32419c3cf19fSMatthew G. Knepley for (i = 0; i < numPoints * Nc; i++) { realCoords[i] = 0.; } 32429d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 32439c3cf19fSMatthew G. Knepley PetscReal *mapped = &realCoords[j * Nc]; 32449d150b73SToby Isaac 32459c3cf19fSMatthew G. Knepley for (k = 0; k < pdim; k++) { 32469371c9d4SSatish Balay for (l = 0; l < Nc; l++) { mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l]; } 32479d150b73SToby Isaac } 32489d150b73SToby Isaac } 32499566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B)); 32509566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes)); 32519566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 32529d150b73SToby Isaac PetscFunctionReturn(0); 32539d150b73SToby Isaac } 32549d150b73SToby Isaac 3255d6143a4eSToby Isaac /*@ 3256d6143a4eSToby Isaac DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element 3257d6143a4eSToby Isaac map. This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not 3258d6143a4eSToby Isaac extend uniquely outside the reference cell (e.g, most non-affine maps) 3259d6143a4eSToby Isaac 3260d6143a4eSToby Isaac Not collective 3261d6143a4eSToby Isaac 3262d6143a4eSToby Isaac Input Parameters: 3263d6143a4eSToby Isaac + dm - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or 3264d6143a4eSToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 3265d6143a4eSToby Isaac as a multilinear map for tensor-product elements 3266d6143a4eSToby Isaac . cell - the cell whose map is used. 3267d6143a4eSToby Isaac . numPoints - the number of points to locate 32681b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim()) 3269d6143a4eSToby Isaac 3270d6143a4eSToby Isaac Output Parameters: 3271d6143a4eSToby Isaac . refCoords - (numPoints x dimension) array of reference coordinates (see DMGetDimension()) 32721b266c99SBarry Smith 32731b266c99SBarry Smith Level: intermediate 327473c9229bSMatthew Knepley 3275db781477SPatrick Sanan .seealso: `DMPlexReferenceToCoordinates()` 3276d6143a4eSToby Isaac @*/ 32779371c9d4SSatish Balay PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[]) { 3278485ad865SMatthew G. Knepley PetscInt dimC, dimR, depth, cStart, cEnd, i; 32799d150b73SToby Isaac DM coordDM = NULL; 32809d150b73SToby Isaac Vec coords; 32819d150b73SToby Isaac PetscFE fe = NULL; 32829d150b73SToby Isaac 3283d6143a4eSToby Isaac PetscFunctionBegin; 32849d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 32859566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dimR)); 32869566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dimC)); 32879d150b73SToby Isaac if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0); 32889566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 32899566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coords)); 32909566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &coordDM)); 32919d150b73SToby Isaac if (coordDM) { 32929d150b73SToby Isaac PetscInt coordFields; 32939d150b73SToby Isaac 32949566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(coordDM, &coordFields)); 32959d150b73SToby Isaac if (coordFields) { 32969d150b73SToby Isaac PetscClassId id; 32979d150b73SToby Isaac PetscObject disc; 32989d150b73SToby Isaac 32999566063dSJacob Faibussowitsch PetscCall(DMGetField(coordDM, 0, NULL, &disc)); 33009566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 33019371c9d4SSatish Balay if (id == PETSCFE_CLASSID) { fe = (PetscFE)disc; } 33029d150b73SToby Isaac } 33039d150b73SToby Isaac } 33049566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 33051dca8a05SBarry Smith PetscCheck(cell >= cStart && cell < cEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "point %" PetscInt_FMT " not in cell range [%" PetscInt_FMT ",%" PetscInt_FMT ")", cell, cStart, cEnd); 33069d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 33079d150b73SToby Isaac PetscInt coneSize; 33089d150b73SToby Isaac PetscBool isSimplex, isTensor; 33099d150b73SToby Isaac 33109566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 33119d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 33129d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 33139d150b73SToby Isaac if (isSimplex) { 33149d150b73SToby Isaac PetscReal detJ, *v0, *J, *invJ; 33159d150b73SToby Isaac 33169566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 33179d150b73SToby Isaac J = &v0[dimC]; 33189d150b73SToby Isaac invJ = &J[dimC * dimC]; 33199566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ)); 33209d150b73SToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */ 3321c330f8ffSToby Isaac const PetscReal x0[3] = {-1., -1., -1.}; 3322c330f8ffSToby Isaac 3323c330f8ffSToby Isaac CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]); 33249d150b73SToby Isaac } 33259566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 33269d150b73SToby Isaac } else if (isTensor) { 33279566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR)); 332863a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize); 33299d150b73SToby Isaac } else { 33309566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR)); 33319d150b73SToby Isaac } 33329d150b73SToby Isaac PetscFunctionReturn(0); 33339d150b73SToby Isaac } 33349d150b73SToby Isaac 33359d150b73SToby Isaac /*@ 33369d150b73SToby Isaac DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map. 33379d150b73SToby Isaac 33389d150b73SToby Isaac Not collective 33399d150b73SToby Isaac 33409d150b73SToby Isaac Input Parameters: 33419d150b73SToby Isaac + dm - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or 33429d150b73SToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 33439d150b73SToby Isaac as a multilinear map for tensor-product elements 33449d150b73SToby Isaac . cell - the cell whose map is used. 33459d150b73SToby Isaac . numPoints - the number of points to locate 3346a2b725a8SWilliam Gropp - refCoords - (numPoints x dimension) array of reference coordinates (see DMGetDimension()) 33479d150b73SToby Isaac 33489d150b73SToby Isaac Output Parameters: 33499d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim()) 33501b266c99SBarry Smith 33511b266c99SBarry Smith Level: intermediate 335273c9229bSMatthew Knepley 3353db781477SPatrick Sanan .seealso: `DMPlexCoordinatesToReference()` 33549d150b73SToby Isaac @*/ 33559371c9d4SSatish Balay PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[]) { 3356485ad865SMatthew G. Knepley PetscInt dimC, dimR, depth, cStart, cEnd, i; 33579d150b73SToby Isaac DM coordDM = NULL; 33589d150b73SToby Isaac Vec coords; 33599d150b73SToby Isaac PetscFE fe = NULL; 33609d150b73SToby Isaac 33619d150b73SToby Isaac PetscFunctionBegin; 33629d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 33639566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dimR)); 33649566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dimC)); 33659d150b73SToby Isaac if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0); 33669566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 33679566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coords)); 33689566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &coordDM)); 33699d150b73SToby Isaac if (coordDM) { 33709d150b73SToby Isaac PetscInt coordFields; 33719d150b73SToby Isaac 33729566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(coordDM, &coordFields)); 33739d150b73SToby Isaac if (coordFields) { 33749d150b73SToby Isaac PetscClassId id; 33759d150b73SToby Isaac PetscObject disc; 33769d150b73SToby Isaac 33779566063dSJacob Faibussowitsch PetscCall(DMGetField(coordDM, 0, NULL, &disc)); 33789566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 33799371c9d4SSatish Balay if (id == PETSCFE_CLASSID) { fe = (PetscFE)disc; } 33809d150b73SToby Isaac } 33819d150b73SToby Isaac } 33829566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 33831dca8a05SBarry Smith PetscCheck(cell >= cStart && cell < cEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "point %" PetscInt_FMT " not in cell range [%" PetscInt_FMT ",%" PetscInt_FMT ")", cell, cStart, cEnd); 33849d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 33859d150b73SToby Isaac PetscInt coneSize; 33869d150b73SToby Isaac PetscBool isSimplex, isTensor; 33879d150b73SToby Isaac 33889566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 33899d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 33909d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 33919d150b73SToby Isaac if (isSimplex) { 33929d150b73SToby Isaac PetscReal detJ, *v0, *J; 33939d150b73SToby Isaac 33949566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 33959d150b73SToby Isaac J = &v0[dimC]; 33969566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ)); 3397c330f8ffSToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */ 3398c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 3399c330f8ffSToby Isaac 3400c330f8ffSToby Isaac CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]); 34019d150b73SToby Isaac } 34029566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 34039d150b73SToby Isaac } else if (isTensor) { 34049566063dSJacob Faibussowitsch PetscCall(DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR)); 340563a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize); 34069d150b73SToby Isaac } else { 34079566063dSJacob Faibussowitsch PetscCall(DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR)); 34089d150b73SToby Isaac } 3409d6143a4eSToby Isaac PetscFunctionReturn(0); 3410d6143a4eSToby Isaac } 34110139fca9SMatthew G. Knepley 34120139fca9SMatthew G. Knepley /*@C 34130139fca9SMatthew G. Knepley DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates. 34140139fca9SMatthew G. Knepley 34150139fca9SMatthew G. Knepley Not collective 34160139fca9SMatthew G. Knepley 34170139fca9SMatthew G. Knepley Input Parameters: 34180139fca9SMatthew G. Knepley + dm - The DM 34190139fca9SMatthew G. Knepley . time - The time 34200139fca9SMatthew G. Knepley - func - The function transforming current coordinates to new coordaintes 34210139fca9SMatthew G. Knepley 34220139fca9SMatthew G. Knepley Calling sequence of func: 34230139fca9SMatthew G. Knepley $ func(PetscInt dim, PetscInt Nf, PetscInt NfAux, 34240139fca9SMatthew G. Knepley $ const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 34250139fca9SMatthew G. Knepley $ const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 34260139fca9SMatthew G. Knepley $ PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]); 34270139fca9SMatthew G. Knepley 34280139fca9SMatthew G. Knepley + dim - The spatial dimension 34290139fca9SMatthew G. Knepley . Nf - The number of input fields (here 1) 34300139fca9SMatthew G. Knepley . NfAux - The number of input auxiliary fields 34310139fca9SMatthew G. Knepley . uOff - The offset of the coordinates in u[] (here 0) 34320139fca9SMatthew G. Knepley . uOff_x - The offset of the coordinates in u_x[] (here 0) 34330139fca9SMatthew G. Knepley . u - The coordinate values at this point in space 34340139fca9SMatthew G. Knepley . u_t - The coordinate time derivative at this point in space (here NULL) 34350139fca9SMatthew G. Knepley . u_x - The coordinate derivatives at this point in space 34360139fca9SMatthew G. Knepley . aOff - The offset of each auxiliary field in u[] 34370139fca9SMatthew G. Knepley . aOff_x - The offset of each auxiliary field in u_x[] 34380139fca9SMatthew G. Knepley . a - The auxiliary field values at this point in space 34390139fca9SMatthew G. Knepley . a_t - The auxiliary field time derivative at this point in space (or NULL) 34400139fca9SMatthew G. Knepley . a_x - The auxiliary field derivatives at this point in space 34410139fca9SMatthew G. Knepley . t - The current time 34420139fca9SMatthew G. Knepley . x - The coordinates of this point (here not used) 34430139fca9SMatthew G. Knepley . numConstants - The number of constants 34440139fca9SMatthew G. Knepley . constants - The value of each constant 34450139fca9SMatthew G. Knepley - f - The new coordinates at this point in space 34460139fca9SMatthew G. Knepley 34470139fca9SMatthew G. Knepley Level: intermediate 34480139fca9SMatthew G. Knepley 3449db781477SPatrick Sanan .seealso: `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMProjectFieldLocal()`, `DMProjectFieldLabelLocal()` 34500139fca9SMatthew G. Knepley @*/ 34519371c9d4SSatish Balay PetscErrorCode DMPlexRemapGeometry(DM dm, PetscReal time, void (*func)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) { 34520139fca9SMatthew G. Knepley DM cdm; 34538bf1a49fSMatthew G. Knepley DMField cf; 34540139fca9SMatthew G. Knepley Vec lCoords, tmpCoords; 34550139fca9SMatthew G. Knepley 34560139fca9SMatthew G. Knepley PetscFunctionBegin; 34579566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 34589566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &lCoords)); 34599566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(cdm, &tmpCoords)); 34609566063dSJacob Faibussowitsch PetscCall(VecCopy(lCoords, tmpCoords)); 34618bf1a49fSMatthew G. Knepley /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */ 34629566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateField(dm, &cf)); 34636858538eSMatthew G. Knepley cdm->coordinates[0].field = cf; 34649566063dSJacob Faibussowitsch PetscCall(DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords)); 34656858538eSMatthew G. Knepley cdm->coordinates[0].field = NULL; 34669566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(cdm, &tmpCoords)); 34679566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, lCoords)); 34680139fca9SMatthew G. Knepley PetscFunctionReturn(0); 34690139fca9SMatthew G. Knepley } 34700139fca9SMatthew G. Knepley 34710139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z, 34720139fca9SMatthew G. Knepley / 1 0 m_0 \ 34730139fca9SMatthew G. Knepley | 0 1 m_1 | 34740139fca9SMatthew G. Knepley \ 0 0 1 / 34750139fca9SMatthew G. Knepley */ 34769371c9d4SSatish Balay static void f0_shear(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar coords[]) { 34770139fca9SMatthew G. Knepley const PetscInt Nc = uOff[1] - uOff[0]; 3478c1f1bd54SMatthew G. Knepley const PetscInt ax = (PetscInt)PetscRealPart(constants[0]); 34790139fca9SMatthew G. Knepley PetscInt c; 34800139fca9SMatthew G. Knepley 34819371c9d4SSatish Balay for (c = 0; c < Nc; ++c) { coords[c] = u[c] + constants[c + 1] * u[ax]; } 34820139fca9SMatthew G. Knepley } 34830139fca9SMatthew G. Knepley 34840139fca9SMatthew G. Knepley /*@C 34850139fca9SMatthew G. Knepley DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates. 34860139fca9SMatthew G. Knepley 34870139fca9SMatthew G. Knepley Not collective 34880139fca9SMatthew G. Knepley 34890139fca9SMatthew G. Knepley Input Parameters: 34900139fca9SMatthew G. Knepley + dm - The DM 34913ee9839eSMatthew G. Knepley . direction - The shear coordinate direction, e.g. 0 is the x-axis 34920139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction 34930139fca9SMatthew G. Knepley 34940139fca9SMatthew G. Knepley Level: intermediate 34950139fca9SMatthew G. Knepley 3496db781477SPatrick Sanan .seealso: `DMPlexRemapGeometry()` 34970139fca9SMatthew G. Knepley @*/ 34989371c9d4SSatish Balay PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[]) { 34990139fca9SMatthew G. Knepley DM cdm; 35000139fca9SMatthew G. Knepley PetscDS cds; 35010139fca9SMatthew G. Knepley PetscObject obj; 35020139fca9SMatthew G. Knepley PetscClassId id; 35030139fca9SMatthew G. Knepley PetscScalar *moduli; 35043ee9839eSMatthew G. Knepley const PetscInt dir = (PetscInt)direction; 35050139fca9SMatthew G. Knepley PetscInt dE, d, e; 35060139fca9SMatthew G. Knepley 35070139fca9SMatthew G. Knepley PetscFunctionBegin; 35089566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 35099566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dE)); 35109566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(dE + 1, &moduli)); 35110139fca9SMatthew G. Knepley moduli[0] = dir; 3512cdaaecf7SMatthew G. Knepley for (d = 0, e = 0; d < dE; ++d) moduli[d + 1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0); 35139566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &cds)); 35149566063dSJacob Faibussowitsch PetscCall(PetscDSGetDiscretization(cds, 0, &obj)); 35159566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(obj, &id)); 35160139fca9SMatthew G. Knepley if (id != PETSCFE_CLASSID) { 35170139fca9SMatthew G. Knepley Vec lCoords; 35180139fca9SMatthew G. Knepley PetscSection cSection; 35190139fca9SMatthew G. Knepley PetscScalar *coords; 35200139fca9SMatthew G. Knepley PetscInt vStart, vEnd, v; 35210139fca9SMatthew G. Knepley 35229566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 35239566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &cSection)); 35249566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &lCoords)); 35259566063dSJacob Faibussowitsch PetscCall(VecGetArray(lCoords, &coords)); 35260139fca9SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 35270139fca9SMatthew G. Knepley PetscReal ds; 35280139fca9SMatthew G. Knepley PetscInt off, c; 35290139fca9SMatthew G. Knepley 35309566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cSection, v, &off)); 35310139fca9SMatthew G. Knepley ds = PetscRealPart(coords[off + dir]); 35320139fca9SMatthew G. Knepley for (c = 0; c < dE; ++c) coords[off + c] += moduli[c] * ds; 35330139fca9SMatthew G. Knepley } 35349566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(lCoords, &coords)); 35350139fca9SMatthew G. Knepley } else { 35369566063dSJacob Faibussowitsch PetscCall(PetscDSSetConstants(cds, dE + 1, moduli)); 35379566063dSJacob Faibussowitsch PetscCall(DMPlexRemapGeometry(dm, 0.0, f0_shear)); 35380139fca9SMatthew G. Knepley } 35399566063dSJacob Faibussowitsch PetscCall(PetscFree(moduli)); 35400139fca9SMatthew G. Knepley PetscFunctionReturn(0); 35410139fca9SMatthew G. Knepley } 3542