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 6be664eb1SMatthew G. Knepley const char *const DMPlexCoordMaps[] = {"none", "shear", "flare", "annulus", "shell", "unknown", "DMPlexCoordMap", "DM_COORD_MAP_", NULL}; 7be664eb1SMatthew G. Knepley 83985bb02SVaclav Hapla /*@ 93985bb02SVaclav Hapla DMPlexFindVertices - Try to find DAG points based on their coordinates. 103985bb02SVaclav Hapla 1120f4b53cSBarry Smith Not Collective (provided `DMGetCoordinatesLocalSetUp()` has been already called) 123985bb02SVaclav Hapla 133985bb02SVaclav Hapla Input Parameters: 1420f4b53cSBarry Smith + dm - The `DMPLEX` object 1520f4b53cSBarry Smith . coordinates - The `Vec` of coordinates of the sought points 1620f4b53cSBarry Smith - eps - The tolerance or `PETSC_DEFAULT` 173985bb02SVaclav Hapla 182fe279fdSBarry Smith Output Parameter: 1920f4b53cSBarry Smith . points - The `IS` of found DAG points or -1 203985bb02SVaclav Hapla 213985bb02SVaclav Hapla Level: intermediate 223985bb02SVaclav Hapla 233985bb02SVaclav Hapla Notes: 2420f4b53cSBarry 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. 253985bb02SVaclav Hapla 2620f4b53cSBarry Smith The output `IS` is living on `PETSC_COMM_SELF` and its length is npoints. 27d3e1f4ccSVaclav Hapla Each rank does the search independently. 2820f4b53cSBarry 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. 293985bb02SVaclav Hapla 3020f4b53cSBarry Smith The output `IS` must be destroyed by user. 313985bb02SVaclav Hapla 323985bb02SVaclav Hapla The tolerance is interpreted as the maximum Euclidean (L2) distance of the sought point from the specified coordinates. 333985bb02SVaclav Hapla 34d3e1f4ccSVaclav 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. 35335ef845SVaclav Hapla 3620f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexCreate()`, `DMGetCoordinatesLocal()` 373985bb02SVaclav Hapla @*/ 38d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points) 39d71ae5a4SJacob Faibussowitsch { 4037900f7dSMatthew G. Knepley PetscInt c, cdim, i, j, o, p, vStart, vEnd; 41d3e1f4ccSVaclav Hapla PetscInt npoints; 42d3e1f4ccSVaclav Hapla const PetscScalar *coord; 433985bb02SVaclav Hapla Vec allCoordsVec; 443985bb02SVaclav Hapla const PetscScalar *allCoords; 45d3e1f4ccSVaclav Hapla PetscInt *dagPoints; 463985bb02SVaclav Hapla 473985bb02SVaclav Hapla PetscFunctionBegin; 483985bb02SVaclav Hapla if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON; 499566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 50d3e1f4ccSVaclav Hapla { 51d3e1f4ccSVaclav Hapla PetscInt n; 52d3e1f4ccSVaclav Hapla 539566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(coordinates, &n)); 5463a3b9bcSJacob 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); 55d3e1f4ccSVaclav Hapla npoints = n / cdim; 56d3e1f4ccSVaclav Hapla } 579566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &allCoordsVec)); 589566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(allCoordsVec, &allCoords)); 599566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coord)); 609566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 6176bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 62335ef845SVaclav Hapla /* check coordinate section is consistent with DM dimension */ 63335ef845SVaclav Hapla PetscSection cs; 64335ef845SVaclav Hapla PetscInt ndof; 65335ef845SVaclav Hapla 669566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &cs)); 673985bb02SVaclav Hapla for (p = vStart; p < vEnd; p++) { 689566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cs, p, &ndof)); 6963a3b9bcSJacob Faibussowitsch PetscCheck(ndof == cdim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %" PetscInt_FMT ": ndof = %" PetscInt_FMT " != %" PetscInt_FMT " = cdim", p, ndof, cdim); 70335ef845SVaclav Hapla } 71335ef845SVaclav Hapla } 729566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npoints, &dagPoints)); 73eca9f518SVaclav Hapla if (eps == 0.0) { 7437900f7dSMatthew G. Knepley for (i = 0, j = 0; i < npoints; i++, j += cdim) { 75eca9f518SVaclav Hapla dagPoints[i] = -1; 7637900f7dSMatthew G. Knepley for (p = vStart, o = 0; p < vEnd; p++, o += cdim) { 7737900f7dSMatthew G. Knepley for (c = 0; c < cdim; c++) { 78d3e1f4ccSVaclav Hapla if (coord[j + c] != allCoords[o + c]) break; 79eca9f518SVaclav Hapla } 8037900f7dSMatthew G. Knepley if (c == cdim) { 81eca9f518SVaclav Hapla dagPoints[i] = p; 82eca9f518SVaclav Hapla break; 83eca9f518SVaclav Hapla } 84eca9f518SVaclav Hapla } 85eca9f518SVaclav Hapla } 86d3e1f4ccSVaclav Hapla } else { 8737900f7dSMatthew G. Knepley for (i = 0, j = 0; i < npoints; i++, j += cdim) { 88d3e1f4ccSVaclav Hapla PetscReal norm; 89d3e1f4ccSVaclav Hapla 90335ef845SVaclav Hapla dagPoints[i] = -1; 9137900f7dSMatthew G. Knepley for (p = vStart, o = 0; p < vEnd; p++, o += cdim) { 923985bb02SVaclav Hapla norm = 0.0; 93ad540459SPierre Jolivet for (c = 0; c < cdim; c++) norm += PetscRealPart(PetscSqr(coord[j + c] - allCoords[o + c])); 943985bb02SVaclav Hapla norm = PetscSqrtReal(norm); 953985bb02SVaclav Hapla if (norm <= eps) { 963985bb02SVaclav Hapla dagPoints[i] = p; 973985bb02SVaclav Hapla break; 983985bb02SVaclav Hapla } 993985bb02SVaclav Hapla } 1003985bb02SVaclav Hapla } 101d3e1f4ccSVaclav Hapla } 1029566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(allCoordsVec, &allCoords)); 1039566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coord)); 1049566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points)); 1053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1063985bb02SVaclav Hapla } 1073985bb02SVaclav Hapla 1086363a54bSMatthew G. Knepley #if 0 109d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection) 110d71ae5a4SJacob Faibussowitsch { 111fea14342SMatthew G. Knepley const PetscReal p0_x = segmentA[0 * 2 + 0]; 112fea14342SMatthew G. Knepley const PetscReal p0_y = segmentA[0 * 2 + 1]; 113fea14342SMatthew G. Knepley const PetscReal p1_x = segmentA[1 * 2 + 0]; 114fea14342SMatthew G. Knepley const PetscReal p1_y = segmentA[1 * 2 + 1]; 115fea14342SMatthew G. Knepley const PetscReal p2_x = segmentB[0 * 2 + 0]; 116fea14342SMatthew G. Knepley const PetscReal p2_y = segmentB[0 * 2 + 1]; 117fea14342SMatthew G. Knepley const PetscReal p3_x = segmentB[1 * 2 + 0]; 118fea14342SMatthew G. Knepley const PetscReal p3_y = segmentB[1 * 2 + 1]; 119fea14342SMatthew G. Knepley const PetscReal s1_x = p1_x - p0_x; 120fea14342SMatthew G. Knepley const PetscReal s1_y = p1_y - p0_y; 121fea14342SMatthew G. Knepley const PetscReal s2_x = p3_x - p2_x; 122fea14342SMatthew G. Knepley const PetscReal s2_y = p3_y - p2_y; 123fea14342SMatthew G. Knepley const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y); 124fea14342SMatthew G. Knepley 125fea14342SMatthew G. Knepley PetscFunctionBegin; 126fea14342SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 127fea14342SMatthew G. Knepley /* Non-parallel lines */ 128fea14342SMatthew G. Knepley if (denom != 0.0) { 129fea14342SMatthew G. Knepley const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom; 130fea14342SMatthew G. Knepley const PetscReal t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom; 131fea14342SMatthew G. Knepley 132fea14342SMatthew G. Knepley if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { 133fea14342SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 134fea14342SMatthew G. Knepley if (intersection) { 135fea14342SMatthew G. Knepley intersection[0] = p0_x + (t * s1_x); 136fea14342SMatthew G. Knepley intersection[1] = p0_y + (t * s1_y); 137fea14342SMatthew G. Knepley } 138fea14342SMatthew G. Knepley } 139fea14342SMatthew G. Knepley } 1403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 141fea14342SMatthew G. Knepley } 142fea14342SMatthew G. Knepley 143ddce0771SMatthew G. Knepley /* The plane is segmentB x segmentC: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */ 144d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetLinePlaneIntersection_3D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], const PetscReal segmentC[], PetscReal intersection[], PetscBool *hasIntersection) 145d71ae5a4SJacob Faibussowitsch { 146ddce0771SMatthew G. Knepley const PetscReal p0_x = segmentA[0 * 3 + 0]; 147ddce0771SMatthew G. Knepley const PetscReal p0_y = segmentA[0 * 3 + 1]; 148ddce0771SMatthew G. Knepley const PetscReal p0_z = segmentA[0 * 3 + 2]; 149ddce0771SMatthew G. Knepley const PetscReal p1_x = segmentA[1 * 3 + 0]; 150ddce0771SMatthew G. Knepley const PetscReal p1_y = segmentA[1 * 3 + 1]; 151ddce0771SMatthew G. Knepley const PetscReal p1_z = segmentA[1 * 3 + 2]; 152ddce0771SMatthew G. Knepley const PetscReal q0_x = segmentB[0 * 3 + 0]; 153ddce0771SMatthew G. Knepley const PetscReal q0_y = segmentB[0 * 3 + 1]; 154ddce0771SMatthew G. Knepley const PetscReal q0_z = segmentB[0 * 3 + 2]; 155ddce0771SMatthew G. Knepley const PetscReal q1_x = segmentB[1 * 3 + 0]; 156ddce0771SMatthew G. Knepley const PetscReal q1_y = segmentB[1 * 3 + 1]; 157ddce0771SMatthew G. Knepley const PetscReal q1_z = segmentB[1 * 3 + 2]; 158ddce0771SMatthew G. Knepley const PetscReal r0_x = segmentC[0 * 3 + 0]; 159ddce0771SMatthew G. Knepley const PetscReal r0_y = segmentC[0 * 3 + 1]; 160ddce0771SMatthew G. Knepley const PetscReal r0_z = segmentC[0 * 3 + 2]; 161ddce0771SMatthew G. Knepley const PetscReal r1_x = segmentC[1 * 3 + 0]; 162ddce0771SMatthew G. Knepley const PetscReal r1_y = segmentC[1 * 3 + 1]; 163ddce0771SMatthew G. Knepley const PetscReal r1_z = segmentC[1 * 3 + 2]; 164ddce0771SMatthew G. Knepley const PetscReal s0_x = p1_x - p0_x; 165ddce0771SMatthew G. Knepley const PetscReal s0_y = p1_y - p0_y; 166ddce0771SMatthew G. Knepley const PetscReal s0_z = p1_z - p0_z; 167ddce0771SMatthew G. Knepley const PetscReal s1_x = q1_x - q0_x; 168ddce0771SMatthew G. Knepley const PetscReal s1_y = q1_y - q0_y; 169ddce0771SMatthew G. Knepley const PetscReal s1_z = q1_z - q0_z; 170ddce0771SMatthew G. Knepley const PetscReal s2_x = r1_x - r0_x; 171ddce0771SMatthew G. Knepley const PetscReal s2_y = r1_y - r0_y; 172ddce0771SMatthew G. Knepley const PetscReal s2_z = r1_z - r0_z; 173ddce0771SMatthew G. Knepley const PetscReal s3_x = s1_y * s2_z - s1_z * s2_y; /* s1 x s2 */ 174ddce0771SMatthew G. Knepley const PetscReal s3_y = s1_z * s2_x - s1_x * s2_z; 175ddce0771SMatthew G. Knepley const PetscReal s3_z = s1_x * s2_y - s1_y * s2_x; 176ddce0771SMatthew G. Knepley const PetscReal s4_x = s0_y * s2_z - s0_z * s2_y; /* s0 x s2 */ 177ddce0771SMatthew G. Knepley const PetscReal s4_y = s0_z * s2_x - s0_x * s2_z; 178ddce0771SMatthew G. Knepley const PetscReal s4_z = s0_x * s2_y - s0_y * s2_x; 179ddce0771SMatthew G. Knepley const PetscReal s5_x = s1_y * s0_z - s1_z * s0_y; /* s1 x s0 */ 180ddce0771SMatthew G. Knepley const PetscReal s5_y = s1_z * s0_x - s1_x * s0_z; 181ddce0771SMatthew G. Knepley const PetscReal s5_z = s1_x * s0_y - s1_y * s0_x; 182ddce0771SMatthew G. Knepley const PetscReal denom = -(s0_x * s3_x + s0_y * s3_y + s0_z * s3_z); /* -s0 . (s1 x s2) */ 183ddce0771SMatthew G. Knepley 184ddce0771SMatthew G. Knepley PetscFunctionBegin; 185ddce0771SMatthew G. Knepley *hasIntersection = PETSC_FALSE; 186ddce0771SMatthew G. Knepley /* Line not parallel to plane */ 187ddce0771SMatthew G. Knepley if (denom != 0.0) { 188ddce0771SMatthew G. Knepley const PetscReal t = (s3_x * (p0_x - q0_x) + s3_y * (p0_y - q0_y) + s3_z * (p0_z - q0_z)) / denom; 189ddce0771SMatthew G. Knepley const PetscReal u = (s4_x * (p0_x - q0_x) + s4_y * (p0_y - q0_y) + s4_z * (p0_z - q0_z)) / denom; 190ddce0771SMatthew G. Knepley const PetscReal v = (s5_x * (p0_x - q0_x) + s5_y * (p0_y - q0_y) + s5_z * (p0_z - q0_z)) / denom; 191ddce0771SMatthew G. Knepley 192ddce0771SMatthew G. Knepley if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && v >= 0 && v <= 1) { 193ddce0771SMatthew G. Knepley *hasIntersection = PETSC_TRUE; 194ddce0771SMatthew G. Knepley if (intersection) { 195ddce0771SMatthew G. Knepley intersection[0] = p0_x + (t * s0_x); 196ddce0771SMatthew G. Knepley intersection[1] = p0_y + (t * s0_y); 197ddce0771SMatthew G. Knepley intersection[2] = p0_z + (t * s0_z); 198ddce0771SMatthew G. Knepley } 199ddce0771SMatthew G. Knepley } 200ddce0771SMatthew G. Knepley } 2013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 202ddce0771SMatthew G. Knepley } 2036363a54bSMatthew G. Knepley #endif 2046363a54bSMatthew G. Knepley 2056363a54bSMatthew 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[]) 2066363a54bSMatthew G. Knepley { 2076363a54bSMatthew G. Knepley PetscReal d[4]; // distance of vertices to the plane 2086363a54bSMatthew G. Knepley PetscReal dp; // distance from origin to the plane 2096363a54bSMatthew G. Knepley PetscInt n = 0; 2106363a54bSMatthew G. Knepley 2116363a54bSMatthew G. Knepley PetscFunctionBegin; 2126363a54bSMatthew G. Knepley if (pos) *pos = PETSC_FALSE; 2136363a54bSMatthew G. Knepley if (Nint) *Nint = 0; 2146363a54bSMatthew G. Knepley if (PetscDefined(USE_DEBUG)) { 2156363a54bSMatthew G. Knepley PetscReal mag = DMPlex_NormD_Internal(cdim, normal); 216b58dcb05SPierre Jolivet PetscCheck(PetscAbsReal(mag - (PetscReal)1.0) < PETSC_SMALL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Normal vector is not normalized: %g", (double)mag); 2176363a54bSMatthew G. Knepley } 2186363a54bSMatthew G. Knepley 2196363a54bSMatthew G. Knepley dp = DMPlex_DotRealD_Internal(cdim, normal, p); 2206363a54bSMatthew G. Knepley for (PetscInt v = 0; v < dim + 1; ++v) { 2216363a54bSMatthew G. Knepley // d[v] is positive, zero, or negative if vertex i is above, on, or below the plane 2226363a54bSMatthew G. Knepley #if defined(PETSC_USE_COMPLEX) 2236363a54bSMatthew G. Knepley PetscReal c[4]; 2246363a54bSMatthew G. Knepley for (PetscInt i = 0; i < cdim; ++i) c[i] = PetscRealPart(coords[v * cdim + i]); 2256363a54bSMatthew G. Knepley d[v] = DMPlex_DotRealD_Internal(cdim, normal, c); 2266363a54bSMatthew G. Knepley #else 2276363a54bSMatthew G. Knepley d[v] = DMPlex_DotRealD_Internal(cdim, normal, &coords[v * cdim]); 2286363a54bSMatthew G. Knepley #endif 2296363a54bSMatthew G. Knepley d[v] -= dp; 2306363a54bSMatthew G. Knepley } 2316363a54bSMatthew G. Knepley 2326363a54bSMatthew G. Knepley // If all d are positive or negative, no intersection 2336363a54bSMatthew G. Knepley { 2346363a54bSMatthew G. Knepley PetscInt v; 2356363a54bSMatthew G. Knepley for (v = 0; v < dim + 1; ++v) 2366363a54bSMatthew G. Knepley if (d[v] >= 0.) break; 2376363a54bSMatthew G. Knepley if (v == dim + 1) PetscFunctionReturn(PETSC_SUCCESS); 2386363a54bSMatthew G. Knepley for (v = 0; v < dim + 1; ++v) 2396363a54bSMatthew G. Knepley if (d[v] <= 0.) break; 2406363a54bSMatthew G. Knepley if (v == dim + 1) { 2416363a54bSMatthew G. Knepley if (pos) *pos = PETSC_TRUE; 2426363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 2436363a54bSMatthew G. Knepley } 2446363a54bSMatthew G. Knepley } 2456363a54bSMatthew G. Knepley 2466363a54bSMatthew G. Knepley for (PetscInt v = 0; v < dim + 1; ++v) { 2476363a54bSMatthew G. Knepley // Points with zero distance are automatically added to the list. 2486363a54bSMatthew G. Knepley if (PetscAbsReal(d[v]) < PETSC_MACHINE_EPSILON) { 2496363a54bSMatthew G. Knepley for (PetscInt i = 0; i < cdim; ++i) intPoints[n * cdim + i] = PetscRealPart(coords[v * cdim + i]); 2506363a54bSMatthew G. Knepley ++n; 2516363a54bSMatthew G. Knepley } else { 2526363a54bSMatthew G. Knepley // For each point with nonzero distance, seek another point with opposite sign 2536363a54bSMatthew G. Knepley // and higher index, and compute the intersection of the line between those 2546363a54bSMatthew G. Knepley // points and the plane. 2556363a54bSMatthew G. Knepley for (PetscInt w = v + 1; w < dim + 1; ++w) { 2566363a54bSMatthew G. Knepley if (d[v] * d[w] < 0.) { 2576363a54bSMatthew G. Knepley PetscReal inv_dist = 1. / (d[v] - d[w]); 2586363a54bSMatthew 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; 2596363a54bSMatthew G. Knepley ++n; 2606363a54bSMatthew G. Knepley } 2616363a54bSMatthew G. Knepley } 2626363a54bSMatthew G. Knepley } 2636363a54bSMatthew G. Knepley } 2646363a54bSMatthew G. Knepley // TODO order output points if there are 4 2656363a54bSMatthew G. Knepley *Nint = n; 2666363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 2676363a54bSMatthew G. Knepley } 2686363a54bSMatthew G. Knepley 2696363a54bSMatthew G. Knepley static PetscErrorCode DMPlexGetPlaneSimplexIntersection_Internal(DM dm, PetscInt dim, PetscInt c, const PetscReal p[], const PetscReal normal[], PetscBool *pos, PetscInt *Nint, PetscReal intPoints[]) 2706363a54bSMatthew G. Knepley { 2716363a54bSMatthew G. Knepley const PetscScalar *array; 2726363a54bSMatthew G. Knepley PetscScalar *coords = NULL; 2736363a54bSMatthew G. Knepley PetscInt numCoords; 2746363a54bSMatthew G. Knepley PetscBool isDG; 2756363a54bSMatthew G. Knepley PetscInt cdim; 2766363a54bSMatthew G. Knepley 2776363a54bSMatthew G. Knepley PetscFunctionBegin; 2786363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 2796363a54bSMatthew 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); 2806363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 2816363a54bSMatthew 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); 2826363a54bSMatthew G. Knepley PetscCall(PetscArrayzero(intPoints, dim * (dim + 1))); 2836363a54bSMatthew G. Knepley 2846363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, coords, p, normal, pos, Nint, intPoints)); 2856363a54bSMatthew G. Knepley 2866363a54bSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 2876363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 2886363a54bSMatthew G. Knepley } 2896363a54bSMatthew G. Knepley 2906363a54bSMatthew G. Knepley static PetscErrorCode DMPlexGetPlaneQuadIntersection_Internal(DM dm, PetscInt dim, PetscInt c, const PetscReal p[], const PetscReal normal[], PetscBool *pos, PetscInt *Nint, PetscReal intPoints[]) 2916363a54bSMatthew G. Knepley { 2926363a54bSMatthew G. Knepley const PetscScalar *array; 2936363a54bSMatthew G. Knepley PetscScalar *coords = NULL; 2946363a54bSMatthew G. Knepley PetscInt numCoords; 2956363a54bSMatthew G. Knepley PetscBool isDG; 2966363a54bSMatthew G. Knepley PetscInt cdim; 2976363a54bSMatthew G. Knepley PetscScalar tcoords[6] = {0., 0., 0., 0., 0., 0.}; 2986363a54bSMatthew G. Knepley const PetscInt vertsA[3] = {0, 1, 3}; 2996363a54bSMatthew G. Knepley const PetscInt vertsB[3] = {1, 2, 3}; 3006363a54bSMatthew G. Knepley PetscInt NintA, NintB; 3016363a54bSMatthew G. Knepley 3026363a54bSMatthew G. Knepley PetscFunctionBegin; 3036363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 3046363a54bSMatthew 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); 3056363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 3066363a54bSMatthew 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); 3076363a54bSMatthew G. Knepley PetscCall(PetscArrayzero(intPoints, dim * 4)); 3086363a54bSMatthew G. Knepley 3096363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 3; ++v) 3106363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsA[v] * cdim + d]; 3116363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintA, intPoints)); 3126363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 3; ++v) 3136363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsB[v] * cdim + d]; 3146363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintB, &intPoints[NintA * cdim])); 3156363a54bSMatthew G. Knepley *Nint = NintA + NintB; 3166363a54bSMatthew G. Knepley 3176363a54bSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 3186363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 3196363a54bSMatthew G. Knepley } 3206363a54bSMatthew G. Knepley 3216363a54bSMatthew G. Knepley static PetscErrorCode DMPlexGetPlaneHexIntersection_Internal(DM dm, PetscInt dim, PetscInt c, const PetscReal p[], const PetscReal normal[], PetscBool *pos, PetscInt *Nint, PetscReal intPoints[]) 3226363a54bSMatthew G. Knepley { 3236363a54bSMatthew G. Knepley const PetscScalar *array; 3246363a54bSMatthew G. Knepley PetscScalar *coords = NULL; 3256363a54bSMatthew G. Knepley PetscInt numCoords; 3266363a54bSMatthew G. Knepley PetscBool isDG; 3276363a54bSMatthew G. Knepley PetscInt cdim; 3286363a54bSMatthew G. Knepley PetscScalar tcoords[12] = {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}; 3296363a54bSMatthew G. Knepley // We split using the (2, 4) main diagonal, so all tets contain those vertices 3306363a54bSMatthew G. Knepley const PetscInt vertsA[4] = {0, 1, 2, 4}; 3316363a54bSMatthew G. Knepley const PetscInt vertsB[4] = {0, 2, 3, 4}; 3326363a54bSMatthew G. Knepley const PetscInt vertsC[4] = {1, 7, 2, 4}; 3336363a54bSMatthew G. Knepley const PetscInt vertsD[4] = {2, 7, 6, 4}; 3346363a54bSMatthew G. Knepley const PetscInt vertsE[4] = {3, 5, 4, 2}; 3356363a54bSMatthew G. Knepley const PetscInt vertsF[4] = {4, 5, 6, 2}; 3366363a54bSMatthew G. Knepley PetscInt NintA, NintB, NintC, NintD, NintE, NintF, Nsum = 0; 3376363a54bSMatthew G. Knepley 3386363a54bSMatthew G. Knepley PetscFunctionBegin; 3396363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 3406363a54bSMatthew 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); 3416363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 3426363a54bSMatthew 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); 3436363a54bSMatthew G. Knepley PetscCall(PetscArrayzero(intPoints, dim * 18)); 3446363a54bSMatthew G. Knepley 3456363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3466363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsA[v] * cdim + d]; 3476363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintA, &intPoints[Nsum * cdim])); 3486363a54bSMatthew G. Knepley Nsum += NintA; 3496363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3506363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsB[v] * cdim + d]; 3516363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintB, &intPoints[Nsum * cdim])); 3526363a54bSMatthew G. Knepley Nsum += NintB; 3536363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3546363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsC[v] * cdim + d]; 3556363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintC, &intPoints[Nsum * cdim])); 3566363a54bSMatthew G. Knepley Nsum += NintC; 3576363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3586363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsD[v] * cdim + d]; 3596363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintD, &intPoints[Nsum * cdim])); 3606363a54bSMatthew G. Knepley Nsum += NintD; 3616363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3626363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsE[v] * cdim + d]; 3636363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintE, &intPoints[Nsum * cdim])); 3646363a54bSMatthew G. Knepley Nsum += NintE; 3656363a54bSMatthew G. Knepley for (PetscInt v = 0; v < 4; ++v) 3666363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) tcoords[v * cdim + d] = coords[vertsF[v] * cdim + d]; 3676363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Coords_Internal(dm, dim, cdim, tcoords, p, normal, pos, &NintF, &intPoints[Nsum * cdim])); 3686363a54bSMatthew G. Knepley Nsum += NintF; 3696363a54bSMatthew G. Knepley *Nint = Nsum; 3706363a54bSMatthew G. Knepley 3716363a54bSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 3726363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 3736363a54bSMatthew G. Knepley } 3746363a54bSMatthew G. Knepley 3756363a54bSMatthew G. Knepley /* 3766363a54bSMatthew G. Knepley DMPlexGetPlaneCellIntersection_Internal - Finds the intersection of a plane with a cell 3776363a54bSMatthew G. Knepley 3786363a54bSMatthew G. Knepley Not collective 3796363a54bSMatthew G. Knepley 3806363a54bSMatthew G. Knepley Input Parameters: 3816363a54bSMatthew G. Knepley + dm - the DM 3826363a54bSMatthew G. Knepley . c - the mesh point 3836363a54bSMatthew G. Knepley . p - a point on the plane. 3846363a54bSMatthew G. Knepley - normal - a normal vector to the plane, must be normalized 3856363a54bSMatthew G. Knepley 3866363a54bSMatthew G. Knepley Output Parameters: 3876363a54bSMatthew G. Knepley . pos - `PETSC_TRUE` is the cell is on the positive side of the plane, `PETSC_FALSE` on the negative side 3886363a54bSMatthew G. Knepley + Nint - the number of intersection points, in [0, 4] 3896363a54bSMatthew G. Knepley - intPoints - the coordinates of the intersection points, should be length at least 12 3906363a54bSMatthew G. Knepley 391baca6076SPierre Jolivet Note: The `pos` argument is only meaningful if the number of intersections is 0. The algorithmic idea comes from https://github.com/chrisk314/tet-plane-intersection. 3926363a54bSMatthew G. Knepley 3936363a54bSMatthew G. Knepley Level: developer 3946363a54bSMatthew G. Knepley 3956363a54bSMatthew G. Knepley .seealso: 3966363a54bSMatthew G. Knepley @*/ 3976363a54bSMatthew G. Knepley static PetscErrorCode DMPlexGetPlaneCellIntersection_Internal(DM dm, PetscInt c, const PetscReal p[], const PetscReal normal[], PetscBool *pos, PetscInt *Nint, PetscReal intPoints[]) 3986363a54bSMatthew G. Knepley { 3996363a54bSMatthew G. Knepley DMPolytopeType ct; 4006363a54bSMatthew G. Knepley 4016363a54bSMatthew G. Knepley PetscFunctionBegin; 4026363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellType(dm, c, &ct)); 4036363a54bSMatthew G. Knepley switch (ct) { 4046363a54bSMatthew G. Knepley case DM_POLYTOPE_SEGMENT: 4056363a54bSMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 4066363a54bSMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 4076363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneSimplexIntersection_Internal(dm, DMPolytopeTypeGetDim(ct), c, p, normal, pos, Nint, intPoints)); 4086363a54bSMatthew G. Knepley break; 4096363a54bSMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 4106363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneQuadIntersection_Internal(dm, DMPolytopeTypeGetDim(ct), c, p, normal, pos, Nint, intPoints)); 4116363a54bSMatthew G. Knepley break; 4126363a54bSMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 4136363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneHexIntersection_Internal(dm, DMPolytopeTypeGetDim(ct), c, p, normal, pos, Nint, intPoints)); 4146363a54bSMatthew G. Knepley break; 4156363a54bSMatthew G. Knepley default: 4166363a54bSMatthew G. Knepley SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No plane intersection for cell %" PetscInt_FMT " with type %s", c, DMPolytopeTypes[ct]); 4176363a54bSMatthew G. Knepley } 4186363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 4196363a54bSMatthew G. Knepley } 420ddce0771SMatthew G. Knepley 421d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 422d71ae5a4SJacob Faibussowitsch { 42314bbb9f0SLawrence Mitchell const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 42414bbb9f0SLawrence Mitchell const PetscReal x = PetscRealPart(point[0]); 42514bbb9f0SLawrence Mitchell PetscReal v0, J, invJ, detJ; 42614bbb9f0SLawrence Mitchell PetscReal xi; 42714bbb9f0SLawrence Mitchell 42814bbb9f0SLawrence Mitchell PetscFunctionBegin; 4299566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ)); 43014bbb9f0SLawrence Mitchell xi = invJ * (x - v0); 43114bbb9f0SLawrence Mitchell 43214bbb9f0SLawrence Mitchell if ((xi >= -eps) && (xi <= 2. + eps)) *cell = c; 43314bbb9f0SLawrence Mitchell else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 4343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43514bbb9f0SLawrence Mitchell } 43614bbb9f0SLawrence Mitchell 437d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 438d71ae5a4SJacob Faibussowitsch { 439ccd2543fSMatthew G Knepley const PetscInt embedDim = 2; 440f5ebc837SMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 441ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 442ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 443ccd2543fSMatthew G Knepley PetscReal v0[2], J[4], invJ[4], detJ; 444ccd2543fSMatthew G Knepley PetscReal xi, eta; 445ccd2543fSMatthew G Knepley 446ccd2543fSMatthew G Knepley PetscFunctionBegin; 4479566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 448ccd2543fSMatthew G Knepley xi = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]); 449ccd2543fSMatthew G Knepley eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]); 450ccd2543fSMatthew G Knepley 451f5ebc837SMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0 + eps)) *cell = c; 452c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 4533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 454ccd2543fSMatthew G Knepley } 455ccd2543fSMatthew G Knepley 456d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[]) 457d71ae5a4SJacob Faibussowitsch { 45862a38674SMatthew G. Knepley const PetscInt embedDim = 2; 45962a38674SMatthew G. Knepley PetscReal x = PetscRealPart(point[0]); 46062a38674SMatthew G. Knepley PetscReal y = PetscRealPart(point[1]); 46162a38674SMatthew G. Knepley PetscReal v0[2], J[4], invJ[4], detJ; 46262a38674SMatthew G. Knepley PetscReal xi, eta, r; 46362a38674SMatthew G. Knepley 46462a38674SMatthew G. Knepley PetscFunctionBegin; 4659566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 46662a38674SMatthew G. Knepley xi = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]); 46762a38674SMatthew G. Knepley eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]); 46862a38674SMatthew G. Knepley 46962a38674SMatthew G. Knepley xi = PetscMax(xi, 0.0); 47062a38674SMatthew G. Knepley eta = PetscMax(eta, 0.0); 47162a38674SMatthew G. Knepley if (xi + eta > 2.0) { 47262a38674SMatthew G. Knepley r = (xi + eta) / 2.0; 47362a38674SMatthew G. Knepley xi /= r; 47462a38674SMatthew G. Knepley eta /= r; 47562a38674SMatthew G. Knepley } 47662a38674SMatthew G. Knepley cpoint[0] = J[0 * embedDim + 0] * xi + J[0 * embedDim + 1] * eta + v0[0]; 47762a38674SMatthew G. Knepley cpoint[1] = J[1 * embedDim + 0] * xi + J[1 * embedDim + 1] * eta + v0[1]; 4783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47962a38674SMatthew G. Knepley } 48062a38674SMatthew G. Knepley 48161451c10SMatthew G. Knepley // This is the ray-casting, or even-odd algorithm: https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule 482d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 483d71ae5a4SJacob Faibussowitsch { 48476b3799dSMatthew G. Knepley const PetscScalar *array; 485a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 486ccd2543fSMatthew G Knepley const PetscInt faces[8] = {0, 1, 1, 2, 2, 3, 3, 0}; 487ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 488ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 48976b3799dSMatthew G. Knepley PetscInt crossings = 0, numCoords, f; 49076b3799dSMatthew G. Knepley PetscBool isDG; 491ccd2543fSMatthew G Knepley 492ccd2543fSMatthew G Knepley PetscFunctionBegin; 49376b3799dSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 49476b3799dSMatthew G. Knepley PetscCheck(numCoords == 8, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Quadrilateral should have 8 coordinates, not %" PetscInt_FMT, numCoords); 495ccd2543fSMatthew G Knepley for (f = 0; f < 4; ++f) { 496ccd2543fSMatthew G Knepley PetscReal x_i = PetscRealPart(coords[faces[2 * f + 0] * 2 + 0]); 497ccd2543fSMatthew G Knepley PetscReal y_i = PetscRealPart(coords[faces[2 * f + 0] * 2 + 1]); 498ccd2543fSMatthew G Knepley PetscReal x_j = PetscRealPart(coords[faces[2 * f + 1] * 2 + 0]); 499ccd2543fSMatthew G Knepley PetscReal y_j = PetscRealPart(coords[faces[2 * f + 1] * 2 + 1]); 50061451c10SMatthew G. Knepley 50161451c10SMatthew G. Knepley if ((x == x_j) && (y == y_j)) { 50261451c10SMatthew G. Knepley // point is a corner 50361451c10SMatthew G. Knepley crossings = 1; 50461451c10SMatthew G. Knepley break; 50561451c10SMatthew G. Knepley } 50661451c10SMatthew G. Knepley if ((y_j > y) != (y_i > y)) { 50761451c10SMatthew G. Knepley PetscReal slope = (x - x_j) * (y_i - y_j) - (x_i - x_j) * (y - y_j); 50861451c10SMatthew G. Knepley if (slope == 0) { 50961451c10SMatthew G. Knepley // point is a corner 51061451c10SMatthew G. Knepley crossings = 1; 51161451c10SMatthew G. Knepley break; 51261451c10SMatthew G. Knepley } 51361451c10SMatthew G. Knepley if ((slope < 0) != (y_i < y_j)) ++crossings; 51461451c10SMatthew G. Knepley } 515ccd2543fSMatthew G Knepley } 516ccd2543fSMatthew G Knepley if (crossings % 2) *cell = c; 517c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 51876b3799dSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 5193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 520ccd2543fSMatthew G Knepley } 521ccd2543fSMatthew G Knepley 522d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 523d71ae5a4SJacob Faibussowitsch { 524ccd2543fSMatthew G Knepley const PetscInt embedDim = 3; 52537900f7dSMatthew G. Knepley const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON; 526ccd2543fSMatthew G Knepley PetscReal v0[3], J[9], invJ[9], detJ; 527ccd2543fSMatthew G Knepley PetscReal x = PetscRealPart(point[0]); 528ccd2543fSMatthew G Knepley PetscReal y = PetscRealPart(point[1]); 529ccd2543fSMatthew G Knepley PetscReal z = PetscRealPart(point[2]); 530ccd2543fSMatthew G Knepley PetscReal xi, eta, zeta; 531ccd2543fSMatthew G Knepley 532ccd2543fSMatthew G Knepley PetscFunctionBegin; 5339566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); 534ccd2543fSMatthew G Knepley xi = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]) + invJ[0 * embedDim + 2] * (z - v0[2]); 535ccd2543fSMatthew G Knepley eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]) + invJ[1 * embedDim + 2] * (z - v0[2]); 536ccd2543fSMatthew G Knepley zeta = invJ[2 * embedDim + 0] * (x - v0[0]) + invJ[2 * embedDim + 1] * (y - v0[1]) + invJ[2 * embedDim + 2] * (z - v0[2]); 537ccd2543fSMatthew G Knepley 53837900f7dSMatthew G. Knepley if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0 + eps)) *cell = c; 539c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 5403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 541ccd2543fSMatthew G Knepley } 542ccd2543fSMatthew G Knepley 543d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 544d71ae5a4SJacob Faibussowitsch { 54576b3799dSMatthew G. Knepley const PetscScalar *array; 546872a9804SMatthew G. Knepley PetscScalar *coords = NULL; 5479371c9d4SSatish 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}; 548ccd2543fSMatthew G Knepley PetscBool found = PETSC_TRUE; 54976b3799dSMatthew G. Knepley PetscInt numCoords, f; 55076b3799dSMatthew G. Knepley PetscBool isDG; 551ccd2543fSMatthew G Knepley 552ccd2543fSMatthew G Knepley PetscFunctionBegin; 55376b3799dSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 55476b3799dSMatthew G. Knepley PetscCheck(numCoords == 24, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Quadrilateral should have 8 coordinates, not %" PetscInt_FMT, numCoords); 555ccd2543fSMatthew G Knepley for (f = 0; f < 6; ++f) { 556ccd2543fSMatthew G Knepley /* Check the point is under plane */ 557ccd2543fSMatthew G Knepley /* Get face normal */ 558ccd2543fSMatthew G Knepley PetscReal v_i[3]; 559ccd2543fSMatthew G Knepley PetscReal v_j[3]; 560ccd2543fSMatthew G Knepley PetscReal normal[3]; 561ccd2543fSMatthew G Knepley PetscReal pp[3]; 562ccd2543fSMatthew G Knepley PetscReal dot; 563ccd2543fSMatthew G Knepley 564ccd2543fSMatthew G Knepley v_i[0] = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]); 565ccd2543fSMatthew G Knepley v_i[1] = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]); 566ccd2543fSMatthew G Knepley v_i[2] = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]); 567ccd2543fSMatthew G Knepley v_j[0] = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]); 568ccd2543fSMatthew G Knepley v_j[1] = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]); 569ccd2543fSMatthew G Knepley v_j[2] = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]); 570ccd2543fSMatthew G Knepley normal[0] = v_i[1] * v_j[2] - v_i[2] * v_j[1]; 571ccd2543fSMatthew G Knepley normal[1] = v_i[2] * v_j[0] - v_i[0] * v_j[2]; 572ccd2543fSMatthew G Knepley normal[2] = v_i[0] * v_j[1] - v_i[1] * v_j[0]; 573ccd2543fSMatthew G Knepley pp[0] = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 0] - point[0]); 574ccd2543fSMatthew G Knepley pp[1] = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 1] - point[1]); 575ccd2543fSMatthew G Knepley pp[2] = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 2] - point[2]); 576ccd2543fSMatthew G Knepley dot = normal[0] * pp[0] + normal[1] * pp[1] + normal[2] * pp[2]; 577ccd2543fSMatthew G Knepley 578ccd2543fSMatthew G Knepley /* Check that projected point is in face (2D location problem) */ 579ccd2543fSMatthew G Knepley if (dot < 0.0) { 580ccd2543fSMatthew G Knepley found = PETSC_FALSE; 581ccd2543fSMatthew G Knepley break; 582ccd2543fSMatthew G Knepley } 583ccd2543fSMatthew G Knepley } 584ccd2543fSMatthew G Knepley if (found) *cell = c; 585c1496c66SMatthew G. Knepley else *cell = DMLOCATEPOINT_POINT_NOT_FOUND; 58676b3799dSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 5873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 588ccd2543fSMatthew G Knepley } 589ccd2543fSMatthew G Knepley 590d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[]) 591d71ae5a4SJacob Faibussowitsch { 592c4eade1cSMatthew G. Knepley PetscInt d; 593c4eade1cSMatthew G. Knepley 594c4eade1cSMatthew G. Knepley PetscFunctionBegin; 595c4eade1cSMatthew G. Knepley box->dim = dim; 596378076f8SMatthew G. Knepley for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = point ? PetscRealPart(point[d]) : 0.; 5973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 598c4eade1cSMatthew G. Knepley } 599c4eade1cSMatthew G. Knepley 600d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box) 601d71ae5a4SJacob Faibussowitsch { 602c4eade1cSMatthew G. Knepley PetscFunctionBegin; 6032b6f951bSStefano Zampini PetscCall(PetscCalloc1(1, box)); 6049566063dSJacob Faibussowitsch PetscCall(PetscGridHashInitialize_Internal(*box, dim, point)); 6053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 606c4eade1cSMatthew G. Knepley } 607c4eade1cSMatthew G. Knepley 608d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[]) 609d71ae5a4SJacob Faibussowitsch { 610c4eade1cSMatthew G. Knepley PetscInt d; 611c4eade1cSMatthew G. Knepley 612c4eade1cSMatthew G. Knepley PetscFunctionBegin; 613c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 614c4eade1cSMatthew G. Knepley box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d])); 615c4eade1cSMatthew G. Knepley box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d])); 616c4eade1cSMatthew G. Knepley } 6173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 618c4eade1cSMatthew G. Knepley } 619c4eade1cSMatthew G. Knepley 6206363a54bSMatthew G. Knepley static PetscErrorCode DMPlexCreateGridHash(DM dm, PetscGridHash *box) 6216363a54bSMatthew G. Knepley { 6226363a54bSMatthew G. Knepley Vec coordinates; 623b48d1484SMatthew G. Knepley const PetscScalar *a; 624b48d1484SMatthew G. Knepley PetscInt cdim, cStart, cEnd; 6256363a54bSMatthew G. Knepley 6266363a54bSMatthew G. Knepley PetscFunctionBegin; 6276363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 628b48d1484SMatthew G. Knepley PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 6296363a54bSMatthew G. Knepley PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 6306363a54bSMatthew G. Knepley 631b48d1484SMatthew G. Knepley PetscCall(VecGetArrayRead(coordinates, &a)); 632b48d1484SMatthew G. Knepley PetscCall(PetscGridHashCreate(PetscObjectComm((PetscObject)dm), cdim, a, box)); 633b48d1484SMatthew G. Knepley PetscCall(VecRestoreArrayRead(coordinates, &a)); 634b48d1484SMatthew G. Knepley for (PetscInt c = cStart; c < cEnd; ++c) { 635b48d1484SMatthew G. Knepley const PetscScalar *array; 636b48d1484SMatthew G. Knepley PetscScalar *coords = NULL; 637b48d1484SMatthew G. Knepley PetscInt numCoords; 638b48d1484SMatthew G. Knepley PetscBool isDG; 6396363a54bSMatthew G. Knepley 640b48d1484SMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 641b48d1484SMatthew G. Knepley for (PetscInt i = 0; i < numCoords / cdim; ++i) PetscCall(PetscGridHashEnlarge(*box, &coords[i * cdim])); 642b48d1484SMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 643b48d1484SMatthew G. Knepley } 6446363a54bSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 6456363a54bSMatthew G. Knepley } 6466363a54bSMatthew G. Knepley 647a4e35b19SJacob Faibussowitsch /*@C 64862a38674SMatthew G. Knepley PetscGridHashSetGrid - Divide the grid into boxes 64962a38674SMatthew G. Knepley 65020f4b53cSBarry Smith Not Collective 65162a38674SMatthew G. Knepley 65262a38674SMatthew G. Knepley Input Parameters: 65362a38674SMatthew G. Knepley + box - The grid hash object 654a3b724e8SBarry Smith . n - The number of boxes in each dimension, may use `PETSC_DETERMINE` for the entries 655a3b724e8SBarry Smith - h - The box size in each dimension, only used if n[d] == `PETSC_DETERMINE`, if not needed you can pass in `NULL` 65662a38674SMatthew G. Knepley 65762a38674SMatthew G. Knepley Level: developer 65862a38674SMatthew G. Knepley 6592fe279fdSBarry Smith .seealso: `DMPLEX`, `PetscGridHashCreate()` 660a4e35b19SJacob Faibussowitsch @*/ 661d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[]) 662d71ae5a4SJacob Faibussowitsch { 663c4eade1cSMatthew G. Knepley PetscInt d; 664c4eade1cSMatthew G. Knepley 665c4eade1cSMatthew G. Knepley PetscFunctionBegin; 6664f572ea9SToby Isaac PetscAssertPointer(n, 2); 6674f572ea9SToby Isaac if (h) PetscAssertPointer(h, 3); 668c4eade1cSMatthew G. Knepley for (d = 0; d < box->dim; ++d) { 669c4eade1cSMatthew G. Knepley box->extent[d] = box->upper[d] - box->lower[d]; 670c4eade1cSMatthew G. Knepley if (n[d] == PETSC_DETERMINE) { 67123f0ada9SStefano Zampini PetscCheck(h, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Missing h"); 672c4eade1cSMatthew G. Knepley box->h[d] = h[d]; 673c4eade1cSMatthew G. Knepley box->n[d] = PetscCeilReal(box->extent[d] / h[d]); 674c4eade1cSMatthew G. Knepley } else { 675c4eade1cSMatthew G. Knepley box->n[d] = n[d]; 676c4eade1cSMatthew G. Knepley box->h[d] = box->extent[d] / n[d]; 677c4eade1cSMatthew G. Knepley } 678c4eade1cSMatthew G. Knepley } 6793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 680c4eade1cSMatthew G. Knepley } 681c4eade1cSMatthew G. Knepley 682a4e35b19SJacob Faibussowitsch /*@C 68362a38674SMatthew G. Knepley PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point 68462a38674SMatthew G. Knepley 68520f4b53cSBarry Smith Not Collective 68662a38674SMatthew G. Knepley 68762a38674SMatthew G. Knepley Input Parameters: 68862a38674SMatthew G. Knepley + box - The grid hash object 68962a38674SMatthew G. Knepley . numPoints - The number of input points 69062a38674SMatthew G. Knepley - points - The input point coordinates 69162a38674SMatthew G. Knepley 69262a38674SMatthew G. Knepley Output Parameters: 693a3b724e8SBarry Smith + dboxes - An array of `numPoints` x `dim` integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 694a3b724e8SBarry Smith - boxes - An array of `numPoints` integers expressing the enclosing box as single number, or `NULL` 69562a38674SMatthew G. Knepley 69662a38674SMatthew G. Knepley Level: developer 69762a38674SMatthew G. Knepley 698f5867de0SMatthew G. Knepley Note: 699f5867de0SMatthew G. Knepley This only guarantees that a box contains a point, not that a cell does. 700f5867de0SMatthew G. Knepley 7012fe279fdSBarry Smith .seealso: `DMPLEX`, `PetscGridHashCreate()` 702a4e35b19SJacob Faibussowitsch @*/ 703d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[]) 704d71ae5a4SJacob Faibussowitsch { 705c4eade1cSMatthew G. Knepley const PetscReal *lower = box->lower; 706c4eade1cSMatthew G. Knepley const PetscReal *upper = box->upper; 707c4eade1cSMatthew G. Knepley const PetscReal *h = box->h; 708c4eade1cSMatthew G. Knepley const PetscInt *n = box->n; 709c4eade1cSMatthew G. Knepley const PetscInt dim = box->dim; 710c4eade1cSMatthew G. Knepley PetscInt d, p; 711c4eade1cSMatthew G. Knepley 712c4eade1cSMatthew G. Knepley PetscFunctionBegin; 713c4eade1cSMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 714c4eade1cSMatthew G. Knepley for (d = 0; d < dim; ++d) { 7151c6dfc3eSMatthew G. Knepley PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]); 716c4eade1cSMatthew G. Knepley 7171c6dfc3eSMatthew G. Knepley if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1; 7182a705cacSMatthew G. Knepley if (dbox == -1 && PetscAbsReal(PetscRealPart(points[p * dim + d]) - lower[d]) < 1.0e-9) dbox = 0; 719b48d1484SMatthew G. Knepley 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 (%g, %g, %g) - (%g, %g, %g)", 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, (double)lower[0], (double)lower[1], (double)lower[2], (double)upper[0], (double)upper[1], (double)upper[2]); 720c4eade1cSMatthew G. Knepley dboxes[p * dim + d] = dbox; 721c4eade1cSMatthew G. Knepley } 7229371c9d4SSatish Balay if (boxes) 7239371c9d4SSatish 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]; 724c4eade1cSMatthew G. Knepley } 7253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 726c4eade1cSMatthew G. Knepley } 727c4eade1cSMatthew G. Knepley 728af74b616SDave May /* 729af74b616SDave May PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point 730af74b616SDave May 73120f4b53cSBarry Smith Not Collective 732af74b616SDave May 733af74b616SDave May Input Parameters: 734af74b616SDave May + box - The grid hash object 735f5867de0SMatthew G. Knepley . cellSection - The PetscSection mapping cells to boxes 736af74b616SDave May . numPoints - The number of input points 737af74b616SDave May - points - The input point coordinates 738af74b616SDave May 739af74b616SDave May Output Parameters: 74020f4b53cSBarry Smith + dboxes - An array of `numPoints`*`dim` integers expressing the enclosing box as (i_0, i_1, ..., i_dim) 74120f4b53cSBarry Smith . boxes - An array of `numPoints` integers expressing the enclosing box as single number, or `NULL` 742af74b616SDave May - found - Flag indicating if point was located within a box 743af74b616SDave May 744af74b616SDave May Level: developer 745af74b616SDave May 746f5867de0SMatthew G. Knepley Note: 74720f4b53cSBarry 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. 748f5867de0SMatthew G. Knepley 7492fe279fdSBarry Smith .seealso: `DMPLEX`, `PetscGridHashGetEnclosingBox()` 750af74b616SDave May */ 751a4e35b19SJacob Faibussowitsch static PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscSection cellSection, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[], PetscBool *found) 752d71ae5a4SJacob Faibussowitsch { 753af74b616SDave May const PetscReal *lower = box->lower; 754af74b616SDave May const PetscReal *upper = box->upper; 755af74b616SDave May const PetscReal *h = box->h; 756af74b616SDave May const PetscInt *n = box->n; 757af74b616SDave May const PetscInt dim = box->dim; 758f5867de0SMatthew G. Knepley PetscInt bStart, bEnd, d, p; 759af74b616SDave May 760af74b616SDave May PetscFunctionBegin; 761f5867de0SMatthew G. Knepley PetscValidHeaderSpecific(cellSection, PETSC_SECTION_CLASSID, 2); 762af74b616SDave May *found = PETSC_FALSE; 763f5867de0SMatthew G. Knepley PetscCall(PetscSectionGetChart(box->cellSection, &bStart, &bEnd)); 764af74b616SDave May for (p = 0; p < numPoints; ++p) { 765af74b616SDave May for (d = 0; d < dim; ++d) { 766af74b616SDave May PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]); 767af74b616SDave May 768af74b616SDave May if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1; 7693ba16761SJacob Faibussowitsch if (dbox < 0 || dbox >= n[d]) PetscFunctionReturn(PETSC_SUCCESS); 770af74b616SDave May dboxes[p * dim + d] = dbox; 771af74b616SDave May } 7729371c9d4SSatish Balay if (boxes) 7739371c9d4SSatish 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]; 774f5867de0SMatthew G. Knepley // It is possible for a box to overlap no grid cells 7753ba16761SJacob Faibussowitsch if (boxes[p] < bStart || boxes[p] >= bEnd) PetscFunctionReturn(PETSC_SUCCESS); 776af74b616SDave May } 777af74b616SDave May *found = PETSC_TRUE; 7783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 779af74b616SDave May } 780af74b616SDave May 781d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashDestroy(PetscGridHash *box) 782d71ae5a4SJacob Faibussowitsch { 783c4eade1cSMatthew G. Knepley PetscFunctionBegin; 784c4eade1cSMatthew G. Knepley if (*box) { 7859566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&(*box)->cellSection)); 7869566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*box)->cells)); 7879566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&(*box)->cellsSparse)); 788c4eade1cSMatthew G. Knepley } 7899566063dSJacob Faibussowitsch PetscCall(PetscFree(*box)); 7903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 791c4eade1cSMatthew G. Knepley } 792c4eade1cSMatthew G. Knepley 793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell) 794d71ae5a4SJacob Faibussowitsch { 795ba2698f1SMatthew G. Knepley DMPolytopeType ct; 796cafe43deSMatthew G. Knepley 797cafe43deSMatthew G. Knepley PetscFunctionBegin; 7989566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cellStart, &ct)); 799ba2698f1SMatthew G. Knepley switch (ct) { 800d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_SEGMENT: 801d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell)); 802d71ae5a4SJacob Faibussowitsch break; 803d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_TRIANGLE: 804d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell)); 805d71ae5a4SJacob Faibussowitsch break; 806d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_QUADRILATERAL: 807d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell)); 808d71ae5a4SJacob Faibussowitsch break; 809d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_TETRAHEDRON: 810d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell)); 811d71ae5a4SJacob Faibussowitsch break; 812d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_HEXAHEDRON: 813d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell)); 814d71ae5a4SJacob Faibussowitsch break; 815d71ae5a4SJacob Faibussowitsch default: 816d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %" PetscInt_FMT " with type %s", cellStart, DMPolytopeTypes[ct]); 817cafe43deSMatthew G. Knepley } 8183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 819cafe43deSMatthew G. Knepley } 820cafe43deSMatthew G. Knepley 82162a38674SMatthew G. Knepley /* 82262a38674SMatthew G. Knepley DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point 82362a38674SMatthew G. Knepley */ 824a4e35b19SJacob Faibussowitsch static PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[]) 825d71ae5a4SJacob Faibussowitsch { 826ba2698f1SMatthew G. Knepley DMPolytopeType ct; 82762a38674SMatthew G. Knepley 82862a38674SMatthew G. Knepley PetscFunctionBegin; 8299566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 830ba2698f1SMatthew G. Knepley switch (ct) { 831d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_TRIANGLE: 832d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint)); 833d71ae5a4SJacob Faibussowitsch break; 83462a38674SMatthew G. Knepley #if 0 835ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 8369566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint));break; 837ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 8389566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint));break; 839ba2698f1SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 8409566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint));break; 84162a38674SMatthew G. Knepley #endif 842d71ae5a4SJacob Faibussowitsch default: 843d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %" PetscInt_FMT " with type %s", cell, DMPolytopeTypes[ct]); 84462a38674SMatthew G. Knepley } 8453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 84662a38674SMatthew G. Knepley } 84762a38674SMatthew G. Knepley 84862a38674SMatthew G. Knepley /* 84920f4b53cSBarry Smith DMPlexComputeGridHash_Internal - Create a grid hash structure covering the `DMPLEX` 85062a38674SMatthew G. Knepley 85120f4b53cSBarry Smith Collective 85262a38674SMatthew G. Knepley 85362a38674SMatthew G. Knepley Input Parameter: 85420f4b53cSBarry Smith . dm - The `DMPLEX` 85562a38674SMatthew G. Knepley 85662a38674SMatthew G. Knepley Output Parameter: 85762a38674SMatthew G. Knepley . localBox - The grid hash object 85862a38674SMatthew G. Knepley 85962a38674SMatthew G. Knepley Level: developer 86062a38674SMatthew G. Knepley 8616363a54bSMatthew G. Knepley Notes: 8626363a54bSMatthew G. Knepley How do we determine all boxes intersecting a given cell? 8636363a54bSMatthew G. Knepley 8646363a54bSMatthew G. Knepley 1) Get convex body enclosing cell. We will use a box called the box-hull. 8656363a54bSMatthew G. Knepley 8666363a54bSMatthew G. Knepley 2) Get smallest brick of boxes enclosing the box-hull 8676363a54bSMatthew G. Knepley 8686363a54bSMatthew G. Knepley 3) Each box is composed of 6 planes, 3 lower and 3 upper. We loop over dimensions, and 8696363a54bSMatthew G. Knepley for each new plane determine whether the cell is on the negative side, positive side, or intersects it. 8706363a54bSMatthew G. Knepley 8716363a54bSMatthew G. Knepley a) If the cell is on the negative side of the lower planes, it is not in the box 8726363a54bSMatthew G. Knepley 8736363a54bSMatthew G. Knepley b) If the cell is on the positive side of the upper planes, it is not in the box 8746363a54bSMatthew G. Knepley 8756363a54bSMatthew G. Knepley c) If there is no intersection, it is in the box 8766363a54bSMatthew G. Knepley 8776363a54bSMatthew G. Knepley d) If any intersection point is within the box limits, it is in the box 8786363a54bSMatthew G. Knepley 87920f4b53cSBarry Smith .seealso: `DMPLEX`, `PetscGridHashCreate()`, `PetscGridHashGetEnclosingBox()` 88062a38674SMatthew G. Knepley */ 88166976f2fSJacob Faibussowitsch static PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox) 882d71ae5a4SJacob Faibussowitsch { 883f5867de0SMatthew G. Knepley PetscInt debug = ((DM_Plex *)dm->data)->printLocate; 884cafe43deSMatthew G. Knepley PetscGridHash lbox; 88596217254SMatthew G. Knepley PetscSF sf; 88696217254SMatthew G. Knepley const PetscInt *leaves; 8876363a54bSMatthew G. Knepley PetscInt *dboxes, *boxes; 8886363a54bSMatthew G. Knepley PetscInt cdim, cStart, cEnd, Nl = -1; 889ddce0771SMatthew G. Knepley PetscBool flg; 890cafe43deSMatthew G. Knepley 891cafe43deSMatthew G. Knepley PetscFunctionBegin; 8926363a54bSMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 8939566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 8946363a54bSMatthew G. Knepley PetscCall(DMPlexCreateGridHash(dm, &lbox)); 8956363a54bSMatthew G. Knepley { 8966363a54bSMatthew G. Knepley PetscInt n[3], d; 8976363a54bSMatthew G. Knepley 8986363a54bSMatthew G. Knepley PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, "-dm_plex_hash_box_faces", n, &d, &flg)); 8999371c9d4SSatish Balay if (flg) { 9006363a54bSMatthew G. Knepley for (PetscInt i = d; i < cdim; ++i) n[i] = n[d - 1]; 9019371c9d4SSatish Balay } else { 9026363a54bSMatthew G. Knepley for (PetscInt i = 0; i < cdim; ++i) n[i] = PetscMax(2, PetscFloorReal(PetscPowReal((PetscReal)(cEnd - cStart), 1.0 / cdim) * 0.8)); 9039371c9d4SSatish Balay } 9049566063dSJacob Faibussowitsch PetscCall(PetscGridHashSetGrid(lbox, n, NULL)); 9059371c9d4SSatish Balay if (debug) 9066363a54bSMatthew 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., 9076363a54bSMatthew 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.)); 9086363a54bSMatthew G. Knepley } 9096363a54bSMatthew G. Knepley 91096217254SMatthew G. Knepley PetscCall(DMGetPointSF(dm, &sf)); 91196217254SMatthew G. Knepley if (sf) PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL)); 91296217254SMatthew G. Knepley Nl = PetscMax(Nl, 0); 9136363a54bSMatthew G. Knepley PetscCall(PetscCalloc2(16 * cdim, &dboxes, 16, &boxes)); 9146363a54bSMatthew G. Knepley 9156363a54bSMatthew G. Knepley PetscCall(DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse)); 9166363a54bSMatthew G. Knepley PetscCall(DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd)); 9176363a54bSMatthew G. Knepley for (PetscInt c = cStart; c < cEnd; ++c) { 9186363a54bSMatthew G. Knepley PetscReal intPoints[6 * 6 * 6 * 3]; 9196363a54bSMatthew G. Knepley const PetscScalar *array; 9206363a54bSMatthew G. Knepley PetscScalar *coords = NULL; 921cafe43deSMatthew G. Knepley const PetscReal *h = lbox->h; 9226363a54bSMatthew G. Knepley PetscReal normal[9] = {1., 0., 0., 0., 1., 0., 0., 0., 1.}; 9236363a54bSMatthew G. Knepley PetscReal *lowerIntPoints[3] = {&intPoints[0 * 6 * 6 * 3], &intPoints[1 * 6 * 6 * 3], &intPoints[2 * 6 * 6 * 3]}; 9246363a54bSMatthew G. Knepley PetscReal *upperIntPoints[3] = {&intPoints[3 * 6 * 6 * 3], &intPoints[4 * 6 * 6 * 3], &intPoints[5 * 6 * 6 * 3]}; 9256363a54bSMatthew G. Knepley PetscReal lp[3], up[3], *tmp; 9266363a54bSMatthew G. Knepley PetscInt numCoords, idx, dlim[6], lowerInt[3], upperInt[3]; 9276363a54bSMatthew G. Knepley PetscBool isDG, lower[3], upper[3]; 928cafe43deSMatthew G. Knepley 92996217254SMatthew G. Knepley PetscCall(PetscFindInt(c, Nl, leaves, &idx)); 93096217254SMatthew G. Knepley if (idx >= 0) continue; 9316363a54bSMatthew G. Knepley // Get grid of boxes containing the cell 9326363a54bSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 9336363a54bSMatthew G. Knepley PetscCall(PetscGridHashGetEnclosingBox(lbox, numCoords / cdim, coords, dboxes, boxes)); 9346363a54bSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords)); 9356363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) dlim[d * 2 + 0] = dlim[d * 2 + 1] = dboxes[d]; 9366363a54bSMatthew G. Knepley for (PetscInt d = cdim; d < 3; ++d) dlim[d * 2 + 0] = dlim[d * 2 + 1] = 0; 9376363a54bSMatthew G. Knepley for (PetscInt e = 1; e < numCoords / cdim; ++e) { 9386363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) { 9396363a54bSMatthew G. Knepley dlim[d * 2 + 0] = PetscMin(dlim[d * 2 + 0], dboxes[e * cdim + d]); 9406363a54bSMatthew G. Knepley dlim[d * 2 + 1] = PetscMax(dlim[d * 2 + 1], dboxes[e * cdim + d]); 941ddce0771SMatthew G. Knepley } 942ddce0771SMatthew G. Knepley } 9436363a54bSMatthew G. Knepley if (debug > 4) { 9446363a54bSMatthew 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])); 945ddce0771SMatthew G. Knepley } 9466363a54bSMatthew G. Knepley // Initialize with lower planes for first box 9476363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) { 9486363a54bSMatthew G. Knepley lp[d] = lbox->lower[d] + dlim[d * 2 + 0] * h[d]; 9496363a54bSMatthew G. Knepley up[d] = lp[d] + h[d]; 9506363a54bSMatthew G. Knepley } 9516363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) { 9526363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneCellIntersection_Internal(dm, c, lp, &normal[d * 3], &lower[d], &lowerInt[d], lowerIntPoints[d])); 9536363a54bSMatthew G. Knepley if (debug > 4) { 9546363a54bSMatthew G. Knepley if (!lowerInt[d]) 9556363a54bSMatthew 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")); 9566363a54bSMatthew 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])); 957cafe43deSMatthew G. Knepley } 958cafe43deSMatthew G. Knepley } 9596363a54bSMatthew G. Knepley // Loop over grid 9606363a54bSMatthew G. Knepley for (PetscInt k = dlim[2 * 2 + 0]; k <= dlim[2 * 2 + 1]; ++k, lp[2] = up[2], up[2] += h[2]) { 9616363a54bSMatthew G. Knepley if (cdim > 2) PetscCall(DMPlexGetPlaneCellIntersection_Internal(dm, c, up, &normal[3 * 2], &upper[2], &upperInt[2], upperIntPoints[2])); 9626363a54bSMatthew G. Knepley if (cdim > 2 && debug > 4) { 9636363a54bSMatthew 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")); 9646363a54bSMatthew 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])); 9656363a54bSMatthew G. Knepley } 9666363a54bSMatthew G. Knepley for (PetscInt j = dlim[1 * 2 + 0]; j <= dlim[1 * 2 + 1]; ++j, lp[1] = up[1], up[1] += h[1]) { 9676363a54bSMatthew G. Knepley if (cdim > 1) PetscCall(DMPlexGetPlaneCellIntersection_Internal(dm, c, up, &normal[3 * 1], &upper[1], &upperInt[1], upperIntPoints[1])); 9686363a54bSMatthew G. Knepley if (cdim > 1 && debug > 4) { 9696363a54bSMatthew G. Knepley if (!upperInt[1]) 9706363a54bSMatthew 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")); 9716363a54bSMatthew 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])); 9726363a54bSMatthew G. Knepley } 9736363a54bSMatthew G. Knepley for (PetscInt i = dlim[0 * 2 + 0]; i <= dlim[0 * 2 + 1]; ++i, lp[0] = up[0], up[0] += h[0]) { 974cafe43deSMatthew G. Knepley const PetscInt box = (k * lbox->n[1] + j) * lbox->n[0] + i; 9756363a54bSMatthew G. Knepley PetscBool excNeg = PETSC_TRUE; 9766363a54bSMatthew G. Knepley PetscBool excPos = PETSC_TRUE; 9776363a54bSMatthew G. Knepley PetscInt NlInt = 0; 9786363a54bSMatthew G. Knepley PetscInt NuInt = 0; 979cafe43deSMatthew G. Knepley 9806363a54bSMatthew G. Knepley PetscCall(DMPlexGetPlaneCellIntersection_Internal(dm, c, up, &normal[3 * 0], &upper[0], &upperInt[0], upperIntPoints[0])); 9816363a54bSMatthew G. Knepley if (debug > 4) { 9826363a54bSMatthew G. Knepley if (!upperInt[0]) 9836363a54bSMatthew 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")); 9846363a54bSMatthew 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])); 9856363a54bSMatthew G. Knepley } 9866363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) { 9876363a54bSMatthew G. Knepley NlInt += lowerInt[d]; 9886363a54bSMatthew G. Knepley NuInt += upperInt[d]; 9896363a54bSMatthew G. Knepley } 9906363a54bSMatthew G. Knepley // If there is no intersection... 9916363a54bSMatthew G. Knepley if (!NlInt && !NuInt) { 9926363a54bSMatthew G. Knepley // If the cell is on the negative side of the lower planes, it is not in the box 9936363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) 9946363a54bSMatthew G. Knepley if (lower[d]) { 9956363a54bSMatthew G. Knepley excNeg = PETSC_FALSE; 9960b6bfacdSStefano Zampini break; 9970b6bfacdSStefano Zampini } 9986363a54bSMatthew G. Knepley // If the cell is on the positive side of the upper planes, it is not in the box 9996363a54bSMatthew G. Knepley for (PetscInt d = 0; d < cdim; ++d) 10006363a54bSMatthew G. Knepley if (!upper[d]) { 10016363a54bSMatthew G. Knepley excPos = PETSC_FALSE; 10029371c9d4SSatish Balay break; 1003ddce0771SMatthew G. Knepley } 10046363a54bSMatthew G. Knepley if (excNeg || excPos) { 10056363a54bSMatthew G. Knepley if (debug && excNeg) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " is on the negative side of the lower plane\n", c)); 10066363a54bSMatthew G. Knepley if (debug && excPos) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " is on the positive side of the upper plane\n", c)); 10076363a54bSMatthew G. Knepley continue; 10086363a54bSMatthew G. Knepley } 10096363a54bSMatthew G. Knepley // Otherwise it is in the box 10106363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Cell %" PetscInt_FMT " is contained in box %" PetscInt_FMT "\n", c, box)); 10116363a54bSMatthew G. Knepley PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 10126363a54bSMatthew G. Knepley continue; 10136363a54bSMatthew G. Knepley } 1014b3e8128dSjosephpu /* 1015b3e8128dSjosephpu If any intersection point is within the box limits, it is in the box 1016b3e8128dSjosephpu We need to have tolerances here since intersection point calculations can introduce errors 1017b3e8128dSjosephpu Initialize a count to track which planes have intersection outside the box. 1018b3e8128dSjosephpu if two adjacent planes have intersection points upper and lower all outside the box, look 1019b3e8128dSjosephpu first at if another plane has intersection points outside the box, if so, it is inside the cell 1020b3e8128dSjosephpu look next if no intersection points exist on the other planes, and check if the planes are on the 1021b3e8128dSjosephpu outside of the intersection points but on opposite ends. If so, the box cuts through the cell. 1022b3e8128dSjosephpu */ 1023b3e8128dSjosephpu PetscInt outsideCount[6] = {0, 0, 0, 0, 0, 0}; 10246363a54bSMatthew G. Knepley for (PetscInt plane = 0; plane < cdim; ++plane) { 10256363a54bSMatthew G. Knepley for (PetscInt ip = 0; ip < lowerInt[plane]; ++ip) { 10266363a54bSMatthew G. Knepley PetscInt d; 10276363a54bSMatthew G. Knepley 10286363a54bSMatthew G. Knepley for (d = 0; d < cdim; ++d) { 1029b3e8128dSjosephpu if ((lowerIntPoints[plane][ip * cdim + d] < (lp[d] - PETSC_SMALL)) || (lowerIntPoints[plane][ip * cdim + d] > (up[d] + PETSC_SMALL))) { 1030b3e8128dSjosephpu if (lowerIntPoints[plane][ip * cdim + d] < (lp[d] - PETSC_SMALL)) outsideCount[d]++; // The lower point is to the left of this box, and we count it 1031b3e8128dSjosephpu break; 1032b3e8128dSjosephpu } 10336363a54bSMatthew G. Knepley } 10346363a54bSMatthew G. Knepley if (d == cdim) { 10356363a54bSMatthew 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)); 10366363a54bSMatthew G. Knepley PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 10376363a54bSMatthew G. Knepley goto end; 10386363a54bSMatthew G. Knepley } 10396363a54bSMatthew G. Knepley } 10406363a54bSMatthew G. Knepley for (PetscInt ip = 0; ip < upperInt[plane]; ++ip) { 10416363a54bSMatthew G. Knepley PetscInt d; 10426363a54bSMatthew G. Knepley 10436363a54bSMatthew G. Knepley for (d = 0; d < cdim; ++d) { 1044b3e8128dSjosephpu if ((upperIntPoints[plane][ip * cdim + d] < (lp[d] - PETSC_SMALL)) || (upperIntPoints[plane][ip * cdim + d] > (up[d] + PETSC_SMALL))) { 1045b3e8128dSjosephpu if (upperIntPoints[plane][ip * cdim + d] > (up[d] + PETSC_SMALL)) outsideCount[cdim + d]++; // The upper point is to the right of this box, and we count it 1046b3e8128dSjosephpu break; 1047b3e8128dSjosephpu } 10486363a54bSMatthew G. Knepley } 10496363a54bSMatthew G. Knepley if (d == cdim) { 10506363a54bSMatthew 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)); 10516363a54bSMatthew G. Knepley PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 10526363a54bSMatthew G. Knepley goto end; 1053ddce0771SMatthew G. Knepley } 1054ddce0771SMatthew G. Knepley } 1055cafe43deSMatthew G. Knepley } 1056b3e8128dSjosephpu /* 1057b3e8128dSjosephpu Check the planes with intersections 1058b3e8128dSjosephpu in 2D, check if the square falls in the middle of a cell 1059b3e8128dSjosephpu ie all four planes have intersection points outside of the box 1060b3e8128dSjosephpu You do not want to be doing this, because it means your grid hashing is finer than your grid, 1061b3e8128dSjosephpu but we should still support it I guess 1062b3e8128dSjosephpu */ 1063b3e8128dSjosephpu if (cdim == 2) { 1064b3e8128dSjosephpu PetscInt nIntersects = 0; 1065b3e8128dSjosephpu for (PetscInt d = 0; d < cdim; ++d) nIntersects += (outsideCount[d] + outsideCount[d + cdim]); 1066b3e8128dSjosephpu // if the count adds up to 8, that means each plane has 2 external intersections and thus it is in the cell 1067b3e8128dSjosephpu if (nIntersects == 8) { 1068b3e8128dSjosephpu PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 1069b3e8128dSjosephpu goto end; 1070b3e8128dSjosephpu } 1071b3e8128dSjosephpu } 1072b3e8128dSjosephpu /* 1073baca6076SPierre Jolivet In 3 dimensions, if two adjacent planes have at least 3 intersections outside the cell in the appropriate direction, 1074b3e8128dSjosephpu we then check the 3rd planar dimension. If a plane falls between intersection points, the cell belongs to that box. 1075b3e8128dSjosephpu If the planes are on opposite sides of the intersection points, the cell belongs to that box and it passes through the cell. 1076b3e8128dSjosephpu */ 1077b3e8128dSjosephpu if (cdim == 3) { 1078b3e8128dSjosephpu PetscInt faces[3] = {0, 0, 0}, checkInternalFace = 0; 1079b3e8128dSjosephpu // Find two adjacent planes with at least 3 intersection points in the upper and lower 1080b3e8128dSjosephpu // if the third plane has 3 intersection points or more, a pyramid base is formed on that plane and it is in the cell 1081b3e8128dSjosephpu for (PetscInt d = 0; d < cdim; ++d) 1082b3e8128dSjosephpu if (outsideCount[d] >= 3 && outsideCount[cdim + d] >= 3) { 1083b3e8128dSjosephpu faces[d]++; 1084b3e8128dSjosephpu checkInternalFace++; 1085b3e8128dSjosephpu } 1086b3e8128dSjosephpu if (checkInternalFace == 3) { 1087b3e8128dSjosephpu // All planes have 3 intersection points, add it. 1088b3e8128dSjosephpu PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 1089b3e8128dSjosephpu goto end; 1090b3e8128dSjosephpu } 1091b3e8128dSjosephpu // Gross, figure out which adjacent faces have at least 3 points 1092b3e8128dSjosephpu PetscInt nonIntersectingFace = -1; 1093b3e8128dSjosephpu if (faces[0] == faces[1]) nonIntersectingFace = 2; 1094b3e8128dSjosephpu if (faces[0] == faces[2]) nonIntersectingFace = 1; 1095b3e8128dSjosephpu if (faces[1] == faces[2]) nonIntersectingFace = 0; 1096b3e8128dSjosephpu if (nonIntersectingFace >= 0) { 1097b3e8128dSjosephpu for (PetscInt plane = 0; plane < cdim; ++plane) { 1098b3e8128dSjosephpu if (!lowerInt[nonIntersectingFace] && !upperInt[nonIntersectingFace]) continue; 1099b3e8128dSjosephpu // If we have 2 adjacent sides with pyramids of intersection outside of them, and there is a point between the end caps at all, it must be between the two non intersecting ends, and the box is inside the cell. 1100b3e8128dSjosephpu for (PetscInt ip = 0; ip < lowerInt[nonIntersectingFace]; ++ip) { 1101b3e8128dSjosephpu if (lowerIntPoints[plane][ip * cdim + nonIntersectingFace] > lp[nonIntersectingFace] - PETSC_SMALL || lowerIntPoints[plane][ip * cdim + nonIntersectingFace] < up[nonIntersectingFace] + PETSC_SMALL) goto setpoint; 1102b3e8128dSjosephpu } 1103b3e8128dSjosephpu for (PetscInt ip = 0; ip < upperInt[nonIntersectingFace]; ++ip) { 1104b3e8128dSjosephpu if (upperIntPoints[plane][ip * cdim + nonIntersectingFace] > lp[nonIntersectingFace] - PETSC_SMALL || upperIntPoints[plane][ip * cdim + nonIntersectingFace] < up[nonIntersectingFace] + PETSC_SMALL) goto setpoint; 1105b3e8128dSjosephpu } 1106b3e8128dSjosephpu goto end; 1107b3e8128dSjosephpu } 1108b3e8128dSjosephpu // The points are within the bonds of the non intersecting planes, add it. 1109b3e8128dSjosephpu setpoint: 1110b3e8128dSjosephpu PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box)); 1111b3e8128dSjosephpu goto end; 1112b3e8128dSjosephpu } 1113b3e8128dSjosephpu } 11146363a54bSMatthew G. Knepley end: 11156363a54bSMatthew G. Knepley lower[0] = upper[0]; 11166363a54bSMatthew G. Knepley lowerInt[0] = upperInt[0]; 11176363a54bSMatthew G. Knepley tmp = lowerIntPoints[0]; 11186363a54bSMatthew G. Knepley lowerIntPoints[0] = upperIntPoints[0]; 11196363a54bSMatthew G. Knepley upperIntPoints[0] = tmp; 11206363a54bSMatthew G. Knepley } 11216363a54bSMatthew G. Knepley lp[0] = lbox->lower[0] + dlim[0 * 2 + 0] * h[0]; 11226363a54bSMatthew G. Knepley up[0] = lp[0] + h[0]; 11236363a54bSMatthew G. Knepley lower[1] = upper[1]; 11246363a54bSMatthew G. Knepley lowerInt[1] = upperInt[1]; 11256363a54bSMatthew G. Knepley tmp = lowerIntPoints[1]; 11266363a54bSMatthew G. Knepley lowerIntPoints[1] = upperIntPoints[1]; 11276363a54bSMatthew G. Knepley upperIntPoints[1] = tmp; 11286363a54bSMatthew G. Knepley } 11296363a54bSMatthew G. Knepley lp[1] = lbox->lower[1] + dlim[1 * 2 + 0] * h[1]; 11306363a54bSMatthew G. Knepley up[1] = lp[1] + h[1]; 11316363a54bSMatthew G. Knepley lower[2] = upper[2]; 11326363a54bSMatthew G. Knepley lowerInt[2] = upperInt[2]; 11336363a54bSMatthew G. Knepley tmp = lowerIntPoints[2]; 11346363a54bSMatthew G. Knepley lowerIntPoints[2] = upperIntPoints[2]; 11356363a54bSMatthew G. Knepley upperIntPoints[2] = tmp; 1136fea14342SMatthew G. Knepley } 1137fea14342SMatthew G. Knepley } 11386363a54bSMatthew G. Knepley PetscCall(PetscFree2(dboxes, boxes)); 11396363a54bSMatthew G. Knepley 11409566063dSJacob Faibussowitsch if (debug) PetscCall(DMLabelView(lbox->cellsSparse, PETSC_VIEWER_STDOUT_SELF)); 11419566063dSJacob Faibussowitsch PetscCall(DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells)); 11429566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lbox->cellsSparse)); 1143cafe43deSMatthew G. Knepley *localBox = lbox; 11443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1145cafe43deSMatthew G. Knepley } 1146cafe43deSMatthew G. Knepley 1147d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF) 1148d71ae5a4SJacob Faibussowitsch { 1149f5867de0SMatthew G. Knepley PetscInt debug = ((DM_Plex *)dm->data)->printLocate; 1150cafe43deSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 1151af74b616SDave May PetscBool hash = mesh->useHashLocation, reuse = PETSC_FALSE; 11523a93e3b7SToby Isaac PetscInt bs, numPoints, p, numFound, *found = NULL; 1153d8206211SMatthew G. Knepley PetscInt dim, Nl = 0, cStart, cEnd, numCells, c, d; 1154d8206211SMatthew G. Knepley PetscSF sf; 1155d8206211SMatthew G. Knepley const PetscInt *leaves; 1156cafe43deSMatthew G. Knepley const PetscInt *boxCells; 11573a93e3b7SToby Isaac PetscSFNode *cells; 1158ccd2543fSMatthew G Knepley PetscScalar *a; 11593a93e3b7SToby Isaac PetscMPIInt result; 1160af74b616SDave May PetscLogDouble t0, t1; 11619cb35068SDave May PetscReal gmin[3], gmax[3]; 11629cb35068SDave May PetscInt terminating_query_type[] = {0, 0, 0}; 11636363a54bSMatthew G. Knepley PetscMPIInt rank; 1164ccd2543fSMatthew G Knepley 1165ccd2543fSMatthew G Knepley PetscFunctionBegin; 11666363a54bSMatthew G. Knepley PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 11679566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_LocatePoints, 0, 0, 0, 0)); 11689566063dSJacob Faibussowitsch PetscCall(PetscTime(&t0)); 11691dca8a05SBarry 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."); 11709566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dim)); 11719566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(v, &bs)); 11729566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF), PETSC_COMM_SELF, &result)); 11731dca8a05SBarry Smith PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT, PetscObjectComm((PetscObject)cellSF), PETSC_ERR_SUP, "Trying parallel point location: only local point location supported"); 117463a3b9bcSJacob 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); 11756858538eSMatthew G. Knepley PetscCall(DMGetCoordinatesLocalSetUp(dm)); 11769566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 1177d8206211SMatthew G. Knepley PetscCall(DMGetPointSF(dm, &sf)); 1178d8206211SMatthew G. Knepley if (sf) PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL)); 1179d8206211SMatthew G. Knepley Nl = PetscMax(Nl, 0); 11809566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(v, &numPoints)); 11819566063dSJacob Faibussowitsch PetscCall(VecGetArray(v, &a)); 1182ccd2543fSMatthew G Knepley numPoints /= bs; 1183af74b616SDave May { 1184af74b616SDave May const PetscSFNode *sf_cells; 1185af74b616SDave May 11869566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(cellSF, NULL, NULL, NULL, &sf_cells)); 1187af74b616SDave May if (sf_cells) { 11889566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Re-using existing StarForest node list\n")); 1189af74b616SDave May cells = (PetscSFNode *)sf_cells; 1190af74b616SDave May reuse = PETSC_TRUE; 1191af74b616SDave May } else { 11929566063dSJacob Faibussowitsch PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n")); 11939566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints, &cells)); 1194af74b616SDave May /* initialize cells if created */ 1195af74b616SDave May for (p = 0; p < numPoints; p++) { 1196af74b616SDave May cells[p].rank = 0; 1197af74b616SDave May cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 1198af74b616SDave May } 1199af74b616SDave May } 1200af74b616SDave May } 120176b3799dSMatthew G. Knepley PetscCall(DMGetBoundingBox(dm, gmin, gmax)); 1202953fc75cSMatthew G. Knepley if (hash) { 12039371c9d4SSatish Balay if (!mesh->lbox) { 120496217254SMatthew G. Knepley PetscCall(PetscInfo(dm, "Initializing grid hashing\n")); 12059371c9d4SSatish Balay PetscCall(DMPlexComputeGridHash_Internal(dm, &mesh->lbox)); 12069371c9d4SSatish Balay } 1207cafe43deSMatthew G. Knepley /* Designate the local box for each point */ 1208cafe43deSMatthew G. Knepley /* Send points to correct process */ 1209cafe43deSMatthew G. Knepley /* Search cells that lie in each subbox */ 1210cafe43deSMatthew G. Knepley /* Should we bin points before doing search? */ 12119566063dSJacob Faibussowitsch PetscCall(ISGetIndices(mesh->lbox->cells, &boxCells)); 1212953fc75cSMatthew G. Knepley } 12133a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; ++p) { 1214ccd2543fSMatthew G Knepley const PetscScalar *point = &a[p * bs]; 1215e56f9228SJed Brown PetscInt dbin[3] = {-1, -1, -1}, bin, cell = -1, cellOffset; 12169cb35068SDave May PetscBool point_outside_domain = PETSC_FALSE; 1217ccd2543fSMatthew G Knepley 12189cb35068SDave May /* check bounding box of domain */ 12199cb35068SDave May for (d = 0; d < dim; d++) { 12209371c9d4SSatish Balay if (PetscRealPart(point[d]) < gmin[d]) { 12219371c9d4SSatish Balay point_outside_domain = PETSC_TRUE; 12229371c9d4SSatish Balay break; 12239371c9d4SSatish Balay } 12249371c9d4SSatish Balay if (PetscRealPart(point[d]) > gmax[d]) { 12259371c9d4SSatish Balay point_outside_domain = PETSC_TRUE; 12269371c9d4SSatish Balay break; 12279371c9d4SSatish Balay } 12289cb35068SDave May } 12299cb35068SDave May if (point_outside_domain) { 1230e9b685f5SMatthew G. Knepley cells[p].rank = 0; 1231e9b685f5SMatthew G. Knepley cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 12329cb35068SDave May terminating_query_type[0]++; 12339cb35068SDave May continue; 12349cb35068SDave May } 1235ccd2543fSMatthew G Knepley 1236af74b616SDave May /* check initial values in cells[].index - abort early if found */ 1237af74b616SDave May if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) { 1238af74b616SDave May c = cells[p].index; 12393a93e3b7SToby Isaac cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND; 12409566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell)); 1241af74b616SDave May if (cell >= 0) { 1242af74b616SDave May cells[p].rank = 0; 1243af74b616SDave May cells[p].index = cell; 1244af74b616SDave May numFound++; 1245af74b616SDave May } 1246af74b616SDave May } 12479cb35068SDave May if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) { 12489cb35068SDave May terminating_query_type[1]++; 12499cb35068SDave May continue; 12509cb35068SDave May } 1251af74b616SDave May 1252953fc75cSMatthew G. Knepley if (hash) { 1253af74b616SDave May PetscBool found_box; 1254af74b616SDave May 12556363a54bSMatthew 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.)); 1256af74b616SDave May /* allow for case that point is outside box - abort early */ 1257f5867de0SMatthew G. Knepley PetscCall(PetscGridHashGetEnclosingBoxQuery(mesh->lbox, mesh->lbox->cellSection, 1, point, dbin, &bin, &found_box)); 1258af74b616SDave May if (found_box) { 12596363a54bSMatthew 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)); 1260cafe43deSMatthew G. Knepley /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */ 12619566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells)); 12629566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset)); 1263cafe43deSMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 12646363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Checking for point in cell %" PetscInt_FMT "\n", rank, boxCells[c])); 12659566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell)); 12663a93e3b7SToby Isaac if (cell >= 0) { 12676363a54bSMatthew G. Knepley if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] FOUND in cell %" PetscInt_FMT "\n", rank, cell)); 12683a93e3b7SToby Isaac cells[p].rank = 0; 12693a93e3b7SToby Isaac cells[p].index = cell; 12703a93e3b7SToby Isaac numFound++; 12719cb35068SDave May terminating_query_type[2]++; 12723a93e3b7SToby Isaac break; 1273ccd2543fSMatthew G Knepley } 12743a93e3b7SToby Isaac } 1275af74b616SDave May } 1276953fc75cSMatthew G. Knepley } else { 1277953fc75cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1278d8206211SMatthew G. Knepley PetscInt idx; 1279d8206211SMatthew G. Knepley 1280d8206211SMatthew G. Knepley PetscCall(PetscFindInt(c, Nl, leaves, &idx)); 1281d8206211SMatthew G. Knepley if (idx >= 0) continue; 12829566063dSJacob Faibussowitsch PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell)); 12833a93e3b7SToby Isaac if (cell >= 0) { 12843a93e3b7SToby Isaac cells[p].rank = 0; 12853a93e3b7SToby Isaac cells[p].index = cell; 12863a93e3b7SToby Isaac numFound++; 12879cb35068SDave May terminating_query_type[2]++; 12883a93e3b7SToby Isaac break; 1289953fc75cSMatthew G. Knepley } 1290953fc75cSMatthew G. Knepley } 12913a93e3b7SToby Isaac } 1292ccd2543fSMatthew G Knepley } 12939566063dSJacob Faibussowitsch if (hash) PetscCall(ISRestoreIndices(mesh->lbox->cells, &boxCells)); 129462a38674SMatthew G. Knepley if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) { 129562a38674SMatthew G. Knepley for (p = 0; p < numPoints; p++) { 129662a38674SMatthew G. Knepley const PetscScalar *point = &a[p * bs]; 1297d52e4eadSJose E. Roman PetscReal cpoint[3] = {0, 0, 0}, diff[3], best[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL}, dist, distMax = PETSC_MAX_REAL; 1298d92c4b9fSToby Isaac PetscInt dbin[3] = {-1, -1, -1}, bin, cellOffset, d, bestc = -1; 129962a38674SMatthew G. Knepley 1300e9b685f5SMatthew G. Knepley if (cells[p].index < 0) { 13019566063dSJacob Faibussowitsch PetscCall(PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin)); 13029566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells)); 13039566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset)); 130462a38674SMatthew G. Knepley for (c = cellOffset; c < cellOffset + numCells; ++c) { 13059566063dSJacob Faibussowitsch PetscCall(DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint)); 1306b716b415SMatthew G. Knepley for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]); 130762a38674SMatthew G. Knepley dist = DMPlex_NormD_Internal(dim, diff); 130862a38674SMatthew G. Knepley if (dist < distMax) { 1309d92c4b9fSToby Isaac for (d = 0; d < dim; ++d) best[d] = cpoint[d]; 1310d92c4b9fSToby Isaac bestc = boxCells[c]; 131162a38674SMatthew G. Knepley distMax = dist; 131262a38674SMatthew G. Knepley } 131362a38674SMatthew G. Knepley } 1314d92c4b9fSToby Isaac if (distMax < PETSC_MAX_REAL) { 1315d92c4b9fSToby Isaac ++numFound; 1316d92c4b9fSToby Isaac cells[p].rank = 0; 1317d92c4b9fSToby Isaac cells[p].index = bestc; 1318d92c4b9fSToby Isaac for (d = 0; d < dim; ++d) a[p * bs + d] = best[d]; 1319d92c4b9fSToby Isaac } 132062a38674SMatthew G. Knepley } 132162a38674SMatthew G. Knepley } 132262a38674SMatthew G. Knepley } 132362a38674SMatthew G. Knepley /* This code is only be relevant when interfaced to parallel point location */ 1324cafe43deSMatthew G. Knepley /* Check for highest numbered proc that claims a point (do we care?) */ 13252d1fa6caSMatthew G. Knepley if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) { 13269566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numFound, &found)); 13273a93e3b7SToby Isaac for (p = 0, numFound = 0; p < numPoints; p++) { 13283a93e3b7SToby Isaac if (cells[p].rank >= 0 && cells[p].index >= 0) { 1329ad540459SPierre Jolivet if (numFound < p) cells[numFound] = cells[p]; 13303a93e3b7SToby Isaac found[numFound++] = p; 13313a93e3b7SToby Isaac } 13323a93e3b7SToby Isaac } 13333a93e3b7SToby Isaac } 13349566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(v, &a)); 133548a46eb9SPierre Jolivet if (!reuse) PetscCall(PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER)); 13369566063dSJacob Faibussowitsch PetscCall(PetscTime(&t1)); 13379cb35068SDave May if (hash) { 133863a3b9bcSJacob 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])); 13399cb35068SDave May } else { 134063a3b9bcSJacob 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])); 13419cb35068SDave May } 134263a3b9bcSJacob 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)))); 13439566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_LocatePoints, 0, 0, 0, 0)); 13443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1345ccd2543fSMatthew G Knepley } 1346ccd2543fSMatthew G Knepley 1347cc4c1da9SBarry Smith /*@ 1348741bfc07SMatthew G. Knepley DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates 1349741bfc07SMatthew G. Knepley 135020f4b53cSBarry Smith Not Collective 1351741bfc07SMatthew G. Knepley 13526b867d5aSJose E. Roman Input/Output Parameter: 1353a3b724e8SBarry Smith . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x, an array of size 4, last two entries are unchanged 1354741bfc07SMatthew G. Knepley 13556b867d5aSJose E. Roman Output Parameter: 1356a3b724e8SBarry Smith . R - The rotation which accomplishes the projection, array of size 4 1357741bfc07SMatthew G. Knepley 1358741bfc07SMatthew G. Knepley Level: developer 1359741bfc07SMatthew G. Knepley 13602fe279fdSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeProjection3Dto1D()`, `DMPlexComputeProjection3Dto2D()` 1361741bfc07SMatthew G. Knepley @*/ 1362d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[]) 1363d71ae5a4SJacob Faibussowitsch { 136417fe8556SMatthew G. Knepley const PetscReal x = PetscRealPart(coords[2] - coords[0]); 136517fe8556SMatthew G. Knepley const PetscReal y = PetscRealPart(coords[3] - coords[1]); 13668b49ba18SBarry Smith const PetscReal r = PetscSqrtReal(x * x + y * y), c = x / r, s = y / r; 136717fe8556SMatthew G. Knepley 136817fe8556SMatthew G. Knepley PetscFunctionBegin; 13699371c9d4SSatish Balay R[0] = c; 13709371c9d4SSatish Balay R[1] = -s; 13719371c9d4SSatish Balay R[2] = s; 13729371c9d4SSatish Balay R[3] = c; 137317fe8556SMatthew G. Knepley coords[0] = 0.0; 13747f07f362SMatthew G. Knepley coords[1] = r; 13753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 137617fe8556SMatthew G. Knepley } 137717fe8556SMatthew G. Knepley 1378cc4c1da9SBarry Smith /*@ 1379741bfc07SMatthew G. Knepley DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates 138028dbe442SToby Isaac 138120f4b53cSBarry Smith Not Collective 138228dbe442SToby Isaac 13836b867d5aSJose E. Roman Input/Output Parameter: 1384a3b724e8SBarry Smith . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z, an array of size 6, the other entries are unchanged 1385741bfc07SMatthew G. Knepley 13866b867d5aSJose E. Roman Output Parameter: 1387a3b724e8SBarry Smith . R - The rotation which accomplishes the projection, an array of size 9 1388741bfc07SMatthew G. Knepley 1389741bfc07SMatthew G. Knepley Level: developer 1390741bfc07SMatthew G. Knepley 13911d27aa22SBarry Smith Note: 13921d27aa22SBarry Smith This uses the basis completion described by Frisvad {cite}`frisvad2012building` 13931d27aa22SBarry Smith 13942fe279fdSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto2D()` 1395741bfc07SMatthew G. Knepley @*/ 1396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[]) 1397d71ae5a4SJacob Faibussowitsch { 139828dbe442SToby Isaac PetscReal x = PetscRealPart(coords[3] - coords[0]); 139928dbe442SToby Isaac PetscReal y = PetscRealPart(coords[4] - coords[1]); 140028dbe442SToby Isaac PetscReal z = PetscRealPart(coords[5] - coords[2]); 140128dbe442SToby Isaac PetscReal r = PetscSqrtReal(x * x + y * y + z * z); 140228dbe442SToby Isaac PetscReal rinv = 1. / r; 140328dbe442SToby Isaac 14044d86920dSPierre Jolivet PetscFunctionBegin; 14059371c9d4SSatish Balay x *= rinv; 14069371c9d4SSatish Balay y *= rinv; 14079371c9d4SSatish Balay z *= rinv; 140828dbe442SToby Isaac if (x > 0.) { 140928dbe442SToby Isaac PetscReal inv1pX = 1. / (1. + x); 141028dbe442SToby Isaac 14119371c9d4SSatish Balay R[0] = x; 14129371c9d4SSatish Balay R[1] = -y; 14139371c9d4SSatish Balay R[2] = -z; 14149371c9d4SSatish Balay R[3] = y; 14159371c9d4SSatish Balay R[4] = 1. - y * y * inv1pX; 14169371c9d4SSatish Balay R[5] = -y * z * inv1pX; 14179371c9d4SSatish Balay R[6] = z; 14189371c9d4SSatish Balay R[7] = -y * z * inv1pX; 14199371c9d4SSatish Balay R[8] = 1. - z * z * inv1pX; 14209371c9d4SSatish Balay } else { 142128dbe442SToby Isaac PetscReal inv1mX = 1. / (1. - x); 142228dbe442SToby Isaac 14239371c9d4SSatish Balay R[0] = x; 14249371c9d4SSatish Balay R[1] = z; 14259371c9d4SSatish Balay R[2] = y; 14269371c9d4SSatish Balay R[3] = y; 14279371c9d4SSatish Balay R[4] = -y * z * inv1mX; 14289371c9d4SSatish Balay R[5] = 1. - y * y * inv1mX; 14299371c9d4SSatish Balay R[6] = z; 14309371c9d4SSatish Balay R[7] = 1. - z * z * inv1mX; 14319371c9d4SSatish Balay R[8] = -y * z * inv1mX; 143228dbe442SToby Isaac } 143328dbe442SToby Isaac coords[0] = 0.0; 143428dbe442SToby Isaac coords[1] = r; 1435cc4c1da9SBarry Smith coords[2] = 0.0; 14363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 143728dbe442SToby Isaac } 143828dbe442SToby Isaac 1439741bfc07SMatthew G. Knepley /*@ 1440c871b86eSJed Brown DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the 1441c871b86eSJed Brown plane. The normal is defined by positive orientation of the first 3 points. 1442741bfc07SMatthew G. Knepley 144320f4b53cSBarry Smith Not Collective 1444741bfc07SMatthew G. Knepley 1445741bfc07SMatthew G. Knepley Input Parameter: 14466b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points) 1447741bfc07SMatthew G. Knepley 14486b867d5aSJose E. Roman Input/Output Parameter: 14496b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first 14506b867d5aSJose E. Roman 2*coordSize/3 entries contain interlaced 2D points, with the rest undefined 14516b867d5aSJose E. Roman 14526b867d5aSJose E. Roman Output Parameter: 14536b867d5aSJose 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. 1454741bfc07SMatthew G. Knepley 1455741bfc07SMatthew G. Knepley Level: developer 1456741bfc07SMatthew G. Knepley 14572fe279fdSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto1D()` 1458741bfc07SMatthew G. Knepley @*/ 1459d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) 1460d71ae5a4SJacob Faibussowitsch { 1461c871b86eSJed Brown PetscReal x1[3], x2[3], n[3], c[3], norm; 1462ccd2543fSMatthew G Knepley const PetscInt dim = 3; 1463c871b86eSJed Brown PetscInt d, p; 1464ccd2543fSMatthew G Knepley 1465ccd2543fSMatthew G Knepley PetscFunctionBegin; 1466ccd2543fSMatthew G Knepley /* 0) Calculate normal vector */ 1467ccd2543fSMatthew G Knepley for (d = 0; d < dim; ++d) { 14681ee9d5ecSMatthew G. Knepley x1[d] = PetscRealPart(coords[1 * dim + d] - coords[0 * dim + d]); 14691ee9d5ecSMatthew G. Knepley x2[d] = PetscRealPart(coords[2 * dim + d] - coords[0 * dim + d]); 1470ccd2543fSMatthew G Knepley } 1471c871b86eSJed Brown // n = x1 \otimes x2 1472ccd2543fSMatthew G Knepley n[0] = x1[1] * x2[2] - x1[2] * x2[1]; 1473ccd2543fSMatthew G Knepley n[1] = x1[2] * x2[0] - x1[0] * x2[2]; 1474ccd2543fSMatthew G Knepley n[2] = x1[0] * x2[1] - x1[1] * x2[0]; 14758b49ba18SBarry Smith norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]); 1476c871b86eSJed Brown for (d = 0; d < dim; d++) n[d] /= norm; 1477c871b86eSJed Brown norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]); 1478c871b86eSJed Brown for (d = 0; d < dim; d++) x1[d] /= norm; 1479c871b86eSJed Brown // x2 = n \otimes x1 1480c871b86eSJed Brown x2[0] = n[1] * x1[2] - n[2] * x1[1]; 1481c871b86eSJed Brown x2[1] = n[2] * x1[0] - n[0] * x1[2]; 1482c871b86eSJed Brown x2[2] = n[0] * x1[1] - n[1] * x1[0]; 1483c871b86eSJed Brown for (d = 0; d < dim; d++) { 1484c871b86eSJed Brown R[d * dim + 0] = x1[d]; 1485c871b86eSJed Brown R[d * dim + 1] = x2[d]; 1486c871b86eSJed Brown R[d * dim + 2] = n[d]; 1487c871b86eSJed Brown c[d] = PetscRealPart(coords[0 * dim + d]); 148873868372SMatthew G. Knepley } 1489c871b86eSJed Brown for (p = 0; p < coordSize / dim; p++) { 1490c871b86eSJed Brown PetscReal y[3]; 1491c871b86eSJed Brown for (d = 0; d < dim; d++) y[d] = PetscRealPart(coords[p * dim + d]) - c[d]; 1492c871b86eSJed 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]; 14937f07f362SMatthew G. Knepley } 14943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1495ccd2543fSMatthew G Knepley } 1496ccd2543fSMatthew G Knepley 1497d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) 1498d71ae5a4SJacob Faibussowitsch { 1499834e62ceSMatthew G. Knepley /* Signed volume is 1/2 the determinant 1500834e62ceSMatthew G. Knepley 1501834e62ceSMatthew G. Knepley | 1 1 1 | 1502834e62ceSMatthew G. Knepley | x0 x1 x2 | 1503834e62ceSMatthew G. Knepley | y0 y1 y2 | 1504834e62ceSMatthew G. Knepley 1505834e62ceSMatthew G. Knepley but if x0,y0 is the origin, we have 1506834e62ceSMatthew G. Knepley 1507834e62ceSMatthew G. Knepley | x1 x2 | 1508834e62ceSMatthew G. Knepley | y1 y2 | 1509834e62ceSMatthew G. Knepley */ 1510834e62ceSMatthew G. Knepley const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1]; 1511834e62ceSMatthew G. Knepley const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1]; 1512834e62ceSMatthew G. Knepley PetscReal M[4], detM; 15139371c9d4SSatish Balay M[0] = x1; 15149371c9d4SSatish Balay M[1] = x2; 15159371c9d4SSatish Balay M[2] = y1; 15169371c9d4SSatish Balay M[3] = y2; 1517923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(&detM, M); 1518834e62ceSMatthew G. Knepley *vol = 0.5 * detM; 15193bc0b13bSBarry Smith (void)PetscLogFlops(5.0); 1520834e62ceSMatthew G. Knepley } 1521834e62ceSMatthew G. Knepley 1522d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) 1523d71ae5a4SJacob Faibussowitsch { 1524834e62ceSMatthew G. Knepley /* Signed volume is 1/6th of the determinant 1525834e62ceSMatthew G. Knepley 1526834e62ceSMatthew G. Knepley | 1 1 1 1 | 1527834e62ceSMatthew G. Knepley | x0 x1 x2 x3 | 1528834e62ceSMatthew G. Knepley | y0 y1 y2 y3 | 1529834e62ceSMatthew G. Knepley | z0 z1 z2 z3 | 1530834e62ceSMatthew G. Knepley 1531834e62ceSMatthew G. Knepley but if x0,y0,z0 is the origin, we have 1532834e62ceSMatthew G. Knepley 1533834e62ceSMatthew G. Knepley | x1 x2 x3 | 1534834e62ceSMatthew G. Knepley | y1 y2 y3 | 1535834e62ceSMatthew G. Knepley | z1 z2 z3 | 1536834e62ceSMatthew G. Knepley */ 1537834e62ceSMatthew G. Knepley const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2]; 1538834e62ceSMatthew G. Knepley const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2]; 1539834e62ceSMatthew G. Knepley const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2]; 15400a3da2c2SToby Isaac const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.); 1541834e62ceSMatthew G. Knepley PetscReal M[9], detM; 15429371c9d4SSatish Balay M[0] = x1; 15439371c9d4SSatish Balay M[1] = x2; 15449371c9d4SSatish Balay M[2] = x3; 15459371c9d4SSatish Balay M[3] = y1; 15469371c9d4SSatish Balay M[4] = y2; 15479371c9d4SSatish Balay M[5] = y3; 15489371c9d4SSatish Balay M[6] = z1; 15499371c9d4SSatish Balay M[7] = z2; 15509371c9d4SSatish Balay M[8] = z3; 1551923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(&detM, M); 15520a3da2c2SToby Isaac *vol = -onesixth * detM; 15533bc0b13bSBarry Smith (void)PetscLogFlops(10.0); 1554834e62ceSMatthew G. Knepley } 1555834e62ceSMatthew G. Knepley 1556d71ae5a4SJacob Faibussowitsch static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) 1557d71ae5a4SJacob Faibussowitsch { 15580a3da2c2SToby Isaac const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.); 1559923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(vol, coords); 15600a3da2c2SToby Isaac *vol *= -onesixth; 15610ec8681fSMatthew G. Knepley } 15620ec8681fSMatthew G. Knepley 1563d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1564d71ae5a4SJacob Faibussowitsch { 1565cb92db44SToby Isaac PetscSection coordSection; 1566cb92db44SToby Isaac Vec coordinates; 1567cb92db44SToby Isaac const PetscScalar *coords; 1568cb92db44SToby Isaac PetscInt dim, d, off; 1569cb92db44SToby Isaac 1570cb92db44SToby Isaac PetscFunctionBegin; 15719566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 15729566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 15739566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, e, &dim)); 15743ba16761SJacob Faibussowitsch if (!dim) PetscFunctionReturn(PETSC_SUCCESS); 15759566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, e, &off)); 15769566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coords)); 15779371c9d4SSatish Balay if (v0) { 15789371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]); 15799371c9d4SSatish Balay } 15809566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coords)); 1581cb92db44SToby Isaac *detJ = 1.; 1582cb92db44SToby Isaac if (J) { 1583cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) J[d] = 0.; 1584cb92db44SToby Isaac for (d = 0; d < dim; d++) J[d * dim + d] = 1.; 1585cb92db44SToby Isaac if (invJ) { 1586cb92db44SToby Isaac for (d = 0; d < dim * dim; d++) invJ[d] = 0.; 1587cb92db44SToby Isaac for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.; 1588cb92db44SToby Isaac } 1589cb92db44SToby Isaac } 15903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1591cb92db44SToby Isaac } 1592cb92db44SToby Isaac 15936858538eSMatthew G. Knepley /*@C 15946858538eSMatthew G. Knepley DMPlexGetCellCoordinates - Get coordinates for a cell, taking into account periodicity 15956858538eSMatthew G. Knepley 159620f4b53cSBarry Smith Not Collective 15976858538eSMatthew G. Knepley 15986858538eSMatthew G. Knepley Input Parameters: 159920f4b53cSBarry Smith + dm - The `DMPLEX` 16006858538eSMatthew G. Knepley - cell - The cell number 16016858538eSMatthew G. Knepley 16026858538eSMatthew G. Knepley Output Parameters: 16036858538eSMatthew G. Knepley + isDG - Using cellwise coordinates 16046858538eSMatthew G. Knepley . Nc - The number of coordinates 16056858538eSMatthew G. Knepley . array - The coordinate array 16066858538eSMatthew G. Knepley - coords - The cell coordinates 16076858538eSMatthew G. Knepley 16086858538eSMatthew G. Knepley Level: developer 16096858538eSMatthew G. Knepley 161020f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexRestoreCellCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCellCoordinatesLocal()` 16116858538eSMatthew G. Knepley @*/ 1612d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[]) 1613d71ae5a4SJacob Faibussowitsch { 16146858538eSMatthew G. Knepley DM cdm; 16156858538eSMatthew G. Knepley Vec coordinates; 16166858538eSMatthew G. Knepley PetscSection cs; 16176858538eSMatthew G. Knepley const PetscScalar *ccoords; 16186858538eSMatthew G. Knepley PetscInt pStart, pEnd; 16196858538eSMatthew G. Knepley 16206858538eSMatthew G. Knepley PetscFunctionBeginHot; 16216858538eSMatthew G. Knepley *isDG = PETSC_FALSE; 16226858538eSMatthew G. Knepley *Nc = 0; 16236858538eSMatthew G. Knepley *array = NULL; 16246858538eSMatthew G. Knepley *coords = NULL; 16256858538eSMatthew G. Knepley /* Check for cellwise coordinates */ 16266858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateSection(dm, &cs)); 16276858538eSMatthew G. Knepley if (!cs) goto cg; 16286858538eSMatthew G. Knepley /* Check that the cell exists in the cellwise section */ 16296858538eSMatthew G. Knepley PetscCall(PetscSectionGetChart(cs, &pStart, &pEnd)); 16306858538eSMatthew G. Knepley if (cell < pStart || cell >= pEnd) goto cg; 16316858538eSMatthew G. Knepley /* Check for cellwise coordinates for this cell */ 16326858538eSMatthew G. Knepley PetscCall(PetscSectionGetDof(cs, cell, Nc)); 16336858538eSMatthew G. Knepley if (!*Nc) goto cg; 16346858538eSMatthew G. Knepley /* Check for cellwise coordinates */ 16356858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocalNoncollective(dm, &coordinates)); 16366858538eSMatthew G. Knepley if (!coordinates) goto cg; 16376858538eSMatthew G. Knepley /* Get cellwise coordinates */ 16386858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 16396858538eSMatthew G. Knepley PetscCall(VecGetArrayRead(coordinates, array)); 16406858538eSMatthew G. Knepley PetscCall(DMPlexPointLocalRead(cdm, cell, *array, &ccoords)); 16416858538eSMatthew G. Knepley PetscCall(DMGetWorkArray(cdm, *Nc, MPIU_SCALAR, coords)); 16426858538eSMatthew G. Knepley PetscCall(PetscArraycpy(*coords, ccoords, *Nc)); 16436858538eSMatthew G. Knepley PetscCall(VecRestoreArrayRead(coordinates, array)); 16446858538eSMatthew G. Knepley *isDG = PETSC_TRUE; 16453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16466858538eSMatthew G. Knepley cg: 16476858538eSMatthew G. Knepley /* Use continuous coordinates */ 16486858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 16496858538eSMatthew G. Knepley PetscCall(DMGetCoordinateSection(dm, &cs)); 16506858538eSMatthew G. Knepley PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates)); 1651e8e188d2SZach Atkins PetscCall(DMPlexVecGetOrientedClosure_Internal(cdm, cs, PETSC_FALSE, coordinates, cell, 0, Nc, coords)); 16523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16536858538eSMatthew G. Knepley } 16546858538eSMatthew G. Knepley 16556858538eSMatthew G. Knepley /*@C 16566858538eSMatthew G. Knepley DMPlexRestoreCellCoordinates - Get coordinates for a cell, taking into account periodicity 16576858538eSMatthew G. Knepley 165820f4b53cSBarry Smith Not Collective 16596858538eSMatthew G. Knepley 16606858538eSMatthew G. Knepley Input Parameters: 166120f4b53cSBarry Smith + dm - The `DMPLEX` 16626858538eSMatthew G. Knepley - cell - The cell number 16636858538eSMatthew G. Knepley 16646858538eSMatthew G. Knepley Output Parameters: 16656858538eSMatthew G. Knepley + isDG - Using cellwise coordinates 16666858538eSMatthew G. Knepley . Nc - The number of coordinates 16676858538eSMatthew G. Knepley . array - The coordinate array 16686858538eSMatthew G. Knepley - coords - The cell coordinates 16696858538eSMatthew G. Knepley 16706858538eSMatthew G. Knepley Level: developer 16716858538eSMatthew G. Knepley 167220f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexGetCellCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCellCoordinatesLocal()` 16736858538eSMatthew G. Knepley @*/ 1674d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexRestoreCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[]) 1675d71ae5a4SJacob Faibussowitsch { 16766858538eSMatthew G. Knepley DM cdm; 16776858538eSMatthew G. Knepley PetscSection cs; 16786858538eSMatthew G. Knepley Vec coordinates; 16796858538eSMatthew G. Knepley 16806858538eSMatthew G. Knepley PetscFunctionBeginHot; 16816858538eSMatthew G. Knepley if (*isDG) { 16826858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 16836858538eSMatthew G. Knepley PetscCall(DMRestoreWorkArray(cdm, *Nc, MPIU_SCALAR, coords)); 16846858538eSMatthew G. Knepley } else { 16856858538eSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 16866858538eSMatthew G. Knepley PetscCall(DMGetCoordinateSection(dm, &cs)); 16876858538eSMatthew G. Knepley PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates)); 16886858538eSMatthew G. Knepley PetscCall(DMPlexVecRestoreClosure(cdm, cs, coordinates, cell, Nc, (PetscScalar **)coords)); 16896858538eSMatthew G. Knepley } 16903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16916858538eSMatthew G. Knepley } 16926858538eSMatthew G. Knepley 1693d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1694d71ae5a4SJacob Faibussowitsch { 16956858538eSMatthew G. Knepley const PetscScalar *array; 1696a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 16976858538eSMatthew G. Knepley PetscInt numCoords, d; 16986858538eSMatthew G. Knepley PetscBool isDG; 169917fe8556SMatthew G. Knepley 170017fe8556SMatthew G. Knepley PetscFunctionBegin; 17016858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 170208401ef6SPierre Jolivet PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 17037f07f362SMatthew G. Knepley *detJ = 0.0; 170428dbe442SToby Isaac if (numCoords == 6) { 170528dbe442SToby Isaac const PetscInt dim = 3; 170628dbe442SToby Isaac PetscReal R[9], J0; 170728dbe442SToby Isaac 17089371c9d4SSatish Balay if (v0) { 17099371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 17109371c9d4SSatish Balay } 17119566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto1D(coords, R)); 171228dbe442SToby Isaac if (J) { 171328dbe442SToby Isaac J0 = 0.5 * PetscRealPart(coords[1]); 17149371c9d4SSatish Balay J[0] = R[0] * J0; 17159371c9d4SSatish Balay J[1] = R[1]; 17169371c9d4SSatish Balay J[2] = R[2]; 17179371c9d4SSatish Balay J[3] = R[3] * J0; 17189371c9d4SSatish Balay J[4] = R[4]; 17199371c9d4SSatish Balay J[5] = R[5]; 17209371c9d4SSatish Balay J[6] = R[6] * J0; 17219371c9d4SSatish Balay J[7] = R[7]; 17229371c9d4SSatish Balay J[8] = R[8]; 172328dbe442SToby Isaac DMPlex_Det3D_Internal(detJ, J); 17242b6f951bSStefano Zampini if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 1725adac9986SMatthew G. Knepley } 172628dbe442SToby Isaac } else if (numCoords == 4) { 17277f07f362SMatthew G. Knepley const PetscInt dim = 2; 17287f07f362SMatthew G. Knepley PetscReal R[4], J0; 17297f07f362SMatthew G. Knepley 17309371c9d4SSatish Balay if (v0) { 17319371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 17329371c9d4SSatish Balay } 17339566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection2Dto1D(coords, R)); 173417fe8556SMatthew G. Knepley if (J) { 17357f07f362SMatthew G. Knepley J0 = 0.5 * PetscRealPart(coords[1]); 17369371c9d4SSatish Balay J[0] = R[0] * J0; 17379371c9d4SSatish Balay J[1] = R[1]; 17389371c9d4SSatish Balay J[2] = R[2] * J0; 17399371c9d4SSatish Balay J[3] = R[3]; 1740923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1741ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ); 1742adac9986SMatthew G. Knepley } 17437f07f362SMatthew G. Knepley } else if (numCoords == 2) { 17447f07f362SMatthew G. Knepley const PetscInt dim = 1; 17457f07f362SMatthew G. Knepley 17469371c9d4SSatish Balay if (v0) { 17479371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 17489371c9d4SSatish Balay } 17497f07f362SMatthew G. Knepley if (J) { 17507f07f362SMatthew G. Knepley J[0] = 0.5 * (PetscRealPart(coords[1]) - PetscRealPart(coords[0])); 175117fe8556SMatthew G. Knepley *detJ = J[0]; 17529566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0)); 17539371c9d4SSatish Balay if (invJ) { 17549371c9d4SSatish Balay invJ[0] = 1.0 / J[0]; 17559371c9d4SSatish Balay PetscCall(PetscLogFlops(1.0)); 17569371c9d4SSatish Balay } 1757adac9986SMatthew G. Knepley } 17586858538eSMatthew 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); 17596858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 17603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 176117fe8556SMatthew G. Knepley } 176217fe8556SMatthew G. Knepley 1763d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1764d71ae5a4SJacob Faibussowitsch { 17656858538eSMatthew G. Knepley const PetscScalar *array; 1766a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 17676858538eSMatthew G. Knepley PetscInt numCoords, d; 17686858538eSMatthew G. Knepley PetscBool isDG; 1769ccd2543fSMatthew G Knepley 1770ccd2543fSMatthew G Knepley PetscFunctionBegin; 17716858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 17726858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 17737f07f362SMatthew G. Knepley *detJ = 0.0; 1774ccd2543fSMatthew G Knepley if (numCoords == 9) { 17757f07f362SMatthew G. Knepley const PetscInt dim = 3; 17767f07f362SMatthew 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}; 17777f07f362SMatthew G. Knepley 17789371c9d4SSatish Balay if (v0) { 17799371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 17809371c9d4SSatish Balay } 17819566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R)); 17827f07f362SMatthew G. Knepley if (J) { 1783b7ad821dSMatthew G. Knepley const PetscInt pdim = 2; 1784b7ad821dSMatthew G. Knepley 1785b7ad821dSMatthew G. Knepley for (d = 0; d < pdim; d++) { 1786ad540459SPierre 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])); 17877f07f362SMatthew G. Knepley } 17889566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1789923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 17907f07f362SMatthew G. Knepley for (d = 0; d < dim; d++) { 17916858538eSMatthew G. Knepley for (PetscInt f = 0; f < dim; f++) { 17927f07f362SMatthew G. Knepley J[d * dim + f] = 0.0; 1793ad540459SPierre Jolivet for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f]; 17947f07f362SMatthew G. Knepley } 17957f07f362SMatthew G. Knepley } 17969566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 17977f07f362SMatthew G. Knepley } 1798ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 17997f07f362SMatthew G. Knepley } else if (numCoords == 6) { 18007f07f362SMatthew G. Knepley const PetscInt dim = 2; 18017f07f362SMatthew G. Knepley 18029371c9d4SSatish Balay if (v0) { 18039371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 18049371c9d4SSatish Balay } 1805ccd2543fSMatthew G Knepley if (J) { 1806ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1807ad540459SPierre 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])); 1808ccd2543fSMatthew G Knepley } 18099566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1810923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1811ccd2543fSMatthew G Knepley } 1812ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ); 181363a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %" PetscInt_FMT " != 6 or 9", numCoords); 18146858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 18153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1816ccd2543fSMatthew G Knepley } 1817ccd2543fSMatthew G Knepley 1818d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1819d71ae5a4SJacob Faibussowitsch { 18206858538eSMatthew G. Knepley const PetscScalar *array; 1821a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 18226858538eSMatthew G. Knepley PetscInt numCoords, d; 18236858538eSMatthew G. Knepley PetscBool isDG; 1824ccd2543fSMatthew G Knepley 1825ccd2543fSMatthew G Knepley PetscFunctionBegin; 18266858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 18276858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 1828dfccc68fSToby Isaac if (!Nq) { 1829412e9a14SMatthew G. Knepley PetscInt vorder[4] = {0, 1, 2, 3}; 1830412e9a14SMatthew G. Knepley 18319371c9d4SSatish Balay if (isTensor) { 18329371c9d4SSatish Balay vorder[2] = 3; 18339371c9d4SSatish Balay vorder[3] = 2; 18349371c9d4SSatish Balay } 18357f07f362SMatthew G. Knepley *detJ = 0.0; 183699dec3a6SMatthew G. Knepley if (numCoords == 12) { 183799dec3a6SMatthew G. Knepley const PetscInt dim = 3; 183899dec3a6SMatthew 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}; 183999dec3a6SMatthew G. Knepley 18409371c9d4SSatish Balay if (v) { 18419371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 18429371c9d4SSatish Balay } 18439566063dSJacob Faibussowitsch PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R)); 184499dec3a6SMatthew G. Knepley if (J) { 184599dec3a6SMatthew G. Knepley const PetscInt pdim = 2; 184699dec3a6SMatthew G. Knepley 184799dec3a6SMatthew G. Knepley for (d = 0; d < pdim; d++) { 1848412e9a14SMatthew G. Knepley J0[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * pdim + d]) - PetscRealPart(coords[vorder[0] * pdim + d])); 1849412e9a14SMatthew G. Knepley J0[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[2] * pdim + d]) - PetscRealPart(coords[vorder[1] * pdim + d])); 185099dec3a6SMatthew G. Knepley } 18519566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1852923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J0); 185399dec3a6SMatthew G. Knepley for (d = 0; d < dim; d++) { 18546858538eSMatthew G. Knepley for (PetscInt f = 0; f < dim; f++) { 185599dec3a6SMatthew G. Knepley J[d * dim + f] = 0.0; 1856ad540459SPierre Jolivet for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f]; 185799dec3a6SMatthew G. Knepley } 185899dec3a6SMatthew G. Knepley } 18599566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 186099dec3a6SMatthew G. Knepley } 1861ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 186271f58de1SToby Isaac } else if (numCoords == 8) { 186399dec3a6SMatthew G. Knepley const PetscInt dim = 2; 186499dec3a6SMatthew G. Knepley 18659371c9d4SSatish Balay if (v) { 18669371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 18679371c9d4SSatish Balay } 1868ccd2543fSMatthew G Knepley if (J) { 1869ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1870412e9a14SMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d])); 1871412e9a14SMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[3] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d])); 1872ccd2543fSMatthew G Knepley } 18739566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(8.0)); 1874923591dfSMatthew G. Knepley DMPlex_Det2D_Internal(detJ, J); 1875ccd2543fSMatthew G Knepley } 1876ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ); 187763a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords); 1878dfccc68fSToby Isaac } else { 1879dfccc68fSToby Isaac const PetscInt Nv = 4; 1880dfccc68fSToby Isaac const PetscInt dimR = 2; 1881412e9a14SMatthew G. Knepley PetscInt zToPlex[4] = {0, 1, 3, 2}; 1882dfccc68fSToby Isaac PetscReal zOrder[12]; 1883dfccc68fSToby Isaac PetscReal zCoeff[12]; 1884dfccc68fSToby Isaac PetscInt i, j, k, l, dim; 1885dfccc68fSToby Isaac 18869371c9d4SSatish Balay if (isTensor) { 18879371c9d4SSatish Balay zToPlex[2] = 2; 18889371c9d4SSatish Balay zToPlex[3] = 3; 18899371c9d4SSatish Balay } 1890dfccc68fSToby Isaac if (numCoords == 12) { 1891dfccc68fSToby Isaac dim = 3; 1892dfccc68fSToby Isaac } else if (numCoords == 8) { 1893dfccc68fSToby Isaac dim = 2; 189463a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords); 1895dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 1896dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 1897dfccc68fSToby Isaac 1898ad540459SPierre Jolivet for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); 1899dfccc68fSToby Isaac } 1900dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 19012df84da0SMatthew G. Knepley /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta): 19022df84da0SMatthew G. Knepley \phi^0 = (1 - xi - eta + xi eta) --> 1 = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3) 19032df84da0SMatthew G. Knepley \phi^1 = (1 + xi - eta - xi eta) --> xi = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3) 19042df84da0SMatthew G. Knepley \phi^2 = (1 - xi + eta - xi eta) --> eta = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3) 19052df84da0SMatthew G. Knepley \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3) 19062df84da0SMatthew G. Knepley */ 1907dfccc68fSToby Isaac zCoeff[dim * 0 + j] = 0.25 * (zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1908dfccc68fSToby Isaac zCoeff[dim * 1 + j] = 0.25 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1909dfccc68fSToby Isaac zCoeff[dim * 2 + j] = 0.25 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1910dfccc68fSToby Isaac zCoeff[dim * 3 + j] = 0.25 * (zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]); 1911dfccc68fSToby Isaac } 1912dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 1913dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1]; 1914dfccc68fSToby Isaac 1915dfccc68fSToby Isaac if (v) { 1916dfccc68fSToby Isaac PetscReal extPoint[4]; 1917dfccc68fSToby Isaac 1918dfccc68fSToby Isaac extPoint[0] = 1.; 1919dfccc68fSToby Isaac extPoint[1] = xi; 1920dfccc68fSToby Isaac extPoint[2] = eta; 1921dfccc68fSToby Isaac extPoint[3] = xi * eta; 1922dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1923dfccc68fSToby Isaac PetscReal val = 0.; 1924dfccc68fSToby Isaac 1925ad540459SPierre Jolivet for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j]; 1926dfccc68fSToby Isaac v[i * dim + j] = val; 1927dfccc68fSToby Isaac } 1928dfccc68fSToby Isaac } 1929dfccc68fSToby Isaac if (J) { 1930dfccc68fSToby Isaac PetscReal extJ[8]; 1931dfccc68fSToby Isaac 1932dfccc68fSToby Isaac extJ[0] = 0.; 1933dfccc68fSToby Isaac extJ[1] = 0.; 1934dfccc68fSToby Isaac extJ[2] = 1.; 1935dfccc68fSToby Isaac extJ[3] = 0.; 1936dfccc68fSToby Isaac extJ[4] = 0.; 1937dfccc68fSToby Isaac extJ[5] = 1.; 1938dfccc68fSToby Isaac extJ[6] = eta; 1939dfccc68fSToby Isaac extJ[7] = xi; 1940dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 1941dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 1942dfccc68fSToby Isaac PetscReal val = 0.; 1943dfccc68fSToby Isaac 1944ad540459SPierre Jolivet for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k]; 1945dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 1946dfccc68fSToby Isaac } 1947dfccc68fSToby Isaac } 1948dfccc68fSToby Isaac if (dim == 3) { /* put the cross product in the third component of the Jacobian */ 1949dfccc68fSToby Isaac PetscReal x, y, z; 1950dfccc68fSToby Isaac PetscReal *iJ = &J[i * dim * dim]; 1951dfccc68fSToby Isaac PetscReal norm; 1952dfccc68fSToby Isaac 1953dfccc68fSToby Isaac x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0]; 1954dfccc68fSToby Isaac y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1]; 1955dfccc68fSToby Isaac z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0]; 1956dfccc68fSToby Isaac norm = PetscSqrtReal(x * x + y * y + z * z); 1957dfccc68fSToby Isaac iJ[2] = x / norm; 1958dfccc68fSToby Isaac iJ[5] = y / norm; 1959dfccc68fSToby Isaac iJ[8] = z / norm; 1960dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 1961ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); 1962dfccc68fSToby Isaac } else { 1963dfccc68fSToby Isaac DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]); 1964ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); 1965dfccc68fSToby Isaac } 1966dfccc68fSToby Isaac } 1967dfccc68fSToby Isaac } 1968dfccc68fSToby Isaac } 19696858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 19703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1971ccd2543fSMatthew G Knepley } 1972ccd2543fSMatthew G Knepley 1973d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 1974d71ae5a4SJacob Faibussowitsch { 19756858538eSMatthew G. Knepley const PetscScalar *array; 1976a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 1977ccd2543fSMatthew G Knepley const PetscInt dim = 3; 19786858538eSMatthew G. Knepley PetscInt numCoords, d; 19796858538eSMatthew G. Knepley PetscBool isDG; 1980ccd2543fSMatthew G Knepley 1981ccd2543fSMatthew G Knepley PetscFunctionBegin; 19826858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 19836858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 19847f07f362SMatthew G. Knepley *detJ = 0.0; 19859371c9d4SSatish Balay if (v0) { 19869371c9d4SSatish Balay for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]); 19879371c9d4SSatish Balay } 1988ccd2543fSMatthew G Knepley if (J) { 1989ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 1990f0df753eSMatthew G. Knepley /* I orient with outward face normals */ 1991f0df753eSMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1992f0df753eSMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1993f0df753eSMatthew G. Knepley J[d * dim + 2] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 1994ccd2543fSMatthew G Knepley } 19959566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 1996923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 1997ccd2543fSMatthew G Knepley } 1998ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 19996858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 20003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2001ccd2543fSMatthew G Knepley } 2002ccd2543fSMatthew G Knepley 2003d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 2004d71ae5a4SJacob Faibussowitsch { 20056858538eSMatthew G. Knepley const PetscScalar *array; 2006a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 2007ccd2543fSMatthew G Knepley const PetscInt dim = 3; 20086858538eSMatthew G. Knepley PetscInt numCoords, d; 20096858538eSMatthew G. Knepley PetscBool isDG; 2010ccd2543fSMatthew G Knepley 2011ccd2543fSMatthew G Knepley PetscFunctionBegin; 20126858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 20136858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 2014dfccc68fSToby Isaac if (!Nq) { 20157f07f362SMatthew G. Knepley *detJ = 0.0; 20169371c9d4SSatish Balay if (v) { 20179371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 20189371c9d4SSatish Balay } 2019ccd2543fSMatthew G Knepley if (J) { 2020ccd2543fSMatthew G Knepley for (d = 0; d < dim; d++) { 2021f0df753eSMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 2022f0df753eSMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 2023f0df753eSMatthew G. Knepley J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 2024ccd2543fSMatthew G Knepley } 20259566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 2026923591dfSMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 2027ccd2543fSMatthew G Knepley } 2028ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 2029dfccc68fSToby Isaac } else { 2030dfccc68fSToby Isaac const PetscInt Nv = 8; 2031dfccc68fSToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 2032dfccc68fSToby Isaac const PetscInt dim = 3; 2033dfccc68fSToby Isaac const PetscInt dimR = 3; 2034dfccc68fSToby Isaac PetscReal zOrder[24]; 2035dfccc68fSToby Isaac PetscReal zCoeff[24]; 2036dfccc68fSToby Isaac PetscInt i, j, k, l; 2037dfccc68fSToby Isaac 2038dfccc68fSToby Isaac for (i = 0; i < Nv; i++) { 2039dfccc68fSToby Isaac PetscInt zi = zToPlex[i]; 2040dfccc68fSToby Isaac 2041ad540459SPierre Jolivet for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); 2042dfccc68fSToby Isaac } 2043dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 2044dfccc68fSToby 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]); 2045dfccc68fSToby 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]); 2046dfccc68fSToby 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]); 2047dfccc68fSToby 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]); 2048dfccc68fSToby 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]); 2049dfccc68fSToby 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]); 2050dfccc68fSToby 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]); 2051dfccc68fSToby 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]); 2052dfccc68fSToby Isaac } 2053dfccc68fSToby Isaac for (i = 0; i < Nq; i++) { 2054dfccc68fSToby Isaac PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2]; 2055dfccc68fSToby Isaac 2056dfccc68fSToby Isaac if (v) { 205791d2b7ceSToby Isaac PetscReal extPoint[8]; 2058dfccc68fSToby Isaac 2059dfccc68fSToby Isaac extPoint[0] = 1.; 2060dfccc68fSToby Isaac extPoint[1] = xi; 2061dfccc68fSToby Isaac extPoint[2] = eta; 2062dfccc68fSToby Isaac extPoint[3] = xi * eta; 2063dfccc68fSToby Isaac extPoint[4] = theta; 2064dfccc68fSToby Isaac extPoint[5] = theta * xi; 2065dfccc68fSToby Isaac extPoint[6] = theta * eta; 2066dfccc68fSToby Isaac extPoint[7] = theta * eta * xi; 2067dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 2068dfccc68fSToby Isaac PetscReal val = 0.; 2069dfccc68fSToby Isaac 2070ad540459SPierre Jolivet for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j]; 2071dfccc68fSToby Isaac v[i * dim + j] = val; 2072dfccc68fSToby Isaac } 2073dfccc68fSToby Isaac } 2074dfccc68fSToby Isaac if (J) { 2075dfccc68fSToby Isaac PetscReal extJ[24]; 2076dfccc68fSToby Isaac 20779371c9d4SSatish Balay extJ[0] = 0.; 20789371c9d4SSatish Balay extJ[1] = 0.; 20799371c9d4SSatish Balay extJ[2] = 0.; 20809371c9d4SSatish Balay extJ[3] = 1.; 20819371c9d4SSatish Balay extJ[4] = 0.; 20829371c9d4SSatish Balay extJ[5] = 0.; 20839371c9d4SSatish Balay extJ[6] = 0.; 20849371c9d4SSatish Balay extJ[7] = 1.; 20859371c9d4SSatish Balay extJ[8] = 0.; 20869371c9d4SSatish Balay extJ[9] = eta; 20879371c9d4SSatish Balay extJ[10] = xi; 20889371c9d4SSatish Balay extJ[11] = 0.; 20899371c9d4SSatish Balay extJ[12] = 0.; 20909371c9d4SSatish Balay extJ[13] = 0.; 20919371c9d4SSatish Balay extJ[14] = 1.; 20929371c9d4SSatish Balay extJ[15] = theta; 20939371c9d4SSatish Balay extJ[16] = 0.; 20949371c9d4SSatish Balay extJ[17] = xi; 20959371c9d4SSatish Balay extJ[18] = 0.; 20969371c9d4SSatish Balay extJ[19] = theta; 20979371c9d4SSatish Balay extJ[20] = eta; 20989371c9d4SSatish Balay extJ[21] = theta * eta; 20999371c9d4SSatish Balay extJ[22] = theta * xi; 21009371c9d4SSatish Balay extJ[23] = eta * xi; 2101dfccc68fSToby Isaac 2102dfccc68fSToby Isaac for (j = 0; j < dim; j++) { 2103dfccc68fSToby Isaac for (k = 0; k < dimR; k++) { 2104dfccc68fSToby Isaac PetscReal val = 0.; 2105dfccc68fSToby Isaac 2106ad540459SPierre Jolivet for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k]; 2107dfccc68fSToby Isaac J[i * dim * dim + dim * j + k] = val; 2108dfccc68fSToby Isaac } 2109dfccc68fSToby Isaac } 2110dfccc68fSToby Isaac DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 2111ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); 2112dfccc68fSToby Isaac } 2113dfccc68fSToby Isaac } 2114dfccc68fSToby Isaac } 21156858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 21163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2117ccd2543fSMatthew G Knepley } 2118ccd2543fSMatthew G Knepley 2119d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 2120d71ae5a4SJacob Faibussowitsch { 21216858538eSMatthew G. Knepley const PetscScalar *array; 21222df84da0SMatthew G. Knepley PetscScalar *coords = NULL; 21232df84da0SMatthew G. Knepley const PetscInt dim = 3; 21246858538eSMatthew G. Knepley PetscInt numCoords, d; 21256858538eSMatthew G. Knepley PetscBool isDG; 21262df84da0SMatthew G. Knepley 21272df84da0SMatthew G. Knepley PetscFunctionBegin; 21286858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 21296858538eSMatthew G. Knepley PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL"); 21302df84da0SMatthew G. Knepley if (!Nq) { 21312df84da0SMatthew G. Knepley /* Assume that the map to the reference is affine */ 21322df84da0SMatthew G. Knepley *detJ = 0.0; 21339371c9d4SSatish Balay if (v) { 21349371c9d4SSatish Balay for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]); 21359371c9d4SSatish Balay } 21362df84da0SMatthew G. Knepley if (J) { 21372df84da0SMatthew G. Knepley for (d = 0; d < dim; d++) { 21382df84da0SMatthew G. Knepley J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 21392df84da0SMatthew G. Knepley J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 21402df84da0SMatthew G. Knepley J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d])); 21412df84da0SMatthew G. Knepley } 21429566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0)); 21432df84da0SMatthew G. Knepley DMPlex_Det3D_Internal(detJ, J); 21442df84da0SMatthew G. Knepley } 2145ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ); 21462df84da0SMatthew G. Knepley } else { 21472df84da0SMatthew G. Knepley const PetscInt dim = 3; 21482df84da0SMatthew G. Knepley const PetscInt dimR = 3; 21492df84da0SMatthew G. Knepley const PetscInt Nv = 6; 21502df84da0SMatthew G. Knepley PetscReal verts[18]; 21512df84da0SMatthew G. Knepley PetscReal coeff[18]; 21522df84da0SMatthew G. Knepley PetscInt i, j, k, l; 21532df84da0SMatthew G. Knepley 21549371c9d4SSatish Balay for (i = 0; i < Nv; ++i) 21559371c9d4SSatish Balay for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]); 21562df84da0SMatthew G. Knepley for (j = 0; j < dim; ++j) { 21572df84da0SMatthew G. Knepley /* Check for triangle, 21582df84da0SMatthew 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) 21592df84da0SMatthew 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) 21602df84da0SMatthew G. Knepley phi^2 = 1/2 (1 + eta) chi^2 = delta(-1, 1) 21612df84da0SMatthew G. Knepley 21622df84da0SMatthew G. Knepley phi^0 + phi^1 + phi^2 = 1 coef_1 = 1/2 ( chi^1 + chi^2) 21632df84da0SMatthew G. Knepley -phi^0 + phi^1 - phi^2 = xi coef_xi = 1/2 (-chi^0 + chi^1) 21642df84da0SMatthew G. Knepley -phi^0 - phi^1 + phi^2 = eta coef_eta = 1/2 (-chi^0 + chi^2) 21652df84da0SMatthew G. Knepley 21662df84da0SMatthew G. Knepley < chi_0 chi_1 chi_2> A / 1 1 1 \ / phi_0 \ <chi> I <phi>^T so we need the inverse transpose 21672df84da0SMatthew G. Knepley | -1 1 -1 | | phi_1 | = 21682df84da0SMatthew G. Knepley \ -1 -1 1 / \ phi_2 / 21692df84da0SMatthew G. Knepley 21702df84da0SMatthew 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 21712df84da0SMatthew G. Knepley */ 21722df84da0SMatthew G. Knepley /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta): 21732df84da0SMatthew G. Knepley \phi^0 = 1/4 ( -xi - eta + xi zeta + eta zeta) --> / 1 1 1 1 1 1 \ 1 21742df84da0SMatthew G. Knepley \phi^1 = 1/4 (1 + eta - zeta - eta zeta) --> | -1 1 -1 -1 -1 1 | eta 21752df84da0SMatthew G. Knepley \phi^2 = 1/4 (1 + xi - zeta - xi zeta) --> | -1 -1 1 -1 1 -1 | xi 21762df84da0SMatthew G. Knepley \phi^3 = 1/4 ( -xi - eta - xi zeta - eta zeta) --> | -1 -1 -1 1 1 1 | zeta 21772df84da0SMatthew G. Knepley \phi^4 = 1/4 (1 + xi + zeta + xi zeta) --> | 1 1 -1 -1 1 -1 | xi zeta 21782df84da0SMatthew G. Knepley \phi^5 = 1/4 (1 + eta + zeta + eta zeta) --> \ 1 -1 1 -1 -1 1 / eta zeta 21792df84da0SMatthew G. Knepley 1/4 / 0 1 1 0 1 1 \ 21802df84da0SMatthew G. Knepley | -1 1 0 -1 0 1 | 21812df84da0SMatthew G. Knepley | -1 0 1 -1 1 0 | 21822df84da0SMatthew G. Knepley | 0 -1 -1 0 1 1 | 21832df84da0SMatthew G. Knepley | 1 0 -1 -1 1 0 | 21842df84da0SMatthew G. Knepley \ 1 -1 0 -1 0 1 / 21852df84da0SMatthew G. Knepley */ 21862df84da0SMatthew G. Knepley coeff[dim * 0 + j] = (1. / 4.) * (verts[dim * 1 + j] + verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]); 21872df84da0SMatthew G. Knepley coeff[dim * 1 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]); 21882df84da0SMatthew G. Knepley coeff[dim * 2 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]); 21892df84da0SMatthew G. Knepley coeff[dim * 3 + j] = (1. / 4.) * (-verts[dim * 1 + j] - verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]); 21902df84da0SMatthew G. Knepley coeff[dim * 4 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]); 21912df84da0SMatthew G. Knepley coeff[dim * 5 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]); 21922df84da0SMatthew G. Knepley /* For reference prism: 21932df84da0SMatthew G. Knepley {0, 0, 0} 21942df84da0SMatthew G. Knepley {0, 1, 0} 21952df84da0SMatthew G. Knepley {1, 0, 0} 21962df84da0SMatthew G. Knepley {0, 0, 1} 21972df84da0SMatthew G. Knepley {0, 0, 0} 21982df84da0SMatthew G. Knepley {0, 0, 0} 21992df84da0SMatthew G. Knepley */ 22002df84da0SMatthew G. Knepley } 22012df84da0SMatthew G. Knepley for (i = 0; i < Nq; ++i) { 22022df84da0SMatthew G. Knepley const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2]; 22032df84da0SMatthew G. Knepley 22042df84da0SMatthew G. Knepley if (v) { 22052df84da0SMatthew G. Knepley PetscReal extPoint[6]; 22062df84da0SMatthew G. Knepley PetscInt c; 22072df84da0SMatthew G. Knepley 22082df84da0SMatthew G. Knepley extPoint[0] = 1.; 22092df84da0SMatthew G. Knepley extPoint[1] = eta; 22102df84da0SMatthew G. Knepley extPoint[2] = xi; 22112df84da0SMatthew G. Knepley extPoint[3] = zeta; 22122df84da0SMatthew G. Knepley extPoint[4] = xi * zeta; 22132df84da0SMatthew G. Knepley extPoint[5] = eta * zeta; 22142df84da0SMatthew G. Knepley for (c = 0; c < dim; ++c) { 22152df84da0SMatthew G. Knepley PetscReal val = 0.; 22162df84da0SMatthew G. Knepley 2217ad540459SPierre Jolivet for (k = 0; k < Nv; ++k) val += extPoint[k] * coeff[k * dim + c]; 22182df84da0SMatthew G. Knepley v[i * dim + c] = val; 22192df84da0SMatthew G. Knepley } 22202df84da0SMatthew G. Knepley } 22212df84da0SMatthew G. Knepley if (J) { 22222df84da0SMatthew G. Knepley PetscReal extJ[18]; 22232df84da0SMatthew G. Knepley 22249371c9d4SSatish Balay extJ[0] = 0.; 22259371c9d4SSatish Balay extJ[1] = 0.; 22269371c9d4SSatish Balay extJ[2] = 0.; 22279371c9d4SSatish Balay extJ[3] = 0.; 22289371c9d4SSatish Balay extJ[4] = 1.; 22299371c9d4SSatish Balay extJ[5] = 0.; 22309371c9d4SSatish Balay extJ[6] = 1.; 22319371c9d4SSatish Balay extJ[7] = 0.; 22329371c9d4SSatish Balay extJ[8] = 0.; 22339371c9d4SSatish Balay extJ[9] = 0.; 22349371c9d4SSatish Balay extJ[10] = 0.; 22359371c9d4SSatish Balay extJ[11] = 1.; 22369371c9d4SSatish Balay extJ[12] = zeta; 22379371c9d4SSatish Balay extJ[13] = 0.; 22389371c9d4SSatish Balay extJ[14] = xi; 22399371c9d4SSatish Balay extJ[15] = 0.; 22409371c9d4SSatish Balay extJ[16] = zeta; 22419371c9d4SSatish Balay extJ[17] = eta; 22422df84da0SMatthew G. Knepley 22432df84da0SMatthew G. Knepley for (j = 0; j < dim; j++) { 22442df84da0SMatthew G. Knepley for (k = 0; k < dimR; k++) { 22452df84da0SMatthew G. Knepley PetscReal val = 0.; 22462df84da0SMatthew G. Knepley 2247ad540459SPierre Jolivet for (l = 0; l < Nv; l++) val += coeff[dim * l + j] * extJ[dimR * l + k]; 22482df84da0SMatthew G. Knepley J[i * dim * dim + dim * j + k] = val; 22492df84da0SMatthew G. Knepley } 22502df84da0SMatthew G. Knepley } 22512df84da0SMatthew G. Knepley DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]); 2252ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); 22532df84da0SMatthew G. Knepley } 22542df84da0SMatthew G. Knepley } 22552df84da0SMatthew G. Knepley } 22566858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords)); 22573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22582df84da0SMatthew G. Knepley } 22592df84da0SMatthew G. Knepley 2260d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 2261d71ae5a4SJacob Faibussowitsch { 2262ba2698f1SMatthew G. Knepley DMPolytopeType ct; 2263dfccc68fSToby Isaac PetscInt depth, dim, coordDim, coneSize, i; 2264dfccc68fSToby Isaac PetscInt Nq = 0; 2265dfccc68fSToby Isaac const PetscReal *points = NULL; 2266dfccc68fSToby Isaac DMLabel depthLabel; 2267c330f8ffSToby Isaac PetscReal xi0[3] = {-1., -1., -1.}, v0[3], J0[9], detJ0; 2268dfccc68fSToby Isaac PetscBool isAffine = PETSC_TRUE; 2269dfccc68fSToby Isaac 2270dfccc68fSToby Isaac PetscFunctionBegin; 22719566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 22729566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 22739566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dm, &depthLabel)); 22749566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(depthLabel, cell, &dim)); 227548a46eb9SPierre Jolivet if (depth == 1 && dim == 1) PetscCall(DMGetDimension(dm, &dim)); 22769566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &coordDim)); 227763a3b9bcSJacob Faibussowitsch PetscCheck(coordDim <= 3, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %" PetscInt_FMT " > 3", coordDim); 22789566063dSJacob Faibussowitsch if (quad) PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL)); 22799566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2280ba2698f1SMatthew G. Knepley switch (ct) { 2281ba2698f1SMatthew G. Knepley case DM_POLYTOPE_POINT: 22829566063dSJacob Faibussowitsch PetscCall(DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ)); 2283dfccc68fSToby Isaac isAffine = PETSC_FALSE; 2284dfccc68fSToby Isaac break; 2285ba2698f1SMatthew G. Knepley case DM_POLYTOPE_SEGMENT: 2286412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 22879566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 22889566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ)); 2289dfccc68fSToby Isaac break; 2290ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 22919566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 22929566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ)); 2293dfccc68fSToby Isaac break; 2294ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 22959566063dSJacob Faibussowitsch PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ)); 2296412e9a14SMatthew G. Knepley isAffine = PETSC_FALSE; 2297412e9a14SMatthew G. Knepley break; 2298412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 22999566063dSJacob Faibussowitsch PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ)); 2300dfccc68fSToby Isaac isAffine = PETSC_FALSE; 2301dfccc68fSToby Isaac break; 2302ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 23039566063dSJacob Faibussowitsch if (Nq) PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0)); 23049566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ)); 2305dfccc68fSToby Isaac break; 2306ba2698f1SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 23079566063dSJacob Faibussowitsch PetscCall(DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ)); 2308dfccc68fSToby Isaac isAffine = PETSC_FALSE; 2309dfccc68fSToby Isaac break; 23102df84da0SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM: 23119566063dSJacob Faibussowitsch PetscCall(DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ)); 23122df84da0SMatthew G. Knepley isAffine = PETSC_FALSE; 23132df84da0SMatthew G. Knepley break; 2314d71ae5a4SJacob Faibussowitsch default: 2315d71ae5a4SJacob 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))]); 2316dfccc68fSToby Isaac } 23177318780aSToby Isaac if (isAffine && Nq) { 2318dfccc68fSToby Isaac if (v) { 2319ad540459SPierre Jolivet for (i = 0; i < Nq; i++) CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]); 2320dfccc68fSToby Isaac } 23217318780aSToby Isaac if (detJ) { 2322ad540459SPierre Jolivet for (i = 0; i < Nq; i++) detJ[i] = detJ0; 23237318780aSToby Isaac } 23247318780aSToby Isaac if (J) { 23257318780aSToby Isaac PetscInt k; 23267318780aSToby Isaac 23277318780aSToby Isaac for (i = 0, k = 0; i < Nq; i++) { 2328dfccc68fSToby Isaac PetscInt j; 2329dfccc68fSToby Isaac 2330ad540459SPierre Jolivet for (j = 0; j < coordDim * coordDim; j++, k++) J[k] = J0[j]; 23317318780aSToby Isaac } 23327318780aSToby Isaac } 23337318780aSToby Isaac if (invJ) { 23347318780aSToby Isaac PetscInt k; 23357318780aSToby Isaac switch (coordDim) { 2336d71ae5a4SJacob Faibussowitsch case 0: 2337d71ae5a4SJacob Faibussowitsch break; 2338d71ae5a4SJacob Faibussowitsch case 1: 2339d71ae5a4SJacob Faibussowitsch invJ[0] = 1. / J0[0]; 2340d71ae5a4SJacob Faibussowitsch break; 2341d71ae5a4SJacob Faibussowitsch case 2: 2342d71ae5a4SJacob Faibussowitsch DMPlex_Invert2D_Internal(invJ, J0, detJ0); 2343d71ae5a4SJacob Faibussowitsch break; 2344d71ae5a4SJacob Faibussowitsch case 3: 2345d71ae5a4SJacob Faibussowitsch DMPlex_Invert3D_Internal(invJ, J0, detJ0); 2346d71ae5a4SJacob Faibussowitsch break; 23477318780aSToby Isaac } 23487318780aSToby Isaac for (i = 1, k = coordDim * coordDim; i < Nq; i++) { 23497318780aSToby Isaac PetscInt j; 23507318780aSToby Isaac 2351ad540459SPierre Jolivet for (j = 0; j < coordDim * coordDim; j++, k++) invJ[k] = invJ[j]; 2352dfccc68fSToby Isaac } 2353dfccc68fSToby Isaac } 2354dfccc68fSToby Isaac } 23553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2356dfccc68fSToby Isaac } 2357dfccc68fSToby Isaac 2358ccd2543fSMatthew G Knepley /*@C 23598e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell 2360ccd2543fSMatthew G Knepley 236120f4b53cSBarry Smith Collective 2362ccd2543fSMatthew G Knepley 23634165533cSJose E. Roman Input Parameters: 236420f4b53cSBarry Smith + dm - the `DMPLEX` 2365ccd2543fSMatthew G Knepley - cell - the cell 2366ccd2543fSMatthew G Knepley 23674165533cSJose E. Roman Output Parameters: 23689b172b3aSMatthew Knepley + v0 - the translation part of this affine transform, meaning the translation to the origin (not the first vertex of the reference cell) 2369ccd2543fSMatthew G Knepley . J - the Jacobian of the transform from the reference element 2370ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian 2371ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant 2372ccd2543fSMatthew G Knepley 2373ccd2543fSMatthew G Knepley Level: advanced 2374ccd2543fSMatthew G Knepley 237520f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeCellGeometryFEM()`, `DMGetCoordinateSection()`, `DMGetCoordinates()` 2376ccd2543fSMatthew G Knepley @*/ 2377d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 2378d71ae5a4SJacob Faibussowitsch { 2379ccd2543fSMatthew G Knepley PetscFunctionBegin; 23809566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, NULL, v0, J, invJ, detJ)); 23813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23828e0841e0SMatthew G. Knepley } 23838e0841e0SMatthew G. Knepley 2384d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 2385d71ae5a4SJacob Faibussowitsch { 23866858538eSMatthew G. Knepley const PetscScalar *array; 23878e0841e0SMatthew G. Knepley PetscScalar *coords = NULL; 23886858538eSMatthew G. Knepley PetscInt numCoords; 23896858538eSMatthew G. Knepley PetscBool isDG; 23906858538eSMatthew G. Knepley PetscQuadrature feQuad; 23918e0841e0SMatthew G. Knepley const PetscReal *quadPoints; 2392ef0bb6c7SMatthew G. Knepley PetscTabulation T; 23936858538eSMatthew G. Knepley PetscInt dim, cdim, pdim, qdim, Nq, q; 23948e0841e0SMatthew G. Knepley 23958e0841e0SMatthew G. Knepley PetscFunctionBegin; 23969566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 23979566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 23986858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords)); 2399dfccc68fSToby Isaac if (!quad) { /* use the first point of the first functional of the dual space */ 2400dfccc68fSToby Isaac PetscDualSpace dsp; 2401dfccc68fSToby Isaac 24029566063dSJacob Faibussowitsch PetscCall(PetscFEGetDualSpace(fe, &dsp)); 24039566063dSJacob Faibussowitsch PetscCall(PetscDualSpaceGetFunctional(dsp, 0, &quad)); 24049566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL)); 2405dfccc68fSToby Isaac Nq = 1; 2406dfccc68fSToby Isaac } else { 24079566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL)); 2408dfccc68fSToby Isaac } 24099566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 24109566063dSJacob Faibussowitsch PetscCall(PetscFEGetQuadrature(fe, &feQuad)); 2411dfccc68fSToby Isaac if (feQuad == quad) { 24129566063dSJacob Faibussowitsch PetscCall(PetscFEGetCellTabulation(fe, J ? 1 : 0, &T)); 241363a3b9bcSJacob 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); 2414dfccc68fSToby Isaac } else { 24159566063dSJacob Faibussowitsch PetscCall(PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T)); 2416dfccc68fSToby Isaac } 241763a3b9bcSJacob Faibussowitsch PetscCheck(qdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %" PetscInt_FMT " != quadrature dimension %" PetscInt_FMT, dim, qdim); 2418ef0bb6c7SMatthew G. Knepley { 2419ef0bb6c7SMatthew G. Knepley const PetscReal *basis = T->T[0]; 2420ef0bb6c7SMatthew G. Knepley const PetscReal *basisDer = T->T[1]; 2421ef0bb6c7SMatthew G. Knepley PetscReal detJt; 2422ef0bb6c7SMatthew G. Knepley 2423b498ca8aSPierre Jolivet PetscAssert(Nq == T->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %" PetscInt_FMT " != %" PetscInt_FMT, Nq, T->Np); 2424b498ca8aSPierre Jolivet PetscAssert(pdim == T->Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %" PetscInt_FMT " != %" PetscInt_FMT, pdim, T->Nb); 2425b498ca8aSPierre Jolivet PetscAssert(dim == T->Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %" PetscInt_FMT " != %" PetscInt_FMT, dim, T->Nc); 2426b498ca8aSPierre Jolivet PetscAssert(cdim == T->cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %" PetscInt_FMT " != %" PetscInt_FMT, cdim, T->cdim); 2427dfccc68fSToby Isaac if (v) { 24289566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(v, Nq * cdim)); 2429f960e424SToby Isaac for (q = 0; q < Nq; ++q) { 2430f960e424SToby Isaac PetscInt i, k; 2431f960e424SToby Isaac 2432301b184aSMatthew G. Knepley for (k = 0; k < pdim; ++k) { 2433301b184aSMatthew G. Knepley const PetscInt vertex = k / cdim; 2434ad540459SPierre Jolivet for (i = 0; i < cdim; ++i) v[q * cdim + i] += basis[(q * pdim + k) * cdim + i] * PetscRealPart(coords[vertex * cdim + i]); 2435301b184aSMatthew G. Knepley } 24369566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * pdim * cdim)); 2437f960e424SToby Isaac } 2438f960e424SToby Isaac } 24398e0841e0SMatthew G. Knepley if (J) { 24409566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(J, Nq * cdim * cdim)); 24418e0841e0SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 24428e0841e0SMatthew G. Knepley PetscInt i, j, k, c, r; 24438e0841e0SMatthew G. Knepley 24448e0841e0SMatthew G. Knepley /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */ 2445301b184aSMatthew G. Knepley for (k = 0; k < pdim; ++k) { 2446301b184aSMatthew G. Knepley const PetscInt vertex = k / cdim; 2447301b184aSMatthew G. Knepley for (j = 0; j < dim; ++j) { 2448ad540459SPierre 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]); 2449301b184aSMatthew G. Knepley } 2450301b184aSMatthew G. Knepley } 24519566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(2.0 * pdim * dim * cdim)); 24528e0841e0SMatthew G. Knepley if (cdim > dim) { 24538e0841e0SMatthew G. Knepley for (c = dim; c < cdim; ++c) 24549371c9d4SSatish Balay for (r = 0; r < cdim; ++r) J[r * cdim + c] = r == c ? 1.0 : 0.0; 24558e0841e0SMatthew G. Knepley } 2456f960e424SToby Isaac if (!detJ && !invJ) continue; 2457a63b72c6SToby Isaac detJt = 0.; 24588e0841e0SMatthew G. Knepley switch (cdim) { 24598e0841e0SMatthew G. Knepley case 3: 2460037dc194SToby Isaac DMPlex_Det3D_Internal(&detJt, &J[q * cdim * dim]); 2461ad540459SPierre Jolivet if (invJ) DMPlex_Invert3D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt); 246217fe8556SMatthew G. Knepley break; 246349dc4407SMatthew G. Knepley case 2: 24649f328543SToby Isaac DMPlex_Det2D_Internal(&detJt, &J[q * cdim * dim]); 2465ad540459SPierre Jolivet if (invJ) DMPlex_Invert2D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt); 246649dc4407SMatthew G. Knepley break; 24678e0841e0SMatthew G. Knepley case 1: 2468037dc194SToby Isaac detJt = J[q * cdim * dim]; 2469037dc194SToby Isaac if (invJ) invJ[q * cdim * dim] = 1.0 / detJt; 247049dc4407SMatthew G. Knepley } 2471f960e424SToby Isaac if (detJ) detJ[q] = detJt; 247249dc4407SMatthew G. Knepley } 247308401ef6SPierre Jolivet } else PetscCheck(!detJ && !invJ, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ"); 247449dc4407SMatthew G. Knepley } 24759566063dSJacob Faibussowitsch if (feQuad != quad) PetscCall(PetscTabulationDestroy(&T)); 24766858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords)); 24773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 24788e0841e0SMatthew G. Knepley } 24798e0841e0SMatthew G. Knepley 24808e0841e0SMatthew G. Knepley /*@C 24818e0841e0SMatthew G. Knepley DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell 24828e0841e0SMatthew G. Knepley 248320f4b53cSBarry Smith Collective 24848e0841e0SMatthew G. Knepley 24854165533cSJose E. Roman Input Parameters: 248620f4b53cSBarry Smith + dm - the `DMPLEX` 24878e0841e0SMatthew G. Knepley . cell - the cell 248820f4b53cSBarry Smith - quad - the quadrature containing the points in the reference element where the geometry will be evaluated. If `quad` is `NULL`, geometry will be 2489dfccc68fSToby Isaac evaluated at the first vertex of the reference element 24908e0841e0SMatthew G. Knepley 24914165533cSJose E. Roman Output Parameters: 2492dfccc68fSToby Isaac + v - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element 24938e0841e0SMatthew G. Knepley . J - the Jacobian of the transform from the reference element at each quadrature point 24948e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point 24958e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point 24968e0841e0SMatthew G. Knepley 24978e0841e0SMatthew G. Knepley Level: advanced 24988e0841e0SMatthew G. Knepley 249920f4b53cSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinateSection()`, `DMGetCoordinates()` 25008e0841e0SMatthew G. Knepley @*/ 2501d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 2502d71ae5a4SJacob Faibussowitsch { 2503bb4a5db5SMatthew G. Knepley DM cdm; 2504dfccc68fSToby Isaac PetscFE fe = NULL; 25058e0841e0SMatthew G. Knepley 25068e0841e0SMatthew G. Knepley PetscFunctionBegin; 25074f572ea9SToby Isaac PetscAssertPointer(detJ, 7); 25089566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 2509bb4a5db5SMatthew G. Knepley if (cdm) { 2510dfccc68fSToby Isaac PetscClassId id; 2511dfccc68fSToby Isaac PetscInt numFields; 2512e5e52638SMatthew G. Knepley PetscDS prob; 2513dfccc68fSToby Isaac PetscObject disc; 2514dfccc68fSToby Isaac 25159566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(cdm, &numFields)); 2516dfccc68fSToby Isaac if (numFields) { 25179566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &prob)); 25189566063dSJacob Faibussowitsch PetscCall(PetscDSGetDiscretization(prob, 0, &disc)); 25199566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 2520ad540459SPierre Jolivet if (id == PETSCFE_CLASSID) fe = (PetscFE)disc; 2521dfccc68fSToby Isaac } 2522dfccc68fSToby Isaac } 25239566063dSJacob Faibussowitsch if (!fe) PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ)); 25249566063dSJacob Faibussowitsch else PetscCall(DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ)); 25253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2526ccd2543fSMatthew G Knepley } 2527834e62ceSMatthew G. Knepley 2528d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2529d71ae5a4SJacob Faibussowitsch { 25309bf2564aSMatt McGurn PetscSection coordSection; 25319bf2564aSMatt McGurn Vec coordinates; 25329bf2564aSMatt McGurn const PetscScalar *coords = NULL; 25339bf2564aSMatt McGurn PetscInt d, dof, off; 25349bf2564aSMatt McGurn 25359bf2564aSMatt McGurn PetscFunctionBegin; 25369566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 25379566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 25389566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(coordinates, &coords)); 25399bf2564aSMatt McGurn 25409bf2564aSMatt McGurn /* for a point the centroid is just the coord */ 25419bf2564aSMatt McGurn if (centroid) { 25429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, cell, &dof)); 25439566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, cell, &off)); 2544ad540459SPierre Jolivet for (d = 0; d < dof; d++) centroid[d] = PetscRealPart(coords[off + d]); 25459bf2564aSMatt McGurn } 25469bf2564aSMatt McGurn if (normal) { 25479bf2564aSMatt McGurn const PetscInt *support, *cones; 25489bf2564aSMatt McGurn PetscInt supportSize; 25499bf2564aSMatt McGurn PetscReal norm, sign; 25509bf2564aSMatt McGurn 25519bf2564aSMatt McGurn /* compute the norm based upon the support centroids */ 25529566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, cell, &supportSize)); 25539566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, cell, &support)); 25549566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL)); 25559bf2564aSMatt McGurn 25569bf2564aSMatt McGurn /* Take the normal from the centroid of the support to the vertex*/ 25579566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, cell, &dof)); 25589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, cell, &off)); 2559ad540459SPierre Jolivet for (d = 0; d < dof; d++) normal[d] -= PetscRealPart(coords[off + d]); 25609bf2564aSMatt McGurn 25619bf2564aSMatt McGurn /* Determine the sign of the normal based upon its location in the support */ 25629566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, support[0], &cones)); 25639bf2564aSMatt McGurn sign = cones[0] == cell ? 1.0 : -1.0; 25649bf2564aSMatt McGurn 25659bf2564aSMatt McGurn norm = DMPlex_NormD_Internal(dim, normal); 25669bf2564aSMatt McGurn for (d = 0; d < dim; ++d) normal[d] /= (norm * sign); 25679bf2564aSMatt McGurn } 2568ad540459SPierre Jolivet if (vol) *vol = 1.0; 25699566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(coordinates, &coords)); 25703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 25719bf2564aSMatt McGurn } 25729bf2564aSMatt McGurn 2573d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2574d71ae5a4SJacob Faibussowitsch { 25756858538eSMatthew G. Knepley const PetscScalar *array; 2576a1e44745SMatthew G. Knepley PetscScalar *coords = NULL; 257721d6a034SMatthew G. Knepley PetscInt cdim, coordSize, d; 25786858538eSMatthew G. Knepley PetscBool isDG; 2579cc08537eSMatthew G. Knepley 2580cc08537eSMatthew G. Knepley PetscFunctionBegin; 258121d6a034SMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 25826858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 258321d6a034SMatthew G. Knepley PetscCheck(coordSize == cdim * 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Edge has %" PetscInt_FMT " coordinates != %" PetscInt_FMT, coordSize, cdim * 2); 2584cc08537eSMatthew G. Knepley if (centroid) { 258521d6a034SMatthew G. Knepley for (d = 0; d < cdim; ++d) centroid[d] = 0.5 * PetscRealPart(coords[d] + coords[cdim + d]); 2586cc08537eSMatthew G. Knepley } 2587cc08537eSMatthew G. Knepley if (normal) { 2588a60a936bSMatthew G. Knepley PetscReal norm; 2589a60a936bSMatthew G. Knepley 259021d6a034SMatthew G. Knepley switch (cdim) { 259121d6a034SMatthew G. Knepley case 3: 2592f315e28eSPierre Jolivet normal[2] = 0.; /* fall through */ 259321d6a034SMatthew G. Knepley case 2: 259421d6a034SMatthew G. Knepley normal[0] = -PetscRealPart(coords[1] - coords[cdim + 1]); 259521d6a034SMatthew G. Knepley normal[1] = PetscRealPart(coords[0] - coords[cdim + 0]); 259621d6a034SMatthew G. Knepley break; 259721d6a034SMatthew G. Knepley case 1: 259821d6a034SMatthew G. Knepley normal[0] = 1.0; 259921d6a034SMatthew G. Knepley break; 260021d6a034SMatthew G. Knepley default: 260121d6a034SMatthew G. Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Dimension %" PetscInt_FMT " not supported", cdim); 260221d6a034SMatthew G. Knepley } 260321d6a034SMatthew G. Knepley norm = DMPlex_NormD_Internal(cdim, normal); 260421d6a034SMatthew G. Knepley for (d = 0; d < cdim; ++d) normal[d] /= norm; 2605cc08537eSMatthew G. Knepley } 2606cc08537eSMatthew G. Knepley if (vol) { 2607714b99b6SMatthew G. Knepley *vol = 0.0; 260821d6a034SMatthew G. Knepley for (d = 0; d < cdim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - coords[cdim + d])); 2609714b99b6SMatthew G. Knepley *vol = PetscSqrtReal(*vol); 2610cc08537eSMatthew G. Knepley } 26116858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 26123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2613cc08537eSMatthew G. Knepley } 2614cc08537eSMatthew G. Knepley 2615cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */ 2616d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2617d71ae5a4SJacob Faibussowitsch { 2618412e9a14SMatthew G. Knepley DMPolytopeType ct; 26196858538eSMatthew G. Knepley const PetscScalar *array; 2620cc08537eSMatthew G. Knepley PetscScalar *coords = NULL; 26216858538eSMatthew G. Knepley PetscInt coordSize; 26226858538eSMatthew G. Knepley PetscBool isDG; 2623793a2a13SMatthew G. Knepley PetscInt fv[4] = {0, 1, 2, 3}; 26246858538eSMatthew G. Knepley PetscInt cdim, numCorners, p, d; 2625cc08537eSMatthew G. Knepley 2626cc08537eSMatthew G. Knepley PetscFunctionBegin; 2627793a2a13SMatthew G. Knepley /* Must check for hybrid cells because prisms have a different orientation scheme */ 26289566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2629412e9a14SMatthew G. Knepley switch (ct) { 26309371c9d4SSatish Balay case DM_POLYTOPE_SEG_PRISM_TENSOR: 26319371c9d4SSatish Balay fv[2] = 3; 26329371c9d4SSatish Balay fv[3] = 2; 26339371c9d4SSatish Balay break; 2634d71ae5a4SJacob Faibussowitsch default: 2635d71ae5a4SJacob Faibussowitsch break; 2636412e9a14SMatthew G. Knepley } 26379566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 26386858538eSMatthew G. Knepley PetscCall(DMPlexGetConeSize(dm, cell, &numCorners)); 26396858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 26403f27a4e6SJed Brown { 26413f27a4e6SJed Brown PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm; 2642793a2a13SMatthew G. Knepley 26433f27a4e6SJed Brown for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]); 26444f99dae5SMatthew G. Knepley for (p = 0; p < numCorners - 2; ++p) { 26453f27a4e6SJed Brown PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.}; 26463f27a4e6SJed Brown for (d = 0; d < cdim; d++) { 26473f27a4e6SJed Brown e0[d] = PetscRealPart(coords[cdim * fv[p + 1] + d]) - origin[d]; 26483f27a4e6SJed Brown e1[d] = PetscRealPart(coords[cdim * fv[p + 2] + d]) - origin[d]; 26493f27a4e6SJed Brown } 26503f27a4e6SJed Brown const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1]; 26513f27a4e6SJed Brown const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2]; 26523f27a4e6SJed Brown const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0]; 26533f27a4e6SJed Brown const PetscReal a = PetscSqrtReal(dx * dx + dy * dy + dz * dz); 26544f99dae5SMatthew G. Knepley 26554f99dae5SMatthew G. Knepley n[0] += dx; 26564f99dae5SMatthew G. Knepley n[1] += dy; 26574f99dae5SMatthew G. Knepley n[2] += dz; 2658ad540459SPierre 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.; 2659ceee4971SMatthew G. Knepley } 26604f99dae5SMatthew G. Knepley norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]); 266161451c10SMatthew G. Knepley // Allow zero volume cells 266261451c10SMatthew G. Knepley if (norm != 0) { 26634f99dae5SMatthew G. Knepley n[0] /= norm; 26644f99dae5SMatthew G. Knepley n[1] /= norm; 26654f99dae5SMatthew G. Knepley n[2] /= norm; 26664f99dae5SMatthew G. Knepley c[0] /= norm; 26674f99dae5SMatthew G. Knepley c[1] /= norm; 26684f99dae5SMatthew G. Knepley c[2] /= norm; 266961451c10SMatthew G. Knepley } 26704f99dae5SMatthew G. Knepley if (vol) *vol = 0.5 * norm; 26719371c9d4SSatish Balay if (centroid) 26729371c9d4SSatish Balay for (d = 0; d < cdim; ++d) centroid[d] = c[d]; 26739371c9d4SSatish Balay if (normal) 26749371c9d4SSatish Balay for (d = 0; d < cdim; ++d) normal[d] = n[d]; 26750a1d6728SMatthew G. Knepley } 26766858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 26773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2678cc08537eSMatthew G. Knepley } 2679cc08537eSMatthew G. Knepley 26800ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */ 2681d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2682d71ae5a4SJacob Faibussowitsch { 2683412e9a14SMatthew G. Knepley DMPolytopeType ct; 26846858538eSMatthew G. Knepley const PetscScalar *array; 26850ec8681fSMatthew G. Knepley PetscScalar *coords = NULL; 26866858538eSMatthew G. Knepley PetscInt coordSize; 26876858538eSMatthew G. Knepley PetscBool isDG; 26883f27a4e6SJed Brown PetscReal vsum = 0.0, vtmp, coordsTmp[3 * 3], origin[3]; 26896858538eSMatthew G. Knepley const PetscInt order[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; 26906858538eSMatthew G. Knepley const PetscInt *cone, *faceSizes, *faces; 26916858538eSMatthew G. Knepley const DMPolytopeType *faceTypes; 2692793a2a13SMatthew G. Knepley PetscBool isHybrid = PETSC_FALSE; 26936858538eSMatthew G. Knepley PetscInt numFaces, f, fOff = 0, p, d; 26940ec8681fSMatthew G. Knepley 26950ec8681fSMatthew G. Knepley PetscFunctionBegin; 269663a3b9bcSJacob Faibussowitsch PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No support for dim %" PetscInt_FMT " > 3", dim); 2697793a2a13SMatthew G. Knepley /* Must check for hybrid cells because prisms have a different orientation scheme */ 26989566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cell, &ct)); 2699412e9a14SMatthew G. Knepley switch (ct) { 2700412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 2701412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 2702412e9a14SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM_TENSOR: 2703d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_QUAD_PRISM_TENSOR: 2704d71ae5a4SJacob Faibussowitsch isHybrid = PETSC_TRUE; 2705d71ae5a4SJacob Faibussowitsch default: 2706d71ae5a4SJacob Faibussowitsch break; 2707412e9a14SMatthew G. Knepley } 2708793a2a13SMatthew G. Knepley 27099371c9d4SSatish Balay if (centroid) 27109371c9d4SSatish Balay for (d = 0; d < dim; ++d) centroid[d] = 0.0; 27116858538eSMatthew G. Knepley PetscCall(DMPlexGetCone(dm, cell, &cone)); 27126858538eSMatthew G. Knepley 27136858538eSMatthew G. Knepley // Using the closure of faces for coordinates does not work in periodic geometries, so we index into the cell coordinates 27146858538eSMatthew G. Knepley PetscCall(DMPlexGetRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces)); 27156858538eSMatthew G. Knepley PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 27160ec8681fSMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 2717793a2a13SMatthew G. Knepley PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */ 2718793a2a13SMatthew G. Knepley 27193f27a4e6SJed Brown // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and 27203f27a4e6SJed Brown // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex 27213f27a4e6SJed Brown // so that all tetrahedra have positive volume. 27229371c9d4SSatish Balay if (f == 0) 27239371c9d4SSatish Balay for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]); 27246858538eSMatthew G. Knepley switch (faceTypes[f]) { 2725ba2698f1SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 27260ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 27276858538eSMatthew G. Knepley coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + 0] * dim + d]) - origin[d]; 27286858538eSMatthew G. Knepley coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + 1] * dim + d]) - origin[d]; 27296858538eSMatthew G. Knepley coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + 2] * dim + d]) - origin[d]; 27300ec8681fSMatthew G. Knepley } 27310ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 27326858538eSMatthew G. Knepley if (flip) vtmp = -vtmp; 27330ec8681fSMatthew G. Knepley vsum += vtmp; 27344f25033aSJed Brown if (centroid) { /* Centroid of OABC = (a+b+c)/4 */ 27350ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 27361ee9d5ecSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp; 27370ec8681fSMatthew G. Knepley } 27380ec8681fSMatthew G. Knepley } 27390ec8681fSMatthew G. Knepley break; 2740ba2698f1SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 27419371c9d4SSatish Balay case DM_POLYTOPE_SEG_PRISM_TENSOR: { 2742793a2a13SMatthew G. Knepley PetscInt fv[4] = {0, 1, 2, 3}; 2743793a2a13SMatthew G. Knepley 274415229ffcSPierre Jolivet /* Side faces for hybrid cells are stored as tensor products */ 27459371c9d4SSatish Balay if (isHybrid && f > 1) { 27469371c9d4SSatish Balay fv[2] = 3; 27479371c9d4SSatish Balay fv[3] = 2; 27489371c9d4SSatish Balay } 27490ec8681fSMatthew G. Knepley /* DO FOR PYRAMID */ 27500ec8681fSMatthew G. Knepley /* First tet */ 27510ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 27526858538eSMatthew G. Knepley coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[0]] * dim + d]) - origin[d]; 27536858538eSMatthew G. Knepley coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d]; 27546858538eSMatthew G. Knepley coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d]; 27550ec8681fSMatthew G. Knepley } 27560ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 27576858538eSMatthew G. Knepley if (flip) vtmp = -vtmp; 27580ec8681fSMatthew G. Knepley vsum += vtmp; 27590ec8681fSMatthew G. Knepley if (centroid) { 27600ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 27610ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp; 27620ec8681fSMatthew G. Knepley } 27630ec8681fSMatthew G. Knepley } 27640ec8681fSMatthew G. Knepley /* Second tet */ 27650ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 27666858538eSMatthew G. Knepley coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d]; 27676858538eSMatthew G. Knepley coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[2]] * dim + d]) - origin[d]; 27686858538eSMatthew G. Knepley coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d]; 27690ec8681fSMatthew G. Knepley } 27700ec8681fSMatthew G. Knepley Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 27716858538eSMatthew G. Knepley if (flip) vtmp = -vtmp; 27720ec8681fSMatthew G. Knepley vsum += vtmp; 27730ec8681fSMatthew G. Knepley if (centroid) { 27740ec8681fSMatthew G. Knepley for (d = 0; d < dim; ++d) { 27750ec8681fSMatthew G. Knepley for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp; 27760ec8681fSMatthew G. Knepley } 27770ec8681fSMatthew G. Knepley } 27780ec8681fSMatthew G. Knepley break; 2779793a2a13SMatthew G. Knepley } 2780d71ae5a4SJacob Faibussowitsch default: 2781d71ae5a4SJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %" PetscInt_FMT " of type %s", cone[f], DMPolytopeTypes[ct]); 27820ec8681fSMatthew G. Knepley } 27836858538eSMatthew G. Knepley fOff += faceSizes[f]; 27840ec8681fSMatthew G. Knepley } 27856858538eSMatthew G. Knepley PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces)); 27866858538eSMatthew G. Knepley PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords)); 27878763be8eSMatthew G. Knepley if (vol) *vol = PetscAbsReal(vsum); 27889371c9d4SSatish Balay if (normal) 27899371c9d4SSatish Balay for (d = 0; d < dim; ++d) normal[d] = 0.0; 27909371c9d4SSatish Balay if (centroid) 27919371c9d4SSatish Balay for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum * 4) + origin[d]; 27923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 27930ec8681fSMatthew G. Knepley } 27940ec8681fSMatthew G. Knepley 2795834e62ceSMatthew G. Knepley /*@C 2796834e62ceSMatthew G. Knepley DMPlexComputeCellGeometryFVM - Compute the volume for a given cell 2797834e62ceSMatthew G. Knepley 279820f4b53cSBarry Smith Collective 2799834e62ceSMatthew G. Knepley 28004165533cSJose E. Roman Input Parameters: 280120f4b53cSBarry Smith + dm - the `DMPLEX` 2802834e62ceSMatthew G. Knepley - cell - the cell 2803834e62ceSMatthew G. Knepley 28044165533cSJose E. Roman Output Parameters: 280560225df5SJacob Faibussowitsch + vol - the cell volume 2806cc08537eSMatthew G. Knepley . centroid - the cell centroid 2807cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate 2808834e62ceSMatthew G. Knepley 2809834e62ceSMatthew G. Knepley Level: advanced 2810834e62ceSMatthew G. Knepley 281120f4b53cSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinateSection()`, `DMGetCoordinates()` 2812834e62ceSMatthew G. Knepley @*/ 2813d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 2814d71ae5a4SJacob Faibussowitsch { 28150ec8681fSMatthew G. Knepley PetscInt depth, dim; 2816834e62ceSMatthew G. Knepley 2817834e62ceSMatthew G. Knepley PetscFunctionBegin; 28189566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 28199566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 282008401ef6SPierre Jolivet PetscCheck(depth == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated"); 28219566063dSJacob Faibussowitsch PetscCall(DMPlexGetPointDepth(dm, cell, &depth)); 2822011ea5d8SMatthew G. Knepley switch (depth) { 2823d71ae5a4SJacob Faibussowitsch case 0: 2824d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal)); 2825d71ae5a4SJacob Faibussowitsch break; 2826d71ae5a4SJacob Faibussowitsch case 1: 2827d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal)); 2828d71ae5a4SJacob Faibussowitsch break; 2829d71ae5a4SJacob Faibussowitsch case 2: 2830d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal)); 2831d71ae5a4SJacob Faibussowitsch break; 2832d71ae5a4SJacob Faibussowitsch case 3: 2833d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal)); 2834d71ae5a4SJacob Faibussowitsch break; 2835d71ae5a4SJacob Faibussowitsch default: 2836d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT " (depth %" PetscInt_FMT ") for element geometry computation", dim, depth); 2837834e62ceSMatthew G. Knepley } 28383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2839834e62ceSMatthew G. Knepley } 2840113c68e6SMatthew G. Knepley 2841c501906fSMatthew G. Knepley /*@ 2842891a9168SMatthew G. Knepley DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method 2843891a9168SMatthew G. Knepley 2844891a9168SMatthew G. Knepley Input Parameter: 284520f4b53cSBarry Smith . dm - The `DMPLEX` 2846891a9168SMatthew G. Knepley 2847891a9168SMatthew G. Knepley Output Parameters: 284820f4b53cSBarry Smith + cellgeom - A `Vec` of `PetscFVCellGeom` data 284920f4b53cSBarry Smith - facegeom - A `Vec` of `PetscFVFaceGeom` data 2850891a9168SMatthew G. Knepley 2851891a9168SMatthew G. Knepley Level: developer 2852891a9168SMatthew G. Knepley 285320f4b53cSBarry Smith .seealso: `DMPLEX`, `PetscFVFaceGeom`, `PetscFVCellGeom` 2854891a9168SMatthew G. Knepley @*/ 2855d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) 2856d71ae5a4SJacob Faibussowitsch { 2857113c68e6SMatthew G. Knepley DM dmFace, dmCell; 2858113c68e6SMatthew G. Knepley DMLabel ghostLabel; 2859113c68e6SMatthew G. Knepley PetscSection sectionFace, sectionCell; 2860113c68e6SMatthew G. Knepley PetscSection coordSection; 2861113c68e6SMatthew G. Knepley Vec coordinates; 2862113c68e6SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 2863113c68e6SMatthew G. Knepley PetscReal minradius, gminradius; 2864113c68e6SMatthew G. Knepley PetscInt dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f; 2865113c68e6SMatthew G. Knepley 2866113c68e6SMatthew G. Knepley PetscFunctionBegin; 28679566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 28689566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 28699566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 2870113c68e6SMatthew G. Knepley /* Make cell centroids and volumes */ 28719566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmCell)); 28729566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection)); 28739566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmCell, coordinates)); 28749566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionCell)); 28759566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 28762827ebadSStefano Zampini PetscCall(DMPlexGetCellTypeStratum(dm, DM_POLYTOPE_FV_GHOST, &cEndInterior, NULL)); 28779566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd)); 28789566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVCellGeom)) / sizeof(PetscScalar)))); 28799566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionCell)); 28809566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmCell, sectionCell)); 28819566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionCell)); 28829566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmCell, cellgeom)); 2883485ad865SMatthew G. Knepley if (cEndInterior < 0) cEndInterior = cEnd; 28849566063dSJacob Faibussowitsch PetscCall(VecGetArray(*cellgeom, &cgeom)); 2885113c68e6SMatthew G. Knepley for (c = cStart; c < cEndInterior; ++c) { 2886113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 2887113c68e6SMatthew G. Knepley 28889566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg)); 28899566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(cg, 1)); 28909566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL)); 2891113c68e6SMatthew G. Knepley } 2892113c68e6SMatthew G. Knepley /* Compute face normals and minimum cell radius */ 28939566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dmFace)); 28949566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionFace)); 28959566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 28969566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionFace, fStart, fEnd)); 28979566063dSJacob Faibussowitsch for (f = fStart; f < fEnd; ++f) PetscCall(PetscSectionSetDof(sectionFace, f, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVFaceGeom)) / sizeof(PetscScalar)))); 28989566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionFace)); 28999566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dmFace, sectionFace)); 29009566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionFace)); 29019566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dmFace, facegeom)); 29029566063dSJacob Faibussowitsch PetscCall(VecGetArray(*facegeom, &fgeom)); 29039566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 2904113c68e6SMatthew G. Knepley minradius = PETSC_MAX_REAL; 2905113c68e6SMatthew G. Knepley for (f = fStart; f < fEnd; ++f) { 2906113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 2907113c68e6SMatthew G. Knepley PetscReal area; 2908412e9a14SMatthew G. Knepley const PetscInt *cells; 2909412e9a14SMatthew G. Knepley PetscInt ncells, ghost = -1, d, numChildren; 2910113c68e6SMatthew G. Knepley 29119566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 29129566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 29139566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &cells)); 29149566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &ncells)); 2915412e9a14SMatthew G. Knepley /* It is possible to get a face with no support when using partition overlap */ 2916412e9a14SMatthew G. Knepley if (!ncells || ghost >= 0 || numChildren) continue; 29179566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, f, fgeom, &fg)); 29189566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal)); 2919113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] *= area; 2920113c68e6SMatthew G. Knepley /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */ 2921113c68e6SMatthew G. Knepley { 2922113c68e6SMatthew G. Knepley PetscFVCellGeom *cL, *cR; 2923113c68e6SMatthew G. Knepley PetscReal *lcentroid, *rcentroid; 29240453c0cdSMatthew G. Knepley PetscReal l[3], r[3], v[3]; 2925113c68e6SMatthew G. Knepley 29269566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL)); 2927113c68e6SMatthew G. Knepley lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid; 292806348e87SToby Isaac if (ncells > 1) { 29299566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR)); 2930113c68e6SMatthew G. Knepley rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid; 29319371c9d4SSatish Balay } else { 293206348e87SToby Isaac rcentroid = fg->centroid; 293306348e87SToby Isaac } 29349566063dSJacob Faibussowitsch PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l)); 29359566063dSJacob Faibussowitsch PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r)); 29360453c0cdSMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, l, r, v); 2937113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) { 2938113c68e6SMatthew G. Knepley for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d]; 2939113c68e6SMatthew G. Knepley } 2940113c68e6SMatthew G. Knepley if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) { 294163a3b9bcSJacob 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]); 294263a3b9bcSJacob 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]); 294363a3b9bcSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " could not be fixed", f); 2944113c68e6SMatthew G. Knepley } 2945113c68e6SMatthew G. Knepley if (cells[0] < cEndInterior) { 2946113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v); 2947113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2948113c68e6SMatthew G. Knepley } 294906348e87SToby Isaac if (ncells > 1 && cells[1] < cEndInterior) { 2950113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v); 2951113c68e6SMatthew G. Knepley minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 2952113c68e6SMatthew G. Knepley } 2953113c68e6SMatthew G. Knepley } 2954113c68e6SMatthew G. Knepley } 2955462c564dSBarry Smith PetscCallMPI(MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm))); 29569566063dSJacob Faibussowitsch PetscCall(DMPlexSetMinRadius(dm, gminradius)); 2957113c68e6SMatthew G. Knepley /* Compute centroids of ghost cells */ 2958113c68e6SMatthew G. Knepley for (c = cEndInterior; c < cEnd; ++c) { 2959113c68e6SMatthew G. Knepley PetscFVFaceGeom *fg; 2960113c68e6SMatthew G. Knepley const PetscInt *cone, *support; 2961113c68e6SMatthew G. Knepley PetscInt coneSize, supportSize, s; 2962113c68e6SMatthew G. Knepley 29639566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dmCell, c, &coneSize)); 296463a3b9bcSJacob Faibussowitsch PetscCheck(coneSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %" PetscInt_FMT " has cone size %" PetscInt_FMT " != 1", c, coneSize); 29659566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dmCell, c, &cone)); 29669566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dmCell, cone[0], &supportSize)); 296763a3b9bcSJacob Faibussowitsch PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " has support size %" PetscInt_FMT " != 2", cone[0], supportSize); 29689566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dmCell, cone[0], &support)); 29699566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg)); 2970113c68e6SMatthew G. Knepley for (s = 0; s < 2; ++s) { 2971113c68e6SMatthew G. Knepley /* Reflect ghost centroid across plane of face */ 2972113c68e6SMatthew G. Knepley if (support[s] == c) { 2973640bce14SSatish Balay PetscFVCellGeom *ci; 2974113c68e6SMatthew G. Knepley PetscFVCellGeom *cg; 2975113c68e6SMatthew G. Knepley PetscReal c2f[3], a; 2976113c68e6SMatthew G. Knepley 29779566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, support[(s + 1) % 2], cgeom, &ci)); 2978113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */ 2979113c68e6SMatthew G. Knepley a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal) / DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal); 29809566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg)); 2981113c68e6SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, 2 * a, fg->normal, ci->centroid, cg->centroid); 2982113c68e6SMatthew G. Knepley cg->volume = ci->volume; 2983113c68e6SMatthew G. Knepley } 2984113c68e6SMatthew G. Knepley } 2985113c68e6SMatthew G. Knepley } 29869566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(*facegeom, &fgeom)); 29879566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(*cellgeom, &cgeom)); 29889566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmCell)); 29899566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmFace)); 29903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2991113c68e6SMatthew G. Knepley } 2992113c68e6SMatthew G. Knepley 2993cc4c1da9SBarry Smith /*@ 2994113c68e6SMatthew G. Knepley DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face 2995113c68e6SMatthew G. Knepley 299620f4b53cSBarry Smith Not Collective 2997113c68e6SMatthew G. Knepley 29984165533cSJose E. Roman Input Parameter: 299920f4b53cSBarry Smith . dm - the `DMPLEX` 3000113c68e6SMatthew G. Knepley 30014165533cSJose E. Roman Output Parameter: 3002a5b23f4aSJose E. Roman . minradius - the minimum cell radius 3003113c68e6SMatthew G. Knepley 3004113c68e6SMatthew G. Knepley Level: developer 3005113c68e6SMatthew G. Knepley 300620f4b53cSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinates()` 3007113c68e6SMatthew G. Knepley @*/ 3008d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) 3009d71ae5a4SJacob Faibussowitsch { 3010113c68e6SMatthew G. Knepley PetscFunctionBegin; 3011113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 30124f572ea9SToby Isaac PetscAssertPointer(minradius, 2); 3013113c68e6SMatthew G. Knepley *minradius = ((DM_Plex *)dm->data)->minradius; 30143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3015113c68e6SMatthew G. Knepley } 3016113c68e6SMatthew G. Knepley 3017cc4c1da9SBarry Smith /*@ 3018113c68e6SMatthew G. Knepley DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face 3019113c68e6SMatthew G. Knepley 302020f4b53cSBarry Smith Logically Collective 3021113c68e6SMatthew G. Knepley 30224165533cSJose E. Roman Input Parameters: 302320f4b53cSBarry Smith + dm - the `DMPLEX` 3024a5b23f4aSJose E. Roman - minradius - the minimum cell radius 3025113c68e6SMatthew G. Knepley 3026113c68e6SMatthew G. Knepley Level: developer 3027113c68e6SMatthew G. Knepley 302820f4b53cSBarry Smith .seealso: `DMPLEX`, `DMSetCoordinates()` 3029113c68e6SMatthew G. Knepley @*/ 3030d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) 3031d71ae5a4SJacob Faibussowitsch { 3032113c68e6SMatthew G. Knepley PetscFunctionBegin; 3033113c68e6SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3034113c68e6SMatthew G. Knepley ((DM_Plex *)dm->data)->minradius = minradius; 30353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3036113c68e6SMatthew G. Knepley } 3037856ac710SMatthew G. Knepley 3038d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 3039d71ae5a4SJacob Faibussowitsch { 3040856ac710SMatthew G. Knepley DMLabel ghostLabel; 3041856ac710SMatthew G. Knepley PetscScalar *dx, *grad, **gref; 3042856ac710SMatthew G. Knepley PetscInt dim, cStart, cEnd, c, cEndInterior, maxNumFaces; 3043856ac710SMatthew G. Knepley 3044856ac710SMatthew G. Knepley PetscFunctionBegin; 30459566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 30469566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 30472827ebadSStefano Zampini PetscCall(DMPlexGetCellTypeStratum(dm, DM_POLYTOPE_FV_GHOST, &cEndInterior, NULL)); 3048089217ebSMatthew G. Knepley cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior; 30499566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxNumFaces, NULL)); 30509566063dSJacob Faibussowitsch PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces)); 30519566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 30529566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref)); 3053856ac710SMatthew G. Knepley for (c = cStart; c < cEndInterior; c++) { 3054856ac710SMatthew G. Knepley const PetscInt *faces; 3055856ac710SMatthew G. Knepley PetscInt numFaces, usedFaces, f, d; 3056640bce14SSatish Balay PetscFVCellGeom *cg; 3057856ac710SMatthew G. Knepley PetscBool boundary; 3058856ac710SMatthew G. Knepley PetscInt ghost; 3059856ac710SMatthew G. Knepley 3060a79418b7SMatt McGurn // do not attempt to compute a gradient reconstruction stencil in a ghost cell. It will never be used 3061a79418b7SMatt McGurn PetscCall(DMLabelGetValue(ghostLabel, c, &ghost)); 3062a79418b7SMatt McGurn if (ghost >= 0) continue; 3063a79418b7SMatt McGurn 30649566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg)); 30659566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, c, &numFaces)); 30669566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, c, &faces)); 306763a3b9bcSJacob 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); 3068856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 3069640bce14SSatish Balay PetscFVCellGeom *cg1; 3070856ac710SMatthew G. Knepley PetscFVFaceGeom *fg; 3071856ac710SMatthew G. Knepley const PetscInt *fcells; 3072856ac710SMatthew G. Knepley PetscInt ncell, side; 3073856ac710SMatthew G. Knepley 30749566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost)); 30759566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary)); 3076856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 30779566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, faces[f], &fcells)); 3078856ac710SMatthew G. Knepley side = (c != fcells[0]); /* c is on left=0 or right=1 of face */ 3079856ac710SMatthew G. Knepley ncell = fcells[!side]; /* the neighbor */ 30809566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg)); 30819566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1)); 3082856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) dx[usedFaces * dim + d] = cg1->centroid[d] - cg->centroid[d]; 3083856ac710SMatthew G. Knepley gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */ 3084856ac710SMatthew G. Knepley } 308528b400f6SJacob Faibussowitsch PetscCheck(usedFaces, PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?"); 30869566063dSJacob Faibussowitsch PetscCall(PetscFVComputeGradient(fvm, usedFaces, dx, grad)); 3087856ac710SMatthew G. Knepley for (f = 0, usedFaces = 0; f < numFaces; ++f) { 30889566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost)); 30899566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary)); 3090856ac710SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 3091856ac710SMatthew G. Knepley for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces * dim + d]; 3092856ac710SMatthew G. Knepley ++usedFaces; 3093856ac710SMatthew G. Knepley } 3094856ac710SMatthew G. Knepley } 30959566063dSJacob Faibussowitsch PetscCall(PetscFree3(dx, grad, gref)); 30963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3097856ac710SMatthew G. Knepley } 3098856ac710SMatthew G. Knepley 3099d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 3100d71ae5a4SJacob Faibussowitsch { 3101b81db932SToby Isaac DMLabel ghostLabel; 3102b81db932SToby Isaac PetscScalar *dx, *grad, **gref; 3103b81db932SToby Isaac PetscInt dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0; 3104b81db932SToby Isaac PetscSection neighSec; 3105b81db932SToby Isaac PetscInt(*neighbors)[2]; 3106b81db932SToby Isaac PetscInt *counter; 3107b81db932SToby Isaac 3108b81db932SToby Isaac PetscFunctionBegin; 31099566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 31109566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 31112827ebadSStefano Zampini PetscCall(DMPlexGetCellTypeStratum(dm, DM_POLYTOPE_FV_GHOST, &cEndInterior, NULL)); 3112485ad865SMatthew G. Knepley if (cEndInterior < 0) cEndInterior = cEnd; 31139566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &neighSec)); 31149566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(neighSec, cStart, cEndInterior)); 31159566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 31169566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "ghost", &ghostLabel)); 3117b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 3118b81db932SToby Isaac const PetscInt *fcells; 3119b81db932SToby Isaac PetscBool boundary; 31205bc680faSToby Isaac PetscInt ghost = -1; 3121b81db932SToby Isaac PetscInt numChildren, numCells, c; 3122b81db932SToby Isaac 31239566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 31249566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, f, &boundary)); 31259566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 3126b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 31279566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &numCells)); 312806348e87SToby Isaac if (numCells == 2) { 31299566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &fcells)); 3130b81db932SToby Isaac for (c = 0; c < 2; c++) { 3131b81db932SToby Isaac PetscInt cell = fcells[c]; 3132b81db932SToby Isaac 313348a46eb9SPierre Jolivet if (cell >= cStart && cell < cEndInterior) PetscCall(PetscSectionAddDof(neighSec, cell, 1)); 3134b81db932SToby Isaac } 3135b81db932SToby Isaac } 313606348e87SToby Isaac } 31379566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(neighSec)); 31389566063dSJacob Faibussowitsch PetscCall(PetscSectionGetMaxDof(neighSec, &maxNumFaces)); 31399566063dSJacob Faibussowitsch PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces)); 3140b81db932SToby Isaac nStart = 0; 31419566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(neighSec, &nEnd)); 3142*57508eceSPierre Jolivet PetscCall(PetscMalloc1(nEnd - nStart, &neighbors)); 3143*57508eceSPierre Jolivet PetscCall(PetscCalloc1(cEndInterior - cStart, &counter)); 3144b81db932SToby Isaac for (f = fStart; f < fEnd; f++) { 3145b81db932SToby Isaac const PetscInt *fcells; 3146b81db932SToby Isaac PetscBool boundary; 31475bc680faSToby Isaac PetscInt ghost = -1; 3148b81db932SToby Isaac PetscInt numChildren, numCells, c; 3149b81db932SToby Isaac 31509566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost)); 31519566063dSJacob Faibussowitsch PetscCall(DMIsBoundaryPoint(dm, f, &boundary)); 31529566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL)); 3153b81db932SToby Isaac if ((ghost >= 0) || boundary || numChildren) continue; 31549566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &numCells)); 315506348e87SToby Isaac if (numCells == 2) { 31569566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, f, &fcells)); 3157b81db932SToby Isaac for (c = 0; c < 2; c++) { 3158b81db932SToby Isaac PetscInt cell = fcells[c], off; 3159b81db932SToby Isaac 3160e6885bbbSToby Isaac if (cell >= cStart && cell < cEndInterior) { 31619566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(neighSec, cell, &off)); 3162b81db932SToby Isaac off += counter[cell - cStart]++; 3163b81db932SToby Isaac neighbors[off][0] = f; 3164b81db932SToby Isaac neighbors[off][1] = fcells[1 - c]; 3165b81db932SToby Isaac } 3166b81db932SToby Isaac } 3167b81db932SToby Isaac } 316806348e87SToby Isaac } 31699566063dSJacob Faibussowitsch PetscCall(PetscFree(counter)); 31709566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref)); 3171b81db932SToby Isaac for (c = cStart; c < cEndInterior; c++) { 3172317218b9SToby Isaac PetscInt numFaces, f, d, off, ghost = -1; 3173640bce14SSatish Balay PetscFVCellGeom *cg; 3174b81db932SToby Isaac 31759566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg)); 31769566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(neighSec, c, &numFaces)); 31779566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(neighSec, c, &off)); 3178a79418b7SMatt McGurn 3179a79418b7SMatt McGurn // do not attempt to compute a gradient reconstruction stencil in a ghost cell. It will never be used 31809566063dSJacob Faibussowitsch if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, c, &ghost)); 3181a79418b7SMatt McGurn if (ghost >= 0) continue; 3182a79418b7SMatt McGurn 318363a3b9bcSJacob 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); 3184b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 3185640bce14SSatish Balay PetscFVCellGeom *cg1; 3186b81db932SToby Isaac PetscFVFaceGeom *fg; 3187b81db932SToby Isaac const PetscInt *fcells; 3188b81db932SToby Isaac PetscInt ncell, side, nface; 3189b81db932SToby Isaac 3190b81db932SToby Isaac nface = neighbors[off + f][0]; 3191b81db932SToby Isaac ncell = neighbors[off + f][1]; 31929566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, nface, &fcells)); 3193b81db932SToby Isaac side = (c != fcells[0]); 31949566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRef(dmFace, nface, fgeom, &fg)); 31959566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1)); 3196b81db932SToby Isaac for (d = 0; d < dim; ++d) dx[f * dim + d] = cg1->centroid[d] - cg->centroid[d]; 3197b81db932SToby Isaac gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */ 3198b81db932SToby Isaac } 31999566063dSJacob Faibussowitsch PetscCall(PetscFVComputeGradient(fvm, numFaces, dx, grad)); 3200b81db932SToby Isaac for (f = 0; f < numFaces; ++f) { 3201b81db932SToby Isaac for (d = 0; d < dim; ++d) gref[f][d] = grad[f * dim + d]; 3202b81db932SToby Isaac } 3203b81db932SToby Isaac } 32049566063dSJacob Faibussowitsch PetscCall(PetscFree3(dx, grad, gref)); 32059566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&neighSec)); 32069566063dSJacob Faibussowitsch PetscCall(PetscFree(neighbors)); 32073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3208b81db932SToby Isaac } 3209b81db932SToby Isaac 3210856ac710SMatthew G. Knepley /*@ 3211856ac710SMatthew G. Knepley DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data 3212856ac710SMatthew G. Knepley 321320f4b53cSBarry Smith Collective 3214856ac710SMatthew G. Knepley 32154165533cSJose E. Roman Input Parameters: 321620f4b53cSBarry Smith + dm - The `DMPLEX` 321720f4b53cSBarry Smith . fvm - The `PetscFV` 321820f4b53cSBarry Smith - cellGeometry - The face geometry from `DMPlexComputeCellGeometryFVM()` 3219856ac710SMatthew G. Knepley 32206b867d5aSJose E. Roman Input/Output Parameter: 322120f4b53cSBarry Smith . faceGeometry - The face geometry from `DMPlexComputeFaceGeometryFVM()`; on output 32226b867d5aSJose E. Roman the geometric factors for gradient calculation are inserted 32236b867d5aSJose E. Roman 32246b867d5aSJose E. Roman Output Parameter: 322520f4b53cSBarry Smith . dmGrad - The `DM` describing the layout of gradient data 3226856ac710SMatthew G. Knepley 3227856ac710SMatthew G. Knepley Level: developer 3228856ac710SMatthew G. Knepley 322920f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexGetFaceGeometryFVM()`, `DMPlexGetCellGeometryFVM()` 3230856ac710SMatthew G. Knepley @*/ 3231d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) 3232d71ae5a4SJacob Faibussowitsch { 3233856ac710SMatthew G. Knepley DM dmFace, dmCell; 3234856ac710SMatthew G. Knepley PetscScalar *fgeom, *cgeom; 3235b81db932SToby Isaac PetscSection sectionGrad, parentSection; 3236856ac710SMatthew G. Knepley PetscInt dim, pdim, cStart, cEnd, cEndInterior, c; 3237856ac710SMatthew G. Knepley 3238856ac710SMatthew G. Knepley PetscFunctionBegin; 32399566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 32409566063dSJacob Faibussowitsch PetscCall(PetscFVGetNumComponents(fvm, &pdim)); 32419566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 32422827ebadSStefano Zampini PetscCall(DMPlexGetCellTypeStratum(dm, DM_POLYTOPE_FV_GHOST, &cEndInterior, NULL)); 3243856ac710SMatthew G. Knepley /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */ 32449566063dSJacob Faibussowitsch PetscCall(VecGetDM(faceGeometry, &dmFace)); 32459566063dSJacob Faibussowitsch PetscCall(VecGetDM(cellGeometry, &dmCell)); 32469566063dSJacob Faibussowitsch PetscCall(VecGetArray(faceGeometry, &fgeom)); 32479566063dSJacob Faibussowitsch PetscCall(VecGetArray(cellGeometry, &cgeom)); 32489566063dSJacob Faibussowitsch PetscCall(DMPlexGetTree(dm, &parentSection, NULL, NULL, NULL, NULL)); 3249b81db932SToby Isaac if (!parentSection) { 32509566063dSJacob Faibussowitsch PetscCall(BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom)); 3251b5a3613cSMatthew G. Knepley } else { 32529566063dSJacob Faibussowitsch PetscCall(BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom)); 3253b81db932SToby Isaac } 32549566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(faceGeometry, &fgeom)); 32559566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(cellGeometry, &cgeom)); 3256856ac710SMatthew G. Knepley /* Create storage for gradients */ 32579566063dSJacob Faibussowitsch PetscCall(DMClone(dm, dmGrad)); 32589566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ionGrad)); 32599566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(sectionGrad, cStart, cEnd)); 32609566063dSJacob Faibussowitsch for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionGrad, c, pdim * dim)); 32619566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(sectionGrad)); 32629566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(*dmGrad, sectionGrad)); 32639566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(§ionGrad)); 32643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3265856ac710SMatthew G. Knepley } 3266b27d5b9eSToby Isaac 3267c501906fSMatthew G. Knepley /*@ 3268c501906fSMatthew G. Knepley DMPlexGetDataFVM - Retrieve precomputed cell geometry 3269c501906fSMatthew G. Knepley 327020f4b53cSBarry Smith Collective 3271c501906fSMatthew G. Knepley 32724165533cSJose E. Roman Input Parameters: 327320f4b53cSBarry Smith + dm - The `DM` 327420f4b53cSBarry Smith - fv - The `PetscFV` 3275c501906fSMatthew G. Knepley 3276c501906fSMatthew G. Knepley Output Parameters: 327760225df5SJacob Faibussowitsch + cellgeom - The cell geometry 327860225df5SJacob Faibussowitsch . facegeom - The face geometry 32796b867d5aSJose E. Roman - gradDM - The gradient matrices 3280c501906fSMatthew G. Knepley 3281c501906fSMatthew G. Knepley Level: developer 3282c501906fSMatthew G. Knepley 328320f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeGeometryFVM()` 3284c501906fSMatthew G. Knepley @*/ 3285d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM) 3286d71ae5a4SJacob Faibussowitsch { 3287b27d5b9eSToby Isaac PetscObject cellgeomobj, facegeomobj; 3288b27d5b9eSToby Isaac 3289b27d5b9eSToby Isaac PetscFunctionBegin; 32909566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj)); 3291b27d5b9eSToby Isaac if (!cellgeomobj) { 3292b27d5b9eSToby Isaac Vec cellgeomInt, facegeomInt; 3293b27d5b9eSToby Isaac 32949566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt)); 32959566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_cellgeom_fvm", (PetscObject)cellgeomInt)); 32969566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_facegeom_fvm", (PetscObject)facegeomInt)); 32979566063dSJacob Faibussowitsch PetscCall(VecDestroy(&cellgeomInt)); 32989566063dSJacob Faibussowitsch PetscCall(VecDestroy(&facegeomInt)); 32999566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj)); 3300b27d5b9eSToby Isaac } 33019566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_facegeom_fvm", &facegeomobj)); 3302b27d5b9eSToby Isaac if (cellgeom) *cellgeom = (Vec)cellgeomobj; 3303b27d5b9eSToby Isaac if (facegeom) *facegeom = (Vec)facegeomobj; 3304b27d5b9eSToby Isaac if (gradDM) { 3305b27d5b9eSToby Isaac PetscObject gradobj; 3306b27d5b9eSToby Isaac PetscBool computeGradients; 3307b27d5b9eSToby Isaac 33089566063dSJacob Faibussowitsch PetscCall(PetscFVGetComputeGradients(fv, &computeGradients)); 3309b27d5b9eSToby Isaac if (!computeGradients) { 3310b27d5b9eSToby Isaac *gradDM = NULL; 33113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3312b27d5b9eSToby Isaac } 33139566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj)); 3314b27d5b9eSToby Isaac if (!gradobj) { 3315b27d5b9eSToby Isaac DM dmGradInt; 3316b27d5b9eSToby Isaac 33179566063dSJacob Faibussowitsch PetscCall(DMPlexComputeGradientFVM(dm, fv, (Vec)facegeomobj, (Vec)cellgeomobj, &dmGradInt)); 33189566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt)); 33199566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dmGradInt)); 33209566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj)); 3321b27d5b9eSToby Isaac } 3322b27d5b9eSToby Isaac *gradDM = (DM)gradobj; 3323b27d5b9eSToby Isaac } 33243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3325b27d5b9eSToby Isaac } 3326d6143a4eSToby Isaac 3327d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess) 3328d71ae5a4SJacob Faibussowitsch { 33299d150b73SToby Isaac PetscInt l, m; 33309d150b73SToby Isaac 3331cd345991SToby Isaac PetscFunctionBeginHot; 33329d150b73SToby Isaac if (dimC == dimR && dimR <= 3) { 33339d150b73SToby Isaac /* invert Jacobian, multiply */ 33349d150b73SToby Isaac PetscScalar det, idet; 33359d150b73SToby Isaac 33369d150b73SToby Isaac switch (dimR) { 3337d71ae5a4SJacob Faibussowitsch case 1: 3338d71ae5a4SJacob Faibussowitsch invJ[0] = 1. / J[0]; 3339d71ae5a4SJacob Faibussowitsch break; 33409d150b73SToby Isaac case 2: 33419d150b73SToby Isaac det = J[0] * J[3] - J[1] * J[2]; 33429d150b73SToby Isaac idet = 1. / det; 33439d150b73SToby Isaac invJ[0] = J[3] * idet; 33449d150b73SToby Isaac invJ[1] = -J[1] * idet; 33459d150b73SToby Isaac invJ[2] = -J[2] * idet; 33469d150b73SToby Isaac invJ[3] = J[0] * idet; 33479d150b73SToby Isaac break; 33489371c9d4SSatish Balay case 3: { 33499d150b73SToby Isaac invJ[0] = J[4] * J[8] - J[5] * J[7]; 33509d150b73SToby Isaac invJ[1] = J[2] * J[7] - J[1] * J[8]; 33519d150b73SToby Isaac invJ[2] = J[1] * J[5] - J[2] * J[4]; 33529d150b73SToby Isaac det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6]; 33539d150b73SToby Isaac idet = 1. / det; 33549d150b73SToby Isaac invJ[0] *= idet; 33559d150b73SToby Isaac invJ[1] *= idet; 33569d150b73SToby Isaac invJ[2] *= idet; 33579d150b73SToby Isaac invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]); 33589d150b73SToby Isaac invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]); 33599d150b73SToby Isaac invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]); 33609d150b73SToby Isaac invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]); 33619d150b73SToby Isaac invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]); 33629d150b73SToby Isaac invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]); 33639371c9d4SSatish Balay } break; 33649d150b73SToby Isaac } 33659d150b73SToby Isaac for (l = 0; l < dimR; l++) { 3366ad540459SPierre Jolivet for (m = 0; m < dimC; m++) guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m]; 33679d150b73SToby Isaac } 33689d150b73SToby Isaac } else { 33699d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX) 33709d150b73SToby Isaac char transpose = 'C'; 33719d150b73SToby Isaac #else 33729d150b73SToby Isaac char transpose = 'T'; 33739d150b73SToby Isaac #endif 33746497c311SBarry Smith PetscBLASInt m = (PetscBLASInt)dimR; 33756497c311SBarry Smith PetscBLASInt n = (PetscBLASInt)dimC; 33769d150b73SToby Isaac PetscBLASInt one = 1; 33776497c311SBarry Smith PetscBLASInt worksize = (PetscBLASInt)(dimR * dimC), info; 33789d150b73SToby Isaac 3379ad540459SPierre Jolivet for (l = 0; l < dimC; l++) invJ[l] = resNeg[l]; 33809d150b73SToby Isaac 3381792fecdfSBarry Smith PetscCallBLAS("LAPACKgels", LAPACKgels_(&transpose, &m, &n, &one, J, &m, invJ, &n, work, &worksize, &info)); 338208401ef6SPierre Jolivet PetscCheck(info == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Bad argument to GELS"); 33839d150b73SToby Isaac 3384ad540459SPierre Jolivet for (l = 0; l < dimR; l++) guess[l] += PetscRealPart(invJ[l]); 33859d150b73SToby Isaac } 33863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 33879d150b73SToby Isaac } 33889d150b73SToby Isaac 3389d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 3390d71ae5a4SJacob Faibussowitsch { 3391c0cbe899SToby Isaac PetscInt coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR); 33929d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 33939d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg; 33949d150b73SToby Isaac PetscScalar *J, *invJ, *work; 33959d150b73SToby Isaac 33969d150b73SToby Isaac PetscFunctionBegin; 33979d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 33989566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 33991dca8a05SBarry Smith PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize); 34009566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData)); 34019566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J)); 34029d150b73SToby Isaac cellCoords = &cellData[0]; 34039d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 34049d150b73SToby Isaac extJ = &cellData[2 * coordSize]; 34059d150b73SToby Isaac resNeg = &cellData[2 * coordSize + dimR]; 34069d150b73SToby Isaac invJ = &J[dimR * dimC]; 34079d150b73SToby Isaac work = &J[2 * dimR * dimC]; 34089d150b73SToby Isaac if (dimR == 2) { 34099d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 34109d150b73SToby Isaac 34119d150b73SToby Isaac for (i = 0; i < 4; i++) { 34129d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 34139d150b73SToby Isaac 3414ad540459SPierre Jolivet for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 34159d150b73SToby Isaac } 34169d150b73SToby Isaac } else if (dimR == 3) { 34179d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 34189d150b73SToby Isaac 34199d150b73SToby Isaac for (i = 0; i < 8; i++) { 34209d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 34219d150b73SToby Isaac 3422ad540459SPierre Jolivet for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 34239d150b73SToby Isaac } 34249d150b73SToby Isaac } else { 3425ad540459SPierre Jolivet for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]); 34269d150b73SToby Isaac } 34279d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 34289d150b73SToby Isaac for (i = 0; i < dimR; i++) { 34299d150b73SToby Isaac PetscReal *swap; 34309d150b73SToby Isaac 34319d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 34329d150b73SToby Isaac for (k = 0; k < dimC; k++) { 34339d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 34349d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 34359d150b73SToby Isaac } 34369d150b73SToby Isaac } 34379d150b73SToby Isaac 34389d150b73SToby Isaac if (i < dimR - 1) { 34399d150b73SToby Isaac swap = cellCoeffs; 34409d150b73SToby Isaac cellCoeffs = cellCoords; 34419d150b73SToby Isaac cellCoords = swap; 34429d150b73SToby Isaac } 34439d150b73SToby Isaac } 34449566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(refCoords, numPoints * dimR)); 34459d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 34469d150b73SToby Isaac for (i = 0; i < maxIts; i++) { 34479d150b73SToby Isaac PetscReal *guess = &refCoords[dimR * j]; 34489d150b73SToby Isaac 34499d150b73SToby Isaac /* compute -residual and Jacobian */ 3450ad540459SPierre Jolivet for (k = 0; k < dimC; k++) resNeg[k] = realCoords[dimC * j + k]; 3451ad540459SPierre Jolivet for (k = 0; k < dimC * dimR; k++) J[k] = 0.; 34529d150b73SToby Isaac for (k = 0; k < numV; k++) { 34539d150b73SToby Isaac PetscReal extCoord = 1.; 34549d150b73SToby Isaac for (l = 0; l < dimR; l++) { 34559d150b73SToby Isaac PetscReal coord = guess[l]; 34569d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 34579d150b73SToby Isaac 34589d150b73SToby Isaac extCoord *= dep * coord + !dep; 34599d150b73SToby Isaac extJ[l] = dep; 34609d150b73SToby Isaac 34619d150b73SToby Isaac for (m = 0; m < dimR; m++) { 34629d150b73SToby Isaac PetscReal coord = guess[m]; 34639d150b73SToby Isaac PetscInt dep = ((k & (1 << m)) >> m) && (m != l); 34649d150b73SToby Isaac PetscReal mult = dep * coord + !dep; 34659d150b73SToby Isaac 34669d150b73SToby Isaac extJ[l] *= mult; 34679d150b73SToby Isaac } 34689d150b73SToby Isaac } 34699d150b73SToby Isaac for (l = 0; l < dimC; l++) { 34709d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 34719d150b73SToby Isaac 34729d150b73SToby Isaac resNeg[l] -= coeff * extCoord; 3473ad540459SPierre Jolivet for (m = 0; m < dimR; m++) J[dimR * l + m] += coeff * extJ[m]; 34749d150b73SToby Isaac } 34759d150b73SToby Isaac } 347676bd3646SJed Brown if (0 && PetscDefined(USE_DEBUG)) { 34770611203eSToby Isaac PetscReal maxAbs = 0.; 34780611203eSToby Isaac 3479ad540459SPierre Jolivet for (l = 0; l < dimC; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l])); 348063a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs)); 34810611203eSToby Isaac } 34829d150b73SToby Isaac 34839566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(dimC, dimR, J, invJ, work, resNeg, guess)); 34849d150b73SToby Isaac } 34859d150b73SToby Isaac } 34869566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J)); 34879566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData)); 34889566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 34893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 34909d150b73SToby Isaac } 34919d150b73SToby Isaac 3492d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR) 3493d71ae5a4SJacob Faibussowitsch { 34949d150b73SToby Isaac PetscInt coordSize, i, j, k, l, numV = (1 << dimR); 34959d150b73SToby Isaac PetscScalar *coordsScalar = NULL; 34969d150b73SToby Isaac PetscReal *cellData, *cellCoords, *cellCoeffs; 34979d150b73SToby Isaac 34989d150b73SToby Isaac PetscFunctionBegin; 34999d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 35009566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 35011dca8a05SBarry Smith PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize); 35029566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData)); 35039d150b73SToby Isaac cellCoords = &cellData[0]; 35049d150b73SToby Isaac cellCoeffs = &cellData[coordSize]; 35059d150b73SToby Isaac if (dimR == 2) { 35069d150b73SToby Isaac const PetscInt zToPlex[4] = {0, 1, 3, 2}; 35079d150b73SToby Isaac 35089d150b73SToby Isaac for (i = 0; i < 4; i++) { 35099d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 35109d150b73SToby Isaac 3511ad540459SPierre Jolivet for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 35129d150b73SToby Isaac } 35139d150b73SToby Isaac } else if (dimR == 3) { 35149d150b73SToby Isaac const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6}; 35159d150b73SToby Isaac 35169d150b73SToby Isaac for (i = 0; i < 8; i++) { 35179d150b73SToby Isaac PetscInt plexI = zToPlex[i]; 35189d150b73SToby Isaac 3519ad540459SPierre Jolivet for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); 35209d150b73SToby Isaac } 35219d150b73SToby Isaac } else { 3522ad540459SPierre Jolivet for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]); 35239d150b73SToby Isaac } 35249d150b73SToby Isaac /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */ 35259d150b73SToby Isaac for (i = 0; i < dimR; i++) { 35269d150b73SToby Isaac PetscReal *swap; 35279d150b73SToby Isaac 35289d150b73SToby Isaac for (j = 0; j < (numV / 2); j++) { 35299d150b73SToby Isaac for (k = 0; k < dimC; k++) { 35309d150b73SToby Isaac cellCoeffs[dimC * j + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]); 35319d150b73SToby Isaac cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]); 35329d150b73SToby Isaac } 35339d150b73SToby Isaac } 35349d150b73SToby Isaac 35359d150b73SToby Isaac if (i < dimR - 1) { 35369d150b73SToby Isaac swap = cellCoeffs; 35379d150b73SToby Isaac cellCoeffs = cellCoords; 35389d150b73SToby Isaac cellCoords = swap; 35399d150b73SToby Isaac } 35409d150b73SToby Isaac } 35419566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(realCoords, numPoints * dimC)); 35429d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 35439d150b73SToby Isaac const PetscReal *guess = &refCoords[dimR * j]; 35449d150b73SToby Isaac PetscReal *mapped = &realCoords[dimC * j]; 35459d150b73SToby Isaac 35469d150b73SToby Isaac for (k = 0; k < numV; k++) { 35479d150b73SToby Isaac PetscReal extCoord = 1.; 35489d150b73SToby Isaac for (l = 0; l < dimR; l++) { 35499d150b73SToby Isaac PetscReal coord = guess[l]; 35509d150b73SToby Isaac PetscInt dep = (k & (1 << l)) >> l; 35519d150b73SToby Isaac 35529d150b73SToby Isaac extCoord *= dep * coord + !dep; 35539d150b73SToby Isaac } 35549d150b73SToby Isaac for (l = 0; l < dimC; l++) { 35559d150b73SToby Isaac PetscReal coeff = cellCoeffs[dimC * k + l]; 35569d150b73SToby Isaac 35579d150b73SToby Isaac mapped[l] += coeff * extCoord; 35589d150b73SToby Isaac } 35599d150b73SToby Isaac } 35609d150b73SToby Isaac } 35619566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData)); 35629566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar)); 35633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 35649d150b73SToby Isaac } 35659d150b73SToby Isaac 35669c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */ 3567d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR) 3568d71ae5a4SJacob Faibussowitsch { 35699c3cf19fSMatthew G. Knepley PetscInt numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize; 3570c6e120d1SToby Isaac PetscScalar *nodes = NULL; 3571c6e120d1SToby Isaac PetscReal *invV, *modes; 3572c6e120d1SToby Isaac PetscReal *B, *D, *resNeg; 3573c6e120d1SToby Isaac PetscScalar *J, *invJ, *work; 35749d150b73SToby Isaac 35759d150b73SToby Isaac PetscFunctionBegin; 35769566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 35779566063dSJacob Faibussowitsch PetscCall(PetscFEGetNumComponents(fe, &numComp)); 357863a3b9bcSJacob 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); 35799566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 35809d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 35819566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes)); 35829d150b73SToby Isaac invV = fe->invV; 3583012b7cc6SMatthew G. Knepley for (i = 0; i < pdim; ++i) { 3584012b7cc6SMatthew G. Knepley modes[i] = 0.; 3585ad540459SPierre Jolivet for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); 35869d150b73SToby Isaac } 35879566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B)); 35889c3cf19fSMatthew G. Knepley D = &B[pdim * Nc]; 35899c3cf19fSMatthew G. Knepley resNeg = &D[pdim * Nc * dimR]; 35909566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J)); 35919c3cf19fSMatthew G. Knepley invJ = &J[Nc * dimR]; 35929c3cf19fSMatthew G. Knepley work = &invJ[Nc * dimR]; 3593ad540459SPierre Jolivet for (i = 0; i < numPoints * dimR; i++) refCoords[i] = 0.; 35949d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 35959b1f03cbSToby 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 */ 35969d150b73SToby Isaac PetscReal *guess = &refCoords[j * dimR]; 35979566063dSJacob Faibussowitsch PetscCall(PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL)); 3598ad540459SPierre Jolivet for (k = 0; k < Nc; k++) resNeg[k] = realCoords[j * Nc + k]; 3599ad540459SPierre Jolivet for (k = 0; k < Nc * dimR; k++) J[k] = 0.; 36009c3cf19fSMatthew G. Knepley for (k = 0; k < pdim; k++) { 36019c3cf19fSMatthew G. Knepley for (l = 0; l < Nc; l++) { 3602012b7cc6SMatthew G. Knepley resNeg[l] -= modes[k] * B[k * Nc + l]; 3603ad540459SPierre Jolivet for (m = 0; m < dimR; m++) J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m]; 36049d150b73SToby Isaac } 36059d150b73SToby Isaac } 360676bd3646SJed Brown if (0 && PetscDefined(USE_DEBUG)) { 36070611203eSToby Isaac PetscReal maxAbs = 0.; 36080611203eSToby Isaac 3609ad540459SPierre Jolivet for (l = 0; l < Nc; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l])); 361063a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs)); 36110611203eSToby Isaac } 36129566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(Nc, dimR, J, invJ, work, resNeg, guess)); 36139d150b73SToby Isaac } 36149d150b73SToby Isaac } 36159566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J)); 36169566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B)); 36179566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes)); 36189566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 36193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 36209d150b73SToby Isaac } 36219d150b73SToby Isaac 36229c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */ 3623d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR) 3624d71ae5a4SJacob Faibussowitsch { 36259c3cf19fSMatthew G. Knepley PetscInt numComp, pdim, i, j, k, l, coordSize; 3626c6e120d1SToby Isaac PetscScalar *nodes = NULL; 3627c6e120d1SToby Isaac PetscReal *invV, *modes; 36289d150b73SToby Isaac PetscReal *B; 36299d150b73SToby Isaac 36309d150b73SToby Isaac PetscFunctionBegin; 36319566063dSJacob Faibussowitsch PetscCall(PetscFEGetDimension(fe, &pdim)); 36329566063dSJacob Faibussowitsch PetscCall(PetscFEGetNumComponents(fe, &numComp)); 363363a3b9bcSJacob 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); 36349566063dSJacob Faibussowitsch PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 36359d150b73SToby Isaac /* convert nodes to values in the stable evaluation basis */ 36369566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes)); 36379d150b73SToby Isaac invV = fe->invV; 3638012b7cc6SMatthew G. Knepley for (i = 0; i < pdim; ++i) { 3639012b7cc6SMatthew G. Knepley modes[i] = 0.; 3640ad540459SPierre Jolivet for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); 36419d150b73SToby Isaac } 36429566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B)); 36439566063dSJacob Faibussowitsch PetscCall(PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL)); 3644ad540459SPierre Jolivet for (i = 0; i < numPoints * Nc; i++) realCoords[i] = 0.; 36459d150b73SToby Isaac for (j = 0; j < numPoints; j++) { 36469c3cf19fSMatthew G. Knepley PetscReal *mapped = &realCoords[j * Nc]; 36479d150b73SToby Isaac 36489c3cf19fSMatthew G. Knepley for (k = 0; k < pdim; k++) { 3649ad540459SPierre Jolivet for (l = 0; l < Nc; l++) mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l]; 36509d150b73SToby Isaac } 36519d150b73SToby Isaac } 36529566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B)); 36539566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes)); 36549566063dSJacob Faibussowitsch PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes)); 36553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 36569d150b73SToby Isaac } 36579d150b73SToby Isaac 3658d6143a4eSToby Isaac /*@ 3659a4e35b19SJacob Faibussowitsch DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element 3660a4e35b19SJacob Faibussowitsch using a single element map. 3661d6143a4eSToby Isaac 366220f4b53cSBarry Smith Not Collective 3663d6143a4eSToby Isaac 3664d6143a4eSToby Isaac Input Parameters: 366520f4b53cSBarry Smith + dm - The mesh, with coordinate maps defined either by a `PetscDS` for the coordinate `DM` (see `DMGetCoordinateDM()`) or 3666d6143a4eSToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 3667d6143a4eSToby Isaac as a multilinear map for tensor-product elements 3668d6143a4eSToby Isaac . cell - the cell whose map is used. 3669d6143a4eSToby Isaac . numPoints - the number of points to locate 367020f4b53cSBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see `DMGetCoordinateDim()`) 3671d6143a4eSToby Isaac 36722fe279fdSBarry Smith Output Parameter: 367320f4b53cSBarry Smith . refCoords - (`numPoints` x `dimension`) array of reference coordinates (see `DMGetDimension()`) 36741b266c99SBarry Smith 36751b266c99SBarry Smith Level: intermediate 367673c9229bSMatthew Knepley 3677a4e35b19SJacob Faibussowitsch Notes: 3678a4e35b19SJacob Faibussowitsch This inversion will be accurate inside the reference element, but may be inaccurate for 3679a4e35b19SJacob Faibussowitsch mappings that do not extend uniquely outside the reference cell (e.g, most non-affine maps) 3680a4e35b19SJacob Faibussowitsch 368120f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexReferenceToCoordinates()` 3682d6143a4eSToby Isaac @*/ 3683d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[]) 3684d71ae5a4SJacob Faibussowitsch { 3685485ad865SMatthew G. Knepley PetscInt dimC, dimR, depth, cStart, cEnd, i; 36869d150b73SToby Isaac DM coordDM = NULL; 36879d150b73SToby Isaac Vec coords; 36889d150b73SToby Isaac PetscFE fe = NULL; 36899d150b73SToby Isaac 3690d6143a4eSToby Isaac PetscFunctionBegin; 36919d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 36929566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dimR)); 36939566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dimC)); 36943ba16761SJacob Faibussowitsch if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(PETSC_SUCCESS); 36959566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 36969566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coords)); 36979566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &coordDM)); 36989d150b73SToby Isaac if (coordDM) { 36999d150b73SToby Isaac PetscInt coordFields; 37009d150b73SToby Isaac 37019566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(coordDM, &coordFields)); 37029d150b73SToby Isaac if (coordFields) { 37039d150b73SToby Isaac PetscClassId id; 37049d150b73SToby Isaac PetscObject disc; 37059d150b73SToby Isaac 37069566063dSJacob Faibussowitsch PetscCall(DMGetField(coordDM, 0, NULL, &disc)); 37079566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 3708ad540459SPierre Jolivet if (id == PETSCFE_CLASSID) fe = (PetscFE)disc; 37099d150b73SToby Isaac } 37109d150b73SToby Isaac } 37119566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 37121dca8a05SBarry 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); 37139d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 37149d150b73SToby Isaac PetscInt coneSize; 37159d150b73SToby Isaac PetscBool isSimplex, isTensor; 37169d150b73SToby Isaac 37179566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 37189d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 37199d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 37209d150b73SToby Isaac if (isSimplex) { 37219d150b73SToby Isaac PetscReal detJ, *v0, *J, *invJ; 37229d150b73SToby Isaac 37239566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 37249d150b73SToby Isaac J = &v0[dimC]; 37259d150b73SToby Isaac invJ = &J[dimC * dimC]; 37269566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ)); 37279d150b73SToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */ 3728c330f8ffSToby Isaac const PetscReal x0[3] = {-1., -1., -1.}; 3729c330f8ffSToby Isaac 3730c330f8ffSToby Isaac CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]); 37319d150b73SToby Isaac } 37329566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 37339d150b73SToby Isaac } else if (isTensor) { 37349566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR)); 373563a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize); 37369d150b73SToby Isaac } else { 37379566063dSJacob Faibussowitsch PetscCall(DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR)); 37389d150b73SToby Isaac } 37393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 37409d150b73SToby Isaac } 37419d150b73SToby Isaac 37429d150b73SToby Isaac /*@ 374315229ffcSPierre Jolivet DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the mesh for a single element map. 37449d150b73SToby Isaac 374520f4b53cSBarry Smith Not Collective 37469d150b73SToby Isaac 37479d150b73SToby Isaac Input Parameters: 37482fe279fdSBarry Smith + dm - The mesh, with coordinate maps defined either by a PetscDS for the coordinate `DM` (see `DMGetCoordinateDM()`) or 37499d150b73SToby Isaac implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or 37509d150b73SToby Isaac as a multilinear map for tensor-product elements 37519d150b73SToby Isaac . cell - the cell whose map is used. 37529d150b73SToby Isaac . numPoints - the number of points to locate 37532fe279fdSBarry Smith - refCoords - (numPoints x dimension) array of reference coordinates (see `DMGetDimension()`) 37549d150b73SToby Isaac 37552fe279fdSBarry Smith Output Parameter: 37562fe279fdSBarry Smith . realCoords - (numPoints x coordinate dimension) array of coordinates (see `DMGetCoordinateDim()`) 37571b266c99SBarry Smith 37581b266c99SBarry Smith Level: intermediate 375973c9229bSMatthew Knepley 37602fe279fdSBarry Smith .seealso: `DMPLEX`, `DMPlexCoordinatesToReference()` 37619d150b73SToby Isaac @*/ 3762d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[]) 3763d71ae5a4SJacob Faibussowitsch { 3764485ad865SMatthew G. Knepley PetscInt dimC, dimR, depth, cStart, cEnd, i; 37659d150b73SToby Isaac DM coordDM = NULL; 37669d150b73SToby Isaac Vec coords; 37679d150b73SToby Isaac PetscFE fe = NULL; 37689d150b73SToby Isaac 37699d150b73SToby Isaac PetscFunctionBegin; 37709d150b73SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 37719566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dimR)); 37729566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dimC)); 37733ba16761SJacob Faibussowitsch if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(PETSC_SUCCESS); 37749566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 37759566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coords)); 37769566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &coordDM)); 37779d150b73SToby Isaac if (coordDM) { 37789d150b73SToby Isaac PetscInt coordFields; 37799d150b73SToby Isaac 37809566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(coordDM, &coordFields)); 37819d150b73SToby Isaac if (coordFields) { 37829d150b73SToby Isaac PetscClassId id; 37839d150b73SToby Isaac PetscObject disc; 37849d150b73SToby Isaac 37859566063dSJacob Faibussowitsch PetscCall(DMGetField(coordDM, 0, NULL, &disc)); 37869566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 3787ad540459SPierre Jolivet if (id == PETSCFE_CLASSID) fe = (PetscFE)disc; 37889d150b73SToby Isaac } 37899d150b73SToby Isaac } 37909566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 37911dca8a05SBarry 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); 37929d150b73SToby Isaac if (!fe) { /* implicit discretization: affine or multilinear */ 37939d150b73SToby Isaac PetscInt coneSize; 37949d150b73SToby Isaac PetscBool isSimplex, isTensor; 37959d150b73SToby Isaac 37969566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 37979d150b73SToby Isaac isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE; 37989d150b73SToby Isaac isTensor = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE; 37999d150b73SToby Isaac if (isSimplex) { 38009d150b73SToby Isaac PetscReal detJ, *v0, *J; 38019d150b73SToby Isaac 38029566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 38039d150b73SToby Isaac J = &v0[dimC]; 38049566063dSJacob Faibussowitsch PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ)); 3805c330f8ffSToby Isaac for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */ 3806c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 3807c330f8ffSToby Isaac 3808c330f8ffSToby Isaac CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]); 38099d150b73SToby Isaac } 38109566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0)); 38119d150b73SToby Isaac } else if (isTensor) { 38129566063dSJacob Faibussowitsch PetscCall(DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR)); 381363a3b9bcSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize); 38149d150b73SToby Isaac } else { 38159566063dSJacob Faibussowitsch PetscCall(DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR)); 38169d150b73SToby Isaac } 38173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3818d6143a4eSToby Isaac } 38190139fca9SMatthew G. Knepley 3820be664eb1SMatthew G. Knepley void coordMap_identity(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 f0[]) 3821be664eb1SMatthew G. Knepley { 3822be664eb1SMatthew G. Knepley const PetscInt Nc = uOff[1] - uOff[0]; 3823be664eb1SMatthew G. Knepley PetscInt c; 3824be664eb1SMatthew G. Knepley 3825be664eb1SMatthew G. Knepley for (c = 0; c < Nc; ++c) f0[c] = u[c]; 3826be664eb1SMatthew G. Knepley } 3827be664eb1SMatthew G. Knepley 3828be664eb1SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z, 3829be664eb1SMatthew G. Knepley / 1 0 m_0 \ 3830be664eb1SMatthew G. Knepley | 0 1 m_1 | 3831be664eb1SMatthew G. Knepley \ 0 0 1 / 3832be664eb1SMatthew G. Knepley */ 3833be664eb1SMatthew G. Knepley void coordMap_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[]) 3834be664eb1SMatthew G. Knepley { 3835be664eb1SMatthew G. Knepley const PetscInt Nc = uOff[1] - uOff[0]; 3836be664eb1SMatthew G. Knepley const PetscInt ax = (PetscInt)PetscRealPart(constants[0]); 3837be664eb1SMatthew G. Knepley PetscInt c; 3838be664eb1SMatthew G. Knepley 3839be664eb1SMatthew G. Knepley for (c = 0; c < Nc; ++c) coords[c] = u[c] + constants[c + 1] * u[ax]; 3840be664eb1SMatthew G. Knepley } 3841be664eb1SMatthew G. Knepley 3842be664eb1SMatthew G. Knepley /* Flare applies the transformation, assuming we fix x_f, 3843be664eb1SMatthew G. Knepley 3844be664eb1SMatthew G. Knepley x_i = x_i * alpha_i x_f 3845be664eb1SMatthew G. Knepley */ 3846be664eb1SMatthew G. Knepley void coordMap_flare(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[]) 3847be664eb1SMatthew G. Knepley { 3848be664eb1SMatthew G. Knepley const PetscInt Nc = uOff[1] - uOff[0]; 3849be664eb1SMatthew G. Knepley const PetscInt cf = (PetscInt)PetscRealPart(constants[0]); 3850be664eb1SMatthew G. Knepley PetscInt c; 3851be664eb1SMatthew G. Knepley 3852be664eb1SMatthew G. Knepley for (c = 0; c < Nc; ++c) coords[c] = u[c] * (c == cf ? 1.0 : constants[c + 1] * u[cf]); 3853be664eb1SMatthew G. Knepley } 3854be664eb1SMatthew G. Knepley 3855be664eb1SMatthew G. Knepley /* 3856be664eb1SMatthew G. Knepley We would like to map the unit square to a quarter of the annulus between circles of radius 1 and 2. We start by mapping the straight sections, which 3857be664eb1SMatthew G. Knepley will correspond to the top and bottom of our square. So 3858be664eb1SMatthew G. Knepley 3859be664eb1SMatthew G. Knepley (0,0)--(1,0) ==> (1,0)--(2,0) Just a shift of (1,0) 3860be664eb1SMatthew G. Knepley (0,1)--(1,1) ==> (0,1)--(0,2) Switch x and y 3861be664eb1SMatthew G. Knepley 3862be664eb1SMatthew G. Knepley So it looks like we want to map each layer in y to a ray, so x is the radius and y is the angle: 3863be664eb1SMatthew G. Knepley 3864be664eb1SMatthew G. Knepley (x, y) ==> (x+1, \pi/2 y) in (r', \theta') space 3865be664eb1SMatthew G. Knepley ==> ((x+1) cos(\pi/2 y), (x+1) sin(\pi/2 y)) in (x', y') space 3866be664eb1SMatthew G. Knepley */ 3867be664eb1SMatthew G. Knepley void coordMap_annulus(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 xp[]) 3868be664eb1SMatthew G. Knepley { 3869be664eb1SMatthew G. Knepley const PetscReal ri = PetscRealPart(constants[0]); 3870be664eb1SMatthew G. Knepley const PetscReal ro = PetscRealPart(constants[1]); 3871be664eb1SMatthew G. Knepley 3872be664eb1SMatthew G. Knepley xp[0] = (x[0] * (ro - ri) + ri) * PetscCosReal(0.5 * PETSC_PI * x[1]); 3873be664eb1SMatthew G. Knepley xp[1] = (x[0] * (ro - ri) + ri) * PetscSinReal(0.5 * PETSC_PI * x[1]); 3874be664eb1SMatthew G. Knepley } 3875be664eb1SMatthew G. Knepley 3876be664eb1SMatthew G. Knepley /* 3877be664eb1SMatthew G. Knepley We would like to map the unit cube to a hemisphere of the spherical shell between balls of radius 1 and 2. We want to map the bottom surface onto the 3878be664eb1SMatthew G. Knepley lower hemisphere and the upper surface onto the top, letting z be the radius. 3879be664eb1SMatthew G. Knepley 3880be664eb1SMatthew G. Knepley (x, y) ==> ((z+3)/2, \pi/2 (|x| or |y|), arctan y/x) in (r', \theta', \phi') space 3881be664eb1SMatthew G. Knepley ==> ((z+3)/2 \cos(\theta') cos(\phi'), (z+3)/2 \cos(\theta') sin(\phi'), (z+3)/2 sin(\theta')) in (x', y', z') space 3882be664eb1SMatthew G. Knepley */ 3883be664eb1SMatthew G. Knepley void coordMap_shell(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 xp[]) 3884be664eb1SMatthew G. Knepley { 3885be664eb1SMatthew G. Knepley const PetscReal pi4 = PETSC_PI / 4.0; 3886be664eb1SMatthew G. Knepley const PetscReal ri = PetscRealPart(constants[0]); 3887be664eb1SMatthew G. Knepley const PetscReal ro = PetscRealPart(constants[1]); 3888be664eb1SMatthew G. Knepley const PetscReal rp = (x[2] + 1) * 0.5 * (ro - ri) + ri; 3889be664eb1SMatthew G. Knepley const PetscReal phip = PetscAtan2Real(x[1], x[0]); 3890be664eb1SMatthew G. Knepley const PetscReal thetap = 0.5 * PETSC_PI * (1.0 - ((((phip <= pi4) && (phip >= -pi4)) || ((phip >= 3.0 * pi4) || (phip <= -3.0 * pi4))) ? PetscAbsReal(x[0]) : PetscAbsReal(x[1]))); 3891be664eb1SMatthew G. Knepley 3892be664eb1SMatthew G. Knepley xp[0] = rp * PetscCosReal(thetap) * PetscCosReal(phip); 3893be664eb1SMatthew G. Knepley xp[1] = rp * PetscCosReal(thetap) * PetscSinReal(phip); 3894be664eb1SMatthew G. Knepley xp[2] = rp * PetscSinReal(thetap); 3895be664eb1SMatthew G. Knepley } 3896be664eb1SMatthew G. Knepley 38970139fca9SMatthew G. Knepley /*@C 38982fe279fdSBarry Smith DMPlexRemapGeometry - This function maps the original `DM` coordinates to new coordinates. 38990139fca9SMatthew G. Knepley 390020f4b53cSBarry Smith Not Collective 39010139fca9SMatthew G. Knepley 39020139fca9SMatthew G. Knepley Input Parameters: 39032fe279fdSBarry Smith + dm - The `DM` 39040139fca9SMatthew G. Knepley . time - The time 3905a4e35b19SJacob Faibussowitsch - func - The function transforming current coordinates to new coordinates 39060139fca9SMatthew G. Knepley 390720f4b53cSBarry Smith Calling sequence of `func`: 39080139fca9SMatthew G. Knepley + dim - The spatial dimension 39090139fca9SMatthew G. Knepley . Nf - The number of input fields (here 1) 39100139fca9SMatthew G. Knepley . NfAux - The number of input auxiliary fields 39110139fca9SMatthew G. Knepley . uOff - The offset of the coordinates in u[] (here 0) 39120139fca9SMatthew G. Knepley . uOff_x - The offset of the coordinates in u_x[] (here 0) 39130139fca9SMatthew G. Knepley . u - The coordinate values at this point in space 391420f4b53cSBarry Smith . u_t - The coordinate time derivative at this point in space (here `NULL`) 39150139fca9SMatthew G. Knepley . u_x - The coordinate derivatives at this point in space 39160139fca9SMatthew G. Knepley . aOff - The offset of each auxiliary field in u[] 39170139fca9SMatthew G. Knepley . aOff_x - The offset of each auxiliary field in u_x[] 39180139fca9SMatthew G. Knepley . a - The auxiliary field values at this point in space 391920f4b53cSBarry Smith . a_t - The auxiliary field time derivative at this point in space (or `NULL`) 39200139fca9SMatthew G. Knepley . a_x - The auxiliary field derivatives at this point in space 39210139fca9SMatthew G. Knepley . t - The current time 39220139fca9SMatthew G. Knepley . x - The coordinates of this point (here not used) 39230139fca9SMatthew G. Knepley . numConstants - The number of constants 39240139fca9SMatthew G. Knepley . constants - The value of each constant 39250139fca9SMatthew G. Knepley - f - The new coordinates at this point in space 39260139fca9SMatthew G. Knepley 39270139fca9SMatthew G. Knepley Level: intermediate 39280139fca9SMatthew G. Knepley 39292fe279fdSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMProjectFieldLocal()`, `DMProjectFieldLabelLocal()` 39300139fca9SMatthew G. Knepley @*/ 3931a4e35b19SJacob Faibussowitsch PetscErrorCode DMPlexRemapGeometry(DM dm, PetscReal time, void (*func)(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 f[])) 3932d71ae5a4SJacob Faibussowitsch { 39330139fca9SMatthew G. Knepley DM cdm; 3934be664eb1SMatthew G. Knepley PetscDS cds; 39358bf1a49fSMatthew G. Knepley DMField cf; 3936be664eb1SMatthew G. Knepley PetscObject obj; 3937be664eb1SMatthew G. Knepley PetscClassId id; 39380139fca9SMatthew G. Knepley Vec lCoords, tmpCoords; 39390139fca9SMatthew G. Knepley 39400139fca9SMatthew G. Knepley PetscFunctionBegin; 39419566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 39429566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &lCoords)); 3943be664eb1SMatthew G. Knepley PetscCall(DMGetDS(cdm, &cds)); 3944be664eb1SMatthew G. Knepley PetscCall(PetscDSGetDiscretization(cds, 0, &obj)); 3945be664eb1SMatthew G. Knepley PetscCall(PetscObjectGetClassId(obj, &id)); 3946be664eb1SMatthew G. Knepley if (id != PETSCFE_CLASSID) { 3947be664eb1SMatthew G. Knepley PetscSection cSection; 3948be664eb1SMatthew G. Knepley const PetscScalar *constants; 3949be664eb1SMatthew G. Knepley PetscScalar *coords, f[16]; 3950be664eb1SMatthew G. Knepley PetscInt dim, cdim, Nc, vStart, vEnd; 3951be664eb1SMatthew G. Knepley 3952be664eb1SMatthew G. Knepley PetscCall(DMGetDimension(dm, &dim)); 3953be664eb1SMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 3954be664eb1SMatthew G. Knepley PetscCheck(cdim <= 16, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Affine version of DMPlexRemapGeometry is currently limited to dimensions <= 16, not %" PetscInt_FMT, cdim); 3955be664eb1SMatthew G. Knepley PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 3956be664eb1SMatthew G. Knepley PetscCall(DMGetCoordinateSection(dm, &cSection)); 3957be664eb1SMatthew G. Knepley PetscCall(PetscDSGetConstants(cds, &Nc, &constants)); 3958be664eb1SMatthew G. Knepley PetscCall(VecGetArrayWrite(lCoords, &coords)); 3959be664eb1SMatthew G. Knepley for (PetscInt v = vStart; v < vEnd; ++v) { 3960be664eb1SMatthew G. Knepley PetscInt uOff[2] = {0, cdim}; 3961be664eb1SMatthew G. Knepley PetscInt off, c; 3962be664eb1SMatthew G. Knepley 3963be664eb1SMatthew G. Knepley PetscCall(PetscSectionGetOffset(cSection, v, &off)); 3964be664eb1SMatthew G. Knepley (*func)(dim, 1, 0, uOff, NULL, &coords[off], NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0.0, NULL, Nc, constants, f); 3965be664eb1SMatthew G. Knepley for (c = 0; c < cdim; ++c) coords[off + c] = f[c]; 3966be664eb1SMatthew G. Knepley } 3967be664eb1SMatthew G. Knepley PetscCall(VecRestoreArrayWrite(lCoords, &coords)); 3968be664eb1SMatthew G. Knepley } else { 39699566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(cdm, &tmpCoords)); 39709566063dSJacob Faibussowitsch PetscCall(VecCopy(lCoords, tmpCoords)); 39718bf1a49fSMatthew G. Knepley /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */ 39729566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateField(dm, &cf)); 39736858538eSMatthew G. Knepley cdm->coordinates[0].field = cf; 39749566063dSJacob Faibussowitsch PetscCall(DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords)); 39756858538eSMatthew G. Knepley cdm->coordinates[0].field = NULL; 39769566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(cdm, &tmpCoords)); 39779566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, lCoords)); 39780139fca9SMatthew G. Knepley } 3979be664eb1SMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 39800139fca9SMatthew G. Knepley } 39810139fca9SMatthew G. Knepley 3982cc4c1da9SBarry Smith /*@ 39830139fca9SMatthew G. Knepley DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates. 39840139fca9SMatthew G. Knepley 398520f4b53cSBarry Smith Not Collective 39860139fca9SMatthew G. Knepley 39870139fca9SMatthew G. Knepley Input Parameters: 398820f4b53cSBarry Smith + dm - The `DMPLEX` 3989a3b724e8SBarry Smith . direction - The shear coordinate direction, e.g. `DM_X` is the x-axis 39900139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction 39910139fca9SMatthew G. Knepley 39920139fca9SMatthew G. Knepley Level: intermediate 39930139fca9SMatthew G. Knepley 3994a3b724e8SBarry Smith .seealso: `DMPLEX`, `DMPlexRemapGeometry()`, `DMDirection`, `DM_X`, `DM_Y`, `DM_Z` 39950139fca9SMatthew G. Knepley @*/ 3996d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[]) 3997d71ae5a4SJacob Faibussowitsch { 39980139fca9SMatthew G. Knepley DM cdm; 39990139fca9SMatthew G. Knepley PetscDS cds; 40000139fca9SMatthew G. Knepley PetscScalar *moduli; 40013ee9839eSMatthew G. Knepley const PetscInt dir = (PetscInt)direction; 40020139fca9SMatthew G. Knepley PetscInt dE, d, e; 40030139fca9SMatthew G. Knepley 40040139fca9SMatthew G. Knepley PetscFunctionBegin; 40059566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 40069566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dE)); 40079566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(dE + 1, &moduli)); 40080139fca9SMatthew G. Knepley moduli[0] = dir; 4009cdaaecf7SMatthew G. Knepley for (d = 0, e = 0; d < dE; ++d) moduli[d + 1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0); 40109566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &cds)); 40119566063dSJacob Faibussowitsch PetscCall(PetscDSSetConstants(cds, dE + 1, moduli)); 4012be664eb1SMatthew G. Knepley PetscCall(DMPlexRemapGeometry(dm, 0.0, coordMap_shear)); 40139566063dSJacob Faibussowitsch PetscCall(PetscFree(moduli)); 40143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 40150139fca9SMatthew G. Knepley } 4016