xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 3ba1676111f5c958fe6c2729b46ca4d523958bb3)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>  /*I      "petscdmplex.h"   I*/
29d150b73SToby Isaac #include <petsc/private/petscfeimpl.h> /*I      "petscfe.h"       I*/
39d150b73SToby Isaac #include <petscblaslapack.h>
4af74b616SDave May #include <petsctime.h>
5ccd2543fSMatthew G Knepley 
63985bb02SVaclav Hapla /*@
73985bb02SVaclav Hapla   DMPlexFindVertices - Try to find DAG points based on their coordinates.
83985bb02SVaclav Hapla 
93985bb02SVaclav Hapla   Not Collective (provided DMGetCoordinatesLocalSetUp() has been called already)
103985bb02SVaclav Hapla 
113985bb02SVaclav Hapla   Input Parameters:
123985bb02SVaclav Hapla + dm - The DMPlex object
13d3e1f4ccSVaclav Hapla . coordinates - The Vec of coordinates of the sought points
143985bb02SVaclav Hapla - eps - The tolerance or PETSC_DEFAULT
153985bb02SVaclav Hapla 
163985bb02SVaclav Hapla   Output Parameters:
17d3e1f4ccSVaclav Hapla . points - The IS of found DAG points or -1
183985bb02SVaclav Hapla 
193985bb02SVaclav Hapla   Level: intermediate
203985bb02SVaclav Hapla 
213985bb02SVaclav Hapla   Notes:
22d3e1f4ccSVaclav Hapla   The length of Vec coordinates must be npoints * dim where dim is the spatial dimension returned by DMGetCoordinateDim() and npoints is the number of sought points.
233985bb02SVaclav Hapla 
24d3e1f4ccSVaclav Hapla   The output IS is living on PETSC_COMM_SELF and its length is npoints.
25d3e1f4ccSVaclav Hapla   Each rank does the search independently.
26d3e1f4ccSVaclav Hapla   If this rank's local DMPlex portion contains the DAG point corresponding to the i-th tuple of coordinates, the i-th entry of the output IS is set to that DAG point, otherwise to -1.
273985bb02SVaclav Hapla 
28d3e1f4ccSVaclav Hapla   The output IS must be destroyed by user.
293985bb02SVaclav Hapla 
303985bb02SVaclav Hapla   The tolerance is interpreted as the maximum Euclidean (L2) distance of the sought point from the specified coordinates.
313985bb02SVaclav Hapla 
32d3e1f4ccSVaclav Hapla   Complexity of this function is currently O(mn) with m number of vertices to find and n number of vertices in the local mesh. This could probably be improved if needed.
33335ef845SVaclav Hapla 
34db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMGetCoordinatesLocal()`
353985bb02SVaclav Hapla @*/
36d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points)
37d71ae5a4SJacob Faibussowitsch {
3837900f7dSMatthew G. Knepley   PetscInt           c, cdim, i, j, o, p, vStart, vEnd;
39d3e1f4ccSVaclav Hapla   PetscInt           npoints;
40d3e1f4ccSVaclav Hapla   const PetscScalar *coord;
413985bb02SVaclav Hapla   Vec                allCoordsVec;
423985bb02SVaclav Hapla   const PetscScalar *allCoords;
43d3e1f4ccSVaclav Hapla   PetscInt          *dagPoints;
443985bb02SVaclav Hapla 
453985bb02SVaclav Hapla   PetscFunctionBegin;
463985bb02SVaclav Hapla   if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON;
479566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
48d3e1f4ccSVaclav Hapla   {
49d3e1f4ccSVaclav Hapla     PetscInt n;
50d3e1f4ccSVaclav Hapla 
519566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(coordinates, &n));
5263a3b9bcSJacob Faibussowitsch     PetscCheck(n % cdim == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Given coordinates Vec has local length %" PetscInt_FMT " not divisible by coordinate dimension %" PetscInt_FMT " of given DM", n, cdim);
53d3e1f4ccSVaclav Hapla     npoints = n / cdim;
54d3e1f4ccSVaclav Hapla   }
559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &allCoordsVec));
569566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(allCoordsVec, &allCoords));
579566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coord));
589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
5976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
60335ef845SVaclav Hapla     /* check coordinate section is consistent with DM dimension */
61335ef845SVaclav Hapla     PetscSection cs;
62335ef845SVaclav Hapla     PetscInt     ndof;
63335ef845SVaclav Hapla 
649566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cs));
653985bb02SVaclav Hapla     for (p = vStart; p < vEnd; p++) {
669566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cs, p, &ndof));
6763a3b9bcSJacob Faibussowitsch       PetscCheck(ndof == cdim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %" PetscInt_FMT ": ndof = %" PetscInt_FMT " != %" PetscInt_FMT " = cdim", p, ndof, cdim);
68335ef845SVaclav Hapla     }
69335ef845SVaclav Hapla   }
709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npoints, &dagPoints));
71eca9f518SVaclav Hapla   if (eps == 0.0) {
7237900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
73eca9f518SVaclav Hapla       dagPoints[i] = -1;
7437900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
7537900f7dSMatthew G. Knepley         for (c = 0; c < cdim; c++) {
76d3e1f4ccSVaclav Hapla           if (coord[j + c] != allCoords[o + c]) break;
77eca9f518SVaclav Hapla         }
7837900f7dSMatthew G. Knepley         if (c == cdim) {
79eca9f518SVaclav Hapla           dagPoints[i] = p;
80eca9f518SVaclav Hapla           break;
81eca9f518SVaclav Hapla         }
82eca9f518SVaclav Hapla       }
83eca9f518SVaclav Hapla     }
84d3e1f4ccSVaclav Hapla   } else {
8537900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
86d3e1f4ccSVaclav Hapla       PetscReal norm;
87d3e1f4ccSVaclav Hapla 
88335ef845SVaclav Hapla       dagPoints[i] = -1;
8937900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
903985bb02SVaclav Hapla         norm = 0.0;
91ad540459SPierre Jolivet         for (c = 0; c < cdim; c++) norm += PetscRealPart(PetscSqr(coord[j + c] - allCoords[o + c]));
923985bb02SVaclav Hapla         norm = PetscSqrtReal(norm);
933985bb02SVaclav Hapla         if (norm <= eps) {
943985bb02SVaclav Hapla           dagPoints[i] = p;
953985bb02SVaclav Hapla           break;
963985bb02SVaclav Hapla         }
973985bb02SVaclav Hapla       }
983985bb02SVaclav Hapla     }
99d3e1f4ccSVaclav Hapla   }
1009566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(allCoordsVec, &allCoords));
1019566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coord));
1029566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points));
103*3ba16761SJacob 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   }
137*3ba16761SJacob 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   }
198*3ba16761SJacob 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;
214*3ba16761SJacob 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;
233*3ba16761SJacob 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];
258*3ba16761SJacob 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));
288*3ba16761SJacob 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;
309*3ba16761SJacob 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));
356*3ba16761SJacob 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]);
366*3ba16761SJacob 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));
374*3ba16761SJacob 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   }
386*3ba16761SJacob 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 
39262a38674SMatthew G. Knepley   Not collective
39362a38674SMatthew G. Knepley 
39462a38674SMatthew G. Knepley   Input Parameters:
39562a38674SMatthew G. Knepley + box - The grid hash object
39662a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
39762a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
39862a38674SMatthew G. Knepley 
39962a38674SMatthew G. Knepley   Level: developer
40062a38674SMatthew G. Knepley 
401db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`
40262a38674SMatthew G. Knepley */
403d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
404d71ae5a4SJacob Faibussowitsch {
405c4eade1cSMatthew G. Knepley   PetscInt d;
406c4eade1cSMatthew G. Knepley 
407c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
408c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
409c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
410c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
411c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
412c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d] / h[d]);
413c4eade1cSMatthew G. Knepley     } else {
414c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
415c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d] / n[d];
416c4eade1cSMatthew G. Knepley     }
417c4eade1cSMatthew G. Knepley   }
418*3ba16761SJacob 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 
42462a38674SMatthew G. Knepley   Not collective
42562a38674SMatthew G. Knepley 
42662a38674SMatthew G. Knepley   Input Parameters:
42762a38674SMatthew G. Knepley + box       - The grid hash object
42862a38674SMatthew G. Knepley . numPoints - The number of input points
42962a38674SMatthew G. Knepley - points    - The input point coordinates
43062a38674SMatthew G. Knepley 
43162a38674SMatthew G. Knepley   Output Parameters:
43262a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
43362a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
43462a38674SMatthew G. Knepley 
43562a38674SMatthew G. Knepley   Level: developer
43662a38674SMatthew G. Knepley 
437f5867de0SMatthew G. Knepley   Note:
438f5867de0SMatthew G. Knepley   This only guarantees that a box contains a point, not that a cell does.
439f5867de0SMatthew G. Knepley 
440db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`
44162a38674SMatthew G. Knepley */
442d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
443d71ae5a4SJacob Faibussowitsch {
444c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
445c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
446c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
447c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
448c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
449c4eade1cSMatthew G. Knepley   PetscInt         d, p;
450c4eade1cSMatthew G. Knepley 
451c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
452c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
453c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4541c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]);
455c4eade1cSMatthew G. Knepley 
4561c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1;
4572a705cacSMatthew G. Knepley       if (dbox == -1 && PetscAbsReal(PetscRealPart(points[p * dim + d]) - lower[d]) < 1.0e-9) dbox = 0;
4589371c9d4SSatish Balay       PetscCheck(dbox >= 0 && dbox<n[d], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %" PetscInt_FMT " (%g, %g, %g) is outside of our bounding box", p, (double)PetscRealPart(points[p * dim + 0]), dim> 1 ? (double)PetscRealPart(points[p * dim + 1]) : 0.0, dim > 2 ? (double)PetscRealPart(points[p * dim + 2]) : 0.0);
459c4eade1cSMatthew G. Knepley       dboxes[p * dim + d] = dbox;
460c4eade1cSMatthew G. Knepley     }
4619371c9d4SSatish Balay     if (boxes)
4629371c9d4SSatish Balay       for (d = dim - 2, boxes[p] = dboxes[p * dim + dim - 1]; d >= 0; --d) boxes[p] = boxes[p] * n[d] + dboxes[p * dim + d];
463c4eade1cSMatthew G. Knepley   }
464*3ba16761SJacob 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 
470af74b616SDave May  Not collective
471af74b616SDave May 
472af74b616SDave May   Input Parameters:
473af74b616SDave May + box         - The grid hash object
474f5867de0SMatthew G. Knepley . cellSection - The PetscSection mapping cells to boxes
475af74b616SDave May . numPoints   - The number of input points
476af74b616SDave May - points      - The input point coordinates
477af74b616SDave May 
478af74b616SDave May   Output Parameters:
479af74b616SDave May + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
480af74b616SDave May . boxes  - An array of numPoints integers expressing the enclosing box as single number, or NULL
481af74b616SDave May - found  - Flag indicating if point was located within a box
482af74b616SDave May 
483af74b616SDave May   Level: developer
484af74b616SDave May 
485f5867de0SMatthew G. Knepley   Note:
486f5867de0SMatthew G. Knepley   This does an additional check that a cell actually contains the point, and found is PETSC_FALSE if no cell does. Thus, this function requires that the cellSection is already constructed.
487f5867de0SMatthew G. Knepley 
488db781477SPatrick Sanan .seealso: `PetscGridHashGetEnclosingBox()`
489af74b616SDave May */
490d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscSection cellSection, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[], PetscBool *found)
491d71ae5a4SJacob Faibussowitsch {
492af74b616SDave May   const PetscReal *lower = box->lower;
493af74b616SDave May   const PetscReal *upper = box->upper;
494af74b616SDave May   const PetscReal *h     = box->h;
495af74b616SDave May   const PetscInt  *n     = box->n;
496af74b616SDave May   const PetscInt   dim   = box->dim;
497f5867de0SMatthew G. Knepley   PetscInt         bStart, bEnd, d, p;
498af74b616SDave May 
499af74b616SDave May   PetscFunctionBegin;
500f5867de0SMatthew G. Knepley   PetscValidHeaderSpecific(cellSection, PETSC_SECTION_CLASSID, 2);
501af74b616SDave May   *found = PETSC_FALSE;
502f5867de0SMatthew G. Knepley   PetscCall(PetscSectionGetChart(box->cellSection, &bStart, &bEnd));
503af74b616SDave May   for (p = 0; p < numPoints; ++p) {
504af74b616SDave May     for (d = 0; d < dim; ++d) {
505af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]);
506af74b616SDave May 
507af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1;
508*3ba16761SJacob 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
514*3ba16761SJacob Faibussowitsch     if (boxes[p] < bStart || boxes[p] >= bEnd) PetscFunctionReturn(PETSC_SUCCESS);
515af74b616SDave May   }
516af74b616SDave May   *found = PETSC_TRUE;
517*3ba16761SJacob 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));
529*3ba16761SJacob 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   }
557*3ba16761SJacob 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   }
584*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58562a38674SMatthew G. Knepley }
58662a38674SMatthew G. Knepley 
58762a38674SMatthew G. Knepley /*
58862a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
58962a38674SMatthew G. Knepley 
590d083f849SBarry Smith   Collective on dm
59162a38674SMatthew G. Knepley 
59262a38674SMatthew G. Knepley   Input Parameter:
59362a38674SMatthew G. Knepley . dm - The Plex
59462a38674SMatthew G. Knepley 
59562a38674SMatthew G. Knepley   Output Parameter:
59662a38674SMatthew G. Knepley . localBox - The grid hash object
59762a38674SMatthew G. Knepley 
59862a38674SMatthew G. Knepley   Level: developer
59962a38674SMatthew G. Knepley 
600db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`, `PetscGridHashGetEnclosingBox()`
60162a38674SMatthew G. Knepley */
602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
603d71ae5a4SJacob Faibussowitsch {
604f5867de0SMatthew G. Knepley   PetscInt           debug = ((DM_Plex *)dm->data)->printLocate;
605cafe43deSMatthew G. Knepley   MPI_Comm           comm;
606cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
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;
796*3ba16761SJacob 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));
994*3ba16761SJacob 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 
1000741bfc07SMatthew G. Knepley   Not collective
1001741bfc07SMatthew G. Knepley 
10026b867d5aSJose E. Roman   Input/Output Parameter:
10036b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x
1004741bfc07SMatthew G. Knepley 
10056b867d5aSJose E. Roman   Output Parameter:
10066b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
1007741bfc07SMatthew G. Knepley 
1008741bfc07SMatthew G. Knepley   Level: developer
1009741bfc07SMatthew G. Knepley 
1010db781477SPatrick Sanan .seealso: `DMPlexComputeProjection3Dto1D()`, `DMPlexComputeProjection3Dto2D()`
1011741bfc07SMatthew G. Knepley @*/
1012d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
1013d71ae5a4SJacob Faibussowitsch {
101417fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
101517fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
10168b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x * x + y * y), c = x / r, s = y / r;
101717fe8556SMatthew G. Knepley 
101817fe8556SMatthew G. Knepley   PetscFunctionBegin;
10199371c9d4SSatish Balay   R[0]      = c;
10209371c9d4SSatish Balay   R[1]      = -s;
10219371c9d4SSatish Balay   R[2]      = s;
10229371c9d4SSatish Balay   R[3]      = c;
102317fe8556SMatthew G. Knepley   coords[0] = 0.0;
10247f07f362SMatthew G. Knepley   coords[1] = r;
1025*3ba16761SJacob 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 
1031741bfc07SMatthew G. Knepley   Not collective
103228dbe442SToby Isaac 
10336b867d5aSJose E. Roman   Input/Output Parameter:
10346b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z
1035741bfc07SMatthew G. Knepley 
10366b867d5aSJose E. Roman   Output Parameter:
10376b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
1038741bfc07SMatthew G. Knepley 
1039741bfc07SMatthew G. Knepley   Note: This uses the basis completion described by Frisvad in http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html, DOI:10.1080/2165347X.2012.689606
1040741bfc07SMatthew G. Knepley 
1041741bfc07SMatthew G. Knepley   Level: developer
1042741bfc07SMatthew G. Knepley 
1043db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto2D()`
1044741bfc07SMatthew G. Knepley @*/
1045d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
1046d71ae5a4SJacob Faibussowitsch {
104728dbe442SToby Isaac   PetscReal x    = PetscRealPart(coords[3] - coords[0]);
104828dbe442SToby Isaac   PetscReal y    = PetscRealPart(coords[4] - coords[1]);
104928dbe442SToby Isaac   PetscReal z    = PetscRealPart(coords[5] - coords[2]);
105028dbe442SToby Isaac   PetscReal r    = PetscSqrtReal(x * x + y * y + z * z);
105128dbe442SToby Isaac   PetscReal rinv = 1. / r;
105228dbe442SToby Isaac   PetscFunctionBegin;
105328dbe442SToby Isaac 
10549371c9d4SSatish Balay   x *= rinv;
10559371c9d4SSatish Balay   y *= rinv;
10569371c9d4SSatish Balay   z *= rinv;
105728dbe442SToby Isaac   if (x > 0.) {
105828dbe442SToby Isaac     PetscReal inv1pX = 1. / (1. + x);
105928dbe442SToby Isaac 
10609371c9d4SSatish Balay     R[0] = x;
10619371c9d4SSatish Balay     R[1] = -y;
10629371c9d4SSatish Balay     R[2] = -z;
10639371c9d4SSatish Balay     R[3] = y;
10649371c9d4SSatish Balay     R[4] = 1. - y * y * inv1pX;
10659371c9d4SSatish Balay     R[5] = -y * z * inv1pX;
10669371c9d4SSatish Balay     R[6] = z;
10679371c9d4SSatish Balay     R[7] = -y * z * inv1pX;
10689371c9d4SSatish Balay     R[8] = 1. - z * z * inv1pX;
10699371c9d4SSatish Balay   } else {
107028dbe442SToby Isaac     PetscReal inv1mX = 1. / (1. - x);
107128dbe442SToby Isaac 
10729371c9d4SSatish Balay     R[0] = x;
10739371c9d4SSatish Balay     R[1] = z;
10749371c9d4SSatish Balay     R[2] = y;
10759371c9d4SSatish Balay     R[3] = y;
10769371c9d4SSatish Balay     R[4] = -y * z * inv1mX;
10779371c9d4SSatish Balay     R[5] = 1. - y * y * inv1mX;
10789371c9d4SSatish Balay     R[6] = z;
10799371c9d4SSatish Balay     R[7] = 1. - z * z * inv1mX;
10809371c9d4SSatish Balay     R[8] = -y * z * inv1mX;
108128dbe442SToby Isaac   }
108228dbe442SToby Isaac   coords[0] = 0.0;
108328dbe442SToby Isaac   coords[1] = r;
1084*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
108528dbe442SToby Isaac }
108628dbe442SToby Isaac 
1087741bfc07SMatthew G. Knepley /*@
1088c871b86eSJed Brown   DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the
1089c871b86eSJed Brown     plane.  The normal is defined by positive orientation of the first 3 points.
1090741bfc07SMatthew G. Knepley 
1091741bfc07SMatthew G. Knepley   Not collective
1092741bfc07SMatthew G. Knepley 
1093741bfc07SMatthew G. Knepley   Input Parameter:
10946b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points)
1095741bfc07SMatthew G. Knepley 
10966b867d5aSJose E. Roman   Input/Output Parameter:
10976b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first
10986b867d5aSJose E. Roman            2*coordSize/3 entries contain interlaced 2D points, with the rest undefined
10996b867d5aSJose E. Roman 
11006b867d5aSJose E. Roman   Output Parameter:
11016b867d5aSJose E. Roman . R - 3x3 row-major rotation matrix whose columns are the tangent basis [t1, t2, n].  Multiplying by R^T transforms from original frame to tangent frame.
1102741bfc07SMatthew G. Knepley 
1103741bfc07SMatthew G. Knepley   Level: developer
1104741bfc07SMatthew G. Knepley 
1105db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto1D()`
1106741bfc07SMatthew G. Knepley @*/
1107d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
1108d71ae5a4SJacob Faibussowitsch {
1109c871b86eSJed Brown   PetscReal      x1[3], x2[3], n[3], c[3], norm;
1110ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1111c871b86eSJed Brown   PetscInt       d, p;
1112ccd2543fSMatthew G Knepley 
1113ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1114ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
1115ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
11161ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1 * dim + d] - coords[0 * dim + d]);
11171ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2 * dim + d] - coords[0 * dim + d]);
1118ccd2543fSMatthew G Knepley   }
1119c871b86eSJed Brown   // n = x1 \otimes x2
1120ccd2543fSMatthew G Knepley   n[0] = x1[1] * x2[2] - x1[2] * x2[1];
1121ccd2543fSMatthew G Knepley   n[1] = x1[2] * x2[0] - x1[0] * x2[2];
1122ccd2543fSMatthew G Knepley   n[2] = x1[0] * x2[1] - x1[1] * x2[0];
11238b49ba18SBarry Smith   norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
1124c871b86eSJed Brown   for (d = 0; d < dim; d++) n[d] /= norm;
1125c871b86eSJed Brown   norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]);
1126c871b86eSJed Brown   for (d = 0; d < dim; d++) x1[d] /= norm;
1127c871b86eSJed Brown   // x2 = n \otimes x1
1128c871b86eSJed Brown   x2[0] = n[1] * x1[2] - n[2] * x1[1];
1129c871b86eSJed Brown   x2[1] = n[2] * x1[0] - n[0] * x1[2];
1130c871b86eSJed Brown   x2[2] = n[0] * x1[1] - n[1] * x1[0];
1131c871b86eSJed Brown   for (d = 0; d < dim; d++) {
1132c871b86eSJed Brown     R[d * dim + 0] = x1[d];
1133c871b86eSJed Brown     R[d * dim + 1] = x2[d];
1134c871b86eSJed Brown     R[d * dim + 2] = n[d];
1135c871b86eSJed Brown     c[d]           = PetscRealPart(coords[0 * dim + d]);
113673868372SMatthew G. Knepley   }
1137c871b86eSJed Brown   for (p = 0; p < coordSize / dim; p++) {
1138c871b86eSJed Brown     PetscReal y[3];
1139c871b86eSJed Brown     for (d = 0; d < dim; d++) y[d] = PetscRealPart(coords[p * dim + d]) - c[d];
1140c871b86eSJed Brown     for (d = 0; d < 2; d++) coords[p * 2 + d] = R[0 * dim + d] * y[0] + R[1 * dim + d] * y[1] + R[2 * dim + d] * y[2];
11417f07f362SMatthew G. Knepley   }
1142*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1143ccd2543fSMatthew G Knepley }
1144ccd2543fSMatthew G Knepley 
1145d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
1146d71ae5a4SJacob Faibussowitsch {
1147834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
1148834e62ceSMatthew G. Knepley 
1149834e62ceSMatthew G. Knepley    |  1  1  1 |
1150834e62ceSMatthew G. Knepley    | x0 x1 x2 |
1151834e62ceSMatthew G. Knepley    | y0 y1 y2 |
1152834e62ceSMatthew G. Knepley 
1153834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
1154834e62ceSMatthew G. Knepley 
1155834e62ceSMatthew G. Knepley    | x1 x2 |
1156834e62ceSMatthew G. Knepley    | y1 y2 |
1157834e62ceSMatthew G. Knepley   */
1158834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
1159834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
1160834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
11619371c9d4SSatish Balay   M[0] = x1;
11629371c9d4SSatish Balay   M[1] = x2;
11639371c9d4SSatish Balay   M[2] = y1;
11649371c9d4SSatish Balay   M[3] = y2;
1165923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
1166834e62ceSMatthew G. Knepley   *vol = 0.5 * detM;
11673bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
1168834e62ceSMatthew G. Knepley }
1169834e62ceSMatthew G. Knepley 
1170d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
1171d71ae5a4SJacob Faibussowitsch {
1172834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
1173834e62ceSMatthew G. Knepley 
1174834e62ceSMatthew G. Knepley    |  1  1  1  1 |
1175834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
1176834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
1177834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
1178834e62ceSMatthew G. Knepley 
1179834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
1180834e62ceSMatthew G. Knepley 
1181834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1182834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1183834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1184834e62ceSMatthew G. Knepley   */
1185834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2];
1186834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2];
1187834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
11880a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1189834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
11909371c9d4SSatish Balay   M[0] = x1;
11919371c9d4SSatish Balay   M[1] = x2;
11929371c9d4SSatish Balay   M[2] = x3;
11939371c9d4SSatish Balay   M[3] = y1;
11949371c9d4SSatish Balay   M[4] = y2;
11959371c9d4SSatish Balay   M[5] = y3;
11969371c9d4SSatish Balay   M[6] = z1;
11979371c9d4SSatish Balay   M[7] = z2;
11989371c9d4SSatish Balay   M[8] = z3;
1199923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
12000a3da2c2SToby Isaac   *vol = -onesixth * detM;
12013bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1202834e62ceSMatthew G. Knepley }
1203834e62ceSMatthew G. Knepley 
1204d71ae5a4SJacob Faibussowitsch static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
1205d71ae5a4SJacob Faibussowitsch {
12060a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1207923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
12080a3da2c2SToby Isaac   *vol *= -onesixth;
12090ec8681fSMatthew G. Knepley }
12100ec8681fSMatthew G. Knepley 
1211d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1212d71ae5a4SJacob Faibussowitsch {
1213cb92db44SToby Isaac   PetscSection       coordSection;
1214cb92db44SToby Isaac   Vec                coordinates;
1215cb92db44SToby Isaac   const PetscScalar *coords;
1216cb92db44SToby Isaac   PetscInt           dim, d, off;
1217cb92db44SToby Isaac 
1218cb92db44SToby Isaac   PetscFunctionBegin;
12199566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
12209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
12219566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(coordSection, e, &dim));
1222*3ba16761SJacob Faibussowitsch   if (!dim) PetscFunctionReturn(PETSC_SUCCESS);
12239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(coordSection, e, &off));
12249566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
12259371c9d4SSatish Balay   if (v0) {
12269371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);
12279371c9d4SSatish Balay   }
12289566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
1229cb92db44SToby Isaac   *detJ = 1.;
1230cb92db44SToby Isaac   if (J) {
1231cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1232cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1233cb92db44SToby Isaac     if (invJ) {
1234cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1235cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1236cb92db44SToby Isaac     }
1237cb92db44SToby Isaac   }
1238*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1239cb92db44SToby Isaac }
1240cb92db44SToby Isaac 
12416858538eSMatthew G. Knepley /*@C
12426858538eSMatthew G. Knepley   DMPlexGetCellCoordinates - Get coordinates for a cell, taking into account periodicity
12436858538eSMatthew G. Knepley 
12446858538eSMatthew G. Knepley   Not collective
12456858538eSMatthew G. Knepley 
12466858538eSMatthew G. Knepley   Input Parameters:
12476858538eSMatthew G. Knepley + dm   - The DM
12486858538eSMatthew G. Knepley - cell - The cell number
12496858538eSMatthew G. Knepley 
12506858538eSMatthew G. Knepley   Output Parameters:
12516858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
12526858538eSMatthew G. Knepley . Nc     - The number of coordinates
12536858538eSMatthew G. Knepley . array  - The coordinate array
12546858538eSMatthew G. Knepley - coords - The cell coordinates
12556858538eSMatthew G. Knepley 
12566858538eSMatthew G. Knepley   Level: developer
12576858538eSMatthew G. Knepley 
12586858538eSMatthew G. Knepley .seealso: DMPlexRestoreCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal()
12596858538eSMatthew G. Knepley @*/
1260d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[])
1261d71ae5a4SJacob Faibussowitsch {
12626858538eSMatthew G. Knepley   DM                 cdm;
12636858538eSMatthew G. Knepley   Vec                coordinates;
12646858538eSMatthew G. Knepley   PetscSection       cs;
12656858538eSMatthew G. Knepley   const PetscScalar *ccoords;
12666858538eSMatthew G. Knepley   PetscInt           pStart, pEnd;
12676858538eSMatthew G. Knepley 
12686858538eSMatthew G. Knepley   PetscFunctionBeginHot;
12696858538eSMatthew G. Knepley   *isDG   = PETSC_FALSE;
12706858538eSMatthew G. Knepley   *Nc     = 0;
12716858538eSMatthew G. Knepley   *array  = NULL;
12726858538eSMatthew G. Knepley   *coords = NULL;
12736858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12746858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateSection(dm, &cs));
12756858538eSMatthew G. Knepley   if (!cs) goto cg;
12766858538eSMatthew G. Knepley   /* Check that the cell exists in the cellwise section */
12776858538eSMatthew G. Knepley   PetscCall(PetscSectionGetChart(cs, &pStart, &pEnd));
12786858538eSMatthew G. Knepley   if (cell < pStart || cell >= pEnd) goto cg;
12796858538eSMatthew G. Knepley   /* Check for cellwise coordinates for this cell */
12806858538eSMatthew G. Knepley   PetscCall(PetscSectionGetDof(cs, cell, Nc));
12816858538eSMatthew G. Knepley   if (!*Nc) goto cg;
12826858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12836858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocalNoncollective(dm, &coordinates));
12846858538eSMatthew G. Knepley   if (!coordinates) goto cg;
12856858538eSMatthew G. Knepley   /* Get cellwise coordinates */
12866858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dm, &cdm));
12876858538eSMatthew G. Knepley   PetscCall(VecGetArrayRead(coordinates, array));
12886858538eSMatthew G. Knepley   PetscCall(DMPlexPointLocalRead(cdm, cell, *array, &ccoords));
12896858538eSMatthew G. Knepley   PetscCall(DMGetWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
12906858538eSMatthew G. Knepley   PetscCall(PetscArraycpy(*coords, ccoords, *Nc));
12916858538eSMatthew G. Knepley   PetscCall(VecRestoreArrayRead(coordinates, array));
12926858538eSMatthew G. Knepley   *isDG = PETSC_TRUE;
1293*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12946858538eSMatthew G. Knepley cg:
12956858538eSMatthew G. Knepley   /* Use continuous coordinates */
12966858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
12976858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &cs));
12986858538eSMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
12996858538eSMatthew G. Knepley   PetscCall(DMPlexVecGetClosure(cdm, cs, coordinates, cell, Nc, coords));
1300*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13016858538eSMatthew G. Knepley }
13026858538eSMatthew G. Knepley 
13036858538eSMatthew G. Knepley /*@C
13046858538eSMatthew G. Knepley   DMPlexRestoreCellCoordinates - Get coordinates for a cell, taking into account periodicity
13056858538eSMatthew G. Knepley 
13066858538eSMatthew G. Knepley   Not collective
13076858538eSMatthew G. Knepley 
13086858538eSMatthew G. Knepley   Input Parameters:
13096858538eSMatthew G. Knepley + dm   - The DM
13106858538eSMatthew G. Knepley - cell - The cell number
13116858538eSMatthew G. Knepley 
13126858538eSMatthew G. Knepley   Output Parameters:
13136858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
13146858538eSMatthew G. Knepley . Nc     - The number of coordinates
13156858538eSMatthew G. Knepley . array  - The coordinate array
13166858538eSMatthew G. Knepley - coords - The cell coordinates
13176858538eSMatthew G. Knepley 
13186858538eSMatthew G. Knepley   Level: developer
13196858538eSMatthew G. Knepley 
13206858538eSMatthew G. Knepley .seealso: DMPlexGetCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal()
13216858538eSMatthew G. Knepley @*/
1322d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexRestoreCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[])
1323d71ae5a4SJacob Faibussowitsch {
13246858538eSMatthew G. Knepley   DM           cdm;
13256858538eSMatthew G. Knepley   PetscSection cs;
13266858538eSMatthew G. Knepley   Vec          coordinates;
13276858538eSMatthew G. Knepley 
13286858538eSMatthew G. Knepley   PetscFunctionBeginHot;
13296858538eSMatthew G. Knepley   if (*isDG) {
13306858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinateDM(dm, &cdm));
13316858538eSMatthew G. Knepley     PetscCall(DMRestoreWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
13326858538eSMatthew G. Knepley   } else {
13336858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
13346858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateSection(dm, &cs));
13356858538eSMatthew G. Knepley     PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
13366858538eSMatthew G. Knepley     PetscCall(DMPlexVecRestoreClosure(cdm, cs, coordinates, cell, Nc, (PetscScalar **)coords));
13376858538eSMatthew G. Knepley   }
1338*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13396858538eSMatthew G. Knepley }
13406858538eSMatthew G. Knepley 
1341d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1342d71ae5a4SJacob Faibussowitsch {
13436858538eSMatthew G. Knepley   const PetscScalar *array;
1344a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
13456858538eSMatthew G. Knepley   PetscInt           numCoords, d;
13466858538eSMatthew G. Knepley   PetscBool          isDG;
134717fe8556SMatthew G. Knepley 
134817fe8556SMatthew G. Knepley   PetscFunctionBegin;
13496858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
135008401ef6SPierre Jolivet   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
13517f07f362SMatthew G. Knepley   *detJ = 0.0;
135228dbe442SToby Isaac   if (numCoords == 6) {
135328dbe442SToby Isaac     const PetscInt dim = 3;
135428dbe442SToby Isaac     PetscReal      R[9], J0;
135528dbe442SToby Isaac 
13569371c9d4SSatish Balay     if (v0) {
13579371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13589371c9d4SSatish Balay     }
13599566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto1D(coords, R));
136028dbe442SToby Isaac     if (J) {
136128dbe442SToby Isaac       J0   = 0.5 * PetscRealPart(coords[1]);
13629371c9d4SSatish Balay       J[0] = R[0] * J0;
13639371c9d4SSatish Balay       J[1] = R[1];
13649371c9d4SSatish Balay       J[2] = R[2];
13659371c9d4SSatish Balay       J[3] = R[3] * J0;
13669371c9d4SSatish Balay       J[4] = R[4];
13679371c9d4SSatish Balay       J[5] = R[5];
13689371c9d4SSatish Balay       J[6] = R[6] * J0;
13699371c9d4SSatish Balay       J[7] = R[7];
13709371c9d4SSatish Balay       J[8] = R[8];
137128dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
1372ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
1373adac9986SMatthew G. Knepley     }
137428dbe442SToby Isaac   } else if (numCoords == 4) {
13757f07f362SMatthew G. Knepley     const PetscInt dim = 2;
13767f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
13777f07f362SMatthew G. Knepley 
13789371c9d4SSatish Balay     if (v0) {
13799371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13809371c9d4SSatish Balay     }
13819566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection2Dto1D(coords, R));
138217fe8556SMatthew G. Knepley     if (J) {
13837f07f362SMatthew G. Knepley       J0   = 0.5 * PetscRealPart(coords[1]);
13849371c9d4SSatish Balay       J[0] = R[0] * J0;
13859371c9d4SSatish Balay       J[1] = R[1];
13869371c9d4SSatish Balay       J[2] = R[2] * J0;
13879371c9d4SSatish Balay       J[3] = R[3];
1388923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1389ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
1390adac9986SMatthew G. Knepley     }
13917f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
13927f07f362SMatthew G. Knepley     const PetscInt dim = 1;
13937f07f362SMatthew G. Knepley 
13949371c9d4SSatish Balay     if (v0) {
13959371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13969371c9d4SSatish Balay     }
13977f07f362SMatthew G. Knepley     if (J) {
13987f07f362SMatthew G. Knepley       J[0]  = 0.5 * (PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
139917fe8556SMatthew G. Knepley       *detJ = J[0];
14009566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(2.0));
14019371c9d4SSatish Balay       if (invJ) {
14029371c9d4SSatish Balay         invJ[0] = 1.0 / J[0];
14039371c9d4SSatish Balay         PetscCall(PetscLogFlops(1.0));
14049371c9d4SSatish Balay       }
1405adac9986SMatthew G. Knepley     }
14066858538eSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for segment %" PetscInt_FMT " is %" PetscInt_FMT " != 2 or 4 or 6", e, numCoords);
14076858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1408*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
140917fe8556SMatthew G. Knepley }
141017fe8556SMatthew G. Knepley 
1411d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1412d71ae5a4SJacob Faibussowitsch {
14136858538eSMatthew G. Knepley   const PetscScalar *array;
1414a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
14156858538eSMatthew G. Knepley   PetscInt           numCoords, d;
14166858538eSMatthew G. Knepley   PetscBool          isDG;
1417ccd2543fSMatthew G Knepley 
1418ccd2543fSMatthew G Knepley   PetscFunctionBegin;
14196858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14206858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
14217f07f362SMatthew G. Knepley   *detJ = 0.0;
1422ccd2543fSMatthew G Knepley   if (numCoords == 9) {
14237f07f362SMatthew G. Knepley     const PetscInt dim = 3;
14247f07f362SMatthew G. Knepley     PetscReal      R[9], J0[9] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
14257f07f362SMatthew G. Knepley 
14269371c9d4SSatish Balay     if (v0) {
14279371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
14289371c9d4SSatish Balay     }
14299566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
14307f07f362SMatthew G. Knepley     if (J) {
1431b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1432b7ad821dSMatthew G. Knepley 
1433b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1434ad540459SPierre Jolivet         for (PetscInt f = 0; f < pdim; f++) J0[d * dim + f] = 0.5 * (PetscRealPart(coords[(f + 1) * pdim + d]) - PetscRealPart(coords[0 * pdim + d]));
14357f07f362SMatthew G. Knepley       }
14369566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1437923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
14387f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
14396858538eSMatthew G. Knepley         for (PetscInt f = 0; f < dim; f++) {
14407f07f362SMatthew G. Knepley           J[d * dim + f] = 0.0;
1441ad540459SPierre Jolivet           for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f];
14427f07f362SMatthew G. Knepley         }
14437f07f362SMatthew G. Knepley       }
14449566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
14457f07f362SMatthew G. Knepley     }
1446ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
14477f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
14487f07f362SMatthew G. Knepley     const PetscInt dim = 2;
14497f07f362SMatthew G. Knepley 
14509371c9d4SSatish Balay     if (v0) {
14519371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
14529371c9d4SSatish Balay     }
1453ccd2543fSMatthew G Knepley     if (J) {
1454ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1455ad540459SPierre Jolivet         for (PetscInt f = 0; f < dim; f++) J[d * dim + f] = 0.5 * (PetscRealPart(coords[(f + 1) * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1456ccd2543fSMatthew G Knepley       }
14579566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1458923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1459ccd2543fSMatthew G Knepley     }
1460ad540459SPierre Jolivet     if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
146163a3b9bcSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %" PetscInt_FMT " != 6 or 9", numCoords);
14626858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1463*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1464ccd2543fSMatthew G Knepley }
1465ccd2543fSMatthew G Knepley 
1466d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1467d71ae5a4SJacob Faibussowitsch {
14686858538eSMatthew G. Knepley   const PetscScalar *array;
1469a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
14706858538eSMatthew G. Knepley   PetscInt           numCoords, d;
14716858538eSMatthew G. Knepley   PetscBool          isDG;
1472ccd2543fSMatthew G Knepley 
1473ccd2543fSMatthew G Knepley   PetscFunctionBegin;
14746858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14756858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1476dfccc68fSToby Isaac   if (!Nq) {
1477412e9a14SMatthew G. Knepley     PetscInt vorder[4] = {0, 1, 2, 3};
1478412e9a14SMatthew G. Knepley 
14799371c9d4SSatish Balay     if (isTensor) {
14809371c9d4SSatish Balay       vorder[2] = 3;
14819371c9d4SSatish Balay       vorder[3] = 2;
14829371c9d4SSatish Balay     }
14837f07f362SMatthew G. Knepley     *detJ = 0.0;
148499dec3a6SMatthew G. Knepley     if (numCoords == 12) {
148599dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
148699dec3a6SMatthew G. Knepley       PetscReal      R[9], J0[9] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
148799dec3a6SMatthew G. Knepley 
14889371c9d4SSatish Balay       if (v) {
14899371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
14909371c9d4SSatish Balay       }
14919566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
149299dec3a6SMatthew G. Knepley       if (J) {
149399dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
149499dec3a6SMatthew G. Knepley 
149599dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
1496412e9a14SMatthew G. Knepley           J0[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * pdim + d]) - PetscRealPart(coords[vorder[0] * pdim + d]));
1497412e9a14SMatthew G. Knepley           J0[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[2] * pdim + d]) - PetscRealPart(coords[vorder[1] * pdim + d]));
149899dec3a6SMatthew G. Knepley         }
14999566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1500923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
150199dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
15026858538eSMatthew G. Knepley           for (PetscInt f = 0; f < dim; f++) {
150399dec3a6SMatthew G. Knepley             J[d * dim + f] = 0.0;
1504ad540459SPierre Jolivet             for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f];
150599dec3a6SMatthew G. Knepley           }
150699dec3a6SMatthew G. Knepley         }
15079566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(18.0));
150899dec3a6SMatthew G. Knepley       }
1509ad540459SPierre Jolivet       if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
151071f58de1SToby Isaac     } else if (numCoords == 8) {
151199dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
151299dec3a6SMatthew G. Knepley 
15139371c9d4SSatish Balay       if (v) {
15149371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
15159371c9d4SSatish Balay       }
1516ccd2543fSMatthew G Knepley       if (J) {
1517ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
1518412e9a14SMatthew G. Knepley           J[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1519412e9a14SMatthew G. Knepley           J[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[3] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1520ccd2543fSMatthew G Knepley         }
15219566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1522923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1523ccd2543fSMatthew G Knepley       }
1524ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
152563a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1526dfccc68fSToby Isaac   } else {
1527dfccc68fSToby Isaac     const PetscInt Nv         = 4;
1528dfccc68fSToby Isaac     const PetscInt dimR       = 2;
1529412e9a14SMatthew G. Knepley     PetscInt       zToPlex[4] = {0, 1, 3, 2};
1530dfccc68fSToby Isaac     PetscReal      zOrder[12];
1531dfccc68fSToby Isaac     PetscReal      zCoeff[12];
1532dfccc68fSToby Isaac     PetscInt       i, j, k, l, dim;
1533dfccc68fSToby Isaac 
15349371c9d4SSatish Balay     if (isTensor) {
15359371c9d4SSatish Balay       zToPlex[2] = 2;
15369371c9d4SSatish Balay       zToPlex[3] = 3;
15379371c9d4SSatish Balay     }
1538dfccc68fSToby Isaac     if (numCoords == 12) {
1539dfccc68fSToby Isaac       dim = 3;
1540dfccc68fSToby Isaac     } else if (numCoords == 8) {
1541dfccc68fSToby Isaac       dim = 2;
154263a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1543dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1544dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1545dfccc68fSToby Isaac 
1546ad540459SPierre Jolivet       for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1547dfccc68fSToby Isaac     }
1548dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
15492df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta):
15502df84da0SMatthew G. Knepley            \phi^0 = (1 - xi - eta + xi eta) --> 1      = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3)
15512df84da0SMatthew G. Knepley            \phi^1 = (1 + xi - eta - xi eta) --> xi     = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3)
15522df84da0SMatthew G. Knepley            \phi^2 = (1 - xi + eta - xi eta) --> eta    = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3)
15532df84da0SMatthew G. Knepley            \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3)
15542df84da0SMatthew G. Knepley       */
1555dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1556dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1557dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1558dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1559dfccc68fSToby Isaac     }
1560dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1561dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1562dfccc68fSToby Isaac 
1563dfccc68fSToby Isaac       if (v) {
1564dfccc68fSToby Isaac         PetscReal extPoint[4];
1565dfccc68fSToby Isaac 
1566dfccc68fSToby Isaac         extPoint[0] = 1.;
1567dfccc68fSToby Isaac         extPoint[1] = xi;
1568dfccc68fSToby Isaac         extPoint[2] = eta;
1569dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1570dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1571dfccc68fSToby Isaac           PetscReal val = 0.;
1572dfccc68fSToby Isaac 
1573ad540459SPierre Jolivet           for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j];
1574dfccc68fSToby Isaac           v[i * dim + j] = val;
1575dfccc68fSToby Isaac         }
1576dfccc68fSToby Isaac       }
1577dfccc68fSToby Isaac       if (J) {
1578dfccc68fSToby Isaac         PetscReal extJ[8];
1579dfccc68fSToby Isaac 
1580dfccc68fSToby Isaac         extJ[0] = 0.;
1581dfccc68fSToby Isaac         extJ[1] = 0.;
1582dfccc68fSToby Isaac         extJ[2] = 1.;
1583dfccc68fSToby Isaac         extJ[3] = 0.;
1584dfccc68fSToby Isaac         extJ[4] = 0.;
1585dfccc68fSToby Isaac         extJ[5] = 1.;
1586dfccc68fSToby Isaac         extJ[6] = eta;
1587dfccc68fSToby Isaac         extJ[7] = xi;
1588dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1589dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1590dfccc68fSToby Isaac             PetscReal val = 0.;
1591dfccc68fSToby Isaac 
1592ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1593dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1594dfccc68fSToby Isaac           }
1595dfccc68fSToby Isaac         }
1596dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1597dfccc68fSToby Isaac           PetscReal  x, y, z;
1598dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1599dfccc68fSToby Isaac           PetscReal  norm;
1600dfccc68fSToby Isaac 
1601dfccc68fSToby Isaac           x     = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1602dfccc68fSToby Isaac           y     = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1603dfccc68fSToby Isaac           z     = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1604dfccc68fSToby Isaac           norm  = PetscSqrtReal(x * x + y * y + z * z);
1605dfccc68fSToby Isaac           iJ[2] = x / norm;
1606dfccc68fSToby Isaac           iJ[5] = y / norm;
1607dfccc68fSToby Isaac           iJ[8] = z / norm;
1608dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1609ad540459SPierre Jolivet           if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1610dfccc68fSToby Isaac         } else {
1611dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1612ad540459SPierre Jolivet           if (invJ) DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1613dfccc68fSToby Isaac         }
1614dfccc68fSToby Isaac       }
1615dfccc68fSToby Isaac     }
1616dfccc68fSToby Isaac   }
16176858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1618*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1619ccd2543fSMatthew G Knepley }
1620ccd2543fSMatthew G Knepley 
1621d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1622d71ae5a4SJacob Faibussowitsch {
16236858538eSMatthew G. Knepley   const PetscScalar *array;
1624a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1625ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
16266858538eSMatthew G. Knepley   PetscInt           numCoords, d;
16276858538eSMatthew G. Knepley   PetscBool          isDG;
1628ccd2543fSMatthew G Knepley 
1629ccd2543fSMatthew G Knepley   PetscFunctionBegin;
16306858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16316858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
16327f07f362SMatthew G. Knepley   *detJ = 0.0;
16339371c9d4SSatish Balay   if (v0) {
16349371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
16359371c9d4SSatish Balay   }
1636ccd2543fSMatthew G Knepley   if (J) {
1637ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1638f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1639f0df753eSMatthew G. Knepley       J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1640f0df753eSMatthew G. Knepley       J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1641f0df753eSMatthew G. Knepley       J[d * dim + 2] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1642ccd2543fSMatthew G Knepley     }
16439566063dSJacob Faibussowitsch     PetscCall(PetscLogFlops(18.0));
1644923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1645ccd2543fSMatthew G Knepley   }
1646ad540459SPierre Jolivet   if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
16476858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1648*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1649ccd2543fSMatthew G Knepley }
1650ccd2543fSMatthew G Knepley 
1651d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1652d71ae5a4SJacob Faibussowitsch {
16536858538eSMatthew G. Knepley   const PetscScalar *array;
1654a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1655ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
16566858538eSMatthew G. Knepley   PetscInt           numCoords, d;
16576858538eSMatthew G. Knepley   PetscBool          isDG;
1658ccd2543fSMatthew G Knepley 
1659ccd2543fSMatthew G Knepley   PetscFunctionBegin;
16606858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16616858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1662dfccc68fSToby Isaac   if (!Nq) {
16637f07f362SMatthew G. Knepley     *detJ = 0.0;
16649371c9d4SSatish Balay     if (v) {
16659371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
16669371c9d4SSatish Balay     }
1667ccd2543fSMatthew G Knepley     if (J) {
1668ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1669f0df753eSMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1670f0df753eSMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1671f0df753eSMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1672ccd2543fSMatthew G Knepley       }
16739566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
1674923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1675ccd2543fSMatthew G Knepley     }
1676ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
1677dfccc68fSToby Isaac   } else {
1678dfccc68fSToby Isaac     const PetscInt Nv         = 8;
1679dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1680dfccc68fSToby Isaac     const PetscInt dim        = 3;
1681dfccc68fSToby Isaac     const PetscInt dimR       = 3;
1682dfccc68fSToby Isaac     PetscReal      zOrder[24];
1683dfccc68fSToby Isaac     PetscReal      zCoeff[24];
1684dfccc68fSToby Isaac     PetscInt       i, j, k, l;
1685dfccc68fSToby Isaac 
1686dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1687dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1688dfccc68fSToby Isaac 
1689ad540459SPierre Jolivet       for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1690dfccc68fSToby Isaac     }
1691dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1692dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.125 * (zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j] + zOrder[dim * 4 + j] + zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1693dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.125 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j] - zOrder[dim * 4 + j] + zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1694dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.125 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j] - zOrder[dim * 4 + j] - zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1695dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.125 * (zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j] + zOrder[dim * 4 + j] - zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1696dfccc68fSToby Isaac       zCoeff[dim * 4 + j] = 0.125 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] - zOrder[dim * 3 + j] + zOrder[dim * 4 + j] + zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1697dfccc68fSToby Isaac       zCoeff[dim * 5 + j] = 0.125 * (+zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] - zOrder[dim * 3 + j] - zOrder[dim * 4 + j] + zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1698dfccc68fSToby Isaac       zCoeff[dim * 6 + j] = 0.125 * (+zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] - zOrder[dim * 3 + j] - zOrder[dim * 4 + j] - zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1699dfccc68fSToby Isaac       zCoeff[dim * 7 + j] = 0.125 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] - zOrder[dim * 3 + j] + zOrder[dim * 4 + j] - zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1700dfccc68fSToby Isaac     }
1701dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1702dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1703dfccc68fSToby Isaac 
1704dfccc68fSToby Isaac       if (v) {
170591d2b7ceSToby Isaac         PetscReal extPoint[8];
1706dfccc68fSToby Isaac 
1707dfccc68fSToby Isaac         extPoint[0] = 1.;
1708dfccc68fSToby Isaac         extPoint[1] = xi;
1709dfccc68fSToby Isaac         extPoint[2] = eta;
1710dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1711dfccc68fSToby Isaac         extPoint[4] = theta;
1712dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1713dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1714dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1715dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1716dfccc68fSToby Isaac           PetscReal val = 0.;
1717dfccc68fSToby Isaac 
1718ad540459SPierre Jolivet           for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j];
1719dfccc68fSToby Isaac           v[i * dim + j] = val;
1720dfccc68fSToby Isaac         }
1721dfccc68fSToby Isaac       }
1722dfccc68fSToby Isaac       if (J) {
1723dfccc68fSToby Isaac         PetscReal extJ[24];
1724dfccc68fSToby Isaac 
17259371c9d4SSatish Balay         extJ[0]  = 0.;
17269371c9d4SSatish Balay         extJ[1]  = 0.;
17279371c9d4SSatish Balay         extJ[2]  = 0.;
17289371c9d4SSatish Balay         extJ[3]  = 1.;
17299371c9d4SSatish Balay         extJ[4]  = 0.;
17309371c9d4SSatish Balay         extJ[5]  = 0.;
17319371c9d4SSatish Balay         extJ[6]  = 0.;
17329371c9d4SSatish Balay         extJ[7]  = 1.;
17339371c9d4SSatish Balay         extJ[8]  = 0.;
17349371c9d4SSatish Balay         extJ[9]  = eta;
17359371c9d4SSatish Balay         extJ[10] = xi;
17369371c9d4SSatish Balay         extJ[11] = 0.;
17379371c9d4SSatish Balay         extJ[12] = 0.;
17389371c9d4SSatish Balay         extJ[13] = 0.;
17399371c9d4SSatish Balay         extJ[14] = 1.;
17409371c9d4SSatish Balay         extJ[15] = theta;
17419371c9d4SSatish Balay         extJ[16] = 0.;
17429371c9d4SSatish Balay         extJ[17] = xi;
17439371c9d4SSatish Balay         extJ[18] = 0.;
17449371c9d4SSatish Balay         extJ[19] = theta;
17459371c9d4SSatish Balay         extJ[20] = eta;
17469371c9d4SSatish Balay         extJ[21] = theta * eta;
17479371c9d4SSatish Balay         extJ[22] = theta * xi;
17489371c9d4SSatish Balay         extJ[23] = eta * xi;
1749dfccc68fSToby Isaac 
1750dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1751dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1752dfccc68fSToby Isaac             PetscReal val = 0.;
1753dfccc68fSToby Isaac 
1754ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1755dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1756dfccc68fSToby Isaac           }
1757dfccc68fSToby Isaac         }
1758dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1759ad540459SPierre Jolivet         if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1760dfccc68fSToby Isaac       }
1761dfccc68fSToby Isaac     }
1762dfccc68fSToby Isaac   }
17636858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1764*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1765ccd2543fSMatthew G Knepley }
1766ccd2543fSMatthew G Knepley 
1767d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1768d71ae5a4SJacob Faibussowitsch {
17696858538eSMatthew G. Knepley   const PetscScalar *array;
17702df84da0SMatthew G. Knepley   PetscScalar       *coords = NULL;
17712df84da0SMatthew G. Knepley   const PetscInt     dim    = 3;
17726858538eSMatthew G. Knepley   PetscInt           numCoords, d;
17736858538eSMatthew G. Knepley   PetscBool          isDG;
17742df84da0SMatthew G. Knepley 
17752df84da0SMatthew G. Knepley   PetscFunctionBegin;
17766858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
17776858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
17782df84da0SMatthew G. Knepley   if (!Nq) {
17792df84da0SMatthew G. Knepley     /* Assume that the map to the reference is affine */
17802df84da0SMatthew G. Knepley     *detJ = 0.0;
17819371c9d4SSatish Balay     if (v) {
17829371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
17839371c9d4SSatish Balay     }
17842df84da0SMatthew G. Knepley     if (J) {
17852df84da0SMatthew G. Knepley       for (d = 0; d < dim; d++) {
17862df84da0SMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17872df84da0SMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17882df84da0SMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17892df84da0SMatthew G. Knepley       }
17909566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
17912df84da0SMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
17922df84da0SMatthew G. Knepley     }
1793ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
17942df84da0SMatthew G. Knepley   } else {
17952df84da0SMatthew G. Knepley     const PetscInt dim  = 3;
17962df84da0SMatthew G. Knepley     const PetscInt dimR = 3;
17972df84da0SMatthew G. Knepley     const PetscInt Nv   = 6;
17982df84da0SMatthew G. Knepley     PetscReal      verts[18];
17992df84da0SMatthew G. Knepley     PetscReal      coeff[18];
18002df84da0SMatthew G. Knepley     PetscInt       i, j, k, l;
18012df84da0SMatthew G. Knepley 
18029371c9d4SSatish Balay     for (i = 0; i < Nv; ++i)
18039371c9d4SSatish Balay       for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]);
18042df84da0SMatthew G. Knepley     for (j = 0; j < dim; ++j) {
18052df84da0SMatthew G. Knepley       /* Check for triangle,
18062df84da0SMatthew G. Knepley            phi^0 = -1/2 (xi + eta)  chi^0 = delta(-1, -1)   x(xi) = \sum_k x_k phi^k(xi) = \sum_k chi^k(x) phi^k(xi)
18072df84da0SMatthew G. Knepley            phi^1 =  1/2 (1 + xi)    chi^1 = delta( 1, -1)   y(xi) = \sum_k y_k phi^k(xi) = \sum_k chi^k(y) phi^k(xi)
18082df84da0SMatthew G. Knepley            phi^2 =  1/2 (1 + eta)   chi^2 = delta(-1,  1)
18092df84da0SMatthew G. Knepley 
18102df84da0SMatthew G. Knepley            phi^0 + phi^1 + phi^2 = 1    coef_1   = 1/2 (         chi^1 + chi^2)
18112df84da0SMatthew G. Knepley           -phi^0 + phi^1 - phi^2 = xi   coef_xi  = 1/2 (-chi^0 + chi^1)
18122df84da0SMatthew G. Knepley           -phi^0 - phi^1 + phi^2 = eta  coef_eta = 1/2 (-chi^0         + chi^2)
18132df84da0SMatthew G. Knepley 
18142df84da0SMatthew G. Knepley           < chi_0 chi_1 chi_2> A /  1  1  1 \ / phi_0 \   <chi> I <phi>^T  so we need the inverse transpose
18152df84da0SMatthew G. Knepley                                  | -1  1 -1 | | phi_1 | =
18162df84da0SMatthew G. Knepley                                  \ -1 -1  1 / \ phi_2 /
18172df84da0SMatthew G. Knepley 
18182df84da0SMatthew G. Knepley           Check phi^0: 1/2 (phi^0 chi^1 + phi^0 chi^2 + phi^0 chi^0 - phi^0 chi^1 + phi^0 chi^0 - phi^0 chi^2) = phi^0 chi^0
18192df84da0SMatthew G. Knepley       */
18202df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta):
18212df84da0SMatthew G. Knepley            \phi^0 = 1/4 (   -xi - eta        + xi zeta + eta zeta) --> /  1  1  1  1  1  1 \ 1
18222df84da0SMatthew G. Knepley            \phi^1 = 1/4 (1      + eta - zeta           - eta zeta) --> | -1  1 -1 -1 -1  1 | eta
18232df84da0SMatthew G. Knepley            \phi^2 = 1/4 (1 + xi       - zeta - xi zeta)            --> | -1 -1  1 -1  1 -1 | xi
18242df84da0SMatthew G. Knepley            \phi^3 = 1/4 (   -xi - eta        - xi zeta - eta zeta) --> | -1 -1 -1  1  1  1 | zeta
18252df84da0SMatthew G. Knepley            \phi^4 = 1/4 (1 + xi       + zeta + xi zeta)            --> |  1  1 -1 -1  1 -1 | xi zeta
18262df84da0SMatthew G. Knepley            \phi^5 = 1/4 (1      + eta + zeta           + eta zeta) --> \  1 -1  1 -1 -1  1 / eta zeta
18272df84da0SMatthew G. Knepley            1/4 /  0  1  1  0  1  1 \
18282df84da0SMatthew G. Knepley                | -1  1  0 -1  0  1 |
18292df84da0SMatthew G. Knepley                | -1  0  1 -1  1  0 |
18302df84da0SMatthew G. Knepley                |  0 -1 -1  0  1  1 |
18312df84da0SMatthew G. Knepley                |  1  0 -1 -1  1  0 |
18322df84da0SMatthew G. Knepley                \  1 -1  0 -1  0  1 /
18332df84da0SMatthew G. Knepley       */
18342df84da0SMatthew G. Knepley       coeff[dim * 0 + j] = (1. / 4.) * (verts[dim * 1 + j] + verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
18352df84da0SMatthew G. Knepley       coeff[dim * 1 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
18362df84da0SMatthew G. Knepley       coeff[dim * 2 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
18372df84da0SMatthew G. Knepley       coeff[dim * 3 + j] = (1. / 4.) * (-verts[dim * 1 + j] - verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
18382df84da0SMatthew G. Knepley       coeff[dim * 4 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
18392df84da0SMatthew G. Knepley       coeff[dim * 5 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
18402df84da0SMatthew G. Knepley       /* For reference prism:
18412df84da0SMatthew G. Knepley       {0, 0, 0}
18422df84da0SMatthew G. Knepley       {0, 1, 0}
18432df84da0SMatthew G. Knepley       {1, 0, 0}
18442df84da0SMatthew G. Knepley       {0, 0, 1}
18452df84da0SMatthew G. Knepley       {0, 0, 0}
18462df84da0SMatthew G. Knepley       {0, 0, 0}
18472df84da0SMatthew G. Knepley       */
18482df84da0SMatthew G. Knepley     }
18492df84da0SMatthew G. Knepley     for (i = 0; i < Nq; ++i) {
18502df84da0SMatthew G. Knepley       const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2];
18512df84da0SMatthew G. Knepley 
18522df84da0SMatthew G. Knepley       if (v) {
18532df84da0SMatthew G. Knepley         PetscReal extPoint[6];
18542df84da0SMatthew G. Knepley         PetscInt  c;
18552df84da0SMatthew G. Knepley 
18562df84da0SMatthew G. Knepley         extPoint[0] = 1.;
18572df84da0SMatthew G. Knepley         extPoint[1] = eta;
18582df84da0SMatthew G. Knepley         extPoint[2] = xi;
18592df84da0SMatthew G. Knepley         extPoint[3] = zeta;
18602df84da0SMatthew G. Knepley         extPoint[4] = xi * zeta;
18612df84da0SMatthew G. Knepley         extPoint[5] = eta * zeta;
18622df84da0SMatthew G. Knepley         for (c = 0; c < dim; ++c) {
18632df84da0SMatthew G. Knepley           PetscReal val = 0.;
18642df84da0SMatthew G. Knepley 
1865ad540459SPierre Jolivet           for (k = 0; k < Nv; ++k) val += extPoint[k] * coeff[k * dim + c];
18662df84da0SMatthew G. Knepley           v[i * dim + c] = val;
18672df84da0SMatthew G. Knepley         }
18682df84da0SMatthew G. Knepley       }
18692df84da0SMatthew G. Knepley       if (J) {
18702df84da0SMatthew G. Knepley         PetscReal extJ[18];
18712df84da0SMatthew G. Knepley 
18729371c9d4SSatish Balay         extJ[0]  = 0.;
18739371c9d4SSatish Balay         extJ[1]  = 0.;
18749371c9d4SSatish Balay         extJ[2]  = 0.;
18759371c9d4SSatish Balay         extJ[3]  = 0.;
18769371c9d4SSatish Balay         extJ[4]  = 1.;
18779371c9d4SSatish Balay         extJ[5]  = 0.;
18789371c9d4SSatish Balay         extJ[6]  = 1.;
18799371c9d4SSatish Balay         extJ[7]  = 0.;
18809371c9d4SSatish Balay         extJ[8]  = 0.;
18819371c9d4SSatish Balay         extJ[9]  = 0.;
18829371c9d4SSatish Balay         extJ[10] = 0.;
18839371c9d4SSatish Balay         extJ[11] = 1.;
18849371c9d4SSatish Balay         extJ[12] = zeta;
18859371c9d4SSatish Balay         extJ[13] = 0.;
18869371c9d4SSatish Balay         extJ[14] = xi;
18879371c9d4SSatish Balay         extJ[15] = 0.;
18889371c9d4SSatish Balay         extJ[16] = zeta;
18899371c9d4SSatish Balay         extJ[17] = eta;
18902df84da0SMatthew G. Knepley 
18912df84da0SMatthew G. Knepley         for (j = 0; j < dim; j++) {
18922df84da0SMatthew G. Knepley           for (k = 0; k < dimR; k++) {
18932df84da0SMatthew G. Knepley             PetscReal val = 0.;
18942df84da0SMatthew G. Knepley 
1895ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += coeff[dim * l + j] * extJ[dimR * l + k];
18962df84da0SMatthew G. Knepley             J[i * dim * dim + dim * j + k] = val;
18972df84da0SMatthew G. Knepley           }
18982df84da0SMatthew G. Knepley         }
18992df84da0SMatthew G. Knepley         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1900ad540459SPierre Jolivet         if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
19012df84da0SMatthew G. Knepley       }
19022df84da0SMatthew G. Knepley     }
19032df84da0SMatthew G. Knepley   }
19046858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1905*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19062df84da0SMatthew G. Knepley }
19072df84da0SMatthew G. Knepley 
1908d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1909d71ae5a4SJacob Faibussowitsch {
1910ba2698f1SMatthew G. Knepley   DMPolytopeType   ct;
1911dfccc68fSToby Isaac   PetscInt         depth, dim, coordDim, coneSize, i;
1912dfccc68fSToby Isaac   PetscInt         Nq     = 0;
1913dfccc68fSToby Isaac   const PetscReal *points = NULL;
1914dfccc68fSToby Isaac   DMLabel          depthLabel;
1915c330f8ffSToby Isaac   PetscReal        xi0[3]   = {-1., -1., -1.}, v0[3], J0[9], detJ0;
1916dfccc68fSToby Isaac   PetscBool        isAffine = PETSC_TRUE;
1917dfccc68fSToby Isaac 
1918dfccc68fSToby Isaac   PetscFunctionBegin;
19199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
19209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
19229566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(depthLabel, cell, &dim));
192348a46eb9SPierre Jolivet   if (depth == 1 && dim == 1) PetscCall(DMGetDimension(dm, &dim));
19249566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &coordDim));
192563a3b9bcSJacob Faibussowitsch   PetscCheck(coordDim <= 3, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %" PetscInt_FMT " > 3", coordDim);
19269566063dSJacob Faibussowitsch   if (quad) PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL));
19279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
1928ba2698f1SMatthew G. Knepley   switch (ct) {
1929ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_POINT:
19309566063dSJacob Faibussowitsch     PetscCall(DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ));
1931dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1932dfccc68fSToby Isaac     break;
1933ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
1934412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
19359566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19369566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ));
1937dfccc68fSToby Isaac     break;
1938ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
19399566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19409566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ));
1941dfccc68fSToby Isaac     break;
1942ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
19439566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ));
1944412e9a14SMatthew G. Knepley     isAffine = PETSC_FALSE;
1945412e9a14SMatthew G. Knepley     break;
1946412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
19479566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ));
1948dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1949dfccc68fSToby Isaac     break;
1950ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
19519566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19529566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ));
1953dfccc68fSToby Isaac     break;
1954ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
19559566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
1956dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1957dfccc68fSToby Isaac     break;
19582df84da0SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM:
19599566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
19602df84da0SMatthew G. Knepley     isAffine = PETSC_FALSE;
19612df84da0SMatthew G. Knepley     break;
1962d71ae5a4SJacob Faibussowitsch   default:
1963d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No element geometry for cell %" PetscInt_FMT " with type %s", cell, DMPolytopeTypes[PetscMax(0, PetscMin(ct, DM_NUM_POLYTOPES))]);
1964dfccc68fSToby Isaac   }
19657318780aSToby Isaac   if (isAffine && Nq) {
1966dfccc68fSToby Isaac     if (v) {
1967ad540459SPierre Jolivet       for (i = 0; i < Nq; i++) CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]);
1968dfccc68fSToby Isaac     }
19697318780aSToby Isaac     if (detJ) {
1970ad540459SPierre Jolivet       for (i = 0; i < Nq; i++) detJ[i] = detJ0;
19717318780aSToby Isaac     }
19727318780aSToby Isaac     if (J) {
19737318780aSToby Isaac       PetscInt k;
19747318780aSToby Isaac 
19757318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1976dfccc68fSToby Isaac         PetscInt j;
1977dfccc68fSToby Isaac 
1978ad540459SPierre Jolivet         for (j = 0; j < coordDim * coordDim; j++, k++) J[k] = J0[j];
19797318780aSToby Isaac       }
19807318780aSToby Isaac     }
19817318780aSToby Isaac     if (invJ) {
19827318780aSToby Isaac       PetscInt k;
19837318780aSToby Isaac       switch (coordDim) {
1984d71ae5a4SJacob Faibussowitsch       case 0:
1985d71ae5a4SJacob Faibussowitsch         break;
1986d71ae5a4SJacob Faibussowitsch       case 1:
1987d71ae5a4SJacob Faibussowitsch         invJ[0] = 1. / J0[0];
1988d71ae5a4SJacob Faibussowitsch         break;
1989d71ae5a4SJacob Faibussowitsch       case 2:
1990d71ae5a4SJacob Faibussowitsch         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
1991d71ae5a4SJacob Faibussowitsch         break;
1992d71ae5a4SJacob Faibussowitsch       case 3:
1993d71ae5a4SJacob Faibussowitsch         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
1994d71ae5a4SJacob Faibussowitsch         break;
19957318780aSToby Isaac       }
19967318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
19977318780aSToby Isaac         PetscInt j;
19987318780aSToby Isaac 
1999ad540459SPierre Jolivet         for (j = 0; j < coordDim * coordDim; j++, k++) invJ[k] = invJ[j];
2000dfccc68fSToby Isaac       }
2001dfccc68fSToby Isaac     }
2002dfccc68fSToby Isaac   }
2003*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2004dfccc68fSToby Isaac }
2005dfccc68fSToby Isaac 
2006ccd2543fSMatthew G Knepley /*@C
20078e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
2008ccd2543fSMatthew G Knepley 
2009d083f849SBarry Smith   Collective on dm
2010ccd2543fSMatthew G Knepley 
20114165533cSJose E. Roman   Input Parameters:
2012ccd2543fSMatthew G Knepley + dm   - the DM
2013ccd2543fSMatthew G Knepley - cell - the cell
2014ccd2543fSMatthew G Knepley 
20154165533cSJose E. Roman   Output Parameters:
20169b172b3aSMatthew Knepley + v0   - the translation part of this affine transform, meaning the translation to the origin (not the first vertex of the reference cell)
2017ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
2018ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
2019ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
2020ccd2543fSMatthew G Knepley 
2021ccd2543fSMatthew G Knepley   Level: advanced
2022ccd2543fSMatthew G Knepley 
2023ccd2543fSMatthew G Knepley   Fortran Notes:
2024ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2025ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
2026ccd2543fSMatthew G Knepley 
2027db781477SPatrick Sanan .seealso: `DMPlexComputeCellGeometryFEM()`, `DMGetCoordinateSection()`, `DMGetCoordinates()`
2028ccd2543fSMatthew G Knepley @*/
2029d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
2030d71ae5a4SJacob Faibussowitsch {
2031ccd2543fSMatthew G Knepley   PetscFunctionBegin;
20329566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, NULL, v0, J, invJ, detJ));
2033*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20348e0841e0SMatthew G. Knepley }
20358e0841e0SMatthew G. Knepley 
2036d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
2037d71ae5a4SJacob Faibussowitsch {
20386858538eSMatthew G. Knepley   const PetscScalar *array;
20398e0841e0SMatthew G. Knepley   PetscScalar       *coords = NULL;
20406858538eSMatthew G. Knepley   PetscInt           numCoords;
20416858538eSMatthew G. Knepley   PetscBool          isDG;
20426858538eSMatthew G. Knepley   PetscQuadrature    feQuad;
20438e0841e0SMatthew G. Knepley   const PetscReal   *quadPoints;
2044ef0bb6c7SMatthew G. Knepley   PetscTabulation    T;
20456858538eSMatthew G. Knepley   PetscInt           dim, cdim, pdim, qdim, Nq, q;
20468e0841e0SMatthew G. Knepley 
20478e0841e0SMatthew G. Knepley   PetscFunctionBegin;
20489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
20499566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
20506858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
2051dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
2052dfccc68fSToby Isaac     PetscDualSpace dsp;
2053dfccc68fSToby Isaac 
20549566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(fe, &dsp));
20559566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetFunctional(dsp, 0, &quad));
20569566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2057dfccc68fSToby Isaac     Nq = 1;
2058dfccc68fSToby Isaac   } else {
20599566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2060dfccc68fSToby Isaac   }
20619566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
20629566063dSJacob Faibussowitsch   PetscCall(PetscFEGetQuadrature(fe, &feQuad));
2063dfccc68fSToby Isaac   if (feQuad == quad) {
20649566063dSJacob Faibussowitsch     PetscCall(PetscFEGetCellTabulation(fe, J ? 1 : 0, &T));
206563a3b9bcSJacob Faibussowitsch     PetscCheck(numCoords == pdim * cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "There are %" PetscInt_FMT " coordinates for point %" PetscInt_FMT " != %" PetscInt_FMT "*%" PetscInt_FMT, numCoords, point, pdim, cdim);
2066dfccc68fSToby Isaac   } else {
20679566063dSJacob Faibussowitsch     PetscCall(PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T));
2068dfccc68fSToby Isaac   }
206963a3b9bcSJacob Faibussowitsch   PetscCheck(qdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %" PetscInt_FMT " != quadrature dimension %" PetscInt_FMT, dim, qdim);
2070ef0bb6c7SMatthew G. Knepley   {
2071ef0bb6c7SMatthew G. Knepley     const PetscReal *basis    = T->T[0];
2072ef0bb6c7SMatthew G. Knepley     const PetscReal *basisDer = T->T[1];
2073ef0bb6c7SMatthew G. Knepley     PetscReal        detJt;
2074ef0bb6c7SMatthew G. Knepley 
2075a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
207663a3b9bcSJacob Faibussowitsch     PetscCheck(Nq == T->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %" PetscInt_FMT " != %" PetscInt_FMT, Nq, T->Np);
207763a3b9bcSJacob Faibussowitsch     PetscCheck(pdim == T->Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %" PetscInt_FMT " != %" PetscInt_FMT, pdim, T->Nb);
207863a3b9bcSJacob Faibussowitsch     PetscCheck(dim == T->Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %" PetscInt_FMT " != %" PetscInt_FMT, dim, T->Nc);
207963a3b9bcSJacob Faibussowitsch     PetscCheck(cdim == T->cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %" PetscInt_FMT " != %" PetscInt_FMT, cdim, T->cdim);
2080a2a9e04cSMatthew G. Knepley #endif
2081dfccc68fSToby Isaac     if (v) {
20829566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(v, Nq * cdim));
2083f960e424SToby Isaac       for (q = 0; q < Nq; ++q) {
2084f960e424SToby Isaac         PetscInt i, k;
2085f960e424SToby Isaac 
2086301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2087301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
2088ad540459SPierre Jolivet           for (i = 0; i < cdim; ++i) v[q * cdim + i] += basis[(q * pdim + k) * cdim + i] * PetscRealPart(coords[vertex * cdim + i]);
2089301b184aSMatthew G. Knepley         }
20909566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * cdim));
2091f960e424SToby Isaac       }
2092f960e424SToby Isaac     }
20938e0841e0SMatthew G. Knepley     if (J) {
20949566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(J, Nq * cdim * cdim));
20958e0841e0SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
20968e0841e0SMatthew G. Knepley         PetscInt i, j, k, c, r;
20978e0841e0SMatthew G. Knepley 
20988e0841e0SMatthew G. Knepley         /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
2099301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2100301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
2101301b184aSMatthew G. Knepley           for (j = 0; j < dim; ++j) {
2102ad540459SPierre Jolivet             for (i = 0; i < cdim; ++i) J[(q * cdim + i) * cdim + j] += basisDer[((q * pdim + k) * cdim + i) * dim + j] * PetscRealPart(coords[vertex * cdim + i]);
2103301b184aSMatthew G. Knepley           }
2104301b184aSMatthew G. Knepley         }
21059566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * dim * cdim));
21068e0841e0SMatthew G. Knepley         if (cdim > dim) {
21078e0841e0SMatthew G. Knepley           for (c = dim; c < cdim; ++c)
21089371c9d4SSatish Balay             for (r = 0; r < cdim; ++r) J[r * cdim + c] = r == c ? 1.0 : 0.0;
21098e0841e0SMatthew G. Knepley         }
2110f960e424SToby Isaac         if (!detJ && !invJ) continue;
2111a63b72c6SToby Isaac         detJt = 0.;
21128e0841e0SMatthew G. Knepley         switch (cdim) {
21138e0841e0SMatthew G. Knepley         case 3:
2114037dc194SToby Isaac           DMPlex_Det3D_Internal(&detJt, &J[q * cdim * dim]);
2115ad540459SPierre Jolivet           if (invJ) DMPlex_Invert3D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt);
211617fe8556SMatthew G. Knepley           break;
211749dc4407SMatthew G. Knepley         case 2:
21189f328543SToby Isaac           DMPlex_Det2D_Internal(&detJt, &J[q * cdim * dim]);
2119ad540459SPierre Jolivet           if (invJ) DMPlex_Invert2D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt);
212049dc4407SMatthew G. Knepley           break;
21218e0841e0SMatthew G. Knepley         case 1:
2122037dc194SToby Isaac           detJt = J[q * cdim * dim];
2123037dc194SToby Isaac           if (invJ) invJ[q * cdim * dim] = 1.0 / detJt;
212449dc4407SMatthew G. Knepley         }
2125f960e424SToby Isaac         if (detJ) detJ[q] = detJt;
212649dc4407SMatthew G. Knepley       }
212708401ef6SPierre Jolivet     } else PetscCheck(!detJ && !invJ, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
212849dc4407SMatthew G. Knepley   }
21299566063dSJacob Faibussowitsch   if (feQuad != quad) PetscCall(PetscTabulationDestroy(&T));
21306858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
2131*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21328e0841e0SMatthew G. Knepley }
21338e0841e0SMatthew G. Knepley 
21348e0841e0SMatthew G. Knepley /*@C
21358e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
21368e0841e0SMatthew G. Knepley 
2137d083f849SBarry Smith   Collective on dm
21388e0841e0SMatthew G. Knepley 
21394165533cSJose E. Roman   Input Parameters:
21408e0841e0SMatthew G. Knepley + dm   - the DM
21418e0841e0SMatthew G. Knepley . cell - the cell
2142dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
2143dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
21448e0841e0SMatthew G. Knepley 
21454165533cSJose E. Roman   Output Parameters:
2146dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
21478e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
21488e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
21498e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
21508e0841e0SMatthew G. Knepley 
21518e0841e0SMatthew G. Knepley   Level: advanced
21528e0841e0SMatthew G. Knepley 
21538e0841e0SMatthew G. Knepley   Fortran Notes:
21548e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
21558e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
21568e0841e0SMatthew G. Knepley 
2157db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()`
21588e0841e0SMatthew G. Knepley @*/
2159d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
2160d71ae5a4SJacob Faibussowitsch {
2161bb4a5db5SMatthew G. Knepley   DM      cdm;
2162dfccc68fSToby Isaac   PetscFE fe = NULL;
21638e0841e0SMatthew G. Knepley 
21648e0841e0SMatthew G. Knepley   PetscFunctionBegin;
2165dadcf809SJacob Faibussowitsch   PetscValidRealPointer(detJ, 7);
21669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
2167bb4a5db5SMatthew G. Knepley   if (cdm) {
2168dfccc68fSToby Isaac     PetscClassId id;
2169dfccc68fSToby Isaac     PetscInt     numFields;
2170e5e52638SMatthew G. Knepley     PetscDS      prob;
2171dfccc68fSToby Isaac     PetscObject  disc;
2172dfccc68fSToby Isaac 
21739566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(cdm, &numFields));
2174dfccc68fSToby Isaac     if (numFields) {
21759566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &prob));
21769566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(prob, 0, &disc));
21779566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
2178ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
2179dfccc68fSToby Isaac     }
2180dfccc68fSToby Isaac   }
21819566063dSJacob Faibussowitsch   if (!fe) PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ));
21829566063dSJacob Faibussowitsch   else PetscCall(DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ));
2183*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2184ccd2543fSMatthew G Knepley }
2185834e62ceSMatthew G. Knepley 
2186d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2187d71ae5a4SJacob Faibussowitsch {
21889bf2564aSMatt McGurn   PetscSection       coordSection;
21899bf2564aSMatt McGurn   Vec                coordinates;
21909bf2564aSMatt McGurn   const PetscScalar *coords = NULL;
21919bf2564aSMatt McGurn   PetscInt           d, dof, off;
21929bf2564aSMatt McGurn 
21939bf2564aSMatt McGurn   PetscFunctionBegin;
21949566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
21959566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
21969566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
21979bf2564aSMatt McGurn 
21989bf2564aSMatt McGurn   /* for a point the centroid is just the coord */
21999bf2564aSMatt McGurn   if (centroid) {
22009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
22019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
2202ad540459SPierre Jolivet     for (d = 0; d < dof; d++) centroid[d] = PetscRealPart(coords[off + d]);
22039bf2564aSMatt McGurn   }
22049bf2564aSMatt McGurn   if (normal) {
22059bf2564aSMatt McGurn     const PetscInt *support, *cones;
22069bf2564aSMatt McGurn     PetscInt        supportSize;
22079bf2564aSMatt McGurn     PetscReal       norm, sign;
22089bf2564aSMatt McGurn 
22099bf2564aSMatt McGurn     /* compute the norm based upon the support centroids */
22109566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, cell, &supportSize));
22119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, cell, &support));
22129566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL));
22139bf2564aSMatt McGurn 
22149bf2564aSMatt McGurn     /* Take the normal from the centroid of the support to the vertex*/
22159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
22169566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
2217ad540459SPierre Jolivet     for (d = 0; d < dof; d++) normal[d] -= PetscRealPart(coords[off + d]);
22189bf2564aSMatt McGurn 
22199bf2564aSMatt McGurn     /* Determine the sign of the normal based upon its location in the support */
22209566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, support[0], &cones));
22219bf2564aSMatt McGurn     sign = cones[0] == cell ? 1.0 : -1.0;
22229bf2564aSMatt McGurn 
22239bf2564aSMatt McGurn     norm = DMPlex_NormD_Internal(dim, normal);
22249bf2564aSMatt McGurn     for (d = 0; d < dim; ++d) normal[d] /= (norm * sign);
22259bf2564aSMatt McGurn   }
2226ad540459SPierre Jolivet   if (vol) *vol = 1.0;
22279566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
2228*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22299bf2564aSMatt McGurn }
22309bf2564aSMatt McGurn 
2231d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2232d71ae5a4SJacob Faibussowitsch {
22336858538eSMatthew G. Knepley   const PetscScalar *array;
2234a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
223521d6a034SMatthew G. Knepley   PetscInt           cdim, coordSize, d;
22366858538eSMatthew G. Knepley   PetscBool          isDG;
2237cc08537eSMatthew G. Knepley 
2238cc08537eSMatthew G. Knepley   PetscFunctionBegin;
223921d6a034SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
22406858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
224121d6a034SMatthew G. Knepley   PetscCheck(coordSize == cdim * 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Edge has %" PetscInt_FMT " coordinates != %" PetscInt_FMT, coordSize, cdim * 2);
2242cc08537eSMatthew G. Knepley   if (centroid) {
224321d6a034SMatthew G. Knepley     for (d = 0; d < cdim; ++d) centroid[d] = 0.5 * PetscRealPart(coords[d] + coords[cdim + d]);
2244cc08537eSMatthew G. Knepley   }
2245cc08537eSMatthew G. Knepley   if (normal) {
2246a60a936bSMatthew G. Knepley     PetscReal norm;
2247a60a936bSMatthew G. Knepley 
224821d6a034SMatthew G. Knepley     switch (cdim) {
224921d6a034SMatthew G. Knepley     case 3:
2250f315e28eSPierre Jolivet       normal[2] = 0.; /* fall through */
225121d6a034SMatthew G. Knepley     case 2:
225221d6a034SMatthew G. Knepley       normal[0] = -PetscRealPart(coords[1] - coords[cdim + 1]);
225321d6a034SMatthew G. Knepley       normal[1] = PetscRealPart(coords[0] - coords[cdim + 0]);
225421d6a034SMatthew G. Knepley       break;
225521d6a034SMatthew G. Knepley     case 1:
225621d6a034SMatthew G. Knepley       normal[0] = 1.0;
225721d6a034SMatthew G. Knepley       break;
225821d6a034SMatthew G. Knepley     default:
225921d6a034SMatthew G. Knepley       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Dimension %" PetscInt_FMT " not supported", cdim);
226021d6a034SMatthew G. Knepley     }
226121d6a034SMatthew G. Knepley     norm = DMPlex_NormD_Internal(cdim, normal);
226221d6a034SMatthew G. Knepley     for (d = 0; d < cdim; ++d) normal[d] /= norm;
2263cc08537eSMatthew G. Knepley   }
2264cc08537eSMatthew G. Knepley   if (vol) {
2265714b99b6SMatthew G. Knepley     *vol = 0.0;
226621d6a034SMatthew G. Knepley     for (d = 0; d < cdim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - coords[cdim + d]));
2267714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
2268cc08537eSMatthew G. Knepley   }
22696858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2270*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2271cc08537eSMatthew G. Knepley }
2272cc08537eSMatthew G. Knepley 
2273cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */
2274d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2275d71ae5a4SJacob Faibussowitsch {
2276412e9a14SMatthew G. Knepley   DMPolytopeType     ct;
22776858538eSMatthew G. Knepley   const PetscScalar *array;
2278cc08537eSMatthew G. Knepley   PetscScalar       *coords = NULL;
22796858538eSMatthew G. Knepley   PetscInt           coordSize;
22806858538eSMatthew G. Knepley   PetscBool          isDG;
2281793a2a13SMatthew G. Knepley   PetscInt           fv[4] = {0, 1, 2, 3};
22826858538eSMatthew G. Knepley   PetscInt           cdim, numCorners, p, d;
2283cc08537eSMatthew G. Knepley 
2284cc08537eSMatthew G. Knepley   PetscFunctionBegin;
2285793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
22869566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2287412e9a14SMatthew G. Knepley   switch (ct) {
22889371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR:
22899371c9d4SSatish Balay     fv[2] = 3;
22909371c9d4SSatish Balay     fv[3] = 2;
22919371c9d4SSatish Balay     break;
2292d71ae5a4SJacob Faibussowitsch   default:
2293d71ae5a4SJacob Faibussowitsch     break;
2294412e9a14SMatthew G. Knepley   }
22959566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
22966858538eSMatthew G. Knepley   PetscCall(DMPlexGetConeSize(dm, cell, &numCorners));
22976858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
22983f27a4e6SJed Brown   {
22993f27a4e6SJed Brown     PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm;
2300793a2a13SMatthew G. Knepley 
23013f27a4e6SJed Brown     for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]);
23024f99dae5SMatthew G. Knepley     for (p = 0; p < numCorners - 2; ++p) {
23033f27a4e6SJed Brown       PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.};
23043f27a4e6SJed Brown       for (d = 0; d < cdim; d++) {
23053f27a4e6SJed Brown         e0[d] = PetscRealPart(coords[cdim * fv[p + 1] + d]) - origin[d];
23063f27a4e6SJed Brown         e1[d] = PetscRealPart(coords[cdim * fv[p + 2] + d]) - origin[d];
23073f27a4e6SJed Brown       }
23083f27a4e6SJed Brown       const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1];
23093f27a4e6SJed Brown       const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2];
23103f27a4e6SJed Brown       const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0];
23113f27a4e6SJed Brown       const PetscReal a  = PetscSqrtReal(dx * dx + dy * dy + dz * dz);
23124f99dae5SMatthew G. Knepley 
23134f99dae5SMatthew G. Knepley       n[0] += dx;
23144f99dae5SMatthew G. Knepley       n[1] += dy;
23154f99dae5SMatthew G. Knepley       n[2] += dz;
2316ad540459SPierre 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.;
2317ceee4971SMatthew G. Knepley     }
23184f99dae5SMatthew G. Knepley     norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
23194f99dae5SMatthew G. Knepley     n[0] /= norm;
23204f99dae5SMatthew G. Knepley     n[1] /= norm;
23214f99dae5SMatthew G. Knepley     n[2] /= norm;
23224f99dae5SMatthew G. Knepley     c[0] /= norm;
23234f99dae5SMatthew G. Knepley     c[1] /= norm;
23244f99dae5SMatthew G. Knepley     c[2] /= norm;
23254f99dae5SMatthew G. Knepley     if (vol) *vol = 0.5 * norm;
23269371c9d4SSatish Balay     if (centroid)
23279371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) centroid[d] = c[d];
23289371c9d4SSatish Balay     if (normal)
23299371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) normal[d] = n[d];
23300a1d6728SMatthew G. Knepley   }
23316858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2332*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2333cc08537eSMatthew G. Knepley }
2334cc08537eSMatthew G. Knepley 
23350ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */
2336d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2337d71ae5a4SJacob Faibussowitsch {
2338412e9a14SMatthew G. Knepley   DMPolytopeType        ct;
23396858538eSMatthew G. Knepley   const PetscScalar    *array;
23400ec8681fSMatthew G. Knepley   PetscScalar          *coords = NULL;
23416858538eSMatthew G. Knepley   PetscInt              coordSize;
23426858538eSMatthew G. Knepley   PetscBool             isDG;
23433f27a4e6SJed Brown   PetscReal             vsum      = 0.0, vtmp, coordsTmp[3 * 3], origin[3];
23446858538eSMatthew G. Knepley   const PetscInt        order[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
23456858538eSMatthew G. Knepley   const PetscInt       *cone, *faceSizes, *faces;
23466858538eSMatthew G. Knepley   const DMPolytopeType *faceTypes;
2347793a2a13SMatthew G. Knepley   PetscBool             isHybrid = PETSC_FALSE;
23486858538eSMatthew G. Knepley   PetscInt              numFaces, f, fOff = 0, p, d;
23490ec8681fSMatthew G. Knepley 
23500ec8681fSMatthew G. Knepley   PetscFunctionBegin;
235163a3b9bcSJacob Faibussowitsch   PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No support for dim %" PetscInt_FMT " > 3", dim);
2352793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
23539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2354412e9a14SMatthew G. Knepley   switch (ct) {
2355412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
2356412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
2357412e9a14SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR:
2358d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
2359d71ae5a4SJacob Faibussowitsch     isHybrid = PETSC_TRUE;
2360d71ae5a4SJacob Faibussowitsch   default:
2361d71ae5a4SJacob Faibussowitsch     break;
2362412e9a14SMatthew G. Knepley   }
2363793a2a13SMatthew G. Knepley 
23649371c9d4SSatish Balay   if (centroid)
23659371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = 0.0;
23666858538eSMatthew G. Knepley   PetscCall(DMPlexGetCone(dm, cell, &cone));
23676858538eSMatthew G. Knepley 
23686858538eSMatthew G. Knepley   // Using the closure of faces for coordinates does not work in periodic geometries, so we index into the cell coordinates
23696858538eSMatthew G. Knepley   PetscCall(DMPlexGetRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
23706858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
23710ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
2372793a2a13SMatthew G. Knepley     PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
2373793a2a13SMatthew G. Knepley 
23743f27a4e6SJed Brown     // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and
23753f27a4e6SJed Brown     // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex
23763f27a4e6SJed Brown     // so that all tetrahedra have positive volume.
23779371c9d4SSatish Balay     if (f == 0)
23789371c9d4SSatish Balay       for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]);
23796858538eSMatthew G. Knepley     switch (faceTypes[f]) {
2380ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
23810ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
23826858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + 0] * dim + d]) - origin[d];
23836858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + 1] * dim + d]) - origin[d];
23846858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + 2] * dim + d]) - origin[d];
23850ec8681fSMatthew G. Knepley       }
23860ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
23876858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
23880ec8681fSMatthew G. Knepley       vsum += vtmp;
23894f25033aSJed Brown       if (centroid) { /* Centroid of OABC = (a+b+c)/4 */
23900ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
23911ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
23920ec8681fSMatthew G. Knepley         }
23930ec8681fSMatthew G. Knepley       }
23940ec8681fSMatthew G. Knepley       break;
2395ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
23969371c9d4SSatish Balay     case DM_POLYTOPE_SEG_PRISM_TENSOR: {
2397793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
2398793a2a13SMatthew G. Knepley 
2399793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
24009371c9d4SSatish Balay       if (isHybrid && f > 1) {
24019371c9d4SSatish Balay         fv[2] = 3;
24029371c9d4SSatish Balay         fv[3] = 2;
24039371c9d4SSatish Balay       }
24040ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
24050ec8681fSMatthew G. Knepley       /* First tet */
24060ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
24076858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[0]] * dim + d]) - origin[d];
24086858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
24096858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
24100ec8681fSMatthew G. Knepley       }
24110ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
24126858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
24130ec8681fSMatthew G. Knepley       vsum += vtmp;
24140ec8681fSMatthew G. Knepley       if (centroid) {
24150ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
24160ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
24170ec8681fSMatthew G. Knepley         }
24180ec8681fSMatthew G. Knepley       }
24190ec8681fSMatthew G. Knepley       /* Second tet */
24200ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
24216858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
24226858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[2]] * dim + d]) - origin[d];
24236858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
24240ec8681fSMatthew G. Knepley       }
24250ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
24266858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
24270ec8681fSMatthew G. Knepley       vsum += vtmp;
24280ec8681fSMatthew G. Knepley       if (centroid) {
24290ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
24300ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
24310ec8681fSMatthew G. Knepley         }
24320ec8681fSMatthew G. Knepley       }
24330ec8681fSMatthew G. Knepley       break;
2434793a2a13SMatthew G. Knepley     }
2435d71ae5a4SJacob Faibussowitsch     default:
2436d71ae5a4SJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %" PetscInt_FMT " of type %s", cone[f], DMPolytopeTypes[ct]);
24370ec8681fSMatthew G. Knepley     }
24386858538eSMatthew G. Knepley     fOff += faceSizes[f];
24390ec8681fSMatthew G. Knepley   }
24406858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
24416858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
24428763be8eSMatthew G. Knepley   if (vol) *vol = PetscAbsReal(vsum);
24439371c9d4SSatish Balay   if (normal)
24449371c9d4SSatish Balay     for (d = 0; d < dim; ++d) normal[d] = 0.0;
24459371c9d4SSatish Balay   if (centroid)
24469371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum * 4) + origin[d];
2447*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24480ec8681fSMatthew G. Knepley }
24490ec8681fSMatthew G. Knepley 
2450834e62ceSMatthew G. Knepley /*@C
2451834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
2452834e62ceSMatthew G. Knepley 
2453d083f849SBarry Smith   Collective on dm
2454834e62ceSMatthew G. Knepley 
24554165533cSJose E. Roman   Input Parameters:
2456834e62ceSMatthew G. Knepley + dm   - the DM
2457834e62ceSMatthew G. Knepley - cell - the cell
2458834e62ceSMatthew G. Knepley 
24594165533cSJose E. Roman   Output Parameters:
2460834e62ceSMatthew G. Knepley + volume   - the cell volume
2461cc08537eSMatthew G. Knepley . centroid - the cell centroid
2462cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
2463834e62ceSMatthew G. Knepley 
2464834e62ceSMatthew G. Knepley   Level: advanced
2465834e62ceSMatthew G. Knepley 
2466834e62ceSMatthew G. Knepley   Fortran Notes:
2467834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2468834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
2469834e62ceSMatthew G. Knepley 
2470db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()`
2471834e62ceSMatthew G. Knepley @*/
2472d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2473d71ae5a4SJacob Faibussowitsch {
24740ec8681fSMatthew G. Knepley   PetscInt depth, dim;
2475834e62ceSMatthew G. Knepley 
2476834e62ceSMatthew G. Knepley   PetscFunctionBegin;
24779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24789566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
247908401ef6SPierre Jolivet   PetscCheck(depth == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
24809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetPointDepth(dm, cell, &depth));
2481011ea5d8SMatthew G. Knepley   switch (depth) {
2482d71ae5a4SJacob Faibussowitsch   case 0:
2483d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal));
2484d71ae5a4SJacob Faibussowitsch     break;
2485d71ae5a4SJacob Faibussowitsch   case 1:
2486d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal));
2487d71ae5a4SJacob Faibussowitsch     break;
2488d71ae5a4SJacob Faibussowitsch   case 2:
2489d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal));
2490d71ae5a4SJacob Faibussowitsch     break;
2491d71ae5a4SJacob Faibussowitsch   case 3:
2492d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal));
2493d71ae5a4SJacob Faibussowitsch     break;
2494d71ae5a4SJacob Faibussowitsch   default:
2495d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT " (depth %" PetscInt_FMT ") for element geometry computation", dim, depth);
2496834e62ceSMatthew G. Knepley   }
2497*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2498834e62ceSMatthew G. Knepley }
2499113c68e6SMatthew G. Knepley 
2500c501906fSMatthew G. Knepley /*@
2501891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2502891a9168SMatthew G. Knepley 
2503891a9168SMatthew G. Knepley   Input Parameter:
2504891a9168SMatthew G. Knepley . dm - The DM
2505891a9168SMatthew G. Knepley 
2506891a9168SMatthew G. Knepley   Output Parameters:
2507891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2508a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data
2509891a9168SMatthew G. Knepley 
2510891a9168SMatthew G. Knepley   Level: developer
2511891a9168SMatthew G. Knepley 
2512455ad57eSMatthew Knepley .seealso: `PetscFVFaceGeom`, `PetscFVCellGeom`
2513891a9168SMatthew G. Knepley @*/
2514d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2515d71ae5a4SJacob Faibussowitsch {
2516113c68e6SMatthew G. Knepley   DM           dmFace, dmCell;
2517113c68e6SMatthew G. Knepley   DMLabel      ghostLabel;
2518113c68e6SMatthew G. Knepley   PetscSection sectionFace, sectionCell;
2519113c68e6SMatthew G. Knepley   PetscSection coordSection;
2520113c68e6SMatthew G. Knepley   Vec          coordinates;
2521113c68e6SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2522113c68e6SMatthew G. Knepley   PetscReal    minradius, gminradius;
2523113c68e6SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2524113c68e6SMatthew G. Knepley 
2525113c68e6SMatthew G. Knepley   PetscFunctionBegin;
25269566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25279566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
25289566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
2529113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
25309566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmCell));
25319566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection));
25329566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmCell, coordinates));
25339566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionCell));
25349566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
25359566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
25369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd));
25379566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVCellGeom)) / sizeof(PetscScalar))));
25389566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionCell));
25399566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmCell, sectionCell));
25409566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionCell));
25419566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmCell, cellgeom));
2542485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
25439566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*cellgeom, &cgeom));
2544113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2545113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2546113c68e6SMatthew G. Knepley 
25479566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg));
25489566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(cg, 1));
25499566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL));
2550113c68e6SMatthew G. Knepley   }
2551113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
25529566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmFace));
25539566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionFace));
25549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
25559566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionFace, fStart, fEnd));
25569566063dSJacob Faibussowitsch   for (f = fStart; f < fEnd; ++f) PetscCall(PetscSectionSetDof(sectionFace, f, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVFaceGeom)) / sizeof(PetscScalar))));
25579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionFace));
25589566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmFace, sectionFace));
25599566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionFace));
25609566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmFace, facegeom));
25619566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*facegeom, &fgeom));
25629566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2563113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2564113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2565113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2566113c68e6SMatthew G. Knepley     PetscReal        area;
2567412e9a14SMatthew G. Knepley     const PetscInt  *cells;
2568412e9a14SMatthew G. Knepley     PetscInt         ncells, ghost = -1, d, numChildren;
2569113c68e6SMatthew G. Knepley 
25709566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
25719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
25729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, f, &cells));
25739566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &ncells));
2574412e9a14SMatthew G. Knepley     /* It is possible to get a face with no support when using partition overlap */
2575412e9a14SMatthew G. Knepley     if (!ncells || ghost >= 0 || numChildren) continue;
25769566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, f, fgeom, &fg));
25779566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal));
2578113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2579113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2580113c68e6SMatthew G. Knepley     {
2581113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
2582113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
25830453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2584113c68e6SMatthew G. Knepley 
25859566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL));
2586113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
258706348e87SToby Isaac       if (ncells > 1) {
25889566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR));
2589113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
25909371c9d4SSatish Balay       } else {
259106348e87SToby Isaac         rcentroid = fg->centroid;
259206348e87SToby Isaac       }
25939566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l));
25949566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r));
25950453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2596113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2597113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2598113c68e6SMatthew G. Knepley       }
2599113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
260063a3b9bcSJacob 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]);
260163a3b9bcSJacob 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]);
260263a3b9bcSJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " could not be fixed", f);
2603113c68e6SMatthew G. Knepley       }
2604113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2605113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2606113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2607113c68e6SMatthew G. Knepley       }
260806348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2609113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2610113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2611113c68e6SMatthew G. Knepley       }
2612113c68e6SMatthew G. Knepley     }
2613113c68e6SMatthew G. Knepley   }
26141c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm)));
26159566063dSJacob Faibussowitsch   PetscCall(DMPlexSetMinRadius(dm, gminradius));
2616113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2617113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2618113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2619113c68e6SMatthew G. Knepley     const PetscInt  *cone, *support;
2620113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2621113c68e6SMatthew G. Knepley 
26229566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dmCell, c, &coneSize));
262363a3b9bcSJacob Faibussowitsch     PetscCheck(coneSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %" PetscInt_FMT " has cone size %" PetscInt_FMT " != 1", c, coneSize);
26249566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dmCell, c, &cone));
26259566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmCell, cone[0], &supportSize));
262663a3b9bcSJacob Faibussowitsch     PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " has support size %" PetscInt_FMT " != 2", cone[0], supportSize);
26279566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dmCell, cone[0], &support));
26289566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg));
2629113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2630113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2631113c68e6SMatthew G. Knepley       if (support[s] == c) {
2632640bce14SSatish Balay         PetscFVCellGeom *ci;
2633113c68e6SMatthew G. Knepley         PetscFVCellGeom *cg;
2634113c68e6SMatthew G. Knepley         PetscReal        c2f[3], a;
2635113c68e6SMatthew G. Knepley 
26369566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, support[(s + 1) % 2], cgeom, &ci));
2637113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2638113c68e6SMatthew G. Knepley         a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal) / DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
26399566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg));
2640113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2 * a, fg->normal, ci->centroid, cg->centroid);
2641113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2642113c68e6SMatthew G. Knepley       }
2643113c68e6SMatthew G. Knepley     }
2644113c68e6SMatthew G. Knepley   }
26459566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*facegeom, &fgeom));
26469566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*cellgeom, &cgeom));
26479566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmCell));
26489566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmFace));
2649*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2650113c68e6SMatthew G. Knepley }
2651113c68e6SMatthew G. Knepley 
2652113c68e6SMatthew G. Knepley /*@C
2653113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2654113c68e6SMatthew G. Knepley 
2655113c68e6SMatthew G. Knepley   Not collective
2656113c68e6SMatthew G. Knepley 
26574165533cSJose E. Roman   Input Parameter:
2658113c68e6SMatthew G. Knepley . dm - the DM
2659113c68e6SMatthew G. Knepley 
26604165533cSJose E. Roman   Output Parameter:
2661a5b23f4aSJose E. Roman . minradius - the minimum cell radius
2662113c68e6SMatthew G. Knepley 
2663113c68e6SMatthew G. Knepley   Level: developer
2664113c68e6SMatthew G. Knepley 
2665db781477SPatrick Sanan .seealso: `DMGetCoordinates()`
2666113c68e6SMatthew G. Knepley @*/
2667d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2668d71ae5a4SJacob Faibussowitsch {
2669113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2670113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2671dadcf809SJacob Faibussowitsch   PetscValidRealPointer(minradius, 2);
2672113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex *)dm->data)->minradius;
2673*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2674113c68e6SMatthew G. Knepley }
2675113c68e6SMatthew G. Knepley 
2676113c68e6SMatthew G. Knepley /*@C
2677113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2678113c68e6SMatthew G. Knepley 
2679113c68e6SMatthew G. Knepley   Logically collective
2680113c68e6SMatthew G. Knepley 
26814165533cSJose E. Roman   Input Parameters:
2682113c68e6SMatthew G. Knepley + dm - the DM
2683a5b23f4aSJose E. Roman - minradius - the minimum cell radius
2684113c68e6SMatthew G. Knepley 
2685113c68e6SMatthew G. Knepley   Level: developer
2686113c68e6SMatthew G. Knepley 
2687db781477SPatrick Sanan .seealso: `DMSetCoordinates()`
2688113c68e6SMatthew G. Knepley @*/
2689d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2690d71ae5a4SJacob Faibussowitsch {
2691113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2692113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2693113c68e6SMatthew G. Knepley   ((DM_Plex *)dm->data)->minradius = minradius;
2694*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2695113c68e6SMatthew G. Knepley }
2696856ac710SMatthew G. Knepley 
2697d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2698d71ae5a4SJacob Faibussowitsch {
2699856ac710SMatthew G. Knepley   DMLabel      ghostLabel;
2700856ac710SMatthew G. Knepley   PetscScalar *dx, *grad, **gref;
2701856ac710SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2702856ac710SMatthew G. Knepley 
2703856ac710SMatthew G. Knepley   PetscFunctionBegin;
27049566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
27059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
27069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2707089217ebSMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
27089566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxNumFaces, NULL));
27099566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
27109566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
27119566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2712856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2713856ac710SMatthew G. Knepley     const PetscInt  *faces;
2714856ac710SMatthew G. Knepley     PetscInt         numFaces, usedFaces, f, d;
2715640bce14SSatish Balay     PetscFVCellGeom *cg;
2716856ac710SMatthew G. Knepley     PetscBool        boundary;
2717856ac710SMatthew G. Knepley     PetscInt         ghost;
2718856ac710SMatthew G. Knepley 
2719a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
2720a79418b7SMatt McGurn     PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2721a79418b7SMatt McGurn     if (ghost >= 0) continue;
2722a79418b7SMatt McGurn 
27239566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
27249566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &numFaces));
27259566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &faces));
272663a3b9bcSJacob 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);
2727856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2728640bce14SSatish Balay       PetscFVCellGeom *cg1;
2729856ac710SMatthew G. Knepley       PetscFVFaceGeom *fg;
2730856ac710SMatthew G. Knepley       const PetscInt  *fcells;
2731856ac710SMatthew G. Knepley       PetscInt         ncell, side;
2732856ac710SMatthew G. Knepley 
27339566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
27349566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2735856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
27369566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, faces[f], &fcells));
2737856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2738856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
27399566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg));
27409566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2741856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces * dim + d] = cg1->centroid[d] - cg->centroid[d];
2742856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */
2743856ac710SMatthew G. Knepley     }
274428b400f6SJacob Faibussowitsch     PetscCheck(usedFaces, PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
27459566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, usedFaces, dx, grad));
2746856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
27479566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
27489566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2749856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2750856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces * dim + d];
2751856ac710SMatthew G. Knepley       ++usedFaces;
2752856ac710SMatthew G. Knepley     }
2753856ac710SMatthew G. Knepley   }
27549566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
2755*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2756856ac710SMatthew G. Knepley }
2757856ac710SMatthew G. Knepley 
2758d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2759d71ae5a4SJacob Faibussowitsch {
2760b81db932SToby Isaac   DMLabel      ghostLabel;
2761b81db932SToby Isaac   PetscScalar *dx, *grad, **gref;
2762b81db932SToby Isaac   PetscInt     dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2763b81db932SToby Isaac   PetscSection neighSec;
2764b81db932SToby Isaac   PetscInt(*neighbors)[2];
2765b81db932SToby Isaac   PetscInt *counter;
2766b81db932SToby Isaac 
2767b81db932SToby Isaac   PetscFunctionBegin;
27689566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
27699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
27709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2771485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
27729566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &neighSec));
27739566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(neighSec, cStart, cEndInterior));
27749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
27759566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2776b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2777b81db932SToby Isaac     const PetscInt *fcells;
2778b81db932SToby Isaac     PetscBool       boundary;
27795bc680faSToby Isaac     PetscInt        ghost = -1;
2780b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2781b81db932SToby Isaac 
27829566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
27839566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
27849566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2785b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
27869566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
278706348e87SToby Isaac     if (numCells == 2) {
27889566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2789b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2790b81db932SToby Isaac         PetscInt cell = fcells[c];
2791b81db932SToby Isaac 
279248a46eb9SPierre Jolivet         if (cell >= cStart && cell < cEndInterior) PetscCall(PetscSectionAddDof(neighSec, cell, 1));
2793b81db932SToby Isaac       }
2794b81db932SToby Isaac     }
279506348e87SToby Isaac   }
27969566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(neighSec));
27979566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetMaxDof(neighSec, &maxNumFaces));
27989566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
2799b81db932SToby Isaac   nStart = 0;
28009566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(neighSec, &nEnd));
28019566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1((nEnd - nStart), &neighbors));
28029566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1((cEndInterior - cStart), &counter));
2803b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2804b81db932SToby Isaac     const PetscInt *fcells;
2805b81db932SToby Isaac     PetscBool       boundary;
28065bc680faSToby Isaac     PetscInt        ghost = -1;
2807b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2808b81db932SToby Isaac 
28099566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
28109566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
28119566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2812b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
28139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
281406348e87SToby Isaac     if (numCells == 2) {
28159566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2816b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2817b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2818b81db932SToby Isaac 
2819e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
28209566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(neighSec, cell, &off));
2821b81db932SToby Isaac           off += counter[cell - cStart]++;
2822b81db932SToby Isaac           neighbors[off][0] = f;
2823b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2824b81db932SToby Isaac         }
2825b81db932SToby Isaac       }
2826b81db932SToby Isaac     }
282706348e87SToby Isaac   }
28289566063dSJacob Faibussowitsch   PetscCall(PetscFree(counter));
28299566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2830b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2831317218b9SToby Isaac     PetscInt         numFaces, f, d, off, ghost = -1;
2832640bce14SSatish Balay     PetscFVCellGeom *cg;
2833b81db932SToby Isaac 
28349566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
28359566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(neighSec, c, &numFaces));
28369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(neighSec, c, &off));
2837a79418b7SMatt McGurn 
2838a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
28399566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2840a79418b7SMatt McGurn     if (ghost >= 0) continue;
2841a79418b7SMatt McGurn 
284263a3b9bcSJacob 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);
2843b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2844640bce14SSatish Balay       PetscFVCellGeom *cg1;
2845b81db932SToby Isaac       PetscFVFaceGeom *fg;
2846b81db932SToby Isaac       const PetscInt  *fcells;
2847b81db932SToby Isaac       PetscInt         ncell, side, nface;
2848b81db932SToby Isaac 
2849b81db932SToby Isaac       nface = neighbors[off + f][0];
2850b81db932SToby Isaac       ncell = neighbors[off + f][1];
28519566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, nface, &fcells));
2852b81db932SToby Isaac       side = (c != fcells[0]);
28539566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, nface, fgeom, &fg));
28549566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2855b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f * dim + d] = cg1->centroid[d] - cg->centroid[d];
2856b81db932SToby Isaac       gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */
2857b81db932SToby Isaac     }
28589566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, numFaces, dx, grad));
2859b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2860b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f * dim + d];
2861b81db932SToby Isaac     }
2862b81db932SToby Isaac   }
28639566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
28649566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&neighSec));
28659566063dSJacob Faibussowitsch   PetscCall(PetscFree(neighbors));
2866*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2867b81db932SToby Isaac }
2868b81db932SToby Isaac 
2869856ac710SMatthew G. Knepley /*@
2870856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2871856ac710SMatthew G. Knepley 
2872d083f849SBarry Smith   Collective on dm
2873856ac710SMatthew G. Knepley 
28744165533cSJose E. Roman   Input Parameters:
2875856ac710SMatthew G. Knepley + dm  - The DM
2876856ac710SMatthew G. Knepley . fvm - The PetscFV
28778f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2878856ac710SMatthew G. Knepley 
28796b867d5aSJose E. Roman   Input/Output Parameter:
28806b867d5aSJose E. Roman . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM(); on output
28816b867d5aSJose E. Roman                  the geometric factors for gradient calculation are inserted
28826b867d5aSJose E. Roman 
28836b867d5aSJose E. Roman   Output Parameter:
28846b867d5aSJose E. Roman . dmGrad - The DM describing the layout of gradient data
2885856ac710SMatthew G. Knepley 
2886856ac710SMatthew G. Knepley   Level: developer
2887856ac710SMatthew G. Knepley 
2888db781477SPatrick Sanan .seealso: `DMPlexGetFaceGeometryFVM()`, `DMPlexGetCellGeometryFVM()`
2889856ac710SMatthew G. Knepley @*/
2890d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2891d71ae5a4SJacob Faibussowitsch {
2892856ac710SMatthew G. Knepley   DM           dmFace, dmCell;
2893856ac710SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2894b81db932SToby Isaac   PetscSection sectionGrad, parentSection;
2895856ac710SMatthew G. Knepley   PetscInt     dim, pdim, cStart, cEnd, cEndInterior, c;
2896856ac710SMatthew G. Knepley 
2897856ac710SMatthew G. Knepley   PetscFunctionBegin;
28989566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
28999566063dSJacob Faibussowitsch   PetscCall(PetscFVGetNumComponents(fvm, &pdim));
29009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
29019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2902856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
29039566063dSJacob Faibussowitsch   PetscCall(VecGetDM(faceGeometry, &dmFace));
29049566063dSJacob Faibussowitsch   PetscCall(VecGetDM(cellGeometry, &dmCell));
29059566063dSJacob Faibussowitsch   PetscCall(VecGetArray(faceGeometry, &fgeom));
29069566063dSJacob Faibussowitsch   PetscCall(VecGetArray(cellGeometry, &cgeom));
29079566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &parentSection, NULL, NULL, NULL, NULL));
2908b81db932SToby Isaac   if (!parentSection) {
29099566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2910b5a3613cSMatthew G. Knepley   } else {
29119566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2912b81db932SToby Isaac   }
29139566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(faceGeometry, &fgeom));
29149566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(cellGeometry, &cgeom));
2915856ac710SMatthew G. Knepley   /* Create storage for gradients */
29169566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, dmGrad));
29179566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionGrad));
29189566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionGrad, cStart, cEnd));
29199566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionGrad, c, pdim * dim));
29209566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionGrad));
29219566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(*dmGrad, sectionGrad));
29229566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionGrad));
2923*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2924856ac710SMatthew G. Knepley }
2925b27d5b9eSToby Isaac 
2926c501906fSMatthew G. Knepley /*@
2927c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2928c501906fSMatthew G. Knepley 
2929d083f849SBarry Smith   Collective on dm
2930c501906fSMatthew G. Knepley 
29314165533cSJose E. Roman   Input Parameters:
2932c501906fSMatthew G. Knepley + dm  - The DM
29336b867d5aSJose E. Roman - fv  - The PetscFV
2934c501906fSMatthew G. Knepley 
2935c501906fSMatthew G. Knepley   Output Parameters:
2936c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2937c501906fSMatthew G. Knepley . faceGeometry - The face geometry
29386b867d5aSJose E. Roman - gradDM       - The gradient matrices
2939c501906fSMatthew G. Knepley 
2940c501906fSMatthew G. Knepley   Level: developer
2941c501906fSMatthew G. Knepley 
2942db781477SPatrick Sanan .seealso: `DMPlexComputeGeometryFVM()`
2943c501906fSMatthew G. Knepley @*/
2944d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2945d71ae5a4SJacob Faibussowitsch {
2946b27d5b9eSToby Isaac   PetscObject cellgeomobj, facegeomobj;
2947b27d5b9eSToby Isaac 
2948b27d5b9eSToby Isaac   PetscFunctionBegin;
29499566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
2950b27d5b9eSToby Isaac   if (!cellgeomobj) {
2951b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2952b27d5b9eSToby Isaac 
29539566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt));
29549566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_cellgeom_fvm", (PetscObject)cellgeomInt));
29559566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_facegeom_fvm", (PetscObject)facegeomInt));
29569566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cellgeomInt));
29579566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&facegeomInt));
29589566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
2959b27d5b9eSToby Isaac   }
29609566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_facegeom_fvm", &facegeomobj));
2961b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec)cellgeomobj;
2962b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec)facegeomobj;
2963b27d5b9eSToby Isaac   if (gradDM) {
2964b27d5b9eSToby Isaac     PetscObject gradobj;
2965b27d5b9eSToby Isaac     PetscBool   computeGradients;
2966b27d5b9eSToby Isaac 
29679566063dSJacob Faibussowitsch     PetscCall(PetscFVGetComputeGradients(fv, &computeGradients));
2968b27d5b9eSToby Isaac     if (!computeGradients) {
2969b27d5b9eSToby Isaac       *gradDM = NULL;
2970*3ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
2971b27d5b9eSToby Isaac     }
29729566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
2973b27d5b9eSToby Isaac     if (!gradobj) {
2974b27d5b9eSToby Isaac       DM dmGradInt;
2975b27d5b9eSToby Isaac 
29769566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeGradientFVM(dm, fv, (Vec)facegeomobj, (Vec)cellgeomobj, &dmGradInt));
29779566063dSJacob Faibussowitsch       PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt));
29789566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dmGradInt));
29799566063dSJacob Faibussowitsch       PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
2980b27d5b9eSToby Isaac     }
2981b27d5b9eSToby Isaac     *gradDM = (DM)gradobj;
2982b27d5b9eSToby Isaac   }
2983*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2984b27d5b9eSToby Isaac }
2985d6143a4eSToby Isaac 
2986d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess)
2987d71ae5a4SJacob Faibussowitsch {
29889d150b73SToby Isaac   PetscInt l, m;
29899d150b73SToby Isaac 
2990cd345991SToby Isaac   PetscFunctionBeginHot;
29919d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
29929d150b73SToby Isaac     /* invert Jacobian, multiply */
29939d150b73SToby Isaac     PetscScalar det, idet;
29949d150b73SToby Isaac 
29959d150b73SToby Isaac     switch (dimR) {
2996d71ae5a4SJacob Faibussowitsch     case 1:
2997d71ae5a4SJacob Faibussowitsch       invJ[0] = 1. / J[0];
2998d71ae5a4SJacob Faibussowitsch       break;
29999d150b73SToby Isaac     case 2:
30009d150b73SToby Isaac       det     = J[0] * J[3] - J[1] * J[2];
30019d150b73SToby Isaac       idet    = 1. / det;
30029d150b73SToby Isaac       invJ[0] = J[3] * idet;
30039d150b73SToby Isaac       invJ[1] = -J[1] * idet;
30049d150b73SToby Isaac       invJ[2] = -J[2] * idet;
30059d150b73SToby Isaac       invJ[3] = J[0] * idet;
30069d150b73SToby Isaac       break;
30079371c9d4SSatish Balay     case 3: {
30089d150b73SToby Isaac       invJ[0] = J[4] * J[8] - J[5] * J[7];
30099d150b73SToby Isaac       invJ[1] = J[2] * J[7] - J[1] * J[8];
30109d150b73SToby Isaac       invJ[2] = J[1] * J[5] - J[2] * J[4];
30119d150b73SToby Isaac       det     = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
30129d150b73SToby Isaac       idet    = 1. / det;
30139d150b73SToby Isaac       invJ[0] *= idet;
30149d150b73SToby Isaac       invJ[1] *= idet;
30159d150b73SToby Isaac       invJ[2] *= idet;
30169d150b73SToby Isaac       invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]);
30179d150b73SToby Isaac       invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]);
30189d150b73SToby Isaac       invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]);
30199d150b73SToby Isaac       invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]);
30209d150b73SToby Isaac       invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]);
30219d150b73SToby Isaac       invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]);
30229371c9d4SSatish Balay     } break;
30239d150b73SToby Isaac     }
30249d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
3025ad540459SPierre Jolivet       for (m = 0; m < dimC; m++) guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
30269d150b73SToby Isaac     }
30279d150b73SToby Isaac   } else {
30289d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
30299d150b73SToby Isaac     char transpose = 'C';
30309d150b73SToby Isaac #else
30319d150b73SToby Isaac     char transpose = 'T';
30329d150b73SToby Isaac #endif
30339d150b73SToby Isaac     PetscBLASInt m        = dimR;
30349d150b73SToby Isaac     PetscBLASInt n        = dimC;
30359d150b73SToby Isaac     PetscBLASInt one      = 1;
30369d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
30379d150b73SToby Isaac 
3038ad540459SPierre Jolivet     for (l = 0; l < dimC; l++) invJ[l] = resNeg[l];
30399d150b73SToby Isaac 
3040792fecdfSBarry Smith     PetscCallBLAS("LAPACKgels", LAPACKgels_(&transpose, &m, &n, &one, J, &m, invJ, &n, work, &worksize, &info));
304108401ef6SPierre Jolivet     PetscCheck(info == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Bad argument to GELS");
30429d150b73SToby Isaac 
3043ad540459SPierre Jolivet     for (l = 0; l < dimR; l++) guess[l] += PetscRealPart(invJ[l]);
30449d150b73SToby Isaac   }
3045*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30469d150b73SToby Isaac }
30479d150b73SToby Isaac 
3048d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
3049d71ae5a4SJacob Faibussowitsch {
3050c0cbe899SToby Isaac   PetscInt     coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
30519d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
30529d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
30539d150b73SToby Isaac   PetscScalar *J, *invJ, *work;
30549d150b73SToby Isaac 
30559d150b73SToby Isaac   PetscFunctionBegin;
30569d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30579566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
30581dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
30599566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
30609566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
30619d150b73SToby Isaac   cellCoords = &cellData[0];
30629d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
30639d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
30649d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
30659d150b73SToby Isaac   invJ       = &J[dimR * dimC];
30669d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
30679d150b73SToby Isaac   if (dimR == 2) {
30689d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
30699d150b73SToby Isaac 
30709d150b73SToby Isaac     for (i = 0; i < 4; i++) {
30719d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
30729d150b73SToby Isaac 
3073ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
30749d150b73SToby Isaac     }
30759d150b73SToby Isaac   } else if (dimR == 3) {
30769d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
30779d150b73SToby Isaac 
30789d150b73SToby Isaac     for (i = 0; i < 8; i++) {
30799d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
30809d150b73SToby Isaac 
3081ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
30829d150b73SToby Isaac     }
30839d150b73SToby Isaac   } else {
3084ad540459SPierre Jolivet     for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]);
30859d150b73SToby Isaac   }
30869d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
30879d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
30889d150b73SToby Isaac     PetscReal *swap;
30899d150b73SToby Isaac 
30909d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
30919d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
30929d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
30939d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
30949d150b73SToby Isaac       }
30959d150b73SToby Isaac     }
30969d150b73SToby Isaac 
30979d150b73SToby Isaac     if (i < dimR - 1) {
30989d150b73SToby Isaac       swap       = cellCoeffs;
30999d150b73SToby Isaac       cellCoeffs = cellCoords;
31009d150b73SToby Isaac       cellCoords = swap;
31019d150b73SToby Isaac     }
31029d150b73SToby Isaac   }
31039566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(refCoords, numPoints * dimR));
31049d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
31059d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
31069d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
31079d150b73SToby Isaac 
31089d150b73SToby Isaac       /* compute -residual and Jacobian */
3109ad540459SPierre Jolivet       for (k = 0; k < dimC; k++) resNeg[k] = realCoords[dimC * j + k];
3110ad540459SPierre Jolivet       for (k = 0; k < dimC * dimR; k++) J[k] = 0.;
31119d150b73SToby Isaac       for (k = 0; k < numV; k++) {
31129d150b73SToby Isaac         PetscReal extCoord = 1.;
31139d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
31149d150b73SToby Isaac           PetscReal coord = guess[l];
31159d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
31169d150b73SToby Isaac 
31179d150b73SToby Isaac           extCoord *= dep * coord + !dep;
31189d150b73SToby Isaac           extJ[l] = dep;
31199d150b73SToby Isaac 
31209d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
31219d150b73SToby Isaac             PetscReal coord = guess[m];
31229d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
31239d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
31249d150b73SToby Isaac 
31259d150b73SToby Isaac             extJ[l] *= mult;
31269d150b73SToby Isaac           }
31279d150b73SToby Isaac         }
31289d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
31299d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
31309d150b73SToby Isaac 
31319d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
3132ad540459SPierre Jolivet           for (m = 0; m < dimR; m++) J[dimR * l + m] += coeff * extJ[m];
31339d150b73SToby Isaac         }
31349d150b73SToby Isaac       }
313576bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
31360611203eSToby Isaac         PetscReal maxAbs = 0.;
31370611203eSToby Isaac 
3138ad540459SPierre Jolivet         for (l = 0; l < dimC; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l]));
313963a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
31400611203eSToby Isaac       }
31419d150b73SToby Isaac 
31429566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(dimC, dimR, J, invJ, work, resNeg, guess));
31439d150b73SToby Isaac     }
31449d150b73SToby Isaac   }
31459566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
31469566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
31479566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
3148*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31499d150b73SToby Isaac }
31509d150b73SToby Isaac 
3151d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
3152d71ae5a4SJacob Faibussowitsch {
31539d150b73SToby Isaac   PetscInt     coordSize, i, j, k, l, numV = (1 << dimR);
31549d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
31559d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs;
31569d150b73SToby Isaac 
31579d150b73SToby Isaac   PetscFunctionBegin;
31589d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31599566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
31601dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
31619566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
31629d150b73SToby Isaac   cellCoords = &cellData[0];
31639d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
31649d150b73SToby Isaac   if (dimR == 2) {
31659d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
31669d150b73SToby Isaac 
31679d150b73SToby Isaac     for (i = 0; i < 4; i++) {
31689d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31699d150b73SToby Isaac 
3170ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
31719d150b73SToby Isaac     }
31729d150b73SToby Isaac   } else if (dimR == 3) {
31739d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
31749d150b73SToby Isaac 
31759d150b73SToby Isaac     for (i = 0; i < 8; i++) {
31769d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31779d150b73SToby Isaac 
3178ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
31799d150b73SToby Isaac     }
31809d150b73SToby Isaac   } else {
3181ad540459SPierre Jolivet     for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]);
31829d150b73SToby Isaac   }
31839d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
31849d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
31859d150b73SToby Isaac     PetscReal *swap;
31869d150b73SToby Isaac 
31879d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
31889d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
31899d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
31909d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
31919d150b73SToby Isaac       }
31929d150b73SToby Isaac     }
31939d150b73SToby Isaac 
31949d150b73SToby Isaac     if (i < dimR - 1) {
31959d150b73SToby Isaac       swap       = cellCoeffs;
31969d150b73SToby Isaac       cellCoeffs = cellCoords;
31979d150b73SToby Isaac       cellCoords = swap;
31989d150b73SToby Isaac     }
31999d150b73SToby Isaac   }
32009566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(realCoords, numPoints * dimC));
32019d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32029d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
32039d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
32049d150b73SToby Isaac 
32059d150b73SToby Isaac     for (k = 0; k < numV; k++) {
32069d150b73SToby Isaac       PetscReal extCoord = 1.;
32079d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
32089d150b73SToby Isaac         PetscReal coord = guess[l];
32099d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
32109d150b73SToby Isaac 
32119d150b73SToby Isaac         extCoord *= dep * coord + !dep;
32129d150b73SToby Isaac       }
32139d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
32149d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
32159d150b73SToby Isaac 
32169d150b73SToby Isaac         mapped[l] += coeff * extCoord;
32179d150b73SToby Isaac       }
32189d150b73SToby Isaac     }
32199d150b73SToby Isaac   }
32209566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
32219566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
3222*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32239d150b73SToby Isaac }
32249d150b73SToby Isaac 
32259c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
3226d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
3227d71ae5a4SJacob Faibussowitsch {
32289c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
3229c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3230c6e120d1SToby Isaac   PetscReal   *invV, *modes;
3231c6e120d1SToby Isaac   PetscReal   *B, *D, *resNeg;
3232c6e120d1SToby Isaac   PetscScalar *J, *invJ, *work;
32339d150b73SToby Isaac 
32349d150b73SToby Isaac   PetscFunctionBegin;
32359566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
32369566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
323763a3b9bcSJacob 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);
32389566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32399d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
32409566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
32419d150b73SToby Isaac   invV = fe->invV;
3242012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3243012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3244ad540459SPierre Jolivet     for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
32459d150b73SToby Isaac   }
32469566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
32479c3cf19fSMatthew G. Knepley   D      = &B[pdim * Nc];
32489c3cf19fSMatthew G. Knepley   resNeg = &D[pdim * Nc * dimR];
32499566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
32509c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
32519c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
3252ad540459SPierre Jolivet   for (i = 0; i < numPoints * dimR; i++) refCoords[i] = 0.;
32539d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32549b1f03cbSToby 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 */
32559d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
32569566063dSJacob Faibussowitsch       PetscCall(PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL));
3257ad540459SPierre Jolivet       for (k = 0; k < Nc; k++) resNeg[k] = realCoords[j * Nc + k];
3258ad540459SPierre Jolivet       for (k = 0; k < Nc * dimR; k++) J[k] = 0.;
32599c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
32609c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
3261012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
3262ad540459SPierre Jolivet           for (m = 0; m < dimR; m++) J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
32639d150b73SToby Isaac         }
32649d150b73SToby Isaac       }
326576bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
32660611203eSToby Isaac         PetscReal maxAbs = 0.;
32670611203eSToby Isaac 
3268ad540459SPierre Jolivet         for (l = 0; l < Nc; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l]));
326963a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
32700611203eSToby Isaac       }
32719566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(Nc, dimR, J, invJ, work, resNeg, guess));
32729d150b73SToby Isaac     }
32739d150b73SToby Isaac   }
32749566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
32759566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
32769566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
32779566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
3278*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32799d150b73SToby Isaac }
32809d150b73SToby Isaac 
32819c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
3282d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
3283d71ae5a4SJacob Faibussowitsch {
32849c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, coordSize;
3285c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3286c6e120d1SToby Isaac   PetscReal   *invV, *modes;
32879d150b73SToby Isaac   PetscReal   *B;
32889d150b73SToby Isaac 
32899d150b73SToby Isaac   PetscFunctionBegin;
32909566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
32919566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
329263a3b9bcSJacob 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);
32939566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32949d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
32959566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
32969d150b73SToby Isaac   invV = fe->invV;
3297012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3298012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3299ad540459SPierre Jolivet     for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
33009d150b73SToby Isaac   }
33019566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
33029566063dSJacob Faibussowitsch   PetscCall(PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL));
3303ad540459SPierre Jolivet   for (i = 0; i < numPoints * Nc; i++) realCoords[i] = 0.;
33049d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
33059c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
33069d150b73SToby Isaac 
33079c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
3308ad540459SPierre Jolivet       for (l = 0; l < Nc; l++) mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
33099d150b73SToby Isaac     }
33109d150b73SToby Isaac   }
33119566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
33129566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
33139566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
3314*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
33159d150b73SToby Isaac }
33169d150b73SToby Isaac 
3317d6143a4eSToby Isaac /*@
3318d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
3319d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
3320d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
3321d6143a4eSToby Isaac 
3322d6143a4eSToby Isaac   Not collective
3323d6143a4eSToby Isaac 
3324d6143a4eSToby Isaac   Input Parameters:
3325d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
3326d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
3327d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
3328d6143a4eSToby Isaac . cell       - the cell whose map is used.
3329d6143a4eSToby Isaac . numPoints  - the number of points to locate
33301b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
3331d6143a4eSToby Isaac 
3332d6143a4eSToby Isaac   Output Parameters:
3333d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
33341b266c99SBarry Smith 
33351b266c99SBarry Smith   Level: intermediate
333673c9229bSMatthew Knepley 
3337db781477SPatrick Sanan .seealso: `DMPlexReferenceToCoordinates()`
3338d6143a4eSToby Isaac @*/
3339d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
3340d71ae5a4SJacob Faibussowitsch {
3341485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
33429d150b73SToby Isaac   DM       coordDM = NULL;
33439d150b73SToby Isaac   Vec      coords;
33449d150b73SToby Isaac   PetscFE  fe = NULL;
33459d150b73SToby Isaac 
3346d6143a4eSToby Isaac   PetscFunctionBegin;
33479d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
33499566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
3350*3ba16761SJacob Faibussowitsch   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(PETSC_SUCCESS);
33519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
33529566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
33539566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
33549d150b73SToby Isaac   if (coordDM) {
33559d150b73SToby Isaac     PetscInt coordFields;
33569d150b73SToby Isaac 
33579566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
33589d150b73SToby Isaac     if (coordFields) {
33599d150b73SToby Isaac       PetscClassId id;
33609d150b73SToby Isaac       PetscObject  disc;
33619d150b73SToby Isaac 
33629566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
33639566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
3364ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
33659d150b73SToby Isaac     }
33669d150b73SToby Isaac   }
33679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
33681dca8a05SBarry 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);
33699d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
33709d150b73SToby Isaac     PetscInt  coneSize;
33719d150b73SToby Isaac     PetscBool isSimplex, isTensor;
33729d150b73SToby Isaac 
33739566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
33749d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
33759d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
33769d150b73SToby Isaac     if (isSimplex) {
33779d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
33789d150b73SToby Isaac 
33799566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
33809d150b73SToby Isaac       J    = &v0[dimC];
33819d150b73SToby Isaac       invJ = &J[dimC * dimC];
33829566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ));
33839d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3384c330f8ffSToby Isaac         const PetscReal x0[3] = {-1., -1., -1.};
3385c330f8ffSToby Isaac 
3386c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
33879d150b73SToby Isaac       }
33889566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
33899d150b73SToby Isaac     } else if (isTensor) {
33909566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
339163a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
33929d150b73SToby Isaac   } else {
33939566063dSJacob Faibussowitsch     PetscCall(DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
33949d150b73SToby Isaac   }
3395*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
33969d150b73SToby Isaac }
33979d150b73SToby Isaac 
33989d150b73SToby Isaac /*@
33999d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
34009d150b73SToby Isaac 
34019d150b73SToby Isaac   Not collective
34029d150b73SToby Isaac 
34039d150b73SToby Isaac   Input Parameters:
34049d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
34059d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
34069d150b73SToby Isaac                as a multilinear map for tensor-product elements
34079d150b73SToby Isaac . cell       - the cell whose map is used.
34089d150b73SToby Isaac . numPoints  - the number of points to locate
3409a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
34109d150b73SToby Isaac 
34119d150b73SToby Isaac   Output Parameters:
34129d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
34131b266c99SBarry Smith 
34141b266c99SBarry Smith    Level: intermediate
341573c9229bSMatthew Knepley 
3416db781477SPatrick Sanan .seealso: `DMPlexCoordinatesToReference()`
34179d150b73SToby Isaac @*/
3418d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
3419d71ae5a4SJacob Faibussowitsch {
3420485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
34219d150b73SToby Isaac   DM       coordDM = NULL;
34229d150b73SToby Isaac   Vec      coords;
34239d150b73SToby Isaac   PetscFE  fe = NULL;
34249d150b73SToby Isaac 
34259d150b73SToby Isaac   PetscFunctionBegin;
34269d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34279566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
34289566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
3429*3ba16761SJacob Faibussowitsch   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(PETSC_SUCCESS);
34309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
34319566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
34329566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
34339d150b73SToby Isaac   if (coordDM) {
34349d150b73SToby Isaac     PetscInt coordFields;
34359d150b73SToby Isaac 
34369566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
34379d150b73SToby Isaac     if (coordFields) {
34389d150b73SToby Isaac       PetscClassId id;
34399d150b73SToby Isaac       PetscObject  disc;
34409d150b73SToby Isaac 
34419566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
34429566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
3443ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
34449d150b73SToby Isaac     }
34459d150b73SToby Isaac   }
34469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
34471dca8a05SBarry 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);
34489d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
34499d150b73SToby Isaac     PetscInt  coneSize;
34509d150b73SToby Isaac     PetscBool isSimplex, isTensor;
34519d150b73SToby Isaac 
34529566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
34539d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
34549d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
34559d150b73SToby Isaac     if (isSimplex) {
34569d150b73SToby Isaac       PetscReal detJ, *v0, *J;
34579d150b73SToby Isaac 
34589566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34599d150b73SToby Isaac       J = &v0[dimC];
34609566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ));
3461c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3462c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3463c330f8ffSToby Isaac 
3464c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
34659d150b73SToby Isaac       }
34669566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34679d150b73SToby Isaac     } else if (isTensor) {
34689566063dSJacob Faibussowitsch       PetscCall(DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
346963a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
34709d150b73SToby Isaac   } else {
34719566063dSJacob Faibussowitsch     PetscCall(DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
34729d150b73SToby Isaac   }
3473*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3474d6143a4eSToby Isaac }
34750139fca9SMatthew G. Knepley 
34760139fca9SMatthew G. Knepley /*@C
34770139fca9SMatthew G. Knepley   DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates.
34780139fca9SMatthew G. Knepley 
34790139fca9SMatthew G. Knepley   Not collective
34800139fca9SMatthew G. Knepley 
34810139fca9SMatthew G. Knepley   Input Parameters:
34820139fca9SMatthew G. Knepley + dm      - The DM
34830139fca9SMatthew G. Knepley . time    - The time
34840139fca9SMatthew G. Knepley - func    - The function transforming current coordinates to new coordaintes
34850139fca9SMatthew G. Knepley 
34860139fca9SMatthew G. Knepley    Calling sequence of func:
34870139fca9SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
34880139fca9SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
34890139fca9SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
34900139fca9SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
34910139fca9SMatthew G. Knepley 
34920139fca9SMatthew G. Knepley +  dim          - The spatial dimension
34930139fca9SMatthew G. Knepley .  Nf           - The number of input fields (here 1)
34940139fca9SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
34950139fca9SMatthew G. Knepley .  uOff         - The offset of the coordinates in u[] (here 0)
34960139fca9SMatthew G. Knepley .  uOff_x       - The offset of the coordinates in u_x[] (here 0)
34970139fca9SMatthew G. Knepley .  u            - The coordinate values at this point in space
34980139fca9SMatthew G. Knepley .  u_t          - The coordinate time derivative at this point in space (here NULL)
34990139fca9SMatthew G. Knepley .  u_x          - The coordinate derivatives at this point in space
35000139fca9SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
35010139fca9SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
35020139fca9SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
35030139fca9SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
35040139fca9SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
35050139fca9SMatthew G. Knepley .  t            - The current time
35060139fca9SMatthew G. Knepley .  x            - The coordinates of this point (here not used)
35070139fca9SMatthew G. Knepley .  numConstants - The number of constants
35080139fca9SMatthew G. Knepley .  constants    - The value of each constant
35090139fca9SMatthew G. Knepley -  f            - The new coordinates at this point in space
35100139fca9SMatthew G. Knepley 
35110139fca9SMatthew G. Knepley   Level: intermediate
35120139fca9SMatthew G. Knepley 
3513db781477SPatrick Sanan .seealso: `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMProjectFieldLocal()`, `DMProjectFieldLabelLocal()`
35140139fca9SMatthew G. Knepley @*/
3515d71ae5a4SJacob 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[]))
3516d71ae5a4SJacob Faibussowitsch {
35170139fca9SMatthew G. Knepley   DM      cdm;
35188bf1a49fSMatthew G. Knepley   DMField cf;
35190139fca9SMatthew G. Knepley   Vec     lCoords, tmpCoords;
35200139fca9SMatthew G. Knepley 
35210139fca9SMatthew G. Knepley   PetscFunctionBegin;
35229566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
35239566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
35249566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(cdm, &tmpCoords));
35259566063dSJacob Faibussowitsch   PetscCall(VecCopy(lCoords, tmpCoords));
35268bf1a49fSMatthew G. Knepley   /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */
35279566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateField(dm, &cf));
35286858538eSMatthew G. Knepley   cdm->coordinates[0].field = cf;
35299566063dSJacob Faibussowitsch   PetscCall(DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords));
35306858538eSMatthew G. Knepley   cdm->coordinates[0].field = NULL;
35319566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(cdm, &tmpCoords));
35329566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, lCoords));
3533*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35340139fca9SMatthew G. Knepley }
35350139fca9SMatthew G. Knepley 
35360139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z,
35370139fca9SMatthew G. Knepley   / 1  0  m_0 \
35380139fca9SMatthew G. Knepley   | 0  1  m_1 |
35390139fca9SMatthew G. Knepley   \ 0  0   1  /
35400139fca9SMatthew G. Knepley */
3541d71ae5a4SJacob 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[])
3542d71ae5a4SJacob Faibussowitsch {
35430139fca9SMatthew G. Knepley   const PetscInt Nc = uOff[1] - uOff[0];
3544c1f1bd54SMatthew G. Knepley   const PetscInt ax = (PetscInt)PetscRealPart(constants[0]);
35450139fca9SMatthew G. Knepley   PetscInt       c;
35460139fca9SMatthew G. Knepley 
3547ad540459SPierre Jolivet   for (c = 0; c < Nc; ++c) coords[c] = u[c] + constants[c + 1] * u[ax];
35480139fca9SMatthew G. Knepley }
35490139fca9SMatthew G. Knepley 
35500139fca9SMatthew G. Knepley /*@C
35510139fca9SMatthew G. Knepley   DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates.
35520139fca9SMatthew G. Knepley 
35530139fca9SMatthew G. Knepley   Not collective
35540139fca9SMatthew G. Knepley 
35550139fca9SMatthew G. Knepley   Input Parameters:
35560139fca9SMatthew G. Knepley + dm          - The DM
35573ee9839eSMatthew G. Knepley . direction   - The shear coordinate direction, e.g. 0 is the x-axis
35580139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction
35590139fca9SMatthew G. Knepley 
35600139fca9SMatthew G. Knepley   Level: intermediate
35610139fca9SMatthew G. Knepley 
3562db781477SPatrick Sanan .seealso: `DMPlexRemapGeometry()`
35630139fca9SMatthew G. Knepley @*/
3564d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[])
3565d71ae5a4SJacob Faibussowitsch {
35660139fca9SMatthew G. Knepley   DM             cdm;
35670139fca9SMatthew G. Knepley   PetscDS        cds;
35680139fca9SMatthew G. Knepley   PetscObject    obj;
35690139fca9SMatthew G. Knepley   PetscClassId   id;
35700139fca9SMatthew G. Knepley   PetscScalar   *moduli;
35713ee9839eSMatthew G. Knepley   const PetscInt dir = (PetscInt)direction;
35720139fca9SMatthew G. Knepley   PetscInt       dE, d, e;
35730139fca9SMatthew G. Knepley 
35740139fca9SMatthew G. Knepley   PetscFunctionBegin;
35759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
35769566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
35779566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(dE + 1, &moduli));
35780139fca9SMatthew G. Knepley   moduli[0] = dir;
3579cdaaecf7SMatthew G. Knepley   for (d = 0, e = 0; d < dE; ++d) moduli[d + 1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0);
35809566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
35819566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
35829566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(obj, &id));
35830139fca9SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
35840139fca9SMatthew G. Knepley     Vec          lCoords;
35850139fca9SMatthew G. Knepley     PetscSection cSection;
35860139fca9SMatthew G. Knepley     PetscScalar *coords;
35870139fca9SMatthew G. Knepley     PetscInt     vStart, vEnd, v;
35880139fca9SMatthew G. Knepley 
35899566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
35909566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cSection));
35919566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
35929566063dSJacob Faibussowitsch     PetscCall(VecGetArray(lCoords, &coords));
35930139fca9SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
35940139fca9SMatthew G. Knepley       PetscReal ds;
35950139fca9SMatthew G. Knepley       PetscInt  off, c;
35960139fca9SMatthew G. Knepley 
35979566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(cSection, v, &off));
35980139fca9SMatthew G. Knepley       ds = PetscRealPart(coords[off + dir]);
35990139fca9SMatthew G. Knepley       for (c = 0; c < dE; ++c) coords[off + c] += moduli[c] * ds;
36000139fca9SMatthew G. Knepley     }
36019566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(lCoords, &coords));
36020139fca9SMatthew G. Knepley   } else {
36039566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, dE + 1, moduli));
36049566063dSJacob Faibussowitsch     PetscCall(DMPlexRemapGeometry(dm, 0.0, f0_shear));
36050139fca9SMatthew G. Knepley   }
36069566063dSJacob Faibussowitsch   PetscCall(PetscFree(moduli));
3607*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36080139fca9SMatthew G. Knepley }
3609