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 920f4b53cSBarry Smith Not Collective (provided `DMGetCoordinatesLocalSetUp()` has been already called) 103985bb02SVaclav Hapla 113985bb02SVaclav Hapla Input Parameters: 1220f4b53cSBarry Smith + dm - The `DMPLEX` object 1320f4b53cSBarry Smith . coordinates - The `Vec` of coordinates of the sought points 1420f4b53cSBarry Smith - eps - The tolerance or `PETSC_DEFAULT` 153985bb02SVaclav Hapla 162fe279fdSBarry Smith Output Parameter: 1720f4b53cSBarry Smith . points - The `IS` of found DAG points or -1 183985bb02SVaclav Hapla 193985bb02SVaclav Hapla Level: intermediate 203985bb02SVaclav Hapla 213985bb02SVaclav Hapla Notes: 2220f4b53cSBarry Smith 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 2420f4b53cSBarry Smith The output `IS` is living on `PETSC_COMM_SELF` and its length is npoints. 25d3e1f4ccSVaclav Hapla Each rank does the search independently. 2620f4b53cSBarry Smith 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 2820f4b53cSBarry Smith 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 3420f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexCreate()`, `DMGetCoordinatesLocal()` 353985bb02SVaclav Hapla @*/ 36d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points) 37d71ae5a4SJacob Faibussowitsch { 3837900f7dSMatthew G. Knepley PetscInt c, cdim, i, j, o, p, vStart, vEnd; 39d3e1f4ccSVaclav Hapla PetscInt npoints; 40d3e1f4ccSVaclav Hapla const PetscScalar *coord; 413985bb02SVaclav Hapla Vec allCoordsVec; 423985bb02SVaclav Hapla const PetscScalar *allCoords; 43d3e1f4ccSVaclav Hapla PetscInt *dagPoints; 443985bb02SVaclav Hapla 453985bb02SVaclav Hapla PetscFunctionBegin; 463985bb02SVaclav Hapla if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON; 479566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 48d3e1f4ccSVaclav Hapla { 49d3e1f4ccSVaclav Hapla PetscInt n; 50d3e1f4ccSVaclav Hapla 519566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(coordinates, &n)); 5263a3b9bcSJacob 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); 53d3e1f4ccSVaclav Hapla npoints = n / cdim; 54d3e1f4ccSVaclav Hapla } 559566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &allCoordsVec)); 569566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(allCoordsVec, &allCoords)); 579566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coord)); 589566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 5976bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 60335ef845SVaclav Hapla /* check coordinate section is consistent with DM dimension */ 61335ef845SVaclav Hapla PetscSection cs; 62335ef845SVaclav Hapla PetscInt ndof; 63335ef845SVaclav Hapla 649566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &cs)); 653985bb02SVaclav Hapla for (p = vStart; p < vEnd; p++) { 669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cs, p, &ndof)); 6763a3b9bcSJacob Faibussowitsch PetscCheck(ndof == cdim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %" PetscInt_FMT ": ndof = %" PetscInt_FMT " != %" PetscInt_FMT " = cdim", p, ndof, cdim); 68335ef845SVaclav Hapla } 69335ef845SVaclav Hapla } 709566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npoints, &dagPoints)); 71eca9f518SVaclav Hapla if (eps == 0.0) { 7237900f7dSMatthew G. Knepley for (i = 0, j = 0; i < npoints; i++, j += cdim) { 73eca9f518SVaclav Hapla dagPoints[i] = -1; 7437900f7dSMatthew G. Knepley for (p = vStart, o = 0; p < vEnd; p++, o += cdim) { 7537900f7dSMatthew G. Knepley for (c = 0; c < cdim; c++) { 76d3e1f4ccSVaclav Hapla if (coord[j + c] != allCoords[o + c]) break; 77eca9f518SVaclav Hapla } 7837900f7dSMatthew G. Knepley if (c == cdim) { 79eca9f518SVaclav Hapla dagPoints[i] = p; 80eca9f518SVaclav Hapla break; 81eca9f518SVaclav Hapla } 82eca9f518SVaclav Hapla } 83eca9f518SVaclav Hapla } 84d3e1f4ccSVaclav Hapla } else { 8537900f7dSMatthew G. Knepley for (i = 0, j = 0; i < npoints; i++, j += cdim) { 86d3e1f4ccSVaclav Hapla PetscReal norm; 87d3e1f4ccSVaclav Hapla 88335ef845SVaclav Hapla dagPoints[i] = -1; 8937900f7dSMatthew G. Knepley for (p = vStart, o = 0; p < vEnd; p++, o += cdim) { 903985bb02SVaclav Hapla norm = 0.0; 91ad540459SPierre Jolivet for (c = 0; c < cdim; c++) norm += PetscRealPart(PetscSqr(coord[j + c] - allCoords[o + c])); 923985bb02SVaclav Hapla norm = PetscSqrtReal(norm); 933985bb02SVaclav Hapla if (norm <= eps) { 943985bb02SVaclav Hapla dagPoints[i] = p; 953985bb02SVaclav Hapla break; 963985bb02SVaclav Hapla } 973985bb02SVaclav Hapla } 983985bb02SVaclav Hapla } 99d3e1f4ccSVaclav Hapla } 1009566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(allCoordsVec, &allCoords)); 1019566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coord)); 1029566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points)); 1033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1043985bb02SVaclav Hapla } 1053985bb02SVaclav Hapla 1066363a54bSMatthew G. Knepley #if 0 107d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection) 108d71ae5a4SJacob Faibussowitsch { 109fea14342SMatthew G. Knepley const PetscReal p0_x = segmentA[0 * 2 + 0]; 110fea14342SMatthew G. Knepley const PetscReal p0_y = segmentA[0 * 2 + 1]; 111fea14342SMatthew G. Knepley const PetscReal p1_x = segmentA[1 * 2 + 0]; 112fea14342SMatthew G. Knepley const PetscReal p1_y = segmentA[1 * 2 + 1]; 113fea14342SMatthew G. Knepley const PetscReal p2_x = segmentB[0 * 2 + 0]; 114fea14342SMatthew G. Knepley const PetscReal p2_y = segmentB[0 * 2 + 1]; 115fea14342SMatthew G. Knepley const PetscReal p3_x = segmentB[1 * 2 + 0]; 116fea14342SMatthew G. Knepley const PetscReal p3_y = segmentB[1 * 2 + 1]; 117fea14342SMatthew G. Knepley const PetscReal s1_x = p1_x - p0_x; 118fea14342SMatthew G. Knepley const PetscReal s1_y = p1_y - p0_y; 119fea14342SMatthew G. Knepley const PetscReal s2_x = p3_x - p2_x; 120fea14342SMatthew G. Knepley const PetscReal s2_y = p3_y - p2_y; 121fea14342SMatthew G. Knepley const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y); 122fea14342SMatthew G. Knepley 123fea14342SMatthew G. Knepley PetscFunctionBegin; 124fea14342SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 125fea14342SMatthew G. Knepley /* Non-parallel lines */ 126fea14342SMatthew G. Knepley if (denom != 0.0) { 127fea14342SMatthew G. Knepley const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom; 128fea14342SMatthew G. Knepley const PetscReal t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom; 129fea14342SMatthew G. Knepley 130fea14342SMatthew G. Knepley if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { 131fea14342SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 132fea14342SMatthew G. Knepley if (intersection) { 133fea14342SMatthew G. Knepley intersection[0] = p0_x + (t * s1_x); 134fea14342SMatthew G. Knepley intersection[1] = p0_y + (t * s1_y); 135fea14342SMatthew G. Knepley } 136fea14342SMatthew G. Knepley } 137fea14342SMatthew G. Knepley } 1383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 139fea14342SMatthew G. Knepley } 140fea14342SMatthew G. Knepley 141ddce0771SMatthew G. Knepley /* The plane is segmentB x segmentC: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */ 142d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetLinePlaneIntersection_3D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], const PetscReal segmentC[], PetscReal intersection[], PetscBool *hasIntersection) 143d71ae5a4SJacob Faibussowitsch { 144ddce0771SMatthew G. Knepley const PetscReal p0_x = segmentA[0 * 3 + 0]; 145ddce0771SMatthew G. Knepley const PetscReal p0_y = segmentA[0 * 3 + 1]; 146ddce0771SMatthew G. Knepley const PetscReal p0_z = segmentA[0 * 3 + 2]; 147ddce0771SMatthew G. Knepley const PetscReal p1_x = segmentA[1 * 3 + 0]; 148ddce0771SMatthew G. Knepley const PetscReal p1_y = segmentA[1 * 3 + 1]; 149ddce0771SMatthew G. Knepley const PetscReal p1_z = segmentA[1 * 3 + 2]; 150ddce0771SMatthew G. Knepley const PetscReal q0_x = segmentB[0 * 3 + 0]; 151ddce0771SMatthew G. Knepley const PetscReal q0_y = segmentB[0 * 3 + 1]; 152ddce0771SMatthew G. Knepley const PetscReal q0_z = segmentB[0 * 3 + 2]; 153ddce0771SMatthew G. Knepley const PetscReal q1_x = segmentB[1 * 3 + 0]; 154ddce0771SMatthew G. Knepley const PetscReal q1_y = segmentB[1 * 3 + 1]; 155ddce0771SMatthew G. Knepley const PetscReal q1_z = segmentB[1 * 3 + 2]; 156ddce0771SMatthew G. Knepley const PetscReal r0_x = segmentC[0 * 3 + 0]; 157ddce0771SMatthew G. Knepley const PetscReal r0_y = segmentC[0 * 3 + 1]; 158ddce0771SMatthew G. Knepley const PetscReal r0_z = segmentC[0 * 3 + 2]; 159ddce0771SMatthew G. Knepley const PetscReal r1_x = segmentC[1 * 3 + 0]; 160ddce0771SMatthew G. Knepley const PetscReal r1_y = segmentC[1 * 3 + 1]; 161ddce0771SMatthew G. Knepley const PetscReal r1_z = segmentC[1 * 3 + 2]; 162ddce0771SMatthew G. Knepley const PetscReal s0_x = p1_x - p0_x; 163ddce0771SMatthew G. Knepley const PetscReal s0_y = p1_y - p0_y; 164ddce0771SMatthew G. Knepley const PetscReal s0_z = p1_z - p0_z; 165ddce0771SMatthew G. Knepley const PetscReal s1_x = q1_x - q0_x; 166ddce0771SMatthew G. Knepley const PetscReal s1_y = q1_y - q0_y; 167ddce0771SMatthew G. Knepley const PetscReal s1_z = q1_z - q0_z; 168ddce0771SMatthew G. Knepley const PetscReal s2_x = r1_x - r0_x; 169ddce0771SMatthew G. Knepley const PetscReal s2_y = r1_y - r0_y; 170ddce0771SMatthew G. Knepley const PetscReal s2_z = r1_z - r0_z; 171ddce0771SMatthew G. Knepley const PetscReal s3_x = s1_y * s2_z - s1_z * s2_y; /* s1 x s2 */ 172ddce0771SMatthew G. Knepley const PetscReal s3_y = s1_z * s2_x - s1_x * s2_z; 173ddce0771SMatthew G. Knepley const PetscReal s3_z = s1_x * s2_y - s1_y * s2_x; 174ddce0771SMatthew G. Knepley const PetscReal s4_x = s0_y * s2_z - s0_z * s2_y; /* s0 x s2 */ 175ddce0771SMatthew G. Knepley const PetscReal s4_y = s0_z * s2_x - s0_x * s2_z; 176ddce0771SMatthew G. Knepley const PetscReal s4_z = s0_x * s2_y - s0_y * s2_x; 177ddce0771SMatthew G. Knepley const PetscReal s5_x = s1_y * s0_z - s1_z * s0_y; /* s1 x s0 */ 178ddce0771SMatthew G. Knepley const PetscReal s5_y = s1_z * s0_x - s1_x * s0_z; 179ddce0771SMatthew G. Knepley const PetscReal s5_z = s1_x * s0_y - s1_y * s0_x; 180ddce0771SMatthew G. Knepley const PetscReal denom = -(s0_x * s3_x + s0_y * s3_y + s0_z * s3_z); /* -s0 . (s1 x s2) */ 181ddce0771SMatthew G. Knepley 182ddce0771SMatthew G. Knepley PetscFunctionBegin; 183ddce0771SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 184ddce0771SMatthew G. Knepley /* Line not parallel to plane */ 185ddce0771SMatthew G. Knepley if (denom != 0.0) { 186ddce0771SMatthew G. Knepley const PetscReal t = (s3_x * (p0_x - q0_x) + s3_y * (p0_y - q0_y) + s3_z * (p0_z - q0_z)) / denom; 187ddce0771SMatthew G. Knepley const PetscReal u = (s4_x * (p0_x - q0_x) + s4_y * (p0_y - q0_y) + s4_z * (p0_z - q0_z)) / denom; 188ddce0771SMatthew G. Knepley const PetscReal v = (s5_x * (p0_x - q0_x) + s5_y * (p0_y - q0_y) + s5_z * (p0_z - q0_z)) / denom; 189ddce0771SMatthew G. Knepley 190ddce0771SMatthew G. Knepley if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && v >= 0 && v <= 1) { 191ddce0771SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 192ddce0771SMatthew G. Knepley if (intersection) { 193ddce0771SMatthew G. Knepley intersection[0] = p0_x + (t * s0_x); 194ddce0771SMatthew G. Knepley intersection[1] = p0_y + (t * s0_y); 195ddce0771SMatthew G. Knepley intersection[2] = p0_z + (t * s0_z); 196ddce0771SMatthew G. Knepley } 197ddce0771SMatthew G. Knepley } 198ddce0771SMatthew G. Knepley } 1993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 200ddce0771SMatthew G. Knepley } 2016363a54bSMatthew G. Knepley #endif 2026363a54bSMatthew G. Knepley 2036363a54bSMatthew G. Knepley static PetscErrorCode DMPlexGetPlaneSimplexIntersection_Coords_Internal(DM dm, PetscInt dim, PetscInt cdim, const PetscScalar coords[], const PetscReal p[], const PetscReal normal[], PetscBool *pos, PetscInt *Nint, PetscReal intPoints[]) 2046363a54bSMatthew G. Knepley { 2056363a54bSMatthew G. Knepley PetscReal d[4]; // distance of vertices to the plane 2066363a54bSMatthew G. Knepley PetscReal dp; // distance from origin to the plane 2076363a54bSMatthew G. Knepley PetscInt n = 0; 2086363a54bSMatthew G. Knepley 2096363a54bSMatthew G. Knepley PetscFunctionBegin; 2106363a54bSMatthew G. Knepley if (pos) *pos = PETSC_FALSE; 2116363a54bSMatthew G. Knepley if (Nint) *Nint = 0; 2126363a54bSMatthew G. Knepley if (PetscDefined(USE_DEBUG)) { 2136363a54bSMatthew G. Knepley PetscReal mag = DMPlex_NormD_Internal(cdim, normal); 214*b58dcb05SPierre Jolivet PetscCheck(PetscAbsReal(mag - (PetscReal)1.0) < PETSC_SMALL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Normal vector is not normalized: %g", (double)mag); 2156363a54bSMatthew G. Knepley } 2166363a54bSMatthew G. Knepley 2176363a54bSMatthew G. Knepley dp = DMPlex_DotRealD_Internal(cdim, normal, p); 2186363a54bSMatthew G. Knepley for (PetscInt v = 0; v < dim + 1; ++v) { 2196363a54bSMatthew G. Knepley // d[v] is positive, zero, or negative if vertex i is above, on, or below the plane 2206363a54bSMatthew G. Knepley #if defined(PETSC_USE_COMPLEX) 2216363a54bSMatthew G. Knepley PetscReal c[4]; 2226363a54bSMatthew G. Knepley for (PetscInt i = 0; i < cdim; ++i) c[i] = PetscRealPart(coords[v * cdim + i]); 2236363a54bSMatthew G. Knepley d[v] = DMPlex_DotRealD_Internal(cdim, normal, c); 2246363a54bSMatthew G. Knepley #else 2256363a54bSMatthew G. Knepley d[v] = DMPlex_DotRealD_Internal(cdim, normal, &coords[v * cdim]); 2266363a54bSMatthew G. Knepley #endif 2276363a54bSMatthew G. Knepley d[v] -= dp; 2286363a54bSMatthew G. Knepley } 2296363a54bSMatthew G. Knepley 2306363a54bSMatthew G. Knepley // If all d are positive or negative, no intersection 2316363a54bSMatthew G. Knepley { 2326363a54bSMatthew G. Knepley PetscInt v; 2336363a54bSMatthew G. Knepley for (v = 0; v < dim + 1; ++v) 2346363a54bSMatthew G. Knepley if (d[v] >= 0.) break; 2356363a54bSMatthew G. Knepley if (v == dim + 1) PetscFunctionReturn(PETSC_SUCCESS); 2366363a54bSMatthew G. Knepley for (v = 0; v < dim + 1; ++v) 2376363a54bSMatthew G. Knepley if (d[v] <= 0.) break; 2386363a54bSMatthew G. Knepley if (v == dim + 1) { 2396363a54bSMatthew G. Knepley if (pos) *pos = PETSC_TRUE; 2406363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 2416363a54bSMatthew G. Knepley } 2426363a54bSMatthew G. Knepley } 2436363a54bSMatthew G. Knepley 2446363a54bSMatthew G. Knepley for (PetscInt v = 0; v < dim + 1; ++v) { 2456363a54bSMatthew G. Knepley // Points with zero distance are automatically added to the list. 2466363a54bSMatthew G. Knepley if (PetscAbsReal(d[v]) < PETSC_MACHINE_EPSILON) { 2476363a54bSMatthew G. Knepley for (PetscInt i = 0; i < cdim; ++i) intPoints[n * cdim + i] = PetscRealPart(coords[v * cdim + i]); 2486363a54bSMatthew G. Knepley ++n; 2496363a54bSMatthew G. Knepley } else { 2506363a54bSMatthew G. Knepley // For each point with nonzero distance, seek another point with opposite sign 2516363a54bSMatthew G. Knepley // and higher index, and compute the intersection of the line between those 2526363a54bSMatthew G. Knepley // points and the plane. 2536363a54bSMatthew G. Knepley for (PetscInt w = v + 1; w < dim + 1; ++w) { 2546363a54bSMatthew G. Knepley if (d[v] * d[w] < 0.) { 2556363a54bSMatthew G. Knepley PetscReal inv_dist = 1. / (d[v] - d[w]); 2566363a54bSMatthew G. Knepley for (PetscInt i = 0; i < cdim; ++i) intPoints[n * cdim + i] = (d[v] * PetscRealPart(coords[w * cdim + i]) - d[w] * PetscRealPart(coords[v * cdim + i])) * inv_dist; 2576363a54bSMatthew G. Knepley ++n; 2586363a54bSMatthew G. Knepley } 2596363a54bSMatthew G. Knepley } 2606363a54bSMatthew G. Knepley } 2616363a54bSMatthew G. Knepley } 2626363a54bSMatthew G. Knepley // TODO order output points if there are 4 2636363a54bSMatthew G. Knepley *Nint = n; 2646363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 2656363a54bSMatthew G. Knepley } 2666363a54bSMatthew G. Knepley 2676363a54bSMatthew G. Knepley static PetscErrorCode DMPlexGetPlaneSimplexIntersection_Internal(DM dm, PetscInt dim, PetscInt c, const PetscReal p[], const PetscReal normal[], PetscBool *pos, PetscInt *Nint, PetscReal intPoints[]) 2686363a54bSMatthew G. Knepley { 2696363a54bSMatthew G. Knepley const PetscScalar *array; 2706363a54bSMatthew G. Knepley PetscScalar *coords = NULL; 2716363a54bSMatthew G. Knepley PetscInt numCoords; 2726363a54bSMatthew G. Knepley PetscBool isDG; 2736363a54bSMatthew G. Knepley PetscInt cdim; 2746363a54bSMatthew G. Knepley 2756363a54bSMatthew G. Knepley PetscFunctionBegin; 2766363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 2776363a54bSMatthew G. Knepley PetscCheck(cdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has coordinates in %" PetscInt_FMT "D instead of %" PetscInt_FMT "D", cdim, dim); 2786363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 2796363a54bSMatthew G. Knepley PetscCheck(numCoords == dim * (dim + 1), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Tetrahedron should have %" PetscInt_FMT " coordinates, not %" PetscInt_FMT, dim * (dim + 1), numCoords); 2806363a54bSMatthew G. Knepley PetscCall(PetscArrayzero(intPoints, dim * (dim + 1))); 2816363a54bSMatthew G. Knepley 2826363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, coords, p, normal, pos, Nint, intPoints)); 2836363a54bSMatthew G. Knepley 2846363a54bSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 2856363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 2866363a54bSMatthew G. Knepley } 2876363a54bSMatthew G. Knepley 2886363a54bSMatthew G. Knepley static PetscErrorCode DMPlexGetPlaneQuadIntersection_Internal(DM dm, PetscInt dim, PetscInt c, const PetscReal p[], const PetscReal normal[], PetscBool *pos, PetscInt *Nint, PetscReal intPoints[]) 2896363a54bSMatthew G. Knepley { 2906363a54bSMatthew G. Knepley const PetscScalar *array; 2916363a54bSMatthew G. Knepley PetscScalar *coords = NULL; 2926363a54bSMatthew G. Knepley PetscInt numCoords; 2936363a54bSMatthew G. Knepley PetscBool isDG; 2946363a54bSMatthew G. Knepley PetscInt cdim; 2956363a54bSMatthew G. Knepley PetscScalar tcoords[6] = {0., 0., 0., 0., 0., 0.}; 2966363a54bSMatthew G. Knepley const PetscInt vertsA[3] = {0, 1, 3}; 2976363a54bSMatthew G. Knepley const PetscInt vertsB[3] = {1, 2, 3}; 2986363a54bSMatthew G. Knepley PetscInt NintA, NintB; 2996363a54bSMatthew G. Knepley 3006363a54bSMatthew G. Knepley PetscFunctionBegin; 3016363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 3026363a54bSMatthew G. Knepley PetscCheck(cdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has coordinates in %" PetscInt_FMT "D instead of %" PetscInt_FMT "D", cdim, dim); 3036363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 3046363a54bSMatthew G. Knepley PetscCheck(numCoords == dim * 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Quadrilateral should have %" PetscInt_FMT " coordinates, not %" PetscInt_FMT, dim * 4, numCoords); 3056363a54bSMatthew G. Knepley PetscCall(PetscArrayzero(intPoints, dim * 4)); 3066363a54bSMatthew G. Knepley 3076363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 3; ++v) 3086363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsA[v] * cdim + d]; 3096363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintA, intPoints)); 3106363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 3; ++v) 3116363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsB[v] * cdim + d]; 3126363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintB, &intPoints[NintA * cdim])); 3136363a54bSMatthew G. Knepley *Nint = NintA + NintB; 3146363a54bSMatthew G. Knepley 3156363a54bSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 3166363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 3176363a54bSMatthew G. Knepley } 3186363a54bSMatthew G. Knepley 3196363a54bSMatthew G. Knepley static PetscErrorCode DMPlexGetPlaneHexIntersection_Internal(DM dm, PetscInt dim, PetscInt c, const PetscReal p[], const PetscReal normal[], PetscBool *pos, PetscInt *Nint, PetscReal intPoints[]) 3206363a54bSMatthew G. Knepley { 3216363a54bSMatthew G. Knepley const PetscScalar *array; 3226363a54bSMatthew G. Knepley PetscScalar *coords = NULL; 3236363a54bSMatthew G. Knepley PetscInt numCoords; 3246363a54bSMatthew G. Knepley PetscBool isDG; 3256363a54bSMatthew G. Knepley PetscInt cdim; 3266363a54bSMatthew G. Knepley PetscScalar tcoords[12] = {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}; 3276363a54bSMatthew G. Knepley // We split using the (2, 4) main diagonal, so all tets contain those vertices 3286363a54bSMatthew G. Knepley const PetscInt vertsA[4] = {0, 1, 2, 4}; 3296363a54bSMatthew G. Knepley const PetscInt vertsB[4] = {0, 2, 3, 4}; 3306363a54bSMatthew G. Knepley const PetscInt vertsC[4] = {1, 7, 2, 4}; 3316363a54bSMatthew G. Knepley const PetscInt vertsD[4] = {2, 7, 6, 4}; 3326363a54bSMatthew G. Knepley const PetscInt vertsE[4] = {3, 5, 4, 2}; 3336363a54bSMatthew G. Knepley const PetscInt vertsF[4] = {4, 5, 6, 2}; 3346363a54bSMatthew G. Knepley PetscInt NintA, NintB, NintC, NintD, NintE, NintF, Nsum = 0; 3356363a54bSMatthew G. Knepley 3366363a54bSMatthew G. Knepley PetscFunctionBegin; 3376363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 3386363a54bSMatthew G. Knepley PetscCheck(cdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has coordinates in %" PetscInt_FMT "D instead of %" PetscInt_FMT "D", cdim, dim); 3396363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 3406363a54bSMatthew G. Knepley PetscCheck(numCoords == dim * 8, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hexahedron should have %" PetscInt_FMT " coordinates, not %" PetscInt_FMT, dim * 8, numCoords); 3416363a54bSMatthew G. Knepley PetscCall(PetscArrayzero(intPoints, dim * 18)); 3426363a54bSMatthew G. Knepley 3436363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3446363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsA[v] * cdim + d]; 3456363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintA, &intPoints[Nsum * cdim])); 3466363a54bSMatthew G. Knepley Nsum += NintA; 3476363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3486363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsB[v] * cdim + d]; 3496363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintB, &intPoints[Nsum * cdim])); 3506363a54bSMatthew G. Knepley Nsum += NintB; 3516363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3526363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsC[v] * cdim + d]; 3536363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintC, &intPoints[Nsum * cdim])); 3546363a54bSMatthew G. Knepley Nsum += NintC; 3556363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3566363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsD[v] * cdim + d]; 3576363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintD, &intPoints[Nsum * cdim])); 3586363a54bSMatthew G. Knepley Nsum += NintD; 3596363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3606363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsE[v] * cdim + d]; 3616363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintE, &intPoints[Nsum * cdim])); 3626363a54bSMatthew G. Knepley Nsum += NintE; 3636363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3646363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsF[v] * cdim + d]; 3656363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintF, &intPoints[Nsum * cdim])); 3666363a54bSMatthew G. Knepley Nsum += NintF; 3676363a54bSMatthew G. Knepley *Nint = Nsum; 3686363a54bSMatthew G. Knepley 3696363a54bSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 3706363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 3716363a54bSMatthew G. Knepley } 3726363a54bSMatthew G. Knepley 3736363a54bSMatthew G. Knepley /* 3746363a54bSMatthew G. Knepley DMPlexGetPlaneCellIntersection_Internal - Finds the intersection of a plane with a cell 3756363a54bSMatthew G. Knepley 3766363a54bSMatthew G. Knepley Not collective 3776363a54bSMatthew G. Knepley 3786363a54bSMatthew G. Knepley Input Parameters: 3796363a54bSMatthew G. Knepley + dm - the DM 3806363a54bSMatthew G. Knepley . c - the mesh point 3816363a54bSMatthew G. Knepley . p - a point on the plane. 3826363a54bSMatthew G. Knepley - normal - a normal vector to the plane, must be normalized 3836363a54bSMatthew G. Knepley 3846363a54bSMatthew G. Knepley Output Parameters: 3856363a54bSMatthew G. Knepley . pos - `PETSC_TRUE` is the cell is on the positive side of the plane, `PETSC_FALSE` on the negative side 3866363a54bSMatthew G. Knepley + Nint - the number of intersection points, in [0, 4] 3876363a54bSMatthew G. Knepley - intPoints - the coordinates of the intersection points, should be length at least 12 3886363a54bSMatthew G. Knepley 3896363a54bSMatthew G. Knepley Note: The `pos` argument is only meaningfull if the number of intersections is 0. The algorithmic idea comes from https://github.com/chrisk314/tet-plane-intersection. 3906363a54bSMatthew G. Knepley 3916363a54bSMatthew G. Knepley Level: developer 3926363a54bSMatthew G. Knepley 3936363a54bSMatthew G. Knepley .seealso: 3946363a54bSMatthew G. Knepley @*/ 3956363a54bSMatthew G. Knepley static PetscErrorCode DMPlexGetPlaneCellIntersection_Internal(DM dm, PetscInt c, const PetscReal p[], const PetscReal normal[], PetscBool *pos, PetscInt *Nint, PetscReal intPoints[]) 3966363a54bSMatthew G. Knepley { 3976363a54bSMatthew G. Knepley DMPolytopeType ct; 3986363a54bSMatthew G. Knepley 3996363a54bSMatthew G. Knepley PetscFunctionBegin; 4006363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellType(dm, c, &ct)); 4016363a54bSMatthew G. Knepley switch (ct) { 4026363a54bSMatthew G. Knepley case DM_POLYTOPE_SEGMENT: 4036363a54bSMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 4046363a54bSMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 4056363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Internal(dm, DMPolytopeTypeGetDim(ct), c, p, normal, pos, Nint, intPoints)); 4066363a54bSMatthew G. Knepley break; 4076363a54bSMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 4086363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneQuadIntersection_Internal(dm, DMPolytopeTypeGetDim(ct), c, p, normal, pos, Nint, intPoints)); 4096363a54bSMatthew G. Knepley break; 4106363a54bSMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 4116363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneHexIntersection_Internal(dm, DMPolytopeTypeGetDim(ct), c, p, normal, pos, Nint, intPoints)); 4126363a54bSMatthew G. Knepley break; 4136363a54bSMatthew G. Knepley default: 4146363a54bSMatthew G. Knepley SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No plane intersection for cell %" PetscInt_FMT " with type %s", c, DMPolytopeTypes[ct]); 4156363a54bSMatthew G. Knepley } 4166363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 4176363a54bSMatthew G. Knepley } 418ddce0771SMatthew G. Knepley 419d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 420d71ae5a4SJacob Faibussowitsch { 42114bbb9f0SLawrence Mitchell const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 42214bbb9f0SLawrence Mitchell const PetscReal x = PetscRealPart(point[0]); 42314bbb9f0SLawrence Mitchell PetscReal v0, J, invJ, detJ; 42414bbb9f0SLawrence Mitchell PetscReal xi; 42514bbb9f0SLawrence Mitchell 42614bbb9f0SLawrence Mitchell PetscFunctionBegin; 4279566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ)); 42814bbb9f0SLawrence Mitchell xi = invJ * (x - v0); 42914bbb9f0SLawrence Mitchell 43014bbb9f0SLawrence Mitchell if ((xi >= -eps) && (xi <= 2. + eps)) *cell = c; 43114bbb9f0SLawrence Mitchell else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 4323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43314bbb9f0SLawrence Mitchell } 43414bbb9f0SLawrence Mitchell 435d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 436d71ae5a4SJacob Faibussowitsch { 437ccd2543fSMatthew G Knepley const PetscInt embedDim = 2; 438f5ebc837SMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 439ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 440ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 441ccd2543fSMatthew G Knepley PetscReal v0[2], J[4], invJ[4], detJ; 442ccd2543fSMatthew G Knepley PetscReal xi, eta; 443ccd2543fSMatthew G Knepley 444ccd2543fSMatthew G Knepley PetscFunctionBegin; 4459566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 446ccd2543fSMatthew G Knepley xi = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]); 447ccd2543fSMatthew G Knepley eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]); 448ccd2543fSMatthew G Knepley 449f5ebc837SMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0 + eps)) *cell = c; 450c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 4513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 452ccd2543fSMatthew G Knepley } 453ccd2543fSMatthew G Knepley 454d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[]) 455d71ae5a4SJacob Faibussowitsch { 45662a38674SMatthew G. Knepley const PetscInt embedDim = 2; 45762a38674SMatthew G. Knepley PetscReal x = PetscRealPart(point[0]); 45862a38674SMatthew G. Knepley PetscReal y = PetscRealPart(point[1]); 45962a38674SMatthew G. Knepley PetscReal v0[2], J[4], invJ[4], detJ; 46062a38674SMatthew G. Knepley PetscReal xi, eta, r; 46162a38674SMatthew G. Knepley 46262a38674SMatthew G. Knepley PetscFunctionBegin; 4639566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 46462a38674SMatthew G. Knepley xi = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]); 46562a38674SMatthew G. Knepley eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]); 46662a38674SMatthew G. Knepley 46762a38674SMatthew G. Knepley xi = PetscMax(xi, 0.0); 46862a38674SMatthew G. Knepley eta = PetscMax(eta, 0.0); 46962a38674SMatthew G. Knepley if (xi + eta > 2.0) { 47062a38674SMatthew G. Knepley r = (xi + eta) / 2.0; 47162a38674SMatthew G. Knepley xi /= r; 47262a38674SMatthew G. Knepley eta /= r; 47362a38674SMatthew G. Knepley } 47462a38674SMatthew G. Knepley cpoint[0] = J[0 * embedDim + 0] * xi + J[0 * embedDim + 1] * eta + v0[0]; 47562a38674SMatthew G. Knepley cpoint[1] = J[1 * embedDim + 0] * xi + J[1 * embedDim + 1] * eta + v0[1]; 4763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47762a38674SMatthew G. Knepley } 47862a38674SMatthew G. Knepley 479d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 480d71ae5a4SJacob Faibussowitsch { 48176b3799dSMatthew G. Knepley const PetscScalar *array; 482a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 483ccd2543fSMatthew G Knepley const PetscInt faces[8] = {0, 1, 1, 2, 2, 3, 3, 0}; 484ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 485ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 48676b3799dSMatthew G. Knepley PetscInt crossings = 0, numCoords, f; 48776b3799dSMatthew G. Knepley PetscBool isDG; 488ccd2543fSMatthew G Knepley 489ccd2543fSMatthew G Knepley PetscFunctionBegin; 49076b3799dSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 49176b3799dSMatthew G. Knepley PetscCheck(numCoords == 8, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Quadrilateral should have 8 coordinates, not %" PetscInt_FMT, numCoords); 492ccd2543fSMatthew G Knepley for (f = 0; f < 4; ++f) { 493ccd2543fSMatthew G Knepley PetscReal x_i = PetscRealPart(coords[faces[2 * f + 0] * 2 + 0]); 494ccd2543fSMatthew G Knepley PetscReal y_i = PetscRealPart(coords[faces[2 * f + 0] * 2 + 1]); 495ccd2543fSMatthew G Knepley PetscReal x_j = PetscRealPart(coords[faces[2 * f + 1] * 2 + 0]); 496ccd2543fSMatthew G Knepley PetscReal y_j = PetscRealPart(coords[faces[2 * f + 1] * 2 + 1]); 497ccd2543fSMatthew G Knepley PetscReal slope = (y_j - y_i) / (x_j - x_i); 498ccd2543fSMatthew G Knepley PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE; 499ccd2543fSMatthew G Knepley PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE; 500ccd2543fSMatthew G Knepley PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE; 501ccd2543fSMatthew G Knepley if ((cond1 || cond2) && above) ++crossings; 502ccd2543fSMatthew G Knepley } 503ccd2543fSMatthew G Knepley if (crossings % 2) *cell = c; 504c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 50576b3799dSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 5063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 507ccd2543fSMatthew G Knepley } 508ccd2543fSMatthew G Knepley 509d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 510d71ae5a4SJacob Faibussowitsch { 511ccd2543fSMatthew G Knepley const PetscInt embedDim = 3; 51237900f7dSMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 513ccd2543fSMatthew G Knepley PetscReal v0[3], J[9], invJ[9], detJ; 514ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 515ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 516ccd2543fSMatthew G Knepley PetscReal z = PetscRealPart(point[2]); 517ccd2543fSMatthew G Knepley PetscReal xi, eta, zeta; 518ccd2543fSMatthew G Knepley 519ccd2543fSMatthew G Knepley PetscFunctionBegin; 5209566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 521ccd2543fSMatthew G Knepley xi = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]) + invJ[0 * embedDim + 2] * (z - v0[2]); 522ccd2543fSMatthew G Knepley eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]) + invJ[1 * embedDim + 2] * (z - v0[2]); 523ccd2543fSMatthew G Knepley zeta = invJ[2 * embedDim + 0] * (x - v0[0]) + invJ[2 * embedDim + 1] * (y - v0[1]) + invJ[2 * embedDim + 2] * (z - v0[2]); 524ccd2543fSMatthew G Knepley 52537900f7dSMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0 + eps)) *cell = c; 526c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 5273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 528ccd2543fSMatthew G Knepley } 529ccd2543fSMatthew G Knepley 530d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 531d71ae5a4SJacob Faibussowitsch { 53276b3799dSMatthew G. Knepley const PetscScalar *array; 533872a9804SMatthew G. Knepley PetscScalar *coords = NULL; 5349371c9d4SSatish 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}; 535ccd2543fSMatthew G Knepley PetscBool found = PETSC_TRUE; 53676b3799dSMatthew G. Knepley PetscInt numCoords, f; 53776b3799dSMatthew G. Knepley PetscBool isDG; 538ccd2543fSMatthew G Knepley 539ccd2543fSMatthew G Knepley PetscFunctionBegin; 54076b3799dSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 54176b3799dSMatthew G. Knepley PetscCheck(numCoords == 24, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Quadrilateral should have 8 coordinates, not %" PetscInt_FMT, numCoords); 542ccd2543fSMatthew G Knepley for (f = 0; f < 6; ++f) { 543ccd2543fSMatthew G Knepley /* Check the point is under plane */ 544ccd2543fSMatthew G Knepley /* Get face normal */ 545ccd2543fSMatthew G Knepley PetscReal v_i[3]; 546ccd2543fSMatthew G Knepley PetscReal v_j[3]; 547ccd2543fSMatthew G Knepley PetscReal normal[3]; 548ccd2543fSMatthew G Knepley PetscReal pp[3]; 549ccd2543fSMatthew G Knepley PetscReal dot; 550ccd2543fSMatthew G Knepley 551ccd2543fSMatthew G Knepley v_i[0] = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]); 552ccd2543fSMatthew G Knepley v_i[1] = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]); 553ccd2543fSMatthew G Knepley v_i[2] = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]); 554ccd2543fSMatthew G Knepley v_j[0] = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]); 555ccd2543fSMatthew G Knepley v_j[1] = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]); 556ccd2543fSMatthew G Knepley v_j[2] = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]); 557ccd2543fSMatthew G Knepley normal[0] = v_i[1] * v_j[2] - v_i[2] * v_j[1]; 558ccd2543fSMatthew G Knepley normal[1] = v_i[2] * v_j[0] - v_i[0] * v_j[2]; 559ccd2543fSMatthew G Knepley normal[2] = v_i[0] * v_j[1] - v_i[1] * v_j[0]; 560ccd2543fSMatthew G Knepley pp[0] = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 0] - point[0]); 561ccd2543fSMatthew G Knepley pp[1] = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 1] - point[1]); 562ccd2543fSMatthew G Knepley pp[2] = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 2] - point[2]); 563ccd2543fSMatthew G Knepley dot = normal[0] * pp[0] + normal[1] * pp[1] + normal[2] * pp[2]; 564ccd2543fSMatthew G Knepley 565ccd2543fSMatthew G Knepley /* Check that projected point is in face (2D location problem) */ 566ccd2543fSMatthew G Knepley if (dot < 0.0) { 567ccd2543fSMatthew G Knepley found = PETSC_FALSE; 568ccd2543fSMatthew G Knepley break; 569ccd2543fSMatthew G Knepley } 570ccd2543fSMatthew G Knepley } 571ccd2543fSMatthew G Knepley if (found) *cell = c; 572c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 57376b3799dSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 5743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 575ccd2543fSMatthew G Knepley } 576ccd2543fSMatthew G Knepley 577d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[]) 578d71ae5a4SJacob Faibussowitsch { 579c4eade1cSMatthew G. Knepley PetscInt d; 580c4eade1cSMatthew G. Knepley 581c4eade1cSMatthew G. Knepley PetscFunctionBegin; 582c4eade1cSMatthew G. Knepley box->dim = dim; 583378076f8SMatthew G. Knepley for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = point ? PetscRealPart(point[d]) : 0.; 5843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 585c4eade1cSMatthew G. Knepley } 586c4eade1cSMatthew G. Knepley 587d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box) 588d71ae5a4SJacob Faibussowitsch { 589c4eade1cSMatthew G. Knepley PetscFunctionBegin; 5909566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(1, box)); 5919566063dSJacob Faibussowitsch PetscCall(PetscGridHashInitialize_Internal(*box, dim, point)); 5923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 593c4eade1cSMatthew G. Knepley } 594c4eade1cSMatthew G. Knepley 595d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[]) 596d71ae5a4SJacob Faibussowitsch { 597c4eade1cSMatthew G. Knepley PetscInt d; 598c4eade1cSMatthew G. Knepley 599c4eade1cSMatthew G. Knepley PetscFunctionBegin; 600c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 601c4eade1cSMatthew G. Knepley box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d])); 602c4eade1cSMatthew G. Knepley box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d])); 603c4eade1cSMatthew G. Knepley } 6043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 605c4eade1cSMatthew G. Knepley } 606c4eade1cSMatthew G. Knepley 6076363a54bSMatthew G. Knepley static PetscErrorCode DMPlexCreateGridHash(DM dm, PetscGridHash *box) 6086363a54bSMatthew G. Knepley { 6096363a54bSMatthew G. Knepley Vec coordinates; 6106363a54bSMatthew G. Knepley const PetscScalar *coords; 6116363a54bSMatthew G. Knepley PetscInt cdim, N, bs; 6126363a54bSMatthew G. Knepley 6136363a54bSMatthew G. Knepley PetscFunctionBegin; 6146363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 6156363a54bSMatthew G. Knepley PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 6166363a54bSMatthew G. Knepley PetscCall(VecGetArrayRead(coordinates, &coords)); 6176363a54bSMatthew G. Knepley PetscCall(VecGetLocalSize(coordinates, &N)); 6186363a54bSMatthew G. Knepley PetscCall(VecGetBlockSize(coordinates, &bs)); 6196363a54bSMatthew G. Knepley PetscCheck(bs == cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Coordinate block size %" PetscInt_FMT " != %" PetscInt_FMT " coordinate dimension", bs, cdim); 6206363a54bSMatthew G. Knepley 6216363a54bSMatthew G. Knepley PetscCall(PetscMalloc1(1, box)); 6226363a54bSMatthew G. Knepley PetscCall(PetscGridHashInitialize_Internal(*box, cdim, coords)); 6236363a54bSMatthew G. Knepley for (PetscInt i = 0; i < N; i += cdim) PetscCall(PetscGridHashEnlarge(*box, &coords[i])); 6246363a54bSMatthew G. Knepley 6256363a54bSMatthew G. Knepley PetscCall(VecRestoreArrayRead(coordinates, &coords)); 6266363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 6276363a54bSMatthew G. Knepley } 6286363a54bSMatthew G. Knepley 62962a38674SMatthew G. Knepley /* 63062a38674SMatthew G. Knepley PetscGridHashSetGrid - Divide the grid into boxes 63162a38674SMatthew G. Knepley 63220f4b53cSBarry Smith Not Collective 63362a38674SMatthew G. Knepley 63462a38674SMatthew G. Knepley Input Parameters: 63562a38674SMatthew G. Knepley + box - The grid hash object 63620f4b53cSBarry Smith . n - The number of boxes in each dimension, or `PETSC_DETERMINE` 63720f4b53cSBarry Smith - h - The box size in each dimension, only used if n[d] == `PETSC_DETERMINE` 63862a38674SMatthew G. Knepley 63962a38674SMatthew G. Knepley Level: developer 64062a38674SMatthew G. Knepley 6412fe279fdSBarry Smith .seealso: `DMPLEX`, `PetscGridHashCreate()` 64262a38674SMatthew G. Knepley */ 643d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[]) 644d71ae5a4SJacob Faibussowitsch { 645c4eade1cSMatthew G. Knepley PetscInt d; 646c4eade1cSMatthew G. Knepley 647c4eade1cSMatthew G. Knepley PetscFunctionBegin; 648c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 649c4eade1cSMatthew G. Knepley box->extent[d] = box->upper[d] - box->lower[d]; 650c4eade1cSMatthew G. Knepley if (n[d] == PETSC_DETERMINE) { 651c4eade1cSMatthew G. Knepley box->h[d] = h[d]; 652c4eade1cSMatthew G. Knepley box->n[d] = PetscCeilReal(box->extent[d] / h[d]); 653c4eade1cSMatthew G. Knepley } else { 654c4eade1cSMatthew G. Knepley box->n[d] = n[d]; 655c4eade1cSMatthew G. Knepley box->h[d] = box->extent[d] / n[d]; 656c4eade1cSMatthew G. Knepley } 657c4eade1cSMatthew G. Knepley } 6583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 659c4eade1cSMatthew G. Knepley } 660c4eade1cSMatthew G. Knepley 66162a38674SMatthew G. Knepley /* 66262a38674SMatthew G. Knepley PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point 66362a38674SMatthew G. Knepley 66420f4b53cSBarry Smith Not Collective 66562a38674SMatthew G. Knepley 66662a38674SMatthew G. Knepley Input Parameters: 66762a38674SMatthew G. Knepley + box - The grid hash object 66862a38674SMatthew G. Knepley . numPoints - The number of input points 66962a38674SMatthew G. Knepley - points - The input point coordinates 67062a38674SMatthew G. Knepley 67162a38674SMatthew G. Knepley Output Parameters: 67262a38674SMatthew G. Knepley + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 67362a38674SMatthew G. Knepley - boxes - An array of numPoints integers expressing the enclosing box as single number, or NULL 67462a38674SMatthew G. Knepley 67562a38674SMatthew G. Knepley Level: developer 67662a38674SMatthew G. Knepley 677f5867de0SMatthew G. Knepley Note: 678f5867de0SMatthew G. Knepley This only guarantees that a box contains a point, not that a cell does. 679f5867de0SMatthew G. Knepley 6802fe279fdSBarry Smith .seealso: `DMPLEX`, `PetscGridHashCreate()` 68162a38674SMatthew G. Knepley */ 682d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[]) 683d71ae5a4SJacob Faibussowitsch { 684c4eade1cSMatthew G. Knepley const PetscReal *lower = box->lower; 685c4eade1cSMatthew G. Knepley const PetscReal *upper = box->upper; 686c4eade1cSMatthew G. Knepley const PetscReal *h = box->h; 687c4eade1cSMatthew G. Knepley const PetscInt *n = box->n; 688c4eade1cSMatthew G. Knepley const PetscInt dim = box->dim; 689c4eade1cSMatthew G. Knepley PetscInt d, p; 690c4eade1cSMatthew G. Knepley 691c4eade1cSMatthew G. Knepley PetscFunctionBegin; 692c4eade1cSMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 693c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) { 6941c6dfc3eSMatthew G. Knepley PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]); 695c4eade1cSMatthew G. Knepley 6961c6dfc3eSMatthew G. Knepley if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1; 6972a705cacSMatthew G. Knepley if (dbox == -1 && PetscAbsReal(PetscRealPart(points[p * dim + d]) - lower[d]) < 1.0e-9) dbox = 0; 6989371c9d4SSatish 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); 699c4eade1cSMatthew G. Knepley dboxes[p * dim + d] = dbox; 700c4eade1cSMatthew G. Knepley } 7019371c9d4SSatish Balay if (boxes) 7029371c9d4SSatish 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]; 703c4eade1cSMatthew G. Knepley } 7043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 705c4eade1cSMatthew G. Knepley } 706c4eade1cSMatthew G. Knepley 707af74b616SDave May /* 708af74b616SDave May PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point 709af74b616SDave May 71020f4b53cSBarry Smith Not Collective 711af74b616SDave May 712af74b616SDave May Input Parameters: 713af74b616SDave May + box - The grid hash object 714f5867de0SMatthew G. Knepley . cellSection - The PetscSection mapping cells to boxes 715af74b616SDave May . numPoints - The number of input points 716af74b616SDave May - points - The input point coordinates 717af74b616SDave May 718af74b616SDave May Output Parameters: 71920f4b53cSBarry Smith + dboxes - An array of `numPoints`*`dim` integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 72020f4b53cSBarry Smith . boxes - An array of `numPoints` integers expressing the enclosing box as single number, or `NULL` 721af74b616SDave May - found - Flag indicating if point was located within a box 722af74b616SDave May 723af74b616SDave May Level: developer 724af74b616SDave May 725f5867de0SMatthew G. Knepley Note: 72620f4b53cSBarry Smith 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 `cellSection` is already constructed. 727f5867de0SMatthew G. Knepley 7282fe279fdSBarry Smith .seealso: `DMPLEX`, `PetscGridHashGetEnclosingBox()` 729af74b616SDave May */ 730d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscSection cellSection, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[], PetscBool *found) 731d71ae5a4SJacob Faibussowitsch { 732af74b616SDave May const PetscReal *lower = box->lower; 733af74b616SDave May const PetscReal *upper = box->upper; 734af74b616SDave May const PetscReal *h = box->h; 735af74b616SDave May const PetscInt *n = box->n; 736af74b616SDave May const PetscInt dim = box->dim; 737f5867de0SMatthew G. Knepley PetscInt bStart, bEnd, d, p; 738af74b616SDave May 739af74b616SDave May PetscFunctionBegin; 740f5867de0SMatthew G. Knepley PetscValidHeaderSpecific(cellSection, PETSC_SECTION_CLASSID, 2); 741af74b616SDave May *found = PETSC_FALSE; 742f5867de0SMatthew G. Knepley PetscCall(PetscSectionGetChart(box->cellSection, &bStart, &bEnd)); 743af74b616SDave May for (p = 0; p < numPoints; ++p) { 744af74b616SDave May for (d = 0; d < dim; ++d) { 745af74b616SDave May PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]); 746af74b616SDave May 747af74b616SDave May if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1; 7483ba16761SJacob Faibussowitsch if (dbox < 0 || dbox >= n[d]) PetscFunctionReturn(PETSC_SUCCESS); 749af74b616SDave May dboxes[p * dim + d] = dbox; 750af74b616SDave May } 7519371c9d4SSatish Balay if (boxes) 7529371c9d4SSatish 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]; 753f5867de0SMatthew G. Knepley // It is possible for a box to overlap no grid cells 7543ba16761SJacob Faibussowitsch if (boxes[p] < bStart || boxes[p] >= bEnd) PetscFunctionReturn(PETSC_SUCCESS); 755af74b616SDave May } 756af74b616SDave May *found = PETSC_TRUE; 7573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 758af74b616SDave May } 759af74b616SDave May 760d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashDestroy(PetscGridHash *box) 761d71ae5a4SJacob Faibussowitsch { 762c4eade1cSMatthew G. Knepley PetscFunctionBegin; 763c4eade1cSMatthew G. Knepley if (*box) { 7649566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&(*box)->cellSection)); 7659566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*box)->cells)); 7669566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&(*box)->cellsSparse)); 767c4eade1cSMatthew G. Knepley } 7689566063dSJacob Faibussowitsch PetscCall(PetscFree(*box)); 7693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 770c4eade1cSMatthew G. Knepley } 771c4eade1cSMatthew G. Knepley 772d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell) 773d71ae5a4SJacob Faibussowitsch { 774ba2698f1SMatthew G. Knepley DMPolytopeType ct; 775cafe43deSMatthew G. Knepley 776cafe43deSMatthew G. Knepley PetscFunctionBegin; 7779566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cellStart, &ct)); 778ba2698f1SMatthew G. Knepley switch (ct) { 779d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_SEGMENT: 780d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell)); 781d71ae5a4SJacob Faibussowitsch break; 782d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_TRIANGLE: 783d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell)); 784d71ae5a4SJacob Faibussowitsch break; 785d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_QUADRILATERAL: 786d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell)); 787d71ae5a4SJacob Faibussowitsch break; 788d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_TETRAHEDRON: 789d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell)); 790d71ae5a4SJacob Faibussowitsch break; 791d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_HEXAHEDRON: 792d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell)); 793d71ae5a4SJacob Faibussowitsch break; 794d71ae5a4SJacob Faibussowitsch default: 795d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %" PetscInt_FMT " with type %s", cellStart, DMPolytopeTypes[ct]); 796cafe43deSMatthew G. Knepley } 7973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 798cafe43deSMatthew G. Knepley } 799cafe43deSMatthew G. Knepley 80062a38674SMatthew G. Knepley /* 80162a38674SMatthew G. Knepley DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point 80262a38674SMatthew G. Knepley */ 803d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[]) 804d71ae5a4SJacob Faibussowitsch { 805ba2698f1SMatthew G. Knepley DMPolytopeType ct; 80662a38674SMatthew G. Knepley 80762a38674SMatthew G. Knepley PetscFunctionBegin; 8089566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 809ba2698f1SMatthew G. Knepley switch (ct) { 810d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_TRIANGLE: 811d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint)); 812d71ae5a4SJacob Faibussowitsch break; 81362a38674SMatthew G. Knepley #if 0 814ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 8159566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint));break; 816ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 8179566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint));break; 818ba2698f1SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 8199566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint));break; 82062a38674SMatthew G. Knepley #endif 821d71ae5a4SJacob Faibussowitsch default: 822d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %" PetscInt_FMT " with type %s", cell, DMPolytopeTypes[ct]); 82362a38674SMatthew G. Knepley } 8243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 82562a38674SMatthew G. Knepley } 82662a38674SMatthew G. Knepley 82762a38674SMatthew G. Knepley /* 82820f4b53cSBarry Smith DMPlexComputeGridHash_Internal - Create a grid hash structure covering the `DMPLEX` 82962a38674SMatthew G. Knepley 83020f4b53cSBarry Smith Collective 83162a38674SMatthew G. Knepley 83262a38674SMatthew G. Knepley Input Parameter: 83320f4b53cSBarry Smith . dm - The `DMPLEX` 83462a38674SMatthew G. Knepley 83562a38674SMatthew G. Knepley Output Parameter: 83662a38674SMatthew G. Knepley . localBox - The grid hash object 83762a38674SMatthew G. Knepley 83862a38674SMatthew G. Knepley Level: developer 83962a38674SMatthew G. Knepley 8406363a54bSMatthew G. Knepley Notes: 8416363a54bSMatthew G. Knepley How do we determine all boxes intersecting a given cell? 8426363a54bSMatthew G. Knepley 8436363a54bSMatthew G. Knepley 1) Get convex body enclosing cell. We will use a box called the box-hull. 8446363a54bSMatthew G. Knepley 8456363a54bSMatthew G. Knepley 2) Get smallest brick of boxes enclosing the box-hull 8466363a54bSMatthew G. Knepley 8476363a54bSMatthew G. Knepley 3) Each box is composed of 6 planes, 3 lower and 3 upper. We loop over dimensions, and 8486363a54bSMatthew G. Knepley for each new plane determine whether the cell is on the negative side, positive side, or intersects it. 8496363a54bSMatthew G. Knepley 8506363a54bSMatthew G. Knepley a) If the cell is on the negative side of the lower planes, it is not in the box 8516363a54bSMatthew G. Knepley 8526363a54bSMatthew G. Knepley b) If the cell is on the positive side of the upper planes, it is not in the box 8536363a54bSMatthew G. Knepley 8546363a54bSMatthew G. Knepley c) If there is no intersection, it is in the box 8556363a54bSMatthew G. Knepley 8566363a54bSMatthew G. Knepley d) If any intersection point is within the box limits, it is in the box 8576363a54bSMatthew G. Knepley 85820f4b53cSBarry Smith .seealso: `DMPLEX`, `PetscGridHashCreate()`, `PetscGridHashGetEnclosingBox()` 85962a38674SMatthew G. Knepley */ 860d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox) 861d71ae5a4SJacob Faibussowitsch { 862f5867de0SMatthew G. Knepley PetscInt debug = ((DM_Plex *)dm->data)->printLocate; 863cafe43deSMatthew G. Knepley PetscGridHash lbox; 86496217254SMatthew G. Knepley PetscSF sf; 86596217254SMatthew G. Knepley const PetscInt *leaves; 8666363a54bSMatthew G. Knepley PetscInt *dboxes, *boxes; 8676363a54bSMatthew G. Knepley PetscInt cdim, cStart, cEnd, Nl = -1; 868ddce0771SMatthew G. Knepley PetscBool flg; 869cafe43deSMatthew G. Knepley 870cafe43deSMatthew G. Knepley PetscFunctionBegin; 8716363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 8729566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 8736363a54bSMatthew G. Knepley PetscCall(DMPlexCreateGridHash(dm, &lbox)); 8746363a54bSMatthew G. Knepley { 8756363a54bSMatthew G. Knepley PetscInt n[3], d; 8766363a54bSMatthew G. Knepley 8776363a54bSMatthew G. Knepley PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, "-dm_plex_hash_box_faces", n, &d, &flg)); 8789371c9d4SSatish Balay if (flg) { 8796363a54bSMatthew G. Knepley for (PetscInt i = d; i < cdim; ++i) n[i] = n[d - 1]; 8809371c9d4SSatish Balay } else { 8816363a54bSMatthew G. Knepley for (PetscInt i = 0; i < cdim; ++i) n[i] = PetscMax(2, PetscFloorReal(PetscPowReal((PetscReal)(cEnd - cStart), 1.0 / cdim) * 0.8)); 8829371c9d4SSatish Balay } 8839566063dSJacob Faibussowitsch PetscCall(PetscGridHashSetGrid(lbox, n, NULL)); 8849371c9d4SSatish Balay if (debug) 8856363a54bSMatthew G. Knepley 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], cdim > 2 ? (double)lbox->lower[2] : 0., 8866363a54bSMatthew G. Knepley (double)lbox->upper[0], (double)lbox->upper[1], cdim > 2 ? (double)lbox->upper[2] : 0, n[0], n[1], cdim > 2 ? n[2] : 0, (double)lbox->h[0], (double)lbox->h[1], cdim > 2 ? (double)lbox->h[2] : 0.)); 8876363a54bSMatthew G. Knepley } 8886363a54bSMatthew G. Knepley 88996217254SMatthew G. Knepley PetscCall(DMGetPointSF(dm, &sf)); 89096217254SMatthew G. Knepley if (sf) PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL)); 89196217254SMatthew G. Knepley Nl = PetscMax(Nl, 0); 8926363a54bSMatthew G. Knepley PetscCall(PetscCalloc2(16 * cdim, &dboxes, 16, &boxes)); 8936363a54bSMatthew G. Knepley 8946363a54bSMatthew G. Knepley PetscCall(DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse)); 8956363a54bSMatthew G. Knepley PetscCall(DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd)); 8966363a54bSMatthew G. Knepley for (PetscInt c = cStart; c < cEnd; ++c) { 8976363a54bSMatthew G. Knepley PetscReal intPoints[6 * 6 * 6 * 3]; 8986363a54bSMatthew G. Knepley const PetscScalar *array; 8996363a54bSMatthew G. Knepley PetscScalar *coords = NULL; 900cafe43deSMatthew G. Knepley const PetscReal *h = lbox->h; 9016363a54bSMatthew G. Knepley PetscReal normal[9] = {1., 0., 0., 0., 1., 0., 0., 0., 1.}; 9026363a54bSMatthew G. Knepley PetscReal *lowerIntPoints[3] = {&intPoints[0 * 6 * 6 * 3], &intPoints[1 * 6 * 6 * 3], &intPoints[2 * 6 * 6 * 3]}; 9036363a54bSMatthew G. Knepley PetscReal *upperIntPoints[3] = {&intPoints[3 * 6 * 6 * 3], &intPoints[4 * 6 * 6 * 3], &intPoints[5 * 6 * 6 * 3]}; 9046363a54bSMatthew G. Knepley PetscReal lp[3], up[3], *tmp; 9056363a54bSMatthew G. Knepley PetscInt numCoords, idx, dlim[6], lowerInt[3], upperInt[3]; 9066363a54bSMatthew G. Knepley PetscBool isDG, lower[3], upper[3]; 907cafe43deSMatthew G. Knepley 90896217254SMatthew G. Knepley PetscCall(PetscFindInt(c, Nl, leaves, &idx)); 90996217254SMatthew G. Knepley if (idx >= 0) continue; 9106363a54bSMatthew G. Knepley // Get grid of boxes containing the cell 9116363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 9126363a54bSMatthew G. Knepley PetscCall(PetscGridHashGetEnclosingBox(lbox, numCoords / cdim, coords, dboxes, boxes)); 9136363a54bSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 9146363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) dlim[d * 2 + 0] = dlim[d * 2 + 1] = dboxes[d]; 9156363a54bSMatthew G. Knepley for (PetscInt d = cdim; d < 3; ++d) dlim[d * 2 + 0] = dlim[d * 2 + 1] = 0; 9166363a54bSMatthew G. Knepley for (PetscInt e = 1; e < numCoords / cdim; ++e) { 9176363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) { 9186363a54bSMatthew G. Knepley dlim[d * 2 + 0] = PetscMin(dlim[d * 2 + 0], dboxes[e * cdim + d]); 9196363a54bSMatthew G. Knepley dlim[d * 2 + 1] = PetscMax(dlim[d * 2 + 1], dboxes[e * cdim + d]); 920ddce0771SMatthew G. Knepley } 921ddce0771SMatthew G. Knepley } 9226363a54bSMatthew G. Knepley if (debug > 4) { 9236363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " direction %" PetscInt_FMT " box limits %" PetscInt_FMT "--%" PetscInt_FMT "\n", c, d, dlim[d * 2 + 0], dlim[d * 2 + 1])); 924ddce0771SMatthew G. Knepley } 9256363a54bSMatthew G. Knepley // Initialize with lower planes for first box 9266363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) { 9276363a54bSMatthew G. Knepley lp[d] = lbox->lower[d] + dlim[d * 2 + 0] * h[d]; 9286363a54bSMatthew G. Knepley up[d] = lp[d] + h[d]; 9296363a54bSMatthew G. Knepley } 9306363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) { 9316363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneCellIntersection_Internal(dm, c, lp, &normal[d * 3], &lower[d], &lowerInt[d], lowerIntPoints[d])); 9326363a54bSMatthew G. Knepley if (debug > 4) { 9336363a54bSMatthew G. Knepley if (!lowerInt[d]) 9346363a54bSMatthew G. Knepley PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " lower direction %" PetscInt_FMT " (%g, %g, %g) does not intersect %s\n", c, d, (double)lp[0], (double)lp[1], cdim > 2 ? (double)lp[2] : 0., lower[d] ? "positive" : "negative")); 9356363a54bSMatthew G. Knepley else PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " lower direction %" PetscInt_FMT " (%g, %g, %g) intersects %" PetscInt_FMT " times\n", c, d, (double)lp[0], (double)lp[1], cdim > 2 ? (double)lp[2] : 0., lowerInt[d])); 936cafe43deSMatthew G. Knepley } 937cafe43deSMatthew G. Knepley } 9386363a54bSMatthew G. Knepley // Loop over grid 9396363a54bSMatthew G. Knepley for (PetscInt k = dlim[2 * 2 + 0]; k <= dlim[2 * 2 + 1]; ++k, lp[2] = up[2], up[2] += h[2]) { 9406363a54bSMatthew G. Knepley if (cdim > 2) PetscCall(DMPlexGetPlaneCellIntersection_Internal(dm, c, up, &normal[3 * 2], &upper[2], &upperInt[2], upperIntPoints[2])); 9416363a54bSMatthew G. Knepley if (cdim > 2 && debug > 4) { 9426363a54bSMatthew G. Knepley if (!upperInt[2]) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " upper direction 2 (%g, %g, %g) does not intersect %s\n", c, (double)up[0], (double)up[1], cdim > 2 ? (double)up[2] : 0., upper[2] ? "positive" : "negative")); 9436363a54bSMatthew G. Knepley else PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " upper direction 2 (%g, %g, %g) intersects %" PetscInt_FMT " times\n", c, (double)up[0], (double)up[1], cdim > 2 ? (double)up[2] : 0., upperInt[2])); 9446363a54bSMatthew G. Knepley } 9456363a54bSMatthew G. Knepley for (PetscInt j = dlim[1 * 2 + 0]; j <= dlim[1 * 2 + 1]; ++j, lp[1] = up[1], up[1] += h[1]) { 9466363a54bSMatthew G. Knepley if (cdim > 1) PetscCall(DMPlexGetPlaneCellIntersection_Internal(dm, c, up, &normal[3 * 1], &upper[1], &upperInt[1], upperIntPoints[1])); 9476363a54bSMatthew G. Knepley if (cdim > 1 && debug > 4) { 9486363a54bSMatthew G. Knepley if (!upperInt[1]) 9496363a54bSMatthew G. Knepley PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " upper direction 1 (%g, %g, %g) does not intersect %s\n", c, (double)up[0], (double)up[1], cdim > 2 ? (double)up[2] : 0., upper[1] ? "positive" : "negative")); 9506363a54bSMatthew G. Knepley else PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " upper direction 1 (%g, %g, %g) intersects %" PetscInt_FMT " times\n", c, (double)up[0], (double)up[1], cdim > 2 ? (double)up[2] : 0., upperInt[1])); 9516363a54bSMatthew G. Knepley } 9526363a54bSMatthew G. Knepley for (PetscInt i = dlim[0 * 2 + 0]; i <= dlim[0 * 2 + 1]; ++i, lp[0] = up[0], up[0] += h[0]) { 953cafe43deSMatthew G. Knepley const PetscInt box = (k * lbox->n[1] + j) * lbox->n[0] + i; 9546363a54bSMatthew G. Knepley PetscBool excNeg = PETSC_TRUE; 9556363a54bSMatthew G. Knepley PetscBool excPos = PETSC_TRUE; 9566363a54bSMatthew G. Knepley PetscInt NlInt = 0; 9576363a54bSMatthew G. Knepley PetscInt NuInt = 0; 958cafe43deSMatthew G. Knepley 9596363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneCellIntersection_Internal(dm, c, up, &normal[3 * 0], &upper[0], &upperInt[0], upperIntPoints[0])); 9606363a54bSMatthew G. Knepley if (debug > 4) { 9616363a54bSMatthew G. Knepley if (!upperInt[0]) 9626363a54bSMatthew G. Knepley PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " upper direction 0 (%g, %g, %g) does not intersect %s\n", c, (double)up[0], (double)up[1], cdim > 2 ? (double)up[2] : 0., upper[0] ? "positive" : "negative")); 9636363a54bSMatthew G. Knepley else PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " upper direction 0 (%g, %g, %g) intersects %" PetscInt_FMT " times\n", c, (double)up[0], (double)up[1], cdim > 2 ? (double)up[2] : 0., upperInt[0])); 9646363a54bSMatthew G. Knepley } 9656363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) { 9666363a54bSMatthew G. Knepley NlInt += lowerInt[d]; 9676363a54bSMatthew G. Knepley NuInt += upperInt[d]; 9686363a54bSMatthew G. Knepley } 9696363a54bSMatthew G. Knepley // If there is no intersection... 9706363a54bSMatthew G. Knepley if (!NlInt && !NuInt) { 9716363a54bSMatthew G. Knepley // If the cell is on the negative side of the lower planes, it is not in the box 9726363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) 9736363a54bSMatthew G. Knepley if (lower[d]) { 9746363a54bSMatthew G. Knepley excNeg = PETSC_FALSE; 9750b6bfacdSStefano Zampini break; 9760b6bfacdSStefano Zampini } 9776363a54bSMatthew G. Knepley // If the cell is on the positive side of the upper planes, it is not in the box 9786363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) 9796363a54bSMatthew G. Knepley if (!upper[d]) { 9806363a54bSMatthew G. Knepley excPos = PETSC_FALSE; 9819371c9d4SSatish Balay break; 982ddce0771SMatthew G. Knepley } 9836363a54bSMatthew G. Knepley if (excNeg || excPos) { 9846363a54bSMatthew G. Knepley if (debug && excNeg) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " is on the negative side of the lower plane\n", c)); 9856363a54bSMatthew G. Knepley if (debug && excPos) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " is on the positive side of the upper plane\n", c)); 9866363a54bSMatthew G. Knepley continue; 9876363a54bSMatthew G. Knepley } 9886363a54bSMatthew G. Knepley // Otherwise it is in the box 9896363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " is contained in box %" PetscInt_FMT "\n", c, box)); 9906363a54bSMatthew G. Knepley PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 9916363a54bSMatthew G. Knepley continue; 9926363a54bSMatthew G. Knepley } 9936363a54bSMatthew G. Knepley // If any intersection point is within the box limits, it is in the box 9946363a54bSMatthew G. Knepley // We need to have tolerances here since intersection point calculations can introduce errors 9956363a54bSMatthew G. Knepley for (PetscInt plane = 0; plane < cdim; ++plane) { 9966363a54bSMatthew G. Knepley for (PetscInt ip = 0; ip < lowerInt[plane]; ++ip) { 9976363a54bSMatthew G. Knepley PetscInt d; 9986363a54bSMatthew G. Knepley 9996363a54bSMatthew G. Knepley for (d = 0; d < cdim; ++d) { 10006363a54bSMatthew G. Knepley if ((lowerIntPoints[plane][ip * cdim + d] < lp[d] - PETSC_SMALL) || (lowerIntPoints[plane][ip * cdim + d] > up[d] + PETSC_SMALL)) break; 10016363a54bSMatthew G. Knepley } 10026363a54bSMatthew G. Knepley if (d == cdim) { 10036363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " intersected lower plane %" PetscInt_FMT " of box %" PetscInt_FMT "\n", c, plane, box)); 10046363a54bSMatthew G. Knepley PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 10056363a54bSMatthew G. Knepley goto end; 10066363a54bSMatthew G. Knepley } 10076363a54bSMatthew G. Knepley } 10086363a54bSMatthew G. Knepley for (PetscInt ip = 0; ip < upperInt[plane]; ++ip) { 10096363a54bSMatthew G. Knepley PetscInt d; 10106363a54bSMatthew G. Knepley 10116363a54bSMatthew G. Knepley for (d = 0; d < cdim; ++d) { 10126363a54bSMatthew G. Knepley if ((upperIntPoints[plane][ip * cdim + d] < lp[d] - PETSC_SMALL) || (upperIntPoints[plane][ip * cdim + d] > up[d] + PETSC_SMALL)) break; 10136363a54bSMatthew G. Knepley } 10146363a54bSMatthew G. Knepley if (d == cdim) { 10156363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " intersected upper plane %" PetscInt_FMT " of box %" PetscInt_FMT "\n", c, plane, box)); 10166363a54bSMatthew G. Knepley PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 10176363a54bSMatthew G. Knepley goto end; 1018ddce0771SMatthew G. Knepley } 1019ddce0771SMatthew G. Knepley } 1020cafe43deSMatthew G. Knepley } 10216363a54bSMatthew G. Knepley end: 10226363a54bSMatthew G. Knepley lower[0] = upper[0]; 10236363a54bSMatthew G. Knepley lowerInt[0] = upperInt[0]; 10246363a54bSMatthew G. Knepley tmp = lowerIntPoints[0]; 10256363a54bSMatthew G. Knepley lowerIntPoints[0] = upperIntPoints[0]; 10266363a54bSMatthew G. Knepley upperIntPoints[0] = tmp; 10276363a54bSMatthew G. Knepley } 10286363a54bSMatthew G. Knepley lp[0] = lbox->lower[0] + dlim[0 * 2 + 0] * h[0]; 10296363a54bSMatthew G. Knepley up[0] = lp[0] + h[0]; 10306363a54bSMatthew G. Knepley lower[1] = upper[1]; 10316363a54bSMatthew G. Knepley lowerInt[1] = upperInt[1]; 10326363a54bSMatthew G. Knepley tmp = lowerIntPoints[1]; 10336363a54bSMatthew G. Knepley lowerIntPoints[1] = upperIntPoints[1]; 10346363a54bSMatthew G. Knepley upperIntPoints[1] = tmp; 10356363a54bSMatthew G. Knepley } 10366363a54bSMatthew G. Knepley lp[1] = lbox->lower[1] + dlim[1 * 2 + 0] * h[1]; 10376363a54bSMatthew G. Knepley up[1] = lp[1] + h[1]; 10386363a54bSMatthew G. Knepley lower[2] = upper[2]; 10396363a54bSMatthew G. Knepley lowerInt[2] = upperInt[2]; 10406363a54bSMatthew G. Knepley tmp = lowerIntPoints[2]; 10416363a54bSMatthew G. Knepley lowerIntPoints[2] = upperIntPoints[2]; 10426363a54bSMatthew G. Knepley upperIntPoints[2] = tmp; 1043fea14342SMatthew G. Knepley } 1044fea14342SMatthew G. Knepley } 10456363a54bSMatthew G. Knepley PetscCall(PetscFree2(dboxes, boxes)); 10466363a54bSMatthew G. Knepley 10479566063dSJacob Faibussowitsch if (debug) PetscCall(DMLabelView(lbox->cellsSparse, PETSC_VIEWER_STDOUT_SELF)); 10489566063dSJacob Faibussowitsch PetscCall(DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells)); 10499566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lbox->cellsSparse)); 1050cafe43deSMatthew G. Knepley *localBox = lbox; 10513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1052cafe43deSMatthew G. Knepley } 1053cafe43deSMatthew G. Knepley 1054d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF) 1055d71ae5a4SJacob Faibussowitsch { 1056f5867de0SMatthew G. Knepley PetscInt debug = ((DM_Plex *)dm->data)->printLocate; 1057cafe43deSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 1058af74b616SDave May PetscBool hash = mesh->useHashLocation, reuse = PETSC_FALSE; 10593a93e3b7SToby Isaac PetscInt bs, numPoints, p, numFound, *found = NULL; 1060d8206211SMatthew G. Knepley PetscInt dim, Nl = 0, cStart, cEnd, numCells, c, d; 1061d8206211SMatthew G. Knepley PetscSF sf; 1062d8206211SMatthew G. Knepley const PetscInt *leaves; 1063cafe43deSMatthew G. Knepley const PetscInt *boxCells; 10643a93e3b7SToby Isaac PetscSFNode *cells; 1065ccd2543fSMatthew G Knepley PetscScalar *a; 10663a93e3b7SToby Isaac PetscMPIInt result; 1067af74b616SDave May PetscLogDouble t0, t1; 10689cb35068SDave May PetscReal gmin[3], gmax[3]; 10699cb35068SDave May PetscInt terminating_query_type[] = {0, 0, 0}; 10706363a54bSMatthew G. Knepley PetscMPIInt rank; 1071ccd2543fSMatthew G Knepley 1072ccd2543fSMatthew G Knepley PetscFunctionBegin; 10736363a54bSMatthew G. Knepley PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 10749566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_LocatePoints, 0, 0, 0, 0)); 10759566063dSJacob Faibussowitsch PetscCall(PetscTime(&t0)); 10761dca8a05SBarry 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."); 10779566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dim)); 10789566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(v, &bs)); 10799566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF), PETSC_COMM_SELF, &result)); 10801dca8a05SBarry Smith PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT, PetscObjectComm((PetscObject)cellSF), PETSC_ERR_SUP, "Trying parallel point location: only local point location supported"); 108163a3b9bcSJacob 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); 10826858538eSMatthew G. Knepley PetscCall(DMGetCoordinatesLocalSetUp(dm)); 10839566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 1084d8206211SMatthew G. Knepley PetscCall(DMGetPointSF(dm, &sf)); 1085d8206211SMatthew G. Knepley if (sf) PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL)); 1086d8206211SMatthew G. Knepley Nl = PetscMax(Nl, 0); 10879566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(v, &numPoints)); 10889566063dSJacob Faibussowitsch PetscCall(VecGetArray(v, &a)); 1089ccd2543fSMatthew G Knepley numPoints /= bs; 1090af74b616SDave May { 1091af74b616SDave May const PetscSFNode *sf_cells; 1092af74b616SDave May 10939566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(cellSF, NULL, NULL, NULL, &sf_cells)); 1094af74b616SDave May if (sf_cells) { 10959566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Re-using existing StarForest node list\n")); 1096af74b616SDave May cells = (PetscSFNode *)sf_cells; 1097af74b616SDave May reuse = PETSC_TRUE; 1098af74b616SDave May } else { 10999566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n")); 11009566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints, &cells)); 1101af74b616SDave May /* initialize cells if created */ 1102af74b616SDave May for (p = 0; p < numPoints; p++) { 1103af74b616SDave May cells[p].rank = 0; 1104af74b616SDave May cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 1105af74b616SDave May } 1106af74b616SDave May } 1107af74b616SDave May } 110876b3799dSMatthew G. Knepley PetscCall(DMGetBoundingBox(dm, gmin, gmax)); 1109953fc75cSMatthew G. Knepley if (hash) { 11109371c9d4SSatish Balay if (!mesh->lbox) { 111196217254SMatthew G. Knepley PetscCall(PetscInfo(dm, "Initializing grid hashing\n")); 11129371c9d4SSatish Balay PetscCall(DMPlexComputeGridHash_Internal(dm, &mesh->lbox)); 11139371c9d4SSatish Balay } 1114cafe43deSMatthew G. Knepley /* Designate the local box for each point */ 1115cafe43deSMatthew G. Knepley /* Send points to correct process */ 1116cafe43deSMatthew G. Knepley /* Search cells that lie in each subbox */ 1117cafe43deSMatthew G. Knepley /* Should we bin points before doing search? */ 11189566063dSJacob Faibussowitsch PetscCall(ISGetIndices(mesh->lbox->cells, &boxCells)); 1119953fc75cSMatthew G. Knepley } 11203a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; ++p) { 1121ccd2543fSMatthew G Knepley const PetscScalar *point = &a[p * bs]; 1122e56f9228SJed Brown PetscInt dbin[3] = {-1, -1, -1}, bin, cell = -1, cellOffset; 11239cb35068SDave May PetscBool point_outside_domain = PETSC_FALSE; 1124ccd2543fSMatthew G Knepley 11259cb35068SDave May /* check bounding box of domain */ 11269cb35068SDave May for (d = 0; d < dim; d++) { 11279371c9d4SSatish Balay if (PetscRealPart(point[d]) < gmin[d]) { 11289371c9d4SSatish Balay point_outside_domain = PETSC_TRUE; 11299371c9d4SSatish Balay break; 11309371c9d4SSatish Balay } 11319371c9d4SSatish Balay if (PetscRealPart(point[d]) > gmax[d]) { 11329371c9d4SSatish Balay point_outside_domain = PETSC_TRUE; 11339371c9d4SSatish Balay break; 11349371c9d4SSatish Balay } 11359cb35068SDave May } 11369cb35068SDave May if (point_outside_domain) { 1137e9b685f5SMatthew G. Knepley cells[p].rank = 0; 1138e9b685f5SMatthew G. Knepley cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 11399cb35068SDave May terminating_query_type[0]++; 11409cb35068SDave May continue; 11419cb35068SDave May } 1142ccd2543fSMatthew G Knepley 1143af74b616SDave May /* check initial values in cells[].index - abort early if found */ 1144af74b616SDave May if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) { 1145af74b616SDave May c = cells[p].index; 11463a93e3b7SToby Isaac cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 11479566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell)); 1148af74b616SDave May if (cell >= 0) { 1149af74b616SDave May cells[p].rank = 0; 1150af74b616SDave May cells[p].index = cell; 1151af74b616SDave May numFound++; 1152af74b616SDave May } 1153af74b616SDave May } 11549cb35068SDave May if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) { 11559cb35068SDave May terminating_query_type[1]++; 11569cb35068SDave May continue; 11579cb35068SDave May } 1158af74b616SDave May 1159953fc75cSMatthew G. Knepley if (hash) { 1160af74b616SDave May PetscBool found_box; 1161af74b616SDave May 11626363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d]Checking point %" PetscInt_FMT " (%.2g, %.2g, %.2g)\n", rank, p, (double)PetscRealPart(point[0]), (double)PetscRealPart(point[1]), dim > 2 ? (double)PetscRealPart(point[2]) : 0.)); 1163af74b616SDave May /* allow for case that point is outside box - abort early */ 1164f5867de0SMatthew G. Knepley PetscCall(PetscGridHashGetEnclosingBoxQuery(mesh->lbox, mesh->lbox->cellSection, 1, point, dbin, &bin, &found_box)); 1165af74b616SDave May if (found_box) { 11666363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Found point in box %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, bin, dbin[0], dbin[1], dim > 2 ? dbin[2] : 0)); 1167cafe43deSMatthew G. Knepley /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */ 11689566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells)); 11699566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset)); 1170cafe43deSMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 11716363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Checking for point in cell %" PetscInt_FMT "\n", rank, boxCells[c])); 11729566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell)); 11733a93e3b7SToby Isaac if (cell >= 0) { 11746363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] FOUND in cell %" PetscInt_FMT "\n", rank, cell)); 11753a93e3b7SToby Isaac cells[p].rank = 0; 11763a93e3b7SToby Isaac cells[p].index = cell; 11773a93e3b7SToby Isaac numFound++; 11789cb35068SDave May terminating_query_type[2]++; 11793a93e3b7SToby Isaac break; 1180ccd2543fSMatthew G Knepley } 11813a93e3b7SToby Isaac } 1182af74b616SDave May } 1183953fc75cSMatthew G. Knepley } else { 1184953fc75cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1185d8206211SMatthew G. Knepley PetscInt idx; 1186d8206211SMatthew G. Knepley 1187d8206211SMatthew G. Knepley PetscCall(PetscFindInt(c, Nl, leaves, &idx)); 1188d8206211SMatthew G. Knepley if (idx >= 0) continue; 11899566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell)); 11903a93e3b7SToby Isaac if (cell >= 0) { 11913a93e3b7SToby Isaac cells[p].rank = 0; 11923a93e3b7SToby Isaac cells[p].index = cell; 11933a93e3b7SToby Isaac numFound++; 11949cb35068SDave May terminating_query_type[2]++; 11953a93e3b7SToby Isaac break; 1196953fc75cSMatthew G. Knepley } 1197953fc75cSMatthew G. Knepley } 11983a93e3b7SToby Isaac } 1199ccd2543fSMatthew G Knepley } 12009566063dSJacob Faibussowitsch if (hash) PetscCall(ISRestoreIndices(mesh->lbox->cells, &boxCells)); 120162a38674SMatthew G. Knepley if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) { 120262a38674SMatthew G. Knepley for (p = 0; p < numPoints; p++) { 120362a38674SMatthew G. Knepley const PetscScalar *point = &a[p * bs]; 1204d92c4b9fSToby Isaac PetscReal cpoint[3], diff[3], best[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL}, dist, distMax = PETSC_MAX_REAL; 1205d92c4b9fSToby Isaac PetscInt dbin[3] = {-1, -1, -1}, bin, cellOffset, d, bestc = -1; 120662a38674SMatthew G. Knepley 1207e9b685f5SMatthew G. Knepley if (cells[p].index < 0) { 12089566063dSJacob Faibussowitsch PetscCall(PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin)); 12099566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells)); 12109566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset)); 121162a38674SMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 12129566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint)); 1213b716b415SMatthew G. Knepley for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]); 121462a38674SMatthew G. Knepley dist = DMPlex_NormD_Internal(dim, diff); 121562a38674SMatthew G. Knepley if (dist < distMax) { 1216d92c4b9fSToby Isaac for (d = 0; d < dim; ++d) best[d] = cpoint[d]; 1217d92c4b9fSToby Isaac bestc = boxCells[c]; 121862a38674SMatthew G. Knepley distMax = dist; 121962a38674SMatthew G. Knepley } 122062a38674SMatthew G. Knepley } 1221d92c4b9fSToby Isaac if (distMax < PETSC_MAX_REAL) { 1222d92c4b9fSToby Isaac ++numFound; 1223d92c4b9fSToby Isaac cells[p].rank = 0; 1224d92c4b9fSToby Isaac cells[p].index = bestc; 1225d92c4b9fSToby Isaac for (d = 0; d < dim; ++d) a[p * bs + d] = best[d]; 1226d92c4b9fSToby Isaac } 122762a38674SMatthew G. Knepley } 122862a38674SMatthew G. Knepley } 122962a38674SMatthew G. Knepley } 123062a38674SMatthew G. Knepley /* This code is only be relevant when interfaced to parallel point location */ 1231cafe43deSMatthew G. Knepley /* Check for highest numbered proc that claims a point (do we care?) */ 12322d1fa6caSMatthew G. Knepley if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) { 12339566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numFound, &found)); 12343a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; p++) { 12353a93e3b7SToby Isaac if (cells[p].rank >= 0 && cells[p].index >= 0) { 1236ad540459SPierre Jolivet if (numFound < p) cells[numFound] = cells[p]; 12373a93e3b7SToby Isaac found[numFound++] = p; 12383a93e3b7SToby Isaac } 12393a93e3b7SToby Isaac } 12403a93e3b7SToby Isaac } 12419566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(v, &a)); 124248a46eb9SPierre Jolivet if (!reuse) PetscCall(PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER)); 12439566063dSJacob Faibussowitsch PetscCall(PetscTime(&t1)); 12449cb35068SDave May if (hash) { 124563a3b9bcSJacob 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])); 12469cb35068SDave May } else { 124763a3b9bcSJacob 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])); 12489cb35068SDave May } 124963a3b9bcSJacob 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)))); 12509566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_LocatePoints, 0, 0, 0, 0)); 12513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1252ccd2543fSMatthew G Knepley } 1253ccd2543fSMatthew G Knepley 1254741bfc07SMatthew G. Knepley /*@C 1255741bfc07SMatthew G. Knepley DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates 1256741bfc07SMatthew G. Knepley 125720f4b53cSBarry Smith Not Collective 1258741bfc07SMatthew G. Knepley 12596b867d5aSJose E. Roman Input/Output Parameter: 12606b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x 1261741bfc07SMatthew G. Knepley 12626b867d5aSJose E. Roman Output Parameter: 12636b867d5aSJose E. Roman . R - The rotation which accomplishes the projection 1264741bfc07SMatthew G. Knepley 1265741bfc07SMatthew G. Knepley Level: developer 1266741bfc07SMatthew G. Knepley 12672fe279fdSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeProjection3Dto1D()`, `DMPlexComputeProjection3Dto2D()` 1268741bfc07SMatthew G. Knepley @*/ 1269d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[]) 1270d71ae5a4SJacob Faibussowitsch { 127117fe8556SMatthew G. Knepley const PetscReal x = PetscRealPart(coords[2] - coords[0]); 127217fe8556SMatthew G. Knepley const PetscReal y = PetscRealPart(coords[3] - coords[1]); 12738b49ba18SBarry Smith const PetscReal r = PetscSqrtReal(x * x + y * y), c = x / r, s = y / r; 127417fe8556SMatthew G. Knepley 127517fe8556SMatthew G. Knepley PetscFunctionBegin; 12769371c9d4SSatish Balay R[0] = c; 12779371c9d4SSatish Balay R[1] = -s; 12789371c9d4SSatish Balay R[2] = s; 12799371c9d4SSatish Balay R[3] = c; 128017fe8556SMatthew G. Knepley coords[0] = 0.0; 12817f07f362SMatthew G. Knepley coords[1] = r; 12823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 128317fe8556SMatthew G. Knepley } 128417fe8556SMatthew G. Knepley 1285741bfc07SMatthew G. Knepley /*@C 1286741bfc07SMatthew G. Knepley DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates 128728dbe442SToby Isaac 128820f4b53cSBarry Smith Not Collective 128928dbe442SToby Isaac 12906b867d5aSJose E. Roman Input/Output Parameter: 12916b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z 1292741bfc07SMatthew G. Knepley 12936b867d5aSJose E. Roman Output Parameter: 12946b867d5aSJose E. Roman . R - The rotation which accomplishes the projection 1295741bfc07SMatthew G. Knepley 129620f4b53cSBarry Smith Note: 129720f4b53cSBarry Smith 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 1298741bfc07SMatthew G. Knepley 1299741bfc07SMatthew G. Knepley Level: developer 1300741bfc07SMatthew G. Knepley 13012fe279fdSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto2D()` 1302741bfc07SMatthew G. Knepley @*/ 1303d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[]) 1304d71ae5a4SJacob Faibussowitsch { 130528dbe442SToby Isaac PetscReal x = PetscRealPart(coords[3] - coords[0]); 130628dbe442SToby Isaac PetscReal y = PetscRealPart(coords[4] - coords[1]); 130728dbe442SToby Isaac PetscReal z = PetscRealPart(coords[5] - coords[2]); 130828dbe442SToby Isaac PetscReal r = PetscSqrtReal(x * x + y * y + z * z); 130928dbe442SToby Isaac PetscReal rinv = 1. / r; 131028dbe442SToby Isaac PetscFunctionBegin; 131128dbe442SToby Isaac 13129371c9d4SSatish Balay x *= rinv; 13139371c9d4SSatish Balay y *= rinv; 13149371c9d4SSatish Balay z *= rinv; 131528dbe442SToby Isaac if (x > 0.) { 131628dbe442SToby Isaac PetscReal inv1pX = 1. / (1. + x); 131728dbe442SToby Isaac 13189371c9d4SSatish Balay R[0] = x; 13199371c9d4SSatish Balay R[1] = -y; 13209371c9d4SSatish Balay R[2] = -z; 13219371c9d4SSatish Balay R[3] = y; 13229371c9d4SSatish Balay R[4] = 1. - y * y * inv1pX; 13239371c9d4SSatish Balay R[5] = -y * z * inv1pX; 13249371c9d4SSatish Balay R[6] = z; 13259371c9d4SSatish Balay R[7] = -y * z * inv1pX; 13269371c9d4SSatish Balay R[8] = 1. - z * z * inv1pX; 13279371c9d4SSatish Balay } else { 132828dbe442SToby Isaac PetscReal inv1mX = 1. / (1. - x); 132928dbe442SToby Isaac 13309371c9d4SSatish Balay R[0] = x; 13319371c9d4SSatish Balay R[1] = z; 13329371c9d4SSatish Balay R[2] = y; 13339371c9d4SSatish Balay R[3] = y; 13349371c9d4SSatish Balay R[4] = -y * z * inv1mX; 13359371c9d4SSatish Balay R[5] = 1. - y * y * inv1mX; 13369371c9d4SSatish Balay R[6] = z; 13379371c9d4SSatish Balay R[7] = 1. - z * z * inv1mX; 13389371c9d4SSatish Balay R[8] = -y * z * inv1mX; 133928dbe442SToby Isaac } 134028dbe442SToby Isaac coords[0] = 0.0; 134128dbe442SToby Isaac coords[1] = r; 13423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 134328dbe442SToby Isaac } 134428dbe442SToby Isaac 1345741bfc07SMatthew G. Knepley /*@ 1346c871b86eSJed Brown DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the 1347c871b86eSJed Brown plane. The normal is defined by positive orientation of the first 3 points. 1348741bfc07SMatthew G. Knepley 134920f4b53cSBarry Smith Not Collective 1350741bfc07SMatthew G. Knepley 1351741bfc07SMatthew G. Knepley Input Parameter: 13526b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points) 1353741bfc07SMatthew G. Knepley 13546b867d5aSJose E. Roman Input/Output Parameter: 13556b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first 13566b867d5aSJose E. Roman 2*coordSize/3 entries contain interlaced 2D points, with the rest undefined 13576b867d5aSJose E. Roman 13586b867d5aSJose E. Roman Output Parameter: 13596b867d5aSJose 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. 1360741bfc07SMatthew G. Knepley 1361741bfc07SMatthew G. Knepley Level: developer 1362741bfc07SMatthew G. Knepley 13632fe279fdSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto1D()` 1364741bfc07SMatthew G. Knepley @*/ 1365d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) 1366d71ae5a4SJacob Faibussowitsch { 1367c871b86eSJed Brown PetscReal x1[3], x2[3], n[3], c[3], norm; 1368ccd2543fSMatthew G Knepley const PetscInt dim = 3; 1369c871b86eSJed Brown PetscInt d, p; 1370ccd2543fSMatthew G Knepley 1371ccd2543fSMatthew G Knepley PetscFunctionBegin; 1372ccd2543fSMatthew G Knepley /* 0) Calculate normal vector */ 1373ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 13741ee9d5ecSMatthew G. Knepley x1[d] = PetscRealPart(coords[1 * dim + d] - coords[0 * dim + d]); 13751ee9d5ecSMatthew G. Knepley x2[d] = PetscRealPart(coords[2 * dim + d] - coords[0 * dim + d]); 1376ccd2543fSMatthew G Knepley } 1377c871b86eSJed Brown // n = x1 \otimes x2 1378ccd2543fSMatthew G Knepley n[0] = x1[1] * x2[2] - x1[2] * x2[1]; 1379ccd2543fSMatthew G Knepley n[1] = x1[2] * x2[0] - x1[0] * x2[2]; 1380ccd2543fSMatthew G Knepley n[2] = x1[0] * x2[1] - x1[1] * x2[0]; 13818b49ba18SBarry Smith norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]); 1382c871b86eSJed Brown for (d = 0; d < dim; d++) n[d] /= norm; 1383c871b86eSJed Brown norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]); 1384c871b86eSJed Brown for (d = 0; d < dim; d++) x1[d] /= norm; 1385c871b86eSJed Brown // x2 = n \otimes x1 1386c871b86eSJed Brown x2[0] = n[1] * x1[2] - n[2] * x1[1]; 1387c871b86eSJed Brown x2[1] = n[2] * x1[0] - n[0] * x1[2]; 1388c871b86eSJed Brown x2[2] = n[0] * x1[1] - n[1] * x1[0]; 1389c871b86eSJed Brown for (d = 0; d < dim; d++) { 1390c871b86eSJed Brown R[d * dim + 0] = x1[d]; 1391c871b86eSJed Brown R[d * dim + 1] = x2[d]; 1392c871b86eSJed Brown R[d * dim + 2] = n[d]; 1393c871b86eSJed Brown c[d] = PetscRealPart(coords[0 * dim + d]); 139473868372SMatthew G. Knepley } 1395c871b86eSJed Brown for (p = 0; p < coordSize / dim; p++) { 1396c871b86eSJed Brown PetscReal y[3]; 1397c871b86eSJed Brown for (d = 0; d < dim; d++) y[d] = PetscRealPart(coords[p * dim + d]) - c[d]; 1398c871b86eSJed 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]; 13997f07f362SMatthew G. Knepley } 14003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1401ccd2543fSMatthew G Knepley } 1402ccd2543fSMatthew G Knepley 1403d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) 1404d71ae5a4SJacob Faibussowitsch { 1405834e62ceSMatthew G. Knepley /* Signed volume is 1/2 the determinant 1406834e62ceSMatthew G. Knepley 1407834e62ceSMatthew G. Knepley | 1 1 1 | 1408834e62ceSMatthew G. Knepley | x0 x1 x2 | 1409834e62ceSMatthew G. Knepley | y0 y1 y2 | 1410834e62ceSMatthew G. Knepley 1411834e62ceSMatthew G. Knepley but if x0,y0 is the origin, we have 1412834e62ceSMatthew G. Knepley 1413834e62ceSMatthew G. Knepley | x1 x2 | 1414834e62ceSMatthew G. Knepley | y1 y2 | 1415834e62ceSMatthew G. Knepley */ 1416834e62ceSMatthew G. Knepley const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1]; 1417834e62ceSMatthew G. Knepley const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1]; 1418834e62ceSMatthew G. Knepley PetscReal M[4], detM; 14199371c9d4SSatish Balay M[0] = x1; 14209371c9d4SSatish Balay M[1] = x2; 14219371c9d4SSatish Balay M[2] = y1; 14229371c9d4SSatish Balay M[3] = y2; 1423923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(&detM, M); 1424834e62ceSMatthew G. Knepley *vol = 0.5 * detM; 14253bc0b13bSBarry Smith (void)PetscLogFlops(5.0); 1426834e62ceSMatthew G. Knepley } 1427834e62ceSMatthew G. Knepley 1428d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) 1429d71ae5a4SJacob Faibussowitsch { 1430834e62ceSMatthew G. Knepley /* Signed volume is 1/6th of the determinant 1431834e62ceSMatthew G. Knepley 1432834e62ceSMatthew G. Knepley | 1 1 1 1 | 1433834e62ceSMatthew G. Knepley | x0 x1 x2 x3 | 1434834e62ceSMatthew G. Knepley | y0 y1 y2 y3 | 1435834e62ceSMatthew G. Knepley | z0 z1 z2 z3 | 1436834e62ceSMatthew G. Knepley 1437834e62ceSMatthew G. Knepley but if x0,y0,z0 is the origin, we have 1438834e62ceSMatthew G. Knepley 1439834e62ceSMatthew G. Knepley | x1 x2 x3 | 1440834e62ceSMatthew G. Knepley | y1 y2 y3 | 1441834e62ceSMatthew G. Knepley | z1 z2 z3 | 1442834e62ceSMatthew G. Knepley */ 1443834e62ceSMatthew G. Knepley const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2]; 1444834e62ceSMatthew G. Knepley const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2]; 1445834e62ceSMatthew G. Knepley const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2]; 14460a3da2c2SToby Isaac const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.); 1447834e62ceSMatthew G. Knepley PetscReal M[9], detM; 14489371c9d4SSatish Balay M[0] = x1; 14499371c9d4SSatish Balay M[1] = x2; 14509371c9d4SSatish Balay M[2] = x3; 14519371c9d4SSatish Balay M[3] = y1; 14529371c9d4SSatish Balay M[4] = y2; 14539371c9d4SSatish Balay M[5] = y3; 14549371c9d4SSatish Balay M[6] = z1; 14559371c9d4SSatish Balay M[7] = z2; 14569371c9d4SSatish Balay M[8] = z3; 1457923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(&detM, M); 14580a3da2c2SToby Isaac *vol = -onesixth * detM; 14593bc0b13bSBarry Smith (void)PetscLogFlops(10.0); 1460834e62ceSMatthew G. Knepley } 1461834e62ceSMatthew G. Knepley 1462d71ae5a4SJacob Faibussowitsch static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) 1463d71ae5a4SJacob Faibussowitsch { 14640a3da2c2SToby Isaac const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.); 1465923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(vol, coords); 14660a3da2c2SToby Isaac *vol *= -onesixth; 14670ec8681fSMatthew G. Knepley } 14680ec8681fSMatthew G. Knepley 1469d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1470d71ae5a4SJacob Faibussowitsch { 1471cb92db44SToby Isaac PetscSection coordSection; 1472cb92db44SToby Isaac Vec coordinates; 1473cb92db44SToby Isaac const PetscScalar *coords; 1474cb92db44SToby Isaac PetscInt dim, d, off; 1475cb92db44SToby Isaac 1476cb92db44SToby Isaac PetscFunctionBegin; 14779566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 14789566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 14799566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, e, &dim)); 14803ba16761SJacob Faibussowitsch if (!dim) PetscFunctionReturn(PETSC_SUCCESS); 14819566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, e, &off)); 14829566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coords)); 14839371c9d4SSatish Balay if (v0) { 14849371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]); 14859371c9d4SSatish Balay } 14869566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coords)); 1487cb92db44SToby Isaac *detJ = 1.; 1488cb92db44SToby Isaac if (J) { 1489cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) J[d] = 0.; 1490cb92db44SToby Isaac for (d = 0; d < dim; d++) J[d * dim + d] = 1.; 1491cb92db44SToby Isaac if (invJ) { 1492cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) invJ[d] = 0.; 1493cb92db44SToby Isaac for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.; 1494cb92db44SToby Isaac } 1495cb92db44SToby Isaac } 14963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1497cb92db44SToby Isaac } 1498cb92db44SToby Isaac 14996858538eSMatthew G. Knepley /*@C 15006858538eSMatthew G. Knepley DMPlexGetCellCoordinates - Get coordinates for a cell, taking into account periodicity 15016858538eSMatthew G. Knepley 150220f4b53cSBarry Smith Not Collective 15036858538eSMatthew G. Knepley 15046858538eSMatthew G. Knepley Input Parameters: 150520f4b53cSBarry Smith + dm - The `DMPLEX` 15066858538eSMatthew G. Knepley - cell - The cell number 15076858538eSMatthew G. Knepley 15086858538eSMatthew G. Knepley Output Parameters: 15096858538eSMatthew G. Knepley + isDG - Using cellwise coordinates 15106858538eSMatthew G. Knepley . Nc - The number of coordinates 15116858538eSMatthew G. Knepley . array - The coordinate array 15126858538eSMatthew G. Knepley - coords - The cell coordinates 15136858538eSMatthew G. Knepley 15146858538eSMatthew G. Knepley Level: developer 15156858538eSMatthew G. Knepley 151620f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexRestoreCellCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCellCoordinatesLocal()` 15176858538eSMatthew G. Knepley @*/ 1518d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[]) 1519d71ae5a4SJacob Faibussowitsch { 15206858538eSMatthew G. Knepley DM cdm; 15216858538eSMatthew G. Knepley Vec coordinates; 15226858538eSMatthew G. Knepley PetscSection cs; 15236858538eSMatthew G. Knepley const PetscScalar *ccoords; 15246858538eSMatthew G. Knepley PetscInt pStart, pEnd; 15256858538eSMatthew G. Knepley 15266858538eSMatthew G. Knepley PetscFunctionBeginHot; 15276858538eSMatthew G. Knepley *isDG = PETSC_FALSE; 15286858538eSMatthew G. Knepley *Nc = 0; 15296858538eSMatthew G. Knepley *array = NULL; 15306858538eSMatthew G. Knepley *coords = NULL; 15316858538eSMatthew G. Knepley /* Check for cellwise coordinates */ 15326858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateSection(dm, &cs)); 15336858538eSMatthew G. Knepley if (!cs) goto cg; 15346858538eSMatthew G. Knepley /* Check that the cell exists in the cellwise section */ 15356858538eSMatthew G. Knepley PetscCall(PetscSectionGetChart(cs, &pStart, &pEnd)); 15366858538eSMatthew G. Knepley if (cell < pStart || cell >= pEnd) goto cg; 15376858538eSMatthew G. Knepley /* Check for cellwise coordinates for this cell */ 15386858538eSMatthew G. Knepley PetscCall(PetscSectionGetDof(cs, cell, Nc)); 15396858538eSMatthew G. Knepley if (!*Nc) goto cg; 15406858538eSMatthew G. Knepley /* Check for cellwise coordinates */ 15416858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocalNoncollective(dm, &coordinates)); 15426858538eSMatthew G. Knepley if (!coordinates) goto cg; 15436858538eSMatthew G. Knepley /* Get cellwise coordinates */ 15446858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 15456858538eSMatthew G. Knepley PetscCall(VecGetArrayRead(coordinates, array)); 15466858538eSMatthew G. Knepley PetscCall(DMPlexPointLocalRead(cdm, cell, *array, &ccoords)); 15476858538eSMatthew G. Knepley PetscCall(DMGetWorkArray(cdm, *Nc, MPIU_SCALAR, coords)); 15486858538eSMatthew G. Knepley PetscCall(PetscArraycpy(*coords, ccoords, *Nc)); 15496858538eSMatthew G. Knepley PetscCall(VecRestoreArrayRead(coordinates, array)); 15506858538eSMatthew G. Knepley *isDG = PETSC_TRUE; 15513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15526858538eSMatthew G. Knepley cg: 15536858538eSMatthew G. Knepley /* Use continuous coordinates */ 15546858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 15556858538eSMatthew G. Knepley PetscCall(DMGetCoordinateSection(dm, &cs)); 15566858538eSMatthew G. Knepley PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates)); 15576858538eSMatthew G. Knepley PetscCall(DMPlexVecGetClosure(cdm, cs, coordinates, cell, Nc, coords)); 15583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15596858538eSMatthew G. Knepley } 15606858538eSMatthew G. Knepley 15616858538eSMatthew G. Knepley /*@C 15626858538eSMatthew G. Knepley DMPlexRestoreCellCoordinates - Get coordinates for a cell, taking into account periodicity 15636858538eSMatthew G. Knepley 156420f4b53cSBarry Smith Not Collective 15656858538eSMatthew G. Knepley 15666858538eSMatthew G. Knepley Input Parameters: 156720f4b53cSBarry Smith + dm - The `DMPLEX` 15686858538eSMatthew G. Knepley - cell - The cell number 15696858538eSMatthew G. Knepley 15706858538eSMatthew G. Knepley Output Parameters: 15716858538eSMatthew G. Knepley + isDG - Using cellwise coordinates 15726858538eSMatthew G. Knepley . Nc - The number of coordinates 15736858538eSMatthew G. Knepley . array - The coordinate array 15746858538eSMatthew G. Knepley - coords - The cell coordinates 15756858538eSMatthew G. Knepley 15766858538eSMatthew G. Knepley Level: developer 15776858538eSMatthew G. Knepley 157820f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexGetCellCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCellCoordinatesLocal()` 15796858538eSMatthew G. Knepley @*/ 1580d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexRestoreCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[]) 1581d71ae5a4SJacob Faibussowitsch { 15826858538eSMatthew G. Knepley DM cdm; 15836858538eSMatthew G. Knepley PetscSection cs; 15846858538eSMatthew G. Knepley Vec coordinates; 15856858538eSMatthew G. Knepley 15866858538eSMatthew G. Knepley PetscFunctionBeginHot; 15876858538eSMatthew G. Knepley if (*isDG) { 15886858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 15896858538eSMatthew G. Knepley PetscCall(DMRestoreWorkArray(cdm, *Nc, MPIU_SCALAR, coords)); 15906858538eSMatthew G. Knepley } else { 15916858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 15926858538eSMatthew G. Knepley PetscCall(DMGetCoordinateSection(dm, &cs)); 15936858538eSMatthew G. Knepley PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates)); 15946858538eSMatthew G. Knepley PetscCall(DMPlexVecRestoreClosure(cdm, cs, coordinates, cell, Nc, (PetscScalar **)coords)); 15956858538eSMatthew G. Knepley } 15963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15976858538eSMatthew G. Knepley } 15986858538eSMatthew G. Knepley 1599d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1600d71ae5a4SJacob Faibussowitsch { 16016858538eSMatthew G. Knepley const PetscScalar *array; 1602a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 16036858538eSMatthew G. Knepley PetscInt numCoords, d; 16046858538eSMatthew G. Knepley PetscBool isDG; 160517fe8556SMatthew G. Knepley 160617fe8556SMatthew G. Knepley PetscFunctionBegin; 16076858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 160808401ef6SPierre Jolivet PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 16097f07f362SMatthew G. Knepley *detJ = 0.0; 161028dbe442SToby Isaac if (numCoords == 6) { 161128dbe442SToby Isaac const PetscInt dim = 3; 161228dbe442SToby Isaac PetscReal R[9], J0; 161328dbe442SToby Isaac 16149371c9d4SSatish Balay if (v0) { 16159371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 16169371c9d4SSatish Balay } 16179566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto1D(coords, R)); 161828dbe442SToby Isaac if (J) { 161928dbe442SToby Isaac J0 = 0.5 * PetscRealPart(coords[1]); 16209371c9d4SSatish Balay J[0] = R[0] * J0; 16219371c9d4SSatish Balay J[1] = R[1]; 16229371c9d4SSatish Balay J[2] = R[2]; 16239371c9d4SSatish Balay J[3] = R[3] * J0; 16249371c9d4SSatish Balay J[4] = R[4]; 16259371c9d4SSatish Balay J[5] = R[5]; 16269371c9d4SSatish Balay J[6] = R[6] * J0; 16279371c9d4SSatish Balay J[7] = R[7]; 16289371c9d4SSatish Balay J[8] = R[8]; 162928dbe442SToby Isaac DMPlex_Det3D_Internal(detJ, J); 1630ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ); 1631adac9986SMatthew G. Knepley } 163228dbe442SToby Isaac } else if (numCoords == 4) { 16337f07f362SMatthew G. Knepley const PetscInt dim = 2; 16347f07f362SMatthew G. Knepley PetscReal R[4], J0; 16357f07f362SMatthew G. Knepley 16369371c9d4SSatish Balay if (v0) { 16379371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 16389371c9d4SSatish Balay } 16399566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection2Dto1D(coords, R)); 164017fe8556SMatthew G. Knepley if (J) { 16417f07f362SMatthew G. Knepley J0 = 0.5 * PetscRealPart(coords[1]); 16429371c9d4SSatish Balay J[0] = R[0] * J0; 16439371c9d4SSatish Balay J[1] = R[1]; 16449371c9d4SSatish Balay J[2] = R[2] * J0; 16459371c9d4SSatish Balay J[3] = R[3]; 1646923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1647ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ); 1648adac9986SMatthew G. Knepley } 16497f07f362SMatthew G. Knepley } else if (numCoords == 2) { 16507f07f362SMatthew G. Knepley const PetscInt dim = 1; 16517f07f362SMatthew G. Knepley 16529371c9d4SSatish Balay if (v0) { 16539371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 16549371c9d4SSatish Balay } 16557f07f362SMatthew G. Knepley if (J) { 16567f07f362SMatthew G. Knepley J[0] = 0.5 * (PetscRealPart(coords[1]) - PetscRealPart(coords[0])); 165717fe8556SMatthew G. Knepley *detJ = J[0]; 16589566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0)); 16599371c9d4SSatish Balay if (invJ) { 16609371c9d4SSatish Balay invJ[0] = 1.0 / J[0]; 16619371c9d4SSatish Balay PetscCall(PetscLogFlops(1.0)); 16629371c9d4SSatish Balay } 1663adac9986SMatthew G. Knepley } 16646858538eSMatthew 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); 16656858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 16663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 166717fe8556SMatthew G. Knepley } 166817fe8556SMatthew G. Knepley 1669d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1670d71ae5a4SJacob Faibussowitsch { 16716858538eSMatthew G. Knepley const PetscScalar *array; 1672a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 16736858538eSMatthew G. Knepley PetscInt numCoords, d; 16746858538eSMatthew G. Knepley PetscBool isDG; 1675ccd2543fSMatthew G Knepley 1676ccd2543fSMatthew G Knepley PetscFunctionBegin; 16776858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 16786858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 16797f07f362SMatthew G. Knepley *detJ = 0.0; 1680ccd2543fSMatthew G Knepley if (numCoords == 9) { 16817f07f362SMatthew G. Knepley const PetscInt dim = 3; 16827f07f362SMatthew 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}; 16837f07f362SMatthew G. Knepley 16849371c9d4SSatish Balay if (v0) { 16859371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 16869371c9d4SSatish Balay } 16879566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R)); 16887f07f362SMatthew G. Knepley if (J) { 1689b7ad821dSMatthew G. Knepley const PetscInt pdim = 2; 1690b7ad821dSMatthew G. Knepley 1691b7ad821dSMatthew G. Knepley for (d = 0; d < pdim; d++) { 1692ad540459SPierre Jolivet for (PetscInt f = 0; f < pdim; f++) J0[d * dim + f] = 0.5 * (PetscRealPart(coords[(f + 1) * pdim + d]) - PetscRealPart(coords[0 * pdim + d])); 16937f07f362SMatthew G. Knepley } 16949566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1695923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 16967f07f362SMatthew G. Knepley for (d = 0; d < dim; d++) { 16976858538eSMatthew G. Knepley for (PetscInt f = 0; f < dim; f++) { 16987f07f362SMatthew G. Knepley J[d * dim + f] = 0.0; 1699ad540459SPierre Jolivet for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f]; 17007f07f362SMatthew G. Knepley } 17017f07f362SMatthew G. Knepley } 17029566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 17037f07f362SMatthew G. Knepley } 1704ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 17057f07f362SMatthew G. Knepley } else if (numCoords == 6) { 17067f07f362SMatthew G. Knepley const PetscInt dim = 2; 17077f07f362SMatthew G. Knepley 17089371c9d4SSatish Balay if (v0) { 17099371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 17109371c9d4SSatish Balay } 1711ccd2543fSMatthew G Knepley if (J) { 1712ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1713ad540459SPierre Jolivet for (PetscInt f = 0; f < dim; f++) J[d * dim + f] = 0.5 * (PetscRealPart(coords[(f + 1) * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1714ccd2543fSMatthew G Knepley } 17159566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1716923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1717ccd2543fSMatthew G Knepley } 1718ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ); 171963a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %" PetscInt_FMT " != 6 or 9", numCoords); 17206858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 17213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1722ccd2543fSMatthew G Knepley } 1723ccd2543fSMatthew G Knepley 1724d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1725d71ae5a4SJacob Faibussowitsch { 17266858538eSMatthew G. Knepley const PetscScalar *array; 1727a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 17286858538eSMatthew G. Knepley PetscInt numCoords, d; 17296858538eSMatthew G. Knepley PetscBool isDG; 1730ccd2543fSMatthew G Knepley 1731ccd2543fSMatthew G Knepley PetscFunctionBegin; 17326858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 17336858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 1734dfccc68fSToby Isaac if (!Nq) { 1735412e9a14SMatthew G. Knepley PetscInt vorder[4] = {0, 1, 2, 3}; 1736412e9a14SMatthew G. Knepley 17379371c9d4SSatish Balay if (isTensor) { 17389371c9d4SSatish Balay vorder[2] = 3; 17399371c9d4SSatish Balay vorder[3] = 2; 17409371c9d4SSatish Balay } 17417f07f362SMatthew G. Knepley *detJ = 0.0; 174299dec3a6SMatthew G. Knepley if (numCoords == 12) { 174399dec3a6SMatthew G. Knepley const PetscInt dim = 3; 174499dec3a6SMatthew 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}; 174599dec3a6SMatthew G. Knepley 17469371c9d4SSatish Balay if (v) { 17479371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 17489371c9d4SSatish Balay } 17499566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R)); 175099dec3a6SMatthew G. Knepley if (J) { 175199dec3a6SMatthew G. Knepley const PetscInt pdim = 2; 175299dec3a6SMatthew G. Knepley 175399dec3a6SMatthew G. Knepley for (d = 0; d < pdim; d++) { 1754412e9a14SMatthew G. Knepley J0[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * pdim + d]) - PetscRealPart(coords[vorder[0] * pdim + d])); 1755412e9a14SMatthew G. Knepley J0[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[2] * pdim + d]) - PetscRealPart(coords[vorder[1] * pdim + d])); 175699dec3a6SMatthew G. Knepley } 17579566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1758923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 175999dec3a6SMatthew G. Knepley for (d = 0; d < dim; d++) { 17606858538eSMatthew G. Knepley for (PetscInt f = 0; f < dim; f++) { 176199dec3a6SMatthew G. Knepley J[d * dim + f] = 0.0; 1762ad540459SPierre Jolivet for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f]; 176399dec3a6SMatthew G. Knepley } 176499dec3a6SMatthew G. Knepley } 17659566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 176699dec3a6SMatthew G. Knepley } 1767ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 176871f58de1SToby Isaac } else if (numCoords == 8) { 176999dec3a6SMatthew G. Knepley const PetscInt dim = 2; 177099dec3a6SMatthew G. Knepley 17719371c9d4SSatish Balay if (v) { 17729371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 17739371c9d4SSatish Balay } 1774ccd2543fSMatthew G Knepley if (J) { 1775ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1776412e9a14SMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d])); 1777412e9a14SMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[3] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d])); 1778ccd2543fSMatthew G Knepley } 17799566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1780923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1781ccd2543fSMatthew G Knepley } 1782ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ); 178363a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords); 1784dfccc68fSToby Isaac } else { 1785dfccc68fSToby Isaac const PetscInt Nv = 4; 1786dfccc68fSToby Isaac const PetscInt dimR = 2; 1787412e9a14SMatthew G. Knepley PetscInt zToPlex[4] = {0, 1, 3, 2}; 1788dfccc68fSToby Isaac PetscReal zOrder[12]; 1789dfccc68fSToby Isaac PetscReal zCoeff[12]; 1790dfccc68fSToby Isaac PetscInt i, j, k, l, dim; 1791dfccc68fSToby Isaac 17929371c9d4SSatish Balay if (isTensor) { 17939371c9d4SSatish Balay zToPlex[2] = 2; 17949371c9d4SSatish Balay zToPlex[3] = 3; 17959371c9d4SSatish Balay } 1796dfccc68fSToby Isaac if (numCoords == 12) { 1797dfccc68fSToby Isaac dim = 3; 1798dfccc68fSToby Isaac } else if (numCoords == 8) { 1799dfccc68fSToby Isaac dim = 2; 180063a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords); 1801dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 1802dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 1803dfccc68fSToby Isaac 1804ad540459SPierre Jolivet for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); 1805dfccc68fSToby Isaac } 1806dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 18072df84da0SMatthew G. Knepley /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta): 18082df84da0SMatthew G. Knepley \phi^0 = (1 - xi - eta + xi eta) --> 1 = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3) 18092df84da0SMatthew G. Knepley \phi^1 = (1 + xi - eta - xi eta) --> xi = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3) 18102df84da0SMatthew G. Knepley \phi^2 = (1 - xi + eta - xi eta) --> eta = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3) 18112df84da0SMatthew G. Knepley \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3) 18122df84da0SMatthew G. Knepley */ 1813dfccc68fSToby Isaac zCoeff[dim * 0 + j] = 0.25 * (zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1814dfccc68fSToby Isaac zCoeff[dim * 1 + j] = 0.25 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1815dfccc68fSToby Isaac zCoeff[dim * 2 + j] = 0.25 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1816dfccc68fSToby Isaac zCoeff[dim * 3 + j] = 0.25 * (zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1817dfccc68fSToby Isaac } 1818dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1819dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1]; 1820dfccc68fSToby Isaac 1821dfccc68fSToby Isaac if (v) { 1822dfccc68fSToby Isaac PetscReal extPoint[4]; 1823dfccc68fSToby Isaac 1824dfccc68fSToby Isaac extPoint[0] = 1.; 1825dfccc68fSToby Isaac extPoint[1] = xi; 1826dfccc68fSToby Isaac extPoint[2] = eta; 1827dfccc68fSToby Isaac extPoint[3] = xi * eta; 1828dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1829dfccc68fSToby Isaac PetscReal val = 0.; 1830dfccc68fSToby Isaac 1831ad540459SPierre Jolivet for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j]; 1832dfccc68fSToby Isaac v[i * dim + j] = val; 1833dfccc68fSToby Isaac } 1834dfccc68fSToby Isaac } 1835dfccc68fSToby Isaac if (J) { 1836dfccc68fSToby Isaac PetscReal extJ[8]; 1837dfccc68fSToby Isaac 1838dfccc68fSToby Isaac extJ[0] = 0.; 1839dfccc68fSToby Isaac extJ[1] = 0.; 1840dfccc68fSToby Isaac extJ[2] = 1.; 1841dfccc68fSToby Isaac extJ[3] = 0.; 1842dfccc68fSToby Isaac extJ[4] = 0.; 1843dfccc68fSToby Isaac extJ[5] = 1.; 1844dfccc68fSToby Isaac extJ[6] = eta; 1845dfccc68fSToby Isaac extJ[7] = xi; 1846dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1847dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 1848dfccc68fSToby Isaac PetscReal val = 0.; 1849dfccc68fSToby Isaac 1850ad540459SPierre Jolivet for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k]; 1851dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 1852dfccc68fSToby Isaac } 1853dfccc68fSToby Isaac } 1854dfccc68fSToby Isaac if (dim == 3) { /* put the cross product in the third component of the Jacobian */ 1855dfccc68fSToby Isaac PetscReal x, y, z; 1856dfccc68fSToby Isaac PetscReal *iJ = &J[i * dim * dim]; 1857dfccc68fSToby Isaac PetscReal norm; 1858dfccc68fSToby Isaac 1859dfccc68fSToby Isaac x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0]; 1860dfccc68fSToby Isaac y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1]; 1861dfccc68fSToby Isaac z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0]; 1862dfccc68fSToby Isaac norm = PetscSqrtReal(x * x + y * y + z * z); 1863dfccc68fSToby Isaac iJ[2] = x / norm; 1864dfccc68fSToby Isaac iJ[5] = y / norm; 1865dfccc68fSToby Isaac iJ[8] = z / norm; 1866dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 1867ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); 1868dfccc68fSToby Isaac } else { 1869dfccc68fSToby Isaac DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]); 1870ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); 1871dfccc68fSToby Isaac } 1872dfccc68fSToby Isaac } 1873dfccc68fSToby Isaac } 1874dfccc68fSToby Isaac } 18756858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 18763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1877ccd2543fSMatthew G Knepley } 1878ccd2543fSMatthew G Knepley 1879d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1880d71ae5a4SJacob Faibussowitsch { 18816858538eSMatthew G. Knepley const PetscScalar *array; 1882a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1883ccd2543fSMatthew G Knepley const PetscInt dim = 3; 18846858538eSMatthew G. Knepley PetscInt numCoords, d; 18856858538eSMatthew G. Knepley PetscBool isDG; 1886ccd2543fSMatthew G Knepley 1887ccd2543fSMatthew G Knepley PetscFunctionBegin; 18886858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 18896858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 18907f07f362SMatthew G. Knepley *detJ = 0.0; 18919371c9d4SSatish Balay if (v0) { 18929371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 18939371c9d4SSatish Balay } 1894ccd2543fSMatthew G Knepley if (J) { 1895ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1896f0df753eSMatthew G. Knepley /* I orient with outward face normals */ 1897f0df753eSMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1898f0df753eSMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1899f0df753eSMatthew G. Knepley J[d * dim + 2] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1900ccd2543fSMatthew G Knepley } 19019566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 1902923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1903ccd2543fSMatthew G Knepley } 1904ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 19056858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 19063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1907ccd2543fSMatthew G Knepley } 1908ccd2543fSMatthew G Knepley 1909d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1910d71ae5a4SJacob Faibussowitsch { 19116858538eSMatthew G. Knepley const PetscScalar *array; 1912a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1913ccd2543fSMatthew G Knepley const PetscInt dim = 3; 19146858538eSMatthew G. Knepley PetscInt numCoords, d; 19156858538eSMatthew G. Knepley PetscBool isDG; 1916ccd2543fSMatthew G Knepley 1917ccd2543fSMatthew G Knepley PetscFunctionBegin; 19186858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 19196858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 1920dfccc68fSToby Isaac if (!Nq) { 19217f07f362SMatthew G. Knepley *detJ = 0.0; 19229371c9d4SSatish Balay if (v) { 19239371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 19249371c9d4SSatish Balay } 1925ccd2543fSMatthew G Knepley if (J) { 1926ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1927f0df753eSMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1928f0df753eSMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1929f0df753eSMatthew G. Knepley J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1930ccd2543fSMatthew G Knepley } 19319566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 1932923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1933ccd2543fSMatthew G Knepley } 1934ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 1935dfccc68fSToby Isaac } else { 1936dfccc68fSToby Isaac const PetscInt Nv = 8; 1937dfccc68fSToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 1938dfccc68fSToby Isaac const PetscInt dim = 3; 1939dfccc68fSToby Isaac const PetscInt dimR = 3; 1940dfccc68fSToby Isaac PetscReal zOrder[24]; 1941dfccc68fSToby Isaac PetscReal zCoeff[24]; 1942dfccc68fSToby Isaac PetscInt i, j, k, l; 1943dfccc68fSToby Isaac 1944dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 1945dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 1946dfccc68fSToby Isaac 1947ad540459SPierre Jolivet for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); 1948dfccc68fSToby Isaac } 1949dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1950dfccc68fSToby 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]); 1951dfccc68fSToby 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]); 1952dfccc68fSToby 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]); 1953dfccc68fSToby 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]); 1954dfccc68fSToby 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]); 1955dfccc68fSToby 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]); 1956dfccc68fSToby 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]); 1957dfccc68fSToby 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]); 1958dfccc68fSToby Isaac } 1959dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1960dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2]; 1961dfccc68fSToby Isaac 1962dfccc68fSToby Isaac if (v) { 196391d2b7ceSToby Isaac PetscReal extPoint[8]; 1964dfccc68fSToby Isaac 1965dfccc68fSToby Isaac extPoint[0] = 1.; 1966dfccc68fSToby Isaac extPoint[1] = xi; 1967dfccc68fSToby Isaac extPoint[2] = eta; 1968dfccc68fSToby Isaac extPoint[3] = xi * eta; 1969dfccc68fSToby Isaac extPoint[4] = theta; 1970dfccc68fSToby Isaac extPoint[5] = theta * xi; 1971dfccc68fSToby Isaac extPoint[6] = theta * eta; 1972dfccc68fSToby Isaac extPoint[7] = theta * eta * xi; 1973dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1974dfccc68fSToby Isaac PetscReal val = 0.; 1975dfccc68fSToby Isaac 1976ad540459SPierre Jolivet for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j]; 1977dfccc68fSToby Isaac v[i * dim + j] = val; 1978dfccc68fSToby Isaac } 1979dfccc68fSToby Isaac } 1980dfccc68fSToby Isaac if (J) { 1981dfccc68fSToby Isaac PetscReal extJ[24]; 1982dfccc68fSToby Isaac 19839371c9d4SSatish Balay extJ[0] = 0.; 19849371c9d4SSatish Balay extJ[1] = 0.; 19859371c9d4SSatish Balay extJ[2] = 0.; 19869371c9d4SSatish Balay extJ[3] = 1.; 19879371c9d4SSatish Balay extJ[4] = 0.; 19889371c9d4SSatish Balay extJ[5] = 0.; 19899371c9d4SSatish Balay extJ[6] = 0.; 19909371c9d4SSatish Balay extJ[7] = 1.; 19919371c9d4SSatish Balay extJ[8] = 0.; 19929371c9d4SSatish Balay extJ[9] = eta; 19939371c9d4SSatish Balay extJ[10] = xi; 19949371c9d4SSatish Balay extJ[11] = 0.; 19959371c9d4SSatish Balay extJ[12] = 0.; 19969371c9d4SSatish Balay extJ[13] = 0.; 19979371c9d4SSatish Balay extJ[14] = 1.; 19989371c9d4SSatish Balay extJ[15] = theta; 19999371c9d4SSatish Balay extJ[16] = 0.; 20009371c9d4SSatish Balay extJ[17] = xi; 20019371c9d4SSatish Balay extJ[18] = 0.; 20029371c9d4SSatish Balay extJ[19] = theta; 20039371c9d4SSatish Balay extJ[20] = eta; 20049371c9d4SSatish Balay extJ[21] = theta * eta; 20059371c9d4SSatish Balay extJ[22] = theta * xi; 20069371c9d4SSatish Balay extJ[23] = eta * xi; 2007dfccc68fSToby Isaac 2008dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 2009dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 2010dfccc68fSToby Isaac PetscReal val = 0.; 2011dfccc68fSToby Isaac 2012ad540459SPierre Jolivet for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k]; 2013dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 2014dfccc68fSToby Isaac } 2015dfccc68fSToby Isaac } 2016dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 2017ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); 2018dfccc68fSToby Isaac } 2019dfccc68fSToby Isaac } 2020dfccc68fSToby Isaac } 20216858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 20223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2023ccd2543fSMatthew G Knepley } 2024ccd2543fSMatthew G Knepley 2025d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 2026d71ae5a4SJacob Faibussowitsch { 20276858538eSMatthew G. Knepley const PetscScalar *array; 20282df84da0SMatthew G. Knepley PetscScalar *coords = NULL; 20292df84da0SMatthew G. Knepley const PetscInt dim = 3; 20306858538eSMatthew G. Knepley PetscInt numCoords, d; 20316858538eSMatthew G. Knepley PetscBool isDG; 20322df84da0SMatthew G. Knepley 20332df84da0SMatthew G. Knepley PetscFunctionBegin; 20346858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 20356858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 20362df84da0SMatthew G. Knepley if (!Nq) { 20372df84da0SMatthew G. Knepley /* Assume that the map to the reference is affine */ 20382df84da0SMatthew G. Knepley *detJ = 0.0; 20399371c9d4SSatish Balay if (v) { 20409371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 20419371c9d4SSatish Balay } 20422df84da0SMatthew G. Knepley if (J) { 20432df84da0SMatthew G. Knepley for (d = 0; d < dim; d++) { 20442df84da0SMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 20452df84da0SMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 20462df84da0SMatthew G. Knepley J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 20472df84da0SMatthew G. Knepley } 20489566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 20492df84da0SMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 20502df84da0SMatthew G. Knepley } 2051ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 20522df84da0SMatthew G. Knepley } else { 20532df84da0SMatthew G. Knepley const PetscInt dim = 3; 20542df84da0SMatthew G. Knepley const PetscInt dimR = 3; 20552df84da0SMatthew G. Knepley const PetscInt Nv = 6; 20562df84da0SMatthew G. Knepley PetscReal verts[18]; 20572df84da0SMatthew G. Knepley PetscReal coeff[18]; 20582df84da0SMatthew G. Knepley PetscInt i, j, k, l; 20592df84da0SMatthew G. Knepley 20609371c9d4SSatish Balay for (i = 0; i < Nv; ++i) 20619371c9d4SSatish Balay for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]); 20622df84da0SMatthew G. Knepley for (j = 0; j < dim; ++j) { 20632df84da0SMatthew G. Knepley /* Check for triangle, 20642df84da0SMatthew 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) 20652df84da0SMatthew 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) 20662df84da0SMatthew G. Knepley phi^2 = 1/2 (1 + eta) chi^2 = delta(-1, 1) 20672df84da0SMatthew G. Knepley 20682df84da0SMatthew G. Knepley phi^0 + phi^1 + phi^2 = 1 coef_1 = 1/2 ( chi^1 + chi^2) 20692df84da0SMatthew G. Knepley -phi^0 + phi^1 - phi^2 = xi coef_xi = 1/2 (-chi^0 + chi^1) 20702df84da0SMatthew G. Knepley -phi^0 - phi^1 + phi^2 = eta coef_eta = 1/2 (-chi^0 + chi^2) 20712df84da0SMatthew G. Knepley 20722df84da0SMatthew G. Knepley < chi_0 chi_1 chi_2> A / 1 1 1 \ / phi_0 \ <chi> I <phi>^T so we need the inverse transpose 20732df84da0SMatthew G. Knepley | -1 1 -1 | | phi_1 | = 20742df84da0SMatthew G. Knepley \ -1 -1 1 / \ phi_2 / 20752df84da0SMatthew G. Knepley 20762df84da0SMatthew 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 20772df84da0SMatthew G. Knepley */ 20782df84da0SMatthew G. Knepley /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta): 20792df84da0SMatthew G. Knepley \phi^0 = 1/4 ( -xi - eta + xi zeta + eta zeta) --> / 1 1 1 1 1 1 \ 1 20802df84da0SMatthew G. Knepley \phi^1 = 1/4 (1 + eta - zeta - eta zeta) --> | -1 1 -1 -1 -1 1 | eta 20812df84da0SMatthew G. Knepley \phi^2 = 1/4 (1 + xi - zeta - xi zeta) --> | -1 -1 1 -1 1 -1 | xi 20822df84da0SMatthew G. Knepley \phi^3 = 1/4 ( -xi - eta - xi zeta - eta zeta) --> | -1 -1 -1 1 1 1 | zeta 20832df84da0SMatthew G. Knepley \phi^4 = 1/4 (1 + xi + zeta + xi zeta) --> | 1 1 -1 -1 1 -1 | xi zeta 20842df84da0SMatthew G. Knepley \phi^5 = 1/4 (1 + eta + zeta + eta zeta) --> \ 1 -1 1 -1 -1 1 / eta zeta 20852df84da0SMatthew G. Knepley 1/4 / 0 1 1 0 1 1 \ 20862df84da0SMatthew G. Knepley | -1 1 0 -1 0 1 | 20872df84da0SMatthew G. Knepley | -1 0 1 -1 1 0 | 20882df84da0SMatthew G. Knepley | 0 -1 -1 0 1 1 | 20892df84da0SMatthew G. Knepley | 1 0 -1 -1 1 0 | 20902df84da0SMatthew G. Knepley \ 1 -1 0 -1 0 1 / 20912df84da0SMatthew G. Knepley */ 20922df84da0SMatthew G. Knepley coeff[dim * 0 + j] = (1. / 4.) * (verts[dim * 1 + j] + verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]); 20932df84da0SMatthew G. Knepley coeff[dim * 1 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]); 20942df84da0SMatthew G. Knepley coeff[dim * 2 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]); 20952df84da0SMatthew G. Knepley coeff[dim * 3 + j] = (1. / 4.) * (-verts[dim * 1 + j] - verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]); 20962df84da0SMatthew G. Knepley coeff[dim * 4 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]); 20972df84da0SMatthew G. Knepley coeff[dim * 5 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]); 20982df84da0SMatthew G. Knepley /* For reference prism: 20992df84da0SMatthew G. Knepley {0, 0, 0} 21002df84da0SMatthew G. Knepley {0, 1, 0} 21012df84da0SMatthew G. Knepley {1, 0, 0} 21022df84da0SMatthew G. Knepley {0, 0, 1} 21032df84da0SMatthew G. Knepley {0, 0, 0} 21042df84da0SMatthew G. Knepley {0, 0, 0} 21052df84da0SMatthew G. Knepley */ 21062df84da0SMatthew G. Knepley } 21072df84da0SMatthew G. Knepley for (i = 0; i < Nq; ++i) { 21082df84da0SMatthew G. Knepley const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2]; 21092df84da0SMatthew G. Knepley 21102df84da0SMatthew G. Knepley if (v) { 21112df84da0SMatthew G. Knepley PetscReal extPoint[6]; 21122df84da0SMatthew G. Knepley PetscInt c; 21132df84da0SMatthew G. Knepley 21142df84da0SMatthew G. Knepley extPoint[0] = 1.; 21152df84da0SMatthew G. Knepley extPoint[1] = eta; 21162df84da0SMatthew G. Knepley extPoint[2] = xi; 21172df84da0SMatthew G. Knepley extPoint[3] = zeta; 21182df84da0SMatthew G. Knepley extPoint[4] = xi * zeta; 21192df84da0SMatthew G. Knepley extPoint[5] = eta * zeta; 21202df84da0SMatthew G. Knepley for (c = 0; c < dim; ++c) { 21212df84da0SMatthew G. Knepley PetscReal val = 0.; 21222df84da0SMatthew G. Knepley 2123ad540459SPierre Jolivet for (k = 0; k < Nv; ++k) val += extPoint[k] * coeff[k * dim + c]; 21242df84da0SMatthew G. Knepley v[i * dim + c] = val; 21252df84da0SMatthew G. Knepley } 21262df84da0SMatthew G. Knepley } 21272df84da0SMatthew G. Knepley if (J) { 21282df84da0SMatthew G. Knepley PetscReal extJ[18]; 21292df84da0SMatthew G. Knepley 21309371c9d4SSatish Balay extJ[0] = 0.; 21319371c9d4SSatish Balay extJ[1] = 0.; 21329371c9d4SSatish Balay extJ[2] = 0.; 21339371c9d4SSatish Balay extJ[3] = 0.; 21349371c9d4SSatish Balay extJ[4] = 1.; 21359371c9d4SSatish Balay extJ[5] = 0.; 21369371c9d4SSatish Balay extJ[6] = 1.; 21379371c9d4SSatish Balay extJ[7] = 0.; 21389371c9d4SSatish Balay extJ[8] = 0.; 21399371c9d4SSatish Balay extJ[9] = 0.; 21409371c9d4SSatish Balay extJ[10] = 0.; 21419371c9d4SSatish Balay extJ[11] = 1.; 21429371c9d4SSatish Balay extJ[12] = zeta; 21439371c9d4SSatish Balay extJ[13] = 0.; 21449371c9d4SSatish Balay extJ[14] = xi; 21459371c9d4SSatish Balay extJ[15] = 0.; 21469371c9d4SSatish Balay extJ[16] = zeta; 21479371c9d4SSatish Balay extJ[17] = eta; 21482df84da0SMatthew G. Knepley 21492df84da0SMatthew G. Knepley for (j = 0; j < dim; j++) { 21502df84da0SMatthew G. Knepley for (k = 0; k < dimR; k++) { 21512df84da0SMatthew G. Knepley PetscReal val = 0.; 21522df84da0SMatthew G. Knepley 2153ad540459SPierre Jolivet for (l = 0; l < Nv; l++) val += coeff[dim * l + j] * extJ[dimR * l + k]; 21542df84da0SMatthew G. Knepley J[i * dim * dim + dim * j + k] = val; 21552df84da0SMatthew G. Knepley } 21562df84da0SMatthew G. Knepley } 21572df84da0SMatthew G. Knepley DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 2158ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); 21592df84da0SMatthew G. Knepley } 21602df84da0SMatthew G. Knepley } 21612df84da0SMatthew G. Knepley } 21626858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 21633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 21642df84da0SMatthew G. Knepley } 21652df84da0SMatthew G. Knepley 2166d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 2167d71ae5a4SJacob Faibussowitsch { 2168ba2698f1SMatthew G. Knepley DMPolytopeType ct; 2169dfccc68fSToby Isaac PetscInt depth, dim, coordDim, coneSize, i; 2170dfccc68fSToby Isaac PetscInt Nq = 0; 2171dfccc68fSToby Isaac const PetscReal *points = NULL; 2172dfccc68fSToby Isaac DMLabel depthLabel; 2173c330f8ffSToby Isaac PetscReal xi0[3] = {-1., -1., -1.}, v0[3], J0[9], detJ0; 2174dfccc68fSToby Isaac PetscBool isAffine = PETSC_TRUE; 2175dfccc68fSToby Isaac 2176dfccc68fSToby Isaac PetscFunctionBegin; 21779566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 21789566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 21799566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dm, &depthLabel)); 21809566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(depthLabel, cell, &dim)); 218148a46eb9SPierre Jolivet if (depth == 1 && dim == 1) PetscCall(DMGetDimension(dm, &dim)); 21829566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &coordDim)); 218363a3b9bcSJacob Faibussowitsch PetscCheck(coordDim <= 3, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %" PetscInt_FMT " > 3", coordDim); 21849566063dSJacob Faibussowitsch if (quad) PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL)); 21859566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2186ba2698f1SMatthew G. Knepley switch (ct) { 2187ba2698f1SMatthew G. Knepley case DM_POLYTOPE_POINT: 21889566063dSJacob Faibussowitsch PetscCall(DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ)); 2189dfccc68fSToby Isaac isAffine = PETSC_FALSE; 2190dfccc68fSToby Isaac break; 2191ba2698f1SMatthew G. Knepley case DM_POLYTOPE_SEGMENT: 2192412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 21939566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 21949566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ)); 2195dfccc68fSToby Isaac break; 2196ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 21979566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 21989566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ)); 2199dfccc68fSToby Isaac break; 2200ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 22019566063dSJacob Faibussowitsch PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ)); 2202412e9a14SMatthew G. Knepley isAffine = PETSC_FALSE; 2203412e9a14SMatthew G. Knepley break; 2204412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 22059566063dSJacob Faibussowitsch PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ)); 2206dfccc68fSToby Isaac isAffine = PETSC_FALSE; 2207dfccc68fSToby Isaac break; 2208ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 22099566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 22109566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ)); 2211dfccc68fSToby Isaac break; 2212ba2698f1SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 22139566063dSJacob Faibussowitsch PetscCall(DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ)); 2214dfccc68fSToby Isaac isAffine = PETSC_FALSE; 2215dfccc68fSToby Isaac break; 22162df84da0SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM: 22179566063dSJacob Faibussowitsch PetscCall(DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ)); 22182df84da0SMatthew G. Knepley isAffine = PETSC_FALSE; 22192df84da0SMatthew G. Knepley break; 2220d71ae5a4SJacob Faibussowitsch default: 2221d71ae5a4SJacob Faibussowitsch 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))]); 2222dfccc68fSToby Isaac } 22237318780aSToby Isaac if (isAffine && Nq) { 2224dfccc68fSToby Isaac if (v) { 2225ad540459SPierre Jolivet for (i = 0; i < Nq; i++) CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]); 2226dfccc68fSToby Isaac } 22277318780aSToby Isaac if (detJ) { 2228ad540459SPierre Jolivet for (i = 0; i < Nq; i++) detJ[i] = detJ0; 22297318780aSToby Isaac } 22307318780aSToby Isaac if (J) { 22317318780aSToby Isaac PetscInt k; 22327318780aSToby Isaac 22337318780aSToby Isaac for (i = 0, k = 0; i < Nq; i++) { 2234dfccc68fSToby Isaac PetscInt j; 2235dfccc68fSToby Isaac 2236ad540459SPierre Jolivet for (j = 0; j < coordDim * coordDim; j++, k++) J[k] = J0[j]; 22377318780aSToby Isaac } 22387318780aSToby Isaac } 22397318780aSToby Isaac if (invJ) { 22407318780aSToby Isaac PetscInt k; 22417318780aSToby Isaac switch (coordDim) { 2242d71ae5a4SJacob Faibussowitsch case 0: 2243d71ae5a4SJacob Faibussowitsch break; 2244d71ae5a4SJacob Faibussowitsch case 1: 2245d71ae5a4SJacob Faibussowitsch invJ[0] = 1. / J0[0]; 2246d71ae5a4SJacob Faibussowitsch break; 2247d71ae5a4SJacob Faibussowitsch case 2: 2248d71ae5a4SJacob Faibussowitsch DMPlex_Invert2D_Internal(invJ, J0, detJ0); 2249d71ae5a4SJacob Faibussowitsch break; 2250d71ae5a4SJacob Faibussowitsch case 3: 2251d71ae5a4SJacob Faibussowitsch DMPlex_Invert3D_Internal(invJ, J0, detJ0); 2252d71ae5a4SJacob Faibussowitsch break; 22537318780aSToby Isaac } 22547318780aSToby Isaac for (i = 1, k = coordDim * coordDim; i < Nq; i++) { 22557318780aSToby Isaac PetscInt j; 22567318780aSToby Isaac 2257ad540459SPierre Jolivet for (j = 0; j < coordDim * coordDim; j++, k++) invJ[k] = invJ[j]; 2258dfccc68fSToby Isaac } 2259dfccc68fSToby Isaac } 2260dfccc68fSToby Isaac } 22613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2262dfccc68fSToby Isaac } 2263dfccc68fSToby Isaac 2264ccd2543fSMatthew G Knepley /*@C 22658e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell 2266ccd2543fSMatthew G Knepley 226720f4b53cSBarry Smith Collective 2268ccd2543fSMatthew G Knepley 22694165533cSJose E. Roman Input Parameters: 227020f4b53cSBarry Smith + dm - the `DMPLEX` 2271ccd2543fSMatthew G Knepley - cell - the cell 2272ccd2543fSMatthew G Knepley 22734165533cSJose E. Roman Output Parameters: 22749b172b3aSMatthew Knepley + v0 - the translation part of this affine transform, meaning the translation to the origin (not the first vertex of the reference cell) 2275ccd2543fSMatthew G Knepley . J - the Jacobian of the transform from the reference element 2276ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian 2277ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant 2278ccd2543fSMatthew G Knepley 2279ccd2543fSMatthew G Knepley Level: advanced 2280ccd2543fSMatthew G Knepley 228120f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeCellGeometryFEM()`, `DMGetCoordinateSection()`, `DMGetCoordinates()` 2282ccd2543fSMatthew G Knepley @*/ 2283d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 2284d71ae5a4SJacob Faibussowitsch { 2285ccd2543fSMatthew G Knepley PetscFunctionBegin; 22869566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, NULL, v0, J, invJ, detJ)); 22873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22888e0841e0SMatthew G. Knepley } 22898e0841e0SMatthew G. Knepley 2290d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 2291d71ae5a4SJacob Faibussowitsch { 22926858538eSMatthew G. Knepley const PetscScalar *array; 22938e0841e0SMatthew G. Knepley PetscScalar *coords = NULL; 22946858538eSMatthew G. Knepley PetscInt numCoords; 22956858538eSMatthew G. Knepley PetscBool isDG; 22966858538eSMatthew G. Knepley PetscQuadrature feQuad; 22978e0841e0SMatthew G. Knepley const PetscReal *quadPoints; 2298ef0bb6c7SMatthew G. Knepley PetscTabulation T; 22996858538eSMatthew G. Knepley PetscInt dim, cdim, pdim, qdim, Nq, q; 23008e0841e0SMatthew G. Knepley 23018e0841e0SMatthew G. Knepley PetscFunctionBegin; 23029566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 23039566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 23046858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords)); 2305dfccc68fSToby Isaac if (!quad) { /* use the first point of the first functional of the dual space */ 2306dfccc68fSToby Isaac PetscDualSpace dsp; 2307dfccc68fSToby Isaac 23089566063dSJacob Faibussowitsch PetscCall(PetscFEGetDualSpace(fe, &dsp)); 23099566063dSJacob Faibussowitsch PetscCall(PetscDualSpaceGetFunctional(dsp, 0, &quad)); 23109566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL)); 2311dfccc68fSToby Isaac Nq = 1; 2312dfccc68fSToby Isaac } else { 23139566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL)); 2314dfccc68fSToby Isaac } 23159566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 23169566063dSJacob Faibussowitsch PetscCall(PetscFEGetQuadrature(fe, &feQuad)); 2317dfccc68fSToby Isaac if (feQuad == quad) { 23189566063dSJacob Faibussowitsch PetscCall(PetscFEGetCellTabulation(fe, J ? 1 : 0, &T)); 231963a3b9bcSJacob 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); 2320dfccc68fSToby Isaac } else { 23219566063dSJacob Faibussowitsch PetscCall(PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T)); 2322dfccc68fSToby Isaac } 232363a3b9bcSJacob Faibussowitsch PetscCheck(qdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %" PetscInt_FMT " != quadrature dimension %" PetscInt_FMT, dim, qdim); 2324ef0bb6c7SMatthew G. Knepley { 2325ef0bb6c7SMatthew G. Knepley const PetscReal *basis = T->T[0]; 2326ef0bb6c7SMatthew G. Knepley const PetscReal *basisDer = T->T[1]; 2327ef0bb6c7SMatthew G. Knepley PetscReal detJt; 2328ef0bb6c7SMatthew G. Knepley 2329a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG) 233063a3b9bcSJacob Faibussowitsch PetscCheck(Nq == T->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %" PetscInt_FMT " != %" PetscInt_FMT, Nq, T->Np); 233163a3b9bcSJacob Faibussowitsch PetscCheck(pdim == T->Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %" PetscInt_FMT " != %" PetscInt_FMT, pdim, T->Nb); 233263a3b9bcSJacob Faibussowitsch PetscCheck(dim == T->Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %" PetscInt_FMT " != %" PetscInt_FMT, dim, T->Nc); 233363a3b9bcSJacob Faibussowitsch PetscCheck(cdim == T->cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %" PetscInt_FMT " != %" PetscInt_FMT, cdim, T->cdim); 2334a2a9e04cSMatthew G. Knepley #endif 2335dfccc68fSToby Isaac if (v) { 23369566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(v, Nq * cdim)); 2337f960e424SToby Isaac for (q = 0; q < Nq; ++q) { 2338f960e424SToby Isaac PetscInt i, k; 2339f960e424SToby Isaac 2340301b184aSMatthew G. Knepley for (k = 0; k < pdim; ++k) { 2341301b184aSMatthew G. Knepley const PetscInt vertex = k / cdim; 2342ad540459SPierre Jolivet for (i = 0; i < cdim; ++i) v[q * cdim + i] += basis[(q * pdim + k) * cdim + i] * PetscRealPart(coords[vertex * cdim + i]); 2343301b184aSMatthew G. Knepley } 23449566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * pdim * cdim)); 2345f960e424SToby Isaac } 2346f960e424SToby Isaac } 23478e0841e0SMatthew G. Knepley if (J) { 23489566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(J, Nq * cdim * cdim)); 23498e0841e0SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 23508e0841e0SMatthew G. Knepley PetscInt i, j, k, c, r; 23518e0841e0SMatthew G. Knepley 23528e0841e0SMatthew G. Knepley /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */ 2353301b184aSMatthew G. Knepley for (k = 0; k < pdim; ++k) { 2354301b184aSMatthew G. Knepley const PetscInt vertex = k / cdim; 2355301b184aSMatthew G. Knepley for (j = 0; j < dim; ++j) { 2356ad540459SPierre Jolivet 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]); 2357301b184aSMatthew G. Knepley } 2358301b184aSMatthew G. Knepley } 23599566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * pdim * dim * cdim)); 23608e0841e0SMatthew G. Knepley if (cdim > dim) { 23618e0841e0SMatthew G. Knepley for (c = dim; c < cdim; ++c) 23629371c9d4SSatish Balay for (r = 0; r < cdim; ++r) J[r * cdim + c] = r == c ? 1.0 : 0.0; 23638e0841e0SMatthew G. Knepley } 2364f960e424SToby Isaac if (!detJ && !invJ) continue; 2365a63b72c6SToby Isaac detJt = 0.; 23668e0841e0SMatthew G. Knepley switch (cdim) { 23678e0841e0SMatthew G. Knepley case 3: 2368037dc194SToby Isaac DMPlex_Det3D_Internal(&detJt, &J[q * cdim * dim]); 2369ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt); 237017fe8556SMatthew G. Knepley break; 237149dc4407SMatthew G. Knepley case 2: 23729f328543SToby Isaac DMPlex_Det2D_Internal(&detJt, &J[q * cdim * dim]); 2373ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt); 237449dc4407SMatthew G. Knepley break; 23758e0841e0SMatthew G. Knepley case 1: 2376037dc194SToby Isaac detJt = J[q * cdim * dim]; 2377037dc194SToby Isaac if (invJ) invJ[q * cdim * dim] = 1.0 / detJt; 237849dc4407SMatthew G. Knepley } 2379f960e424SToby Isaac if (detJ) detJ[q] = detJt; 238049dc4407SMatthew G. Knepley } 238108401ef6SPierre Jolivet } else PetscCheck(!detJ && !invJ, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ"); 238249dc4407SMatthew G. Knepley } 23839566063dSJacob Faibussowitsch if (feQuad != quad) PetscCall(PetscTabulationDestroy(&T)); 23846858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords)); 23853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23868e0841e0SMatthew G. Knepley } 23878e0841e0SMatthew G. Knepley 23888e0841e0SMatthew G. Knepley /*@C 23898e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell 23908e0841e0SMatthew G. Knepley 239120f4b53cSBarry Smith Collective 23928e0841e0SMatthew G. Knepley 23934165533cSJose E. Roman Input Parameters: 239420f4b53cSBarry Smith + dm - the `DMPLEX` 23958e0841e0SMatthew G. Knepley . cell - the cell 239620f4b53cSBarry Smith - quad - the quadrature containing the points in the reference element where the geometry will be evaluated. If `quad` is `NULL`, geometry will be 2397dfccc68fSToby Isaac evaluated at the first vertex of the reference element 23988e0841e0SMatthew G. Knepley 23994165533cSJose E. Roman Output Parameters: 2400dfccc68fSToby Isaac + v - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element 24018e0841e0SMatthew G. Knepley . J - the Jacobian of the transform from the reference element at each quadrature point 24028e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point 24038e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point 24048e0841e0SMatthew G. Knepley 24058e0841e0SMatthew G. Knepley Level: advanced 24068e0841e0SMatthew G. Knepley 240720f4b53cSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinateSection()`, `DMGetCoordinates()` 24088e0841e0SMatthew G. Knepley @*/ 2409d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 2410d71ae5a4SJacob Faibussowitsch { 2411bb4a5db5SMatthew G. Knepley DM cdm; 2412dfccc68fSToby Isaac PetscFE fe = NULL; 24138e0841e0SMatthew G. Knepley 24148e0841e0SMatthew G. Knepley PetscFunctionBegin; 2415dadcf809SJacob Faibussowitsch PetscValidRealPointer(detJ, 7); 24169566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 2417bb4a5db5SMatthew G. Knepley if (cdm) { 2418dfccc68fSToby Isaac PetscClassId id; 2419dfccc68fSToby Isaac PetscInt numFields; 2420e5e52638SMatthew G. Knepley PetscDS prob; 2421dfccc68fSToby Isaac PetscObject disc; 2422dfccc68fSToby Isaac 24239566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(cdm, &numFields)); 2424dfccc68fSToby Isaac if (numFields) { 24259566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &prob)); 24269566063dSJacob Faibussowitsch PetscCall(PetscDSGetDiscretization(prob, 0, &disc)); 24279566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 2428ad540459SPierre Jolivet if (id == PETSCFE_CLASSID) fe = (PetscFE)disc; 2429dfccc68fSToby Isaac } 2430dfccc68fSToby Isaac } 24319566063dSJacob Faibussowitsch if (!fe) PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ)); 24329566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ)); 24333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2434ccd2543fSMatthew G Knepley } 2435834e62ceSMatthew G. Knepley 2436d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2437d71ae5a4SJacob Faibussowitsch { 24389bf2564aSMatt McGurn PetscSection coordSection; 24399bf2564aSMatt McGurn Vec coordinates; 24409bf2564aSMatt McGurn const PetscScalar *coords = NULL; 24419bf2564aSMatt McGurn PetscInt d, dof, off; 24429bf2564aSMatt McGurn 24439bf2564aSMatt McGurn PetscFunctionBegin; 24449566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 24459566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 24469566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coords)); 24479bf2564aSMatt McGurn 24489bf2564aSMatt McGurn /* for a point the centroid is just the coord */ 24499bf2564aSMatt McGurn if (centroid) { 24509566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, cell, &dof)); 24519566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, cell, &off)); 2452ad540459SPierre Jolivet for (d = 0; d < dof; d++) centroid[d] = PetscRealPart(coords[off + d]); 24539bf2564aSMatt McGurn } 24549bf2564aSMatt McGurn if (normal) { 24559bf2564aSMatt McGurn const PetscInt *support, *cones; 24569bf2564aSMatt McGurn PetscInt supportSize; 24579bf2564aSMatt McGurn PetscReal norm, sign; 24589bf2564aSMatt McGurn 24599bf2564aSMatt McGurn /* compute the norm based upon the support centroids */ 24609566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, cell, &supportSize)); 24619566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, cell, &support)); 24629566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL)); 24639bf2564aSMatt McGurn 24649bf2564aSMatt McGurn /* Take the normal from the centroid of the support to the vertex*/ 24659566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, cell, &dof)); 24669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, cell, &off)); 2467ad540459SPierre Jolivet for (d = 0; d < dof; d++) normal[d] -= PetscRealPart(coords[off + d]); 24689bf2564aSMatt McGurn 24699bf2564aSMatt McGurn /* Determine the sign of the normal based upon its location in the support */ 24709566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, support[0], &cones)); 24719bf2564aSMatt McGurn sign = cones[0] == cell ? 1.0 : -1.0; 24729bf2564aSMatt McGurn 24739bf2564aSMatt McGurn norm = DMPlex_NormD_Internal(dim, normal); 24749bf2564aSMatt McGurn for (d = 0; d < dim; ++d) normal[d] /= (norm * sign); 24759bf2564aSMatt McGurn } 2476ad540459SPierre Jolivet if (vol) *vol = 1.0; 24779566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coords)); 24783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 24799bf2564aSMatt McGurn } 24809bf2564aSMatt McGurn 2481d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2482d71ae5a4SJacob Faibussowitsch { 24836858538eSMatthew G. Knepley const PetscScalar *array; 2484a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 248521d6a034SMatthew G. Knepley PetscInt cdim, coordSize, d; 24866858538eSMatthew G. Knepley PetscBool isDG; 2487cc08537eSMatthew G. Knepley 2488cc08537eSMatthew G. Knepley PetscFunctionBegin; 248921d6a034SMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 24906858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 249121d6a034SMatthew G. Knepley PetscCheck(coordSize == cdim * 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Edge has %" PetscInt_FMT " coordinates != %" PetscInt_FMT, coordSize, cdim * 2); 2492cc08537eSMatthew G. Knepley if (centroid) { 249321d6a034SMatthew G. Knepley for (d = 0; d < cdim; ++d) centroid[d] = 0.5 * PetscRealPart(coords[d] + coords[cdim + d]); 2494cc08537eSMatthew G. Knepley } 2495cc08537eSMatthew G. Knepley if (normal) { 2496a60a936bSMatthew G. Knepley PetscReal norm; 2497a60a936bSMatthew G. Knepley 249821d6a034SMatthew G. Knepley switch (cdim) { 249921d6a034SMatthew G. Knepley case 3: 2500f315e28eSPierre Jolivet normal[2] = 0.; /* fall through */ 250121d6a034SMatthew G. Knepley case 2: 250221d6a034SMatthew G. Knepley normal[0] = -PetscRealPart(coords[1] - coords[cdim + 1]); 250321d6a034SMatthew G. Knepley normal[1] = PetscRealPart(coords[0] - coords[cdim + 0]); 250421d6a034SMatthew G. Knepley break; 250521d6a034SMatthew G. Knepley case 1: 250621d6a034SMatthew G. Knepley normal[0] = 1.0; 250721d6a034SMatthew G. Knepley break; 250821d6a034SMatthew G. Knepley default: 250921d6a034SMatthew G. Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Dimension %" PetscInt_FMT " not supported", cdim); 251021d6a034SMatthew G. Knepley } 251121d6a034SMatthew G. Knepley norm = DMPlex_NormD_Internal(cdim, normal); 251221d6a034SMatthew G. Knepley for (d = 0; d < cdim; ++d) normal[d] /= norm; 2513cc08537eSMatthew G. Knepley } 2514cc08537eSMatthew G. Knepley if (vol) { 2515714b99b6SMatthew G. Knepley *vol = 0.0; 251621d6a034SMatthew G. Knepley for (d = 0; d < cdim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - coords[cdim + d])); 2517714b99b6SMatthew G. Knepley *vol = PetscSqrtReal(*vol); 2518cc08537eSMatthew G. Knepley } 25196858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 25203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2521cc08537eSMatthew G. Knepley } 2522cc08537eSMatthew G. Knepley 2523cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */ 2524d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2525d71ae5a4SJacob Faibussowitsch { 2526412e9a14SMatthew G. Knepley DMPolytopeType ct; 25276858538eSMatthew G. Knepley const PetscScalar *array; 2528cc08537eSMatthew G. Knepley PetscScalar *coords = NULL; 25296858538eSMatthew G. Knepley PetscInt coordSize; 25306858538eSMatthew G. Knepley PetscBool isDG; 2531793a2a13SMatthew G. Knepley PetscInt fv[4] = {0, 1, 2, 3}; 25326858538eSMatthew G. Knepley PetscInt cdim, numCorners, p, d; 2533cc08537eSMatthew G. Knepley 2534cc08537eSMatthew G. Knepley PetscFunctionBegin; 2535793a2a13SMatthew G. Knepley /* Must check for hybrid cells because prisms have a different orientation scheme */ 25369566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2537412e9a14SMatthew G. Knepley switch (ct) { 25389371c9d4SSatish Balay case DM_POLYTOPE_SEG_PRISM_TENSOR: 25399371c9d4SSatish Balay fv[2] = 3; 25409371c9d4SSatish Balay fv[3] = 2; 25419371c9d4SSatish Balay break; 2542d71ae5a4SJacob Faibussowitsch default: 2543d71ae5a4SJacob Faibussowitsch break; 2544412e9a14SMatthew G. Knepley } 25459566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 25466858538eSMatthew G. Knepley PetscCall(DMPlexGetConeSize(dm, cell, &numCorners)); 25476858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 25483f27a4e6SJed Brown { 25493f27a4e6SJed Brown PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm; 2550793a2a13SMatthew G. Knepley 25513f27a4e6SJed Brown for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]); 25524f99dae5SMatthew G. Knepley for (p = 0; p < numCorners - 2; ++p) { 25533f27a4e6SJed Brown PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.}; 25543f27a4e6SJed Brown for (d = 0; d < cdim; d++) { 25553f27a4e6SJed Brown e0[d] = PetscRealPart(coords[cdim * fv[p + 1] + d]) - origin[d]; 25563f27a4e6SJed Brown e1[d] = PetscRealPart(coords[cdim * fv[p + 2] + d]) - origin[d]; 25573f27a4e6SJed Brown } 25583f27a4e6SJed Brown const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1]; 25593f27a4e6SJed Brown const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2]; 25603f27a4e6SJed Brown const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0]; 25613f27a4e6SJed Brown const PetscReal a = PetscSqrtReal(dx * dx + dy * dy + dz * dz); 25624f99dae5SMatthew G. Knepley 25634f99dae5SMatthew G. Knepley n[0] += dx; 25644f99dae5SMatthew G. Knepley n[1] += dy; 25654f99dae5SMatthew G. Knepley n[2] += dz; 2566ad540459SPierre Jolivet 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.; 2567ceee4971SMatthew G. Knepley } 25684f99dae5SMatthew G. Knepley norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]); 25694f99dae5SMatthew G. Knepley n[0] /= norm; 25704f99dae5SMatthew G. Knepley n[1] /= norm; 25714f99dae5SMatthew G. Knepley n[2] /= norm; 25724f99dae5SMatthew G. Knepley c[0] /= norm; 25734f99dae5SMatthew G. Knepley c[1] /= norm; 25744f99dae5SMatthew G. Knepley c[2] /= norm; 25754f99dae5SMatthew G. Knepley if (vol) *vol = 0.5 * norm; 25769371c9d4SSatish Balay if (centroid) 25779371c9d4SSatish Balay for (d = 0; d < cdim; ++d) centroid[d] = c[d]; 25789371c9d4SSatish Balay if (normal) 25799371c9d4SSatish Balay for (d = 0; d < cdim; ++d) normal[d] = n[d]; 25800a1d6728SMatthew G. Knepley } 25816858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 25823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2583cc08537eSMatthew G. Knepley } 2584cc08537eSMatthew G. Knepley 25850ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */ 2586d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2587d71ae5a4SJacob Faibussowitsch { 2588412e9a14SMatthew G. Knepley DMPolytopeType ct; 25896858538eSMatthew G. Knepley const PetscScalar *array; 25900ec8681fSMatthew G. Knepley PetscScalar *coords = NULL; 25916858538eSMatthew G. Knepley PetscInt coordSize; 25926858538eSMatthew G. Knepley PetscBool isDG; 25933f27a4e6SJed Brown PetscReal vsum = 0.0, vtmp, coordsTmp[3 * 3], origin[3]; 25946858538eSMatthew G. Knepley const PetscInt order[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; 25956858538eSMatthew G. Knepley const PetscInt *cone, *faceSizes, *faces; 25966858538eSMatthew G. Knepley const DMPolytopeType *faceTypes; 2597793a2a13SMatthew G. Knepley PetscBool isHybrid = PETSC_FALSE; 25986858538eSMatthew G. Knepley PetscInt numFaces, f, fOff = 0, p, d; 25990ec8681fSMatthew G. Knepley 26000ec8681fSMatthew G. Knepley PetscFunctionBegin; 260163a3b9bcSJacob Faibussowitsch PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No support for dim %" PetscInt_FMT " > 3", dim); 2602793a2a13SMatthew G. Knepley /* Must check for hybrid cells because prisms have a different orientation scheme */ 26039566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2604412e9a14SMatthew G. Knepley switch (ct) { 2605412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 2606412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 2607412e9a14SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM_TENSOR: 2608d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_QUAD_PRISM_TENSOR: 2609d71ae5a4SJacob Faibussowitsch isHybrid = PETSC_TRUE; 2610d71ae5a4SJacob Faibussowitsch default: 2611d71ae5a4SJacob Faibussowitsch break; 2612412e9a14SMatthew G. Knepley } 2613793a2a13SMatthew G. Knepley 26149371c9d4SSatish Balay if (centroid) 26159371c9d4SSatish Balay for (d = 0; d < dim; ++d) centroid[d] = 0.0; 26166858538eSMatthew G. Knepley PetscCall(DMPlexGetCone(dm, cell, &cone)); 26176858538eSMatthew G. Knepley 26186858538eSMatthew G. Knepley // Using the closure of faces for coordinates does not work in periodic geometries, so we index into the cell coordinates 26196858538eSMatthew G. Knepley PetscCall(DMPlexGetRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces)); 26206858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 26210ec8681fSMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 2622793a2a13SMatthew G. Knepley PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */ 2623793a2a13SMatthew G. Knepley 26243f27a4e6SJed Brown // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and 26253f27a4e6SJed Brown // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex 26263f27a4e6SJed Brown // so that all tetrahedra have positive volume. 26279371c9d4SSatish Balay if (f == 0) 26289371c9d4SSatish Balay for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]); 26296858538eSMatthew G. Knepley switch (faceTypes[f]) { 2630ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 26310ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 26326858538eSMatthew G. Knepley coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + 0] * dim + d]) - origin[d]; 26336858538eSMatthew G. Knepley coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + 1] * dim + d]) - origin[d]; 26346858538eSMatthew G. Knepley coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + 2] * dim + d]) - origin[d]; 26350ec8681fSMatthew G. Knepley } 26360ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 26376858538eSMatthew G. Knepley if (flip) vtmp = -vtmp; 26380ec8681fSMatthew G. Knepley vsum += vtmp; 26394f25033aSJed Brown if (centroid) { /* Centroid of OABC = (a+b+c)/4 */ 26400ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 26411ee9d5ecSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp; 26420ec8681fSMatthew G. Knepley } 26430ec8681fSMatthew G. Knepley } 26440ec8681fSMatthew G. Knepley break; 2645ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 26469371c9d4SSatish Balay case DM_POLYTOPE_SEG_PRISM_TENSOR: { 2647793a2a13SMatthew G. Knepley PetscInt fv[4] = {0, 1, 2, 3}; 2648793a2a13SMatthew G. Knepley 2649793a2a13SMatthew G. Knepley /* Side faces for hybrid cells are are stored as tensor products */ 26509371c9d4SSatish Balay if (isHybrid && f > 1) { 26519371c9d4SSatish Balay fv[2] = 3; 26529371c9d4SSatish Balay fv[3] = 2; 26539371c9d4SSatish Balay } 26540ec8681fSMatthew G. Knepley /* DO FOR PYRAMID */ 26550ec8681fSMatthew G. Knepley /* First tet */ 26560ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 26576858538eSMatthew G. Knepley coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[0]] * dim + d]) - origin[d]; 26586858538eSMatthew G. Knepley coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d]; 26596858538eSMatthew G. Knepley coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d]; 26600ec8681fSMatthew G. Knepley } 26610ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 26626858538eSMatthew G. Knepley if (flip) vtmp = -vtmp; 26630ec8681fSMatthew G. Knepley vsum += vtmp; 26640ec8681fSMatthew G. Knepley if (centroid) { 26650ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 26660ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp; 26670ec8681fSMatthew G. Knepley } 26680ec8681fSMatthew G. Knepley } 26690ec8681fSMatthew G. Knepley /* Second tet */ 26700ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 26716858538eSMatthew G. Knepley coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d]; 26726858538eSMatthew G. Knepley coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[2]] * dim + d]) - origin[d]; 26736858538eSMatthew G. Knepley coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d]; 26740ec8681fSMatthew G. Knepley } 26750ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 26766858538eSMatthew G. Knepley if (flip) vtmp = -vtmp; 26770ec8681fSMatthew G. Knepley vsum += vtmp; 26780ec8681fSMatthew G. Knepley if (centroid) { 26790ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 26800ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp; 26810ec8681fSMatthew G. Knepley } 26820ec8681fSMatthew G. Knepley } 26830ec8681fSMatthew G. Knepley break; 2684793a2a13SMatthew G. Knepley } 2685d71ae5a4SJacob Faibussowitsch default: 2686d71ae5a4SJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %" PetscInt_FMT " of type %s", cone[f], DMPolytopeTypes[ct]); 26870ec8681fSMatthew G. Knepley } 26886858538eSMatthew G. Knepley fOff += faceSizes[f]; 26890ec8681fSMatthew G. Knepley } 26906858538eSMatthew G. Knepley PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces)); 26916858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 26928763be8eSMatthew G. Knepley if (vol) *vol = PetscAbsReal(vsum); 26939371c9d4SSatish Balay if (normal) 26949371c9d4SSatish Balay for (d = 0; d < dim; ++d) normal[d] = 0.0; 26959371c9d4SSatish Balay if (centroid) 26969371c9d4SSatish Balay for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum * 4) + origin[d]; 26973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26980ec8681fSMatthew G. Knepley } 26990ec8681fSMatthew G. Knepley 2700834e62ceSMatthew G. Knepley /*@C 2701834e62ceSMatthew G. Knepley DMPlexComputeCellGeometryFVM - Compute the volume for a given cell 2702834e62ceSMatthew G. Knepley 270320f4b53cSBarry Smith Collective 2704834e62ceSMatthew G. Knepley 27054165533cSJose E. Roman Input Parameters: 270620f4b53cSBarry Smith + dm - the `DMPLEX` 2707834e62ceSMatthew G. Knepley - cell - the cell 2708834e62ceSMatthew G. Knepley 27094165533cSJose E. Roman Output Parameters: 2710834e62ceSMatthew G. Knepley + volume - the cell volume 2711cc08537eSMatthew G. Knepley . centroid - the cell centroid 2712cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate 2713834e62ceSMatthew G. Knepley 2714834e62ceSMatthew G. Knepley Level: advanced 2715834e62ceSMatthew G. Knepley 271620f4b53cSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinateSection()`, `DMGetCoordinates()` 2717834e62ceSMatthew G. Knepley @*/ 2718d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2719d71ae5a4SJacob Faibussowitsch { 27200ec8681fSMatthew G. Knepley PetscInt depth, dim; 2721834e62ceSMatthew G. Knepley 2722834e62ceSMatthew G. Knepley PetscFunctionBegin; 27239566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 27249566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 272508401ef6SPierre Jolivet PetscCheck(depth == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated"); 27269566063dSJacob Faibussowitsch PetscCall(DMPlexGetPointDepth(dm, cell, &depth)); 2727011ea5d8SMatthew G. Knepley switch (depth) { 2728d71ae5a4SJacob Faibussowitsch case 0: 2729d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal)); 2730d71ae5a4SJacob Faibussowitsch break; 2731d71ae5a4SJacob Faibussowitsch case 1: 2732d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal)); 2733d71ae5a4SJacob Faibussowitsch break; 2734d71ae5a4SJacob Faibussowitsch case 2: 2735d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal)); 2736d71ae5a4SJacob Faibussowitsch break; 2737d71ae5a4SJacob Faibussowitsch case 3: 2738d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal)); 2739d71ae5a4SJacob Faibussowitsch break; 2740d71ae5a4SJacob Faibussowitsch default: 2741d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT " (depth %" PetscInt_FMT ") for element geometry computation", dim, depth); 2742834e62ceSMatthew G. Knepley } 27433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2744834e62ceSMatthew G. Knepley } 2745113c68e6SMatthew G. Knepley 2746c501906fSMatthew G. Knepley /*@ 2747891a9168SMatthew G. Knepley DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method 2748891a9168SMatthew G. Knepley 2749891a9168SMatthew G. Knepley Input Parameter: 275020f4b53cSBarry Smith . dm - The `DMPLEX` 2751891a9168SMatthew G. Knepley 2752891a9168SMatthew G. Knepley Output Parameters: 275320f4b53cSBarry Smith + cellgeom - A `Vec` of `PetscFVCellGeom` data 275420f4b53cSBarry Smith - facegeom - A `Vec` of `PetscFVFaceGeom` data 2755891a9168SMatthew G. Knepley 2756891a9168SMatthew G. Knepley Level: developer 2757891a9168SMatthew G. Knepley 275820f4b53cSBarry Smith .seealso: `DMPLEX`, `PetscFVFaceGeom`, `PetscFVCellGeom` 2759891a9168SMatthew G. Knepley @*/ 2760d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) 2761d71ae5a4SJacob Faibussowitsch { 2762113c68e6SMatthew G. Knepley DM dmFace, dmCell; 2763113c68e6SMatthew G. Knepley DMLabel ghostLabel; 2764113c68e6SMatthew G. Knepley PetscSection sectionFace, sectionCell; 2765113c68e6SMatthew G. Knepley PetscSection coordSection; 2766113c68e6SMatthew G. Knepley Vec coordinates; 2767113c68e6SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 2768113c68e6SMatthew G. Knepley PetscReal minradius, gminradius; 2769113c68e6SMatthew G. Knepley PetscInt dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f; 2770113c68e6SMatthew G. Knepley 2771113c68e6SMatthew G. Knepley PetscFunctionBegin; 27729566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 27739566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 27749566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 2775113c68e6SMatthew G. Knepley /* Make cell centroids and volumes */ 27769566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmCell)); 27779566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection)); 27789566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmCell, coordinates)); 27799566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionCell)); 27809566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 27819566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 27829566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd)); 27839566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVCellGeom)) / sizeof(PetscScalar)))); 27849566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionCell)); 27859566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmCell, sectionCell)); 27869566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionCell)); 27879566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmCell, cellgeom)); 2788485ad865SMatthew G. Knepley if (cEndInterior < 0) cEndInterior = cEnd; 27899566063dSJacob Faibussowitsch PetscCall(VecGetArray(*cellgeom, &cgeom)); 2790113c68e6SMatthew G. Knepley for (c = cStart; c < cEndInterior; ++c) { 2791113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 2792113c68e6SMatthew G. Knepley 27939566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg)); 27949566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(cg, 1)); 27959566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL)); 2796113c68e6SMatthew G. Knepley } 2797113c68e6SMatthew G. Knepley /* Compute face normals and minimum cell radius */ 27989566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmFace)); 27999566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionFace)); 28009566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 28019566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionFace, fStart, fEnd)); 28029566063dSJacob Faibussowitsch for (f = fStart; f < fEnd; ++f) PetscCall(PetscSectionSetDof(sectionFace, f, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVFaceGeom)) / sizeof(PetscScalar)))); 28039566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionFace)); 28049566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmFace, sectionFace)); 28059566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionFace)); 28069566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmFace, facegeom)); 28079566063dSJacob Faibussowitsch PetscCall(VecGetArray(*facegeom, &fgeom)); 28089566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 2809113c68e6SMatthew G. Knepley minradius = PETSC_MAX_REAL; 2810113c68e6SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 2811113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 2812113c68e6SMatthew G. Knepley PetscReal area; 2813412e9a14SMatthew G. Knepley const PetscInt *cells; 2814412e9a14SMatthew G. Knepley PetscInt ncells, ghost = -1, d, numChildren; 2815113c68e6SMatthew G. Knepley 28169566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 28179566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 28189566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &cells)); 28199566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &ncells)); 2820412e9a14SMatthew G. Knepley /* It is possible to get a face with no support when using partition overlap */ 2821412e9a14SMatthew G. Knepley if (!ncells || ghost >= 0 || numChildren) continue; 28229566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, f, fgeom, &fg)); 28239566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal)); 2824113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] *= area; 2825113c68e6SMatthew G. Knepley /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */ 2826113c68e6SMatthew G. Knepley { 2827113c68e6SMatthew G. Knepley PetscFVCellGeom *cL, *cR; 2828113c68e6SMatthew G. Knepley PetscReal *lcentroid, *rcentroid; 28290453c0cdSMatthew G. Knepley PetscReal l[3], r[3], v[3]; 2830113c68e6SMatthew G. Knepley 28319566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL)); 2832113c68e6SMatthew G. Knepley lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid; 283306348e87SToby Isaac if (ncells > 1) { 28349566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR)); 2835113c68e6SMatthew G. Knepley rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid; 28369371c9d4SSatish Balay } else { 283706348e87SToby Isaac rcentroid = fg->centroid; 283806348e87SToby Isaac } 28399566063dSJacob Faibussowitsch PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l)); 28409566063dSJacob Faibussowitsch PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r)); 28410453c0cdSMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, l, r, v); 2842113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) { 2843113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d]; 2844113c68e6SMatthew G. Knepley } 2845113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) { 284663a3b9bcSJacob 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]); 284763a3b9bcSJacob 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]); 284863a3b9bcSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " could not be fixed", f); 2849113c68e6SMatthew G. Knepley } 2850113c68e6SMatthew G. Knepley if (cells[0] < cEndInterior) { 2851113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v); 2852113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2853113c68e6SMatthew G. Knepley } 285406348e87SToby Isaac if (ncells > 1 && cells[1] < cEndInterior) { 2855113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v); 2856113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2857113c68e6SMatthew G. Knepley } 2858113c68e6SMatthew G. Knepley } 2859113c68e6SMatthew G. Knepley } 28601c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm))); 28619566063dSJacob Faibussowitsch PetscCall(DMPlexSetMinRadius(dm, gminradius)); 2862113c68e6SMatthew G. Knepley /* Compute centroids of ghost cells */ 2863113c68e6SMatthew G. Knepley for (c = cEndInterior; c < cEnd; ++c) { 2864113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 2865113c68e6SMatthew G. Knepley const PetscInt *cone, *support; 2866113c68e6SMatthew G. Knepley PetscInt coneSize, supportSize, s; 2867113c68e6SMatthew G. Knepley 28689566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dmCell, c, &coneSize)); 286963a3b9bcSJacob Faibussowitsch PetscCheck(coneSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %" PetscInt_FMT " has cone size %" PetscInt_FMT " != 1", c, coneSize); 28709566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dmCell, c, &cone)); 28719566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dmCell, cone[0], &supportSize)); 287263a3b9bcSJacob Faibussowitsch PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " has support size %" PetscInt_FMT " != 2", cone[0], supportSize); 28739566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dmCell, cone[0], &support)); 28749566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg)); 2875113c68e6SMatthew G. Knepley for (s = 0; s < 2; ++s) { 2876113c68e6SMatthew G. Knepley /* Reflect ghost centroid across plane of face */ 2877113c68e6SMatthew G. Knepley if (support[s] == c) { 2878640bce14SSatish Balay PetscFVCellGeom *ci; 2879113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 2880113c68e6SMatthew G. Knepley PetscReal c2f[3], a; 2881113c68e6SMatthew G. Knepley 28829566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, support[(s + 1) % 2], cgeom, &ci)); 2883113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */ 2884113c68e6SMatthew G. Knepley a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal) / DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal); 28859566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg)); 2886113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, 2 * a, fg->normal, ci->centroid, cg->centroid); 2887113c68e6SMatthew G. Knepley cg->volume = ci->volume; 2888113c68e6SMatthew G. Knepley } 2889113c68e6SMatthew G. Knepley } 2890113c68e6SMatthew G. Knepley } 28919566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(*facegeom, &fgeom)); 28929566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(*cellgeom, &cgeom)); 28939566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmCell)); 28949566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmFace)); 28953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2896113c68e6SMatthew G. Knepley } 2897113c68e6SMatthew G. Knepley 2898113c68e6SMatthew G. Knepley /*@C 2899113c68e6SMatthew G. Knepley DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face 2900113c68e6SMatthew G. Knepley 290120f4b53cSBarry Smith Not Collective 2902113c68e6SMatthew G. Knepley 29034165533cSJose E. Roman Input Parameter: 290420f4b53cSBarry Smith . dm - the `DMPLEX` 2905113c68e6SMatthew G. Knepley 29064165533cSJose E. Roman Output Parameter: 2907a5b23f4aSJose E. Roman . minradius - the minimum cell radius 2908113c68e6SMatthew G. Knepley 2909113c68e6SMatthew G. Knepley Level: developer 2910113c68e6SMatthew G. Knepley 291120f4b53cSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinates()` 2912113c68e6SMatthew G. Knepley @*/ 2913d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) 2914d71ae5a4SJacob Faibussowitsch { 2915113c68e6SMatthew G. Knepley PetscFunctionBegin; 2916113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2917dadcf809SJacob Faibussowitsch PetscValidRealPointer(minradius, 2); 2918113c68e6SMatthew G. Knepley *minradius = ((DM_Plex *)dm->data)->minradius; 29193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2920113c68e6SMatthew G. Knepley } 2921113c68e6SMatthew G. Knepley 2922113c68e6SMatthew G. Knepley /*@C 2923113c68e6SMatthew G. Knepley DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face 2924113c68e6SMatthew G. Knepley 292520f4b53cSBarry Smith Logically Collective 2926113c68e6SMatthew G. Knepley 29274165533cSJose E. Roman Input Parameters: 292820f4b53cSBarry Smith + dm - the `DMPLEX` 2929a5b23f4aSJose E. Roman - minradius - the minimum cell radius 2930113c68e6SMatthew G. Knepley 2931113c68e6SMatthew G. Knepley Level: developer 2932113c68e6SMatthew G. Knepley 293320f4b53cSBarry Smith .seealso: `DMPLEX`, `DMSetCoordinates()` 2934113c68e6SMatthew G. Knepley @*/ 2935d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) 2936d71ae5a4SJacob Faibussowitsch { 2937113c68e6SMatthew G. Knepley PetscFunctionBegin; 2938113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2939113c68e6SMatthew G. Knepley ((DM_Plex *)dm->data)->minradius = minradius; 29403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2941113c68e6SMatthew G. Knepley } 2942856ac710SMatthew G. Knepley 2943d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 2944d71ae5a4SJacob Faibussowitsch { 2945856ac710SMatthew G. Knepley DMLabel ghostLabel; 2946856ac710SMatthew G. Knepley PetscScalar *dx, *grad, **gref; 2947856ac710SMatthew G. Knepley PetscInt dim, cStart, cEnd, c, cEndInterior, maxNumFaces; 2948856ac710SMatthew G. Knepley 2949856ac710SMatthew G. Knepley PetscFunctionBegin; 29509566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 29519566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 29529566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 2953089217ebSMatthew G. Knepley cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior; 29549566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxNumFaces, NULL)); 29559566063dSJacob Faibussowitsch PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces)); 29569566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 29579566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref)); 2958856ac710SMatthew G. Knepley for (c = cStart; c < cEndInterior; c++) { 2959856ac710SMatthew G. Knepley const PetscInt *faces; 2960856ac710SMatthew G. Knepley PetscInt numFaces, usedFaces, f, d; 2961640bce14SSatish Balay PetscFVCellGeom *cg; 2962856ac710SMatthew G. Knepley PetscBool boundary; 2963856ac710SMatthew G. Knepley PetscInt ghost; 2964856ac710SMatthew G. Knepley 2965a79418b7SMatt McGurn // do not attempt to compute a gradient reconstruction stencil in a ghost cell. It will never be used 2966a79418b7SMatt McGurn PetscCall(DMLabelGetValue(ghostLabel, c, &ghost)); 2967a79418b7SMatt McGurn if (ghost >= 0) continue; 2968a79418b7SMatt McGurn 29699566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg)); 29709566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, c, &numFaces)); 29719566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, c, &faces)); 297263a3b9bcSJacob 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); 2973856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 2974640bce14SSatish Balay PetscFVCellGeom *cg1; 2975856ac710SMatthew G. Knepley PetscFVFaceGeom *fg; 2976856ac710SMatthew G. Knepley const PetscInt *fcells; 2977856ac710SMatthew G. Knepley PetscInt ncell, side; 2978856ac710SMatthew G. Knepley 29799566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost)); 29809566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary)); 2981856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 29829566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, faces[f], &fcells)); 2983856ac710SMatthew G. Knepley side = (c != fcells[0]); /* c is on left=0 or right=1 of face */ 2984856ac710SMatthew G. Knepley ncell = fcells[!side]; /* the neighbor */ 29859566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg)); 29869566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1)); 2987856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) dx[usedFaces * dim + d] = cg1->centroid[d] - cg->centroid[d]; 2988856ac710SMatthew G. Knepley gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */ 2989856ac710SMatthew G. Knepley } 299028b400f6SJacob Faibussowitsch PetscCheck(usedFaces, PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?"); 29919566063dSJacob Faibussowitsch PetscCall(PetscFVComputeGradient(fvm, usedFaces, dx, grad)); 2992856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 29939566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost)); 29949566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary)); 2995856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 2996856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces * dim + d]; 2997856ac710SMatthew G. Knepley ++usedFaces; 2998856ac710SMatthew G. Knepley } 2999856ac710SMatthew G. Knepley } 30009566063dSJacob Faibussowitsch PetscCall(PetscFree3(dx, grad, gref)); 30013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3002856ac710SMatthew G. Knepley } 3003856ac710SMatthew G. Knepley 3004d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 3005d71ae5a4SJacob Faibussowitsch { 3006b81db932SToby Isaac DMLabel ghostLabel; 3007b81db932SToby Isaac PetscScalar *dx, *grad, **gref; 3008b81db932SToby Isaac PetscInt dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0; 3009b81db932SToby Isaac PetscSection neighSec; 3010b81db932SToby Isaac PetscInt(*neighbors)[2]; 3011b81db932SToby Isaac PetscInt *counter; 3012b81db932SToby Isaac 3013b81db932SToby Isaac PetscFunctionBegin; 30149566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 30159566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 30169566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 3017485ad865SMatthew G. Knepley if (cEndInterior < 0) cEndInterior = cEnd; 30189566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &neighSec)); 30199566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(neighSec, cStart, cEndInterior)); 30209566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 30219566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 3022b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 3023b81db932SToby Isaac const PetscInt *fcells; 3024b81db932SToby Isaac PetscBool boundary; 30255bc680faSToby Isaac PetscInt ghost = -1; 3026b81db932SToby Isaac PetscInt numChildren, numCells, c; 3027b81db932SToby Isaac 30289566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 30299566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, f, &boundary)); 30309566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 3031b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 30329566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &numCells)); 303306348e87SToby Isaac if (numCells == 2) { 30349566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &fcells)); 3035b81db932SToby Isaac for (c = 0; c < 2; c++) { 3036b81db932SToby Isaac PetscInt cell = fcells[c]; 3037b81db932SToby Isaac 303848a46eb9SPierre Jolivet if (cell >= cStart && cell < cEndInterior) PetscCall(PetscSectionAddDof(neighSec, cell, 1)); 3039b81db932SToby Isaac } 3040b81db932SToby Isaac } 304106348e87SToby Isaac } 30429566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(neighSec)); 30439566063dSJacob Faibussowitsch PetscCall(PetscSectionGetMaxDof(neighSec, &maxNumFaces)); 30449566063dSJacob Faibussowitsch PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces)); 3045b81db932SToby Isaac nStart = 0; 30469566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(neighSec, &nEnd)); 30479566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((nEnd - nStart), &neighbors)); 30489566063dSJacob Faibussowitsch PetscCall(PetscCalloc1((cEndInterior - cStart), &counter)); 3049b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 3050b81db932SToby Isaac const PetscInt *fcells; 3051b81db932SToby Isaac PetscBool boundary; 30525bc680faSToby Isaac PetscInt ghost = -1; 3053b81db932SToby Isaac PetscInt numChildren, numCells, c; 3054b81db932SToby Isaac 30559566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 30569566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, f, &boundary)); 30579566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 3058b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 30599566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &numCells)); 306006348e87SToby Isaac if (numCells == 2) { 30619566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &fcells)); 3062b81db932SToby Isaac for (c = 0; c < 2; c++) { 3063b81db932SToby Isaac PetscInt cell = fcells[c], off; 3064b81db932SToby Isaac 3065e6885bbbSToby Isaac if (cell >= cStart && cell < cEndInterior) { 30669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(neighSec, cell, &off)); 3067b81db932SToby Isaac off += counter[cell - cStart]++; 3068b81db932SToby Isaac neighbors[off][0] = f; 3069b81db932SToby Isaac neighbors[off][1] = fcells[1 - c]; 3070b81db932SToby Isaac } 3071b81db932SToby Isaac } 3072b81db932SToby Isaac } 307306348e87SToby Isaac } 30749566063dSJacob Faibussowitsch PetscCall(PetscFree(counter)); 30759566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref)); 3076b81db932SToby Isaac for (c = cStart; c < cEndInterior; c++) { 3077317218b9SToby Isaac PetscInt numFaces, f, d, off, ghost = -1; 3078640bce14SSatish Balay PetscFVCellGeom *cg; 3079b81db932SToby Isaac 30809566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg)); 30819566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(neighSec, c, &numFaces)); 30829566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(neighSec, c, &off)); 3083a79418b7SMatt McGurn 3084a79418b7SMatt McGurn // do not attempt to compute a gradient reconstruction stencil in a ghost cell. It will never be used 30859566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, c, &ghost)); 3086a79418b7SMatt McGurn if (ghost >= 0) continue; 3087a79418b7SMatt McGurn 308863a3b9bcSJacob 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); 3089b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 3090640bce14SSatish Balay PetscFVCellGeom *cg1; 3091b81db932SToby Isaac PetscFVFaceGeom *fg; 3092b81db932SToby Isaac const PetscInt *fcells; 3093b81db932SToby Isaac PetscInt ncell, side, nface; 3094b81db932SToby Isaac 3095b81db932SToby Isaac nface = neighbors[off + f][0]; 3096b81db932SToby Isaac ncell = neighbors[off + f][1]; 30979566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, nface, &fcells)); 3098b81db932SToby Isaac side = (c != fcells[0]); 30999566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, nface, fgeom, &fg)); 31009566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1)); 3101b81db932SToby Isaac for (d = 0; d < dim; ++d) dx[f * dim + d] = cg1->centroid[d] - cg->centroid[d]; 3102b81db932SToby Isaac gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */ 3103b81db932SToby Isaac } 31049566063dSJacob Faibussowitsch PetscCall(PetscFVComputeGradient(fvm, numFaces, dx, grad)); 3105b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 3106b81db932SToby Isaac for (d = 0; d < dim; ++d) gref[f][d] = grad[f * dim + d]; 3107b81db932SToby Isaac } 3108b81db932SToby Isaac } 31099566063dSJacob Faibussowitsch PetscCall(PetscFree3(dx, grad, gref)); 31109566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&neighSec)); 31119566063dSJacob Faibussowitsch PetscCall(PetscFree(neighbors)); 31123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3113b81db932SToby Isaac } 3114b81db932SToby Isaac 3115856ac710SMatthew G. Knepley /*@ 3116856ac710SMatthew G. Knepley DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data 3117856ac710SMatthew G. Knepley 311820f4b53cSBarry Smith Collective 3119856ac710SMatthew G. Knepley 31204165533cSJose E. Roman Input Parameters: 312120f4b53cSBarry Smith + dm - The `DMPLEX` 312220f4b53cSBarry Smith . fvm - The `PetscFV` 312320f4b53cSBarry Smith - cellGeometry - The face geometry from `DMPlexComputeCellGeometryFVM()` 3124856ac710SMatthew G. Knepley 31256b867d5aSJose E. Roman Input/Output Parameter: 312620f4b53cSBarry Smith . faceGeometry - The face geometry from `DMPlexComputeFaceGeometryFVM()`; on output 31276b867d5aSJose E. Roman the geometric factors for gradient calculation are inserted 31286b867d5aSJose E. Roman 31296b867d5aSJose E. Roman Output Parameter: 313020f4b53cSBarry Smith . dmGrad - The `DM` describing the layout of gradient data 3131856ac710SMatthew G. Knepley 3132856ac710SMatthew G. Knepley Level: developer 3133856ac710SMatthew G. Knepley 313420f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexGetFaceGeometryFVM()`, `DMPlexGetCellGeometryFVM()` 3135856ac710SMatthew G. Knepley @*/ 3136d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) 3137d71ae5a4SJacob Faibussowitsch { 3138856ac710SMatthew G. Knepley DM dmFace, dmCell; 3139856ac710SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 3140b81db932SToby Isaac PetscSection sectionGrad, parentSection; 3141856ac710SMatthew G. Knepley PetscInt dim, pdim, cStart, cEnd, cEndInterior, c; 3142856ac710SMatthew G. Knepley 3143856ac710SMatthew G. Knepley PetscFunctionBegin; 31449566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 31459566063dSJacob Faibussowitsch PetscCall(PetscFVGetNumComponents(fvm, &pdim)); 31469566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 31479566063dSJacob Faibussowitsch PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL)); 3148856ac710SMatthew G. Knepley /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */ 31499566063dSJacob Faibussowitsch PetscCall(VecGetDM(faceGeometry, &dmFace)); 31509566063dSJacob Faibussowitsch PetscCall(VecGetDM(cellGeometry, &dmCell)); 31519566063dSJacob Faibussowitsch PetscCall(VecGetArray(faceGeometry, &fgeom)); 31529566063dSJacob Faibussowitsch PetscCall(VecGetArray(cellGeometry, &cgeom)); 31539566063dSJacob Faibussowitsch PetscCall(DMPlexGetTree(dm, &parentSection, NULL, NULL, NULL, NULL)); 3154b81db932SToby Isaac if (!parentSection) { 31559566063dSJacob Faibussowitsch PetscCall(BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom)); 3156b5a3613cSMatthew G. Knepley } else { 31579566063dSJacob Faibussowitsch PetscCall(BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom)); 3158b81db932SToby Isaac } 31599566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(faceGeometry, &fgeom)); 31609566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(cellGeometry, &cgeom)); 3161856ac710SMatthew G. Knepley /* Create storage for gradients */ 31629566063dSJacob Faibussowitsch PetscCall(DMClone(dm, dmGrad)); 31639566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionGrad)); 31649566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionGrad, cStart, cEnd)); 31659566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionGrad, c, pdim * dim)); 31669566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionGrad)); 31679566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(*dmGrad, sectionGrad)); 31689566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionGrad)); 31693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3170856ac710SMatthew G. Knepley } 3171b27d5b9eSToby Isaac 3172c501906fSMatthew G. Knepley /*@ 3173c501906fSMatthew G. Knepley DMPlexGetDataFVM - Retrieve precomputed cell geometry 3174c501906fSMatthew G. Knepley 317520f4b53cSBarry Smith Collective 3176c501906fSMatthew G. Knepley 31774165533cSJose E. Roman Input Parameters: 317820f4b53cSBarry Smith + dm - The `DM` 317920f4b53cSBarry Smith - fv - The `PetscFV` 3180c501906fSMatthew G. Knepley 3181c501906fSMatthew G. Knepley Output Parameters: 3182c501906fSMatthew G. Knepley + cellGeometry - The cell geometry 3183c501906fSMatthew G. Knepley . faceGeometry - The face geometry 31846b867d5aSJose E. Roman - gradDM - The gradient matrices 3185c501906fSMatthew G. Knepley 3186c501906fSMatthew G. Knepley Level: developer 3187c501906fSMatthew G. Knepley 318820f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeGeometryFVM()` 3189c501906fSMatthew G. Knepley @*/ 3190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM) 3191d71ae5a4SJacob Faibussowitsch { 3192b27d5b9eSToby Isaac PetscObject cellgeomobj, facegeomobj; 3193b27d5b9eSToby Isaac 3194b27d5b9eSToby Isaac PetscFunctionBegin; 31959566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj)); 3196b27d5b9eSToby Isaac if (!cellgeomobj) { 3197b27d5b9eSToby Isaac Vec cellgeomInt, facegeomInt; 3198b27d5b9eSToby Isaac 31999566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt)); 32009566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_cellgeom_fvm", (PetscObject)cellgeomInt)); 32019566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_facegeom_fvm", (PetscObject)facegeomInt)); 32029566063dSJacob Faibussowitsch PetscCall(VecDestroy(&cellgeomInt)); 32039566063dSJacob Faibussowitsch PetscCall(VecDestroy(&facegeomInt)); 32049566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj)); 3205b27d5b9eSToby Isaac } 32069566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_facegeom_fvm", &facegeomobj)); 3207b27d5b9eSToby Isaac if (cellgeom) *cellgeom = (Vec)cellgeomobj; 3208b27d5b9eSToby Isaac if (facegeom) *facegeom = (Vec)facegeomobj; 3209b27d5b9eSToby Isaac if (gradDM) { 3210b27d5b9eSToby Isaac PetscObject gradobj; 3211b27d5b9eSToby Isaac PetscBool computeGradients; 3212b27d5b9eSToby Isaac 32139566063dSJacob Faibussowitsch PetscCall(PetscFVGetComputeGradients(fv, &computeGradients)); 3214b27d5b9eSToby Isaac if (!computeGradients) { 3215b27d5b9eSToby Isaac *gradDM = NULL; 32163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3217b27d5b9eSToby Isaac } 32189566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj)); 3219b27d5b9eSToby Isaac if (!gradobj) { 3220b27d5b9eSToby Isaac DM dmGradInt; 3221b27d5b9eSToby Isaac 32229566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGradientFVM(dm, fv, (Vec)facegeomobj, (Vec)cellgeomobj, &dmGradInt)); 32239566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt)); 32249566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmGradInt)); 32259566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj)); 3226b27d5b9eSToby Isaac } 3227b27d5b9eSToby Isaac *gradDM = (DM)gradobj; 3228b27d5b9eSToby Isaac } 32293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3230b27d5b9eSToby Isaac } 3231d6143a4eSToby Isaac 3232d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess) 3233d71ae5a4SJacob Faibussowitsch { 32349d150b73SToby Isaac PetscInt l, m; 32359d150b73SToby Isaac 3236cd345991SToby Isaac PetscFunctionBeginHot; 32379d150b73SToby Isaac if (dimC == dimR && dimR <= 3) { 32389d150b73SToby Isaac /* invert Jacobian, multiply */ 32399d150b73SToby Isaac PetscScalar det, idet; 32409d150b73SToby Isaac 32419d150b73SToby Isaac switch (dimR) { 3242d71ae5a4SJacob Faibussowitsch case 1: 3243d71ae5a4SJacob Faibussowitsch invJ[0] = 1. / J[0]; 3244d71ae5a4SJacob Faibussowitsch break; 32459d150b73SToby Isaac case 2: 32469d150b73SToby Isaac det = J[0] * J[3] - J[1] * J[2]; 32479d150b73SToby Isaac idet = 1. / det; 32489d150b73SToby Isaac invJ[0] = J[3] * idet; 32499d150b73SToby Isaac invJ[1] = -J[1] * idet; 32509d150b73SToby Isaac invJ[2] = -J[2] * idet; 32519d150b73SToby Isaac invJ[3] = J[0] * idet; 32529d150b73SToby Isaac break; 32539371c9d4SSatish Balay case 3: { 32549d150b73SToby Isaac invJ[0] = J[4] * J[8] - J[5] * J[7]; 32559d150b73SToby Isaac invJ[1] = J[2] * J[7] - J[1] * J[8]; 32569d150b73SToby Isaac invJ[2] = J[1] * J[5] - J[2] * J[4]; 32579d150b73SToby Isaac det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6]; 32589d150b73SToby Isaac idet = 1. / det; 32599d150b73SToby Isaac invJ[0] *= idet; 32609d150b73SToby Isaac invJ[1] *= idet; 32619d150b73SToby Isaac invJ[2] *= idet; 32629d150b73SToby Isaac invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]); 32639d150b73SToby Isaac invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]); 32649d150b73SToby Isaac invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]); 32659d150b73SToby Isaac invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]); 32669d150b73SToby Isaac invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]); 32679d150b73SToby Isaac invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]); 32689371c9d4SSatish Balay } break; 32699d150b73SToby Isaac } 32709d150b73SToby Isaac for (l = 0; l < dimR; l++) { 3271ad540459SPierre Jolivet for (m = 0; m < dimC; m++) guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m]; 32729d150b73SToby Isaac } 32739d150b73SToby Isaac } else { 32749d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX) 32759d150b73SToby Isaac char transpose = 'C'; 32769d150b73SToby Isaac #else 32779d150b73SToby Isaac char transpose = 'T'; 32789d150b73SToby Isaac #endif 32799d150b73SToby Isaac PetscBLASInt m = dimR; 32809d150b73SToby Isaac PetscBLASInt n = dimC; 32819d150b73SToby Isaac PetscBLASInt one = 1; 32829d150b73SToby Isaac PetscBLASInt worksize = dimR * dimC, info; 32839d150b73SToby Isaac 3284ad540459SPierre Jolivet for (l = 0; l < dimC; l++) invJ[l] = resNeg[l]; 32859d150b73SToby Isaac 3286792fecdfSBarry Smith PetscCallBLAS("LAPACKgels", LAPACKgels_(&transpose, &m, &n, &one, J, &m, invJ, &n, work, &worksize, &info)); 328708401ef6SPierre Jolivet PetscCheck(info == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Bad argument to GELS"); 32889d150b73SToby Isaac 3289ad540459SPierre Jolivet for (l = 0; l < dimR; l++) guess[l] += PetscRealPart(invJ[l]); 32909d150b73SToby Isaac } 32913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32929d150b73SToby Isaac } 32939d150b73SToby Isaac 3294d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 3295d71ae5a4SJacob Faibussowitsch { 3296c0cbe899SToby Isaac PetscInt coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR); 32979d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 32989d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg; 32999d150b73SToby Isaac PetscScalar *J, *invJ, *work; 33009d150b73SToby Isaac 33019d150b73SToby Isaac PetscFunctionBegin; 33029d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 33039566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 33041dca8a05SBarry Smith PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize); 33059566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData)); 33069566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J)); 33079d150b73SToby Isaac cellCoords = &cellData[0]; 33089d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 33099d150b73SToby Isaac extJ = &cellData[2 * coordSize]; 33109d150b73SToby Isaac resNeg = &cellData[2 * coordSize + dimR]; 33119d150b73SToby Isaac invJ = &J[dimR * dimC]; 33129d150b73SToby Isaac work = &J[2 * dimR * dimC]; 33139d150b73SToby Isaac if (dimR == 2) { 33149d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 33159d150b73SToby Isaac 33169d150b73SToby Isaac for (i = 0; i < 4; i++) { 33179d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 33189d150b73SToby Isaac 3319ad540459SPierre Jolivet for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 33209d150b73SToby Isaac } 33219d150b73SToby Isaac } else if (dimR == 3) { 33229d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 33239d150b73SToby Isaac 33249d150b73SToby Isaac for (i = 0; i < 8; i++) { 33259d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 33269d150b73SToby Isaac 3327ad540459SPierre Jolivet for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 33289d150b73SToby Isaac } 33299d150b73SToby Isaac } else { 3330ad540459SPierre Jolivet for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]); 33319d150b73SToby Isaac } 33329d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 33339d150b73SToby Isaac for (i = 0; i < dimR; i++) { 33349d150b73SToby Isaac PetscReal *swap; 33359d150b73SToby Isaac 33369d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 33379d150b73SToby Isaac for (k = 0; k < dimC; k++) { 33389d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 33399d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 33409d150b73SToby Isaac } 33419d150b73SToby Isaac } 33429d150b73SToby Isaac 33439d150b73SToby Isaac if (i < dimR - 1) { 33449d150b73SToby Isaac swap = cellCoeffs; 33459d150b73SToby Isaac cellCoeffs = cellCoords; 33469d150b73SToby Isaac cellCoords = swap; 33479d150b73SToby Isaac } 33489d150b73SToby Isaac } 33499566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(refCoords, numPoints * dimR)); 33509d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 33519d150b73SToby Isaac for (i = 0; i < maxIts; i++) { 33529d150b73SToby Isaac PetscReal *guess = &refCoords[dimR * j]; 33539d150b73SToby Isaac 33549d150b73SToby Isaac /* compute -residual and Jacobian */ 3355ad540459SPierre Jolivet for (k = 0; k < dimC; k++) resNeg[k] = realCoords[dimC * j + k]; 3356ad540459SPierre Jolivet for (k = 0; k < dimC * dimR; k++) J[k] = 0.; 33579d150b73SToby Isaac for (k = 0; k < numV; k++) { 33589d150b73SToby Isaac PetscReal extCoord = 1.; 33599d150b73SToby Isaac for (l = 0; l < dimR; l++) { 33609d150b73SToby Isaac PetscReal coord = guess[l]; 33619d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 33629d150b73SToby Isaac 33639d150b73SToby Isaac extCoord *= dep * coord + !dep; 33649d150b73SToby Isaac extJ[l] = dep; 33659d150b73SToby Isaac 33669d150b73SToby Isaac for (m = 0; m < dimR; m++) { 33679d150b73SToby Isaac PetscReal coord = guess[m]; 33689d150b73SToby Isaac PetscInt dep = ((k & (1 << m)) >> m) && (m != l); 33699d150b73SToby Isaac PetscReal mult = dep * coord + !dep; 33709d150b73SToby Isaac 33719d150b73SToby Isaac extJ[l] *= mult; 33729d150b73SToby Isaac } 33739d150b73SToby Isaac } 33749d150b73SToby Isaac for (l = 0; l < dimC; l++) { 33759d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 33769d150b73SToby Isaac 33779d150b73SToby Isaac resNeg[l] -= coeff * extCoord; 3378ad540459SPierre Jolivet for (m = 0; m < dimR; m++) J[dimR * l + m] += coeff * extJ[m]; 33799d150b73SToby Isaac } 33809d150b73SToby Isaac } 338176bd3646SJed Brown if (0 && PetscDefined(USE_DEBUG)) { 33820611203eSToby Isaac PetscReal maxAbs = 0.; 33830611203eSToby Isaac 3384ad540459SPierre Jolivet for (l = 0; l < dimC; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l])); 338563a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs)); 33860611203eSToby Isaac } 33879d150b73SToby Isaac 33889566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(dimC, dimR, J, invJ, work, resNeg, guess)); 33899d150b73SToby Isaac } 33909d150b73SToby Isaac } 33919566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J)); 33929566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData)); 33939566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 33943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 33959d150b73SToby Isaac } 33969d150b73SToby Isaac 3397d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 3398d71ae5a4SJacob Faibussowitsch { 33999d150b73SToby Isaac PetscInt coordSize, i, j, k, l, numV = (1 << dimR); 34009d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 34019d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs; 34029d150b73SToby Isaac 34039d150b73SToby Isaac PetscFunctionBegin; 34049d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 34059566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 34061dca8a05SBarry Smith PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize); 34079566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData)); 34089d150b73SToby Isaac cellCoords = &cellData[0]; 34099d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 34109d150b73SToby Isaac if (dimR == 2) { 34119d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 34129d150b73SToby Isaac 34139d150b73SToby Isaac for (i = 0; i < 4; i++) { 34149d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 34159d150b73SToby Isaac 3416ad540459SPierre Jolivet for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 34179d150b73SToby Isaac } 34189d150b73SToby Isaac } else if (dimR == 3) { 34199d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 34209d150b73SToby Isaac 34219d150b73SToby Isaac for (i = 0; i < 8; i++) { 34229d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 34239d150b73SToby Isaac 3424ad540459SPierre Jolivet for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 34259d150b73SToby Isaac } 34269d150b73SToby Isaac } else { 3427ad540459SPierre Jolivet for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]); 34289d150b73SToby Isaac } 34299d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 34309d150b73SToby Isaac for (i = 0; i < dimR; i++) { 34319d150b73SToby Isaac PetscReal *swap; 34329d150b73SToby Isaac 34339d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 34349d150b73SToby Isaac for (k = 0; k < dimC; k++) { 34359d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 34369d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 34379d150b73SToby Isaac } 34389d150b73SToby Isaac } 34399d150b73SToby Isaac 34409d150b73SToby Isaac if (i < dimR - 1) { 34419d150b73SToby Isaac swap = cellCoeffs; 34429d150b73SToby Isaac cellCoeffs = cellCoords; 34439d150b73SToby Isaac cellCoords = swap; 34449d150b73SToby Isaac } 34459d150b73SToby Isaac } 34469566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(realCoords, numPoints * dimC)); 34479d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 34489d150b73SToby Isaac const PetscReal *guess = &refCoords[dimR * j]; 34499d150b73SToby Isaac PetscReal *mapped = &realCoords[dimC * j]; 34509d150b73SToby Isaac 34519d150b73SToby Isaac for (k = 0; k < numV; k++) { 34529d150b73SToby Isaac PetscReal extCoord = 1.; 34539d150b73SToby Isaac for (l = 0; l < dimR; l++) { 34549d150b73SToby Isaac PetscReal coord = guess[l]; 34559d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 34569d150b73SToby Isaac 34579d150b73SToby Isaac extCoord *= dep * coord + !dep; 34589d150b73SToby Isaac } 34599d150b73SToby Isaac for (l = 0; l < dimC; l++) { 34609d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 34619d150b73SToby Isaac 34629d150b73SToby Isaac mapped[l] += coeff * extCoord; 34639d150b73SToby Isaac } 34649d150b73SToby Isaac } 34659d150b73SToby Isaac } 34669566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData)); 34679566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 34683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 34699d150b73SToby Isaac } 34709d150b73SToby Isaac 34719c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */ 3472d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR) 3473d71ae5a4SJacob Faibussowitsch { 34749c3cf19fSMatthew G. Knepley PetscInt numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize; 3475c6e120d1SToby Isaac PetscScalar *nodes = NULL; 3476c6e120d1SToby Isaac PetscReal *invV, *modes; 3477c6e120d1SToby Isaac PetscReal *B, *D, *resNeg; 3478c6e120d1SToby Isaac PetscScalar *J, *invJ, *work; 34799d150b73SToby Isaac 34809d150b73SToby Isaac PetscFunctionBegin; 34819566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 34829566063dSJacob Faibussowitsch PetscCall(PetscFEGetNumComponents(fe, &numComp)); 348363a3b9bcSJacob 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); 34849566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 34859d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 34869566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes)); 34879d150b73SToby Isaac invV = fe->invV; 3488012b7cc6SMatthew G. Knepley for (i = 0; i < pdim; ++i) { 3489012b7cc6SMatthew G. Knepley modes[i] = 0.; 3490ad540459SPierre Jolivet for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); 34919d150b73SToby Isaac } 34929566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B)); 34939c3cf19fSMatthew G. Knepley D = &B[pdim * Nc]; 34949c3cf19fSMatthew G. Knepley resNeg = &D[pdim * Nc * dimR]; 34959566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J)); 34969c3cf19fSMatthew G. Knepley invJ = &J[Nc * dimR]; 34979c3cf19fSMatthew G. Knepley work = &invJ[Nc * dimR]; 3498ad540459SPierre Jolivet for (i = 0; i < numPoints * dimR; i++) refCoords[i] = 0.; 34999d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 35009b1f03cbSToby 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 */ 35019d150b73SToby Isaac PetscReal *guess = &refCoords[j * dimR]; 35029566063dSJacob Faibussowitsch PetscCall(PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL)); 3503ad540459SPierre Jolivet for (k = 0; k < Nc; k++) resNeg[k] = realCoords[j * Nc + k]; 3504ad540459SPierre Jolivet for (k = 0; k < Nc * dimR; k++) J[k] = 0.; 35059c3cf19fSMatthew G. Knepley for (k = 0; k < pdim; k++) { 35069c3cf19fSMatthew G. Knepley for (l = 0; l < Nc; l++) { 3507012b7cc6SMatthew G. Knepley resNeg[l] -= modes[k] * B[k * Nc + l]; 3508ad540459SPierre Jolivet for (m = 0; m < dimR; m++) J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m]; 35099d150b73SToby Isaac } 35109d150b73SToby Isaac } 351176bd3646SJed Brown if (0 && PetscDefined(USE_DEBUG)) { 35120611203eSToby Isaac PetscReal maxAbs = 0.; 35130611203eSToby Isaac 3514ad540459SPierre Jolivet for (l = 0; l < Nc; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l])); 351563a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs)); 35160611203eSToby Isaac } 35179566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(Nc, dimR, J, invJ, work, resNeg, guess)); 35189d150b73SToby Isaac } 35199d150b73SToby Isaac } 35209566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J)); 35219566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B)); 35229566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes)); 35239566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 35243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 35259d150b73SToby Isaac } 35269d150b73SToby Isaac 35279c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */ 3528d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR) 3529d71ae5a4SJacob Faibussowitsch { 35309c3cf19fSMatthew G. Knepley PetscInt numComp, pdim, i, j, k, l, coordSize; 3531c6e120d1SToby Isaac PetscScalar *nodes = NULL; 3532c6e120d1SToby Isaac PetscReal *invV, *modes; 35339d150b73SToby Isaac PetscReal *B; 35349d150b73SToby Isaac 35359d150b73SToby Isaac PetscFunctionBegin; 35369566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 35379566063dSJacob Faibussowitsch PetscCall(PetscFEGetNumComponents(fe, &numComp)); 353863a3b9bcSJacob 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); 35399566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 35409d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 35419566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes)); 35429d150b73SToby Isaac invV = fe->invV; 3543012b7cc6SMatthew G. Knepley for (i = 0; i < pdim; ++i) { 3544012b7cc6SMatthew G. Knepley modes[i] = 0.; 3545ad540459SPierre Jolivet for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); 35469d150b73SToby Isaac } 35479566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B)); 35489566063dSJacob Faibussowitsch PetscCall(PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL)); 3549ad540459SPierre Jolivet for (i = 0; i < numPoints * Nc; i++) realCoords[i] = 0.; 35509d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 35519c3cf19fSMatthew G. Knepley PetscReal *mapped = &realCoords[j * Nc]; 35529d150b73SToby Isaac 35539c3cf19fSMatthew G. Knepley for (k = 0; k < pdim; k++) { 3554ad540459SPierre Jolivet for (l = 0; l < Nc; l++) mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l]; 35559d150b73SToby Isaac } 35569d150b73SToby Isaac } 35579566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B)); 35589566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes)); 35599566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 35603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 35619d150b73SToby Isaac } 35629d150b73SToby Isaac 3563d6143a4eSToby Isaac /*@ 3564d6143a4eSToby Isaac DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element 3565d6143a4eSToby Isaac map. This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not 3566d6143a4eSToby Isaac extend uniquely outside the reference cell (e.g, most non-affine maps) 3567d6143a4eSToby Isaac 356820f4b53cSBarry Smith Not Collective 3569d6143a4eSToby Isaac 3570d6143a4eSToby Isaac Input Parameters: 357120f4b53cSBarry Smith + dm - The mesh, with coordinate maps defined either by a `PetscDS` for the coordinate `DM` (see `DMGetCoordinateDM()`) or 3572d6143a4eSToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 3573d6143a4eSToby Isaac as a multilinear map for tensor-product elements 3574d6143a4eSToby Isaac . cell - the cell whose map is used. 3575d6143a4eSToby Isaac . numPoints - the number of points to locate 357620f4b53cSBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see `DMGetCoordinateDim()`) 3577d6143a4eSToby Isaac 35782fe279fdSBarry Smith Output Parameter: 357920f4b53cSBarry Smith . refCoords - (`numPoints` x `dimension`) array of reference coordinates (see `DMGetDimension()`) 35801b266c99SBarry Smith 35811b266c99SBarry Smith Level: intermediate 358273c9229bSMatthew Knepley 358320f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexReferenceToCoordinates()` 3584d6143a4eSToby Isaac @*/ 3585d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[]) 3586d71ae5a4SJacob Faibussowitsch { 3587485ad865SMatthew G. Knepley PetscInt dimC, dimR, depth, cStart, cEnd, i; 35889d150b73SToby Isaac DM coordDM = NULL; 35899d150b73SToby Isaac Vec coords; 35909d150b73SToby Isaac PetscFE fe = NULL; 35919d150b73SToby Isaac 3592d6143a4eSToby Isaac PetscFunctionBegin; 35939d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 35949566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dimR)); 35959566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dimC)); 35963ba16761SJacob Faibussowitsch if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(PETSC_SUCCESS); 35979566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 35989566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coords)); 35999566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &coordDM)); 36009d150b73SToby Isaac if (coordDM) { 36019d150b73SToby Isaac PetscInt coordFields; 36029d150b73SToby Isaac 36039566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(coordDM, &coordFields)); 36049d150b73SToby Isaac if (coordFields) { 36059d150b73SToby Isaac PetscClassId id; 36069d150b73SToby Isaac PetscObject disc; 36079d150b73SToby Isaac 36089566063dSJacob Faibussowitsch PetscCall(DMGetField(coordDM, 0, NULL, &disc)); 36099566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 3610ad540459SPierre Jolivet if (id == PETSCFE_CLASSID) fe = (PetscFE)disc; 36119d150b73SToby Isaac } 36129d150b73SToby Isaac } 36139566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 36141dca8a05SBarry 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); 36159d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 36169d150b73SToby Isaac PetscInt coneSize; 36179d150b73SToby Isaac PetscBool isSimplex, isTensor; 36189d150b73SToby Isaac 36199566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 36209d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 36219d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 36229d150b73SToby Isaac if (isSimplex) { 36239d150b73SToby Isaac PetscReal detJ, *v0, *J, *invJ; 36249d150b73SToby Isaac 36259566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 36269d150b73SToby Isaac J = &v0[dimC]; 36279d150b73SToby Isaac invJ = &J[dimC * dimC]; 36289566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ)); 36299d150b73SToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */ 3630c330f8ffSToby Isaac const PetscReal x0[3] = {-1., -1., -1.}; 3631c330f8ffSToby Isaac 3632c330f8ffSToby Isaac CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]); 36339d150b73SToby Isaac } 36349566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 36359d150b73SToby Isaac } else if (isTensor) { 36369566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR)); 363763a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize); 36389d150b73SToby Isaac } else { 36399566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR)); 36409d150b73SToby Isaac } 36413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 36429d150b73SToby Isaac } 36439d150b73SToby Isaac 36449d150b73SToby Isaac /*@ 36459d150b73SToby Isaac DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map. 36469d150b73SToby Isaac 364720f4b53cSBarry Smith Not Collective 36489d150b73SToby Isaac 36499d150b73SToby Isaac Input Parameters: 36502fe279fdSBarry Smith + dm - The mesh, with coordinate maps defined either by a PetscDS for the coordinate `DM` (see `DMGetCoordinateDM()`) or 36519d150b73SToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 36529d150b73SToby Isaac as a multilinear map for tensor-product elements 36539d150b73SToby Isaac . cell - the cell whose map is used. 36549d150b73SToby Isaac . numPoints - the number of points to locate 36552fe279fdSBarry Smith - refCoords - (numPoints x dimension) array of reference coordinates (see `DMGetDimension()`) 36569d150b73SToby Isaac 36572fe279fdSBarry Smith Output Parameter: 36582fe279fdSBarry Smith . realCoords - (numPoints x coordinate dimension) array of coordinates (see `DMGetCoordinateDim()`) 36591b266c99SBarry Smith 36601b266c99SBarry Smith Level: intermediate 366173c9229bSMatthew Knepley 36622fe279fdSBarry Smith .seealso: `DMPLEX`, `DMPlexCoordinatesToReference()` 36639d150b73SToby Isaac @*/ 3664d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[]) 3665d71ae5a4SJacob Faibussowitsch { 3666485ad865SMatthew G. Knepley PetscInt dimC, dimR, depth, cStart, cEnd, i; 36679d150b73SToby Isaac DM coordDM = NULL; 36689d150b73SToby Isaac Vec coords; 36699d150b73SToby Isaac PetscFE fe = NULL; 36709d150b73SToby Isaac 36719d150b73SToby Isaac PetscFunctionBegin; 36729d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 36739566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dimR)); 36749566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dimC)); 36753ba16761SJacob Faibussowitsch if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(PETSC_SUCCESS); 36769566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 36779566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coords)); 36789566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &coordDM)); 36799d150b73SToby Isaac if (coordDM) { 36809d150b73SToby Isaac PetscInt coordFields; 36819d150b73SToby Isaac 36829566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(coordDM, &coordFields)); 36839d150b73SToby Isaac if (coordFields) { 36849d150b73SToby Isaac PetscClassId id; 36859d150b73SToby Isaac PetscObject disc; 36869d150b73SToby Isaac 36879566063dSJacob Faibussowitsch PetscCall(DMGetField(coordDM, 0, NULL, &disc)); 36889566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 3689ad540459SPierre Jolivet if (id == PETSCFE_CLASSID) fe = (PetscFE)disc; 36909d150b73SToby Isaac } 36919d150b73SToby Isaac } 36929566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 36931dca8a05SBarry 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); 36949d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 36959d150b73SToby Isaac PetscInt coneSize; 36969d150b73SToby Isaac PetscBool isSimplex, isTensor; 36979d150b73SToby Isaac 36989566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 36999d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 37009d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 37019d150b73SToby Isaac if (isSimplex) { 37029d150b73SToby Isaac PetscReal detJ, *v0, *J; 37039d150b73SToby Isaac 37049566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 37059d150b73SToby Isaac J = &v0[dimC]; 37069566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ)); 3707c330f8ffSToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */ 3708c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 3709c330f8ffSToby Isaac 3710c330f8ffSToby Isaac CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]); 37119d150b73SToby Isaac } 37129566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 37139d150b73SToby Isaac } else if (isTensor) { 37149566063dSJacob Faibussowitsch PetscCall(DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR)); 371563a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize); 37169d150b73SToby Isaac } else { 37179566063dSJacob Faibussowitsch PetscCall(DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR)); 37189d150b73SToby Isaac } 37193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3720d6143a4eSToby Isaac } 37210139fca9SMatthew G. Knepley 37220139fca9SMatthew G. Knepley /*@C 37232fe279fdSBarry Smith DMPlexRemapGeometry - This function maps the original `DM` coordinates to new coordinates. 37240139fca9SMatthew G. Knepley 372520f4b53cSBarry Smith Not Collective 37260139fca9SMatthew G. Knepley 37270139fca9SMatthew G. Knepley Input Parameters: 37282fe279fdSBarry Smith + dm - The `DM` 37290139fca9SMatthew G. Knepley . time - The time 37300139fca9SMatthew G. Knepley - func - The function transforming current coordinates to new coordaintes 37310139fca9SMatthew G. Knepley 373220f4b53cSBarry Smith Calling sequence of `func`: 373320f4b53cSBarry Smith .vb 373420f4b53cSBarry Smith void func(PetscInt dim, PetscInt Nf, PetscInt NfAux, 373520f4b53cSBarry Smith const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 373620f4b53cSBarry Smith const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 373720f4b53cSBarry Smith PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]); 373820f4b53cSBarry Smith .ve 37390139fca9SMatthew G. Knepley + dim - The spatial dimension 37400139fca9SMatthew G. Knepley . Nf - The number of input fields (here 1) 37410139fca9SMatthew G. Knepley . NfAux - The number of input auxiliary fields 37420139fca9SMatthew G. Knepley . uOff - The offset of the coordinates in u[] (here 0) 37430139fca9SMatthew G. Knepley . uOff_x - The offset of the coordinates in u_x[] (here 0) 37440139fca9SMatthew G. Knepley . u - The coordinate values at this point in space 374520f4b53cSBarry Smith . u_t - The coordinate time derivative at this point in space (here `NULL`) 37460139fca9SMatthew G. Knepley . u_x - The coordinate derivatives at this point in space 37470139fca9SMatthew G. Knepley . aOff - The offset of each auxiliary field in u[] 37480139fca9SMatthew G. Knepley . aOff_x - The offset of each auxiliary field in u_x[] 37490139fca9SMatthew G. Knepley . a - The auxiliary field values at this point in space 375020f4b53cSBarry Smith . a_t - The auxiliary field time derivative at this point in space (or `NULL`) 37510139fca9SMatthew G. Knepley . a_x - The auxiliary field derivatives at this point in space 37520139fca9SMatthew G. Knepley . t - The current time 37530139fca9SMatthew G. Knepley . x - The coordinates of this point (here not used) 37540139fca9SMatthew G. Knepley . numConstants - The number of constants 37550139fca9SMatthew G. Knepley . constants - The value of each constant 37560139fca9SMatthew G. Knepley - f - The new coordinates at this point in space 37570139fca9SMatthew G. Knepley 37580139fca9SMatthew G. Knepley Level: intermediate 37590139fca9SMatthew G. Knepley 37602fe279fdSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMProjectFieldLocal()`, `DMProjectFieldLabelLocal()` 37610139fca9SMatthew G. Knepley @*/ 3762d71ae5a4SJacob Faibussowitsch 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[])) 3763d71ae5a4SJacob Faibussowitsch { 37640139fca9SMatthew G. Knepley DM cdm; 37658bf1a49fSMatthew G. Knepley DMField cf; 37660139fca9SMatthew G. Knepley Vec lCoords, tmpCoords; 37670139fca9SMatthew G. Knepley 37680139fca9SMatthew G. Knepley PetscFunctionBegin; 37699566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 37709566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &lCoords)); 37719566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(cdm, &tmpCoords)); 37729566063dSJacob Faibussowitsch PetscCall(VecCopy(lCoords, tmpCoords)); 37738bf1a49fSMatthew G. Knepley /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */ 37749566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateField(dm, &cf)); 37756858538eSMatthew G. Knepley cdm->coordinates[0].field = cf; 37769566063dSJacob Faibussowitsch PetscCall(DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords)); 37776858538eSMatthew G. Knepley cdm->coordinates[0].field = NULL; 37789566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(cdm, &tmpCoords)); 37799566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, lCoords)); 37803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 37810139fca9SMatthew G. Knepley } 37820139fca9SMatthew G. Knepley 37830139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z, 37840139fca9SMatthew G. Knepley / 1 0 m_0 \ 37850139fca9SMatthew G. Knepley | 0 1 m_1 | 37860139fca9SMatthew G. Knepley \ 0 0 1 / 37870139fca9SMatthew G. Knepley */ 3788d71ae5a4SJacob Faibussowitsch 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[]) 3789d71ae5a4SJacob Faibussowitsch { 37900139fca9SMatthew G. Knepley const PetscInt Nc = uOff[1] - uOff[0]; 3791c1f1bd54SMatthew G. Knepley const PetscInt ax = (PetscInt)PetscRealPart(constants[0]); 37920139fca9SMatthew G. Knepley PetscInt c; 37930139fca9SMatthew G. Knepley 3794ad540459SPierre Jolivet for (c = 0; c < Nc; ++c) coords[c] = u[c] + constants[c + 1] * u[ax]; 37950139fca9SMatthew G. Knepley } 37960139fca9SMatthew G. Knepley 37970139fca9SMatthew G. Knepley /*@C 37980139fca9SMatthew G. Knepley DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates. 37990139fca9SMatthew G. Knepley 380020f4b53cSBarry Smith Not Collective 38010139fca9SMatthew G. Knepley 38020139fca9SMatthew G. Knepley Input Parameters: 380320f4b53cSBarry Smith + dm - The `DMPLEX` 38043ee9839eSMatthew G. Knepley . direction - The shear coordinate direction, e.g. 0 is the x-axis 38050139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction 38060139fca9SMatthew G. Knepley 38070139fca9SMatthew G. Knepley Level: intermediate 38080139fca9SMatthew G. Knepley 380920f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexRemapGeometry()` 38100139fca9SMatthew G. Knepley @*/ 3811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[]) 3812d71ae5a4SJacob Faibussowitsch { 38130139fca9SMatthew G. Knepley DM cdm; 38140139fca9SMatthew G. Knepley PetscDS cds; 38150139fca9SMatthew G. Knepley PetscObject obj; 38160139fca9SMatthew G. Knepley PetscClassId id; 38170139fca9SMatthew G. Knepley PetscScalar *moduli; 38183ee9839eSMatthew G. Knepley const PetscInt dir = (PetscInt)direction; 38190139fca9SMatthew G. Knepley PetscInt dE, d, e; 38200139fca9SMatthew G. Knepley 38210139fca9SMatthew G. Knepley PetscFunctionBegin; 38229566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 38239566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dE)); 38249566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(dE + 1, &moduli)); 38250139fca9SMatthew G. Knepley moduli[0] = dir; 3826cdaaecf7SMatthew G. Knepley for (d = 0, e = 0; d < dE; ++d) moduli[d + 1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0); 38279566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &cds)); 38289566063dSJacob Faibussowitsch PetscCall(PetscDSGetDiscretization(cds, 0, &obj)); 38299566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(obj, &id)); 38300139fca9SMatthew G. Knepley if (id != PETSCFE_CLASSID) { 38310139fca9SMatthew G. Knepley Vec lCoords; 38320139fca9SMatthew G. Knepley PetscSection cSection; 38330139fca9SMatthew G. Knepley PetscScalar *coords; 38340139fca9SMatthew G. Knepley PetscInt vStart, vEnd, v; 38350139fca9SMatthew G. Knepley 38369566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 38379566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &cSection)); 38389566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &lCoords)); 38399566063dSJacob Faibussowitsch PetscCall(VecGetArray(lCoords, &coords)); 38400139fca9SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 38410139fca9SMatthew G. Knepley PetscReal ds; 38420139fca9SMatthew G. Knepley PetscInt off, c; 38430139fca9SMatthew G. Knepley 38449566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cSection, v, &off)); 38450139fca9SMatthew G. Knepley ds = PetscRealPart(coords[off + dir]); 38460139fca9SMatthew G. Knepley for (c = 0; c < dE; ++c) coords[off + c] += moduli[c] * ds; 38470139fca9SMatthew G. Knepley } 38489566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(lCoords, &coords)); 38490139fca9SMatthew G. Knepley } else { 38509566063dSJacob Faibussowitsch PetscCall(PetscDSSetConstants(cds, dE + 1, moduli)); 38519566063dSJacob Faibussowitsch PetscCall(DMPlexRemapGeometry(dm, 0.0, f0_shear)); 38520139fca9SMatthew G. Knepley } 38539566063dSJacob Faibussowitsch PetscCall(PetscFree(moduli)); 38543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 38550139fca9SMatthew G. Knepley } 3856