xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 20f4b53cbb5e9bd9ef12b76a8697d60d197cda17) !
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 
9*20f4b53cSBarry Smith   Not Collective (provided `DMGetCoordinatesLocalSetUp()` has been already called)
103985bb02SVaclav Hapla 
113985bb02SVaclav Hapla   Input Parameters:
12*20f4b53cSBarry Smith + dm - The `DMPLEX` object
13*20f4b53cSBarry Smith . coordinates - The `Vec` of coordinates of the sought points
14*20f4b53cSBarry Smith - eps - The tolerance or `PETSC_DEFAULT`
153985bb02SVaclav Hapla 
163985bb02SVaclav Hapla   Output Parameters:
17*20f4b53cSBarry Smith . points - The `IS` of found DAG points or -1
183985bb02SVaclav Hapla 
193985bb02SVaclav Hapla   Level: intermediate
203985bb02SVaclav Hapla 
213985bb02SVaclav Hapla   Notes:
22*20f4b53cSBarry Smith   The length of `Vec` coordinates must be npoints * dim where dim is the spatial dimension returned by `DMGetCoordinateDim()` and npoints is the number of sought points.
233985bb02SVaclav Hapla 
24*20f4b53cSBarry Smith   The output `IS` is living on `PETSC_COMM_SELF` and its length is npoints.
25d3e1f4ccSVaclav Hapla   Each rank does the search independently.
26*20f4b53cSBarry Smith   If this rank's local `DMPLEX` portion contains the DAG point corresponding to the i-th tuple of coordinates, the i-th entry of the output `IS` is set to that DAG point, otherwise to -1.
273985bb02SVaclav Hapla 
28*20f4b53cSBarry Smith   The output `IS` must be destroyed by user.
293985bb02SVaclav Hapla 
303985bb02SVaclav Hapla   The tolerance is interpreted as the maximum Euclidean (L2) distance of the sought point from the specified coordinates.
313985bb02SVaclav Hapla 
32d3e1f4ccSVaclav Hapla   Complexity of this function is currently O(mn) with m number of vertices to find and n number of vertices in the local mesh. This could probably be improved if needed.
33335ef845SVaclav Hapla 
34*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexCreate()`, `DMGetCoordinatesLocal()`
353985bb02SVaclav Hapla @*/
36d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points)
37d71ae5a4SJacob Faibussowitsch {
3837900f7dSMatthew G. Knepley   PetscInt           c, cdim, i, j, o, p, vStart, vEnd;
39d3e1f4ccSVaclav Hapla   PetscInt           npoints;
40d3e1f4ccSVaclav Hapla   const PetscScalar *coord;
413985bb02SVaclav Hapla   Vec                allCoordsVec;
423985bb02SVaclav Hapla   const PetscScalar *allCoords;
43d3e1f4ccSVaclav Hapla   PetscInt          *dagPoints;
443985bb02SVaclav Hapla 
453985bb02SVaclav Hapla   PetscFunctionBegin;
463985bb02SVaclav Hapla   if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON;
479566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
48d3e1f4ccSVaclav Hapla   {
49d3e1f4ccSVaclav Hapla     PetscInt n;
50d3e1f4ccSVaclav Hapla 
519566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(coordinates, &n));
5263a3b9bcSJacob Faibussowitsch     PetscCheck(n % cdim == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Given coordinates Vec has local length %" PetscInt_FMT " not divisible by coordinate dimension %" PetscInt_FMT " of given DM", n, cdim);
53d3e1f4ccSVaclav Hapla     npoints = n / cdim;
54d3e1f4ccSVaclav Hapla   }
559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &allCoordsVec));
569566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(allCoordsVec, &allCoords));
579566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coord));
589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
5976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
60335ef845SVaclav Hapla     /* check coordinate section is consistent with DM dimension */
61335ef845SVaclav Hapla     PetscSection cs;
62335ef845SVaclav Hapla     PetscInt     ndof;
63335ef845SVaclav Hapla 
649566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cs));
653985bb02SVaclav Hapla     for (p = vStart; p < vEnd; p++) {
669566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cs, p, &ndof));
6763a3b9bcSJacob Faibussowitsch       PetscCheck(ndof == cdim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %" PetscInt_FMT ": ndof = %" PetscInt_FMT " != %" PetscInt_FMT " = cdim", p, ndof, cdim);
68335ef845SVaclav Hapla     }
69335ef845SVaclav Hapla   }
709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npoints, &dagPoints));
71eca9f518SVaclav Hapla   if (eps == 0.0) {
7237900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
73eca9f518SVaclav Hapla       dagPoints[i] = -1;
7437900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
7537900f7dSMatthew G. Knepley         for (c = 0; c < cdim; c++) {
76d3e1f4ccSVaclav Hapla           if (coord[j + c] != allCoords[o + c]) break;
77eca9f518SVaclav Hapla         }
7837900f7dSMatthew G. Knepley         if (c == cdim) {
79eca9f518SVaclav Hapla           dagPoints[i] = p;
80eca9f518SVaclav Hapla           break;
81eca9f518SVaclav Hapla         }
82eca9f518SVaclav Hapla       }
83eca9f518SVaclav Hapla     }
84d3e1f4ccSVaclav Hapla   } else {
8537900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
86d3e1f4ccSVaclav Hapla       PetscReal norm;
87d3e1f4ccSVaclav Hapla 
88335ef845SVaclav Hapla       dagPoints[i] = -1;
8937900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
903985bb02SVaclav Hapla         norm = 0.0;
91ad540459SPierre Jolivet         for (c = 0; c < cdim; c++) norm += PetscRealPart(PetscSqr(coord[j + c] - allCoords[o + c]));
923985bb02SVaclav Hapla         norm = PetscSqrtReal(norm);
933985bb02SVaclav Hapla         if (norm <= eps) {
943985bb02SVaclav Hapla           dagPoints[i] = p;
953985bb02SVaclav Hapla           break;
963985bb02SVaclav Hapla         }
973985bb02SVaclav Hapla       }
983985bb02SVaclav Hapla     }
99d3e1f4ccSVaclav Hapla   }
1009566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(allCoordsVec, &allCoords));
1019566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coord));
1029566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points));
1033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1043985bb02SVaclav Hapla }
1053985bb02SVaclav Hapla 
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   }
1373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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   }
1983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
2143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
2333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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];
2583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
2883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
3093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
3563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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]);
3663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
3743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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   }
3863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
387c4eade1cSMatthew G. Knepley }
388c4eade1cSMatthew G. Knepley 
38962a38674SMatthew G. Knepley /*
39062a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
39162a38674SMatthew G. Knepley 
392*20f4b53cSBarry Smith   Not Collective
39362a38674SMatthew G. Knepley 
39462a38674SMatthew G. Knepley   Input Parameters:
39562a38674SMatthew G. Knepley + box - The grid hash object
396*20f4b53cSBarry Smith . n   - The number of boxes in each dimension, or `PETSC_DETERMINE`
397*20f4b53cSBarry Smith - 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   }
4183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
419c4eade1cSMatthew G. Knepley }
420c4eade1cSMatthew G. Knepley 
42162a38674SMatthew G. Knepley /*
42262a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
42362a38674SMatthew G. Knepley 
424*20f4b53cSBarry Smith   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   }
4643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
465c4eade1cSMatthew G. Knepley }
466c4eade1cSMatthew G. Knepley 
467af74b616SDave May /*
468af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
469af74b616SDave May 
470*20f4b53cSBarry Smith  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:
479*20f4b53cSBarry Smith + dboxes - An array of `numPoints`*`dim` integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
480*20f4b53cSBarry Smith . 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:
486*20f4b53cSBarry Smith   This does an additional check that a cell actually contains the point, and found is `PETSC_FALSE` if no cell does. Thus, this function requires that `cellSection` is already constructed.
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;
5083ba16761SJacob Faibussowitsch       if (dbox < 0 || dbox >= n[d]) PetscFunctionReturn(PETSC_SUCCESS);
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
5143ba16761SJacob Faibussowitsch     if (boxes[p] < bStart || boxes[p] >= bEnd) PetscFunctionReturn(PETSC_SUCCESS);
515af74b616SDave May   }
516af74b616SDave May   *found = PETSC_TRUE;
5173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
5293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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   }
5573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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   }
5843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58562a38674SMatthew G. Knepley }
58662a38674SMatthew G. Knepley 
58762a38674SMatthew G. Knepley /*
588*20f4b53cSBarry Smith   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the `DMPLEX`
58962a38674SMatthew G. Knepley 
590*20f4b53cSBarry Smith   Collective
59162a38674SMatthew G. Knepley 
59262a38674SMatthew G. Knepley   Input Parameter:
593*20f4b53cSBarry Smith . dm - The `DMPLEX`
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 
600*20f4b53cSBarry Smith .seealso: `DMPLEX`, `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;
60796217254SMatthew 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;
61496217254SMatthew G. Knepley   const PetscInt    *leaves;
615ddce0771SMatthew G. Knepley   PetscInt           n[3] = {2, 2, 2};
61696217254SMatthew 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));
65696217254SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &sf));
65796217254SMatthew G. Knepley   if (sf) PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
65896217254SMatthew 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;
66596217254SMatthew 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 
66996217254SMatthew G. Knepley     PetscCall(PetscFindInt(c, Nl, leaves, &idx));
67096217254SMatthew 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)
6909d67f9e5SMatthew G. Knepley         PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " has vertex (%g, %g, %g) in box %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ")\n", c, (double)PetscRealPart(ccoords[e * dim + 0]), dim > 1 ? (double)PetscRealPart(ccoords[e * dim + 1]) : 0., dim > 2 ? (double)PetscRealPart(ccoords[e * dim + 2]) : 0., 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;
7963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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) {
85496217254SMatthew 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));
9943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
1000*20f4b53cSBarry Smith   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;
10253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
1031*20f4b53cSBarry Smith   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 
1039*20f4b53cSBarry Smith   Note:
1040*20f4b53cSBarry Smith   This uses the basis completion described by Frisvad in http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html, DOI:10.1080/2165347X.2012.689606
1041741bfc07SMatthew G. Knepley 
1042741bfc07SMatthew G. Knepley   Level: developer
1043741bfc07SMatthew G. Knepley 
1044db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto2D()`
1045741bfc07SMatthew G. Knepley @*/
1046d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
1047d71ae5a4SJacob Faibussowitsch {
104828dbe442SToby Isaac   PetscReal x    = PetscRealPart(coords[3] - coords[0]);
104928dbe442SToby Isaac   PetscReal y    = PetscRealPart(coords[4] - coords[1]);
105028dbe442SToby Isaac   PetscReal z    = PetscRealPart(coords[5] - coords[2]);
105128dbe442SToby Isaac   PetscReal r    = PetscSqrtReal(x * x + y * y + z * z);
105228dbe442SToby Isaac   PetscReal rinv = 1. / r;
105328dbe442SToby Isaac   PetscFunctionBegin;
105428dbe442SToby Isaac 
10559371c9d4SSatish Balay   x *= rinv;
10569371c9d4SSatish Balay   y *= rinv;
10579371c9d4SSatish Balay   z *= rinv;
105828dbe442SToby Isaac   if (x > 0.) {
105928dbe442SToby Isaac     PetscReal inv1pX = 1. / (1. + x);
106028dbe442SToby Isaac 
10619371c9d4SSatish Balay     R[0] = x;
10629371c9d4SSatish Balay     R[1] = -y;
10639371c9d4SSatish Balay     R[2] = -z;
10649371c9d4SSatish Balay     R[3] = y;
10659371c9d4SSatish Balay     R[4] = 1. - y * y * inv1pX;
10669371c9d4SSatish Balay     R[5] = -y * z * inv1pX;
10679371c9d4SSatish Balay     R[6] = z;
10689371c9d4SSatish Balay     R[7] = -y * z * inv1pX;
10699371c9d4SSatish Balay     R[8] = 1. - z * z * inv1pX;
10709371c9d4SSatish Balay   } else {
107128dbe442SToby Isaac     PetscReal inv1mX = 1. / (1. - x);
107228dbe442SToby Isaac 
10739371c9d4SSatish Balay     R[0] = x;
10749371c9d4SSatish Balay     R[1] = z;
10759371c9d4SSatish Balay     R[2] = y;
10769371c9d4SSatish Balay     R[3] = y;
10779371c9d4SSatish Balay     R[4] = -y * z * inv1mX;
10789371c9d4SSatish Balay     R[5] = 1. - y * y * inv1mX;
10799371c9d4SSatish Balay     R[6] = z;
10809371c9d4SSatish Balay     R[7] = 1. - z * z * inv1mX;
10819371c9d4SSatish Balay     R[8] = -y * z * inv1mX;
108228dbe442SToby Isaac   }
108328dbe442SToby Isaac   coords[0] = 0.0;
108428dbe442SToby Isaac   coords[1] = r;
10853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
108628dbe442SToby Isaac }
108728dbe442SToby Isaac 
1088741bfc07SMatthew G. Knepley /*@
1089c871b86eSJed Brown   DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the
1090c871b86eSJed Brown     plane.  The normal is defined by positive orientation of the first 3 points.
1091741bfc07SMatthew G. Knepley 
1092*20f4b53cSBarry Smith   Not Collective
1093741bfc07SMatthew G. Knepley 
1094741bfc07SMatthew G. Knepley   Input Parameter:
10956b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points)
1096741bfc07SMatthew G. Knepley 
10976b867d5aSJose E. Roman   Input/Output Parameter:
10986b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first
10996b867d5aSJose E. Roman            2*coordSize/3 entries contain interlaced 2D points, with the rest undefined
11006b867d5aSJose E. Roman 
11016b867d5aSJose E. Roman   Output Parameter:
11026b867d5aSJose 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.
1103741bfc07SMatthew G. Knepley 
1104741bfc07SMatthew G. Knepley   Level: developer
1105741bfc07SMatthew G. Knepley 
1106db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto1D()`
1107741bfc07SMatthew G. Knepley @*/
1108d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
1109d71ae5a4SJacob Faibussowitsch {
1110c871b86eSJed Brown   PetscReal      x1[3], x2[3], n[3], c[3], norm;
1111ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1112c871b86eSJed Brown   PetscInt       d, p;
1113ccd2543fSMatthew G Knepley 
1114ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1115ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
1116ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
11171ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1 * dim + d] - coords[0 * dim + d]);
11181ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2 * dim + d] - coords[0 * dim + d]);
1119ccd2543fSMatthew G Knepley   }
1120c871b86eSJed Brown   // n = x1 \otimes x2
1121ccd2543fSMatthew G Knepley   n[0] = x1[1] * x2[2] - x1[2] * x2[1];
1122ccd2543fSMatthew G Knepley   n[1] = x1[2] * x2[0] - x1[0] * x2[2];
1123ccd2543fSMatthew G Knepley   n[2] = x1[0] * x2[1] - x1[1] * x2[0];
11248b49ba18SBarry Smith   norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
1125c871b86eSJed Brown   for (d = 0; d < dim; d++) n[d] /= norm;
1126c871b86eSJed Brown   norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]);
1127c871b86eSJed Brown   for (d = 0; d < dim; d++) x1[d] /= norm;
1128c871b86eSJed Brown   // x2 = n \otimes x1
1129c871b86eSJed Brown   x2[0] = n[1] * x1[2] - n[2] * x1[1];
1130c871b86eSJed Brown   x2[1] = n[2] * x1[0] - n[0] * x1[2];
1131c871b86eSJed Brown   x2[2] = n[0] * x1[1] - n[1] * x1[0];
1132c871b86eSJed Brown   for (d = 0; d < dim; d++) {
1133c871b86eSJed Brown     R[d * dim + 0] = x1[d];
1134c871b86eSJed Brown     R[d * dim + 1] = x2[d];
1135c871b86eSJed Brown     R[d * dim + 2] = n[d];
1136c871b86eSJed Brown     c[d]           = PetscRealPart(coords[0 * dim + d]);
113773868372SMatthew G. Knepley   }
1138c871b86eSJed Brown   for (p = 0; p < coordSize / dim; p++) {
1139c871b86eSJed Brown     PetscReal y[3];
1140c871b86eSJed Brown     for (d = 0; d < dim; d++) y[d] = PetscRealPart(coords[p * dim + d]) - c[d];
1141c871b86eSJed 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];
11427f07f362SMatthew G. Knepley   }
11433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1144ccd2543fSMatthew G Knepley }
1145ccd2543fSMatthew G Knepley 
1146d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
1147d71ae5a4SJacob Faibussowitsch {
1148834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
1149834e62ceSMatthew G. Knepley 
1150834e62ceSMatthew G. Knepley    |  1  1  1 |
1151834e62ceSMatthew G. Knepley    | x0 x1 x2 |
1152834e62ceSMatthew G. Knepley    | y0 y1 y2 |
1153834e62ceSMatthew G. Knepley 
1154834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
1155834e62ceSMatthew G. Knepley 
1156834e62ceSMatthew G. Knepley    | x1 x2 |
1157834e62ceSMatthew G. Knepley    | y1 y2 |
1158834e62ceSMatthew G. Knepley   */
1159834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
1160834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
1161834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
11629371c9d4SSatish Balay   M[0] = x1;
11639371c9d4SSatish Balay   M[1] = x2;
11649371c9d4SSatish Balay   M[2] = y1;
11659371c9d4SSatish Balay   M[3] = y2;
1166923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
1167834e62ceSMatthew G. Knepley   *vol = 0.5 * detM;
11683bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
1169834e62ceSMatthew G. Knepley }
1170834e62ceSMatthew G. Knepley 
1171d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
1172d71ae5a4SJacob Faibussowitsch {
1173834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
1174834e62ceSMatthew G. Knepley 
1175834e62ceSMatthew G. Knepley    |  1  1  1  1 |
1176834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
1177834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
1178834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
1179834e62ceSMatthew G. Knepley 
1180834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
1181834e62ceSMatthew G. Knepley 
1182834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1183834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1184834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1185834e62ceSMatthew G. Knepley   */
1186834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2];
1187834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2];
1188834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
11890a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1190834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
11919371c9d4SSatish Balay   M[0] = x1;
11929371c9d4SSatish Balay   M[1] = x2;
11939371c9d4SSatish Balay   M[2] = x3;
11949371c9d4SSatish Balay   M[3] = y1;
11959371c9d4SSatish Balay   M[4] = y2;
11969371c9d4SSatish Balay   M[5] = y3;
11979371c9d4SSatish Balay   M[6] = z1;
11989371c9d4SSatish Balay   M[7] = z2;
11999371c9d4SSatish Balay   M[8] = z3;
1200923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
12010a3da2c2SToby Isaac   *vol = -onesixth * detM;
12023bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1203834e62ceSMatthew G. Knepley }
1204834e62ceSMatthew G. Knepley 
1205d71ae5a4SJacob Faibussowitsch static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
1206d71ae5a4SJacob Faibussowitsch {
12070a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1208923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
12090a3da2c2SToby Isaac   *vol *= -onesixth;
12100ec8681fSMatthew G. Knepley }
12110ec8681fSMatthew G. Knepley 
1212d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1213d71ae5a4SJacob Faibussowitsch {
1214cb92db44SToby Isaac   PetscSection       coordSection;
1215cb92db44SToby Isaac   Vec                coordinates;
1216cb92db44SToby Isaac   const PetscScalar *coords;
1217cb92db44SToby Isaac   PetscInt           dim, d, off;
1218cb92db44SToby Isaac 
1219cb92db44SToby Isaac   PetscFunctionBegin;
12209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
12219566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
12229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(coordSection, e, &dim));
12233ba16761SJacob Faibussowitsch   if (!dim) PetscFunctionReturn(PETSC_SUCCESS);
12249566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(coordSection, e, &off));
12259566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
12269371c9d4SSatish Balay   if (v0) {
12279371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);
12289371c9d4SSatish Balay   }
12299566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
1230cb92db44SToby Isaac   *detJ = 1.;
1231cb92db44SToby Isaac   if (J) {
1232cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1233cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1234cb92db44SToby Isaac     if (invJ) {
1235cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1236cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1237cb92db44SToby Isaac     }
1238cb92db44SToby Isaac   }
12393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1240cb92db44SToby Isaac }
1241cb92db44SToby Isaac 
12426858538eSMatthew G. Knepley /*@C
12436858538eSMatthew G. Knepley   DMPlexGetCellCoordinates - Get coordinates for a cell, taking into account periodicity
12446858538eSMatthew G. Knepley 
1245*20f4b53cSBarry Smith   Not Collective
12466858538eSMatthew G. Knepley 
12476858538eSMatthew G. Knepley   Input Parameters:
1248*20f4b53cSBarry Smith + dm   - The `DMPLEX`
12496858538eSMatthew G. Knepley - cell - The cell number
12506858538eSMatthew G. Knepley 
12516858538eSMatthew G. Knepley   Output Parameters:
12526858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
12536858538eSMatthew G. Knepley . Nc     - The number of coordinates
12546858538eSMatthew G. Knepley . array  - The coordinate array
12556858538eSMatthew G. Knepley - coords - The cell coordinates
12566858538eSMatthew G. Knepley 
12576858538eSMatthew G. Knepley   Level: developer
12586858538eSMatthew G. Knepley 
1259*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexRestoreCellCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCellCoordinatesLocal()`
12606858538eSMatthew G. Knepley @*/
1261d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[])
1262d71ae5a4SJacob Faibussowitsch {
12636858538eSMatthew G. Knepley   DM                 cdm;
12646858538eSMatthew G. Knepley   Vec                coordinates;
12656858538eSMatthew G. Knepley   PetscSection       cs;
12666858538eSMatthew G. Knepley   const PetscScalar *ccoords;
12676858538eSMatthew G. Knepley   PetscInt           pStart, pEnd;
12686858538eSMatthew G. Knepley 
12696858538eSMatthew G. Knepley   PetscFunctionBeginHot;
12706858538eSMatthew G. Knepley   *isDG   = PETSC_FALSE;
12716858538eSMatthew G. Knepley   *Nc     = 0;
12726858538eSMatthew G. Knepley   *array  = NULL;
12736858538eSMatthew G. Knepley   *coords = NULL;
12746858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12756858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateSection(dm, &cs));
12766858538eSMatthew G. Knepley   if (!cs) goto cg;
12776858538eSMatthew G. Knepley   /* Check that the cell exists in the cellwise section */
12786858538eSMatthew G. Knepley   PetscCall(PetscSectionGetChart(cs, &pStart, &pEnd));
12796858538eSMatthew G. Knepley   if (cell < pStart || cell >= pEnd) goto cg;
12806858538eSMatthew G. Knepley   /* Check for cellwise coordinates for this cell */
12816858538eSMatthew G. Knepley   PetscCall(PetscSectionGetDof(cs, cell, Nc));
12826858538eSMatthew G. Knepley   if (!*Nc) goto cg;
12836858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12846858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocalNoncollective(dm, &coordinates));
12856858538eSMatthew G. Knepley   if (!coordinates) goto cg;
12866858538eSMatthew G. Knepley   /* Get cellwise coordinates */
12876858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dm, &cdm));
12886858538eSMatthew G. Knepley   PetscCall(VecGetArrayRead(coordinates, array));
12896858538eSMatthew G. Knepley   PetscCall(DMPlexPointLocalRead(cdm, cell, *array, &ccoords));
12906858538eSMatthew G. Knepley   PetscCall(DMGetWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
12916858538eSMatthew G. Knepley   PetscCall(PetscArraycpy(*coords, ccoords, *Nc));
12926858538eSMatthew G. Knepley   PetscCall(VecRestoreArrayRead(coordinates, array));
12936858538eSMatthew G. Knepley   *isDG = PETSC_TRUE;
12943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12956858538eSMatthew G. Knepley cg:
12966858538eSMatthew G. Knepley   /* Use continuous coordinates */
12976858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
12986858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &cs));
12996858538eSMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
13006858538eSMatthew G. Knepley   PetscCall(DMPlexVecGetClosure(cdm, cs, coordinates, cell, Nc, coords));
13013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13026858538eSMatthew G. Knepley }
13036858538eSMatthew G. Knepley 
13046858538eSMatthew G. Knepley /*@C
13056858538eSMatthew G. Knepley   DMPlexRestoreCellCoordinates - Get coordinates for a cell, taking into account periodicity
13066858538eSMatthew G. Knepley 
1307*20f4b53cSBarry Smith   Not Collective
13086858538eSMatthew G. Knepley 
13096858538eSMatthew G. Knepley   Input Parameters:
1310*20f4b53cSBarry Smith + dm   - The `DMPLEX`
13116858538eSMatthew G. Knepley - cell - The cell number
13126858538eSMatthew G. Knepley 
13136858538eSMatthew G. Knepley   Output Parameters:
13146858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
13156858538eSMatthew G. Knepley . Nc     - The number of coordinates
13166858538eSMatthew G. Knepley . array  - The coordinate array
13176858538eSMatthew G. Knepley - coords - The cell coordinates
13186858538eSMatthew G. Knepley 
13196858538eSMatthew G. Knepley   Level: developer
13206858538eSMatthew G. Knepley 
1321*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexGetCellCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCellCoordinatesLocal()`
13226858538eSMatthew G. Knepley @*/
1323d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexRestoreCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[])
1324d71ae5a4SJacob Faibussowitsch {
13256858538eSMatthew G. Knepley   DM           cdm;
13266858538eSMatthew G. Knepley   PetscSection cs;
13276858538eSMatthew G. Knepley   Vec          coordinates;
13286858538eSMatthew G. Knepley 
13296858538eSMatthew G. Knepley   PetscFunctionBeginHot;
13306858538eSMatthew G. Knepley   if (*isDG) {
13316858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinateDM(dm, &cdm));
13326858538eSMatthew G. Knepley     PetscCall(DMRestoreWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
13336858538eSMatthew G. Knepley   } else {
13346858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
13356858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateSection(dm, &cs));
13366858538eSMatthew G. Knepley     PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
13376858538eSMatthew G. Knepley     PetscCall(DMPlexVecRestoreClosure(cdm, cs, coordinates, cell, Nc, (PetscScalar **)coords));
13386858538eSMatthew G. Knepley   }
13393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13406858538eSMatthew G. Knepley }
13416858538eSMatthew G. Knepley 
1342d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1343d71ae5a4SJacob Faibussowitsch {
13446858538eSMatthew G. Knepley   const PetscScalar *array;
1345a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
13466858538eSMatthew G. Knepley   PetscInt           numCoords, d;
13476858538eSMatthew G. Knepley   PetscBool          isDG;
134817fe8556SMatthew G. Knepley 
134917fe8556SMatthew G. Knepley   PetscFunctionBegin;
13506858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
135108401ef6SPierre Jolivet   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
13527f07f362SMatthew G. Knepley   *detJ = 0.0;
135328dbe442SToby Isaac   if (numCoords == 6) {
135428dbe442SToby Isaac     const PetscInt dim = 3;
135528dbe442SToby Isaac     PetscReal      R[9], J0;
135628dbe442SToby Isaac 
13579371c9d4SSatish Balay     if (v0) {
13589371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13599371c9d4SSatish Balay     }
13609566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto1D(coords, R));
136128dbe442SToby Isaac     if (J) {
136228dbe442SToby Isaac       J0   = 0.5 * PetscRealPart(coords[1]);
13639371c9d4SSatish Balay       J[0] = R[0] * J0;
13649371c9d4SSatish Balay       J[1] = R[1];
13659371c9d4SSatish Balay       J[2] = R[2];
13669371c9d4SSatish Balay       J[3] = R[3] * J0;
13679371c9d4SSatish Balay       J[4] = R[4];
13689371c9d4SSatish Balay       J[5] = R[5];
13699371c9d4SSatish Balay       J[6] = R[6] * J0;
13709371c9d4SSatish Balay       J[7] = R[7];
13719371c9d4SSatish Balay       J[8] = R[8];
137228dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
1373ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
1374adac9986SMatthew G. Knepley     }
137528dbe442SToby Isaac   } else if (numCoords == 4) {
13767f07f362SMatthew G. Knepley     const PetscInt dim = 2;
13777f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
13787f07f362SMatthew G. Knepley 
13799371c9d4SSatish Balay     if (v0) {
13809371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13819371c9d4SSatish Balay     }
13829566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection2Dto1D(coords, R));
138317fe8556SMatthew G. Knepley     if (J) {
13847f07f362SMatthew G. Knepley       J0   = 0.5 * PetscRealPart(coords[1]);
13859371c9d4SSatish Balay       J[0] = R[0] * J0;
13869371c9d4SSatish Balay       J[1] = R[1];
13879371c9d4SSatish Balay       J[2] = R[2] * J0;
13889371c9d4SSatish Balay       J[3] = R[3];
1389923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1390ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
1391adac9986SMatthew G. Knepley     }
13927f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
13937f07f362SMatthew G. Knepley     const PetscInt dim = 1;
13947f07f362SMatthew G. Knepley 
13959371c9d4SSatish Balay     if (v0) {
13969371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13979371c9d4SSatish Balay     }
13987f07f362SMatthew G. Knepley     if (J) {
13997f07f362SMatthew G. Knepley       J[0]  = 0.5 * (PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
140017fe8556SMatthew G. Knepley       *detJ = J[0];
14019566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(2.0));
14029371c9d4SSatish Balay       if (invJ) {
14039371c9d4SSatish Balay         invJ[0] = 1.0 / J[0];
14049371c9d4SSatish Balay         PetscCall(PetscLogFlops(1.0));
14059371c9d4SSatish Balay       }
1406adac9986SMatthew G. Knepley     }
14076858538eSMatthew 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);
14086858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
141017fe8556SMatthew G. Knepley }
141117fe8556SMatthew G. Knepley 
1412d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1413d71ae5a4SJacob Faibussowitsch {
14146858538eSMatthew G. Knepley   const PetscScalar *array;
1415a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
14166858538eSMatthew G. Knepley   PetscInt           numCoords, d;
14176858538eSMatthew G. Knepley   PetscBool          isDG;
1418ccd2543fSMatthew G Knepley 
1419ccd2543fSMatthew G Knepley   PetscFunctionBegin;
14206858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14216858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
14227f07f362SMatthew G. Knepley   *detJ = 0.0;
1423ccd2543fSMatthew G Knepley   if (numCoords == 9) {
14247f07f362SMatthew G. Knepley     const PetscInt dim = 3;
14257f07f362SMatthew 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};
14267f07f362SMatthew G. Knepley 
14279371c9d4SSatish Balay     if (v0) {
14289371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
14299371c9d4SSatish Balay     }
14309566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
14317f07f362SMatthew G. Knepley     if (J) {
1432b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1433b7ad821dSMatthew G. Knepley 
1434b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1435ad540459SPierre 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]));
14367f07f362SMatthew G. Knepley       }
14379566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1438923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
14397f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
14406858538eSMatthew G. Knepley         for (PetscInt f = 0; f < dim; f++) {
14417f07f362SMatthew G. Knepley           J[d * dim + f] = 0.0;
1442ad540459SPierre Jolivet           for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f];
14437f07f362SMatthew G. Knepley         }
14447f07f362SMatthew G. Knepley       }
14459566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
14467f07f362SMatthew G. Knepley     }
1447ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
14487f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
14497f07f362SMatthew G. Knepley     const PetscInt dim = 2;
14507f07f362SMatthew G. Knepley 
14519371c9d4SSatish Balay     if (v0) {
14529371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
14539371c9d4SSatish Balay     }
1454ccd2543fSMatthew G Knepley     if (J) {
1455ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1456ad540459SPierre 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]));
1457ccd2543fSMatthew G Knepley       }
14589566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1459923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1460ccd2543fSMatthew G Knepley     }
1461ad540459SPierre Jolivet     if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
146263a3b9bcSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %" PetscInt_FMT " != 6 or 9", numCoords);
14636858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1465ccd2543fSMatthew G Knepley }
1466ccd2543fSMatthew G Knepley 
1467d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1468d71ae5a4SJacob Faibussowitsch {
14696858538eSMatthew G. Knepley   const PetscScalar *array;
1470a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
14716858538eSMatthew G. Knepley   PetscInt           numCoords, d;
14726858538eSMatthew G. Knepley   PetscBool          isDG;
1473ccd2543fSMatthew G Knepley 
1474ccd2543fSMatthew G Knepley   PetscFunctionBegin;
14756858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14766858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1477dfccc68fSToby Isaac   if (!Nq) {
1478412e9a14SMatthew G. Knepley     PetscInt vorder[4] = {0, 1, 2, 3};
1479412e9a14SMatthew G. Knepley 
14809371c9d4SSatish Balay     if (isTensor) {
14819371c9d4SSatish Balay       vorder[2] = 3;
14829371c9d4SSatish Balay       vorder[3] = 2;
14839371c9d4SSatish Balay     }
14847f07f362SMatthew G. Knepley     *detJ = 0.0;
148599dec3a6SMatthew G. Knepley     if (numCoords == 12) {
148699dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
148799dec3a6SMatthew 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};
148899dec3a6SMatthew G. Knepley 
14899371c9d4SSatish Balay       if (v) {
14909371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
14919371c9d4SSatish Balay       }
14929566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
149399dec3a6SMatthew G. Knepley       if (J) {
149499dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
149599dec3a6SMatthew G. Knepley 
149699dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
1497412e9a14SMatthew G. Knepley           J0[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * pdim + d]) - PetscRealPart(coords[vorder[0] * pdim + d]));
1498412e9a14SMatthew G. Knepley           J0[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[2] * pdim + d]) - PetscRealPart(coords[vorder[1] * pdim + d]));
149999dec3a6SMatthew G. Knepley         }
15009566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1501923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
150299dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
15036858538eSMatthew G. Knepley           for (PetscInt f = 0; f < dim; f++) {
150499dec3a6SMatthew G. Knepley             J[d * dim + f] = 0.0;
1505ad540459SPierre Jolivet             for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f];
150699dec3a6SMatthew G. Knepley           }
150799dec3a6SMatthew G. Knepley         }
15089566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(18.0));
150999dec3a6SMatthew G. Knepley       }
1510ad540459SPierre Jolivet       if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
151171f58de1SToby Isaac     } else if (numCoords == 8) {
151299dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
151399dec3a6SMatthew G. Knepley 
15149371c9d4SSatish Balay       if (v) {
15159371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
15169371c9d4SSatish Balay       }
1517ccd2543fSMatthew G Knepley       if (J) {
1518ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
1519412e9a14SMatthew G. Knepley           J[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1520412e9a14SMatthew G. Knepley           J[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[3] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1521ccd2543fSMatthew G Knepley         }
15229566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1523923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1524ccd2543fSMatthew G Knepley       }
1525ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
152663a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1527dfccc68fSToby Isaac   } else {
1528dfccc68fSToby Isaac     const PetscInt Nv         = 4;
1529dfccc68fSToby Isaac     const PetscInt dimR       = 2;
1530412e9a14SMatthew G. Knepley     PetscInt       zToPlex[4] = {0, 1, 3, 2};
1531dfccc68fSToby Isaac     PetscReal      zOrder[12];
1532dfccc68fSToby Isaac     PetscReal      zCoeff[12];
1533dfccc68fSToby Isaac     PetscInt       i, j, k, l, dim;
1534dfccc68fSToby Isaac 
15359371c9d4SSatish Balay     if (isTensor) {
15369371c9d4SSatish Balay       zToPlex[2] = 2;
15379371c9d4SSatish Balay       zToPlex[3] = 3;
15389371c9d4SSatish Balay     }
1539dfccc68fSToby Isaac     if (numCoords == 12) {
1540dfccc68fSToby Isaac       dim = 3;
1541dfccc68fSToby Isaac     } else if (numCoords == 8) {
1542dfccc68fSToby Isaac       dim = 2;
154363a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1544dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1545dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1546dfccc68fSToby Isaac 
1547ad540459SPierre Jolivet       for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1548dfccc68fSToby Isaac     }
1549dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
15502df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta):
15512df84da0SMatthew G. Knepley            \phi^0 = (1 - xi - eta + xi eta) --> 1      = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3)
15522df84da0SMatthew G. Knepley            \phi^1 = (1 + xi - eta - xi eta) --> xi     = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3)
15532df84da0SMatthew G. Knepley            \phi^2 = (1 - xi + eta - xi eta) --> eta    = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3)
15542df84da0SMatthew G. Knepley            \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3)
15552df84da0SMatthew G. Knepley       */
1556dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1557dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1558dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1559dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1560dfccc68fSToby Isaac     }
1561dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1562dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1563dfccc68fSToby Isaac 
1564dfccc68fSToby Isaac       if (v) {
1565dfccc68fSToby Isaac         PetscReal extPoint[4];
1566dfccc68fSToby Isaac 
1567dfccc68fSToby Isaac         extPoint[0] = 1.;
1568dfccc68fSToby Isaac         extPoint[1] = xi;
1569dfccc68fSToby Isaac         extPoint[2] = eta;
1570dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1571dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1572dfccc68fSToby Isaac           PetscReal val = 0.;
1573dfccc68fSToby Isaac 
1574ad540459SPierre Jolivet           for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j];
1575dfccc68fSToby Isaac           v[i * dim + j] = val;
1576dfccc68fSToby Isaac         }
1577dfccc68fSToby Isaac       }
1578dfccc68fSToby Isaac       if (J) {
1579dfccc68fSToby Isaac         PetscReal extJ[8];
1580dfccc68fSToby Isaac 
1581dfccc68fSToby Isaac         extJ[0] = 0.;
1582dfccc68fSToby Isaac         extJ[1] = 0.;
1583dfccc68fSToby Isaac         extJ[2] = 1.;
1584dfccc68fSToby Isaac         extJ[3] = 0.;
1585dfccc68fSToby Isaac         extJ[4] = 0.;
1586dfccc68fSToby Isaac         extJ[5] = 1.;
1587dfccc68fSToby Isaac         extJ[6] = eta;
1588dfccc68fSToby Isaac         extJ[7] = xi;
1589dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1590dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1591dfccc68fSToby Isaac             PetscReal val = 0.;
1592dfccc68fSToby Isaac 
1593ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1594dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1595dfccc68fSToby Isaac           }
1596dfccc68fSToby Isaac         }
1597dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1598dfccc68fSToby Isaac           PetscReal  x, y, z;
1599dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1600dfccc68fSToby Isaac           PetscReal  norm;
1601dfccc68fSToby Isaac 
1602dfccc68fSToby Isaac           x     = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1603dfccc68fSToby Isaac           y     = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1604dfccc68fSToby Isaac           z     = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1605dfccc68fSToby Isaac           norm  = PetscSqrtReal(x * x + y * y + z * z);
1606dfccc68fSToby Isaac           iJ[2] = x / norm;
1607dfccc68fSToby Isaac           iJ[5] = y / norm;
1608dfccc68fSToby Isaac           iJ[8] = z / norm;
1609dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1610ad540459SPierre Jolivet           if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1611dfccc68fSToby Isaac         } else {
1612dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1613ad540459SPierre Jolivet           if (invJ) DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1614dfccc68fSToby Isaac         }
1615dfccc68fSToby Isaac       }
1616dfccc68fSToby Isaac     }
1617dfccc68fSToby Isaac   }
16186858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1620ccd2543fSMatthew G Knepley }
1621ccd2543fSMatthew G Knepley 
1622d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1623d71ae5a4SJacob Faibussowitsch {
16246858538eSMatthew G. Knepley   const PetscScalar *array;
1625a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1626ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
16276858538eSMatthew G. Knepley   PetscInt           numCoords, d;
16286858538eSMatthew G. Knepley   PetscBool          isDG;
1629ccd2543fSMatthew G Knepley 
1630ccd2543fSMatthew G Knepley   PetscFunctionBegin;
16316858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16326858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
16337f07f362SMatthew G. Knepley   *detJ = 0.0;
16349371c9d4SSatish Balay   if (v0) {
16359371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
16369371c9d4SSatish Balay   }
1637ccd2543fSMatthew G Knepley   if (J) {
1638ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1639f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1640f0df753eSMatthew G. Knepley       J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1641f0df753eSMatthew G. Knepley       J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1642f0df753eSMatthew G. Knepley       J[d * dim + 2] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1643ccd2543fSMatthew G Knepley     }
16449566063dSJacob Faibussowitsch     PetscCall(PetscLogFlops(18.0));
1645923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1646ccd2543fSMatthew G Knepley   }
1647ad540459SPierre Jolivet   if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
16486858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1650ccd2543fSMatthew G Knepley }
1651ccd2543fSMatthew G Knepley 
1652d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1653d71ae5a4SJacob Faibussowitsch {
16546858538eSMatthew G. Knepley   const PetscScalar *array;
1655a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1656ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
16576858538eSMatthew G. Knepley   PetscInt           numCoords, d;
16586858538eSMatthew G. Knepley   PetscBool          isDG;
1659ccd2543fSMatthew G Knepley 
1660ccd2543fSMatthew G Knepley   PetscFunctionBegin;
16616858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16626858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1663dfccc68fSToby Isaac   if (!Nq) {
16647f07f362SMatthew G. Knepley     *detJ = 0.0;
16659371c9d4SSatish Balay     if (v) {
16669371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
16679371c9d4SSatish Balay     }
1668ccd2543fSMatthew G Knepley     if (J) {
1669ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1670f0df753eSMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1671f0df753eSMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1672f0df753eSMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1673ccd2543fSMatthew G Knepley       }
16749566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
1675923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1676ccd2543fSMatthew G Knepley     }
1677ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
1678dfccc68fSToby Isaac   } else {
1679dfccc68fSToby Isaac     const PetscInt Nv         = 8;
1680dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1681dfccc68fSToby Isaac     const PetscInt dim        = 3;
1682dfccc68fSToby Isaac     const PetscInt dimR       = 3;
1683dfccc68fSToby Isaac     PetscReal      zOrder[24];
1684dfccc68fSToby Isaac     PetscReal      zCoeff[24];
1685dfccc68fSToby Isaac     PetscInt       i, j, k, l;
1686dfccc68fSToby Isaac 
1687dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1688dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1689dfccc68fSToby Isaac 
1690ad540459SPierre Jolivet       for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1691dfccc68fSToby Isaac     }
1692dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1693dfccc68fSToby 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]);
1694dfccc68fSToby 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]);
1695dfccc68fSToby 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]);
1696dfccc68fSToby 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]);
1697dfccc68fSToby 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]);
1698dfccc68fSToby 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]);
1699dfccc68fSToby 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]);
1700dfccc68fSToby 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]);
1701dfccc68fSToby Isaac     }
1702dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1703dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1704dfccc68fSToby Isaac 
1705dfccc68fSToby Isaac       if (v) {
170691d2b7ceSToby Isaac         PetscReal extPoint[8];
1707dfccc68fSToby Isaac 
1708dfccc68fSToby Isaac         extPoint[0] = 1.;
1709dfccc68fSToby Isaac         extPoint[1] = xi;
1710dfccc68fSToby Isaac         extPoint[2] = eta;
1711dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1712dfccc68fSToby Isaac         extPoint[4] = theta;
1713dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1714dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1715dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1716dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1717dfccc68fSToby Isaac           PetscReal val = 0.;
1718dfccc68fSToby Isaac 
1719ad540459SPierre Jolivet           for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j];
1720dfccc68fSToby Isaac           v[i * dim + j] = val;
1721dfccc68fSToby Isaac         }
1722dfccc68fSToby Isaac       }
1723dfccc68fSToby Isaac       if (J) {
1724dfccc68fSToby Isaac         PetscReal extJ[24];
1725dfccc68fSToby Isaac 
17269371c9d4SSatish Balay         extJ[0]  = 0.;
17279371c9d4SSatish Balay         extJ[1]  = 0.;
17289371c9d4SSatish Balay         extJ[2]  = 0.;
17299371c9d4SSatish Balay         extJ[3]  = 1.;
17309371c9d4SSatish Balay         extJ[4]  = 0.;
17319371c9d4SSatish Balay         extJ[5]  = 0.;
17329371c9d4SSatish Balay         extJ[6]  = 0.;
17339371c9d4SSatish Balay         extJ[7]  = 1.;
17349371c9d4SSatish Balay         extJ[8]  = 0.;
17359371c9d4SSatish Balay         extJ[9]  = eta;
17369371c9d4SSatish Balay         extJ[10] = xi;
17379371c9d4SSatish Balay         extJ[11] = 0.;
17389371c9d4SSatish Balay         extJ[12] = 0.;
17399371c9d4SSatish Balay         extJ[13] = 0.;
17409371c9d4SSatish Balay         extJ[14] = 1.;
17419371c9d4SSatish Balay         extJ[15] = theta;
17429371c9d4SSatish Balay         extJ[16] = 0.;
17439371c9d4SSatish Balay         extJ[17] = xi;
17449371c9d4SSatish Balay         extJ[18] = 0.;
17459371c9d4SSatish Balay         extJ[19] = theta;
17469371c9d4SSatish Balay         extJ[20] = eta;
17479371c9d4SSatish Balay         extJ[21] = theta * eta;
17489371c9d4SSatish Balay         extJ[22] = theta * xi;
17499371c9d4SSatish Balay         extJ[23] = eta * xi;
1750dfccc68fSToby Isaac 
1751dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1752dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1753dfccc68fSToby Isaac             PetscReal val = 0.;
1754dfccc68fSToby Isaac 
1755ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1756dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1757dfccc68fSToby Isaac           }
1758dfccc68fSToby Isaac         }
1759dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1760ad540459SPierre Jolivet         if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1761dfccc68fSToby Isaac       }
1762dfccc68fSToby Isaac     }
1763dfccc68fSToby Isaac   }
17646858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
17653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1766ccd2543fSMatthew G Knepley }
1767ccd2543fSMatthew G Knepley 
1768d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1769d71ae5a4SJacob Faibussowitsch {
17706858538eSMatthew G. Knepley   const PetscScalar *array;
17712df84da0SMatthew G. Knepley   PetscScalar       *coords = NULL;
17722df84da0SMatthew G. Knepley   const PetscInt     dim    = 3;
17736858538eSMatthew G. Knepley   PetscInt           numCoords, d;
17746858538eSMatthew G. Knepley   PetscBool          isDG;
17752df84da0SMatthew G. Knepley 
17762df84da0SMatthew G. Knepley   PetscFunctionBegin;
17776858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
17786858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
17792df84da0SMatthew G. Knepley   if (!Nq) {
17802df84da0SMatthew G. Knepley     /* Assume that the map to the reference is affine */
17812df84da0SMatthew G. Knepley     *detJ = 0.0;
17829371c9d4SSatish Balay     if (v) {
17839371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
17849371c9d4SSatish Balay     }
17852df84da0SMatthew G. Knepley     if (J) {
17862df84da0SMatthew G. Knepley       for (d = 0; d < dim; d++) {
17872df84da0SMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17882df84da0SMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17892df84da0SMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17902df84da0SMatthew G. Knepley       }
17919566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
17922df84da0SMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
17932df84da0SMatthew G. Knepley     }
1794ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
17952df84da0SMatthew G. Knepley   } else {
17962df84da0SMatthew G. Knepley     const PetscInt dim  = 3;
17972df84da0SMatthew G. Knepley     const PetscInt dimR = 3;
17982df84da0SMatthew G. Knepley     const PetscInt Nv   = 6;
17992df84da0SMatthew G. Knepley     PetscReal      verts[18];
18002df84da0SMatthew G. Knepley     PetscReal      coeff[18];
18012df84da0SMatthew G. Knepley     PetscInt       i, j, k, l;
18022df84da0SMatthew G. Knepley 
18039371c9d4SSatish Balay     for (i = 0; i < Nv; ++i)
18049371c9d4SSatish Balay       for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]);
18052df84da0SMatthew G. Knepley     for (j = 0; j < dim; ++j) {
18062df84da0SMatthew G. Knepley       /* Check for triangle,
18072df84da0SMatthew 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)
18082df84da0SMatthew 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)
18092df84da0SMatthew G. Knepley            phi^2 =  1/2 (1 + eta)   chi^2 = delta(-1,  1)
18102df84da0SMatthew G. Knepley 
18112df84da0SMatthew G. Knepley            phi^0 + phi^1 + phi^2 = 1    coef_1   = 1/2 (         chi^1 + chi^2)
18122df84da0SMatthew G. Knepley           -phi^0 + phi^1 - phi^2 = xi   coef_xi  = 1/2 (-chi^0 + chi^1)
18132df84da0SMatthew G. Knepley           -phi^0 - phi^1 + phi^2 = eta  coef_eta = 1/2 (-chi^0         + chi^2)
18142df84da0SMatthew G. Knepley 
18152df84da0SMatthew G. Knepley           < chi_0 chi_1 chi_2> A /  1  1  1 \ / phi_0 \   <chi> I <phi>^T  so we need the inverse transpose
18162df84da0SMatthew G. Knepley                                  | -1  1 -1 | | phi_1 | =
18172df84da0SMatthew G. Knepley                                  \ -1 -1  1 / \ phi_2 /
18182df84da0SMatthew G. Knepley 
18192df84da0SMatthew 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
18202df84da0SMatthew G. Knepley       */
18212df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta):
18222df84da0SMatthew G. Knepley            \phi^0 = 1/4 (   -xi - eta        + xi zeta + eta zeta) --> /  1  1  1  1  1  1 \ 1
18232df84da0SMatthew G. Knepley            \phi^1 = 1/4 (1      + eta - zeta           - eta zeta) --> | -1  1 -1 -1 -1  1 | eta
18242df84da0SMatthew G. Knepley            \phi^2 = 1/4 (1 + xi       - zeta - xi zeta)            --> | -1 -1  1 -1  1 -1 | xi
18252df84da0SMatthew G. Knepley            \phi^3 = 1/4 (   -xi - eta        - xi zeta - eta zeta) --> | -1 -1 -1  1  1  1 | zeta
18262df84da0SMatthew G. Knepley            \phi^4 = 1/4 (1 + xi       + zeta + xi zeta)            --> |  1  1 -1 -1  1 -1 | xi zeta
18272df84da0SMatthew G. Knepley            \phi^5 = 1/4 (1      + eta + zeta           + eta zeta) --> \  1 -1  1 -1 -1  1 / eta zeta
18282df84da0SMatthew G. Knepley            1/4 /  0  1  1  0  1  1 \
18292df84da0SMatthew G. Knepley                | -1  1  0 -1  0  1 |
18302df84da0SMatthew G. Knepley                | -1  0  1 -1  1  0 |
18312df84da0SMatthew G. Knepley                |  0 -1 -1  0  1  1 |
18322df84da0SMatthew G. Knepley                |  1  0 -1 -1  1  0 |
18332df84da0SMatthew G. Knepley                \  1 -1  0 -1  0  1 /
18342df84da0SMatthew G. Knepley       */
18352df84da0SMatthew G. Knepley       coeff[dim * 0 + j] = (1. / 4.) * (verts[dim * 1 + j] + verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
18362df84da0SMatthew G. Knepley       coeff[dim * 1 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
18372df84da0SMatthew G. Knepley       coeff[dim * 2 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
18382df84da0SMatthew G. Knepley       coeff[dim * 3 + j] = (1. / 4.) * (-verts[dim * 1 + j] - verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
18392df84da0SMatthew G. Knepley       coeff[dim * 4 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
18402df84da0SMatthew G. Knepley       coeff[dim * 5 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
18412df84da0SMatthew G. Knepley       /* For reference prism:
18422df84da0SMatthew G. Knepley       {0, 0, 0}
18432df84da0SMatthew G. Knepley       {0, 1, 0}
18442df84da0SMatthew G. Knepley       {1, 0, 0}
18452df84da0SMatthew G. Knepley       {0, 0, 1}
18462df84da0SMatthew G. Knepley       {0, 0, 0}
18472df84da0SMatthew G. Knepley       {0, 0, 0}
18482df84da0SMatthew G. Knepley       */
18492df84da0SMatthew G. Knepley     }
18502df84da0SMatthew G. Knepley     for (i = 0; i < Nq; ++i) {
18512df84da0SMatthew G. Knepley       const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2];
18522df84da0SMatthew G. Knepley 
18532df84da0SMatthew G. Knepley       if (v) {
18542df84da0SMatthew G. Knepley         PetscReal extPoint[6];
18552df84da0SMatthew G. Knepley         PetscInt  c;
18562df84da0SMatthew G. Knepley 
18572df84da0SMatthew G. Knepley         extPoint[0] = 1.;
18582df84da0SMatthew G. Knepley         extPoint[1] = eta;
18592df84da0SMatthew G. Knepley         extPoint[2] = xi;
18602df84da0SMatthew G. Knepley         extPoint[3] = zeta;
18612df84da0SMatthew G. Knepley         extPoint[4] = xi * zeta;
18622df84da0SMatthew G. Knepley         extPoint[5] = eta * zeta;
18632df84da0SMatthew G. Knepley         for (c = 0; c < dim; ++c) {
18642df84da0SMatthew G. Knepley           PetscReal val = 0.;
18652df84da0SMatthew G. Knepley 
1866ad540459SPierre Jolivet           for (k = 0; k < Nv; ++k) val += extPoint[k] * coeff[k * dim + c];
18672df84da0SMatthew G. Knepley           v[i * dim + c] = val;
18682df84da0SMatthew G. Knepley         }
18692df84da0SMatthew G. Knepley       }
18702df84da0SMatthew G. Knepley       if (J) {
18712df84da0SMatthew G. Knepley         PetscReal extJ[18];
18722df84da0SMatthew G. Knepley 
18739371c9d4SSatish Balay         extJ[0]  = 0.;
18749371c9d4SSatish Balay         extJ[1]  = 0.;
18759371c9d4SSatish Balay         extJ[2]  = 0.;
18769371c9d4SSatish Balay         extJ[3]  = 0.;
18779371c9d4SSatish Balay         extJ[4]  = 1.;
18789371c9d4SSatish Balay         extJ[5]  = 0.;
18799371c9d4SSatish Balay         extJ[6]  = 1.;
18809371c9d4SSatish Balay         extJ[7]  = 0.;
18819371c9d4SSatish Balay         extJ[8]  = 0.;
18829371c9d4SSatish Balay         extJ[9]  = 0.;
18839371c9d4SSatish Balay         extJ[10] = 0.;
18849371c9d4SSatish Balay         extJ[11] = 1.;
18859371c9d4SSatish Balay         extJ[12] = zeta;
18869371c9d4SSatish Balay         extJ[13] = 0.;
18879371c9d4SSatish Balay         extJ[14] = xi;
18889371c9d4SSatish Balay         extJ[15] = 0.;
18899371c9d4SSatish Balay         extJ[16] = zeta;
18909371c9d4SSatish Balay         extJ[17] = eta;
18912df84da0SMatthew G. Knepley 
18922df84da0SMatthew G. Knepley         for (j = 0; j < dim; j++) {
18932df84da0SMatthew G. Knepley           for (k = 0; k < dimR; k++) {
18942df84da0SMatthew G. Knepley             PetscReal val = 0.;
18952df84da0SMatthew G. Knepley 
1896ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += coeff[dim * l + j] * extJ[dimR * l + k];
18972df84da0SMatthew G. Knepley             J[i * dim * dim + dim * j + k] = val;
18982df84da0SMatthew G. Knepley           }
18992df84da0SMatthew G. Knepley         }
19002df84da0SMatthew G. Knepley         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1901ad540459SPierre Jolivet         if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
19022df84da0SMatthew G. Knepley       }
19032df84da0SMatthew G. Knepley     }
19042df84da0SMatthew G. Knepley   }
19056858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
19063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19072df84da0SMatthew G. Knepley }
19082df84da0SMatthew G. Knepley 
1909d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1910d71ae5a4SJacob Faibussowitsch {
1911ba2698f1SMatthew G. Knepley   DMPolytopeType   ct;
1912dfccc68fSToby Isaac   PetscInt         depth, dim, coordDim, coneSize, i;
1913dfccc68fSToby Isaac   PetscInt         Nq     = 0;
1914dfccc68fSToby Isaac   const PetscReal *points = NULL;
1915dfccc68fSToby Isaac   DMLabel          depthLabel;
1916c330f8ffSToby Isaac   PetscReal        xi0[3]   = {-1., -1., -1.}, v0[3], J0[9], detJ0;
1917dfccc68fSToby Isaac   PetscBool        isAffine = PETSC_TRUE;
1918dfccc68fSToby Isaac 
1919dfccc68fSToby Isaac   PetscFunctionBegin;
19209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
19219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
19239566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(depthLabel, cell, &dim));
192448a46eb9SPierre Jolivet   if (depth == 1 && dim == 1) PetscCall(DMGetDimension(dm, &dim));
19259566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &coordDim));
192663a3b9bcSJacob Faibussowitsch   PetscCheck(coordDim <= 3, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %" PetscInt_FMT " > 3", coordDim);
19279566063dSJacob Faibussowitsch   if (quad) PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL));
19289566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
1929ba2698f1SMatthew G. Knepley   switch (ct) {
1930ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_POINT:
19319566063dSJacob Faibussowitsch     PetscCall(DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ));
1932dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1933dfccc68fSToby Isaac     break;
1934ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
1935412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
19369566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19379566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ));
1938dfccc68fSToby Isaac     break;
1939ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
19409566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19419566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ));
1942dfccc68fSToby Isaac     break;
1943ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
19449566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ));
1945412e9a14SMatthew G. Knepley     isAffine = PETSC_FALSE;
1946412e9a14SMatthew G. Knepley     break;
1947412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
19489566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ));
1949dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1950dfccc68fSToby Isaac     break;
1951ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
19529566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19539566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ));
1954dfccc68fSToby Isaac     break;
1955ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
19569566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
1957dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1958dfccc68fSToby Isaac     break;
19592df84da0SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM:
19609566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
19612df84da0SMatthew G. Knepley     isAffine = PETSC_FALSE;
19622df84da0SMatthew G. Knepley     break;
1963d71ae5a4SJacob Faibussowitsch   default:
1964d71ae5a4SJacob 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))]);
1965dfccc68fSToby Isaac   }
19667318780aSToby Isaac   if (isAffine && Nq) {
1967dfccc68fSToby Isaac     if (v) {
1968ad540459SPierre Jolivet       for (i = 0; i < Nq; i++) CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]);
1969dfccc68fSToby Isaac     }
19707318780aSToby Isaac     if (detJ) {
1971ad540459SPierre Jolivet       for (i = 0; i < Nq; i++) detJ[i] = detJ0;
19727318780aSToby Isaac     }
19737318780aSToby Isaac     if (J) {
19747318780aSToby Isaac       PetscInt k;
19757318780aSToby Isaac 
19767318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1977dfccc68fSToby Isaac         PetscInt j;
1978dfccc68fSToby Isaac 
1979ad540459SPierre Jolivet         for (j = 0; j < coordDim * coordDim; j++, k++) J[k] = J0[j];
19807318780aSToby Isaac       }
19817318780aSToby Isaac     }
19827318780aSToby Isaac     if (invJ) {
19837318780aSToby Isaac       PetscInt k;
19847318780aSToby Isaac       switch (coordDim) {
1985d71ae5a4SJacob Faibussowitsch       case 0:
1986d71ae5a4SJacob Faibussowitsch         break;
1987d71ae5a4SJacob Faibussowitsch       case 1:
1988d71ae5a4SJacob Faibussowitsch         invJ[0] = 1. / J0[0];
1989d71ae5a4SJacob Faibussowitsch         break;
1990d71ae5a4SJacob Faibussowitsch       case 2:
1991d71ae5a4SJacob Faibussowitsch         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
1992d71ae5a4SJacob Faibussowitsch         break;
1993d71ae5a4SJacob Faibussowitsch       case 3:
1994d71ae5a4SJacob Faibussowitsch         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
1995d71ae5a4SJacob Faibussowitsch         break;
19967318780aSToby Isaac       }
19977318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
19987318780aSToby Isaac         PetscInt j;
19997318780aSToby Isaac 
2000ad540459SPierre Jolivet         for (j = 0; j < coordDim * coordDim; j++, k++) invJ[k] = invJ[j];
2001dfccc68fSToby Isaac       }
2002dfccc68fSToby Isaac     }
2003dfccc68fSToby Isaac   }
20043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2005dfccc68fSToby Isaac }
2006dfccc68fSToby Isaac 
2007ccd2543fSMatthew G Knepley /*@C
20088e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
2009ccd2543fSMatthew G Knepley 
2010*20f4b53cSBarry Smith   Collective
2011ccd2543fSMatthew G Knepley 
20124165533cSJose E. Roman   Input Parameters:
2013*20f4b53cSBarry Smith + dm   - the `DMPLEX`
2014ccd2543fSMatthew G Knepley - cell - the cell
2015ccd2543fSMatthew G Knepley 
20164165533cSJose E. Roman   Output Parameters:
20179b172b3aSMatthew Knepley + v0   - the translation part of this affine transform, meaning the translation to the origin (not the first vertex of the reference cell)
2018ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
2019ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
2020ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
2021ccd2543fSMatthew G Knepley 
2022ccd2543fSMatthew G Knepley   Level: advanced
2023ccd2543fSMatthew G Knepley 
2024*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeCellGeometryFEM()`, `DMGetCoordinateSection()`, `DMGetCoordinates()`
2025ccd2543fSMatthew G Knepley @*/
2026d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
2027d71ae5a4SJacob Faibussowitsch {
2028ccd2543fSMatthew G Knepley   PetscFunctionBegin;
20299566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, NULL, v0, J, invJ, detJ));
20303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20318e0841e0SMatthew G. Knepley }
20328e0841e0SMatthew G. Knepley 
2033d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
2034d71ae5a4SJacob Faibussowitsch {
20356858538eSMatthew G. Knepley   const PetscScalar *array;
20368e0841e0SMatthew G. Knepley   PetscScalar       *coords = NULL;
20376858538eSMatthew G. Knepley   PetscInt           numCoords;
20386858538eSMatthew G. Knepley   PetscBool          isDG;
20396858538eSMatthew G. Knepley   PetscQuadrature    feQuad;
20408e0841e0SMatthew G. Knepley   const PetscReal   *quadPoints;
2041ef0bb6c7SMatthew G. Knepley   PetscTabulation    T;
20426858538eSMatthew G. Knepley   PetscInt           dim, cdim, pdim, qdim, Nq, q;
20438e0841e0SMatthew G. Knepley 
20448e0841e0SMatthew G. Knepley   PetscFunctionBegin;
20459566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
20469566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
20476858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
2048dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
2049dfccc68fSToby Isaac     PetscDualSpace dsp;
2050dfccc68fSToby Isaac 
20519566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(fe, &dsp));
20529566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetFunctional(dsp, 0, &quad));
20539566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2054dfccc68fSToby Isaac     Nq = 1;
2055dfccc68fSToby Isaac   } else {
20569566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2057dfccc68fSToby Isaac   }
20589566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
20599566063dSJacob Faibussowitsch   PetscCall(PetscFEGetQuadrature(fe, &feQuad));
2060dfccc68fSToby Isaac   if (feQuad == quad) {
20619566063dSJacob Faibussowitsch     PetscCall(PetscFEGetCellTabulation(fe, J ? 1 : 0, &T));
206263a3b9bcSJacob 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);
2063dfccc68fSToby Isaac   } else {
20649566063dSJacob Faibussowitsch     PetscCall(PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T));
2065dfccc68fSToby Isaac   }
206663a3b9bcSJacob Faibussowitsch   PetscCheck(qdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %" PetscInt_FMT " != quadrature dimension %" PetscInt_FMT, dim, qdim);
2067ef0bb6c7SMatthew G. Knepley   {
2068ef0bb6c7SMatthew G. Knepley     const PetscReal *basis    = T->T[0];
2069ef0bb6c7SMatthew G. Knepley     const PetscReal *basisDer = T->T[1];
2070ef0bb6c7SMatthew G. Knepley     PetscReal        detJt;
2071ef0bb6c7SMatthew G. Knepley 
2072a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
207363a3b9bcSJacob Faibussowitsch     PetscCheck(Nq == T->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %" PetscInt_FMT " != %" PetscInt_FMT, Nq, T->Np);
207463a3b9bcSJacob Faibussowitsch     PetscCheck(pdim == T->Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %" PetscInt_FMT " != %" PetscInt_FMT, pdim, T->Nb);
207563a3b9bcSJacob Faibussowitsch     PetscCheck(dim == T->Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %" PetscInt_FMT " != %" PetscInt_FMT, dim, T->Nc);
207663a3b9bcSJacob Faibussowitsch     PetscCheck(cdim == T->cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %" PetscInt_FMT " != %" PetscInt_FMT, cdim, T->cdim);
2077a2a9e04cSMatthew G. Knepley #endif
2078dfccc68fSToby Isaac     if (v) {
20799566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(v, Nq * cdim));
2080f960e424SToby Isaac       for (q = 0; q < Nq; ++q) {
2081f960e424SToby Isaac         PetscInt i, k;
2082f960e424SToby Isaac 
2083301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2084301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
2085ad540459SPierre Jolivet           for (i = 0; i < cdim; ++i) v[q * cdim + i] += basis[(q * pdim + k) * cdim + i] * PetscRealPart(coords[vertex * cdim + i]);
2086301b184aSMatthew G. Knepley         }
20879566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * cdim));
2088f960e424SToby Isaac       }
2089f960e424SToby Isaac     }
20908e0841e0SMatthew G. Knepley     if (J) {
20919566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(J, Nq * cdim * cdim));
20928e0841e0SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
20938e0841e0SMatthew G. Knepley         PetscInt i, j, k, c, r;
20948e0841e0SMatthew G. Knepley 
20958e0841e0SMatthew G. Knepley         /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
2096301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2097301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
2098301b184aSMatthew G. Knepley           for (j = 0; j < dim; ++j) {
2099ad540459SPierre 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]);
2100301b184aSMatthew G. Knepley           }
2101301b184aSMatthew G. Knepley         }
21029566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * dim * cdim));
21038e0841e0SMatthew G. Knepley         if (cdim > dim) {
21048e0841e0SMatthew G. Knepley           for (c = dim; c < cdim; ++c)
21059371c9d4SSatish Balay             for (r = 0; r < cdim; ++r) J[r * cdim + c] = r == c ? 1.0 : 0.0;
21068e0841e0SMatthew G. Knepley         }
2107f960e424SToby Isaac         if (!detJ && !invJ) continue;
2108a63b72c6SToby Isaac         detJt = 0.;
21098e0841e0SMatthew G. Knepley         switch (cdim) {
21108e0841e0SMatthew G. Knepley         case 3:
2111037dc194SToby Isaac           DMPlex_Det3D_Internal(&detJt, &J[q * cdim * dim]);
2112ad540459SPierre Jolivet           if (invJ) DMPlex_Invert3D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt);
211317fe8556SMatthew G. Knepley           break;
211449dc4407SMatthew G. Knepley         case 2:
21159f328543SToby Isaac           DMPlex_Det2D_Internal(&detJt, &J[q * cdim * dim]);
2116ad540459SPierre Jolivet           if (invJ) DMPlex_Invert2D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt);
211749dc4407SMatthew G. Knepley           break;
21188e0841e0SMatthew G. Knepley         case 1:
2119037dc194SToby Isaac           detJt = J[q * cdim * dim];
2120037dc194SToby Isaac           if (invJ) invJ[q * cdim * dim] = 1.0 / detJt;
212149dc4407SMatthew G. Knepley         }
2122f960e424SToby Isaac         if (detJ) detJ[q] = detJt;
212349dc4407SMatthew G. Knepley       }
212408401ef6SPierre Jolivet     } else PetscCheck(!detJ && !invJ, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
212549dc4407SMatthew G. Knepley   }
21269566063dSJacob Faibussowitsch   if (feQuad != quad) PetscCall(PetscTabulationDestroy(&T));
21276858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
21283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21298e0841e0SMatthew G. Knepley }
21308e0841e0SMatthew G. Knepley 
21318e0841e0SMatthew G. Knepley /*@C
21328e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
21338e0841e0SMatthew G. Knepley 
2134*20f4b53cSBarry Smith   Collective
21358e0841e0SMatthew G. Knepley 
21364165533cSJose E. Roman   Input Parameters:
2137*20f4b53cSBarry Smith + dm   - the `DMPLEX`
21388e0841e0SMatthew G. Knepley . cell - the cell
2139*20f4b53cSBarry Smith - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If `quad` is `NULL`, geometry will be
2140dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
21418e0841e0SMatthew G. Knepley 
21424165533cSJose E. Roman   Output Parameters:
2143dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
21448e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
21458e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
21468e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
21478e0841e0SMatthew G. Knepley 
21488e0841e0SMatthew G. Knepley   Level: advanced
21498e0841e0SMatthew G. Knepley 
2150*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinateSection()`, `DMGetCoordinates()`
21518e0841e0SMatthew G. Knepley @*/
2152d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
2153d71ae5a4SJacob Faibussowitsch {
2154bb4a5db5SMatthew G. Knepley   DM      cdm;
2155dfccc68fSToby Isaac   PetscFE fe = NULL;
21568e0841e0SMatthew G. Knepley 
21578e0841e0SMatthew G. Knepley   PetscFunctionBegin;
2158dadcf809SJacob Faibussowitsch   PetscValidRealPointer(detJ, 7);
21599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
2160bb4a5db5SMatthew G. Knepley   if (cdm) {
2161dfccc68fSToby Isaac     PetscClassId id;
2162dfccc68fSToby Isaac     PetscInt     numFields;
2163e5e52638SMatthew G. Knepley     PetscDS      prob;
2164dfccc68fSToby Isaac     PetscObject  disc;
2165dfccc68fSToby Isaac 
21669566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(cdm, &numFields));
2167dfccc68fSToby Isaac     if (numFields) {
21689566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &prob));
21699566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(prob, 0, &disc));
21709566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
2171ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
2172dfccc68fSToby Isaac     }
2173dfccc68fSToby Isaac   }
21749566063dSJacob Faibussowitsch   if (!fe) PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ));
21759566063dSJacob Faibussowitsch   else PetscCall(DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ));
21763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2177ccd2543fSMatthew G Knepley }
2178834e62ceSMatthew G. Knepley 
2179d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2180d71ae5a4SJacob Faibussowitsch {
21819bf2564aSMatt McGurn   PetscSection       coordSection;
21829bf2564aSMatt McGurn   Vec                coordinates;
21839bf2564aSMatt McGurn   const PetscScalar *coords = NULL;
21849bf2564aSMatt McGurn   PetscInt           d, dof, off;
21859bf2564aSMatt McGurn 
21869bf2564aSMatt McGurn   PetscFunctionBegin;
21879566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
21889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
21899566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
21909bf2564aSMatt McGurn 
21919bf2564aSMatt McGurn   /* for a point the centroid is just the coord */
21929bf2564aSMatt McGurn   if (centroid) {
21939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
21949566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
2195ad540459SPierre Jolivet     for (d = 0; d < dof; d++) centroid[d] = PetscRealPart(coords[off + d]);
21969bf2564aSMatt McGurn   }
21979bf2564aSMatt McGurn   if (normal) {
21989bf2564aSMatt McGurn     const PetscInt *support, *cones;
21999bf2564aSMatt McGurn     PetscInt        supportSize;
22009bf2564aSMatt McGurn     PetscReal       norm, sign;
22019bf2564aSMatt McGurn 
22029bf2564aSMatt McGurn     /* compute the norm based upon the support centroids */
22039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, cell, &supportSize));
22049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, cell, &support));
22059566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL));
22069bf2564aSMatt McGurn 
22079bf2564aSMatt McGurn     /* Take the normal from the centroid of the support to the vertex*/
22089566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
22099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
2210ad540459SPierre Jolivet     for (d = 0; d < dof; d++) normal[d] -= PetscRealPart(coords[off + d]);
22119bf2564aSMatt McGurn 
22129bf2564aSMatt McGurn     /* Determine the sign of the normal based upon its location in the support */
22139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, support[0], &cones));
22149bf2564aSMatt McGurn     sign = cones[0] == cell ? 1.0 : -1.0;
22159bf2564aSMatt McGurn 
22169bf2564aSMatt McGurn     norm = DMPlex_NormD_Internal(dim, normal);
22179bf2564aSMatt McGurn     for (d = 0; d < dim; ++d) normal[d] /= (norm * sign);
22189bf2564aSMatt McGurn   }
2219ad540459SPierre Jolivet   if (vol) *vol = 1.0;
22209566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
22213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22229bf2564aSMatt McGurn }
22239bf2564aSMatt McGurn 
2224d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2225d71ae5a4SJacob Faibussowitsch {
22266858538eSMatthew G. Knepley   const PetscScalar *array;
2227a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
222821d6a034SMatthew G. Knepley   PetscInt           cdim, coordSize, d;
22296858538eSMatthew G. Knepley   PetscBool          isDG;
2230cc08537eSMatthew G. Knepley 
2231cc08537eSMatthew G. Knepley   PetscFunctionBegin;
223221d6a034SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
22336858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
223421d6a034SMatthew G. Knepley   PetscCheck(coordSize == cdim * 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Edge has %" PetscInt_FMT " coordinates != %" PetscInt_FMT, coordSize, cdim * 2);
2235cc08537eSMatthew G. Knepley   if (centroid) {
223621d6a034SMatthew G. Knepley     for (d = 0; d < cdim; ++d) centroid[d] = 0.5 * PetscRealPart(coords[d] + coords[cdim + d]);
2237cc08537eSMatthew G. Knepley   }
2238cc08537eSMatthew G. Knepley   if (normal) {
2239a60a936bSMatthew G. Knepley     PetscReal norm;
2240a60a936bSMatthew G. Knepley 
224121d6a034SMatthew G. Knepley     switch (cdim) {
224221d6a034SMatthew G. Knepley     case 3:
2243f315e28eSPierre Jolivet       normal[2] = 0.; /* fall through */
224421d6a034SMatthew G. Knepley     case 2:
224521d6a034SMatthew G. Knepley       normal[0] = -PetscRealPart(coords[1] - coords[cdim + 1]);
224621d6a034SMatthew G. Knepley       normal[1] = PetscRealPart(coords[0] - coords[cdim + 0]);
224721d6a034SMatthew G. Knepley       break;
224821d6a034SMatthew G. Knepley     case 1:
224921d6a034SMatthew G. Knepley       normal[0] = 1.0;
225021d6a034SMatthew G. Knepley       break;
225121d6a034SMatthew G. Knepley     default:
225221d6a034SMatthew G. Knepley       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Dimension %" PetscInt_FMT " not supported", cdim);
225321d6a034SMatthew G. Knepley     }
225421d6a034SMatthew G. Knepley     norm = DMPlex_NormD_Internal(cdim, normal);
225521d6a034SMatthew G. Knepley     for (d = 0; d < cdim; ++d) normal[d] /= norm;
2256cc08537eSMatthew G. Knepley   }
2257cc08537eSMatthew G. Knepley   if (vol) {
2258714b99b6SMatthew G. Knepley     *vol = 0.0;
225921d6a034SMatthew G. Knepley     for (d = 0; d < cdim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - coords[cdim + d]));
2260714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
2261cc08537eSMatthew G. Knepley   }
22626858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
22633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2264cc08537eSMatthew G. Knepley }
2265cc08537eSMatthew G. Knepley 
2266cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */
2267d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2268d71ae5a4SJacob Faibussowitsch {
2269412e9a14SMatthew G. Knepley   DMPolytopeType     ct;
22706858538eSMatthew G. Knepley   const PetscScalar *array;
2271cc08537eSMatthew G. Knepley   PetscScalar       *coords = NULL;
22726858538eSMatthew G. Knepley   PetscInt           coordSize;
22736858538eSMatthew G. Knepley   PetscBool          isDG;
2274793a2a13SMatthew G. Knepley   PetscInt           fv[4] = {0, 1, 2, 3};
22756858538eSMatthew G. Knepley   PetscInt           cdim, numCorners, p, d;
2276cc08537eSMatthew G. Knepley 
2277cc08537eSMatthew G. Knepley   PetscFunctionBegin;
2278793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
22799566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2280412e9a14SMatthew G. Knepley   switch (ct) {
22819371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR:
22829371c9d4SSatish Balay     fv[2] = 3;
22839371c9d4SSatish Balay     fv[3] = 2;
22849371c9d4SSatish Balay     break;
2285d71ae5a4SJacob Faibussowitsch   default:
2286d71ae5a4SJacob Faibussowitsch     break;
2287412e9a14SMatthew G. Knepley   }
22889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
22896858538eSMatthew G. Knepley   PetscCall(DMPlexGetConeSize(dm, cell, &numCorners));
22906858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
22913f27a4e6SJed Brown   {
22923f27a4e6SJed Brown     PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm;
2293793a2a13SMatthew G. Knepley 
22943f27a4e6SJed Brown     for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]);
22954f99dae5SMatthew G. Knepley     for (p = 0; p < numCorners - 2; ++p) {
22963f27a4e6SJed Brown       PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.};
22973f27a4e6SJed Brown       for (d = 0; d < cdim; d++) {
22983f27a4e6SJed Brown         e0[d] = PetscRealPart(coords[cdim * fv[p + 1] + d]) - origin[d];
22993f27a4e6SJed Brown         e1[d] = PetscRealPart(coords[cdim * fv[p + 2] + d]) - origin[d];
23003f27a4e6SJed Brown       }
23013f27a4e6SJed Brown       const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1];
23023f27a4e6SJed Brown       const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2];
23033f27a4e6SJed Brown       const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0];
23043f27a4e6SJed Brown       const PetscReal a  = PetscSqrtReal(dx * dx + dy * dy + dz * dz);
23054f99dae5SMatthew G. Knepley 
23064f99dae5SMatthew G. Knepley       n[0] += dx;
23074f99dae5SMatthew G. Knepley       n[1] += dy;
23084f99dae5SMatthew G. Knepley       n[2] += dz;
2309ad540459SPierre 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.;
2310ceee4971SMatthew G. Knepley     }
23114f99dae5SMatthew G. Knepley     norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
23124f99dae5SMatthew G. Knepley     n[0] /= norm;
23134f99dae5SMatthew G. Knepley     n[1] /= norm;
23144f99dae5SMatthew G. Knepley     n[2] /= norm;
23154f99dae5SMatthew G. Knepley     c[0] /= norm;
23164f99dae5SMatthew G. Knepley     c[1] /= norm;
23174f99dae5SMatthew G. Knepley     c[2] /= norm;
23184f99dae5SMatthew G. Knepley     if (vol) *vol = 0.5 * norm;
23199371c9d4SSatish Balay     if (centroid)
23209371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) centroid[d] = c[d];
23219371c9d4SSatish Balay     if (normal)
23229371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) normal[d] = n[d];
23230a1d6728SMatthew G. Knepley   }
23246858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
23253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2326cc08537eSMatthew G. Knepley }
2327cc08537eSMatthew G. Knepley 
23280ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */
2329d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2330d71ae5a4SJacob Faibussowitsch {
2331412e9a14SMatthew G. Knepley   DMPolytopeType        ct;
23326858538eSMatthew G. Knepley   const PetscScalar    *array;
23330ec8681fSMatthew G. Knepley   PetscScalar          *coords = NULL;
23346858538eSMatthew G. Knepley   PetscInt              coordSize;
23356858538eSMatthew G. Knepley   PetscBool             isDG;
23363f27a4e6SJed Brown   PetscReal             vsum      = 0.0, vtmp, coordsTmp[3 * 3], origin[3];
23376858538eSMatthew G. Knepley   const PetscInt        order[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
23386858538eSMatthew G. Knepley   const PetscInt       *cone, *faceSizes, *faces;
23396858538eSMatthew G. Knepley   const DMPolytopeType *faceTypes;
2340793a2a13SMatthew G. Knepley   PetscBool             isHybrid = PETSC_FALSE;
23416858538eSMatthew G. Knepley   PetscInt              numFaces, f, fOff = 0, p, d;
23420ec8681fSMatthew G. Knepley 
23430ec8681fSMatthew G. Knepley   PetscFunctionBegin;
234463a3b9bcSJacob Faibussowitsch   PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No support for dim %" PetscInt_FMT " > 3", dim);
2345793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
23469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2347412e9a14SMatthew G. Knepley   switch (ct) {
2348412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
2349412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
2350412e9a14SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR:
2351d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
2352d71ae5a4SJacob Faibussowitsch     isHybrid = PETSC_TRUE;
2353d71ae5a4SJacob Faibussowitsch   default:
2354d71ae5a4SJacob Faibussowitsch     break;
2355412e9a14SMatthew G. Knepley   }
2356793a2a13SMatthew G. Knepley 
23579371c9d4SSatish Balay   if (centroid)
23589371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = 0.0;
23596858538eSMatthew G. Knepley   PetscCall(DMPlexGetCone(dm, cell, &cone));
23606858538eSMatthew G. Knepley 
23616858538eSMatthew G. Knepley   // Using the closure of faces for coordinates does not work in periodic geometries, so we index into the cell coordinates
23626858538eSMatthew G. Knepley   PetscCall(DMPlexGetRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
23636858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
23640ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
2365793a2a13SMatthew G. Knepley     PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
2366793a2a13SMatthew G. Knepley 
23673f27a4e6SJed Brown     // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and
23683f27a4e6SJed Brown     // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex
23693f27a4e6SJed Brown     // so that all tetrahedra have positive volume.
23709371c9d4SSatish Balay     if (f == 0)
23719371c9d4SSatish Balay       for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]);
23726858538eSMatthew G. Knepley     switch (faceTypes[f]) {
2373ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
23740ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
23756858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + 0] * dim + d]) - origin[d];
23766858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + 1] * dim + d]) - origin[d];
23776858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + 2] * dim + d]) - origin[d];
23780ec8681fSMatthew G. Knepley       }
23790ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
23806858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
23810ec8681fSMatthew G. Knepley       vsum += vtmp;
23824f25033aSJed Brown       if (centroid) { /* Centroid of OABC = (a+b+c)/4 */
23830ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
23841ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
23850ec8681fSMatthew G. Knepley         }
23860ec8681fSMatthew G. Knepley       }
23870ec8681fSMatthew G. Knepley       break;
2388ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
23899371c9d4SSatish Balay     case DM_POLYTOPE_SEG_PRISM_TENSOR: {
2390793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
2391793a2a13SMatthew G. Knepley 
2392793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
23939371c9d4SSatish Balay       if (isHybrid && f > 1) {
23949371c9d4SSatish Balay         fv[2] = 3;
23959371c9d4SSatish Balay         fv[3] = 2;
23969371c9d4SSatish Balay       }
23970ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
23980ec8681fSMatthew G. Knepley       /* First tet */
23990ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
24006858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[0]] * dim + d]) - origin[d];
24016858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
24026858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
24030ec8681fSMatthew G. Knepley       }
24040ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
24056858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
24060ec8681fSMatthew G. Knepley       vsum += vtmp;
24070ec8681fSMatthew G. Knepley       if (centroid) {
24080ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
24090ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
24100ec8681fSMatthew G. Knepley         }
24110ec8681fSMatthew G. Knepley       }
24120ec8681fSMatthew G. Knepley       /* Second tet */
24130ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
24146858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
24156858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[2]] * dim + d]) - origin[d];
24166858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
24170ec8681fSMatthew G. Knepley       }
24180ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
24196858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
24200ec8681fSMatthew G. Knepley       vsum += vtmp;
24210ec8681fSMatthew G. Knepley       if (centroid) {
24220ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
24230ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
24240ec8681fSMatthew G. Knepley         }
24250ec8681fSMatthew G. Knepley       }
24260ec8681fSMatthew G. Knepley       break;
2427793a2a13SMatthew G. Knepley     }
2428d71ae5a4SJacob Faibussowitsch     default:
2429d71ae5a4SJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %" PetscInt_FMT " of type %s", cone[f], DMPolytopeTypes[ct]);
24300ec8681fSMatthew G. Knepley     }
24316858538eSMatthew G. Knepley     fOff += faceSizes[f];
24320ec8681fSMatthew G. Knepley   }
24336858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
24346858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
24358763be8eSMatthew G. Knepley   if (vol) *vol = PetscAbsReal(vsum);
24369371c9d4SSatish Balay   if (normal)
24379371c9d4SSatish Balay     for (d = 0; d < dim; ++d) normal[d] = 0.0;
24389371c9d4SSatish Balay   if (centroid)
24399371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum * 4) + origin[d];
24403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24410ec8681fSMatthew G. Knepley }
24420ec8681fSMatthew G. Knepley 
2443834e62ceSMatthew G. Knepley /*@C
2444834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
2445834e62ceSMatthew G. Knepley 
2446*20f4b53cSBarry Smith   Collective
2447834e62ceSMatthew G. Knepley 
24484165533cSJose E. Roman   Input Parameters:
2449*20f4b53cSBarry Smith + dm   - the `DMPLEX`
2450834e62ceSMatthew G. Knepley - cell - the cell
2451834e62ceSMatthew G. Knepley 
24524165533cSJose E. Roman   Output Parameters:
2453834e62ceSMatthew G. Knepley + volume   - the cell volume
2454cc08537eSMatthew G. Knepley . centroid - the cell centroid
2455cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
2456834e62ceSMatthew G. Knepley 
2457834e62ceSMatthew G. Knepley   Level: advanced
2458834e62ceSMatthew G. Knepley 
2459*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinateSection()`, `DMGetCoordinates()`
2460834e62ceSMatthew G. Knepley @*/
2461d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2462d71ae5a4SJacob Faibussowitsch {
24630ec8681fSMatthew G. Knepley   PetscInt depth, dim;
2464834e62ceSMatthew G. Knepley 
2465834e62ceSMatthew G. Knepley   PetscFunctionBegin;
24669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24679566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
246808401ef6SPierre Jolivet   PetscCheck(depth == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
24699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetPointDepth(dm, cell, &depth));
2470011ea5d8SMatthew G. Knepley   switch (depth) {
2471d71ae5a4SJacob Faibussowitsch   case 0:
2472d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal));
2473d71ae5a4SJacob Faibussowitsch     break;
2474d71ae5a4SJacob Faibussowitsch   case 1:
2475d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal));
2476d71ae5a4SJacob Faibussowitsch     break;
2477d71ae5a4SJacob Faibussowitsch   case 2:
2478d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal));
2479d71ae5a4SJacob Faibussowitsch     break;
2480d71ae5a4SJacob Faibussowitsch   case 3:
2481d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal));
2482d71ae5a4SJacob Faibussowitsch     break;
2483d71ae5a4SJacob Faibussowitsch   default:
2484d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT " (depth %" PetscInt_FMT ") for element geometry computation", dim, depth);
2485834e62ceSMatthew G. Knepley   }
24863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2487834e62ceSMatthew G. Knepley }
2488113c68e6SMatthew G. Knepley 
2489c501906fSMatthew G. Knepley /*@
2490891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2491891a9168SMatthew G. Knepley 
2492891a9168SMatthew G. Knepley   Input Parameter:
2493*20f4b53cSBarry Smith . dm - The `DMPLEX`
2494891a9168SMatthew G. Knepley 
2495891a9168SMatthew G. Knepley   Output Parameters:
2496*20f4b53cSBarry Smith + cellgeom - A `Vec` of `PetscFVCellGeom` data
2497*20f4b53cSBarry Smith - facegeom - A `Vec` of `PetscFVFaceGeom` data
2498891a9168SMatthew G. Knepley 
2499891a9168SMatthew G. Knepley   Level: developer
2500891a9168SMatthew G. Knepley 
2501*20f4b53cSBarry Smith .seealso: `DMPLEX`, `PetscFVFaceGeom`, `PetscFVCellGeom`
2502891a9168SMatthew G. Knepley @*/
2503d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2504d71ae5a4SJacob Faibussowitsch {
2505113c68e6SMatthew G. Knepley   DM           dmFace, dmCell;
2506113c68e6SMatthew G. Knepley   DMLabel      ghostLabel;
2507113c68e6SMatthew G. Knepley   PetscSection sectionFace, sectionCell;
2508113c68e6SMatthew G. Knepley   PetscSection coordSection;
2509113c68e6SMatthew G. Knepley   Vec          coordinates;
2510113c68e6SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2511113c68e6SMatthew G. Knepley   PetscReal    minradius, gminradius;
2512113c68e6SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2513113c68e6SMatthew G. Knepley 
2514113c68e6SMatthew G. Knepley   PetscFunctionBegin;
25159566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25169566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
25179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
2518113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
25199566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmCell));
25209566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection));
25219566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmCell, coordinates));
25229566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionCell));
25239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
25249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
25259566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd));
25269566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVCellGeom)) / sizeof(PetscScalar))));
25279566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionCell));
25289566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmCell, sectionCell));
25299566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionCell));
25309566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmCell, cellgeom));
2531485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
25329566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*cellgeom, &cgeom));
2533113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2534113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2535113c68e6SMatthew G. Knepley 
25369566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg));
25379566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(cg, 1));
25389566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL));
2539113c68e6SMatthew G. Knepley   }
2540113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
25419566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmFace));
25429566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionFace));
25439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
25449566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionFace, fStart, fEnd));
25459566063dSJacob Faibussowitsch   for (f = fStart; f < fEnd; ++f) PetscCall(PetscSectionSetDof(sectionFace, f, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVFaceGeom)) / sizeof(PetscScalar))));
25469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionFace));
25479566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmFace, sectionFace));
25489566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionFace));
25499566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmFace, facegeom));
25509566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*facegeom, &fgeom));
25519566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2552113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2553113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2554113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2555113c68e6SMatthew G. Knepley     PetscReal        area;
2556412e9a14SMatthew G. Knepley     const PetscInt  *cells;
2557412e9a14SMatthew G. Knepley     PetscInt         ncells, ghost = -1, d, numChildren;
2558113c68e6SMatthew G. Knepley 
25599566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
25609566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
25619566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, f, &cells));
25629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &ncells));
2563412e9a14SMatthew G. Knepley     /* It is possible to get a face with no support when using partition overlap */
2564412e9a14SMatthew G. Knepley     if (!ncells || ghost >= 0 || numChildren) continue;
25659566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, f, fgeom, &fg));
25669566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal));
2567113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2568113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2569113c68e6SMatthew G. Knepley     {
2570113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
2571113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
25720453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2573113c68e6SMatthew G. Knepley 
25749566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL));
2575113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
257606348e87SToby Isaac       if (ncells > 1) {
25779566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR));
2578113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
25799371c9d4SSatish Balay       } else {
258006348e87SToby Isaac         rcentroid = fg->centroid;
258106348e87SToby Isaac       }
25829566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l));
25839566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r));
25840453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2585113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2586113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2587113c68e6SMatthew G. Knepley       }
2588113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
258963a3b9bcSJacob 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]);
259063a3b9bcSJacob 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]);
259163a3b9bcSJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " could not be fixed", f);
2592113c68e6SMatthew G. Knepley       }
2593113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2594113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2595113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2596113c68e6SMatthew G. Knepley       }
259706348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2598113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2599113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2600113c68e6SMatthew G. Knepley       }
2601113c68e6SMatthew G. Knepley     }
2602113c68e6SMatthew G. Knepley   }
26031c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm)));
26049566063dSJacob Faibussowitsch   PetscCall(DMPlexSetMinRadius(dm, gminradius));
2605113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2606113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2607113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2608113c68e6SMatthew G. Knepley     const PetscInt  *cone, *support;
2609113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2610113c68e6SMatthew G. Knepley 
26119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dmCell, c, &coneSize));
261263a3b9bcSJacob Faibussowitsch     PetscCheck(coneSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %" PetscInt_FMT " has cone size %" PetscInt_FMT " != 1", c, coneSize);
26139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dmCell, c, &cone));
26149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmCell, cone[0], &supportSize));
261563a3b9bcSJacob Faibussowitsch     PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " has support size %" PetscInt_FMT " != 2", cone[0], supportSize);
26169566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dmCell, cone[0], &support));
26179566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg));
2618113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2619113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2620113c68e6SMatthew G. Knepley       if (support[s] == c) {
2621640bce14SSatish Balay         PetscFVCellGeom *ci;
2622113c68e6SMatthew G. Knepley         PetscFVCellGeom *cg;
2623113c68e6SMatthew G. Knepley         PetscReal        c2f[3], a;
2624113c68e6SMatthew G. Knepley 
26259566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, support[(s + 1) % 2], cgeom, &ci));
2626113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2627113c68e6SMatthew G. Knepley         a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal) / DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
26289566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg));
2629113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2 * a, fg->normal, ci->centroid, cg->centroid);
2630113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2631113c68e6SMatthew G. Knepley       }
2632113c68e6SMatthew G. Knepley     }
2633113c68e6SMatthew G. Knepley   }
26349566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*facegeom, &fgeom));
26359566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*cellgeom, &cgeom));
26369566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmCell));
26379566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmFace));
26383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2639113c68e6SMatthew G. Knepley }
2640113c68e6SMatthew G. Knepley 
2641113c68e6SMatthew G. Knepley /*@C
2642113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2643113c68e6SMatthew G. Knepley 
2644*20f4b53cSBarry Smith   Not Collective
2645113c68e6SMatthew G. Knepley 
26464165533cSJose E. Roman   Input Parameter:
2647*20f4b53cSBarry Smith . dm - the `DMPLEX`
2648113c68e6SMatthew G. Knepley 
26494165533cSJose E. Roman   Output Parameter:
2650a5b23f4aSJose E. Roman . minradius - the minimum cell radius
2651113c68e6SMatthew G. Knepley 
2652113c68e6SMatthew G. Knepley   Level: developer
2653113c68e6SMatthew G. Knepley 
2654*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMGetCoordinates()`
2655113c68e6SMatthew G. Knepley @*/
2656d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2657d71ae5a4SJacob Faibussowitsch {
2658113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2659113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2660dadcf809SJacob Faibussowitsch   PetscValidRealPointer(minradius, 2);
2661113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex *)dm->data)->minradius;
26623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2663113c68e6SMatthew G. Knepley }
2664113c68e6SMatthew G. Knepley 
2665113c68e6SMatthew G. Knepley /*@C
2666113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2667113c68e6SMatthew G. Knepley 
2668*20f4b53cSBarry Smith   Logically Collective
2669113c68e6SMatthew G. Knepley 
26704165533cSJose E. Roman   Input Parameters:
2671*20f4b53cSBarry Smith + dm - the `DMPLEX`
2672a5b23f4aSJose E. Roman - minradius - the minimum cell radius
2673113c68e6SMatthew G. Knepley 
2674113c68e6SMatthew G. Knepley   Level: developer
2675113c68e6SMatthew G. Knepley 
2676*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMSetCoordinates()`
2677113c68e6SMatthew G. Knepley @*/
2678d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2679d71ae5a4SJacob Faibussowitsch {
2680113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2681113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2682113c68e6SMatthew G. Knepley   ((DM_Plex *)dm->data)->minradius = minradius;
26833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2684113c68e6SMatthew G. Knepley }
2685856ac710SMatthew G. Knepley 
2686d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2687d71ae5a4SJacob Faibussowitsch {
2688856ac710SMatthew G. Knepley   DMLabel      ghostLabel;
2689856ac710SMatthew G. Knepley   PetscScalar *dx, *grad, **gref;
2690856ac710SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2691856ac710SMatthew G. Knepley 
2692856ac710SMatthew G. Knepley   PetscFunctionBegin;
26939566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
26959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2696089217ebSMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
26979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxNumFaces, NULL));
26989566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
26999566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
27009566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2701856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2702856ac710SMatthew G. Knepley     const PetscInt  *faces;
2703856ac710SMatthew G. Knepley     PetscInt         numFaces, usedFaces, f, d;
2704640bce14SSatish Balay     PetscFVCellGeom *cg;
2705856ac710SMatthew G. Knepley     PetscBool        boundary;
2706856ac710SMatthew G. Knepley     PetscInt         ghost;
2707856ac710SMatthew G. Knepley 
2708a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
2709a79418b7SMatt McGurn     PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2710a79418b7SMatt McGurn     if (ghost >= 0) continue;
2711a79418b7SMatt McGurn 
27129566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
27139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &numFaces));
27149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &faces));
271563a3b9bcSJacob 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);
2716856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2717640bce14SSatish Balay       PetscFVCellGeom *cg1;
2718856ac710SMatthew G. Knepley       PetscFVFaceGeom *fg;
2719856ac710SMatthew G. Knepley       const PetscInt  *fcells;
2720856ac710SMatthew G. Knepley       PetscInt         ncell, side;
2721856ac710SMatthew G. Knepley 
27229566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
27239566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2724856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
27259566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, faces[f], &fcells));
2726856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2727856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
27289566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg));
27299566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2730856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces * dim + d] = cg1->centroid[d] - cg->centroid[d];
2731856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */
2732856ac710SMatthew G. Knepley     }
273328b400f6SJacob Faibussowitsch     PetscCheck(usedFaces, PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
27349566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, usedFaces, dx, grad));
2735856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
27369566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
27379566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2738856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2739856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces * dim + d];
2740856ac710SMatthew G. Knepley       ++usedFaces;
2741856ac710SMatthew G. Knepley     }
2742856ac710SMatthew G. Knepley   }
27439566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
27443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2745856ac710SMatthew G. Knepley }
2746856ac710SMatthew G. Knepley 
2747d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2748d71ae5a4SJacob Faibussowitsch {
2749b81db932SToby Isaac   DMLabel      ghostLabel;
2750b81db932SToby Isaac   PetscScalar *dx, *grad, **gref;
2751b81db932SToby Isaac   PetscInt     dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2752b81db932SToby Isaac   PetscSection neighSec;
2753b81db932SToby Isaac   PetscInt(*neighbors)[2];
2754b81db932SToby Isaac   PetscInt *counter;
2755b81db932SToby Isaac 
2756b81db932SToby Isaac   PetscFunctionBegin;
27579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
27589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
27599566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2760485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
27619566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &neighSec));
27629566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(neighSec, cStart, cEndInterior));
27639566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
27649566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2765b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2766b81db932SToby Isaac     const PetscInt *fcells;
2767b81db932SToby Isaac     PetscBool       boundary;
27685bc680faSToby Isaac     PetscInt        ghost = -1;
2769b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2770b81db932SToby Isaac 
27719566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
27729566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
27739566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2774b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
27759566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
277606348e87SToby Isaac     if (numCells == 2) {
27779566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2778b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2779b81db932SToby Isaac         PetscInt cell = fcells[c];
2780b81db932SToby Isaac 
278148a46eb9SPierre Jolivet         if (cell >= cStart && cell < cEndInterior) PetscCall(PetscSectionAddDof(neighSec, cell, 1));
2782b81db932SToby Isaac       }
2783b81db932SToby Isaac     }
278406348e87SToby Isaac   }
27859566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(neighSec));
27869566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetMaxDof(neighSec, &maxNumFaces));
27879566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
2788b81db932SToby Isaac   nStart = 0;
27899566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(neighSec, &nEnd));
27909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1((nEnd - nStart), &neighbors));
27919566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1((cEndInterior - cStart), &counter));
2792b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2793b81db932SToby Isaac     const PetscInt *fcells;
2794b81db932SToby Isaac     PetscBool       boundary;
27955bc680faSToby Isaac     PetscInt        ghost = -1;
2796b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2797b81db932SToby Isaac 
27989566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
27999566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
28009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2801b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
28029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
280306348e87SToby Isaac     if (numCells == 2) {
28049566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2805b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2806b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2807b81db932SToby Isaac 
2808e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
28099566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(neighSec, cell, &off));
2810b81db932SToby Isaac           off += counter[cell - cStart]++;
2811b81db932SToby Isaac           neighbors[off][0] = f;
2812b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2813b81db932SToby Isaac         }
2814b81db932SToby Isaac       }
2815b81db932SToby Isaac     }
281606348e87SToby Isaac   }
28179566063dSJacob Faibussowitsch   PetscCall(PetscFree(counter));
28189566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2819b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2820317218b9SToby Isaac     PetscInt         numFaces, f, d, off, ghost = -1;
2821640bce14SSatish Balay     PetscFVCellGeom *cg;
2822b81db932SToby Isaac 
28239566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
28249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(neighSec, c, &numFaces));
28259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(neighSec, c, &off));
2826a79418b7SMatt McGurn 
2827a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
28289566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2829a79418b7SMatt McGurn     if (ghost >= 0) continue;
2830a79418b7SMatt McGurn 
283163a3b9bcSJacob 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);
2832b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2833640bce14SSatish Balay       PetscFVCellGeom *cg1;
2834b81db932SToby Isaac       PetscFVFaceGeom *fg;
2835b81db932SToby Isaac       const PetscInt  *fcells;
2836b81db932SToby Isaac       PetscInt         ncell, side, nface;
2837b81db932SToby Isaac 
2838b81db932SToby Isaac       nface = neighbors[off + f][0];
2839b81db932SToby Isaac       ncell = neighbors[off + f][1];
28409566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, nface, &fcells));
2841b81db932SToby Isaac       side = (c != fcells[0]);
28429566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, nface, fgeom, &fg));
28439566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2844b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f * dim + d] = cg1->centroid[d] - cg->centroid[d];
2845b81db932SToby Isaac       gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */
2846b81db932SToby Isaac     }
28479566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, numFaces, dx, grad));
2848b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2849b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f * dim + d];
2850b81db932SToby Isaac     }
2851b81db932SToby Isaac   }
28529566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
28539566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&neighSec));
28549566063dSJacob Faibussowitsch   PetscCall(PetscFree(neighbors));
28553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2856b81db932SToby Isaac }
2857b81db932SToby Isaac 
2858856ac710SMatthew G. Knepley /*@
2859856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2860856ac710SMatthew G. Knepley 
2861*20f4b53cSBarry Smith   Collective
2862856ac710SMatthew G. Knepley 
28634165533cSJose E. Roman   Input Parameters:
2864*20f4b53cSBarry Smith + dm  - The `DMPLEX`
2865*20f4b53cSBarry Smith . fvm - The `PetscFV`
2866*20f4b53cSBarry Smith - cellGeometry - The face geometry from `DMPlexComputeCellGeometryFVM()`
2867856ac710SMatthew G. Knepley 
28686b867d5aSJose E. Roman   Input/Output Parameter:
2869*20f4b53cSBarry Smith . faceGeometry - The face geometry from `DMPlexComputeFaceGeometryFVM()`; on output
28706b867d5aSJose E. Roman                  the geometric factors for gradient calculation are inserted
28716b867d5aSJose E. Roman 
28726b867d5aSJose E. Roman   Output Parameter:
2873*20f4b53cSBarry Smith . dmGrad - The `DM` describing the layout of gradient data
2874856ac710SMatthew G. Knepley 
2875856ac710SMatthew G. Knepley   Level: developer
2876856ac710SMatthew G. Knepley 
2877*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexGetFaceGeometryFVM()`, `DMPlexGetCellGeometryFVM()`
2878856ac710SMatthew G. Knepley @*/
2879d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2880d71ae5a4SJacob Faibussowitsch {
2881856ac710SMatthew G. Knepley   DM           dmFace, dmCell;
2882856ac710SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2883b81db932SToby Isaac   PetscSection sectionGrad, parentSection;
2884856ac710SMatthew G. Knepley   PetscInt     dim, pdim, cStart, cEnd, cEndInterior, c;
2885856ac710SMatthew G. Knepley 
2886856ac710SMatthew G. Knepley   PetscFunctionBegin;
28879566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
28889566063dSJacob Faibussowitsch   PetscCall(PetscFVGetNumComponents(fvm, &pdim));
28899566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
28909566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2891856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
28929566063dSJacob Faibussowitsch   PetscCall(VecGetDM(faceGeometry, &dmFace));
28939566063dSJacob Faibussowitsch   PetscCall(VecGetDM(cellGeometry, &dmCell));
28949566063dSJacob Faibussowitsch   PetscCall(VecGetArray(faceGeometry, &fgeom));
28959566063dSJacob Faibussowitsch   PetscCall(VecGetArray(cellGeometry, &cgeom));
28969566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &parentSection, NULL, NULL, NULL, NULL));
2897b81db932SToby Isaac   if (!parentSection) {
28989566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2899b5a3613cSMatthew G. Knepley   } else {
29009566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2901b81db932SToby Isaac   }
29029566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(faceGeometry, &fgeom));
29039566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(cellGeometry, &cgeom));
2904856ac710SMatthew G. Knepley   /* Create storage for gradients */
29059566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, dmGrad));
29069566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionGrad));
29079566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionGrad, cStart, cEnd));
29089566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionGrad, c, pdim * dim));
29099566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionGrad));
29109566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(*dmGrad, sectionGrad));
29119566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionGrad));
29123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2913856ac710SMatthew G. Knepley }
2914b27d5b9eSToby Isaac 
2915c501906fSMatthew G. Knepley /*@
2916c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2917c501906fSMatthew G. Knepley 
2918*20f4b53cSBarry Smith   Collective
2919c501906fSMatthew G. Knepley 
29204165533cSJose E. Roman   Input Parameters:
2921*20f4b53cSBarry Smith + dm  - The `DM`
2922*20f4b53cSBarry Smith - fv  - The `PetscFV`
2923c501906fSMatthew G. Knepley 
2924c501906fSMatthew G. Knepley   Output Parameters:
2925c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2926c501906fSMatthew G. Knepley . faceGeometry - The face geometry
29276b867d5aSJose E. Roman - gradDM       - The gradient matrices
2928c501906fSMatthew G. Knepley 
2929c501906fSMatthew G. Knepley   Level: developer
2930c501906fSMatthew G. Knepley 
2931*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexComputeGeometryFVM()`
2932c501906fSMatthew G. Knepley @*/
2933d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2934d71ae5a4SJacob Faibussowitsch {
2935b27d5b9eSToby Isaac   PetscObject cellgeomobj, facegeomobj;
2936b27d5b9eSToby Isaac 
2937b27d5b9eSToby Isaac   PetscFunctionBegin;
29389566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
2939b27d5b9eSToby Isaac   if (!cellgeomobj) {
2940b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2941b27d5b9eSToby Isaac 
29429566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt));
29439566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_cellgeom_fvm", (PetscObject)cellgeomInt));
29449566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_facegeom_fvm", (PetscObject)facegeomInt));
29459566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cellgeomInt));
29469566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&facegeomInt));
29479566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
2948b27d5b9eSToby Isaac   }
29499566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_facegeom_fvm", &facegeomobj));
2950b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec)cellgeomobj;
2951b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec)facegeomobj;
2952b27d5b9eSToby Isaac   if (gradDM) {
2953b27d5b9eSToby Isaac     PetscObject gradobj;
2954b27d5b9eSToby Isaac     PetscBool   computeGradients;
2955b27d5b9eSToby Isaac 
29569566063dSJacob Faibussowitsch     PetscCall(PetscFVGetComputeGradients(fv, &computeGradients));
2957b27d5b9eSToby Isaac     if (!computeGradients) {
2958b27d5b9eSToby Isaac       *gradDM = NULL;
29593ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
2960b27d5b9eSToby Isaac     }
29619566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
2962b27d5b9eSToby Isaac     if (!gradobj) {
2963b27d5b9eSToby Isaac       DM dmGradInt;
2964b27d5b9eSToby Isaac 
29659566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeGradientFVM(dm, fv, (Vec)facegeomobj, (Vec)cellgeomobj, &dmGradInt));
29669566063dSJacob Faibussowitsch       PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt));
29679566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dmGradInt));
29689566063dSJacob Faibussowitsch       PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
2969b27d5b9eSToby Isaac     }
2970b27d5b9eSToby Isaac     *gradDM = (DM)gradobj;
2971b27d5b9eSToby Isaac   }
29723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2973b27d5b9eSToby Isaac }
2974d6143a4eSToby Isaac 
2975d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess)
2976d71ae5a4SJacob Faibussowitsch {
29779d150b73SToby Isaac   PetscInt l, m;
29789d150b73SToby Isaac 
2979cd345991SToby Isaac   PetscFunctionBeginHot;
29809d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
29819d150b73SToby Isaac     /* invert Jacobian, multiply */
29829d150b73SToby Isaac     PetscScalar det, idet;
29839d150b73SToby Isaac 
29849d150b73SToby Isaac     switch (dimR) {
2985d71ae5a4SJacob Faibussowitsch     case 1:
2986d71ae5a4SJacob Faibussowitsch       invJ[0] = 1. / J[0];
2987d71ae5a4SJacob Faibussowitsch       break;
29889d150b73SToby Isaac     case 2:
29899d150b73SToby Isaac       det     = J[0] * J[3] - J[1] * J[2];
29909d150b73SToby Isaac       idet    = 1. / det;
29919d150b73SToby Isaac       invJ[0] = J[3] * idet;
29929d150b73SToby Isaac       invJ[1] = -J[1] * idet;
29939d150b73SToby Isaac       invJ[2] = -J[2] * idet;
29949d150b73SToby Isaac       invJ[3] = J[0] * idet;
29959d150b73SToby Isaac       break;
29969371c9d4SSatish Balay     case 3: {
29979d150b73SToby Isaac       invJ[0] = J[4] * J[8] - J[5] * J[7];
29989d150b73SToby Isaac       invJ[1] = J[2] * J[7] - J[1] * J[8];
29999d150b73SToby Isaac       invJ[2] = J[1] * J[5] - J[2] * J[4];
30009d150b73SToby Isaac       det     = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
30019d150b73SToby Isaac       idet    = 1. / det;
30029d150b73SToby Isaac       invJ[0] *= idet;
30039d150b73SToby Isaac       invJ[1] *= idet;
30049d150b73SToby Isaac       invJ[2] *= idet;
30059d150b73SToby Isaac       invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]);
30069d150b73SToby Isaac       invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]);
30079d150b73SToby Isaac       invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]);
30089d150b73SToby Isaac       invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]);
30099d150b73SToby Isaac       invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]);
30109d150b73SToby Isaac       invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]);
30119371c9d4SSatish Balay     } break;
30129d150b73SToby Isaac     }
30139d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
3014ad540459SPierre Jolivet       for (m = 0; m < dimC; m++) guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
30159d150b73SToby Isaac     }
30169d150b73SToby Isaac   } else {
30179d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
30189d150b73SToby Isaac     char transpose = 'C';
30199d150b73SToby Isaac #else
30209d150b73SToby Isaac     char transpose = 'T';
30219d150b73SToby Isaac #endif
30229d150b73SToby Isaac     PetscBLASInt m        = dimR;
30239d150b73SToby Isaac     PetscBLASInt n        = dimC;
30249d150b73SToby Isaac     PetscBLASInt one      = 1;
30259d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
30269d150b73SToby Isaac 
3027ad540459SPierre Jolivet     for (l = 0; l < dimC; l++) invJ[l] = resNeg[l];
30289d150b73SToby Isaac 
3029792fecdfSBarry Smith     PetscCallBLAS("LAPACKgels", LAPACKgels_(&transpose, &m, &n, &one, J, &m, invJ, &n, work, &worksize, &info));
303008401ef6SPierre Jolivet     PetscCheck(info == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Bad argument to GELS");
30319d150b73SToby Isaac 
3032ad540459SPierre Jolivet     for (l = 0; l < dimR; l++) guess[l] += PetscRealPart(invJ[l]);
30339d150b73SToby Isaac   }
30343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30359d150b73SToby Isaac }
30369d150b73SToby Isaac 
3037d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
3038d71ae5a4SJacob Faibussowitsch {
3039c0cbe899SToby Isaac   PetscInt     coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
30409d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
30419d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
30429d150b73SToby Isaac   PetscScalar *J, *invJ, *work;
30439d150b73SToby Isaac 
30449d150b73SToby Isaac   PetscFunctionBegin;
30459d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30469566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
30471dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
30489566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
30499566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
30509d150b73SToby Isaac   cellCoords = &cellData[0];
30519d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
30529d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
30539d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
30549d150b73SToby Isaac   invJ       = &J[dimR * dimC];
30559d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
30569d150b73SToby Isaac   if (dimR == 2) {
30579d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
30589d150b73SToby Isaac 
30599d150b73SToby Isaac     for (i = 0; i < 4; i++) {
30609d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
30619d150b73SToby Isaac 
3062ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
30639d150b73SToby Isaac     }
30649d150b73SToby Isaac   } else if (dimR == 3) {
30659d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
30669d150b73SToby Isaac 
30679d150b73SToby Isaac     for (i = 0; i < 8; i++) {
30689d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
30699d150b73SToby Isaac 
3070ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
30719d150b73SToby Isaac     }
30729d150b73SToby Isaac   } else {
3073ad540459SPierre Jolivet     for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]);
30749d150b73SToby Isaac   }
30759d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
30769d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
30779d150b73SToby Isaac     PetscReal *swap;
30789d150b73SToby Isaac 
30799d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
30809d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
30819d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
30829d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
30839d150b73SToby Isaac       }
30849d150b73SToby Isaac     }
30859d150b73SToby Isaac 
30869d150b73SToby Isaac     if (i < dimR - 1) {
30879d150b73SToby Isaac       swap       = cellCoeffs;
30889d150b73SToby Isaac       cellCoeffs = cellCoords;
30899d150b73SToby Isaac       cellCoords = swap;
30909d150b73SToby Isaac     }
30919d150b73SToby Isaac   }
30929566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(refCoords, numPoints * dimR));
30939d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
30949d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
30959d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
30969d150b73SToby Isaac 
30979d150b73SToby Isaac       /* compute -residual and Jacobian */
3098ad540459SPierre Jolivet       for (k = 0; k < dimC; k++) resNeg[k] = realCoords[dimC * j + k];
3099ad540459SPierre Jolivet       for (k = 0; k < dimC * dimR; k++) J[k] = 0.;
31009d150b73SToby Isaac       for (k = 0; k < numV; k++) {
31019d150b73SToby Isaac         PetscReal extCoord = 1.;
31029d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
31039d150b73SToby Isaac           PetscReal coord = guess[l];
31049d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
31059d150b73SToby Isaac 
31069d150b73SToby Isaac           extCoord *= dep * coord + !dep;
31079d150b73SToby Isaac           extJ[l] = dep;
31089d150b73SToby Isaac 
31099d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
31109d150b73SToby Isaac             PetscReal coord = guess[m];
31119d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
31129d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
31139d150b73SToby Isaac 
31149d150b73SToby Isaac             extJ[l] *= mult;
31159d150b73SToby Isaac           }
31169d150b73SToby Isaac         }
31179d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
31189d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
31199d150b73SToby Isaac 
31209d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
3121ad540459SPierre Jolivet           for (m = 0; m < dimR; m++) J[dimR * l + m] += coeff * extJ[m];
31229d150b73SToby Isaac         }
31239d150b73SToby Isaac       }
312476bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
31250611203eSToby Isaac         PetscReal maxAbs = 0.;
31260611203eSToby Isaac 
3127ad540459SPierre Jolivet         for (l = 0; l < dimC; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l]));
312863a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
31290611203eSToby Isaac       }
31309d150b73SToby Isaac 
31319566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(dimC, dimR, J, invJ, work, resNeg, guess));
31329d150b73SToby Isaac     }
31339d150b73SToby Isaac   }
31349566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
31359566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
31369566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
31373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31389d150b73SToby Isaac }
31399d150b73SToby Isaac 
3140d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
3141d71ae5a4SJacob Faibussowitsch {
31429d150b73SToby Isaac   PetscInt     coordSize, i, j, k, l, numV = (1 << dimR);
31439d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
31449d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs;
31459d150b73SToby Isaac 
31469d150b73SToby Isaac   PetscFunctionBegin;
31479d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31489566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
31491dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
31509566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
31519d150b73SToby Isaac   cellCoords = &cellData[0];
31529d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
31539d150b73SToby Isaac   if (dimR == 2) {
31549d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
31559d150b73SToby Isaac 
31569d150b73SToby Isaac     for (i = 0; i < 4; i++) {
31579d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31589d150b73SToby Isaac 
3159ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
31609d150b73SToby Isaac     }
31619d150b73SToby Isaac   } else if (dimR == 3) {
31629d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
31639d150b73SToby Isaac 
31649d150b73SToby Isaac     for (i = 0; i < 8; i++) {
31659d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31669d150b73SToby Isaac 
3167ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
31689d150b73SToby Isaac     }
31699d150b73SToby Isaac   } else {
3170ad540459SPierre Jolivet     for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]);
31719d150b73SToby Isaac   }
31729d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
31739d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
31749d150b73SToby Isaac     PetscReal *swap;
31759d150b73SToby Isaac 
31769d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
31779d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
31789d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
31799d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
31809d150b73SToby Isaac       }
31819d150b73SToby Isaac     }
31829d150b73SToby Isaac 
31839d150b73SToby Isaac     if (i < dimR - 1) {
31849d150b73SToby Isaac       swap       = cellCoeffs;
31859d150b73SToby Isaac       cellCoeffs = cellCoords;
31869d150b73SToby Isaac       cellCoords = swap;
31879d150b73SToby Isaac     }
31889d150b73SToby Isaac   }
31899566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(realCoords, numPoints * dimC));
31909d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
31919d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
31929d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
31939d150b73SToby Isaac 
31949d150b73SToby Isaac     for (k = 0; k < numV; k++) {
31959d150b73SToby Isaac       PetscReal extCoord = 1.;
31969d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
31979d150b73SToby Isaac         PetscReal coord = guess[l];
31989d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
31999d150b73SToby Isaac 
32009d150b73SToby Isaac         extCoord *= dep * coord + !dep;
32019d150b73SToby Isaac       }
32029d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
32039d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
32049d150b73SToby Isaac 
32059d150b73SToby Isaac         mapped[l] += coeff * extCoord;
32069d150b73SToby Isaac       }
32079d150b73SToby Isaac     }
32089d150b73SToby Isaac   }
32099566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
32109566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
32113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32129d150b73SToby Isaac }
32139d150b73SToby Isaac 
32149c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
3215d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
3216d71ae5a4SJacob Faibussowitsch {
32179c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
3218c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3219c6e120d1SToby Isaac   PetscReal   *invV, *modes;
3220c6e120d1SToby Isaac   PetscReal   *B, *D, *resNeg;
3221c6e120d1SToby Isaac   PetscScalar *J, *invJ, *work;
32229d150b73SToby Isaac 
32239d150b73SToby Isaac   PetscFunctionBegin;
32249566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
32259566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
322663a3b9bcSJacob 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);
32279566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32289d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
32299566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
32309d150b73SToby Isaac   invV = fe->invV;
3231012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3232012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3233ad540459SPierre Jolivet     for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
32349d150b73SToby Isaac   }
32359566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
32369c3cf19fSMatthew G. Knepley   D      = &B[pdim * Nc];
32379c3cf19fSMatthew G. Knepley   resNeg = &D[pdim * Nc * dimR];
32389566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
32399c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
32409c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
3241ad540459SPierre Jolivet   for (i = 0; i < numPoints * dimR; i++) refCoords[i] = 0.;
32429d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32439b1f03cbSToby 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 */
32449d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
32459566063dSJacob Faibussowitsch       PetscCall(PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL));
3246ad540459SPierre Jolivet       for (k = 0; k < Nc; k++) resNeg[k] = realCoords[j * Nc + k];
3247ad540459SPierre Jolivet       for (k = 0; k < Nc * dimR; k++) J[k] = 0.;
32489c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
32499c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
3250012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
3251ad540459SPierre Jolivet           for (m = 0; m < dimR; m++) J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
32529d150b73SToby Isaac         }
32539d150b73SToby Isaac       }
325476bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
32550611203eSToby Isaac         PetscReal maxAbs = 0.;
32560611203eSToby Isaac 
3257ad540459SPierre Jolivet         for (l = 0; l < Nc; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l]));
325863a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
32590611203eSToby Isaac       }
32609566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(Nc, dimR, J, invJ, work, resNeg, guess));
32619d150b73SToby Isaac     }
32629d150b73SToby Isaac   }
32639566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
32649566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
32659566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
32669566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32689d150b73SToby Isaac }
32699d150b73SToby Isaac 
32709c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
3271d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
3272d71ae5a4SJacob Faibussowitsch {
32739c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, coordSize;
3274c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3275c6e120d1SToby Isaac   PetscReal   *invV, *modes;
32769d150b73SToby Isaac   PetscReal   *B;
32779d150b73SToby Isaac 
32789d150b73SToby Isaac   PetscFunctionBegin;
32799566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
32809566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
328163a3b9bcSJacob 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);
32829566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32839d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
32849566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
32859d150b73SToby Isaac   invV = fe->invV;
3286012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3287012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3288ad540459SPierre Jolivet     for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
32899d150b73SToby Isaac   }
32909566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
32919566063dSJacob Faibussowitsch   PetscCall(PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL));
3292ad540459SPierre Jolivet   for (i = 0; i < numPoints * Nc; i++) realCoords[i] = 0.;
32939d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32949c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
32959d150b73SToby Isaac 
32969c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
3297ad540459SPierre Jolivet       for (l = 0; l < Nc; l++) mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
32989d150b73SToby Isaac     }
32999d150b73SToby Isaac   }
33009566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
33019566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
33029566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
33033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
33049d150b73SToby Isaac }
33059d150b73SToby Isaac 
3306d6143a4eSToby Isaac /*@
3307d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
3308d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
3309d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
3310d6143a4eSToby Isaac 
3311*20f4b53cSBarry Smith   Not Collective
3312d6143a4eSToby Isaac 
3313d6143a4eSToby Isaac   Input Parameters:
3314*20f4b53cSBarry Smith + dm         - The mesh, with coordinate maps defined either by a `PetscDS` for the coordinate `DM` (see `DMGetCoordinateDM()`) or
3315d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
3316d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
3317d6143a4eSToby Isaac . cell       - the cell whose map is used.
3318d6143a4eSToby Isaac . numPoints  - the number of points to locate
3319*20f4b53cSBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see `DMGetCoordinateDim()`)
3320d6143a4eSToby Isaac 
3321d6143a4eSToby Isaac   Output Parameters:
3322*20f4b53cSBarry Smith . refCoords  - (`numPoints` x `dimension`) array of reference coordinates (see `DMGetDimension()`)
33231b266c99SBarry Smith 
33241b266c99SBarry Smith   Level: intermediate
332573c9229bSMatthew Knepley 
3326*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexReferenceToCoordinates()`
3327d6143a4eSToby Isaac @*/
3328d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
3329d71ae5a4SJacob Faibussowitsch {
3330485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
33319d150b73SToby Isaac   DM       coordDM = NULL;
33329d150b73SToby Isaac   Vec      coords;
33339d150b73SToby Isaac   PetscFE  fe = NULL;
33349d150b73SToby Isaac 
3335d6143a4eSToby Isaac   PetscFunctionBegin;
33369d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33379566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
33389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
33393ba16761SJacob Faibussowitsch   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(PETSC_SUCCESS);
33409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
33419566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
33429566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
33439d150b73SToby Isaac   if (coordDM) {
33449d150b73SToby Isaac     PetscInt coordFields;
33459d150b73SToby Isaac 
33469566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
33479d150b73SToby Isaac     if (coordFields) {
33489d150b73SToby Isaac       PetscClassId id;
33499d150b73SToby Isaac       PetscObject  disc;
33509d150b73SToby Isaac 
33519566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
33529566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
3353ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
33549d150b73SToby Isaac     }
33559d150b73SToby Isaac   }
33569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
33571dca8a05SBarry 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);
33589d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
33599d150b73SToby Isaac     PetscInt  coneSize;
33609d150b73SToby Isaac     PetscBool isSimplex, isTensor;
33619d150b73SToby Isaac 
33629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
33639d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
33649d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
33659d150b73SToby Isaac     if (isSimplex) {
33669d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
33679d150b73SToby Isaac 
33689566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
33699d150b73SToby Isaac       J    = &v0[dimC];
33709d150b73SToby Isaac       invJ = &J[dimC * dimC];
33719566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ));
33729d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3373c330f8ffSToby Isaac         const PetscReal x0[3] = {-1., -1., -1.};
3374c330f8ffSToby Isaac 
3375c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
33769d150b73SToby Isaac       }
33779566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
33789d150b73SToby Isaac     } else if (isTensor) {
33799566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
338063a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
33819d150b73SToby Isaac   } else {
33829566063dSJacob Faibussowitsch     PetscCall(DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
33839d150b73SToby Isaac   }
33843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
33859d150b73SToby Isaac }
33869d150b73SToby Isaac 
33879d150b73SToby Isaac /*@
33889d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
33899d150b73SToby Isaac 
3390*20f4b53cSBarry Smith   Not Collective
33919d150b73SToby Isaac 
33929d150b73SToby Isaac   Input Parameters:
33939d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
33949d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
33959d150b73SToby Isaac                as a multilinear map for tensor-product elements
33969d150b73SToby Isaac . cell       - the cell whose map is used.
33979d150b73SToby Isaac . numPoints  - the number of points to locate
3398a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
33999d150b73SToby Isaac 
34009d150b73SToby Isaac   Output Parameters:
34019d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
34021b266c99SBarry Smith 
34031b266c99SBarry Smith    Level: intermediate
340473c9229bSMatthew Knepley 
3405db781477SPatrick Sanan .seealso: `DMPlexCoordinatesToReference()`
34069d150b73SToby Isaac @*/
3407d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
3408d71ae5a4SJacob Faibussowitsch {
3409485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
34109d150b73SToby Isaac   DM       coordDM = NULL;
34119d150b73SToby Isaac   Vec      coords;
34129d150b73SToby Isaac   PetscFE  fe = NULL;
34139d150b73SToby Isaac 
34149d150b73SToby Isaac   PetscFunctionBegin;
34159d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34169566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
34179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
34183ba16761SJacob Faibussowitsch   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(PETSC_SUCCESS);
34199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
34209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
34219566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
34229d150b73SToby Isaac   if (coordDM) {
34239d150b73SToby Isaac     PetscInt coordFields;
34249d150b73SToby Isaac 
34259566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
34269d150b73SToby Isaac     if (coordFields) {
34279d150b73SToby Isaac       PetscClassId id;
34289d150b73SToby Isaac       PetscObject  disc;
34299d150b73SToby Isaac 
34309566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
34319566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
3432ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
34339d150b73SToby Isaac     }
34349d150b73SToby Isaac   }
34359566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
34361dca8a05SBarry 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);
34379d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
34389d150b73SToby Isaac     PetscInt  coneSize;
34399d150b73SToby Isaac     PetscBool isSimplex, isTensor;
34409d150b73SToby Isaac 
34419566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
34429d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
34439d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
34449d150b73SToby Isaac     if (isSimplex) {
34459d150b73SToby Isaac       PetscReal detJ, *v0, *J;
34469d150b73SToby Isaac 
34479566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34489d150b73SToby Isaac       J = &v0[dimC];
34499566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ));
3450c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3451c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3452c330f8ffSToby Isaac 
3453c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
34549d150b73SToby Isaac       }
34559566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34569d150b73SToby Isaac     } else if (isTensor) {
34579566063dSJacob Faibussowitsch       PetscCall(DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
345863a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
34599d150b73SToby Isaac   } else {
34609566063dSJacob Faibussowitsch     PetscCall(DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
34619d150b73SToby Isaac   }
34623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3463d6143a4eSToby Isaac }
34640139fca9SMatthew G. Knepley 
34650139fca9SMatthew G. Knepley /*@C
34660139fca9SMatthew G. Knepley   DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates.
34670139fca9SMatthew G. Knepley 
3468*20f4b53cSBarry Smith   Not Collective
34690139fca9SMatthew G. Knepley 
34700139fca9SMatthew G. Knepley   Input Parameters:
34710139fca9SMatthew G. Knepley + dm      - The DM
34720139fca9SMatthew G. Knepley . time    - The time
34730139fca9SMatthew G. Knepley - func    - The function transforming current coordinates to new coordaintes
34740139fca9SMatthew G. Knepley 
3475*20f4b53cSBarry Smith    Calling sequence of `func`:
3476*20f4b53cSBarry Smith .vb
3477*20f4b53cSBarry Smith    void func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
3478*20f4b53cSBarry Smith              const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
3479*20f4b53cSBarry Smith              const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
3480*20f4b53cSBarry Smith              PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
3481*20f4b53cSBarry Smith .ve
34820139fca9SMatthew G. Knepley +  dim          - The spatial dimension
34830139fca9SMatthew G. Knepley .  Nf           - The number of input fields (here 1)
34840139fca9SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
34850139fca9SMatthew G. Knepley .  uOff         - The offset of the coordinates in u[] (here 0)
34860139fca9SMatthew G. Knepley .  uOff_x       - The offset of the coordinates in u_x[] (here 0)
34870139fca9SMatthew G. Knepley .  u            - The coordinate values at this point in space
3488*20f4b53cSBarry Smith .  u_t          - The coordinate time derivative at this point in space (here `NULL`)
34890139fca9SMatthew G. Knepley .  u_x          - The coordinate derivatives at this point in space
34900139fca9SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
34910139fca9SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
34920139fca9SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
3493*20f4b53cSBarry Smith .  a_t          - The auxiliary field time derivative at this point in space (or `NULL`)
34940139fca9SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
34950139fca9SMatthew G. Knepley .  t            - The current time
34960139fca9SMatthew G. Knepley .  x            - The coordinates of this point (here not used)
34970139fca9SMatthew G. Knepley .  numConstants - The number of constants
34980139fca9SMatthew G. Knepley .  constants    - The value of each constant
34990139fca9SMatthew G. Knepley -  f            - The new coordinates at this point in space
35000139fca9SMatthew G. Knepley 
35010139fca9SMatthew G. Knepley   Level: intermediate
35020139fca9SMatthew G. Knepley 
3503db781477SPatrick Sanan .seealso: `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMProjectFieldLocal()`, `DMProjectFieldLabelLocal()`
35040139fca9SMatthew G. Knepley @*/
3505d71ae5a4SJacob 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[]))
3506d71ae5a4SJacob Faibussowitsch {
35070139fca9SMatthew G. Knepley   DM      cdm;
35088bf1a49fSMatthew G. Knepley   DMField cf;
35090139fca9SMatthew G. Knepley   Vec     lCoords, tmpCoords;
35100139fca9SMatthew G. Knepley 
35110139fca9SMatthew G. Knepley   PetscFunctionBegin;
35129566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
35139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
35149566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(cdm, &tmpCoords));
35159566063dSJacob Faibussowitsch   PetscCall(VecCopy(lCoords, tmpCoords));
35168bf1a49fSMatthew G. Knepley   /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */
35179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateField(dm, &cf));
35186858538eSMatthew G. Knepley   cdm->coordinates[0].field = cf;
35199566063dSJacob Faibussowitsch   PetscCall(DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords));
35206858538eSMatthew G. Knepley   cdm->coordinates[0].field = NULL;
35219566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(cdm, &tmpCoords));
35229566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, lCoords));
35233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35240139fca9SMatthew G. Knepley }
35250139fca9SMatthew G. Knepley 
35260139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z,
35270139fca9SMatthew G. Knepley   / 1  0  m_0 \
35280139fca9SMatthew G. Knepley   | 0  1  m_1 |
35290139fca9SMatthew G. Knepley   \ 0  0   1  /
35300139fca9SMatthew G. Knepley */
3531d71ae5a4SJacob 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[])
3532d71ae5a4SJacob Faibussowitsch {
35330139fca9SMatthew G. Knepley   const PetscInt Nc = uOff[1] - uOff[0];
3534c1f1bd54SMatthew G. Knepley   const PetscInt ax = (PetscInt)PetscRealPart(constants[0]);
35350139fca9SMatthew G. Knepley   PetscInt       c;
35360139fca9SMatthew G. Knepley 
3537ad540459SPierre Jolivet   for (c = 0; c < Nc; ++c) coords[c] = u[c] + constants[c + 1] * u[ax];
35380139fca9SMatthew G. Knepley }
35390139fca9SMatthew G. Knepley 
35400139fca9SMatthew G. Knepley /*@C
35410139fca9SMatthew G. Knepley   DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates.
35420139fca9SMatthew G. Knepley 
3543*20f4b53cSBarry Smith   Not Collective
35440139fca9SMatthew G. Knepley 
35450139fca9SMatthew G. Knepley   Input Parameters:
3546*20f4b53cSBarry Smith + dm          - The `DMPLEX`
35473ee9839eSMatthew G. Knepley . direction   - The shear coordinate direction, e.g. 0 is the x-axis
35480139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction
35490139fca9SMatthew G. Knepley 
35500139fca9SMatthew G. Knepley   Level: intermediate
35510139fca9SMatthew G. Knepley 
3552*20f4b53cSBarry Smith .seealso: `DMPLEX`, `DMPlexRemapGeometry()`
35530139fca9SMatthew G. Knepley @*/
3554d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[])
3555d71ae5a4SJacob Faibussowitsch {
35560139fca9SMatthew G. Knepley   DM             cdm;
35570139fca9SMatthew G. Knepley   PetscDS        cds;
35580139fca9SMatthew G. Knepley   PetscObject    obj;
35590139fca9SMatthew G. Knepley   PetscClassId   id;
35600139fca9SMatthew G. Knepley   PetscScalar   *moduli;
35613ee9839eSMatthew G. Knepley   const PetscInt dir = (PetscInt)direction;
35620139fca9SMatthew G. Knepley   PetscInt       dE, d, e;
35630139fca9SMatthew G. Knepley 
35640139fca9SMatthew G. Knepley   PetscFunctionBegin;
35659566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
35669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
35679566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(dE + 1, &moduli));
35680139fca9SMatthew G. Knepley   moduli[0] = dir;
3569cdaaecf7SMatthew G. Knepley   for (d = 0, e = 0; d < dE; ++d) moduli[d + 1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0);
35709566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
35719566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
35729566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(obj, &id));
35730139fca9SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
35740139fca9SMatthew G. Knepley     Vec          lCoords;
35750139fca9SMatthew G. Knepley     PetscSection cSection;
35760139fca9SMatthew G. Knepley     PetscScalar *coords;
35770139fca9SMatthew G. Knepley     PetscInt     vStart, vEnd, v;
35780139fca9SMatthew G. Knepley 
35799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
35809566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cSection));
35819566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
35829566063dSJacob Faibussowitsch     PetscCall(VecGetArray(lCoords, &coords));
35830139fca9SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
35840139fca9SMatthew G. Knepley       PetscReal ds;
35850139fca9SMatthew G. Knepley       PetscInt  off, c;
35860139fca9SMatthew G. Knepley 
35879566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(cSection, v, &off));
35880139fca9SMatthew G. Knepley       ds = PetscRealPart(coords[off + dir]);
35890139fca9SMatthew G. Knepley       for (c = 0; c < dE; ++c) coords[off + c] += moduli[c] * ds;
35900139fca9SMatthew G. Knepley     }
35919566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(lCoords, &coords));
35920139fca9SMatthew G. Knepley   } else {
35939566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, dE + 1, moduli));
35949566063dSJacob Faibussowitsch     PetscCall(DMPlexRemapGeometry(dm, 0.0, f0_shear));
35950139fca9SMatthew G. Knepley   }
35969566063dSJacob Faibussowitsch   PetscCall(PetscFree(moduli));
35973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35980139fca9SMatthew G. Knepley }
3599