xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 9621725433c30ea061c9def08e05f80d9c426f98)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>  /*I      "petscdmplex.h"   I*/
29d150b73SToby Isaac #include <petsc/private/petscfeimpl.h> /*I      "petscfe.h"       I*/
39d150b73SToby Isaac #include <petscblaslapack.h>
4af74b616SDave May #include <petsctime.h>
5ccd2543fSMatthew G Knepley 
63985bb02SVaclav Hapla /*@
73985bb02SVaclav Hapla   DMPlexFindVertices - Try to find DAG points based on their coordinates.
83985bb02SVaclav Hapla 
93985bb02SVaclav Hapla   Not Collective (provided DMGetCoordinatesLocalSetUp() has been called already)
103985bb02SVaclav Hapla 
113985bb02SVaclav Hapla   Input Parameters:
123985bb02SVaclav Hapla + dm - The DMPlex object
13d3e1f4ccSVaclav Hapla . coordinates - The Vec of coordinates of the sought points
143985bb02SVaclav Hapla - eps - The tolerance or PETSC_DEFAULT
153985bb02SVaclav Hapla 
163985bb02SVaclav Hapla   Output Parameters:
17d3e1f4ccSVaclav Hapla . points - The IS of found DAG points or -1
183985bb02SVaclav Hapla 
193985bb02SVaclav Hapla   Level: intermediate
203985bb02SVaclav Hapla 
213985bb02SVaclav Hapla   Notes:
22d3e1f4ccSVaclav Hapla   The length of Vec coordinates must be npoints * dim where dim is the spatial dimension returned by DMGetCoordinateDim() and npoints is the number of sought points.
233985bb02SVaclav Hapla 
24d3e1f4ccSVaclav Hapla   The output IS is living on PETSC_COMM_SELF and its length is npoints.
25d3e1f4ccSVaclav Hapla   Each rank does the search independently.
26d3e1f4ccSVaclav Hapla   If this rank's local DMPlex portion contains the DAG point corresponding to the i-th tuple of coordinates, the i-th entry of the output IS is set to that DAG point, otherwise to -1.
273985bb02SVaclav Hapla 
28d3e1f4ccSVaclav Hapla   The output IS must be destroyed by user.
293985bb02SVaclav Hapla 
303985bb02SVaclav Hapla   The tolerance is interpreted as the maximum Euclidean (L2) distance of the sought point from the specified coordinates.
313985bb02SVaclav Hapla 
32d3e1f4ccSVaclav Hapla   Complexity of this function is currently O(mn) with m number of vertices to find and n number of vertices in the local mesh. This could probably be improved if needed.
33335ef845SVaclav Hapla 
34db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMGetCoordinatesLocal()`
353985bb02SVaclav Hapla @*/
36d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points)
37d71ae5a4SJacob Faibussowitsch {
3837900f7dSMatthew G. Knepley   PetscInt           c, cdim, i, j, o, p, vStart, vEnd;
39d3e1f4ccSVaclav Hapla   PetscInt           npoints;
40d3e1f4ccSVaclav Hapla   const PetscScalar *coord;
413985bb02SVaclav Hapla   Vec                allCoordsVec;
423985bb02SVaclav Hapla   const PetscScalar *allCoords;
43d3e1f4ccSVaclav Hapla   PetscInt          *dagPoints;
443985bb02SVaclav Hapla 
453985bb02SVaclav Hapla   PetscFunctionBegin;
463985bb02SVaclav Hapla   if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON;
479566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
48d3e1f4ccSVaclav Hapla   {
49d3e1f4ccSVaclav Hapla     PetscInt n;
50d3e1f4ccSVaclav Hapla 
519566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(coordinates, &n));
5263a3b9bcSJacob Faibussowitsch     PetscCheck(n % cdim == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Given coordinates Vec has local length %" PetscInt_FMT " not divisible by coordinate dimension %" PetscInt_FMT " of given DM", n, cdim);
53d3e1f4ccSVaclav Hapla     npoints = n / cdim;
54d3e1f4ccSVaclav Hapla   }
559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &allCoordsVec));
569566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(allCoordsVec, &allCoords));
579566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coord));
589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
5976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
60335ef845SVaclav Hapla     /* check coordinate section is consistent with DM dimension */
61335ef845SVaclav Hapla     PetscSection cs;
62335ef845SVaclav Hapla     PetscInt     ndof;
63335ef845SVaclav Hapla 
649566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cs));
653985bb02SVaclav Hapla     for (p = vStart; p < vEnd; p++) {
669566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cs, p, &ndof));
6763a3b9bcSJacob Faibussowitsch       PetscCheck(ndof == cdim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %" PetscInt_FMT ": ndof = %" PetscInt_FMT " != %" PetscInt_FMT " = cdim", p, ndof, cdim);
68335ef845SVaclav Hapla     }
69335ef845SVaclav Hapla   }
709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npoints, &dagPoints));
71eca9f518SVaclav Hapla   if (eps == 0.0) {
7237900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
73eca9f518SVaclav Hapla       dagPoints[i] = -1;
7437900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
7537900f7dSMatthew G. Knepley         for (c = 0; c < cdim; c++) {
76d3e1f4ccSVaclav Hapla           if (coord[j + c] != allCoords[o + c]) break;
77eca9f518SVaclav Hapla         }
7837900f7dSMatthew G. Knepley         if (c == cdim) {
79eca9f518SVaclav Hapla           dagPoints[i] = p;
80eca9f518SVaclav Hapla           break;
81eca9f518SVaclav Hapla         }
82eca9f518SVaclav Hapla       }
83eca9f518SVaclav Hapla     }
84d3e1f4ccSVaclav Hapla   } else {
8537900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
86d3e1f4ccSVaclav Hapla       PetscReal norm;
87d3e1f4ccSVaclav Hapla 
88335ef845SVaclav Hapla       dagPoints[i] = -1;
8937900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
903985bb02SVaclav Hapla         norm = 0.0;
91ad540459SPierre Jolivet         for (c = 0; c < cdim; c++) norm += PetscRealPart(PetscSqr(coord[j + c] - allCoords[o + c]));
923985bb02SVaclav Hapla         norm = PetscSqrtReal(norm);
933985bb02SVaclav Hapla         if (norm <= eps) {
943985bb02SVaclav Hapla           dagPoints[i] = p;
953985bb02SVaclav Hapla           break;
963985bb02SVaclav Hapla         }
973985bb02SVaclav Hapla       }
983985bb02SVaclav Hapla     }
99d3e1f4ccSVaclav Hapla   }
1009566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(allCoordsVec, &allCoords));
1019566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coord));
1029566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points));
1033985bb02SVaclav Hapla   PetscFunctionReturn(0);
1043985bb02SVaclav Hapla }
1053985bb02SVaclav Hapla 
106d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
107d71ae5a4SJacob Faibussowitsch {
108fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0 * 2 + 0];
109fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0 * 2 + 1];
110fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1 * 2 + 0];
111fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1 * 2 + 1];
112fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0 * 2 + 0];
113fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0 * 2 + 1];
114fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1 * 2 + 0];
115fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1 * 2 + 1];
116fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
117fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
118fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
119fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
120fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
121fea14342SMatthew G. Knepley 
122fea14342SMatthew G. Knepley   PetscFunctionBegin;
123fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
124fea14342SMatthew G. Knepley   /* Non-parallel lines */
125fea14342SMatthew G. Knepley   if (denom != 0.0) {
126fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
127fea14342SMatthew G. Knepley     const PetscReal t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
128fea14342SMatthew G. Knepley 
129fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
130fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
131fea14342SMatthew G. Knepley       if (intersection) {
132fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
133fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
134fea14342SMatthew G. Knepley       }
135fea14342SMatthew G. Knepley     }
136fea14342SMatthew G. Knepley   }
137fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
138fea14342SMatthew G. Knepley }
139fea14342SMatthew G. Knepley 
140ddce0771SMatthew G. Knepley /* The plane is segmentB x segmentC: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */
141d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetLinePlaneIntersection_3D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], const PetscReal segmentC[], PetscReal intersection[], PetscBool *hasIntersection)
142d71ae5a4SJacob Faibussowitsch {
143ddce0771SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0 * 3 + 0];
144ddce0771SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0 * 3 + 1];
145ddce0771SMatthew G. Knepley   const PetscReal p0_z  = segmentA[0 * 3 + 2];
146ddce0771SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1 * 3 + 0];
147ddce0771SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1 * 3 + 1];
148ddce0771SMatthew G. Knepley   const PetscReal p1_z  = segmentA[1 * 3 + 2];
149ddce0771SMatthew G. Knepley   const PetscReal q0_x  = segmentB[0 * 3 + 0];
150ddce0771SMatthew G. Knepley   const PetscReal q0_y  = segmentB[0 * 3 + 1];
151ddce0771SMatthew G. Knepley   const PetscReal q0_z  = segmentB[0 * 3 + 2];
152ddce0771SMatthew G. Knepley   const PetscReal q1_x  = segmentB[1 * 3 + 0];
153ddce0771SMatthew G. Knepley   const PetscReal q1_y  = segmentB[1 * 3 + 1];
154ddce0771SMatthew G. Knepley   const PetscReal q1_z  = segmentB[1 * 3 + 2];
155ddce0771SMatthew G. Knepley   const PetscReal r0_x  = segmentC[0 * 3 + 0];
156ddce0771SMatthew G. Knepley   const PetscReal r0_y  = segmentC[0 * 3 + 1];
157ddce0771SMatthew G. Knepley   const PetscReal r0_z  = segmentC[0 * 3 + 2];
158ddce0771SMatthew G. Knepley   const PetscReal r1_x  = segmentC[1 * 3 + 0];
159ddce0771SMatthew G. Knepley   const PetscReal r1_y  = segmentC[1 * 3 + 1];
160ddce0771SMatthew G. Knepley   const PetscReal r1_z  = segmentC[1 * 3 + 2];
161ddce0771SMatthew G. Knepley   const PetscReal s0_x  = p1_x - p0_x;
162ddce0771SMatthew G. Knepley   const PetscReal s0_y  = p1_y - p0_y;
163ddce0771SMatthew G. Knepley   const PetscReal s0_z  = p1_z - p0_z;
164ddce0771SMatthew G. Knepley   const PetscReal s1_x  = q1_x - q0_x;
165ddce0771SMatthew G. Knepley   const PetscReal s1_y  = q1_y - q0_y;
166ddce0771SMatthew G. Knepley   const PetscReal s1_z  = q1_z - q0_z;
167ddce0771SMatthew G. Knepley   const PetscReal s2_x  = r1_x - r0_x;
168ddce0771SMatthew G. Knepley   const PetscReal s2_y  = r1_y - r0_y;
169ddce0771SMatthew G. Knepley   const PetscReal s2_z  = r1_z - r0_z;
170ddce0771SMatthew G. Knepley   const PetscReal s3_x  = s1_y * s2_z - s1_z * s2_y; /* s1 x s2 */
171ddce0771SMatthew G. Knepley   const PetscReal s3_y  = s1_z * s2_x - s1_x * s2_z;
172ddce0771SMatthew G. Knepley   const PetscReal s3_z  = s1_x * s2_y - s1_y * s2_x;
173ddce0771SMatthew G. Knepley   const PetscReal s4_x  = s0_y * s2_z - s0_z * s2_y; /* s0 x s2 */
174ddce0771SMatthew G. Knepley   const PetscReal s4_y  = s0_z * s2_x - s0_x * s2_z;
175ddce0771SMatthew G. Knepley   const PetscReal s4_z  = s0_x * s2_y - s0_y * s2_x;
176ddce0771SMatthew G. Knepley   const PetscReal s5_x  = s1_y * s0_z - s1_z * s0_y; /* s1 x s0 */
177ddce0771SMatthew G. Knepley   const PetscReal s5_y  = s1_z * s0_x - s1_x * s0_z;
178ddce0771SMatthew G. Knepley   const PetscReal s5_z  = s1_x * s0_y - s1_y * s0_x;
179ddce0771SMatthew G. Knepley   const PetscReal denom = -(s0_x * s3_x + s0_y * s3_y + s0_z * s3_z); /* -s0 . (s1 x s2) */
180ddce0771SMatthew G. Knepley 
181ddce0771SMatthew G. Knepley   PetscFunctionBegin;
182ddce0771SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
183ddce0771SMatthew G. Knepley   /* Line not parallel to plane */
184ddce0771SMatthew G. Knepley   if (denom != 0.0) {
185ddce0771SMatthew G. Knepley     const PetscReal t = (s3_x * (p0_x - q0_x) + s3_y * (p0_y - q0_y) + s3_z * (p0_z - q0_z)) / denom;
186ddce0771SMatthew G. Knepley     const PetscReal u = (s4_x * (p0_x - q0_x) + s4_y * (p0_y - q0_y) + s4_z * (p0_z - q0_z)) / denom;
187ddce0771SMatthew G. Knepley     const PetscReal v = (s5_x * (p0_x - q0_x) + s5_y * (p0_y - q0_y) + s5_z * (p0_z - q0_z)) / denom;
188ddce0771SMatthew G. Knepley 
189ddce0771SMatthew G. Knepley     if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && v >= 0 && v <= 1) {
190ddce0771SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
191ddce0771SMatthew G. Knepley       if (intersection) {
192ddce0771SMatthew G. Knepley         intersection[0] = p0_x + (t * s0_x);
193ddce0771SMatthew G. Knepley         intersection[1] = p0_y + (t * s0_y);
194ddce0771SMatthew G. Knepley         intersection[2] = p0_z + (t * s0_z);
195ddce0771SMatthew G. Knepley       }
196ddce0771SMatthew G. Knepley     }
197ddce0771SMatthew G. Knepley   }
198ddce0771SMatthew G. Knepley   PetscFunctionReturn(0);
199ddce0771SMatthew G. Knepley }
200ddce0771SMatthew G. Knepley 
201d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
202d71ae5a4SJacob Faibussowitsch {
20314bbb9f0SLawrence Mitchell   const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON;
20414bbb9f0SLawrence Mitchell   const PetscReal x   = PetscRealPart(point[0]);
20514bbb9f0SLawrence Mitchell   PetscReal       v0, J, invJ, detJ;
20614bbb9f0SLawrence Mitchell   PetscReal       xi;
20714bbb9f0SLawrence Mitchell 
20814bbb9f0SLawrence Mitchell   PetscFunctionBegin;
2099566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ));
21014bbb9f0SLawrence Mitchell   xi = invJ * (x - v0);
21114bbb9f0SLawrence Mitchell 
21214bbb9f0SLawrence Mitchell   if ((xi >= -eps) && (xi <= 2. + eps)) *cell = c;
21314bbb9f0SLawrence Mitchell   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
21414bbb9f0SLawrence Mitchell   PetscFunctionReturn(0);
21514bbb9f0SLawrence Mitchell }
21614bbb9f0SLawrence Mitchell 
217d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
218d71ae5a4SJacob Faibussowitsch {
219ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
220f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
221ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
222ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
223ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
224ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
225ccd2543fSMatthew G Knepley 
226ccd2543fSMatthew G Knepley   PetscFunctionBegin;
2279566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
228ccd2543fSMatthew G Knepley   xi  = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]);
229ccd2543fSMatthew G Knepley   eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]);
230ccd2543fSMatthew G Knepley 
231f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0 + eps)) *cell = c;
232c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
233ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
234ccd2543fSMatthew G Knepley }
235ccd2543fSMatthew G Knepley 
236d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
237d71ae5a4SJacob Faibussowitsch {
23862a38674SMatthew G. Knepley   const PetscInt embedDim = 2;
23962a38674SMatthew G. Knepley   PetscReal      x        = PetscRealPart(point[0]);
24062a38674SMatthew G. Knepley   PetscReal      y        = PetscRealPart(point[1]);
24162a38674SMatthew G. Knepley   PetscReal      v0[2], J[4], invJ[4], detJ;
24262a38674SMatthew G. Knepley   PetscReal      xi, eta, r;
24362a38674SMatthew G. Knepley 
24462a38674SMatthew G. Knepley   PetscFunctionBegin;
2459566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
24662a38674SMatthew G. Knepley   xi  = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]);
24762a38674SMatthew G. Knepley   eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]);
24862a38674SMatthew G. Knepley 
24962a38674SMatthew G. Knepley   xi  = PetscMax(xi, 0.0);
25062a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
25162a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
25262a38674SMatthew G. Knepley     r = (xi + eta) / 2.0;
25362a38674SMatthew G. Knepley     xi /= r;
25462a38674SMatthew G. Knepley     eta /= r;
25562a38674SMatthew G. Knepley   }
25662a38674SMatthew G. Knepley   cpoint[0] = J[0 * embedDim + 0] * xi + J[0 * embedDim + 1] * eta + v0[0];
25762a38674SMatthew G. Knepley   cpoint[1] = J[1 * embedDim + 0] * xi + J[1 * embedDim + 1] * eta + v0[1];
25862a38674SMatthew G. Knepley   PetscFunctionReturn(0);
25962a38674SMatthew G. Knepley }
26062a38674SMatthew G. Knepley 
261d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
262d71ae5a4SJacob Faibussowitsch {
26376b3799dSMatthew G. Knepley   const PetscScalar *array;
264a1e44745SMatthew G. Knepley   PetscScalar       *coords    = NULL;
265ccd2543fSMatthew G Knepley   const PetscInt     faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
266ccd2543fSMatthew G Knepley   PetscReal          x         = PetscRealPart(point[0]);
267ccd2543fSMatthew G Knepley   PetscReal          y         = PetscRealPart(point[1]);
26876b3799dSMatthew G. Knepley   PetscInt           crossings = 0, numCoords, f;
26976b3799dSMatthew G. Knepley   PetscBool          isDG;
270ccd2543fSMatthew G Knepley 
271ccd2543fSMatthew G Knepley   PetscFunctionBegin;
27276b3799dSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords));
27376b3799dSMatthew G. Knepley   PetscCheck(numCoords == 8, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Quadrilateral should have 8 coordinates, not %" PetscInt_FMT, numCoords);
274ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
275ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2 * f + 0] * 2 + 0]);
276ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2 * f + 0] * 2 + 1]);
277ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2 * f + 1] * 2 + 0]);
278ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2 * f + 1] * 2 + 1]);
279ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
280ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
281ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
282ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
283ccd2543fSMatthew G Knepley     if ((cond1 || cond2) && above) ++crossings;
284ccd2543fSMatthew G Knepley   }
285ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
286c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
28776b3799dSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords));
288ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
289ccd2543fSMatthew G Knepley }
290ccd2543fSMatthew G Knepley 
291d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
292d71ae5a4SJacob Faibussowitsch {
293ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 3;
29437900f7dSMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
295ccd2543fSMatthew G Knepley   PetscReal       v0[3], J[9], invJ[9], detJ;
296ccd2543fSMatthew G Knepley   PetscReal       x = PetscRealPart(point[0]);
297ccd2543fSMatthew G Knepley   PetscReal       y = PetscRealPart(point[1]);
298ccd2543fSMatthew G Knepley   PetscReal       z = PetscRealPart(point[2]);
299ccd2543fSMatthew G Knepley   PetscReal       xi, eta, zeta;
300ccd2543fSMatthew G Knepley 
301ccd2543fSMatthew G Knepley   PetscFunctionBegin;
3029566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
303ccd2543fSMatthew G Knepley   xi   = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]) + invJ[0 * embedDim + 2] * (z - v0[2]);
304ccd2543fSMatthew G Knepley   eta  = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]) + invJ[1 * embedDim + 2] * (z - v0[2]);
305ccd2543fSMatthew G Knepley   zeta = invJ[2 * embedDim + 0] * (x - v0[0]) + invJ[2 * embedDim + 1] * (y - v0[1]) + invJ[2 * embedDim + 2] * (z - v0[2]);
306ccd2543fSMatthew G Knepley 
30737900f7dSMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0 + eps)) *cell = c;
308c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
309ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
310ccd2543fSMatthew G Knepley }
311ccd2543fSMatthew G Knepley 
312d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
313d71ae5a4SJacob Faibussowitsch {
31476b3799dSMatthew G. Knepley   const PetscScalar *array;
315872a9804SMatthew G. Knepley   PetscScalar       *coords    = NULL;
3169371c9d4SSatish 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};
317ccd2543fSMatthew G Knepley   PetscBool          found     = PETSC_TRUE;
31876b3799dSMatthew G. Knepley   PetscInt           numCoords, f;
31976b3799dSMatthew G. Knepley   PetscBool          isDG;
320ccd2543fSMatthew G Knepley 
321ccd2543fSMatthew G Knepley   PetscFunctionBegin;
32276b3799dSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords));
32376b3799dSMatthew G. Knepley   PetscCheck(numCoords == 24, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Quadrilateral should have 8 coordinates, not %" PetscInt_FMT, numCoords);
324ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
325ccd2543fSMatthew G Knepley     /* Check the point is under plane */
326ccd2543fSMatthew G Knepley     /*   Get face normal */
327ccd2543fSMatthew G Knepley     PetscReal v_i[3];
328ccd2543fSMatthew G Knepley     PetscReal v_j[3];
329ccd2543fSMatthew G Knepley     PetscReal normal[3];
330ccd2543fSMatthew G Knepley     PetscReal pp[3];
331ccd2543fSMatthew G Knepley     PetscReal dot;
332ccd2543fSMatthew G Knepley 
333ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]);
334ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]);
335ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]);
336ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]);
337ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]);
338ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]);
339ccd2543fSMatthew G Knepley     normal[0] = v_i[1] * v_j[2] - v_i[2] * v_j[1];
340ccd2543fSMatthew G Knepley     normal[1] = v_i[2] * v_j[0] - v_i[0] * v_j[2];
341ccd2543fSMatthew G Knepley     normal[2] = v_i[0] * v_j[1] - v_i[1] * v_j[0];
342ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 0] - point[0]);
343ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 1] - point[1]);
344ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 2] - point[2]);
345ccd2543fSMatthew G Knepley     dot       = normal[0] * pp[0] + normal[1] * pp[1] + normal[2] * pp[2];
346ccd2543fSMatthew G Knepley 
347ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
348ccd2543fSMatthew G Knepley     if (dot < 0.0) {
349ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
350ccd2543fSMatthew G Knepley       break;
351ccd2543fSMatthew G Knepley     }
352ccd2543fSMatthew G Knepley   }
353ccd2543fSMatthew G Knepley   if (found) *cell = c;
354c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
35576b3799dSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords));
356ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
357ccd2543fSMatthew G Knepley }
358ccd2543fSMatthew G Knepley 
359d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
360d71ae5a4SJacob Faibussowitsch {
361c4eade1cSMatthew G. Knepley   PetscInt d;
362c4eade1cSMatthew G. Knepley 
363c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
364c4eade1cSMatthew G. Knepley   box->dim = dim;
365c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
366c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
367c4eade1cSMatthew G. Knepley }
368c4eade1cSMatthew G. Knepley 
369d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
370d71ae5a4SJacob Faibussowitsch {
371c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
3729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, box));
3739566063dSJacob Faibussowitsch   PetscCall(PetscGridHashInitialize_Internal(*box, dim, point));
374c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
375c4eade1cSMatthew G. Knepley }
376c4eade1cSMatthew G. Knepley 
377d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
378d71ae5a4SJacob Faibussowitsch {
379c4eade1cSMatthew G. Knepley   PetscInt d;
380c4eade1cSMatthew G. Knepley 
381c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
382c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
383c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
384c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
385c4eade1cSMatthew G. Knepley   }
386c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
387c4eade1cSMatthew G. Knepley }
388c4eade1cSMatthew G. Knepley 
38962a38674SMatthew G. Knepley /*
39062a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
39162a38674SMatthew G. Knepley 
39262a38674SMatthew G. Knepley   Not collective
39362a38674SMatthew G. Knepley 
39462a38674SMatthew G. Knepley   Input Parameters:
39562a38674SMatthew G. Knepley + box - The grid hash object
39662a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
39762a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
39862a38674SMatthew G. Knepley 
39962a38674SMatthew G. Knepley   Level: developer
40062a38674SMatthew G. Knepley 
401db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`
40262a38674SMatthew G. Knepley */
403d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
404d71ae5a4SJacob Faibussowitsch {
405c4eade1cSMatthew G. Knepley   PetscInt d;
406c4eade1cSMatthew G. Knepley 
407c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
408c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
409c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
410c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
411c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
412c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d] / h[d]);
413c4eade1cSMatthew G. Knepley     } else {
414c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
415c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d] / n[d];
416c4eade1cSMatthew G. Knepley     }
417c4eade1cSMatthew G. Knepley   }
418c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
419c4eade1cSMatthew G. Knepley }
420c4eade1cSMatthew G. Knepley 
42162a38674SMatthew G. Knepley /*
42262a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
42362a38674SMatthew G. Knepley 
42462a38674SMatthew G. Knepley   Not collective
42562a38674SMatthew G. Knepley 
42662a38674SMatthew G. Knepley   Input Parameters:
42762a38674SMatthew G. Knepley + box       - The grid hash object
42862a38674SMatthew G. Knepley . numPoints - The number of input points
42962a38674SMatthew G. Knepley - points    - The input point coordinates
43062a38674SMatthew G. Knepley 
43162a38674SMatthew G. Knepley   Output Parameters:
43262a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
43362a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
43462a38674SMatthew G. Knepley 
43562a38674SMatthew G. Knepley   Level: developer
43662a38674SMatthew G. Knepley 
437f5867de0SMatthew G. Knepley   Note:
438f5867de0SMatthew G. Knepley   This only guarantees that a box contains a point, not that a cell does.
439f5867de0SMatthew G. Knepley 
440db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`
44162a38674SMatthew G. Knepley */
442d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
443d71ae5a4SJacob Faibussowitsch {
444c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
445c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
446c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
447c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
448c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
449c4eade1cSMatthew G. Knepley   PetscInt         d, p;
450c4eade1cSMatthew G. Knepley 
451c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
452c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
453c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4541c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]);
455c4eade1cSMatthew G. Knepley 
4561c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1;
4572a705cacSMatthew G. Knepley       if (dbox == -1 && PetscAbsReal(PetscRealPart(points[p * dim + d]) - lower[d]) < 1.0e-9) dbox = 0;
4589371c9d4SSatish Balay       PetscCheck(dbox >= 0 && dbox<n[d], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %" PetscInt_FMT " (%g, %g, %g) is outside of our bounding box", p, (double)PetscRealPart(points[p * dim + 0]), dim> 1 ? (double)PetscRealPart(points[p * dim + 1]) : 0.0, dim > 2 ? (double)PetscRealPart(points[p * dim + 2]) : 0.0);
459c4eade1cSMatthew G. Knepley       dboxes[p * dim + d] = dbox;
460c4eade1cSMatthew G. Knepley     }
4619371c9d4SSatish Balay     if (boxes)
4629371c9d4SSatish 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];
463c4eade1cSMatthew G. Knepley   }
464c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
465c4eade1cSMatthew G. Knepley }
466c4eade1cSMatthew G. Knepley 
467af74b616SDave May /*
468af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
469af74b616SDave May 
470af74b616SDave May  Not collective
471af74b616SDave May 
472af74b616SDave May   Input Parameters:
473af74b616SDave May + box         - The grid hash object
474f5867de0SMatthew G. Knepley . cellSection - The PetscSection mapping cells to boxes
475af74b616SDave May . numPoints   - The number of input points
476af74b616SDave May - points      - The input point coordinates
477af74b616SDave May 
478af74b616SDave May   Output Parameters:
479af74b616SDave May + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
480af74b616SDave May . boxes  - An array of numPoints integers expressing the enclosing box as single number, or NULL
481af74b616SDave May - found  - Flag indicating if point was located within a box
482af74b616SDave May 
483af74b616SDave May   Level: developer
484af74b616SDave May 
485f5867de0SMatthew G. Knepley   Note:
486f5867de0SMatthew G. Knepley   This does an additional check that a cell actually contains the point, and found is PETSC_FALSE if no cell does. Thus, this function requires that the cellSection is already constructed.
487f5867de0SMatthew G. Knepley 
488db781477SPatrick Sanan .seealso: `PetscGridHashGetEnclosingBox()`
489af74b616SDave May */
490d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscSection cellSection, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[], PetscBool *found)
491d71ae5a4SJacob Faibussowitsch {
492af74b616SDave May   const PetscReal *lower = box->lower;
493af74b616SDave May   const PetscReal *upper = box->upper;
494af74b616SDave May   const PetscReal *h     = box->h;
495af74b616SDave May   const PetscInt  *n     = box->n;
496af74b616SDave May   const PetscInt   dim   = box->dim;
497f5867de0SMatthew G. Knepley   PetscInt         bStart, bEnd, d, p;
498af74b616SDave May 
499af74b616SDave May   PetscFunctionBegin;
500f5867de0SMatthew G. Knepley   PetscValidHeaderSpecific(cellSection, PETSC_SECTION_CLASSID, 2);
501af74b616SDave May   *found = PETSC_FALSE;
502f5867de0SMatthew G. Knepley   PetscCall(PetscSectionGetChart(box->cellSection, &bStart, &bEnd));
503af74b616SDave May   for (p = 0; p < numPoints; ++p) {
504af74b616SDave May     for (d = 0; d < dim; ++d) {
505af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]);
506af74b616SDave May 
507af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1;
508f5867de0SMatthew G. Knepley       if (dbox < 0 || dbox >= n[d]) PetscFunctionReturn(0);
509af74b616SDave May       dboxes[p * dim + d] = dbox;
510af74b616SDave May     }
5119371c9d4SSatish Balay     if (boxes)
5129371c9d4SSatish 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];
513f5867de0SMatthew G. Knepley     // It is possible for a box to overlap no grid cells
514f5867de0SMatthew G. Knepley     if (boxes[p] < bStart || boxes[p] >= bEnd) PetscFunctionReturn(0);
515af74b616SDave May   }
516af74b616SDave May   *found = PETSC_TRUE;
517af74b616SDave May   PetscFunctionReturn(0);
518af74b616SDave May }
519af74b616SDave May 
520d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
521d71ae5a4SJacob Faibussowitsch {
522c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
523c4eade1cSMatthew G. Knepley   if (*box) {
5249566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&(*box)->cellSection));
5259566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&(*box)->cells));
5269566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&(*box)->cellsSparse));
527c4eade1cSMatthew G. Knepley   }
5289566063dSJacob Faibussowitsch   PetscCall(PetscFree(*box));
529c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
530c4eade1cSMatthew G. Knepley }
531c4eade1cSMatthew G. Knepley 
532d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
533d71ae5a4SJacob Faibussowitsch {
534ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
535cafe43deSMatthew G. Knepley 
536cafe43deSMatthew G. Knepley   PetscFunctionBegin;
5379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cellStart, &ct));
538ba2698f1SMatthew G. Knepley   switch (ct) {
539d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_SEGMENT:
540d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell));
541d71ae5a4SJacob Faibussowitsch     break;
542d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_TRIANGLE:
543d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell));
544d71ae5a4SJacob Faibussowitsch     break;
545d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUADRILATERAL:
546d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell));
547d71ae5a4SJacob Faibussowitsch     break;
548d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_TETRAHEDRON:
549d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell));
550d71ae5a4SJacob Faibussowitsch     break;
551d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
552d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell));
553d71ae5a4SJacob Faibussowitsch     break;
554d71ae5a4SJacob Faibussowitsch   default:
555d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %" PetscInt_FMT " with type %s", cellStart, DMPolytopeTypes[ct]);
556cafe43deSMatthew G. Knepley   }
557cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
558cafe43deSMatthew G. Knepley }
559cafe43deSMatthew G. Knepley 
56062a38674SMatthew G. Knepley /*
56162a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
56262a38674SMatthew G. Knepley */
563d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
564d71ae5a4SJacob Faibussowitsch {
565ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
56662a38674SMatthew G. Knepley 
56762a38674SMatthew G. Knepley   PetscFunctionBegin;
5689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
569ba2698f1SMatthew G. Knepley   switch (ct) {
570d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_TRIANGLE:
571d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint));
572d71ae5a4SJacob Faibussowitsch     break;
57362a38674SMatthew G. Knepley #if 0
574ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
5759566063dSJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint));break;
576ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
5779566063dSJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint));break;
578ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
5799566063dSJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint));break;
58062a38674SMatthew G. Knepley #endif
581d71ae5a4SJacob Faibussowitsch   default:
582d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %" PetscInt_FMT " with type %s", cell, DMPolytopeTypes[ct]);
58362a38674SMatthew G. Knepley   }
58462a38674SMatthew G. Knepley   PetscFunctionReturn(0);
58562a38674SMatthew G. Knepley }
58662a38674SMatthew G. Knepley 
58762a38674SMatthew G. Knepley /*
58862a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
58962a38674SMatthew G. Knepley 
590d083f849SBarry Smith   Collective on dm
59162a38674SMatthew G. Knepley 
59262a38674SMatthew G. Knepley   Input Parameter:
59362a38674SMatthew G. Knepley . dm - The Plex
59462a38674SMatthew G. Knepley 
59562a38674SMatthew G. Knepley   Output Parameter:
59662a38674SMatthew G. Knepley . localBox - The grid hash object
59762a38674SMatthew G. Knepley 
59862a38674SMatthew G. Knepley   Level: developer
59962a38674SMatthew G. Knepley 
600db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`, `PetscGridHashGetEnclosingBox()`
60162a38674SMatthew G. Knepley */
602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
603d71ae5a4SJacob Faibussowitsch {
604f5867de0SMatthew G. Knepley   PetscInt           debug = ((DM_Plex *)dm->data)->printLocate;
605cafe43deSMatthew G. Knepley   MPI_Comm           comm;
606cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
607*96217254SMatthew G. Knepley   PetscSF            sf;
608cafe43deSMatthew G. Knepley   Vec                coordinates;
609cafe43deSMatthew G. Knepley   PetscSection       coordSection;
610cafe43deSMatthew G. Knepley   Vec                coordsLocal;
611cafe43deSMatthew G. Knepley   const PetscScalar *coords;
612ddce0771SMatthew G. Knepley   PetscScalar       *edgeCoords;
613722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
614*96217254SMatthew G. Knepley   const PetscInt    *leaves;
615ddce0771SMatthew G. Knepley   PetscInt           n[3] = {2, 2, 2};
616*96217254SMatthew G. Knepley   PetscInt           dim, N, Nl = 0, maxConeSize, cStart, cEnd, c, eStart, eEnd, i;
617ddce0771SMatthew G. Knepley   PetscBool          flg;
618cafe43deSMatthew G. Knepley 
619cafe43deSMatthew G. Knepley   PetscFunctionBegin;
6209566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
6219566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
6229566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
6239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
6249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
6259566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(coordinates, &N));
6269566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
6279566063dSJacob Faibussowitsch   PetscCall(PetscGridHashCreate(comm, dim, coords, &lbox));
6289566063dSJacob Faibussowitsch   for (i = 0; i < N; i += dim) PetscCall(PetscGridHashEnlarge(lbox, &coords[i]));
6299566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
630ddce0771SMatthew G. Knepley   c = dim;
6319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, "-dm_plex_hash_box_faces", n, &c, &flg));
6329371c9d4SSatish Balay   if (flg) {
6339371c9d4SSatish Balay     for (i = c; i < dim; ++i) n[i] = n[c - 1];
6349371c9d4SSatish Balay   } else {
6359371c9d4SSatish Balay     for (i = 0; i < dim; ++i) n[i] = PetscMax(2, PetscFloorReal(PetscPowReal((PetscReal)(cEnd - cStart), 1.0 / dim) * 0.8));
6369371c9d4SSatish Balay   }
6379566063dSJacob Faibussowitsch   PetscCall(PetscGridHashSetGrid(lbox, n, NULL));
6389371c9d4SSatish Balay   if (debug)
6399371c9d4SSatish Balay     PetscCall(PetscPrintf(PETSC_COMM_SELF, "GridHash:\n  (%g, %g, %g) -- (%g, %g, %g)\n  n %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n  h %g %g %g\n", (double)lbox->lower[0], (double)lbox->lower[1], (double)lbox->lower[2], (double)lbox->upper[0],
6409371c9d4SSatish Balay                           (double)lbox->upper[1], (double)lbox->upper[2], n[0], n[1], n[2], (double)lbox->h[0], (double)lbox->h[1], (double)lbox->h[2]));
641cafe43deSMatthew G. Knepley #if 0
642cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
6431c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm));
6441c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm));
645cafe43deSMatthew G. Knepley #endif
646cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
647cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
648cafe43deSMatthew G. Knepley   /* Create label */
6499566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
650b26b5bf9SMatthew G. Knepley   if (dim < 2) eStart = eEnd = -1;
6519566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse));
6529566063dSJacob Faibussowitsch   PetscCall(DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd));
653a8d69d7bSBarry Smith   /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
6549566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
6559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
656*96217254SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &sf));
657*96217254SMatthew G. Knepley   if (sf) PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
658*96217254SMatthew G. Knepley   Nl = PetscMax(Nl, 0);
6599566063dSJacob Faibussowitsch   PetscCall(PetscCalloc3(16 * dim, &dboxes, 16, &boxes, PetscPowInt(maxConeSize, dim) * dim, &edgeCoords));
660cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
661cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
662cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
66338353de4SMatthew G. Knepley     PetscInt         csize   = 0;
664ddce0771SMatthew G. Knepley     PetscInt        *closure = NULL;
665*96217254SMatthew G. Knepley     PetscInt         Ncl, cl, Ne = 0, idx;
666cafe43deSMatthew G. Knepley     PetscScalar      point[3];
667cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
668cafe43deSMatthew G. Knepley 
669*96217254SMatthew G. Knepley     PetscCall(PetscFindInt(c, Nl, leaves, &idx));
670*96217254SMatthew G. Knepley     if (idx >= 0) continue;
671ddce0771SMatthew G. Knepley     /* Get all edges in cell */
6729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure));
673ddce0771SMatthew G. Knepley     for (cl = 0; cl < Ncl * 2; ++cl) {
674ddce0771SMatthew G. Knepley       if ((closure[cl] >= eStart) && (closure[cl] < eEnd)) {
675ddce0771SMatthew G. Knepley         PetscScalar *ecoords = &edgeCoords[Ne * dim * 2];
676ddce0771SMatthew G. Knepley         PetscInt     ecsize  = dim * 2;
677ddce0771SMatthew G. Knepley 
6789566063dSJacob Faibussowitsch         PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, closure[cl], &ecsize, &ecoords));
67963a3b9bcSJacob Faibussowitsch         PetscCheck(ecsize == dim * 2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Got %" PetscInt_FMT " coords for edge, instead of %" PetscInt_FMT, ecsize, dim * 2);
680ddce0771SMatthew G. Knepley         ++Ne;
681ddce0771SMatthew G. Knepley       }
682ddce0771SMatthew G. Knepley     }
6839566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure));
684cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
6859566063dSJacob Faibussowitsch     PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords));
6869566063dSJacob Faibussowitsch     PetscCall(PetscGridHashGetEnclosingBox(lbox, csize / dim, ccoords, dboxes, boxes));
687722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
688ddce0771SMatthew G. Knepley     for (e = 0; e < csize / dim; ++e) {
6899371c9d4SSatish Balay       if (debug)
6909371c9d4SSatish Balay         PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " has vertex in box %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ")\n", c, boxes[e], dboxes[e * dim + 0], dim > 1 ? dboxes[e * dim + 1] : -1, dim > 2 ? dboxes[e * dim + 2] : -1));
6919566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(lbox->cellsSparse, c, boxes[e]));
692ddce0771SMatthew G. Knepley     }
693cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
694ad540459SPierre Jolivet     for (d = 0; d < dim; ++d) dlim[d * 2 + 0] = dlim[d * 2 + 1] = dboxes[d];
695ad540459SPierre Jolivet     for (d = dim; d < 3; ++d) dlim[d * 2 + 0] = dlim[d * 2 + 1] = 0;
696cafe43deSMatthew G. Knepley     for (e = 1; e < dim + 1; ++e) {
697cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
698cafe43deSMatthew G. Knepley         dlim[d * 2 + 0] = PetscMin(dlim[d * 2 + 0], dboxes[e * dim + d]);
699cafe43deSMatthew G. Knepley         dlim[d * 2 + 1] = PetscMax(dlim[d * 2 + 1], dboxes[e * dim + d]);
700cafe43deSMatthew G. Knepley       }
701cafe43deSMatthew G. Knepley     }
702fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
703cafe43deSMatthew G. Knepley     for (k = dlim[2 * 2 + 0], point[2] = lbox->lower[2] + k * h[2]; k <= dlim[2 * 2 + 1]; ++k, point[2] += h[2]) {
704cafe43deSMatthew G. Knepley       for (j = dlim[1 * 2 + 0], point[1] = lbox->lower[1] + j * h[1]; j <= dlim[1 * 2 + 1]; ++j, point[1] += h[1]) {
705cafe43deSMatthew G. Knepley         for (i = dlim[0 * 2 + 0], point[0] = lbox->lower[0] + i * h[0]; i <= dlim[0 * 2 + 1]; ++i, point[0] += h[0]) {
706cafe43deSMatthew G. Knepley           const PetscInt box = (k * lbox->n[1] + j) * lbox->n[0] + i;
707cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
708fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
709cafe43deSMatthew G. Knepley 
7109371c9d4SSatish Balay           if (debug)
7119371c9d4SSatish Balay             PetscCall(PetscPrintf(PETSC_COMM_SELF, "Box %" PetscInt_FMT ": (%.2g, %.2g, %.2g) -- (%.2g, %.2g, %.2g)\n", box, (double)PetscRealPart(point[0]), (double)PetscRealPart(point[1]), (double)PetscRealPart(point[2]), (double)PetscRealPart(point[0] + h[0]), (double)PetscRealPart(point[1] + h[1]), (double)PetscRealPart(point[2] + h[2])));
712ddce0771SMatthew G. Knepley           /* Check whether cell contains any vertex of this subbox TODO vectorize this */
713cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
714cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
715cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
7169566063dSJacob Faibussowitsch                 PetscCall(DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell));
7170b6bfacdSStefano Zampini                 if (cell >= 0) {
7189371c9d4SSatish Balay                   if (debug)
7199371c9d4SSatish Balay                     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Cell %" PetscInt_FMT " contains vertex (%.2g, %.2g, %.2g) of box %" PetscInt_FMT "\n", c, (double)PetscRealPart(cpoint[0]), (double)PetscRealPart(cpoint[1]), (double)PetscRealPart(cpoint[2]), box));
7209566063dSJacob Faibussowitsch                   PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box));
7210b6bfacdSStefano Zampini                   jj = kk = 2;
7220b6bfacdSStefano Zampini                   break;
7230b6bfacdSStefano Zampini                 }
724cafe43deSMatthew G. Knepley               }
725cafe43deSMatthew G. Knepley             }
726cafe43deSMatthew G. Knepley           }
727ddce0771SMatthew G. Knepley           /* Check whether cell edge intersects any face of these subboxes TODO vectorize this */
728ddce0771SMatthew G. Knepley           for (edge = 0; edge < Ne; ++edge) {
729a5cae605SSatish Balay             PetscReal segA[6] = {0., 0., 0., 0., 0., 0.};
730a5cae605SSatish Balay             PetscReal segB[6] = {0., 0., 0., 0., 0., 0.};
731a5cae605SSatish Balay             PetscReal segC[6] = {0., 0., 0., 0., 0., 0.};
732fea14342SMatthew G. Knepley 
73363a3b9bcSJacob Faibussowitsch             PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected dim %" PetscInt_FMT " > 3", dim);
734ddce0771SMatthew G. Knepley             for (d = 0; d < dim * 2; ++d) segA[d] = PetscRealPart(edgeCoords[edge * dim * 2 + d]);
735ddce0771SMatthew G. Knepley             /* 1D: (x) -- (x+h)               0 -- 1
736ddce0771SMatthew G. Knepley                2D: (x,   y)   -- (x,   y+h)   (0, 0) -- (0, 1)
737ddce0771SMatthew G. Knepley                    (x+h, y)   -- (x+h, y+h)   (1, 0) -- (1, 1)
738ddce0771SMatthew G. Knepley                    (x,   y)   -- (x+h, y)     (0, 0) -- (1, 0)
739ddce0771SMatthew G. Knepley                    (x,   y+h) -- (x+h, y+h)   (0, 1) -- (1, 1)
740ddce0771SMatthew G. Knepley                3D: (x,   y,   z)   -- (x,   y+h, z),   (x,   y,   z)   -- (x,   y,   z+h) (0, 0, 0) -- (0, 1, 0), (0, 0, 0) -- (0, 0, 1)
741ddce0771SMatthew G. Knepley                    (x+h, y,   z)   -- (x+h, y+h, z),   (x+h, y,   z)   -- (x+h, y,   z+h) (1, 0, 0) -- (1, 1, 0), (1, 0, 0) -- (1, 0, 1)
742ddce0771SMatthew G. Knepley                    (x,   y,   z)   -- (x+h, y,   z),   (x,   y,   z)   -- (x,   y,   z+h) (0, 0, 0) -- (1, 0, 0), (0, 0, 0) -- (0, 0, 1)
743ddce0771SMatthew G. Knepley                    (x,   y+h, z)   -- (x+h, y+h, z),   (x,   y+h, z)   -- (x,   y+h, z+h) (0, 1, 0) -- (1, 1, 0), (0, 1, 0) -- (0, 1, 1)
744ddce0771SMatthew G. Knepley                    (x,   y,   z)   -- (x+h, y,   z),   (x,   y,   z)   -- (x,   y+h, z)   (0, 0, 0) -- (1, 0, 0), (0, 0, 0) -- (0, 1, 0)
745ddce0771SMatthew G. Knepley                    (x,   y,   z+h) -- (x+h, y,   z+h), (x,   y,   z+h) -- (x,   y+h, z+h) (0, 0, 1) -- (1, 0, 1), (0, 0, 1) -- (0, 1, 1)
746ddce0771SMatthew G. Knepley              */
747ddce0771SMatthew G. Knepley             /* Loop over faces with normal in direction d */
748ddce0771SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
749ddce0771SMatthew G. Knepley               PetscBool intersects = PETSC_FALSE;
750ddce0771SMatthew G. Knepley               PetscInt  e          = (d + 1) % dim;
751ddce0771SMatthew G. Knepley               PetscInt  f          = (d + 2) % dim;
752ddce0771SMatthew G. Knepley 
753ddce0771SMatthew G. Knepley               /* There are two faces in each dimension */
754ddce0771SMatthew G. Knepley               for (ii = 0; ii < 2; ++ii) {
755ddce0771SMatthew G. Knepley                 segB[d]       = PetscRealPart(point[d] + ii * h[d]);
756ddce0771SMatthew G. Knepley                 segB[dim + d] = PetscRealPart(point[d] + ii * h[d]);
757ddce0771SMatthew G. Knepley                 segC[d]       = PetscRealPart(point[d] + ii * h[d]);
758ddce0771SMatthew G. Knepley                 segC[dim + d] = PetscRealPart(point[d] + ii * h[d]);
759ddce0771SMatthew G. Knepley                 if (dim > 1) {
760ddce0771SMatthew G. Knepley                   segB[e]       = PetscRealPart(point[e] + 0 * h[e]);
761ddce0771SMatthew G. Knepley                   segB[dim + e] = PetscRealPart(point[e] + 1 * h[e]);
762ddce0771SMatthew G. Knepley                   segC[e]       = PetscRealPart(point[e] + 0 * h[e]);
763ddce0771SMatthew G. Knepley                   segC[dim + e] = PetscRealPart(point[e] + 0 * h[e]);
764ddce0771SMatthew G. Knepley                 }
765ddce0771SMatthew G. Knepley                 if (dim > 2) {
766ddce0771SMatthew G. Knepley                   segB[f]       = PetscRealPart(point[f] + 0 * h[f]);
767ddce0771SMatthew G. Knepley                   segB[dim + f] = PetscRealPart(point[f] + 0 * h[f]);
768ddce0771SMatthew G. Knepley                   segC[f]       = PetscRealPart(point[f] + 0 * h[f]);
769ddce0771SMatthew G. Knepley                   segC[dim + f] = PetscRealPart(point[f] + 1 * h[f]);
770ddce0771SMatthew G. Knepley                 }
771ddce0771SMatthew G. Knepley                 if (dim == 2) {
7729566063dSJacob Faibussowitsch                   PetscCall(DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects));
773ddce0771SMatthew G. Knepley                 } else if (dim == 3) {
7749566063dSJacob Faibussowitsch                   PetscCall(DMPlexGetLinePlaneIntersection_3D_Internal(segA, segB, segC, NULL, &intersects));
775ddce0771SMatthew G. Knepley                 }
776ddce0771SMatthew G. Knepley                 if (intersects) {
7779371c9d4SSatish Balay                   if (debug)
7789371c9d4SSatish Balay                     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Cell %" PetscInt_FMT " edge %" PetscInt_FMT " (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g) intersects box %" PetscInt_FMT ", face (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g) (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g)\n", c, edge, (double)segA[0], (double)segA[1], (double)segA[2], (double)segA[3], (double)segA[4], (double)segA[5], box, (double)segB[0], (double)segB[1], (double)segB[2], (double)segB[3], (double)segB[4], (double)segB[5], (double)segC[0], (double)segC[1], (double)segC[2], (double)segC[3], (double)segC[4], (double)segC[5]));
7799371c9d4SSatish Balay                   PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box));
7809371c9d4SSatish Balay                   edge = Ne;
7819371c9d4SSatish Balay                   break;
782ddce0771SMatthew G. Knepley                 }
783ddce0771SMatthew G. Knepley               }
784ddce0771SMatthew G. Knepley             }
785cafe43deSMatthew G. Knepley           }
786fea14342SMatthew G. Knepley         }
787fea14342SMatthew G. Knepley       }
788fea14342SMatthew G. Knepley     }
7899566063dSJacob Faibussowitsch     PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords));
790fea14342SMatthew G. Knepley   }
7919566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dboxes, boxes, edgeCoords));
7929566063dSJacob Faibussowitsch   if (debug) PetscCall(DMLabelView(lbox->cellsSparse, PETSC_VIEWER_STDOUT_SELF));
7939566063dSJacob Faibussowitsch   PetscCall(DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells));
7949566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&lbox->cellsSparse));
795cafe43deSMatthew G. Knepley   *localBox = lbox;
796cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
797cafe43deSMatthew G. Knepley }
798cafe43deSMatthew G. Knepley 
799d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
800d71ae5a4SJacob Faibussowitsch {
801f5867de0SMatthew G. Knepley   PetscInt        debug = ((DM_Plex *)dm->data)->printLocate;
802cafe43deSMatthew G. Knepley   DM_Plex        *mesh  = (DM_Plex *)dm->data;
803af74b616SDave May   PetscBool       hash = mesh->useHashLocation, reuse = PETSC_FALSE;
8043a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
805d8206211SMatthew G. Knepley   PetscInt        dim, Nl = 0, cStart, cEnd, numCells, c, d;
806d8206211SMatthew G. Knepley   PetscSF         sf;
807d8206211SMatthew G. Knepley   const PetscInt *leaves;
808cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
8093a93e3b7SToby Isaac   PetscSFNode    *cells;
810ccd2543fSMatthew G Knepley   PetscScalar    *a;
8113a93e3b7SToby Isaac   PetscMPIInt     result;
812af74b616SDave May   PetscLogDouble  t0, t1;
8139cb35068SDave May   PetscReal       gmin[3], gmax[3];
8149cb35068SDave May   PetscInt        terminating_query_type[] = {0, 0, 0};
815ccd2543fSMatthew G Knepley 
816ccd2543fSMatthew G Knepley   PetscFunctionBegin;
8179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_LocatePoints, 0, 0, 0, 0));
8189566063dSJacob Faibussowitsch   PetscCall(PetscTime(&t0));
8191dca8a05SBarry 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.");
8209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
8219566063dSJacob Faibussowitsch   PetscCall(VecGetBlockSize(v, &bs));
8229566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF), PETSC_COMM_SELF, &result));
8231dca8a05SBarry Smith   PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT, PetscObjectComm((PetscObject)cellSF), PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
82463a3b9bcSJacob 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);
8256858538eSMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalSetUp(dm));
8269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
827d8206211SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &sf));
828d8206211SMatthew G. Knepley   if (sf) PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
829d8206211SMatthew G. Knepley   Nl = PetscMax(Nl, 0);
8309566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(v, &numPoints));
8319566063dSJacob Faibussowitsch   PetscCall(VecGetArray(v, &a));
832ccd2543fSMatthew G Knepley   numPoints /= bs;
833af74b616SDave May   {
834af74b616SDave May     const PetscSFNode *sf_cells;
835af74b616SDave May 
8369566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(cellSF, NULL, NULL, NULL, &sf_cells));
837af74b616SDave May     if (sf_cells) {
8389566063dSJacob Faibussowitsch       PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Re-using existing StarForest node list\n"));
839af74b616SDave May       cells = (PetscSFNode *)sf_cells;
840af74b616SDave May       reuse = PETSC_TRUE;
841af74b616SDave May     } else {
8429566063dSJacob Faibussowitsch       PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n"));
8439566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numPoints, &cells));
844af74b616SDave May       /* initialize cells if created */
845af74b616SDave May       for (p = 0; p < numPoints; p++) {
846af74b616SDave May         cells[p].rank  = 0;
847af74b616SDave May         cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
848af74b616SDave May       }
849af74b616SDave May     }
850af74b616SDave May   }
85176b3799dSMatthew G. Knepley   PetscCall(DMGetBoundingBox(dm, gmin, gmax));
852953fc75cSMatthew G. Knepley   if (hash) {
8539371c9d4SSatish Balay     if (!mesh->lbox) {
854*96217254SMatthew G. Knepley       PetscCall(PetscInfo(dm, "Initializing grid hashing\n"));
8559371c9d4SSatish Balay       PetscCall(DMPlexComputeGridHash_Internal(dm, &mesh->lbox));
8569371c9d4SSatish Balay     }
857cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
858cafe43deSMatthew G. Knepley     /* Send points to correct process */
859cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
860cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
8619566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(mesh->lbox->cells, &boxCells));
862953fc75cSMatthew G. Knepley   }
8633a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
864ccd2543fSMatthew G Knepley     const PetscScalar *point   = &a[p * bs];
865e56f9228SJed Brown     PetscInt           dbin[3] = {-1, -1, -1}, bin, cell = -1, cellOffset;
8669cb35068SDave May     PetscBool          point_outside_domain = PETSC_FALSE;
867ccd2543fSMatthew G Knepley 
8689cb35068SDave May     /* check bounding box of domain */
8699cb35068SDave May     for (d = 0; d < dim; d++) {
8709371c9d4SSatish Balay       if (PetscRealPart(point[d]) < gmin[d]) {
8719371c9d4SSatish Balay         point_outside_domain = PETSC_TRUE;
8729371c9d4SSatish Balay         break;
8739371c9d4SSatish Balay       }
8749371c9d4SSatish Balay       if (PetscRealPart(point[d]) > gmax[d]) {
8759371c9d4SSatish Balay         point_outside_domain = PETSC_TRUE;
8769371c9d4SSatish Balay         break;
8779371c9d4SSatish Balay       }
8789cb35068SDave May     }
8799cb35068SDave May     if (point_outside_domain) {
880e9b685f5SMatthew G. Knepley       cells[p].rank  = 0;
881e9b685f5SMatthew G. Knepley       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
8829cb35068SDave May       terminating_query_type[0]++;
8839cb35068SDave May       continue;
8849cb35068SDave May     }
885ccd2543fSMatthew G Knepley 
886af74b616SDave May     /* check initial values in cells[].index - abort early if found */
887af74b616SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
888af74b616SDave May       c              = cells[p].index;
8893a93e3b7SToby Isaac       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
8909566063dSJacob Faibussowitsch       PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell));
891af74b616SDave May       if (cell >= 0) {
892af74b616SDave May         cells[p].rank  = 0;
893af74b616SDave May         cells[p].index = cell;
894af74b616SDave May         numFound++;
895af74b616SDave May       }
896af74b616SDave May     }
8979cb35068SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
8989cb35068SDave May       terminating_query_type[1]++;
8999cb35068SDave May       continue;
9009cb35068SDave May     }
901af74b616SDave May 
902953fc75cSMatthew G. Knepley     if (hash) {
903af74b616SDave May       PetscBool found_box;
904af74b616SDave May 
90563a3b9bcSJacob Faibussowitsch       if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Checking point %" PetscInt_FMT " (%.2g, %.2g, %.2g)\n", p, (double)PetscRealPart(point[0]), (double)PetscRealPart(point[1]), (double)PetscRealPart(point[2])));
906af74b616SDave May       /* allow for case that point is outside box - abort early */
907f5867de0SMatthew G. Knepley       PetscCall(PetscGridHashGetEnclosingBoxQuery(mesh->lbox, mesh->lbox->cellSection, 1, point, dbin, &bin, &found_box));
908af74b616SDave May       if (found_box) {
90963a3b9bcSJacob Faibussowitsch         if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Found point in box %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ")\n", bin, dbin[0], dbin[1], dbin[2]));
910cafe43deSMatthew G. Knepley         /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
9119566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells));
9129566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset));
913cafe43deSMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
91463a3b9bcSJacob Faibussowitsch           if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "    Checking for point in cell %" PetscInt_FMT "\n", boxCells[c]));
9159566063dSJacob Faibussowitsch           PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell));
9163a93e3b7SToby Isaac           if (cell >= 0) {
91763a3b9bcSJacob Faibussowitsch             if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "      FOUND in cell %" PetscInt_FMT "\n", cell));
9183a93e3b7SToby Isaac             cells[p].rank  = 0;
9193a93e3b7SToby Isaac             cells[p].index = cell;
9203a93e3b7SToby Isaac             numFound++;
9219cb35068SDave May             terminating_query_type[2]++;
9223a93e3b7SToby Isaac             break;
923ccd2543fSMatthew G Knepley           }
9243a93e3b7SToby Isaac         }
925af74b616SDave May       }
926953fc75cSMatthew G. Knepley     } else {
927953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
928d8206211SMatthew G. Knepley         PetscInt idx;
929d8206211SMatthew G. Knepley 
930d8206211SMatthew G. Knepley         PetscCall(PetscFindInt(c, Nl, leaves, &idx));
931d8206211SMatthew G. Knepley         if (idx >= 0) continue;
9329566063dSJacob Faibussowitsch         PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell));
9333a93e3b7SToby Isaac         if (cell >= 0) {
9343a93e3b7SToby Isaac           cells[p].rank  = 0;
9353a93e3b7SToby Isaac           cells[p].index = cell;
9363a93e3b7SToby Isaac           numFound++;
9379cb35068SDave May           terminating_query_type[2]++;
9383a93e3b7SToby Isaac           break;
939953fc75cSMatthew G. Knepley         }
940953fc75cSMatthew G. Knepley       }
9413a93e3b7SToby Isaac     }
942ccd2543fSMatthew G Knepley   }
9439566063dSJacob Faibussowitsch   if (hash) PetscCall(ISRestoreIndices(mesh->lbox->cells, &boxCells));
94462a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
94562a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
94662a38674SMatthew G. Knepley       const PetscScalar *point = &a[p * bs];
947d92c4b9fSToby Isaac       PetscReal          cpoint[3], diff[3], best[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL}, dist, distMax = PETSC_MAX_REAL;
948d92c4b9fSToby Isaac       PetscInt           dbin[3] = {-1, -1, -1}, bin, cellOffset, d, bestc = -1;
94962a38674SMatthew G. Knepley 
950e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
9519566063dSJacob Faibussowitsch         PetscCall(PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin));
9529566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells));
9539566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset));
95462a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
9559566063dSJacob Faibussowitsch           PetscCall(DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint));
956b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
95762a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
95862a38674SMatthew G. Knepley           if (dist < distMax) {
959d92c4b9fSToby Isaac             for (d = 0; d < dim; ++d) best[d] = cpoint[d];
960d92c4b9fSToby Isaac             bestc   = boxCells[c];
96162a38674SMatthew G. Knepley             distMax = dist;
96262a38674SMatthew G. Knepley           }
96362a38674SMatthew G. Knepley         }
964d92c4b9fSToby Isaac         if (distMax < PETSC_MAX_REAL) {
965d92c4b9fSToby Isaac           ++numFound;
966d92c4b9fSToby Isaac           cells[p].rank  = 0;
967d92c4b9fSToby Isaac           cells[p].index = bestc;
968d92c4b9fSToby Isaac           for (d = 0; d < dim; ++d) a[p * bs + d] = best[d];
969d92c4b9fSToby Isaac         }
97062a38674SMatthew G. Knepley       }
97162a38674SMatthew G. Knepley     }
97262a38674SMatthew G. Knepley   }
97362a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
974cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
9752d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
9769566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numFound, &found));
9773a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
9783a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
979ad540459SPierre Jolivet         if (numFound < p) cells[numFound] = cells[p];
9803a93e3b7SToby Isaac         found[numFound++] = p;
9813a93e3b7SToby Isaac       }
9823a93e3b7SToby Isaac     }
9833a93e3b7SToby Isaac   }
9849566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(v, &a));
98548a46eb9SPierre Jolivet   if (!reuse) PetscCall(PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER));
9869566063dSJacob Faibussowitsch   PetscCall(PetscTime(&t1));
9879cb35068SDave May   if (hash) {
98863a3b9bcSJacob 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]));
9899cb35068SDave May   } else {
99063a3b9bcSJacob 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]));
9919cb35068SDave May   }
99263a3b9bcSJacob 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))));
9939566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_LocatePoints, 0, 0, 0, 0));
994ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
995ccd2543fSMatthew G Knepley }
996ccd2543fSMatthew G Knepley 
997741bfc07SMatthew G. Knepley /*@C
998741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
999741bfc07SMatthew G. Knepley 
1000741bfc07SMatthew G. Knepley   Not collective
1001741bfc07SMatthew G. Knepley 
10026b867d5aSJose E. Roman   Input/Output Parameter:
10036b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x
1004741bfc07SMatthew G. Knepley 
10056b867d5aSJose E. Roman   Output Parameter:
10066b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
1007741bfc07SMatthew G. Knepley 
1008741bfc07SMatthew G. Knepley   Level: developer
1009741bfc07SMatthew G. Knepley 
1010db781477SPatrick Sanan .seealso: `DMPlexComputeProjection3Dto1D()`, `DMPlexComputeProjection3Dto2D()`
1011741bfc07SMatthew G. Knepley @*/
1012d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
1013d71ae5a4SJacob Faibussowitsch {
101417fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
101517fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
10168b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x * x + y * y), c = x / r, s = y / r;
101717fe8556SMatthew G. Knepley 
101817fe8556SMatthew G. Knepley   PetscFunctionBegin;
10199371c9d4SSatish Balay   R[0]      = c;
10209371c9d4SSatish Balay   R[1]      = -s;
10219371c9d4SSatish Balay   R[2]      = s;
10229371c9d4SSatish Balay   R[3]      = c;
102317fe8556SMatthew G. Knepley   coords[0] = 0.0;
10247f07f362SMatthew G. Knepley   coords[1] = r;
102517fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
102617fe8556SMatthew G. Knepley }
102717fe8556SMatthew G. Knepley 
1028741bfc07SMatthew G. Knepley /*@C
1029741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
103028dbe442SToby Isaac 
1031741bfc07SMatthew G. Knepley   Not collective
103228dbe442SToby Isaac 
10336b867d5aSJose E. Roman   Input/Output Parameter:
10346b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z
1035741bfc07SMatthew G. Knepley 
10366b867d5aSJose E. Roman   Output Parameter:
10376b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
1038741bfc07SMatthew G. Knepley 
1039741bfc07SMatthew G. Knepley   Note: This uses the basis completion described by Frisvad in http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html, DOI:10.1080/2165347X.2012.689606
1040741bfc07SMatthew G. Knepley 
1041741bfc07SMatthew G. Knepley   Level: developer
1042741bfc07SMatthew G. Knepley 
1043db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto2D()`
1044741bfc07SMatthew G. Knepley @*/
1045d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
1046d71ae5a4SJacob Faibussowitsch {
104728dbe442SToby Isaac   PetscReal x    = PetscRealPart(coords[3] - coords[0]);
104828dbe442SToby Isaac   PetscReal y    = PetscRealPart(coords[4] - coords[1]);
104928dbe442SToby Isaac   PetscReal z    = PetscRealPart(coords[5] - coords[2]);
105028dbe442SToby Isaac   PetscReal r    = PetscSqrtReal(x * x + y * y + z * z);
105128dbe442SToby Isaac   PetscReal rinv = 1. / r;
105228dbe442SToby Isaac   PetscFunctionBegin;
105328dbe442SToby Isaac 
10549371c9d4SSatish Balay   x *= rinv;
10559371c9d4SSatish Balay   y *= rinv;
10569371c9d4SSatish Balay   z *= rinv;
105728dbe442SToby Isaac   if (x > 0.) {
105828dbe442SToby Isaac     PetscReal inv1pX = 1. / (1. + x);
105928dbe442SToby Isaac 
10609371c9d4SSatish Balay     R[0] = x;
10619371c9d4SSatish Balay     R[1] = -y;
10629371c9d4SSatish Balay     R[2] = -z;
10639371c9d4SSatish Balay     R[3] = y;
10649371c9d4SSatish Balay     R[4] = 1. - y * y * inv1pX;
10659371c9d4SSatish Balay     R[5] = -y * z * inv1pX;
10669371c9d4SSatish Balay     R[6] = z;
10679371c9d4SSatish Balay     R[7] = -y * z * inv1pX;
10689371c9d4SSatish Balay     R[8] = 1. - z * z * inv1pX;
10699371c9d4SSatish Balay   } else {
107028dbe442SToby Isaac     PetscReal inv1mX = 1. / (1. - x);
107128dbe442SToby Isaac 
10729371c9d4SSatish Balay     R[0] = x;
10739371c9d4SSatish Balay     R[1] = z;
10749371c9d4SSatish Balay     R[2] = y;
10759371c9d4SSatish Balay     R[3] = y;
10769371c9d4SSatish Balay     R[4] = -y * z * inv1mX;
10779371c9d4SSatish Balay     R[5] = 1. - y * y * inv1mX;
10789371c9d4SSatish Balay     R[6] = z;
10799371c9d4SSatish Balay     R[7] = 1. - z * z * inv1mX;
10809371c9d4SSatish Balay     R[8] = -y * z * inv1mX;
108128dbe442SToby Isaac   }
108228dbe442SToby Isaac   coords[0] = 0.0;
108328dbe442SToby Isaac   coords[1] = r;
108428dbe442SToby Isaac   PetscFunctionReturn(0);
108528dbe442SToby Isaac }
108628dbe442SToby Isaac 
1087741bfc07SMatthew G. Knepley /*@
1088c871b86eSJed Brown   DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the
1089c871b86eSJed Brown     plane.  The normal is defined by positive orientation of the first 3 points.
1090741bfc07SMatthew G. Knepley 
1091741bfc07SMatthew G. Knepley   Not collective
1092741bfc07SMatthew G. Knepley 
1093741bfc07SMatthew G. Knepley   Input Parameter:
10946b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points)
1095741bfc07SMatthew G. Knepley 
10966b867d5aSJose E. Roman   Input/Output Parameter:
10976b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first
10986b867d5aSJose E. Roman            2*coordSize/3 entries contain interlaced 2D points, with the rest undefined
10996b867d5aSJose E. Roman 
11006b867d5aSJose E. Roman   Output Parameter:
11016b867d5aSJose 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.
1102741bfc07SMatthew G. Knepley 
1103741bfc07SMatthew G. Knepley   Level: developer
1104741bfc07SMatthew G. Knepley 
1105db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto1D()`
1106741bfc07SMatthew G. Knepley @*/
1107d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
1108d71ae5a4SJacob Faibussowitsch {
1109c871b86eSJed Brown   PetscReal      x1[3], x2[3], n[3], c[3], norm;
1110ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1111c871b86eSJed Brown   PetscInt       d, p;
1112ccd2543fSMatthew G Knepley 
1113ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1114ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
1115ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
11161ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1 * dim + d] - coords[0 * dim + d]);
11171ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2 * dim + d] - coords[0 * dim + d]);
1118ccd2543fSMatthew G Knepley   }
1119c871b86eSJed Brown   // n = x1 \otimes x2
1120ccd2543fSMatthew G Knepley   n[0] = x1[1] * x2[2] - x1[2] * x2[1];
1121ccd2543fSMatthew G Knepley   n[1] = x1[2] * x2[0] - x1[0] * x2[2];
1122ccd2543fSMatthew G Knepley   n[2] = x1[0] * x2[1] - x1[1] * x2[0];
11238b49ba18SBarry Smith   norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
1124c871b86eSJed Brown   for (d = 0; d < dim; d++) n[d] /= norm;
1125c871b86eSJed Brown   norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]);
1126c871b86eSJed Brown   for (d = 0; d < dim; d++) x1[d] /= norm;
1127c871b86eSJed Brown   // x2 = n \otimes x1
1128c871b86eSJed Brown   x2[0] = n[1] * x1[2] - n[2] * x1[1];
1129c871b86eSJed Brown   x2[1] = n[2] * x1[0] - n[0] * x1[2];
1130c871b86eSJed Brown   x2[2] = n[0] * x1[1] - n[1] * x1[0];
1131c871b86eSJed Brown   for (d = 0; d < dim; d++) {
1132c871b86eSJed Brown     R[d * dim + 0] = x1[d];
1133c871b86eSJed Brown     R[d * dim + 1] = x2[d];
1134c871b86eSJed Brown     R[d * dim + 2] = n[d];
1135c871b86eSJed Brown     c[d]           = PetscRealPart(coords[0 * dim + d]);
113673868372SMatthew G. Knepley   }
1137c871b86eSJed Brown   for (p = 0; p < coordSize / dim; p++) {
1138c871b86eSJed Brown     PetscReal y[3];
1139c871b86eSJed Brown     for (d = 0; d < dim; d++) y[d] = PetscRealPart(coords[p * dim + d]) - c[d];
1140c871b86eSJed 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];
11417f07f362SMatthew G. Knepley   }
1142ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1143ccd2543fSMatthew G Knepley }
1144ccd2543fSMatthew G Knepley 
1145d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
1146d71ae5a4SJacob Faibussowitsch {
1147834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
1148834e62ceSMatthew G. Knepley 
1149834e62ceSMatthew G. Knepley    |  1  1  1 |
1150834e62ceSMatthew G. Knepley    | x0 x1 x2 |
1151834e62ceSMatthew G. Knepley    | y0 y1 y2 |
1152834e62ceSMatthew G. Knepley 
1153834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
1154834e62ceSMatthew G. Knepley 
1155834e62ceSMatthew G. Knepley    | x1 x2 |
1156834e62ceSMatthew G. Knepley    | y1 y2 |
1157834e62ceSMatthew G. Knepley   */
1158834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
1159834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
1160834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
11619371c9d4SSatish Balay   M[0] = x1;
11629371c9d4SSatish Balay   M[1] = x2;
11639371c9d4SSatish Balay   M[2] = y1;
11649371c9d4SSatish Balay   M[3] = y2;
1165923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
1166834e62ceSMatthew G. Knepley   *vol = 0.5 * detM;
11673bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
1168834e62ceSMatthew G. Knepley }
1169834e62ceSMatthew G. Knepley 
1170d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
1171d71ae5a4SJacob Faibussowitsch {
1172834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
1173834e62ceSMatthew G. Knepley 
1174834e62ceSMatthew G. Knepley    |  1  1  1  1 |
1175834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
1176834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
1177834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
1178834e62ceSMatthew G. Knepley 
1179834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
1180834e62ceSMatthew G. Knepley 
1181834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1182834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1183834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1184834e62ceSMatthew G. Knepley   */
1185834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2];
1186834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2];
1187834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
11880a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1189834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
11909371c9d4SSatish Balay   M[0] = x1;
11919371c9d4SSatish Balay   M[1] = x2;
11929371c9d4SSatish Balay   M[2] = x3;
11939371c9d4SSatish Balay   M[3] = y1;
11949371c9d4SSatish Balay   M[4] = y2;
11959371c9d4SSatish Balay   M[5] = y3;
11969371c9d4SSatish Balay   M[6] = z1;
11979371c9d4SSatish Balay   M[7] = z2;
11989371c9d4SSatish Balay   M[8] = z3;
1199923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
12000a3da2c2SToby Isaac   *vol = -onesixth * detM;
12013bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1202834e62ceSMatthew G. Knepley }
1203834e62ceSMatthew G. Knepley 
1204d71ae5a4SJacob Faibussowitsch static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
1205d71ae5a4SJacob Faibussowitsch {
12060a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1207923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
12080a3da2c2SToby Isaac   *vol *= -onesixth;
12090ec8681fSMatthew G. Knepley }
12100ec8681fSMatthew G. Knepley 
1211d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1212d71ae5a4SJacob Faibussowitsch {
1213cb92db44SToby Isaac   PetscSection       coordSection;
1214cb92db44SToby Isaac   Vec                coordinates;
1215cb92db44SToby Isaac   const PetscScalar *coords;
1216cb92db44SToby Isaac   PetscInt           dim, d, off;
1217cb92db44SToby Isaac 
1218cb92db44SToby Isaac   PetscFunctionBegin;
12199566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
12209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
12219566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(coordSection, e, &dim));
1222cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
12239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(coordSection, e, &off));
12249566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
12259371c9d4SSatish Balay   if (v0) {
12269371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);
12279371c9d4SSatish Balay   }
12289566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
1229cb92db44SToby Isaac   *detJ = 1.;
1230cb92db44SToby Isaac   if (J) {
1231cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1232cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1233cb92db44SToby Isaac     if (invJ) {
1234cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1235cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1236cb92db44SToby Isaac     }
1237cb92db44SToby Isaac   }
1238cb92db44SToby Isaac   PetscFunctionReturn(0);
1239cb92db44SToby Isaac }
1240cb92db44SToby Isaac 
12416858538eSMatthew G. Knepley /*@C
12426858538eSMatthew G. Knepley   DMPlexGetCellCoordinates - Get coordinates for a cell, taking into account periodicity
12436858538eSMatthew G. Knepley 
12446858538eSMatthew G. Knepley   Not collective
12456858538eSMatthew G. Knepley 
12466858538eSMatthew G. Knepley   Input Parameters:
12476858538eSMatthew G. Knepley + dm   - The DM
12486858538eSMatthew G. Knepley - cell - The cell number
12496858538eSMatthew G. Knepley 
12506858538eSMatthew G. Knepley   Output Parameters:
12516858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
12526858538eSMatthew G. Knepley . Nc     - The number of coordinates
12536858538eSMatthew G. Knepley . array  - The coordinate array
12546858538eSMatthew G. Knepley - coords - The cell coordinates
12556858538eSMatthew G. Knepley 
12566858538eSMatthew G. Knepley   Level: developer
12576858538eSMatthew G. Knepley 
12586858538eSMatthew G. Knepley .seealso: DMPlexRestoreCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal()
12596858538eSMatthew G. Knepley @*/
1260d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[])
1261d71ae5a4SJacob Faibussowitsch {
12626858538eSMatthew G. Knepley   DM                 cdm;
12636858538eSMatthew G. Knepley   Vec                coordinates;
12646858538eSMatthew G. Knepley   PetscSection       cs;
12656858538eSMatthew G. Knepley   const PetscScalar *ccoords;
12666858538eSMatthew G. Knepley   PetscInt           pStart, pEnd;
12676858538eSMatthew G. Knepley 
12686858538eSMatthew G. Knepley   PetscFunctionBeginHot;
12696858538eSMatthew G. Knepley   *isDG   = PETSC_FALSE;
12706858538eSMatthew G. Knepley   *Nc     = 0;
12716858538eSMatthew G. Knepley   *array  = NULL;
12726858538eSMatthew G. Knepley   *coords = NULL;
12736858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12746858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateSection(dm, &cs));
12756858538eSMatthew G. Knepley   if (!cs) goto cg;
12766858538eSMatthew G. Knepley   /* Check that the cell exists in the cellwise section */
12776858538eSMatthew G. Knepley   PetscCall(PetscSectionGetChart(cs, &pStart, &pEnd));
12786858538eSMatthew G. Knepley   if (cell < pStart || cell >= pEnd) goto cg;
12796858538eSMatthew G. Knepley   /* Check for cellwise coordinates for this cell */
12806858538eSMatthew G. Knepley   PetscCall(PetscSectionGetDof(cs, cell, Nc));
12816858538eSMatthew G. Knepley   if (!*Nc) goto cg;
12826858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12836858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocalNoncollective(dm, &coordinates));
12846858538eSMatthew G. Knepley   if (!coordinates) goto cg;
12856858538eSMatthew G. Knepley   /* Get cellwise coordinates */
12866858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dm, &cdm));
12876858538eSMatthew G. Knepley   PetscCall(VecGetArrayRead(coordinates, array));
12886858538eSMatthew G. Knepley   PetscCall(DMPlexPointLocalRead(cdm, cell, *array, &ccoords));
12896858538eSMatthew G. Knepley   PetscCall(DMGetWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
12906858538eSMatthew G. Knepley   PetscCall(PetscArraycpy(*coords, ccoords, *Nc));
12916858538eSMatthew G. Knepley   PetscCall(VecRestoreArrayRead(coordinates, array));
12926858538eSMatthew G. Knepley   *isDG = PETSC_TRUE;
12936858538eSMatthew G. Knepley   PetscFunctionReturn(0);
12946858538eSMatthew G. Knepley cg:
12956858538eSMatthew G. Knepley   /* Use continuous coordinates */
12966858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
12976858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &cs));
12986858538eSMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
12996858538eSMatthew G. Knepley   PetscCall(DMPlexVecGetClosure(cdm, cs, coordinates, cell, Nc, coords));
13006858538eSMatthew G. Knepley   PetscFunctionReturn(0);
13016858538eSMatthew G. Knepley }
13026858538eSMatthew G. Knepley 
13036858538eSMatthew G. Knepley /*@C
13046858538eSMatthew G. Knepley   DMPlexRestoreCellCoordinates - Get coordinates for a cell, taking into account periodicity
13056858538eSMatthew G. Knepley 
13066858538eSMatthew G. Knepley   Not collective
13076858538eSMatthew G. Knepley 
13086858538eSMatthew G. Knepley   Input Parameters:
13096858538eSMatthew G. Knepley + dm   - The DM
13106858538eSMatthew G. Knepley - cell - The cell number
13116858538eSMatthew G. Knepley 
13126858538eSMatthew G. Knepley   Output Parameters:
13136858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
13146858538eSMatthew G. Knepley . Nc     - The number of coordinates
13156858538eSMatthew G. Knepley . array  - The coordinate array
13166858538eSMatthew G. Knepley - coords - The cell coordinates
13176858538eSMatthew G. Knepley 
13186858538eSMatthew G. Knepley   Level: developer
13196858538eSMatthew G. Knepley 
13206858538eSMatthew G. Knepley .seealso: DMPlexGetCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal()
13216858538eSMatthew G. Knepley @*/
1322d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexRestoreCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[])
1323d71ae5a4SJacob Faibussowitsch {
13246858538eSMatthew G. Knepley   DM           cdm;
13256858538eSMatthew G. Knepley   PetscSection cs;
13266858538eSMatthew G. Knepley   Vec          coordinates;
13276858538eSMatthew G. Knepley 
13286858538eSMatthew G. Knepley   PetscFunctionBeginHot;
13296858538eSMatthew G. Knepley   if (*isDG) {
13306858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinateDM(dm, &cdm));
13316858538eSMatthew G. Knepley     PetscCall(DMRestoreWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
13326858538eSMatthew G. Knepley   } else {
13336858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
13346858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateSection(dm, &cs));
13356858538eSMatthew G. Knepley     PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
13366858538eSMatthew G. Knepley     PetscCall(DMPlexVecRestoreClosure(cdm, cs, coordinates, cell, Nc, (PetscScalar **)coords));
13376858538eSMatthew G. Knepley   }
13386858538eSMatthew G. Knepley   PetscFunctionReturn(0);
13396858538eSMatthew G. Knepley }
13406858538eSMatthew G. Knepley 
1341d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1342d71ae5a4SJacob Faibussowitsch {
13436858538eSMatthew G. Knepley   const PetscScalar *array;
1344a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
13456858538eSMatthew G. Knepley   PetscInt           numCoords, d;
13466858538eSMatthew G. Knepley   PetscBool          isDG;
134717fe8556SMatthew G. Knepley 
134817fe8556SMatthew G. Knepley   PetscFunctionBegin;
13496858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
135008401ef6SPierre Jolivet   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
13517f07f362SMatthew G. Knepley   *detJ = 0.0;
135228dbe442SToby Isaac   if (numCoords == 6) {
135328dbe442SToby Isaac     const PetscInt dim = 3;
135428dbe442SToby Isaac     PetscReal      R[9], J0;
135528dbe442SToby Isaac 
13569371c9d4SSatish Balay     if (v0) {
13579371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13589371c9d4SSatish Balay     }
13599566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto1D(coords, R));
136028dbe442SToby Isaac     if (J) {
136128dbe442SToby Isaac       J0   = 0.5 * PetscRealPart(coords[1]);
13629371c9d4SSatish Balay       J[0] = R[0] * J0;
13639371c9d4SSatish Balay       J[1] = R[1];
13649371c9d4SSatish Balay       J[2] = R[2];
13659371c9d4SSatish Balay       J[3] = R[3] * J0;
13669371c9d4SSatish Balay       J[4] = R[4];
13679371c9d4SSatish Balay       J[5] = R[5];
13689371c9d4SSatish Balay       J[6] = R[6] * J0;
13699371c9d4SSatish Balay       J[7] = R[7];
13709371c9d4SSatish Balay       J[8] = R[8];
137128dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
1372ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
1373adac9986SMatthew G. Knepley     }
137428dbe442SToby Isaac   } else if (numCoords == 4) {
13757f07f362SMatthew G. Knepley     const PetscInt dim = 2;
13767f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
13777f07f362SMatthew G. Knepley 
13789371c9d4SSatish Balay     if (v0) {
13799371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13809371c9d4SSatish Balay     }
13819566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection2Dto1D(coords, R));
138217fe8556SMatthew G. Knepley     if (J) {
13837f07f362SMatthew G. Knepley       J0   = 0.5 * PetscRealPart(coords[1]);
13849371c9d4SSatish Balay       J[0] = R[0] * J0;
13859371c9d4SSatish Balay       J[1] = R[1];
13869371c9d4SSatish Balay       J[2] = R[2] * J0;
13879371c9d4SSatish Balay       J[3] = R[3];
1388923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1389ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
1390adac9986SMatthew G. Knepley     }
13917f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
13927f07f362SMatthew G. Knepley     const PetscInt dim = 1;
13937f07f362SMatthew G. Knepley 
13949371c9d4SSatish Balay     if (v0) {
13959371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13969371c9d4SSatish Balay     }
13977f07f362SMatthew G. Knepley     if (J) {
13987f07f362SMatthew G. Knepley       J[0]  = 0.5 * (PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
139917fe8556SMatthew G. Knepley       *detJ = J[0];
14009566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(2.0));
14019371c9d4SSatish Balay       if (invJ) {
14029371c9d4SSatish Balay         invJ[0] = 1.0 / J[0];
14039371c9d4SSatish Balay         PetscCall(PetscLogFlops(1.0));
14049371c9d4SSatish Balay       }
1405adac9986SMatthew G. Knepley     }
14066858538eSMatthew 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);
14076858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
140817fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
140917fe8556SMatthew G. Knepley }
141017fe8556SMatthew G. Knepley 
1411d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1412d71ae5a4SJacob Faibussowitsch {
14136858538eSMatthew G. Knepley   const PetscScalar *array;
1414a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
14156858538eSMatthew G. Knepley   PetscInt           numCoords, d;
14166858538eSMatthew G. Knepley   PetscBool          isDG;
1417ccd2543fSMatthew G Knepley 
1418ccd2543fSMatthew G Knepley   PetscFunctionBegin;
14196858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14206858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
14217f07f362SMatthew G. Knepley   *detJ = 0.0;
1422ccd2543fSMatthew G Knepley   if (numCoords == 9) {
14237f07f362SMatthew G. Knepley     const PetscInt dim = 3;
14247f07f362SMatthew 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};
14257f07f362SMatthew G. Knepley 
14269371c9d4SSatish Balay     if (v0) {
14279371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
14289371c9d4SSatish Balay     }
14299566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
14307f07f362SMatthew G. Knepley     if (J) {
1431b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1432b7ad821dSMatthew G. Knepley 
1433b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1434ad540459SPierre 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]));
14357f07f362SMatthew G. Knepley       }
14369566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1437923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
14387f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
14396858538eSMatthew G. Knepley         for (PetscInt f = 0; f < dim; f++) {
14407f07f362SMatthew G. Knepley           J[d * dim + f] = 0.0;
1441ad540459SPierre Jolivet           for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f];
14427f07f362SMatthew G. Knepley         }
14437f07f362SMatthew G. Knepley       }
14449566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
14457f07f362SMatthew G. Knepley     }
1446ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
14477f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
14487f07f362SMatthew G. Knepley     const PetscInt dim = 2;
14497f07f362SMatthew G. Knepley 
14509371c9d4SSatish Balay     if (v0) {
14519371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
14529371c9d4SSatish Balay     }
1453ccd2543fSMatthew G Knepley     if (J) {
1454ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1455ad540459SPierre 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]));
1456ccd2543fSMatthew G Knepley       }
14579566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1458923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1459ccd2543fSMatthew G Knepley     }
1460ad540459SPierre Jolivet     if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
146163a3b9bcSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %" PetscInt_FMT " != 6 or 9", numCoords);
14626858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1463ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1464ccd2543fSMatthew G Knepley }
1465ccd2543fSMatthew G Knepley 
1466d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1467d71ae5a4SJacob Faibussowitsch {
14686858538eSMatthew G. Knepley   const PetscScalar *array;
1469a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
14706858538eSMatthew G. Knepley   PetscInt           numCoords, d;
14716858538eSMatthew G. Knepley   PetscBool          isDG;
1472ccd2543fSMatthew G Knepley 
1473ccd2543fSMatthew G Knepley   PetscFunctionBegin;
14746858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14756858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1476dfccc68fSToby Isaac   if (!Nq) {
1477412e9a14SMatthew G. Knepley     PetscInt vorder[4] = {0, 1, 2, 3};
1478412e9a14SMatthew G. Knepley 
14799371c9d4SSatish Balay     if (isTensor) {
14809371c9d4SSatish Balay       vorder[2] = 3;
14819371c9d4SSatish Balay       vorder[3] = 2;
14829371c9d4SSatish Balay     }
14837f07f362SMatthew G. Knepley     *detJ = 0.0;
148499dec3a6SMatthew G. Knepley     if (numCoords == 12) {
148599dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
148699dec3a6SMatthew 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};
148799dec3a6SMatthew G. Knepley 
14889371c9d4SSatish Balay       if (v) {
14899371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
14909371c9d4SSatish Balay       }
14919566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
149299dec3a6SMatthew G. Knepley       if (J) {
149399dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
149499dec3a6SMatthew G. Knepley 
149599dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
1496412e9a14SMatthew G. Knepley           J0[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * pdim + d]) - PetscRealPart(coords[vorder[0] * pdim + d]));
1497412e9a14SMatthew G. Knepley           J0[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[2] * pdim + d]) - PetscRealPart(coords[vorder[1] * pdim + d]));
149899dec3a6SMatthew G. Knepley         }
14999566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1500923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
150199dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
15026858538eSMatthew G. Knepley           for (PetscInt f = 0; f < dim; f++) {
150399dec3a6SMatthew G. Knepley             J[d * dim + f] = 0.0;
1504ad540459SPierre Jolivet             for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f];
150599dec3a6SMatthew G. Knepley           }
150699dec3a6SMatthew G. Knepley         }
15079566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(18.0));
150899dec3a6SMatthew G. Knepley       }
1509ad540459SPierre Jolivet       if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
151071f58de1SToby Isaac     } else if (numCoords == 8) {
151199dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
151299dec3a6SMatthew G. Knepley 
15139371c9d4SSatish Balay       if (v) {
15149371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
15159371c9d4SSatish Balay       }
1516ccd2543fSMatthew G Knepley       if (J) {
1517ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
1518412e9a14SMatthew G. Knepley           J[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1519412e9a14SMatthew G. Knepley           J[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[3] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1520ccd2543fSMatthew G Knepley         }
15219566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1522923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1523ccd2543fSMatthew G Knepley       }
1524ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
152563a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1526dfccc68fSToby Isaac   } else {
1527dfccc68fSToby Isaac     const PetscInt Nv         = 4;
1528dfccc68fSToby Isaac     const PetscInt dimR       = 2;
1529412e9a14SMatthew G. Knepley     PetscInt       zToPlex[4] = {0, 1, 3, 2};
1530dfccc68fSToby Isaac     PetscReal      zOrder[12];
1531dfccc68fSToby Isaac     PetscReal      zCoeff[12];
1532dfccc68fSToby Isaac     PetscInt       i, j, k, l, dim;
1533dfccc68fSToby Isaac 
15349371c9d4SSatish Balay     if (isTensor) {
15359371c9d4SSatish Balay       zToPlex[2] = 2;
15369371c9d4SSatish Balay       zToPlex[3] = 3;
15379371c9d4SSatish Balay     }
1538dfccc68fSToby Isaac     if (numCoords == 12) {
1539dfccc68fSToby Isaac       dim = 3;
1540dfccc68fSToby Isaac     } else if (numCoords == 8) {
1541dfccc68fSToby Isaac       dim = 2;
154263a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1543dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1544dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1545dfccc68fSToby Isaac 
1546ad540459SPierre Jolivet       for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1547dfccc68fSToby Isaac     }
1548dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
15492df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta):
15502df84da0SMatthew G. Knepley            \phi^0 = (1 - xi - eta + xi eta) --> 1      = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3)
15512df84da0SMatthew G. Knepley            \phi^1 = (1 + xi - eta - xi eta) --> xi     = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3)
15522df84da0SMatthew G. Knepley            \phi^2 = (1 - xi + eta - xi eta) --> eta    = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3)
15532df84da0SMatthew G. Knepley            \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3)
15542df84da0SMatthew G. Knepley       */
1555dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1556dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1557dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1558dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1559dfccc68fSToby Isaac     }
1560dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1561dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1562dfccc68fSToby Isaac 
1563dfccc68fSToby Isaac       if (v) {
1564dfccc68fSToby Isaac         PetscReal extPoint[4];
1565dfccc68fSToby Isaac 
1566dfccc68fSToby Isaac         extPoint[0] = 1.;
1567dfccc68fSToby Isaac         extPoint[1] = xi;
1568dfccc68fSToby Isaac         extPoint[2] = eta;
1569dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1570dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1571dfccc68fSToby Isaac           PetscReal val = 0.;
1572dfccc68fSToby Isaac 
1573ad540459SPierre Jolivet           for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j];
1574dfccc68fSToby Isaac           v[i * dim + j] = val;
1575dfccc68fSToby Isaac         }
1576dfccc68fSToby Isaac       }
1577dfccc68fSToby Isaac       if (J) {
1578dfccc68fSToby Isaac         PetscReal extJ[8];
1579dfccc68fSToby Isaac 
1580dfccc68fSToby Isaac         extJ[0] = 0.;
1581dfccc68fSToby Isaac         extJ[1] = 0.;
1582dfccc68fSToby Isaac         extJ[2] = 1.;
1583dfccc68fSToby Isaac         extJ[3] = 0.;
1584dfccc68fSToby Isaac         extJ[4] = 0.;
1585dfccc68fSToby Isaac         extJ[5] = 1.;
1586dfccc68fSToby Isaac         extJ[6] = eta;
1587dfccc68fSToby Isaac         extJ[7] = xi;
1588dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1589dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1590dfccc68fSToby Isaac             PetscReal val = 0.;
1591dfccc68fSToby Isaac 
1592ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1593dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1594dfccc68fSToby Isaac           }
1595dfccc68fSToby Isaac         }
1596dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1597dfccc68fSToby Isaac           PetscReal  x, y, z;
1598dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1599dfccc68fSToby Isaac           PetscReal  norm;
1600dfccc68fSToby Isaac 
1601dfccc68fSToby Isaac           x     = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1602dfccc68fSToby Isaac           y     = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1603dfccc68fSToby Isaac           z     = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1604dfccc68fSToby Isaac           norm  = PetscSqrtReal(x * x + y * y + z * z);
1605dfccc68fSToby Isaac           iJ[2] = x / norm;
1606dfccc68fSToby Isaac           iJ[5] = y / norm;
1607dfccc68fSToby Isaac           iJ[8] = z / norm;
1608dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1609ad540459SPierre Jolivet           if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1610dfccc68fSToby Isaac         } else {
1611dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1612ad540459SPierre Jolivet           if (invJ) DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1613dfccc68fSToby Isaac         }
1614dfccc68fSToby Isaac       }
1615dfccc68fSToby Isaac     }
1616dfccc68fSToby Isaac   }
16176858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1618ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1619ccd2543fSMatthew G Knepley }
1620ccd2543fSMatthew G Knepley 
1621d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1622d71ae5a4SJacob Faibussowitsch {
16236858538eSMatthew G. Knepley   const PetscScalar *array;
1624a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1625ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
16266858538eSMatthew G. Knepley   PetscInt           numCoords, d;
16276858538eSMatthew G. Knepley   PetscBool          isDG;
1628ccd2543fSMatthew G Knepley 
1629ccd2543fSMatthew G Knepley   PetscFunctionBegin;
16306858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16316858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
16327f07f362SMatthew G. Knepley   *detJ = 0.0;
16339371c9d4SSatish Balay   if (v0) {
16349371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
16359371c9d4SSatish Balay   }
1636ccd2543fSMatthew G Knepley   if (J) {
1637ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1638f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1639f0df753eSMatthew G. Knepley       J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1640f0df753eSMatthew G. Knepley       J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1641f0df753eSMatthew G. Knepley       J[d * dim + 2] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1642ccd2543fSMatthew G Knepley     }
16439566063dSJacob Faibussowitsch     PetscCall(PetscLogFlops(18.0));
1644923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1645ccd2543fSMatthew G Knepley   }
1646ad540459SPierre Jolivet   if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
16476858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1648ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1649ccd2543fSMatthew G Knepley }
1650ccd2543fSMatthew G Knepley 
1651d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1652d71ae5a4SJacob Faibussowitsch {
16536858538eSMatthew G. Knepley   const PetscScalar *array;
1654a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1655ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
16566858538eSMatthew G. Knepley   PetscInt           numCoords, d;
16576858538eSMatthew G. Knepley   PetscBool          isDG;
1658ccd2543fSMatthew G Knepley 
1659ccd2543fSMatthew G Knepley   PetscFunctionBegin;
16606858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16616858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1662dfccc68fSToby Isaac   if (!Nq) {
16637f07f362SMatthew G. Knepley     *detJ = 0.0;
16649371c9d4SSatish Balay     if (v) {
16659371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
16669371c9d4SSatish Balay     }
1667ccd2543fSMatthew G Knepley     if (J) {
1668ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1669f0df753eSMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1670f0df753eSMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1671f0df753eSMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1672ccd2543fSMatthew G Knepley       }
16739566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
1674923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1675ccd2543fSMatthew G Knepley     }
1676ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
1677dfccc68fSToby Isaac   } else {
1678dfccc68fSToby Isaac     const PetscInt Nv         = 8;
1679dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1680dfccc68fSToby Isaac     const PetscInt dim        = 3;
1681dfccc68fSToby Isaac     const PetscInt dimR       = 3;
1682dfccc68fSToby Isaac     PetscReal      zOrder[24];
1683dfccc68fSToby Isaac     PetscReal      zCoeff[24];
1684dfccc68fSToby Isaac     PetscInt       i, j, k, l;
1685dfccc68fSToby Isaac 
1686dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1687dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1688dfccc68fSToby Isaac 
1689ad540459SPierre Jolivet       for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1690dfccc68fSToby Isaac     }
1691dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1692dfccc68fSToby 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]);
1693dfccc68fSToby 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]);
1694dfccc68fSToby 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]);
1695dfccc68fSToby 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]);
1696dfccc68fSToby 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]);
1697dfccc68fSToby 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]);
1698dfccc68fSToby 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]);
1699dfccc68fSToby 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]);
1700dfccc68fSToby Isaac     }
1701dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1702dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1703dfccc68fSToby Isaac 
1704dfccc68fSToby Isaac       if (v) {
170591d2b7ceSToby Isaac         PetscReal extPoint[8];
1706dfccc68fSToby Isaac 
1707dfccc68fSToby Isaac         extPoint[0] = 1.;
1708dfccc68fSToby Isaac         extPoint[1] = xi;
1709dfccc68fSToby Isaac         extPoint[2] = eta;
1710dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1711dfccc68fSToby Isaac         extPoint[4] = theta;
1712dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1713dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1714dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1715dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1716dfccc68fSToby Isaac           PetscReal val = 0.;
1717dfccc68fSToby Isaac 
1718ad540459SPierre Jolivet           for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j];
1719dfccc68fSToby Isaac           v[i * dim + j] = val;
1720dfccc68fSToby Isaac         }
1721dfccc68fSToby Isaac       }
1722dfccc68fSToby Isaac       if (J) {
1723dfccc68fSToby Isaac         PetscReal extJ[24];
1724dfccc68fSToby Isaac 
17259371c9d4SSatish Balay         extJ[0]  = 0.;
17269371c9d4SSatish Balay         extJ[1]  = 0.;
17279371c9d4SSatish Balay         extJ[2]  = 0.;
17289371c9d4SSatish Balay         extJ[3]  = 1.;
17299371c9d4SSatish Balay         extJ[4]  = 0.;
17309371c9d4SSatish Balay         extJ[5]  = 0.;
17319371c9d4SSatish Balay         extJ[6]  = 0.;
17329371c9d4SSatish Balay         extJ[7]  = 1.;
17339371c9d4SSatish Balay         extJ[8]  = 0.;
17349371c9d4SSatish Balay         extJ[9]  = eta;
17359371c9d4SSatish Balay         extJ[10] = xi;
17369371c9d4SSatish Balay         extJ[11] = 0.;
17379371c9d4SSatish Balay         extJ[12] = 0.;
17389371c9d4SSatish Balay         extJ[13] = 0.;
17399371c9d4SSatish Balay         extJ[14] = 1.;
17409371c9d4SSatish Balay         extJ[15] = theta;
17419371c9d4SSatish Balay         extJ[16] = 0.;
17429371c9d4SSatish Balay         extJ[17] = xi;
17439371c9d4SSatish Balay         extJ[18] = 0.;
17449371c9d4SSatish Balay         extJ[19] = theta;
17459371c9d4SSatish Balay         extJ[20] = eta;
17469371c9d4SSatish Balay         extJ[21] = theta * eta;
17479371c9d4SSatish Balay         extJ[22] = theta * xi;
17489371c9d4SSatish Balay         extJ[23] = eta * xi;
1749dfccc68fSToby Isaac 
1750dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1751dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1752dfccc68fSToby Isaac             PetscReal val = 0.;
1753dfccc68fSToby Isaac 
1754ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1755dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1756dfccc68fSToby Isaac           }
1757dfccc68fSToby Isaac         }
1758dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1759ad540459SPierre Jolivet         if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1760dfccc68fSToby Isaac       }
1761dfccc68fSToby Isaac     }
1762dfccc68fSToby Isaac   }
17636858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1764ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1765ccd2543fSMatthew G Knepley }
1766ccd2543fSMatthew G Knepley 
1767d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1768d71ae5a4SJacob Faibussowitsch {
17696858538eSMatthew G. Knepley   const PetscScalar *array;
17702df84da0SMatthew G. Knepley   PetscScalar       *coords = NULL;
17712df84da0SMatthew G. Knepley   const PetscInt     dim    = 3;
17726858538eSMatthew G. Knepley   PetscInt           numCoords, d;
17736858538eSMatthew G. Knepley   PetscBool          isDG;
17742df84da0SMatthew G. Knepley 
17752df84da0SMatthew G. Knepley   PetscFunctionBegin;
17766858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
17776858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
17782df84da0SMatthew G. Knepley   if (!Nq) {
17792df84da0SMatthew G. Knepley     /* Assume that the map to the reference is affine */
17802df84da0SMatthew G. Knepley     *detJ = 0.0;
17819371c9d4SSatish Balay     if (v) {
17829371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
17839371c9d4SSatish Balay     }
17842df84da0SMatthew G. Knepley     if (J) {
17852df84da0SMatthew G. Knepley       for (d = 0; d < dim; d++) {
17862df84da0SMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17872df84da0SMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17882df84da0SMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17892df84da0SMatthew G. Knepley       }
17909566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
17912df84da0SMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
17922df84da0SMatthew G. Knepley     }
1793ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
17942df84da0SMatthew G. Knepley   } else {
17952df84da0SMatthew G. Knepley     const PetscInt dim  = 3;
17962df84da0SMatthew G. Knepley     const PetscInt dimR = 3;
17972df84da0SMatthew G. Knepley     const PetscInt Nv   = 6;
17982df84da0SMatthew G. Knepley     PetscReal      verts[18];
17992df84da0SMatthew G. Knepley     PetscReal      coeff[18];
18002df84da0SMatthew G. Knepley     PetscInt       i, j, k, l;
18012df84da0SMatthew G. Knepley 
18029371c9d4SSatish Balay     for (i = 0; i < Nv; ++i)
18039371c9d4SSatish Balay       for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]);
18042df84da0SMatthew G. Knepley     for (j = 0; j < dim; ++j) {
18052df84da0SMatthew G. Knepley       /* Check for triangle,
18062df84da0SMatthew 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)
18072df84da0SMatthew 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)
18082df84da0SMatthew G. Knepley            phi^2 =  1/2 (1 + eta)   chi^2 = delta(-1,  1)
18092df84da0SMatthew G. Knepley 
18102df84da0SMatthew G. Knepley            phi^0 + phi^1 + phi^2 = 1    coef_1   = 1/2 (         chi^1 + chi^2)
18112df84da0SMatthew G. Knepley           -phi^0 + phi^1 - phi^2 = xi   coef_xi  = 1/2 (-chi^0 + chi^1)
18122df84da0SMatthew G. Knepley           -phi^0 - phi^1 + phi^2 = eta  coef_eta = 1/2 (-chi^0         + chi^2)
18132df84da0SMatthew G. Knepley 
18142df84da0SMatthew G. Knepley           < chi_0 chi_1 chi_2> A /  1  1  1 \ / phi_0 \   <chi> I <phi>^T  so we need the inverse transpose
18152df84da0SMatthew G. Knepley                                  | -1  1 -1 | | phi_1 | =
18162df84da0SMatthew G. Knepley                                  \ -1 -1  1 / \ phi_2 /
18172df84da0SMatthew G. Knepley 
18182df84da0SMatthew 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
18192df84da0SMatthew G. Knepley       */
18202df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta):
18212df84da0SMatthew G. Knepley            \phi^0 = 1/4 (   -xi - eta        + xi zeta + eta zeta) --> /  1  1  1  1  1  1 \ 1
18222df84da0SMatthew G. Knepley            \phi^1 = 1/4 (1      + eta - zeta           - eta zeta) --> | -1  1 -1 -1 -1  1 | eta
18232df84da0SMatthew G. Knepley            \phi^2 = 1/4 (1 + xi       - zeta - xi zeta)            --> | -1 -1  1 -1  1 -1 | xi
18242df84da0SMatthew G. Knepley            \phi^3 = 1/4 (   -xi - eta        - xi zeta - eta zeta) --> | -1 -1 -1  1  1  1 | zeta
18252df84da0SMatthew G. Knepley            \phi^4 = 1/4 (1 + xi       + zeta + xi zeta)            --> |  1  1 -1 -1  1 -1 | xi zeta
18262df84da0SMatthew G. Knepley            \phi^5 = 1/4 (1      + eta + zeta           + eta zeta) --> \  1 -1  1 -1 -1  1 / eta zeta
18272df84da0SMatthew G. Knepley            1/4 /  0  1  1  0  1  1 \
18282df84da0SMatthew G. Knepley                | -1  1  0 -1  0  1 |
18292df84da0SMatthew G. Knepley                | -1  0  1 -1  1  0 |
18302df84da0SMatthew G. Knepley                |  0 -1 -1  0  1  1 |
18312df84da0SMatthew G. Knepley                |  1  0 -1 -1  1  0 |
18322df84da0SMatthew G. Knepley                \  1 -1  0 -1  0  1 /
18332df84da0SMatthew G. Knepley       */
18342df84da0SMatthew G. Knepley       coeff[dim * 0 + j] = (1. / 4.) * (verts[dim * 1 + j] + verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
18352df84da0SMatthew G. Knepley       coeff[dim * 1 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
18362df84da0SMatthew G. Knepley       coeff[dim * 2 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
18372df84da0SMatthew G. Knepley       coeff[dim * 3 + j] = (1. / 4.) * (-verts[dim * 1 + j] - verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
18382df84da0SMatthew G. Knepley       coeff[dim * 4 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
18392df84da0SMatthew G. Knepley       coeff[dim * 5 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
18402df84da0SMatthew G. Knepley       /* For reference prism:
18412df84da0SMatthew G. Knepley       {0, 0, 0}
18422df84da0SMatthew G. Knepley       {0, 1, 0}
18432df84da0SMatthew G. Knepley       {1, 0, 0}
18442df84da0SMatthew G. Knepley       {0, 0, 1}
18452df84da0SMatthew G. Knepley       {0, 0, 0}
18462df84da0SMatthew G. Knepley       {0, 0, 0}
18472df84da0SMatthew G. Knepley       */
18482df84da0SMatthew G. Knepley     }
18492df84da0SMatthew G. Knepley     for (i = 0; i < Nq; ++i) {
18502df84da0SMatthew G. Knepley       const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2];
18512df84da0SMatthew G. Knepley 
18522df84da0SMatthew G. Knepley       if (v) {
18532df84da0SMatthew G. Knepley         PetscReal extPoint[6];
18542df84da0SMatthew G. Knepley         PetscInt  c;
18552df84da0SMatthew G. Knepley 
18562df84da0SMatthew G. Knepley         extPoint[0] = 1.;
18572df84da0SMatthew G. Knepley         extPoint[1] = eta;
18582df84da0SMatthew G. Knepley         extPoint[2] = xi;
18592df84da0SMatthew G. Knepley         extPoint[3] = zeta;
18602df84da0SMatthew G. Knepley         extPoint[4] = xi * zeta;
18612df84da0SMatthew G. Knepley         extPoint[5] = eta * zeta;
18622df84da0SMatthew G. Knepley         for (c = 0; c < dim; ++c) {
18632df84da0SMatthew G. Knepley           PetscReal val = 0.;
18642df84da0SMatthew G. Knepley 
1865ad540459SPierre Jolivet           for (k = 0; k < Nv; ++k) val += extPoint[k] * coeff[k * dim + c];
18662df84da0SMatthew G. Knepley           v[i * dim + c] = val;
18672df84da0SMatthew G. Knepley         }
18682df84da0SMatthew G. Knepley       }
18692df84da0SMatthew G. Knepley       if (J) {
18702df84da0SMatthew G. Knepley         PetscReal extJ[18];
18712df84da0SMatthew G. Knepley 
18729371c9d4SSatish Balay         extJ[0]  = 0.;
18739371c9d4SSatish Balay         extJ[1]  = 0.;
18749371c9d4SSatish Balay         extJ[2]  = 0.;
18759371c9d4SSatish Balay         extJ[3]  = 0.;
18769371c9d4SSatish Balay         extJ[4]  = 1.;
18779371c9d4SSatish Balay         extJ[5]  = 0.;
18789371c9d4SSatish Balay         extJ[6]  = 1.;
18799371c9d4SSatish Balay         extJ[7]  = 0.;
18809371c9d4SSatish Balay         extJ[8]  = 0.;
18819371c9d4SSatish Balay         extJ[9]  = 0.;
18829371c9d4SSatish Balay         extJ[10] = 0.;
18839371c9d4SSatish Balay         extJ[11] = 1.;
18849371c9d4SSatish Balay         extJ[12] = zeta;
18859371c9d4SSatish Balay         extJ[13] = 0.;
18869371c9d4SSatish Balay         extJ[14] = xi;
18879371c9d4SSatish Balay         extJ[15] = 0.;
18889371c9d4SSatish Balay         extJ[16] = zeta;
18899371c9d4SSatish Balay         extJ[17] = eta;
18902df84da0SMatthew G. Knepley 
18912df84da0SMatthew G. Knepley         for (j = 0; j < dim; j++) {
18922df84da0SMatthew G. Knepley           for (k = 0; k < dimR; k++) {
18932df84da0SMatthew G. Knepley             PetscReal val = 0.;
18942df84da0SMatthew G. Knepley 
1895ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += coeff[dim * l + j] * extJ[dimR * l + k];
18962df84da0SMatthew G. Knepley             J[i * dim * dim + dim * j + k] = val;
18972df84da0SMatthew G. Knepley           }
18982df84da0SMatthew G. Knepley         }
18992df84da0SMatthew G. Knepley         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1900ad540459SPierre Jolivet         if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
19012df84da0SMatthew G. Knepley       }
19022df84da0SMatthew G. Knepley     }
19032df84da0SMatthew G. Knepley   }
19046858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
19052df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
19062df84da0SMatthew G. Knepley }
19072df84da0SMatthew G. Knepley 
1908d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1909d71ae5a4SJacob Faibussowitsch {
1910ba2698f1SMatthew G. Knepley   DMPolytopeType   ct;
1911dfccc68fSToby Isaac   PetscInt         depth, dim, coordDim, coneSize, i;
1912dfccc68fSToby Isaac   PetscInt         Nq     = 0;
1913dfccc68fSToby Isaac   const PetscReal *points = NULL;
1914dfccc68fSToby Isaac   DMLabel          depthLabel;
1915c330f8ffSToby Isaac   PetscReal        xi0[3]   = {-1., -1., -1.}, v0[3], J0[9], detJ0;
1916dfccc68fSToby Isaac   PetscBool        isAffine = PETSC_TRUE;
1917dfccc68fSToby Isaac 
1918dfccc68fSToby Isaac   PetscFunctionBegin;
19199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
19209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
19229566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(depthLabel, cell, &dim));
192348a46eb9SPierre Jolivet   if (depth == 1 && dim == 1) PetscCall(DMGetDimension(dm, &dim));
19249566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &coordDim));
192563a3b9bcSJacob Faibussowitsch   PetscCheck(coordDim <= 3, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %" PetscInt_FMT " > 3", coordDim);
19269566063dSJacob Faibussowitsch   if (quad) PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL));
19279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
1928ba2698f1SMatthew G. Knepley   switch (ct) {
1929ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_POINT:
19309566063dSJacob Faibussowitsch     PetscCall(DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ));
1931dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1932dfccc68fSToby Isaac     break;
1933ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
1934412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
19359566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19369566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ));
1937dfccc68fSToby Isaac     break;
1938ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
19399566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19409566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ));
1941dfccc68fSToby Isaac     break;
1942ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
19439566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ));
1944412e9a14SMatthew G. Knepley     isAffine = PETSC_FALSE;
1945412e9a14SMatthew G. Knepley     break;
1946412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
19479566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ));
1948dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1949dfccc68fSToby Isaac     break;
1950ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
19519566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19529566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ));
1953dfccc68fSToby Isaac     break;
1954ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
19559566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
1956dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1957dfccc68fSToby Isaac     break;
19582df84da0SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM:
19599566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
19602df84da0SMatthew G. Knepley     isAffine = PETSC_FALSE;
19612df84da0SMatthew G. Knepley     break;
1962d71ae5a4SJacob Faibussowitsch   default:
1963d71ae5a4SJacob 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))]);
1964dfccc68fSToby Isaac   }
19657318780aSToby Isaac   if (isAffine && Nq) {
1966dfccc68fSToby Isaac     if (v) {
1967ad540459SPierre Jolivet       for (i = 0; i < Nq; i++) CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]);
1968dfccc68fSToby Isaac     }
19697318780aSToby Isaac     if (detJ) {
1970ad540459SPierre Jolivet       for (i = 0; i < Nq; i++) detJ[i] = detJ0;
19717318780aSToby Isaac     }
19727318780aSToby Isaac     if (J) {
19737318780aSToby Isaac       PetscInt k;
19747318780aSToby Isaac 
19757318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1976dfccc68fSToby Isaac         PetscInt j;
1977dfccc68fSToby Isaac 
1978ad540459SPierre Jolivet         for (j = 0; j < coordDim * coordDim; j++, k++) J[k] = J0[j];
19797318780aSToby Isaac       }
19807318780aSToby Isaac     }
19817318780aSToby Isaac     if (invJ) {
19827318780aSToby Isaac       PetscInt k;
19837318780aSToby Isaac       switch (coordDim) {
1984d71ae5a4SJacob Faibussowitsch       case 0:
1985d71ae5a4SJacob Faibussowitsch         break;
1986d71ae5a4SJacob Faibussowitsch       case 1:
1987d71ae5a4SJacob Faibussowitsch         invJ[0] = 1. / J0[0];
1988d71ae5a4SJacob Faibussowitsch         break;
1989d71ae5a4SJacob Faibussowitsch       case 2:
1990d71ae5a4SJacob Faibussowitsch         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
1991d71ae5a4SJacob Faibussowitsch         break;
1992d71ae5a4SJacob Faibussowitsch       case 3:
1993d71ae5a4SJacob Faibussowitsch         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
1994d71ae5a4SJacob Faibussowitsch         break;
19957318780aSToby Isaac       }
19967318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
19977318780aSToby Isaac         PetscInt j;
19987318780aSToby Isaac 
1999ad540459SPierre Jolivet         for (j = 0; j < coordDim * coordDim; j++, k++) invJ[k] = invJ[j];
2000dfccc68fSToby Isaac       }
2001dfccc68fSToby Isaac     }
2002dfccc68fSToby Isaac   }
2003dfccc68fSToby Isaac   PetscFunctionReturn(0);
2004dfccc68fSToby Isaac }
2005dfccc68fSToby Isaac 
2006ccd2543fSMatthew G Knepley /*@C
20078e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
2008ccd2543fSMatthew G Knepley 
2009d083f849SBarry Smith   Collective on dm
2010ccd2543fSMatthew G Knepley 
20114165533cSJose E. Roman   Input Parameters:
2012ccd2543fSMatthew G Knepley + dm   - the DM
2013ccd2543fSMatthew G Knepley - cell - the cell
2014ccd2543fSMatthew G Knepley 
20154165533cSJose E. Roman   Output Parameters:
20169b172b3aSMatthew Knepley + v0   - the translation part of this affine transform, meaning the translation to the origin (not the first vertex of the reference cell)
2017ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
2018ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
2019ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
2020ccd2543fSMatthew G Knepley 
2021ccd2543fSMatthew G Knepley   Level: advanced
2022ccd2543fSMatthew G Knepley 
2023ccd2543fSMatthew G Knepley   Fortran Notes:
2024ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2025ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
2026ccd2543fSMatthew G Knepley 
2027db781477SPatrick Sanan .seealso: `DMPlexComputeCellGeometryFEM()`, `DMGetCoordinateSection()`, `DMGetCoordinates()`
2028ccd2543fSMatthew G Knepley @*/
2029d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
2030d71ae5a4SJacob Faibussowitsch {
2031ccd2543fSMatthew G Knepley   PetscFunctionBegin;
20329566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, NULL, v0, J, invJ, detJ));
20338e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
20348e0841e0SMatthew G. Knepley }
20358e0841e0SMatthew G. Knepley 
2036d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
2037d71ae5a4SJacob Faibussowitsch {
20386858538eSMatthew G. Knepley   const PetscScalar *array;
20398e0841e0SMatthew G. Knepley   PetscScalar       *coords = NULL;
20406858538eSMatthew G. Knepley   PetscInt           numCoords;
20416858538eSMatthew G. Knepley   PetscBool          isDG;
20426858538eSMatthew G. Knepley   PetscQuadrature    feQuad;
20438e0841e0SMatthew G. Knepley   const PetscReal   *quadPoints;
2044ef0bb6c7SMatthew G. Knepley   PetscTabulation    T;
20456858538eSMatthew G. Knepley   PetscInt           dim, cdim, pdim, qdim, Nq, q;
20468e0841e0SMatthew G. Knepley 
20478e0841e0SMatthew G. Knepley   PetscFunctionBegin;
20489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
20499566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
20506858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
2051dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
2052dfccc68fSToby Isaac     PetscDualSpace dsp;
2053dfccc68fSToby Isaac 
20549566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(fe, &dsp));
20559566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetFunctional(dsp, 0, &quad));
20569566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2057dfccc68fSToby Isaac     Nq = 1;
2058dfccc68fSToby Isaac   } else {
20599566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2060dfccc68fSToby Isaac   }
20619566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
20629566063dSJacob Faibussowitsch   PetscCall(PetscFEGetQuadrature(fe, &feQuad));
2063dfccc68fSToby Isaac   if (feQuad == quad) {
20649566063dSJacob Faibussowitsch     PetscCall(PetscFEGetCellTabulation(fe, J ? 1 : 0, &T));
206563a3b9bcSJacob 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);
2066dfccc68fSToby Isaac   } else {
20679566063dSJacob Faibussowitsch     PetscCall(PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T));
2068dfccc68fSToby Isaac   }
206963a3b9bcSJacob Faibussowitsch   PetscCheck(qdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %" PetscInt_FMT " != quadrature dimension %" PetscInt_FMT, dim, qdim);
2070ef0bb6c7SMatthew G. Knepley   {
2071ef0bb6c7SMatthew G. Knepley     const PetscReal *basis    = T->T[0];
2072ef0bb6c7SMatthew G. Knepley     const PetscReal *basisDer = T->T[1];
2073ef0bb6c7SMatthew G. Knepley     PetscReal        detJt;
2074ef0bb6c7SMatthew G. Knepley 
2075a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
207663a3b9bcSJacob Faibussowitsch     PetscCheck(Nq == T->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %" PetscInt_FMT " != %" PetscInt_FMT, Nq, T->Np);
207763a3b9bcSJacob Faibussowitsch     PetscCheck(pdim == T->Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %" PetscInt_FMT " != %" PetscInt_FMT, pdim, T->Nb);
207863a3b9bcSJacob Faibussowitsch     PetscCheck(dim == T->Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %" PetscInt_FMT " != %" PetscInt_FMT, dim, T->Nc);
207963a3b9bcSJacob Faibussowitsch     PetscCheck(cdim == T->cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %" PetscInt_FMT " != %" PetscInt_FMT, cdim, T->cdim);
2080a2a9e04cSMatthew G. Knepley #endif
2081dfccc68fSToby Isaac     if (v) {
20829566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(v, Nq * cdim));
2083f960e424SToby Isaac       for (q = 0; q < Nq; ++q) {
2084f960e424SToby Isaac         PetscInt i, k;
2085f960e424SToby Isaac 
2086301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2087301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
2088ad540459SPierre Jolivet           for (i = 0; i < cdim; ++i) v[q * cdim + i] += basis[(q * pdim + k) * cdim + i] * PetscRealPart(coords[vertex * cdim + i]);
2089301b184aSMatthew G. Knepley         }
20909566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * cdim));
2091f960e424SToby Isaac       }
2092f960e424SToby Isaac     }
20938e0841e0SMatthew G. Knepley     if (J) {
20949566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(J, Nq * cdim * cdim));
20958e0841e0SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
20968e0841e0SMatthew G. Knepley         PetscInt i, j, k, c, r;
20978e0841e0SMatthew G. Knepley 
20988e0841e0SMatthew G. Knepley         /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
2099301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2100301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
2101301b184aSMatthew G. Knepley           for (j = 0; j < dim; ++j) {
2102ad540459SPierre 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]);
2103301b184aSMatthew G. Knepley           }
2104301b184aSMatthew G. Knepley         }
21059566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * dim * cdim));
21068e0841e0SMatthew G. Knepley         if (cdim > dim) {
21078e0841e0SMatthew G. Knepley           for (c = dim; c < cdim; ++c)
21089371c9d4SSatish Balay             for (r = 0; r < cdim; ++r) J[r * cdim + c] = r == c ? 1.0 : 0.0;
21098e0841e0SMatthew G. Knepley         }
2110f960e424SToby Isaac         if (!detJ && !invJ) continue;
2111a63b72c6SToby Isaac         detJt = 0.;
21128e0841e0SMatthew G. Knepley         switch (cdim) {
21138e0841e0SMatthew G. Knepley         case 3:
2114037dc194SToby Isaac           DMPlex_Det3D_Internal(&detJt, &J[q * cdim * dim]);
2115ad540459SPierre Jolivet           if (invJ) DMPlex_Invert3D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt);
211617fe8556SMatthew G. Knepley           break;
211749dc4407SMatthew G. Knepley         case 2:
21189f328543SToby Isaac           DMPlex_Det2D_Internal(&detJt, &J[q * cdim * dim]);
2119ad540459SPierre Jolivet           if (invJ) DMPlex_Invert2D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt);
212049dc4407SMatthew G. Knepley           break;
21218e0841e0SMatthew G. Knepley         case 1:
2122037dc194SToby Isaac           detJt = J[q * cdim * dim];
2123037dc194SToby Isaac           if (invJ) invJ[q * cdim * dim] = 1.0 / detJt;
212449dc4407SMatthew G. Knepley         }
2125f960e424SToby Isaac         if (detJ) detJ[q] = detJt;
212649dc4407SMatthew G. Knepley       }
212708401ef6SPierre Jolivet     } else PetscCheck(!detJ && !invJ, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
212849dc4407SMatthew G. Knepley   }
21299566063dSJacob Faibussowitsch   if (feQuad != quad) PetscCall(PetscTabulationDestroy(&T));
21306858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
21318e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
21328e0841e0SMatthew G. Knepley }
21338e0841e0SMatthew G. Knepley 
21348e0841e0SMatthew G. Knepley /*@C
21358e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
21368e0841e0SMatthew G. Knepley 
2137d083f849SBarry Smith   Collective on dm
21388e0841e0SMatthew G. Knepley 
21394165533cSJose E. Roman   Input Parameters:
21408e0841e0SMatthew G. Knepley + dm   - the DM
21418e0841e0SMatthew G. Knepley . cell - the cell
2142dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
2143dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
21448e0841e0SMatthew G. Knepley 
21454165533cSJose E. Roman   Output Parameters:
2146dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
21478e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
21488e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
21498e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
21508e0841e0SMatthew G. Knepley 
21518e0841e0SMatthew G. Knepley   Level: advanced
21528e0841e0SMatthew G. Knepley 
21538e0841e0SMatthew G. Knepley   Fortran Notes:
21548e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
21558e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
21568e0841e0SMatthew G. Knepley 
2157db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()`
21588e0841e0SMatthew G. Knepley @*/
2159d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
2160d71ae5a4SJacob Faibussowitsch {
2161bb4a5db5SMatthew G. Knepley   DM      cdm;
2162dfccc68fSToby Isaac   PetscFE fe = NULL;
21638e0841e0SMatthew G. Knepley 
21648e0841e0SMatthew G. Knepley   PetscFunctionBegin;
2165dadcf809SJacob Faibussowitsch   PetscValidRealPointer(detJ, 7);
21669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
2167bb4a5db5SMatthew G. Knepley   if (cdm) {
2168dfccc68fSToby Isaac     PetscClassId id;
2169dfccc68fSToby Isaac     PetscInt     numFields;
2170e5e52638SMatthew G. Knepley     PetscDS      prob;
2171dfccc68fSToby Isaac     PetscObject  disc;
2172dfccc68fSToby Isaac 
21739566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(cdm, &numFields));
2174dfccc68fSToby Isaac     if (numFields) {
21759566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &prob));
21769566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(prob, 0, &disc));
21779566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
2178ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
2179dfccc68fSToby Isaac     }
2180dfccc68fSToby Isaac   }
21819566063dSJacob Faibussowitsch   if (!fe) PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ));
21829566063dSJacob Faibussowitsch   else PetscCall(DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ));
2183ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
2184ccd2543fSMatthew G Knepley }
2185834e62ceSMatthew G. Knepley 
2186d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2187d71ae5a4SJacob Faibussowitsch {
21889bf2564aSMatt McGurn   PetscSection       coordSection;
21899bf2564aSMatt McGurn   Vec                coordinates;
21909bf2564aSMatt McGurn   const PetscScalar *coords = NULL;
21919bf2564aSMatt McGurn   PetscInt           d, dof, off;
21929bf2564aSMatt McGurn 
21939bf2564aSMatt McGurn   PetscFunctionBegin;
21949566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
21959566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
21969566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
21979bf2564aSMatt McGurn 
21989bf2564aSMatt McGurn   /* for a point the centroid is just the coord */
21999bf2564aSMatt McGurn   if (centroid) {
22009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
22019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
2202ad540459SPierre Jolivet     for (d = 0; d < dof; d++) centroid[d] = PetscRealPart(coords[off + d]);
22039bf2564aSMatt McGurn   }
22049bf2564aSMatt McGurn   if (normal) {
22059bf2564aSMatt McGurn     const PetscInt *support, *cones;
22069bf2564aSMatt McGurn     PetscInt        supportSize;
22079bf2564aSMatt McGurn     PetscReal       norm, sign;
22089bf2564aSMatt McGurn 
22099bf2564aSMatt McGurn     /* compute the norm based upon the support centroids */
22109566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, cell, &supportSize));
22119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, cell, &support));
22129566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL));
22139bf2564aSMatt McGurn 
22149bf2564aSMatt McGurn     /* Take the normal from the centroid of the support to the vertex*/
22159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
22169566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
2217ad540459SPierre Jolivet     for (d = 0; d < dof; d++) normal[d] -= PetscRealPart(coords[off + d]);
22189bf2564aSMatt McGurn 
22199bf2564aSMatt McGurn     /* Determine the sign of the normal based upon its location in the support */
22209566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, support[0], &cones));
22219bf2564aSMatt McGurn     sign = cones[0] == cell ? 1.0 : -1.0;
22229bf2564aSMatt McGurn 
22239bf2564aSMatt McGurn     norm = DMPlex_NormD_Internal(dim, normal);
22249bf2564aSMatt McGurn     for (d = 0; d < dim; ++d) normal[d] /= (norm * sign);
22259bf2564aSMatt McGurn   }
2226ad540459SPierre Jolivet   if (vol) *vol = 1.0;
22279566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
22289bf2564aSMatt McGurn   PetscFunctionReturn(0);
22299bf2564aSMatt McGurn }
22309bf2564aSMatt McGurn 
2231d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2232d71ae5a4SJacob Faibussowitsch {
22336858538eSMatthew G. Knepley   const PetscScalar *array;
2234a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
2235714b99b6SMatthew G. Knepley   PetscInt           coordSize, d;
22366858538eSMatthew G. Knepley   PetscBool          isDG;
2237cc08537eSMatthew G. Knepley 
2238cc08537eSMatthew G. Knepley   PetscFunctionBegin;
22396858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2240cc08537eSMatthew G. Knepley   if (centroid) {
22416858538eSMatthew G. Knepley     for (d = 0; d < dim; ++d) centroid[d] = 0.5 * PetscRealPart(coords[d] + coords[dim + d]);
2242cc08537eSMatthew G. Knepley   }
2243cc08537eSMatthew G. Knepley   if (normal) {
2244a60a936bSMatthew G. Knepley     PetscReal norm;
2245a60a936bSMatthew G. Knepley 
224608401ef6SPierre Jolivet     PetscCheck(dim == 2, PETSC_COMM_SELF, PETSC_ERR_SUP, "We only support 2D edges right now");
22476858538eSMatthew G. Knepley     normal[0] = -PetscRealPart(coords[1] - coords[dim + 1]);
22486858538eSMatthew G. Knepley     normal[1] = PetscRealPart(coords[0] - coords[dim + 0]);
2249714b99b6SMatthew G. Knepley     norm      = DMPlex_NormD_Internal(dim, normal);
2250714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) normal[d] /= norm;
2251cc08537eSMatthew G. Knepley   }
2252cc08537eSMatthew G. Knepley   if (vol) {
2253714b99b6SMatthew G. Knepley     *vol = 0.0;
22546858538eSMatthew G. Knepley     for (d = 0; d < dim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - coords[dim + d]));
2255714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
2256cc08537eSMatthew G. Knepley   }
22576858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2258cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
2259cc08537eSMatthew G. Knepley }
2260cc08537eSMatthew G. Knepley 
2261cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */
2262d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2263d71ae5a4SJacob Faibussowitsch {
2264412e9a14SMatthew G. Knepley   DMPolytopeType     ct;
22656858538eSMatthew G. Knepley   const PetscScalar *array;
2266cc08537eSMatthew G. Knepley   PetscScalar       *coords = NULL;
22676858538eSMatthew G. Knepley   PetscInt           coordSize;
22686858538eSMatthew G. Knepley   PetscBool          isDG;
2269793a2a13SMatthew G. Knepley   PetscInt           fv[4] = {0, 1, 2, 3};
22706858538eSMatthew G. Knepley   PetscInt           cdim, numCorners, p, d;
2271cc08537eSMatthew G. Knepley 
2272cc08537eSMatthew G. Knepley   PetscFunctionBegin;
2273793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
22749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2275412e9a14SMatthew G. Knepley   switch (ct) {
22769371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR:
22779371c9d4SSatish Balay     fv[2] = 3;
22789371c9d4SSatish Balay     fv[3] = 2;
22799371c9d4SSatish Balay     break;
2280d71ae5a4SJacob Faibussowitsch   default:
2281d71ae5a4SJacob Faibussowitsch     break;
2282412e9a14SMatthew G. Knepley   }
22839566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
22846858538eSMatthew G. Knepley   PetscCall(DMPlexGetConeSize(dm, cell, &numCorners));
22856858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
22863f27a4e6SJed Brown   {
22873f27a4e6SJed Brown     PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm;
2288793a2a13SMatthew G. Knepley 
22893f27a4e6SJed Brown     for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]);
22904f99dae5SMatthew G. Knepley     for (p = 0; p < numCorners - 2; ++p) {
22913f27a4e6SJed Brown       PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.};
22923f27a4e6SJed Brown       for (d = 0; d < cdim; d++) {
22933f27a4e6SJed Brown         e0[d] = PetscRealPart(coords[cdim * fv[p + 1] + d]) - origin[d];
22943f27a4e6SJed Brown         e1[d] = PetscRealPart(coords[cdim * fv[p + 2] + d]) - origin[d];
22953f27a4e6SJed Brown       }
22963f27a4e6SJed Brown       const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1];
22973f27a4e6SJed Brown       const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2];
22983f27a4e6SJed Brown       const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0];
22993f27a4e6SJed Brown       const PetscReal a  = PetscSqrtReal(dx * dx + dy * dy + dz * dz);
23004f99dae5SMatthew G. Knepley 
23014f99dae5SMatthew G. Knepley       n[0] += dx;
23024f99dae5SMatthew G. Knepley       n[1] += dy;
23034f99dae5SMatthew G. Knepley       n[2] += dz;
2304ad540459SPierre 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.;
2305ceee4971SMatthew G. Knepley     }
23064f99dae5SMatthew G. Knepley     norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
23074f99dae5SMatthew G. Knepley     n[0] /= norm;
23084f99dae5SMatthew G. Knepley     n[1] /= norm;
23094f99dae5SMatthew G. Knepley     n[2] /= norm;
23104f99dae5SMatthew G. Knepley     c[0] /= norm;
23114f99dae5SMatthew G. Knepley     c[1] /= norm;
23124f99dae5SMatthew G. Knepley     c[2] /= norm;
23134f99dae5SMatthew G. Knepley     if (vol) *vol = 0.5 * norm;
23149371c9d4SSatish Balay     if (centroid)
23159371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) centroid[d] = c[d];
23169371c9d4SSatish Balay     if (normal)
23179371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) normal[d] = n[d];
23180a1d6728SMatthew G. Knepley   }
23196858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2320cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
2321cc08537eSMatthew G. Knepley }
2322cc08537eSMatthew G. Knepley 
23230ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */
2324d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2325d71ae5a4SJacob Faibussowitsch {
2326412e9a14SMatthew G. Knepley   DMPolytopeType        ct;
23276858538eSMatthew G. Knepley   const PetscScalar    *array;
23280ec8681fSMatthew G. Knepley   PetscScalar          *coords = NULL;
23296858538eSMatthew G. Knepley   PetscInt              coordSize;
23306858538eSMatthew G. Knepley   PetscBool             isDG;
23313f27a4e6SJed Brown   PetscReal             vsum      = 0.0, vtmp, coordsTmp[3 * 3], origin[3];
23326858538eSMatthew G. Knepley   const PetscInt        order[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
23336858538eSMatthew G. Knepley   const PetscInt       *cone, *faceSizes, *faces;
23346858538eSMatthew G. Knepley   const DMPolytopeType *faceTypes;
2335793a2a13SMatthew G. Knepley   PetscBool             isHybrid = PETSC_FALSE;
23366858538eSMatthew G. Knepley   PetscInt              numFaces, f, fOff = 0, p, d;
23370ec8681fSMatthew G. Knepley 
23380ec8681fSMatthew G. Knepley   PetscFunctionBegin;
233963a3b9bcSJacob Faibussowitsch   PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No support for dim %" PetscInt_FMT " > 3", dim);
2340793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
23419566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2342412e9a14SMatthew G. Knepley   switch (ct) {
2343412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
2344412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
2345412e9a14SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR:
2346d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
2347d71ae5a4SJacob Faibussowitsch     isHybrid = PETSC_TRUE;
2348d71ae5a4SJacob Faibussowitsch   default:
2349d71ae5a4SJacob Faibussowitsch     break;
2350412e9a14SMatthew G. Knepley   }
2351793a2a13SMatthew G. Knepley 
23529371c9d4SSatish Balay   if (centroid)
23539371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = 0.0;
23546858538eSMatthew G. Knepley   PetscCall(DMPlexGetCone(dm, cell, &cone));
23556858538eSMatthew G. Knepley 
23566858538eSMatthew G. Knepley   // Using the closure of faces for coordinates does not work in periodic geometries, so we index into the cell coordinates
23576858538eSMatthew G. Knepley   PetscCall(DMPlexGetRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
23586858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
23590ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
2360793a2a13SMatthew G. Knepley     PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
2361793a2a13SMatthew G. Knepley 
23623f27a4e6SJed Brown     // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and
23633f27a4e6SJed Brown     // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex
23643f27a4e6SJed Brown     // so that all tetrahedra have positive volume.
23659371c9d4SSatish Balay     if (f == 0)
23669371c9d4SSatish Balay       for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]);
23676858538eSMatthew G. Knepley     switch (faceTypes[f]) {
2368ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
23690ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
23706858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + 0] * dim + d]) - origin[d];
23716858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + 1] * dim + d]) - origin[d];
23726858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + 2] * dim + d]) - origin[d];
23730ec8681fSMatthew G. Knepley       }
23740ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
23756858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
23760ec8681fSMatthew G. Knepley       vsum += vtmp;
23774f25033aSJed Brown       if (centroid) { /* Centroid of OABC = (a+b+c)/4 */
23780ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
23791ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
23800ec8681fSMatthew G. Knepley         }
23810ec8681fSMatthew G. Knepley       }
23820ec8681fSMatthew G. Knepley       break;
2383ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
23849371c9d4SSatish Balay     case DM_POLYTOPE_SEG_PRISM_TENSOR: {
2385793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
2386793a2a13SMatthew G. Knepley 
2387793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
23889371c9d4SSatish Balay       if (isHybrid && f > 1) {
23899371c9d4SSatish Balay         fv[2] = 3;
23909371c9d4SSatish Balay         fv[3] = 2;
23919371c9d4SSatish Balay       }
23920ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
23930ec8681fSMatthew G. Knepley       /* First tet */
23940ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
23956858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[0]] * dim + d]) - origin[d];
23966858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
23976858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
23980ec8681fSMatthew G. Knepley       }
23990ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
24006858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
24010ec8681fSMatthew G. Knepley       vsum += vtmp;
24020ec8681fSMatthew G. Knepley       if (centroid) {
24030ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
24040ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
24050ec8681fSMatthew G. Knepley         }
24060ec8681fSMatthew G. Knepley       }
24070ec8681fSMatthew G. Knepley       /* Second tet */
24080ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
24096858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
24106858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[2]] * dim + d]) - origin[d];
24116858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
24120ec8681fSMatthew G. Knepley       }
24130ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
24146858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
24150ec8681fSMatthew G. Knepley       vsum += vtmp;
24160ec8681fSMatthew G. Knepley       if (centroid) {
24170ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
24180ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
24190ec8681fSMatthew G. Knepley         }
24200ec8681fSMatthew G. Knepley       }
24210ec8681fSMatthew G. Knepley       break;
2422793a2a13SMatthew G. Knepley     }
2423d71ae5a4SJacob Faibussowitsch     default:
2424d71ae5a4SJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %" PetscInt_FMT " of type %s", cone[f], DMPolytopeTypes[ct]);
24250ec8681fSMatthew G. Knepley     }
24266858538eSMatthew G. Knepley     fOff += faceSizes[f];
24270ec8681fSMatthew G. Knepley   }
24286858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
24296858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
24308763be8eSMatthew G. Knepley   if (vol) *vol = PetscAbsReal(vsum);
24319371c9d4SSatish Balay   if (normal)
24329371c9d4SSatish Balay     for (d = 0; d < dim; ++d) normal[d] = 0.0;
24339371c9d4SSatish Balay   if (centroid)
24349371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum * 4) + origin[d];
24350ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
24360ec8681fSMatthew G. Knepley }
24370ec8681fSMatthew G. Knepley 
2438834e62ceSMatthew G. Knepley /*@C
2439834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
2440834e62ceSMatthew G. Knepley 
2441d083f849SBarry Smith   Collective on dm
2442834e62ceSMatthew G. Knepley 
24434165533cSJose E. Roman   Input Parameters:
2444834e62ceSMatthew G. Knepley + dm   - the DM
2445834e62ceSMatthew G. Knepley - cell - the cell
2446834e62ceSMatthew G. Knepley 
24474165533cSJose E. Roman   Output Parameters:
2448834e62ceSMatthew G. Knepley + volume   - the cell volume
2449cc08537eSMatthew G. Knepley . centroid - the cell centroid
2450cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
2451834e62ceSMatthew G. Knepley 
2452834e62ceSMatthew G. Knepley   Level: advanced
2453834e62ceSMatthew G. Knepley 
2454834e62ceSMatthew G. Knepley   Fortran Notes:
2455834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2456834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
2457834e62ceSMatthew G. Knepley 
2458db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()`
2459834e62ceSMatthew G. Knepley @*/
2460d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2461d71ae5a4SJacob Faibussowitsch {
24620ec8681fSMatthew G. Knepley   PetscInt depth, dim;
2463834e62ceSMatthew G. Knepley 
2464834e62ceSMatthew G. Knepley   PetscFunctionBegin;
24659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24669566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
246708401ef6SPierre Jolivet   PetscCheck(depth == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
24689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetPointDepth(dm, cell, &depth));
2469011ea5d8SMatthew G. Knepley   switch (depth) {
2470d71ae5a4SJacob Faibussowitsch   case 0:
2471d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal));
2472d71ae5a4SJacob Faibussowitsch     break;
2473d71ae5a4SJacob Faibussowitsch   case 1:
2474d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal));
2475d71ae5a4SJacob Faibussowitsch     break;
2476d71ae5a4SJacob Faibussowitsch   case 2:
2477d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal));
2478d71ae5a4SJacob Faibussowitsch     break;
2479d71ae5a4SJacob Faibussowitsch   case 3:
2480d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal));
2481d71ae5a4SJacob Faibussowitsch     break;
2482d71ae5a4SJacob Faibussowitsch   default:
2483d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT " (depth %" PetscInt_FMT ") for element geometry computation", dim, depth);
2484834e62ceSMatthew G. Knepley   }
2485834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
2486834e62ceSMatthew G. Knepley }
2487113c68e6SMatthew G. Knepley 
2488c501906fSMatthew G. Knepley /*@
2489c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
2490c501906fSMatthew G. Knepley 
2491c501906fSMatthew G. Knepley   Collective on dm
2492c501906fSMatthew G. Knepley 
2493c501906fSMatthew G. Knepley   Input Parameter:
2494c501906fSMatthew G. Knepley . dm - The DMPlex
2495c501906fSMatthew G. Knepley 
2496c501906fSMatthew G. Knepley   Output Parameter:
2497c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
2498c501906fSMatthew G. Knepley 
2499c501906fSMatthew G. Knepley   Level: beginner
2500c501906fSMatthew G. Knepley 
2501c501906fSMatthew G. Knepley @*/
2502d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
2503d71ae5a4SJacob Faibussowitsch {
2504c0d900a5SMatthew G. Knepley   DM           dmCell;
2505c0d900a5SMatthew G. Knepley   Vec          coordinates;
2506c0d900a5SMatthew G. Knepley   PetscSection coordSection, sectionCell;
2507c0d900a5SMatthew G. Knepley   PetscScalar *cgeom;
2508412e9a14SMatthew G. Knepley   PetscInt     cStart, cEnd, c;
2509c0d900a5SMatthew G. Knepley 
2510c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
25119566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmCell));
25129566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
25139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
25149566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection));
25159566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmCell, coordinates));
25169566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionCell));
25179566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
25189566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd));
2519c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
25209566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFEGeom)) / sizeof(PetscScalar))));
25219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionCell));
25229566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmCell, sectionCell));
25239566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionCell));
25249566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmCell, cellgeom));
25259566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*cellgeom, &cgeom));
2526c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2527cf0b7c11SKarl Rupp     PetscFEGeom *cg;
2528c0d900a5SMatthew G. Knepley 
25299566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg));
25309566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(cg, 1));
25319566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ));
253263a3b9bcSJacob Faibussowitsch     PetscCheck(*cg->detJ > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %" PetscInt_FMT, (double)*cg->detJ, c);
2533c0d900a5SMatthew G. Knepley   }
2534c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
2535c0d900a5SMatthew G. Knepley }
2536c0d900a5SMatthew G. Knepley 
2537891a9168SMatthew G. Knepley /*@
2538891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2539891a9168SMatthew G. Knepley 
2540891a9168SMatthew G. Knepley   Input Parameter:
2541891a9168SMatthew G. Knepley . dm - The DM
2542891a9168SMatthew G. Knepley 
2543891a9168SMatthew G. Knepley   Output Parameters:
2544891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2545a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data
2546891a9168SMatthew G. Knepley 
2547891a9168SMatthew G. Knepley   Level: developer
2548891a9168SMatthew G. Knepley 
2549db781477SPatrick Sanan .seealso: `PetscFVFaceGeom`, `PetscFVCellGeom`, `DMPlexComputeGeometryFEM()`
2550891a9168SMatthew G. Knepley @*/
2551d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2552d71ae5a4SJacob Faibussowitsch {
2553113c68e6SMatthew G. Knepley   DM           dmFace, dmCell;
2554113c68e6SMatthew G. Knepley   DMLabel      ghostLabel;
2555113c68e6SMatthew G. Knepley   PetscSection sectionFace, sectionCell;
2556113c68e6SMatthew G. Knepley   PetscSection coordSection;
2557113c68e6SMatthew G. Knepley   Vec          coordinates;
2558113c68e6SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2559113c68e6SMatthew G. Knepley   PetscReal    minradius, gminradius;
2560113c68e6SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2561113c68e6SMatthew G. Knepley 
2562113c68e6SMatthew G. Knepley   PetscFunctionBegin;
25639566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25649566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
25659566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
2566113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
25679566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmCell));
25689566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection));
25699566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmCell, coordinates));
25709566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionCell));
25719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
25729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
25739566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd));
25749566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVCellGeom)) / sizeof(PetscScalar))));
25759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionCell));
25769566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmCell, sectionCell));
25779566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionCell));
25789566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmCell, cellgeom));
2579485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
25809566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*cellgeom, &cgeom));
2581113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2582113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2583113c68e6SMatthew G. Knepley 
25849566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg));
25859566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(cg, 1));
25869566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL));
2587113c68e6SMatthew G. Knepley   }
2588113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
25899566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmFace));
25909566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionFace));
25919566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
25929566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionFace, fStart, fEnd));
25939566063dSJacob Faibussowitsch   for (f = fStart; f < fEnd; ++f) PetscCall(PetscSectionSetDof(sectionFace, f, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVFaceGeom)) / sizeof(PetscScalar))));
25949566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionFace));
25959566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmFace, sectionFace));
25969566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionFace));
25979566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmFace, facegeom));
25989566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*facegeom, &fgeom));
25999566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2600113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2601113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2602113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2603113c68e6SMatthew G. Knepley     PetscReal        area;
2604412e9a14SMatthew G. Knepley     const PetscInt  *cells;
2605412e9a14SMatthew G. Knepley     PetscInt         ncells, ghost = -1, d, numChildren;
2606113c68e6SMatthew G. Knepley 
26079566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
26089566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
26099566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, f, &cells));
26109566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &ncells));
2611412e9a14SMatthew G. Knepley     /* It is possible to get a face with no support when using partition overlap */
2612412e9a14SMatthew G. Knepley     if (!ncells || ghost >= 0 || numChildren) continue;
26139566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, f, fgeom, &fg));
26149566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal));
2615113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2616113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2617113c68e6SMatthew G. Knepley     {
2618113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
2619113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
26200453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2621113c68e6SMatthew G. Knepley 
26229566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL));
2623113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
262406348e87SToby Isaac       if (ncells > 1) {
26259566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR));
2626113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
26279371c9d4SSatish Balay       } else {
262806348e87SToby Isaac         rcentroid = fg->centroid;
262906348e87SToby Isaac       }
26309566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l));
26319566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r));
26320453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2633113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2634113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2635113c68e6SMatthew G. Knepley       }
2636113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
263763a3b9bcSJacob 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]);
263863a3b9bcSJacob 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]);
263963a3b9bcSJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " could not be fixed", f);
2640113c68e6SMatthew G. Knepley       }
2641113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2642113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2643113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2644113c68e6SMatthew G. Knepley       }
264506348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2646113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2647113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2648113c68e6SMatthew G. Knepley       }
2649113c68e6SMatthew G. Knepley     }
2650113c68e6SMatthew G. Knepley   }
26511c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm)));
26529566063dSJacob Faibussowitsch   PetscCall(DMPlexSetMinRadius(dm, gminradius));
2653113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2654113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2655113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2656113c68e6SMatthew G. Knepley     const PetscInt  *cone, *support;
2657113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2658113c68e6SMatthew G. Knepley 
26599566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dmCell, c, &coneSize));
266063a3b9bcSJacob Faibussowitsch     PetscCheck(coneSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %" PetscInt_FMT " has cone size %" PetscInt_FMT " != 1", c, coneSize);
26619566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dmCell, c, &cone));
26629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmCell, cone[0], &supportSize));
266363a3b9bcSJacob Faibussowitsch     PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " has support size %" PetscInt_FMT " != 2", cone[0], supportSize);
26649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dmCell, cone[0], &support));
26659566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg));
2666113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2667113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2668113c68e6SMatthew G. Knepley       if (support[s] == c) {
2669640bce14SSatish Balay         PetscFVCellGeom *ci;
2670113c68e6SMatthew G. Knepley         PetscFVCellGeom *cg;
2671113c68e6SMatthew G. Knepley         PetscReal        c2f[3], a;
2672113c68e6SMatthew G. Knepley 
26739566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, support[(s + 1) % 2], cgeom, &ci));
2674113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2675113c68e6SMatthew G. Knepley         a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal) / DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
26769566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg));
2677113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2 * a, fg->normal, ci->centroid, cg->centroid);
2678113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2679113c68e6SMatthew G. Knepley       }
2680113c68e6SMatthew G. Knepley     }
2681113c68e6SMatthew G. Knepley   }
26829566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*facegeom, &fgeom));
26839566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*cellgeom, &cgeom));
26849566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmCell));
26859566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmFace));
2686113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2687113c68e6SMatthew G. Knepley }
2688113c68e6SMatthew G. Knepley 
2689113c68e6SMatthew G. Knepley /*@C
2690113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2691113c68e6SMatthew G. Knepley 
2692113c68e6SMatthew G. Knepley   Not collective
2693113c68e6SMatthew G. Knepley 
26944165533cSJose E. Roman   Input Parameter:
2695113c68e6SMatthew G. Knepley . dm - the DM
2696113c68e6SMatthew G. Knepley 
26974165533cSJose E. Roman   Output Parameter:
2698a5b23f4aSJose E. Roman . minradius - the minimum cell radius
2699113c68e6SMatthew G. Knepley 
2700113c68e6SMatthew G. Knepley   Level: developer
2701113c68e6SMatthew G. Knepley 
2702db781477SPatrick Sanan .seealso: `DMGetCoordinates()`
2703113c68e6SMatthew G. Knepley @*/
2704d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2705d71ae5a4SJacob Faibussowitsch {
2706113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2707113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2708dadcf809SJacob Faibussowitsch   PetscValidRealPointer(minradius, 2);
2709113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex *)dm->data)->minradius;
2710113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2711113c68e6SMatthew G. Knepley }
2712113c68e6SMatthew G. Knepley 
2713113c68e6SMatthew G. Knepley /*@C
2714113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2715113c68e6SMatthew G. Knepley 
2716113c68e6SMatthew G. Knepley   Logically collective
2717113c68e6SMatthew G. Knepley 
27184165533cSJose E. Roman   Input Parameters:
2719113c68e6SMatthew G. Knepley + dm - the DM
2720a5b23f4aSJose E. Roman - minradius - the minimum cell radius
2721113c68e6SMatthew G. Knepley 
2722113c68e6SMatthew G. Knepley   Level: developer
2723113c68e6SMatthew G. Knepley 
2724db781477SPatrick Sanan .seealso: `DMSetCoordinates()`
2725113c68e6SMatthew G. Knepley @*/
2726d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2727d71ae5a4SJacob Faibussowitsch {
2728113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2729113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2730113c68e6SMatthew G. Knepley   ((DM_Plex *)dm->data)->minradius = minradius;
2731113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2732113c68e6SMatthew G. Knepley }
2733856ac710SMatthew G. Knepley 
2734d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2735d71ae5a4SJacob Faibussowitsch {
2736856ac710SMatthew G. Knepley   DMLabel      ghostLabel;
2737856ac710SMatthew G. Knepley   PetscScalar *dx, *grad, **gref;
2738856ac710SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2739856ac710SMatthew G. Knepley 
2740856ac710SMatthew G. Knepley   PetscFunctionBegin;
27419566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
27429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
27439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2744089217ebSMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
27459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxNumFaces, NULL));
27469566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
27479566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
27489566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2749856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2750856ac710SMatthew G. Knepley     const PetscInt  *faces;
2751856ac710SMatthew G. Knepley     PetscInt         numFaces, usedFaces, f, d;
2752640bce14SSatish Balay     PetscFVCellGeom *cg;
2753856ac710SMatthew G. Knepley     PetscBool        boundary;
2754856ac710SMatthew G. Knepley     PetscInt         ghost;
2755856ac710SMatthew G. Knepley 
2756a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
2757a79418b7SMatt McGurn     PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2758a79418b7SMatt McGurn     if (ghost >= 0) continue;
2759a79418b7SMatt McGurn 
27609566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
27619566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &numFaces));
27629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &faces));
276363a3b9bcSJacob 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);
2764856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2765640bce14SSatish Balay       PetscFVCellGeom *cg1;
2766856ac710SMatthew G. Knepley       PetscFVFaceGeom *fg;
2767856ac710SMatthew G. Knepley       const PetscInt  *fcells;
2768856ac710SMatthew G. Knepley       PetscInt         ncell, side;
2769856ac710SMatthew G. Knepley 
27709566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
27719566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2772856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
27739566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, faces[f], &fcells));
2774856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2775856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
27769566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg));
27779566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2778856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces * dim + d] = cg1->centroid[d] - cg->centroid[d];
2779856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */
2780856ac710SMatthew G. Knepley     }
278128b400f6SJacob Faibussowitsch     PetscCheck(usedFaces, PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
27829566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, usedFaces, dx, grad));
2783856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
27849566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
27859566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2786856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2787856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces * dim + d];
2788856ac710SMatthew G. Knepley       ++usedFaces;
2789856ac710SMatthew G. Knepley     }
2790856ac710SMatthew G. Knepley   }
27919566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
2792856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2793856ac710SMatthew G. Knepley }
2794856ac710SMatthew G. Knepley 
2795d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2796d71ae5a4SJacob Faibussowitsch {
2797b81db932SToby Isaac   DMLabel      ghostLabel;
2798b81db932SToby Isaac   PetscScalar *dx, *grad, **gref;
2799b81db932SToby Isaac   PetscInt     dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2800b81db932SToby Isaac   PetscSection neighSec;
2801b81db932SToby Isaac   PetscInt(*neighbors)[2];
2802b81db932SToby Isaac   PetscInt *counter;
2803b81db932SToby Isaac 
2804b81db932SToby Isaac   PetscFunctionBegin;
28059566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
28069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
28079566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2808485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
28099566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &neighSec));
28109566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(neighSec, cStart, cEndInterior));
28119566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
28129566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2813b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2814b81db932SToby Isaac     const PetscInt *fcells;
2815b81db932SToby Isaac     PetscBool       boundary;
28165bc680faSToby Isaac     PetscInt        ghost = -1;
2817b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2818b81db932SToby Isaac 
28199566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
28209566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
28219566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2822b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
28239566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
282406348e87SToby Isaac     if (numCells == 2) {
28259566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2826b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2827b81db932SToby Isaac         PetscInt cell = fcells[c];
2828b81db932SToby Isaac 
282948a46eb9SPierre Jolivet         if (cell >= cStart && cell < cEndInterior) PetscCall(PetscSectionAddDof(neighSec, cell, 1));
2830b81db932SToby Isaac       }
2831b81db932SToby Isaac     }
283206348e87SToby Isaac   }
28339566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(neighSec));
28349566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetMaxDof(neighSec, &maxNumFaces));
28359566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
2836b81db932SToby Isaac   nStart = 0;
28379566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(neighSec, &nEnd));
28389566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1((nEnd - nStart), &neighbors));
28399566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1((cEndInterior - cStart), &counter));
2840b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2841b81db932SToby Isaac     const PetscInt *fcells;
2842b81db932SToby Isaac     PetscBool       boundary;
28435bc680faSToby Isaac     PetscInt        ghost = -1;
2844b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2845b81db932SToby Isaac 
28469566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
28479566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
28489566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2849b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
28509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
285106348e87SToby Isaac     if (numCells == 2) {
28529566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2853b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2854b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2855b81db932SToby Isaac 
2856e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
28579566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(neighSec, cell, &off));
2858b81db932SToby Isaac           off += counter[cell - cStart]++;
2859b81db932SToby Isaac           neighbors[off][0] = f;
2860b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2861b81db932SToby Isaac         }
2862b81db932SToby Isaac       }
2863b81db932SToby Isaac     }
286406348e87SToby Isaac   }
28659566063dSJacob Faibussowitsch   PetscCall(PetscFree(counter));
28669566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2867b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2868317218b9SToby Isaac     PetscInt         numFaces, f, d, off, ghost = -1;
2869640bce14SSatish Balay     PetscFVCellGeom *cg;
2870b81db932SToby Isaac 
28719566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
28729566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(neighSec, c, &numFaces));
28739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(neighSec, c, &off));
2874a79418b7SMatt McGurn 
2875a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
28769566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2877a79418b7SMatt McGurn     if (ghost >= 0) continue;
2878a79418b7SMatt McGurn 
287963a3b9bcSJacob 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);
2880b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2881640bce14SSatish Balay       PetscFVCellGeom *cg1;
2882b81db932SToby Isaac       PetscFVFaceGeom *fg;
2883b81db932SToby Isaac       const PetscInt  *fcells;
2884b81db932SToby Isaac       PetscInt         ncell, side, nface;
2885b81db932SToby Isaac 
2886b81db932SToby Isaac       nface = neighbors[off + f][0];
2887b81db932SToby Isaac       ncell = neighbors[off + f][1];
28889566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, nface, &fcells));
2889b81db932SToby Isaac       side = (c != fcells[0]);
28909566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, nface, fgeom, &fg));
28919566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2892b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f * dim + d] = cg1->centroid[d] - cg->centroid[d];
2893b81db932SToby Isaac       gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */
2894b81db932SToby Isaac     }
28959566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, numFaces, dx, grad));
2896b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2897b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f * dim + d];
2898b81db932SToby Isaac     }
2899b81db932SToby Isaac   }
29009566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
29019566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&neighSec));
29029566063dSJacob Faibussowitsch   PetscCall(PetscFree(neighbors));
2903b81db932SToby Isaac   PetscFunctionReturn(0);
2904b81db932SToby Isaac }
2905b81db932SToby Isaac 
2906856ac710SMatthew G. Knepley /*@
2907856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2908856ac710SMatthew G. Knepley 
2909d083f849SBarry Smith   Collective on dm
2910856ac710SMatthew G. Knepley 
29114165533cSJose E. Roman   Input Parameters:
2912856ac710SMatthew G. Knepley + dm  - The DM
2913856ac710SMatthew G. Knepley . fvm - The PetscFV
29148f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2915856ac710SMatthew G. Knepley 
29166b867d5aSJose E. Roman   Input/Output Parameter:
29176b867d5aSJose E. Roman . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM(); on output
29186b867d5aSJose E. Roman                  the geometric factors for gradient calculation are inserted
29196b867d5aSJose E. Roman 
29206b867d5aSJose E. Roman   Output Parameter:
29216b867d5aSJose E. Roman . dmGrad - The DM describing the layout of gradient data
2922856ac710SMatthew G. Knepley 
2923856ac710SMatthew G. Knepley   Level: developer
2924856ac710SMatthew G. Knepley 
2925db781477SPatrick Sanan .seealso: `DMPlexGetFaceGeometryFVM()`, `DMPlexGetCellGeometryFVM()`
2926856ac710SMatthew G. Knepley @*/
2927d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2928d71ae5a4SJacob Faibussowitsch {
2929856ac710SMatthew G. Knepley   DM           dmFace, dmCell;
2930856ac710SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2931b81db932SToby Isaac   PetscSection sectionGrad, parentSection;
2932856ac710SMatthew G. Knepley   PetscInt     dim, pdim, cStart, cEnd, cEndInterior, c;
2933856ac710SMatthew G. Knepley 
2934856ac710SMatthew G. Knepley   PetscFunctionBegin;
29359566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
29369566063dSJacob Faibussowitsch   PetscCall(PetscFVGetNumComponents(fvm, &pdim));
29379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
29389566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2939856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
29409566063dSJacob Faibussowitsch   PetscCall(VecGetDM(faceGeometry, &dmFace));
29419566063dSJacob Faibussowitsch   PetscCall(VecGetDM(cellGeometry, &dmCell));
29429566063dSJacob Faibussowitsch   PetscCall(VecGetArray(faceGeometry, &fgeom));
29439566063dSJacob Faibussowitsch   PetscCall(VecGetArray(cellGeometry, &cgeom));
29449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &parentSection, NULL, NULL, NULL, NULL));
2945b81db932SToby Isaac   if (!parentSection) {
29469566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2947b5a3613cSMatthew G. Knepley   } else {
29489566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2949b81db932SToby Isaac   }
29509566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(faceGeometry, &fgeom));
29519566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(cellGeometry, &cgeom));
2952856ac710SMatthew G. Knepley   /* Create storage for gradients */
29539566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, dmGrad));
29549566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionGrad));
29559566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionGrad, cStart, cEnd));
29569566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionGrad, c, pdim * dim));
29579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionGrad));
29589566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(*dmGrad, sectionGrad));
29599566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionGrad));
2960856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2961856ac710SMatthew G. Knepley }
2962b27d5b9eSToby Isaac 
2963c501906fSMatthew G. Knepley /*@
2964c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2965c501906fSMatthew G. Knepley 
2966d083f849SBarry Smith   Collective on dm
2967c501906fSMatthew G. Knepley 
29684165533cSJose E. Roman   Input Parameters:
2969c501906fSMatthew G. Knepley + dm  - The DM
29706b867d5aSJose E. Roman - fv  - The PetscFV
2971c501906fSMatthew G. Knepley 
2972c501906fSMatthew G. Knepley   Output Parameters:
2973c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2974c501906fSMatthew G. Knepley . faceGeometry - The face geometry
29756b867d5aSJose E. Roman - gradDM       - The gradient matrices
2976c501906fSMatthew G. Knepley 
2977c501906fSMatthew G. Knepley   Level: developer
2978c501906fSMatthew G. Knepley 
2979db781477SPatrick Sanan .seealso: `DMPlexComputeGeometryFVM()`
2980c501906fSMatthew G. Knepley @*/
2981d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2982d71ae5a4SJacob Faibussowitsch {
2983b27d5b9eSToby Isaac   PetscObject cellgeomobj, facegeomobj;
2984b27d5b9eSToby Isaac 
2985b27d5b9eSToby Isaac   PetscFunctionBegin;
29869566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
2987b27d5b9eSToby Isaac   if (!cellgeomobj) {
2988b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2989b27d5b9eSToby Isaac 
29909566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt));
29919566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_cellgeom_fvm", (PetscObject)cellgeomInt));
29929566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_facegeom_fvm", (PetscObject)facegeomInt));
29939566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cellgeomInt));
29949566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&facegeomInt));
29959566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
2996b27d5b9eSToby Isaac   }
29979566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_facegeom_fvm", &facegeomobj));
2998b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec)cellgeomobj;
2999b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec)facegeomobj;
3000b27d5b9eSToby Isaac   if (gradDM) {
3001b27d5b9eSToby Isaac     PetscObject gradobj;
3002b27d5b9eSToby Isaac     PetscBool   computeGradients;
3003b27d5b9eSToby Isaac 
30049566063dSJacob Faibussowitsch     PetscCall(PetscFVGetComputeGradients(fv, &computeGradients));
3005b27d5b9eSToby Isaac     if (!computeGradients) {
3006b27d5b9eSToby Isaac       *gradDM = NULL;
3007b27d5b9eSToby Isaac       PetscFunctionReturn(0);
3008b27d5b9eSToby Isaac     }
30099566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
3010b27d5b9eSToby Isaac     if (!gradobj) {
3011b27d5b9eSToby Isaac       DM dmGradInt;
3012b27d5b9eSToby Isaac 
30139566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeGradientFVM(dm, fv, (Vec)facegeomobj, (Vec)cellgeomobj, &dmGradInt));
30149566063dSJacob Faibussowitsch       PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt));
30159566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dmGradInt));
30169566063dSJacob Faibussowitsch       PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
3017b27d5b9eSToby Isaac     }
3018b27d5b9eSToby Isaac     *gradDM = (DM)gradobj;
3019b27d5b9eSToby Isaac   }
3020b27d5b9eSToby Isaac   PetscFunctionReturn(0);
3021b27d5b9eSToby Isaac }
3022d6143a4eSToby Isaac 
3023d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess)
3024d71ae5a4SJacob Faibussowitsch {
30259d150b73SToby Isaac   PetscInt l, m;
30269d150b73SToby Isaac 
3027cd345991SToby Isaac   PetscFunctionBeginHot;
30289d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
30299d150b73SToby Isaac     /* invert Jacobian, multiply */
30309d150b73SToby Isaac     PetscScalar det, idet;
30319d150b73SToby Isaac 
30329d150b73SToby Isaac     switch (dimR) {
3033d71ae5a4SJacob Faibussowitsch     case 1:
3034d71ae5a4SJacob Faibussowitsch       invJ[0] = 1. / J[0];
3035d71ae5a4SJacob Faibussowitsch       break;
30369d150b73SToby Isaac     case 2:
30379d150b73SToby Isaac       det     = J[0] * J[3] - J[1] * J[2];
30389d150b73SToby Isaac       idet    = 1. / det;
30399d150b73SToby Isaac       invJ[0] = J[3] * idet;
30409d150b73SToby Isaac       invJ[1] = -J[1] * idet;
30419d150b73SToby Isaac       invJ[2] = -J[2] * idet;
30429d150b73SToby Isaac       invJ[3] = J[0] * idet;
30439d150b73SToby Isaac       break;
30449371c9d4SSatish Balay     case 3: {
30459d150b73SToby Isaac       invJ[0] = J[4] * J[8] - J[5] * J[7];
30469d150b73SToby Isaac       invJ[1] = J[2] * J[7] - J[1] * J[8];
30479d150b73SToby Isaac       invJ[2] = J[1] * J[5] - J[2] * J[4];
30489d150b73SToby Isaac       det     = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
30499d150b73SToby Isaac       idet    = 1. / det;
30509d150b73SToby Isaac       invJ[0] *= idet;
30519d150b73SToby Isaac       invJ[1] *= idet;
30529d150b73SToby Isaac       invJ[2] *= idet;
30539d150b73SToby Isaac       invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]);
30549d150b73SToby Isaac       invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]);
30559d150b73SToby Isaac       invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]);
30569d150b73SToby Isaac       invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]);
30579d150b73SToby Isaac       invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]);
30589d150b73SToby Isaac       invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]);
30599371c9d4SSatish Balay     } break;
30609d150b73SToby Isaac     }
30619d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
3062ad540459SPierre Jolivet       for (m = 0; m < dimC; m++) guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
30639d150b73SToby Isaac     }
30649d150b73SToby Isaac   } else {
30659d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
30669d150b73SToby Isaac     char transpose = 'C';
30679d150b73SToby Isaac #else
30689d150b73SToby Isaac     char transpose = 'T';
30699d150b73SToby Isaac #endif
30709d150b73SToby Isaac     PetscBLASInt m        = dimR;
30719d150b73SToby Isaac     PetscBLASInt n        = dimC;
30729d150b73SToby Isaac     PetscBLASInt one      = 1;
30739d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
30749d150b73SToby Isaac 
3075ad540459SPierre Jolivet     for (l = 0; l < dimC; l++) invJ[l] = resNeg[l];
30769d150b73SToby Isaac 
3077792fecdfSBarry Smith     PetscCallBLAS("LAPACKgels", LAPACKgels_(&transpose, &m, &n, &one, J, &m, invJ, &n, work, &worksize, &info));
307808401ef6SPierre Jolivet     PetscCheck(info == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Bad argument to GELS");
30799d150b73SToby Isaac 
3080ad540459SPierre Jolivet     for (l = 0; l < dimR; l++) guess[l] += PetscRealPart(invJ[l]);
30819d150b73SToby Isaac   }
30829d150b73SToby Isaac   PetscFunctionReturn(0);
30839d150b73SToby Isaac }
30849d150b73SToby Isaac 
3085d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
3086d71ae5a4SJacob Faibussowitsch {
3087c0cbe899SToby Isaac   PetscInt     coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
30889d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
30899d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
30909d150b73SToby Isaac   PetscScalar *J, *invJ, *work;
30919d150b73SToby Isaac 
30929d150b73SToby Isaac   PetscFunctionBegin;
30939d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30949566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
30951dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
30969566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
30979566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
30989d150b73SToby Isaac   cellCoords = &cellData[0];
30999d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
31009d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
31019d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
31029d150b73SToby Isaac   invJ       = &J[dimR * dimC];
31039d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
31049d150b73SToby Isaac   if (dimR == 2) {
31059d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
31069d150b73SToby Isaac 
31079d150b73SToby Isaac     for (i = 0; i < 4; i++) {
31089d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31099d150b73SToby Isaac 
3110ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
31119d150b73SToby Isaac     }
31129d150b73SToby Isaac   } else if (dimR == 3) {
31139d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
31149d150b73SToby Isaac 
31159d150b73SToby Isaac     for (i = 0; i < 8; i++) {
31169d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31179d150b73SToby Isaac 
3118ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
31199d150b73SToby Isaac     }
31209d150b73SToby Isaac   } else {
3121ad540459SPierre Jolivet     for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]);
31229d150b73SToby Isaac   }
31239d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
31249d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
31259d150b73SToby Isaac     PetscReal *swap;
31269d150b73SToby Isaac 
31279d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
31289d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
31299d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
31309d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
31319d150b73SToby Isaac       }
31329d150b73SToby Isaac     }
31339d150b73SToby Isaac 
31349d150b73SToby Isaac     if (i < dimR - 1) {
31359d150b73SToby Isaac       swap       = cellCoeffs;
31369d150b73SToby Isaac       cellCoeffs = cellCoords;
31379d150b73SToby Isaac       cellCoords = swap;
31389d150b73SToby Isaac     }
31399d150b73SToby Isaac   }
31409566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(refCoords, numPoints * dimR));
31419d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
31429d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
31439d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
31449d150b73SToby Isaac 
31459d150b73SToby Isaac       /* compute -residual and Jacobian */
3146ad540459SPierre Jolivet       for (k = 0; k < dimC; k++) resNeg[k] = realCoords[dimC * j + k];
3147ad540459SPierre Jolivet       for (k = 0; k < dimC * dimR; k++) J[k] = 0.;
31489d150b73SToby Isaac       for (k = 0; k < numV; k++) {
31499d150b73SToby Isaac         PetscReal extCoord = 1.;
31509d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
31519d150b73SToby Isaac           PetscReal coord = guess[l];
31529d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
31539d150b73SToby Isaac 
31549d150b73SToby Isaac           extCoord *= dep * coord + !dep;
31559d150b73SToby Isaac           extJ[l] = dep;
31569d150b73SToby Isaac 
31579d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
31589d150b73SToby Isaac             PetscReal coord = guess[m];
31599d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
31609d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
31619d150b73SToby Isaac 
31629d150b73SToby Isaac             extJ[l] *= mult;
31639d150b73SToby Isaac           }
31649d150b73SToby Isaac         }
31659d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
31669d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
31679d150b73SToby Isaac 
31689d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
3169ad540459SPierre Jolivet           for (m = 0; m < dimR; m++) J[dimR * l + m] += coeff * extJ[m];
31709d150b73SToby Isaac         }
31719d150b73SToby Isaac       }
317276bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
31730611203eSToby Isaac         PetscReal maxAbs = 0.;
31740611203eSToby Isaac 
3175ad540459SPierre Jolivet         for (l = 0; l < dimC; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l]));
317663a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
31770611203eSToby Isaac       }
31789d150b73SToby Isaac 
31799566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(dimC, dimR, J, invJ, work, resNeg, guess));
31809d150b73SToby Isaac     }
31819d150b73SToby Isaac   }
31829566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
31839566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
31849566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
31859d150b73SToby Isaac   PetscFunctionReturn(0);
31869d150b73SToby Isaac }
31879d150b73SToby Isaac 
3188d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
3189d71ae5a4SJacob Faibussowitsch {
31909d150b73SToby Isaac   PetscInt     coordSize, i, j, k, l, numV = (1 << dimR);
31919d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
31929d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs;
31939d150b73SToby Isaac 
31949d150b73SToby Isaac   PetscFunctionBegin;
31959d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31969566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
31971dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
31989566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
31999d150b73SToby Isaac   cellCoords = &cellData[0];
32009d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
32019d150b73SToby Isaac   if (dimR == 2) {
32029d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
32039d150b73SToby Isaac 
32049d150b73SToby Isaac     for (i = 0; i < 4; i++) {
32059d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
32069d150b73SToby Isaac 
3207ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
32089d150b73SToby Isaac     }
32099d150b73SToby Isaac   } else if (dimR == 3) {
32109d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
32119d150b73SToby Isaac 
32129d150b73SToby Isaac     for (i = 0; i < 8; i++) {
32139d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
32149d150b73SToby Isaac 
3215ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
32169d150b73SToby Isaac     }
32179d150b73SToby Isaac   } else {
3218ad540459SPierre Jolivet     for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]);
32199d150b73SToby Isaac   }
32209d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
32219d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
32229d150b73SToby Isaac     PetscReal *swap;
32239d150b73SToby Isaac 
32249d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
32259d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
32269d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
32279d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
32289d150b73SToby Isaac       }
32299d150b73SToby Isaac     }
32309d150b73SToby Isaac 
32319d150b73SToby Isaac     if (i < dimR - 1) {
32329d150b73SToby Isaac       swap       = cellCoeffs;
32339d150b73SToby Isaac       cellCoeffs = cellCoords;
32349d150b73SToby Isaac       cellCoords = swap;
32359d150b73SToby Isaac     }
32369d150b73SToby Isaac   }
32379566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(realCoords, numPoints * dimC));
32389d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32399d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
32409d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
32419d150b73SToby Isaac 
32429d150b73SToby Isaac     for (k = 0; k < numV; k++) {
32439d150b73SToby Isaac       PetscReal extCoord = 1.;
32449d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
32459d150b73SToby Isaac         PetscReal coord = guess[l];
32469d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
32479d150b73SToby Isaac 
32489d150b73SToby Isaac         extCoord *= dep * coord + !dep;
32499d150b73SToby Isaac       }
32509d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
32519d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
32529d150b73SToby Isaac 
32539d150b73SToby Isaac         mapped[l] += coeff * extCoord;
32549d150b73SToby Isaac       }
32559d150b73SToby Isaac     }
32569d150b73SToby Isaac   }
32579566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
32589566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
32599d150b73SToby Isaac   PetscFunctionReturn(0);
32609d150b73SToby Isaac }
32619d150b73SToby Isaac 
32629c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
3263d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
3264d71ae5a4SJacob Faibussowitsch {
32659c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
3266c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3267c6e120d1SToby Isaac   PetscReal   *invV, *modes;
3268c6e120d1SToby Isaac   PetscReal   *B, *D, *resNeg;
3269c6e120d1SToby Isaac   PetscScalar *J, *invJ, *work;
32709d150b73SToby Isaac 
32719d150b73SToby Isaac   PetscFunctionBegin;
32729566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
32739566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
327463a3b9bcSJacob 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);
32759566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32769d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
32779566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
32789d150b73SToby Isaac   invV = fe->invV;
3279012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3280012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3281ad540459SPierre Jolivet     for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
32829d150b73SToby Isaac   }
32839566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
32849c3cf19fSMatthew G. Knepley   D      = &B[pdim * Nc];
32859c3cf19fSMatthew G. Knepley   resNeg = &D[pdim * Nc * dimR];
32869566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
32879c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
32889c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
3289ad540459SPierre Jolivet   for (i = 0; i < numPoints * dimR; i++) refCoords[i] = 0.;
32909d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32919b1f03cbSToby 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 */
32929d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
32939566063dSJacob Faibussowitsch       PetscCall(PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL));
3294ad540459SPierre Jolivet       for (k = 0; k < Nc; k++) resNeg[k] = realCoords[j * Nc + k];
3295ad540459SPierre Jolivet       for (k = 0; k < Nc * dimR; k++) J[k] = 0.;
32969c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
32979c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
3298012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
3299ad540459SPierre Jolivet           for (m = 0; m < dimR; m++) J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
33009d150b73SToby Isaac         }
33019d150b73SToby Isaac       }
330276bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
33030611203eSToby Isaac         PetscReal maxAbs = 0.;
33040611203eSToby Isaac 
3305ad540459SPierre Jolivet         for (l = 0; l < Nc; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l]));
330663a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
33070611203eSToby Isaac       }
33089566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(Nc, dimR, J, invJ, work, resNeg, guess));
33099d150b73SToby Isaac     }
33109d150b73SToby Isaac   }
33119566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
33129566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
33139566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
33149566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
33159d150b73SToby Isaac   PetscFunctionReturn(0);
33169d150b73SToby Isaac }
33179d150b73SToby Isaac 
33189c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
3319d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
3320d71ae5a4SJacob Faibussowitsch {
33219c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, coordSize;
3322c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3323c6e120d1SToby Isaac   PetscReal   *invV, *modes;
33249d150b73SToby Isaac   PetscReal   *B;
33259d150b73SToby Isaac 
33269d150b73SToby Isaac   PetscFunctionBegin;
33279566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
33289566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
332963a3b9bcSJacob 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);
33309566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
33319d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
33329566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
33339d150b73SToby Isaac   invV = fe->invV;
3334012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3335012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3336ad540459SPierre Jolivet     for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
33379d150b73SToby Isaac   }
33389566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
33399566063dSJacob Faibussowitsch   PetscCall(PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL));
3340ad540459SPierre Jolivet   for (i = 0; i < numPoints * Nc; i++) realCoords[i] = 0.;
33419d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
33429c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
33439d150b73SToby Isaac 
33449c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
3345ad540459SPierre Jolivet       for (l = 0; l < Nc; l++) mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
33469d150b73SToby Isaac     }
33479d150b73SToby Isaac   }
33489566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
33499566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
33509566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
33519d150b73SToby Isaac   PetscFunctionReturn(0);
33529d150b73SToby Isaac }
33539d150b73SToby Isaac 
3354d6143a4eSToby Isaac /*@
3355d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
3356d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
3357d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
3358d6143a4eSToby Isaac 
3359d6143a4eSToby Isaac   Not collective
3360d6143a4eSToby Isaac 
3361d6143a4eSToby Isaac   Input Parameters:
3362d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
3363d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
3364d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
3365d6143a4eSToby Isaac . cell       - the cell whose map is used.
3366d6143a4eSToby Isaac . numPoints  - the number of points to locate
33671b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
3368d6143a4eSToby Isaac 
3369d6143a4eSToby Isaac   Output Parameters:
3370d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
33711b266c99SBarry Smith 
33721b266c99SBarry Smith   Level: intermediate
337373c9229bSMatthew Knepley 
3374db781477SPatrick Sanan .seealso: `DMPlexReferenceToCoordinates()`
3375d6143a4eSToby Isaac @*/
3376d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
3377d71ae5a4SJacob Faibussowitsch {
3378485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
33799d150b73SToby Isaac   DM       coordDM = NULL;
33809d150b73SToby Isaac   Vec      coords;
33819d150b73SToby Isaac   PetscFE  fe = NULL;
33829d150b73SToby Isaac 
3383d6143a4eSToby Isaac   PetscFunctionBegin;
33849d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33859566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
33869566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
33879d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
33889566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
33899566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
33909566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
33919d150b73SToby Isaac   if (coordDM) {
33929d150b73SToby Isaac     PetscInt coordFields;
33939d150b73SToby Isaac 
33949566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
33959d150b73SToby Isaac     if (coordFields) {
33969d150b73SToby Isaac       PetscClassId id;
33979d150b73SToby Isaac       PetscObject  disc;
33989d150b73SToby Isaac 
33999566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
34009566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
3401ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
34029d150b73SToby Isaac     }
34039d150b73SToby Isaac   }
34049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
34051dca8a05SBarry 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);
34069d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
34079d150b73SToby Isaac     PetscInt  coneSize;
34089d150b73SToby Isaac     PetscBool isSimplex, isTensor;
34099d150b73SToby Isaac 
34109566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
34119d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
34129d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
34139d150b73SToby Isaac     if (isSimplex) {
34149d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
34159d150b73SToby Isaac 
34169566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34179d150b73SToby Isaac       J    = &v0[dimC];
34189d150b73SToby Isaac       invJ = &J[dimC * dimC];
34199566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ));
34209d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3421c330f8ffSToby Isaac         const PetscReal x0[3] = {-1., -1., -1.};
3422c330f8ffSToby Isaac 
3423c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
34249d150b73SToby Isaac       }
34259566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34269d150b73SToby Isaac     } else if (isTensor) {
34279566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
342863a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
34299d150b73SToby Isaac   } else {
34309566063dSJacob Faibussowitsch     PetscCall(DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
34319d150b73SToby Isaac   }
34329d150b73SToby Isaac   PetscFunctionReturn(0);
34339d150b73SToby Isaac }
34349d150b73SToby Isaac 
34359d150b73SToby Isaac /*@
34369d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
34379d150b73SToby Isaac 
34389d150b73SToby Isaac   Not collective
34399d150b73SToby Isaac 
34409d150b73SToby Isaac   Input Parameters:
34419d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
34429d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
34439d150b73SToby Isaac                as a multilinear map for tensor-product elements
34449d150b73SToby Isaac . cell       - the cell whose map is used.
34459d150b73SToby Isaac . numPoints  - the number of points to locate
3446a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
34479d150b73SToby Isaac 
34489d150b73SToby Isaac   Output Parameters:
34499d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
34501b266c99SBarry Smith 
34511b266c99SBarry Smith    Level: intermediate
345273c9229bSMatthew Knepley 
3453db781477SPatrick Sanan .seealso: `DMPlexCoordinatesToReference()`
34549d150b73SToby Isaac @*/
3455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
3456d71ae5a4SJacob Faibussowitsch {
3457485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
34589d150b73SToby Isaac   DM       coordDM = NULL;
34599d150b73SToby Isaac   Vec      coords;
34609d150b73SToby Isaac   PetscFE  fe = NULL;
34619d150b73SToby Isaac 
34629d150b73SToby Isaac   PetscFunctionBegin;
34639d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34649566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
34659566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
34669d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
34679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
34689566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
34699566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
34709d150b73SToby Isaac   if (coordDM) {
34719d150b73SToby Isaac     PetscInt coordFields;
34729d150b73SToby Isaac 
34739566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
34749d150b73SToby Isaac     if (coordFields) {
34759d150b73SToby Isaac       PetscClassId id;
34769d150b73SToby Isaac       PetscObject  disc;
34779d150b73SToby Isaac 
34789566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
34799566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
3480ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
34819d150b73SToby Isaac     }
34829d150b73SToby Isaac   }
34839566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
34841dca8a05SBarry 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);
34859d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
34869d150b73SToby Isaac     PetscInt  coneSize;
34879d150b73SToby Isaac     PetscBool isSimplex, isTensor;
34889d150b73SToby Isaac 
34899566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
34909d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
34919d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
34929d150b73SToby Isaac     if (isSimplex) {
34939d150b73SToby Isaac       PetscReal detJ, *v0, *J;
34949d150b73SToby Isaac 
34959566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34969d150b73SToby Isaac       J = &v0[dimC];
34979566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ));
3498c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3499c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3500c330f8ffSToby Isaac 
3501c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
35029d150b73SToby Isaac       }
35039566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
35049d150b73SToby Isaac     } else if (isTensor) {
35059566063dSJacob Faibussowitsch       PetscCall(DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
350663a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
35079d150b73SToby Isaac   } else {
35089566063dSJacob Faibussowitsch     PetscCall(DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
35099d150b73SToby Isaac   }
3510d6143a4eSToby Isaac   PetscFunctionReturn(0);
3511d6143a4eSToby Isaac }
35120139fca9SMatthew G. Knepley 
35130139fca9SMatthew G. Knepley /*@C
35140139fca9SMatthew G. Knepley   DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates.
35150139fca9SMatthew G. Knepley 
35160139fca9SMatthew G. Knepley   Not collective
35170139fca9SMatthew G. Knepley 
35180139fca9SMatthew G. Knepley   Input Parameters:
35190139fca9SMatthew G. Knepley + dm      - The DM
35200139fca9SMatthew G. Knepley . time    - The time
35210139fca9SMatthew G. Knepley - func    - The function transforming current coordinates to new coordaintes
35220139fca9SMatthew G. Knepley 
35230139fca9SMatthew G. Knepley    Calling sequence of func:
35240139fca9SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
35250139fca9SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
35260139fca9SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
35270139fca9SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
35280139fca9SMatthew G. Knepley 
35290139fca9SMatthew G. Knepley +  dim          - The spatial dimension
35300139fca9SMatthew G. Knepley .  Nf           - The number of input fields (here 1)
35310139fca9SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
35320139fca9SMatthew G. Knepley .  uOff         - The offset of the coordinates in u[] (here 0)
35330139fca9SMatthew G. Knepley .  uOff_x       - The offset of the coordinates in u_x[] (here 0)
35340139fca9SMatthew G. Knepley .  u            - The coordinate values at this point in space
35350139fca9SMatthew G. Knepley .  u_t          - The coordinate time derivative at this point in space (here NULL)
35360139fca9SMatthew G. Knepley .  u_x          - The coordinate derivatives at this point in space
35370139fca9SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
35380139fca9SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
35390139fca9SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
35400139fca9SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
35410139fca9SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
35420139fca9SMatthew G. Knepley .  t            - The current time
35430139fca9SMatthew G. Knepley .  x            - The coordinates of this point (here not used)
35440139fca9SMatthew G. Knepley .  numConstants - The number of constants
35450139fca9SMatthew G. Knepley .  constants    - The value of each constant
35460139fca9SMatthew G. Knepley -  f            - The new coordinates at this point in space
35470139fca9SMatthew G. Knepley 
35480139fca9SMatthew G. Knepley   Level: intermediate
35490139fca9SMatthew G. Knepley 
3550db781477SPatrick Sanan .seealso: `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMProjectFieldLocal()`, `DMProjectFieldLabelLocal()`
35510139fca9SMatthew G. Knepley @*/
3552d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexRemapGeometry(DM dm, PetscReal time, void (*func)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]))
3553d71ae5a4SJacob Faibussowitsch {
35540139fca9SMatthew G. Knepley   DM      cdm;
35558bf1a49fSMatthew G. Knepley   DMField cf;
35560139fca9SMatthew G. Knepley   Vec     lCoords, tmpCoords;
35570139fca9SMatthew G. Knepley 
35580139fca9SMatthew G. Knepley   PetscFunctionBegin;
35599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
35609566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
35619566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(cdm, &tmpCoords));
35629566063dSJacob Faibussowitsch   PetscCall(VecCopy(lCoords, tmpCoords));
35638bf1a49fSMatthew G. Knepley   /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */
35649566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateField(dm, &cf));
35656858538eSMatthew G. Knepley   cdm->coordinates[0].field = cf;
35669566063dSJacob Faibussowitsch   PetscCall(DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords));
35676858538eSMatthew G. Knepley   cdm->coordinates[0].field = NULL;
35689566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(cdm, &tmpCoords));
35699566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, lCoords));
35700139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
35710139fca9SMatthew G. Knepley }
35720139fca9SMatthew G. Knepley 
35730139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z,
35740139fca9SMatthew G. Knepley   / 1  0  m_0 \
35750139fca9SMatthew G. Knepley   | 0  1  m_1 |
35760139fca9SMatthew G. Knepley   \ 0  0   1  /
35770139fca9SMatthew G. Knepley */
3578d71ae5a4SJacob Faibussowitsch static void f0_shear(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar coords[])
3579d71ae5a4SJacob Faibussowitsch {
35800139fca9SMatthew G. Knepley   const PetscInt Nc = uOff[1] - uOff[0];
3581c1f1bd54SMatthew G. Knepley   const PetscInt ax = (PetscInt)PetscRealPart(constants[0]);
35820139fca9SMatthew G. Knepley   PetscInt       c;
35830139fca9SMatthew G. Knepley 
3584ad540459SPierre Jolivet   for (c = 0; c < Nc; ++c) coords[c] = u[c] + constants[c + 1] * u[ax];
35850139fca9SMatthew G. Knepley }
35860139fca9SMatthew G. Knepley 
35870139fca9SMatthew G. Knepley /*@C
35880139fca9SMatthew G. Knepley   DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates.
35890139fca9SMatthew G. Knepley 
35900139fca9SMatthew G. Knepley   Not collective
35910139fca9SMatthew G. Knepley 
35920139fca9SMatthew G. Knepley   Input Parameters:
35930139fca9SMatthew G. Knepley + dm          - The DM
35943ee9839eSMatthew G. Knepley . direction   - The shear coordinate direction, e.g. 0 is the x-axis
35950139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction
35960139fca9SMatthew G. Knepley 
35970139fca9SMatthew G. Knepley   Level: intermediate
35980139fca9SMatthew G. Knepley 
3599db781477SPatrick Sanan .seealso: `DMPlexRemapGeometry()`
36000139fca9SMatthew G. Knepley @*/
3601d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[])
3602d71ae5a4SJacob Faibussowitsch {
36030139fca9SMatthew G. Knepley   DM             cdm;
36040139fca9SMatthew G. Knepley   PetscDS        cds;
36050139fca9SMatthew G. Knepley   PetscObject    obj;
36060139fca9SMatthew G. Knepley   PetscClassId   id;
36070139fca9SMatthew G. Knepley   PetscScalar   *moduli;
36083ee9839eSMatthew G. Knepley   const PetscInt dir = (PetscInt)direction;
36090139fca9SMatthew G. Knepley   PetscInt       dE, d, e;
36100139fca9SMatthew G. Knepley 
36110139fca9SMatthew G. Knepley   PetscFunctionBegin;
36129566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
36139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
36149566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(dE + 1, &moduli));
36150139fca9SMatthew G. Knepley   moduli[0] = dir;
3616cdaaecf7SMatthew G. Knepley   for (d = 0, e = 0; d < dE; ++d) moduli[d + 1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0);
36179566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
36189566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
36199566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(obj, &id));
36200139fca9SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
36210139fca9SMatthew G. Knepley     Vec          lCoords;
36220139fca9SMatthew G. Knepley     PetscSection cSection;
36230139fca9SMatthew G. Knepley     PetscScalar *coords;
36240139fca9SMatthew G. Knepley     PetscInt     vStart, vEnd, v;
36250139fca9SMatthew G. Knepley 
36269566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
36279566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cSection));
36289566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
36299566063dSJacob Faibussowitsch     PetscCall(VecGetArray(lCoords, &coords));
36300139fca9SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
36310139fca9SMatthew G. Knepley       PetscReal ds;
36320139fca9SMatthew G. Knepley       PetscInt  off, c;
36330139fca9SMatthew G. Knepley 
36349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(cSection, v, &off));
36350139fca9SMatthew G. Knepley       ds = PetscRealPart(coords[off + dir]);
36360139fca9SMatthew G. Knepley       for (c = 0; c < dE; ++c) coords[off + c] += moduli[c] * ds;
36370139fca9SMatthew G. Knepley     }
36389566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(lCoords, &coords));
36390139fca9SMatthew G. Knepley   } else {
36409566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, dE + 1, moduli));
36419566063dSJacob Faibussowitsch     PetscCall(DMPlexRemapGeometry(dm, 0.0, f0_shear));
36420139fca9SMatthew G. Knepley   }
36439566063dSJacob Faibussowitsch   PetscCall(PetscFree(moduli));
36440139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
36450139fca9SMatthew G. Knepley }
3646