xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision f315e28e6c7a303f1b983e95d6ef67b6b4e4d848)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>  /*I      "petscdmplex.h"   I*/
29d150b73SToby Isaac #include <petsc/private/petscfeimpl.h> /*I      "petscfe.h"       I*/
39d150b73SToby Isaac #include <petscblaslapack.h>
4af74b616SDave May #include <petsctime.h>
5ccd2543fSMatthew G Knepley 
63985bb02SVaclav Hapla /*@
73985bb02SVaclav Hapla   DMPlexFindVertices - Try to find DAG points based on their coordinates.
83985bb02SVaclav Hapla 
93985bb02SVaclav Hapla   Not Collective (provided DMGetCoordinatesLocalSetUp() has been called already)
103985bb02SVaclav Hapla 
113985bb02SVaclav Hapla   Input Parameters:
123985bb02SVaclav Hapla + dm - The DMPlex object
13d3e1f4ccSVaclav Hapla . coordinates - The Vec of coordinates of the sought points
143985bb02SVaclav Hapla - eps - The tolerance or PETSC_DEFAULT
153985bb02SVaclav Hapla 
163985bb02SVaclav Hapla   Output Parameters:
17d3e1f4ccSVaclav Hapla . points - The IS of found DAG points or -1
183985bb02SVaclav Hapla 
193985bb02SVaclav Hapla   Level: intermediate
203985bb02SVaclav Hapla 
213985bb02SVaclav Hapla   Notes:
22d3e1f4ccSVaclav Hapla   The length of Vec coordinates must be npoints * dim where dim is the spatial dimension returned by DMGetCoordinateDim() and npoints is the number of sought points.
233985bb02SVaclav Hapla 
24d3e1f4ccSVaclav Hapla   The output IS is living on PETSC_COMM_SELF and its length is npoints.
25d3e1f4ccSVaclav Hapla   Each rank does the search independently.
26d3e1f4ccSVaclav Hapla   If this rank's local DMPlex portion contains the DAG point corresponding to the i-th tuple of coordinates, the i-th entry of the output IS is set to that DAG point, otherwise to -1.
273985bb02SVaclav Hapla 
28d3e1f4ccSVaclav Hapla   The output IS must be destroyed by user.
293985bb02SVaclav Hapla 
303985bb02SVaclav Hapla   The tolerance is interpreted as the maximum Euclidean (L2) distance of the sought point from the specified coordinates.
313985bb02SVaclav Hapla 
32d3e1f4ccSVaclav Hapla   Complexity of this function is currently O(mn) with m number of vertices to find and n number of vertices in the local mesh. This could probably be improved if needed.
33335ef845SVaclav Hapla 
34db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMGetCoordinatesLocal()`
353985bb02SVaclav Hapla @*/
36d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points)
37d71ae5a4SJacob Faibussowitsch {
3837900f7dSMatthew G. Knepley   PetscInt           c, cdim, i, j, o, p, vStart, vEnd;
39d3e1f4ccSVaclav Hapla   PetscInt           npoints;
40d3e1f4ccSVaclav Hapla   const PetscScalar *coord;
413985bb02SVaclav Hapla   Vec                allCoordsVec;
423985bb02SVaclav Hapla   const PetscScalar *allCoords;
43d3e1f4ccSVaclav Hapla   PetscInt          *dagPoints;
443985bb02SVaclav Hapla 
453985bb02SVaclav Hapla   PetscFunctionBegin;
463985bb02SVaclav Hapla   if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON;
479566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
48d3e1f4ccSVaclav Hapla   {
49d3e1f4ccSVaclav Hapla     PetscInt n;
50d3e1f4ccSVaclav Hapla 
519566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(coordinates, &n));
5263a3b9bcSJacob Faibussowitsch     PetscCheck(n % cdim == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Given coordinates Vec has local length %" PetscInt_FMT " not divisible by coordinate dimension %" PetscInt_FMT " of given DM", n, cdim);
53d3e1f4ccSVaclav Hapla     npoints = n / cdim;
54d3e1f4ccSVaclav Hapla   }
559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &allCoordsVec));
569566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(allCoordsVec, &allCoords));
579566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coord));
589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
5976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
60335ef845SVaclav Hapla     /* check coordinate section is consistent with DM dimension */
61335ef845SVaclav Hapla     PetscSection cs;
62335ef845SVaclav Hapla     PetscInt     ndof;
63335ef845SVaclav Hapla 
649566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cs));
653985bb02SVaclav Hapla     for (p = vStart; p < vEnd; p++) {
669566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cs, p, &ndof));
6763a3b9bcSJacob Faibussowitsch       PetscCheck(ndof == cdim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %" PetscInt_FMT ": ndof = %" PetscInt_FMT " != %" PetscInt_FMT " = cdim", p, ndof, cdim);
68335ef845SVaclav Hapla     }
69335ef845SVaclav Hapla   }
709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npoints, &dagPoints));
71eca9f518SVaclav Hapla   if (eps == 0.0) {
7237900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
73eca9f518SVaclav Hapla       dagPoints[i] = -1;
7437900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
7537900f7dSMatthew G. Knepley         for (c = 0; c < cdim; c++) {
76d3e1f4ccSVaclav Hapla           if (coord[j + c] != allCoords[o + c]) break;
77eca9f518SVaclav Hapla         }
7837900f7dSMatthew G. Knepley         if (c == cdim) {
79eca9f518SVaclav Hapla           dagPoints[i] = p;
80eca9f518SVaclav Hapla           break;
81eca9f518SVaclav Hapla         }
82eca9f518SVaclav Hapla       }
83eca9f518SVaclav Hapla     }
84d3e1f4ccSVaclav Hapla   } else {
8537900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
86d3e1f4ccSVaclav Hapla       PetscReal norm;
87d3e1f4ccSVaclav Hapla 
88335ef845SVaclav Hapla       dagPoints[i] = -1;
8937900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
903985bb02SVaclav Hapla         norm = 0.0;
91ad540459SPierre Jolivet         for (c = 0; c < cdim; c++) norm += PetscRealPart(PetscSqr(coord[j + c] - allCoords[o + c]));
923985bb02SVaclav Hapla         norm = PetscSqrtReal(norm);
933985bb02SVaclav Hapla         if (norm <= eps) {
943985bb02SVaclav Hapla           dagPoints[i] = p;
953985bb02SVaclav Hapla           break;
963985bb02SVaclav Hapla         }
973985bb02SVaclav Hapla       }
983985bb02SVaclav Hapla     }
99d3e1f4ccSVaclav Hapla   }
1009566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(allCoordsVec, &allCoords));
1019566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coord));
1029566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points));
1033985bb02SVaclav Hapla   PetscFunctionReturn(0);
1043985bb02SVaclav Hapla }
1053985bb02SVaclav Hapla 
106d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
107d71ae5a4SJacob Faibussowitsch {
108fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0 * 2 + 0];
109fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0 * 2 + 1];
110fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1 * 2 + 0];
111fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1 * 2 + 1];
112fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0 * 2 + 0];
113fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0 * 2 + 1];
114fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1 * 2 + 0];
115fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1 * 2 + 1];
116fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
117fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
118fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
119fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
120fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
121fea14342SMatthew G. Knepley 
122fea14342SMatthew G. Knepley   PetscFunctionBegin;
123fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
124fea14342SMatthew G. Knepley   /* Non-parallel lines */
125fea14342SMatthew G. Knepley   if (denom != 0.0) {
126fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
127fea14342SMatthew G. Knepley     const PetscReal t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
128fea14342SMatthew G. Knepley 
129fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
130fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
131fea14342SMatthew G. Knepley       if (intersection) {
132fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
133fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
134fea14342SMatthew G. Knepley       }
135fea14342SMatthew G. Knepley     }
136fea14342SMatthew G. Knepley   }
137fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
138fea14342SMatthew G. Knepley }
139fea14342SMatthew G. Knepley 
140ddce0771SMatthew G. Knepley /* The plane is segmentB x segmentC: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */
141d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetLinePlaneIntersection_3D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], const PetscReal segmentC[], PetscReal intersection[], PetscBool *hasIntersection)
142d71ae5a4SJacob Faibussowitsch {
143ddce0771SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0 * 3 + 0];
144ddce0771SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0 * 3 + 1];
145ddce0771SMatthew G. Knepley   const PetscReal p0_z  = segmentA[0 * 3 + 2];
146ddce0771SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1 * 3 + 0];
147ddce0771SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1 * 3 + 1];
148ddce0771SMatthew G. Knepley   const PetscReal p1_z  = segmentA[1 * 3 + 2];
149ddce0771SMatthew G. Knepley   const PetscReal q0_x  = segmentB[0 * 3 + 0];
150ddce0771SMatthew G. Knepley   const PetscReal q0_y  = segmentB[0 * 3 + 1];
151ddce0771SMatthew G. Knepley   const PetscReal q0_z  = segmentB[0 * 3 + 2];
152ddce0771SMatthew G. Knepley   const PetscReal q1_x  = segmentB[1 * 3 + 0];
153ddce0771SMatthew G. Knepley   const PetscReal q1_y  = segmentB[1 * 3 + 1];
154ddce0771SMatthew G. Knepley   const PetscReal q1_z  = segmentB[1 * 3 + 2];
155ddce0771SMatthew G. Knepley   const PetscReal r0_x  = segmentC[0 * 3 + 0];
156ddce0771SMatthew G. Knepley   const PetscReal r0_y  = segmentC[0 * 3 + 1];
157ddce0771SMatthew G. Knepley   const PetscReal r0_z  = segmentC[0 * 3 + 2];
158ddce0771SMatthew G. Knepley   const PetscReal r1_x  = segmentC[1 * 3 + 0];
159ddce0771SMatthew G. Knepley   const PetscReal r1_y  = segmentC[1 * 3 + 1];
160ddce0771SMatthew G. Knepley   const PetscReal r1_z  = segmentC[1 * 3 + 2];
161ddce0771SMatthew G. Knepley   const PetscReal s0_x  = p1_x - p0_x;
162ddce0771SMatthew G. Knepley   const PetscReal s0_y  = p1_y - p0_y;
163ddce0771SMatthew G. Knepley   const PetscReal s0_z  = p1_z - p0_z;
164ddce0771SMatthew G. Knepley   const PetscReal s1_x  = q1_x - q0_x;
165ddce0771SMatthew G. Knepley   const PetscReal s1_y  = q1_y - q0_y;
166ddce0771SMatthew G. Knepley   const PetscReal s1_z  = q1_z - q0_z;
167ddce0771SMatthew G. Knepley   const PetscReal s2_x  = r1_x - r0_x;
168ddce0771SMatthew G. Knepley   const PetscReal s2_y  = r1_y - r0_y;
169ddce0771SMatthew G. Knepley   const PetscReal s2_z  = r1_z - r0_z;
170ddce0771SMatthew G. Knepley   const PetscReal s3_x  = s1_y * s2_z - s1_z * s2_y; /* s1 x s2 */
171ddce0771SMatthew G. Knepley   const PetscReal s3_y  = s1_z * s2_x - s1_x * s2_z;
172ddce0771SMatthew G. Knepley   const PetscReal s3_z  = s1_x * s2_y - s1_y * s2_x;
173ddce0771SMatthew G. Knepley   const PetscReal s4_x  = s0_y * s2_z - s0_z * s2_y; /* s0 x s2 */
174ddce0771SMatthew G. Knepley   const PetscReal s4_y  = s0_z * s2_x - s0_x * s2_z;
175ddce0771SMatthew G. Knepley   const PetscReal s4_z  = s0_x * s2_y - s0_y * s2_x;
176ddce0771SMatthew G. Knepley   const PetscReal s5_x  = s1_y * s0_z - s1_z * s0_y; /* s1 x s0 */
177ddce0771SMatthew G. Knepley   const PetscReal s5_y  = s1_z * s0_x - s1_x * s0_z;
178ddce0771SMatthew G. Knepley   const PetscReal s5_z  = s1_x * s0_y - s1_y * s0_x;
179ddce0771SMatthew G. Knepley   const PetscReal denom = -(s0_x * s3_x + s0_y * s3_y + s0_z * s3_z); /* -s0 . (s1 x s2) */
180ddce0771SMatthew G. Knepley 
181ddce0771SMatthew G. Knepley   PetscFunctionBegin;
182ddce0771SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
183ddce0771SMatthew G. Knepley   /* Line not parallel to plane */
184ddce0771SMatthew G. Knepley   if (denom != 0.0) {
185ddce0771SMatthew G. Knepley     const PetscReal t = (s3_x * (p0_x - q0_x) + s3_y * (p0_y - q0_y) + s3_z * (p0_z - q0_z)) / denom;
186ddce0771SMatthew G. Knepley     const PetscReal u = (s4_x * (p0_x - q0_x) + s4_y * (p0_y - q0_y) + s4_z * (p0_z - q0_z)) / denom;
187ddce0771SMatthew G. Knepley     const PetscReal v = (s5_x * (p0_x - q0_x) + s5_y * (p0_y - q0_y) + s5_z * (p0_z - q0_z)) / denom;
188ddce0771SMatthew G. Knepley 
189ddce0771SMatthew G. Knepley     if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && v >= 0 && v <= 1) {
190ddce0771SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
191ddce0771SMatthew G. Knepley       if (intersection) {
192ddce0771SMatthew G. Knepley         intersection[0] = p0_x + (t * s0_x);
193ddce0771SMatthew G. Knepley         intersection[1] = p0_y + (t * s0_y);
194ddce0771SMatthew G. Knepley         intersection[2] = p0_z + (t * s0_z);
195ddce0771SMatthew G. Knepley       }
196ddce0771SMatthew G. Knepley     }
197ddce0771SMatthew G. Knepley   }
198ddce0771SMatthew G. Knepley   PetscFunctionReturn(0);
199ddce0771SMatthew G. Knepley }
200ddce0771SMatthew G. Knepley 
201d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
202d71ae5a4SJacob Faibussowitsch {
20314bbb9f0SLawrence Mitchell   const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON;
20414bbb9f0SLawrence Mitchell   const PetscReal x   = PetscRealPart(point[0]);
20514bbb9f0SLawrence Mitchell   PetscReal       v0, J, invJ, detJ;
20614bbb9f0SLawrence Mitchell   PetscReal       xi;
20714bbb9f0SLawrence Mitchell 
20814bbb9f0SLawrence Mitchell   PetscFunctionBegin;
2099566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ));
21014bbb9f0SLawrence Mitchell   xi = invJ * (x - v0);
21114bbb9f0SLawrence Mitchell 
21214bbb9f0SLawrence Mitchell   if ((xi >= -eps) && (xi <= 2. + eps)) *cell = c;
21314bbb9f0SLawrence Mitchell   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
21414bbb9f0SLawrence Mitchell   PetscFunctionReturn(0);
21514bbb9f0SLawrence Mitchell }
21614bbb9f0SLawrence Mitchell 
217d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
218d71ae5a4SJacob Faibussowitsch {
219ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
220f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
221ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
222ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
223ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
224ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
225ccd2543fSMatthew G Knepley 
226ccd2543fSMatthew G Knepley   PetscFunctionBegin;
2279566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
228ccd2543fSMatthew G Knepley   xi  = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]);
229ccd2543fSMatthew G Knepley   eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]);
230ccd2543fSMatthew G Knepley 
231f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0 + eps)) *cell = c;
232c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
233ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
234ccd2543fSMatthew G Knepley }
235ccd2543fSMatthew G Knepley 
236d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
237d71ae5a4SJacob Faibussowitsch {
23862a38674SMatthew G. Knepley   const PetscInt embedDim = 2;
23962a38674SMatthew G. Knepley   PetscReal      x        = PetscRealPart(point[0]);
24062a38674SMatthew G. Knepley   PetscReal      y        = PetscRealPart(point[1]);
24162a38674SMatthew G. Knepley   PetscReal      v0[2], J[4], invJ[4], detJ;
24262a38674SMatthew G. Knepley   PetscReal      xi, eta, r;
24362a38674SMatthew G. Knepley 
24462a38674SMatthew G. Knepley   PetscFunctionBegin;
2459566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
24662a38674SMatthew G. Knepley   xi  = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]);
24762a38674SMatthew G. Knepley   eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]);
24862a38674SMatthew G. Knepley 
24962a38674SMatthew G. Knepley   xi  = PetscMax(xi, 0.0);
25062a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
25162a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
25262a38674SMatthew G. Knepley     r = (xi + eta) / 2.0;
25362a38674SMatthew G. Knepley     xi /= r;
25462a38674SMatthew G. Knepley     eta /= r;
25562a38674SMatthew G. Knepley   }
25662a38674SMatthew G. Knepley   cpoint[0] = J[0 * embedDim + 0] * xi + J[0 * embedDim + 1] * eta + v0[0];
25762a38674SMatthew G. Knepley   cpoint[1] = J[1 * embedDim + 0] * xi + J[1 * embedDim + 1] * eta + v0[1];
25862a38674SMatthew G. Knepley   PetscFunctionReturn(0);
25962a38674SMatthew G. Knepley }
26062a38674SMatthew G. Knepley 
261d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
262d71ae5a4SJacob Faibussowitsch {
26376b3799dSMatthew G. Knepley   const PetscScalar *array;
264a1e44745SMatthew G. Knepley   PetscScalar       *coords    = NULL;
265ccd2543fSMatthew G Knepley   const PetscInt     faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
266ccd2543fSMatthew G Knepley   PetscReal          x         = PetscRealPart(point[0]);
267ccd2543fSMatthew G Knepley   PetscReal          y         = PetscRealPart(point[1]);
26876b3799dSMatthew G. Knepley   PetscInt           crossings = 0, numCoords, f;
26976b3799dSMatthew G. Knepley   PetscBool          isDG;
270ccd2543fSMatthew G Knepley 
271ccd2543fSMatthew G Knepley   PetscFunctionBegin;
27276b3799dSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords));
27376b3799dSMatthew G. Knepley   PetscCheck(numCoords == 8, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Quadrilateral should have 8 coordinates, not %" PetscInt_FMT, numCoords);
274ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
275ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2 * f + 0] * 2 + 0]);
276ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2 * f + 0] * 2 + 1]);
277ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2 * f + 1] * 2 + 0]);
278ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2 * f + 1] * 2 + 1]);
279ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
280ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
281ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
282ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
283ccd2543fSMatthew G Knepley     if ((cond1 || cond2) && above) ++crossings;
284ccd2543fSMatthew G Knepley   }
285ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
286c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
28776b3799dSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords));
288ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
289ccd2543fSMatthew G Knepley }
290ccd2543fSMatthew G Knepley 
291d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
292d71ae5a4SJacob Faibussowitsch {
293ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 3;
29437900f7dSMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
295ccd2543fSMatthew G Knepley   PetscReal       v0[3], J[9], invJ[9], detJ;
296ccd2543fSMatthew G Knepley   PetscReal       x = PetscRealPart(point[0]);
297ccd2543fSMatthew G Knepley   PetscReal       y = PetscRealPart(point[1]);
298ccd2543fSMatthew G Knepley   PetscReal       z = PetscRealPart(point[2]);
299ccd2543fSMatthew G Knepley   PetscReal       xi, eta, zeta;
300ccd2543fSMatthew G Knepley 
301ccd2543fSMatthew G Knepley   PetscFunctionBegin;
3029566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
303ccd2543fSMatthew G Knepley   xi   = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]) + invJ[0 * embedDim + 2] * (z - v0[2]);
304ccd2543fSMatthew G Knepley   eta  = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]) + invJ[1 * embedDim + 2] * (z - v0[2]);
305ccd2543fSMatthew G Knepley   zeta = invJ[2 * embedDim + 0] * (x - v0[0]) + invJ[2 * embedDim + 1] * (y - v0[1]) + invJ[2 * embedDim + 2] * (z - v0[2]);
306ccd2543fSMatthew G Knepley 
30737900f7dSMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0 + eps)) *cell = c;
308c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
309ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
310ccd2543fSMatthew G Knepley }
311ccd2543fSMatthew G Knepley 
312d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
313d71ae5a4SJacob Faibussowitsch {
31476b3799dSMatthew G. Knepley   const PetscScalar *array;
315872a9804SMatthew G. Knepley   PetscScalar       *coords    = NULL;
3169371c9d4SSatish Balay   const PetscInt     faces[24] = {0, 3, 2, 1, 5, 4, 7, 6, 3, 0, 4, 5, 1, 2, 6, 7, 3, 5, 6, 2, 0, 1, 7, 4};
317ccd2543fSMatthew G Knepley   PetscBool          found     = PETSC_TRUE;
31876b3799dSMatthew G. Knepley   PetscInt           numCoords, f;
31976b3799dSMatthew G. Knepley   PetscBool          isDG;
320ccd2543fSMatthew G Knepley 
321ccd2543fSMatthew G Knepley   PetscFunctionBegin;
32276b3799dSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords));
32376b3799dSMatthew G. Knepley   PetscCheck(numCoords == 24, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Quadrilateral should have 8 coordinates, not %" PetscInt_FMT, numCoords);
324ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
325ccd2543fSMatthew G Knepley     /* Check the point is under plane */
326ccd2543fSMatthew G Knepley     /*   Get face normal */
327ccd2543fSMatthew G Knepley     PetscReal v_i[3];
328ccd2543fSMatthew G Knepley     PetscReal v_j[3];
329ccd2543fSMatthew G Knepley     PetscReal normal[3];
330ccd2543fSMatthew G Knepley     PetscReal pp[3];
331ccd2543fSMatthew G Knepley     PetscReal dot;
332ccd2543fSMatthew G Knepley 
333ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]);
334ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]);
335ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]);
336ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]);
337ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]);
338ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]);
339ccd2543fSMatthew G Knepley     normal[0] = v_i[1] * v_j[2] - v_i[2] * v_j[1];
340ccd2543fSMatthew G Knepley     normal[1] = v_i[2] * v_j[0] - v_i[0] * v_j[2];
341ccd2543fSMatthew G Knepley     normal[2] = v_i[0] * v_j[1] - v_i[1] * v_j[0];
342ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 0] - point[0]);
343ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 1] - point[1]);
344ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 2] - point[2]);
345ccd2543fSMatthew G Knepley     dot       = normal[0] * pp[0] + normal[1] * pp[1] + normal[2] * pp[2];
346ccd2543fSMatthew G Knepley 
347ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
348ccd2543fSMatthew G Knepley     if (dot < 0.0) {
349ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
350ccd2543fSMatthew G Knepley       break;
351ccd2543fSMatthew G Knepley     }
352ccd2543fSMatthew G Knepley   }
353ccd2543fSMatthew G Knepley   if (found) *cell = c;
354c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
35576b3799dSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &coords));
356ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
357ccd2543fSMatthew G Knepley }
358ccd2543fSMatthew G Knepley 
359d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
360d71ae5a4SJacob Faibussowitsch {
361c4eade1cSMatthew G. Knepley   PetscInt d;
362c4eade1cSMatthew G. Knepley 
363c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
364c4eade1cSMatthew G. Knepley   box->dim = dim;
365c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
366c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
367c4eade1cSMatthew G. Knepley }
368c4eade1cSMatthew G. Knepley 
369d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
370d71ae5a4SJacob Faibussowitsch {
371c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
3729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, box));
3739566063dSJacob Faibussowitsch   PetscCall(PetscGridHashInitialize_Internal(*box, dim, point));
374c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
375c4eade1cSMatthew G. Knepley }
376c4eade1cSMatthew G. Knepley 
377d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
378d71ae5a4SJacob Faibussowitsch {
379c4eade1cSMatthew G. Knepley   PetscInt d;
380c4eade1cSMatthew G. Knepley 
381c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
382c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
383c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
384c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
385c4eade1cSMatthew G. Knepley   }
386c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
387c4eade1cSMatthew G. Knepley }
388c4eade1cSMatthew G. Knepley 
38962a38674SMatthew G. Knepley /*
39062a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
39162a38674SMatthew G. Knepley 
39262a38674SMatthew G. Knepley   Not collective
39362a38674SMatthew G. Knepley 
39462a38674SMatthew G. Knepley   Input Parameters:
39562a38674SMatthew G. Knepley + box - The grid hash object
39662a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
39762a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
39862a38674SMatthew G. Knepley 
39962a38674SMatthew G. Knepley   Level: developer
40062a38674SMatthew G. Knepley 
401db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`
40262a38674SMatthew G. Knepley */
403d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
404d71ae5a4SJacob Faibussowitsch {
405c4eade1cSMatthew G. Knepley   PetscInt d;
406c4eade1cSMatthew G. Knepley 
407c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
408c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
409c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
410c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
411c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
412c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d] / h[d]);
413c4eade1cSMatthew G. Knepley     } else {
414c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
415c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d] / n[d];
416c4eade1cSMatthew G. Knepley     }
417c4eade1cSMatthew G. Knepley   }
418c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
419c4eade1cSMatthew G. Knepley }
420c4eade1cSMatthew G. Knepley 
42162a38674SMatthew G. Knepley /*
42262a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
42362a38674SMatthew G. Knepley 
42462a38674SMatthew G. Knepley   Not collective
42562a38674SMatthew G. Knepley 
42662a38674SMatthew G. Knepley   Input Parameters:
42762a38674SMatthew G. Knepley + box       - The grid hash object
42862a38674SMatthew G. Knepley . numPoints - The number of input points
42962a38674SMatthew G. Knepley - points    - The input point coordinates
43062a38674SMatthew G. Knepley 
43162a38674SMatthew G. Knepley   Output Parameters:
43262a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
43362a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
43462a38674SMatthew G. Knepley 
43562a38674SMatthew G. Knepley   Level: developer
43662a38674SMatthew G. Knepley 
437f5867de0SMatthew G. Knepley   Note:
438f5867de0SMatthew G. Knepley   This only guarantees that a box contains a point, not that a cell does.
439f5867de0SMatthew G. Knepley 
440db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`
44162a38674SMatthew G. Knepley */
442d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
443d71ae5a4SJacob Faibussowitsch {
444c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
445c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
446c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
447c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
448c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
449c4eade1cSMatthew G. Knepley   PetscInt         d, p;
450c4eade1cSMatthew G. Knepley 
451c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
452c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
453c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4541c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]);
455c4eade1cSMatthew G. Knepley 
4561c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1;
4572a705cacSMatthew G. Knepley       if (dbox == -1 && PetscAbsReal(PetscRealPart(points[p * dim + d]) - lower[d]) < 1.0e-9) dbox = 0;
4589371c9d4SSatish Balay       PetscCheck(dbox >= 0 && dbox<n[d], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %" PetscInt_FMT " (%g, %g, %g) is outside of our bounding box", p, (double)PetscRealPart(points[p * dim + 0]), dim> 1 ? (double)PetscRealPart(points[p * dim + 1]) : 0.0, dim > 2 ? (double)PetscRealPart(points[p * dim + 2]) : 0.0);
459c4eade1cSMatthew G. Knepley       dboxes[p * dim + d] = dbox;
460c4eade1cSMatthew G. Knepley     }
4619371c9d4SSatish Balay     if (boxes)
4629371c9d4SSatish Balay       for (d = dim - 2, boxes[p] = dboxes[p * dim + dim - 1]; d >= 0; --d) boxes[p] = boxes[p] * n[d] + dboxes[p * dim + d];
463c4eade1cSMatthew G. Knepley   }
464c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
465c4eade1cSMatthew G. Knepley }
466c4eade1cSMatthew G. Knepley 
467af74b616SDave May /*
468af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
469af74b616SDave May 
470af74b616SDave May  Not collective
471af74b616SDave May 
472af74b616SDave May   Input Parameters:
473af74b616SDave May + box         - The grid hash object
474f5867de0SMatthew G. Knepley . cellSection - The PetscSection mapping cells to boxes
475af74b616SDave May . numPoints   - The number of input points
476af74b616SDave May - points      - The input point coordinates
477af74b616SDave May 
478af74b616SDave May   Output Parameters:
479af74b616SDave May + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
480af74b616SDave May . boxes  - An array of numPoints integers expressing the enclosing box as single number, or NULL
481af74b616SDave May - found  - Flag indicating if point was located within a box
482af74b616SDave May 
483af74b616SDave May   Level: developer
484af74b616SDave May 
485f5867de0SMatthew G. Knepley   Note:
486f5867de0SMatthew G. Knepley   This does an additional check that a cell actually contains the point, and found is PETSC_FALSE if no cell does. Thus, this function requires that the cellSection is already constructed.
487f5867de0SMatthew G. Knepley 
488db781477SPatrick Sanan .seealso: `PetscGridHashGetEnclosingBox()`
489af74b616SDave May */
490d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscSection cellSection, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[], PetscBool *found)
491d71ae5a4SJacob Faibussowitsch {
492af74b616SDave May   const PetscReal *lower = box->lower;
493af74b616SDave May   const PetscReal *upper = box->upper;
494af74b616SDave May   const PetscReal *h     = box->h;
495af74b616SDave May   const PetscInt  *n     = box->n;
496af74b616SDave May   const PetscInt   dim   = box->dim;
497f5867de0SMatthew G. Knepley   PetscInt         bStart, bEnd, d, p;
498af74b616SDave May 
499af74b616SDave May   PetscFunctionBegin;
500f5867de0SMatthew G. Knepley   PetscValidHeaderSpecific(cellSection, PETSC_SECTION_CLASSID, 2);
501af74b616SDave May   *found = PETSC_FALSE;
502f5867de0SMatthew G. Knepley   PetscCall(PetscSectionGetChart(box->cellSection, &bStart, &bEnd));
503af74b616SDave May   for (p = 0; p < numPoints; ++p) {
504af74b616SDave May     for (d = 0; d < dim; ++d) {
505af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]);
506af74b616SDave May 
507af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1;
508f5867de0SMatthew G. Knepley       if (dbox < 0 || dbox >= n[d]) PetscFunctionReturn(0);
509af74b616SDave May       dboxes[p * dim + d] = dbox;
510af74b616SDave May     }
5119371c9d4SSatish Balay     if (boxes)
5129371c9d4SSatish Balay       for (d = dim - 2, boxes[p] = dboxes[p * dim + dim - 1]; d >= 0; --d) boxes[p] = boxes[p] * n[d] + dboxes[p * dim + d];
513f5867de0SMatthew G. Knepley     // It is possible for a box to overlap no grid cells
514f5867de0SMatthew G. Knepley     if (boxes[p] < bStart || boxes[p] >= bEnd) PetscFunctionReturn(0);
515af74b616SDave May   }
516af74b616SDave May   *found = PETSC_TRUE;
517af74b616SDave May   PetscFunctionReturn(0);
518af74b616SDave May }
519af74b616SDave May 
520d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
521d71ae5a4SJacob Faibussowitsch {
522c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
523c4eade1cSMatthew G. Knepley   if (*box) {
5249566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&(*box)->cellSection));
5259566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&(*box)->cells));
5269566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&(*box)->cellsSparse));
527c4eade1cSMatthew G. Knepley   }
5289566063dSJacob Faibussowitsch   PetscCall(PetscFree(*box));
529c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
530c4eade1cSMatthew G. Knepley }
531c4eade1cSMatthew G. Knepley 
532d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
533d71ae5a4SJacob Faibussowitsch {
534ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
535cafe43deSMatthew G. Knepley 
536cafe43deSMatthew G. Knepley   PetscFunctionBegin;
5379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cellStart, &ct));
538ba2698f1SMatthew G. Knepley   switch (ct) {
539d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_SEGMENT:
540d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell));
541d71ae5a4SJacob Faibussowitsch     break;
542d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_TRIANGLE:
543d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell));
544d71ae5a4SJacob Faibussowitsch     break;
545d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUADRILATERAL:
546d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell));
547d71ae5a4SJacob Faibussowitsch     break;
548d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_TETRAHEDRON:
549d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell));
550d71ae5a4SJacob Faibussowitsch     break;
551d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
552d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell));
553d71ae5a4SJacob Faibussowitsch     break;
554d71ae5a4SJacob Faibussowitsch   default:
555d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %" PetscInt_FMT " with type %s", cellStart, DMPolytopeTypes[ct]);
556cafe43deSMatthew G. Knepley   }
557cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
558cafe43deSMatthew G. Knepley }
559cafe43deSMatthew G. Knepley 
56062a38674SMatthew G. Knepley /*
56162a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
56262a38674SMatthew G. Knepley */
563d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
564d71ae5a4SJacob Faibussowitsch {
565ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
56662a38674SMatthew G. Knepley 
56762a38674SMatthew G. Knepley   PetscFunctionBegin;
5689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
569ba2698f1SMatthew G. Knepley   switch (ct) {
570d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_TRIANGLE:
571d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint));
572d71ae5a4SJacob Faibussowitsch     break;
57362a38674SMatthew G. Knepley #if 0
574ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
5759566063dSJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint));break;
576ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
5779566063dSJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint));break;
578ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
5799566063dSJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint));break;
58062a38674SMatthew G. Knepley #endif
581d71ae5a4SJacob Faibussowitsch   default:
582d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %" PetscInt_FMT " with type %s", cell, DMPolytopeTypes[ct]);
58362a38674SMatthew G. Knepley   }
58462a38674SMatthew G. Knepley   PetscFunctionReturn(0);
58562a38674SMatthew G. Knepley }
58662a38674SMatthew G. Knepley 
58762a38674SMatthew G. Knepley /*
58862a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
58962a38674SMatthew G. Knepley 
590d083f849SBarry Smith   Collective on dm
59162a38674SMatthew G. Knepley 
59262a38674SMatthew G. Knepley   Input Parameter:
59362a38674SMatthew G. Knepley . dm - The Plex
59462a38674SMatthew G. Knepley 
59562a38674SMatthew G. Knepley   Output Parameter:
59662a38674SMatthew G. Knepley . localBox - The grid hash object
59762a38674SMatthew G. Knepley 
59862a38674SMatthew G. Knepley   Level: developer
59962a38674SMatthew G. Knepley 
600db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`, `PetscGridHashGetEnclosingBox()`
60162a38674SMatthew G. Knepley */
602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
603d71ae5a4SJacob Faibussowitsch {
604f5867de0SMatthew G. Knepley   PetscInt           debug = ((DM_Plex *)dm->data)->printLocate;
605cafe43deSMatthew G. Knepley   MPI_Comm           comm;
606cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
607cafe43deSMatthew G. Knepley   Vec                coordinates;
608cafe43deSMatthew G. Knepley   PetscSection       coordSection;
609cafe43deSMatthew G. Knepley   Vec                coordsLocal;
610cafe43deSMatthew G. Knepley   const PetscScalar *coords;
611ddce0771SMatthew G. Knepley   PetscScalar       *edgeCoords;
612722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
613ddce0771SMatthew G. Knepley   PetscInt           n[3] = {2, 2, 2};
614ddce0771SMatthew G. Knepley   PetscInt           dim, N, maxConeSize, cStart, cEnd, c, eStart, eEnd, i;
615ddce0771SMatthew G. Knepley   PetscBool          flg;
616cafe43deSMatthew G. Knepley 
617cafe43deSMatthew G. Knepley   PetscFunctionBegin;
6189566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
6199566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
6209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
6219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
6229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
6239566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(coordinates, &N));
6249566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
6259566063dSJacob Faibussowitsch   PetscCall(PetscGridHashCreate(comm, dim, coords, &lbox));
6269566063dSJacob Faibussowitsch   for (i = 0; i < N; i += dim) PetscCall(PetscGridHashEnlarge(lbox, &coords[i]));
6279566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
628ddce0771SMatthew G. Knepley   c = dim;
6299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, "-dm_plex_hash_box_faces", n, &c, &flg));
6309371c9d4SSatish Balay   if (flg) {
6319371c9d4SSatish Balay     for (i = c; i < dim; ++i) n[i] = n[c - 1];
6329371c9d4SSatish Balay   } else {
6339371c9d4SSatish Balay     for (i = 0; i < dim; ++i) n[i] = PetscMax(2, PetscFloorReal(PetscPowReal((PetscReal)(cEnd - cStart), 1.0 / dim) * 0.8));
6349371c9d4SSatish Balay   }
6359566063dSJacob Faibussowitsch   PetscCall(PetscGridHashSetGrid(lbox, n, NULL));
6369371c9d4SSatish Balay   if (debug)
6379371c9d4SSatish 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],
6389371c9d4SSatish 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]));
639cafe43deSMatthew G. Knepley #if 0
640cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
6411c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm));
6421c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm));
643cafe43deSMatthew G. Knepley #endif
644cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
645cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
646cafe43deSMatthew G. Knepley   /* Create label */
6479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
648b26b5bf9SMatthew G. Knepley   if (dim < 2) eStart = eEnd = -1;
6499566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse));
6509566063dSJacob Faibussowitsch   PetscCall(DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd));
651a8d69d7bSBarry Smith   /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
6529566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
6539566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6549566063dSJacob Faibussowitsch   PetscCall(PetscCalloc3(16 * dim, &dboxes, 16, &boxes, PetscPowInt(maxConeSize, dim) * dim, &edgeCoords));
655cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
656cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
657cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
65838353de4SMatthew G. Knepley     PetscInt         csize   = 0;
659ddce0771SMatthew G. Knepley     PetscInt        *closure = NULL;
660ddce0771SMatthew G. Knepley     PetscInt         Ncl, cl, Ne = 0;
661cafe43deSMatthew G. Knepley     PetscScalar      point[3];
662cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
663cafe43deSMatthew G. Knepley 
664ddce0771SMatthew G. Knepley     /* Get all edges in cell */
6659566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure));
666ddce0771SMatthew G. Knepley     for (cl = 0; cl < Ncl * 2; ++cl) {
667ddce0771SMatthew G. Knepley       if ((closure[cl] >= eStart) && (closure[cl] < eEnd)) {
668ddce0771SMatthew G. Knepley         PetscScalar *ecoords = &edgeCoords[Ne * dim * 2];
669ddce0771SMatthew G. Knepley         PetscInt     ecsize  = dim * 2;
670ddce0771SMatthew G. Knepley 
6719566063dSJacob Faibussowitsch         PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, closure[cl], &ecsize, &ecoords));
67263a3b9bcSJacob 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);
673ddce0771SMatthew G. Knepley         ++Ne;
674ddce0771SMatthew G. Knepley       }
675ddce0771SMatthew G. Knepley     }
6769566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure));
677cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
6789566063dSJacob Faibussowitsch     PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords));
6799566063dSJacob Faibussowitsch     PetscCall(PetscGridHashGetEnclosingBox(lbox, csize / dim, ccoords, dboxes, boxes));
680722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
681ddce0771SMatthew G. Knepley     for (e = 0; e < csize / dim; ++e) {
6829371c9d4SSatish Balay       if (debug)
6839d67f9e5SMatthew 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));
6849566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(lbox->cellsSparse, c, boxes[e]));
685ddce0771SMatthew G. Knepley     }
686cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
687ad540459SPierre Jolivet     for (d = 0; d < dim; ++d) dlim[d * 2 + 0] = dlim[d * 2 + 1] = dboxes[d];
688ad540459SPierre Jolivet     for (d = dim; d < 3; ++d) dlim[d * 2 + 0] = dlim[d * 2 + 1] = 0;
689cafe43deSMatthew G. Knepley     for (e = 1; e < dim + 1; ++e) {
690cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
691cafe43deSMatthew G. Knepley         dlim[d * 2 + 0] = PetscMin(dlim[d * 2 + 0], dboxes[e * dim + d]);
692cafe43deSMatthew G. Knepley         dlim[d * 2 + 1] = PetscMax(dlim[d * 2 + 1], dboxes[e * dim + d]);
693cafe43deSMatthew G. Knepley       }
694cafe43deSMatthew G. Knepley     }
695fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
696cafe43deSMatthew 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]) {
697cafe43deSMatthew 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]) {
698cafe43deSMatthew 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]) {
699cafe43deSMatthew G. Knepley           const PetscInt box = (k * lbox->n[1] + j) * lbox->n[0] + i;
700cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
701fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
702cafe43deSMatthew G. Knepley 
7039371c9d4SSatish Balay           if (debug)
7049371c9d4SSatish 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])));
705ddce0771SMatthew G. Knepley           /* Check whether cell contains any vertex of this subbox TODO vectorize this */
706cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
707cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
708cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
7099566063dSJacob Faibussowitsch                 PetscCall(DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell));
7100b6bfacdSStefano Zampini                 if (cell >= 0) {
7119371c9d4SSatish Balay                   if (debug)
7129371c9d4SSatish 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));
7139566063dSJacob Faibussowitsch                   PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box));
7140b6bfacdSStefano Zampini                   jj = kk = 2;
7150b6bfacdSStefano Zampini                   break;
7160b6bfacdSStefano Zampini                 }
717cafe43deSMatthew G. Knepley               }
718cafe43deSMatthew G. Knepley             }
719cafe43deSMatthew G. Knepley           }
720ddce0771SMatthew G. Knepley           /* Check whether cell edge intersects any face of these subboxes TODO vectorize this */
721ddce0771SMatthew G. Knepley           for (edge = 0; edge < Ne; ++edge) {
722a5cae605SSatish Balay             PetscReal segA[6] = {0., 0., 0., 0., 0., 0.};
723a5cae605SSatish Balay             PetscReal segB[6] = {0., 0., 0., 0., 0., 0.};
724a5cae605SSatish Balay             PetscReal segC[6] = {0., 0., 0., 0., 0., 0.};
725fea14342SMatthew G. Knepley 
72663a3b9bcSJacob Faibussowitsch             PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected dim %" PetscInt_FMT " > 3", dim);
727ddce0771SMatthew G. Knepley             for (d = 0; d < dim * 2; ++d) segA[d] = PetscRealPart(edgeCoords[edge * dim * 2 + d]);
728ddce0771SMatthew G. Knepley             /* 1D: (x) -- (x+h)               0 -- 1
729ddce0771SMatthew G. Knepley                2D: (x,   y)   -- (x,   y+h)   (0, 0) -- (0, 1)
730ddce0771SMatthew G. Knepley                    (x+h, y)   -- (x+h, y+h)   (1, 0) -- (1, 1)
731ddce0771SMatthew G. Knepley                    (x,   y)   -- (x+h, y)     (0, 0) -- (1, 0)
732ddce0771SMatthew G. Knepley                    (x,   y+h) -- (x+h, y+h)   (0, 1) -- (1, 1)
733ddce0771SMatthew 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)
734ddce0771SMatthew 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)
735ddce0771SMatthew 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)
736ddce0771SMatthew 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)
737ddce0771SMatthew 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)
738ddce0771SMatthew 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)
739ddce0771SMatthew G. Knepley              */
740ddce0771SMatthew G. Knepley             /* Loop over faces with normal in direction d */
741ddce0771SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
742ddce0771SMatthew G. Knepley               PetscBool intersects = PETSC_FALSE;
743ddce0771SMatthew G. Knepley               PetscInt  e          = (d + 1) % dim;
744ddce0771SMatthew G. Knepley               PetscInt  f          = (d + 2) % dim;
745ddce0771SMatthew G. Knepley 
746ddce0771SMatthew G. Knepley               /* There are two faces in each dimension */
747ddce0771SMatthew G. Knepley               for (ii = 0; ii < 2; ++ii) {
748ddce0771SMatthew G. Knepley                 segB[d]       = PetscRealPart(point[d] + ii * h[d]);
749ddce0771SMatthew G. Knepley                 segB[dim + d] = PetscRealPart(point[d] + ii * h[d]);
750ddce0771SMatthew G. Knepley                 segC[d]       = PetscRealPart(point[d] + ii * h[d]);
751ddce0771SMatthew G. Knepley                 segC[dim + d] = PetscRealPart(point[d] + ii * h[d]);
752ddce0771SMatthew G. Knepley                 if (dim > 1) {
753ddce0771SMatthew G. Knepley                   segB[e]       = PetscRealPart(point[e] + 0 * h[e]);
754ddce0771SMatthew G. Knepley                   segB[dim + e] = PetscRealPart(point[e] + 1 * h[e]);
755ddce0771SMatthew G. Knepley                   segC[e]       = PetscRealPart(point[e] + 0 * h[e]);
756ddce0771SMatthew G. Knepley                   segC[dim + e] = PetscRealPart(point[e] + 0 * h[e]);
757ddce0771SMatthew G. Knepley                 }
758ddce0771SMatthew G. Knepley                 if (dim > 2) {
759ddce0771SMatthew G. Knepley                   segB[f]       = PetscRealPart(point[f] + 0 * h[f]);
760ddce0771SMatthew G. Knepley                   segB[dim + f] = PetscRealPart(point[f] + 0 * h[f]);
761ddce0771SMatthew G. Knepley                   segC[f]       = PetscRealPart(point[f] + 0 * h[f]);
762ddce0771SMatthew G. Knepley                   segC[dim + f] = PetscRealPart(point[f] + 1 * h[f]);
763ddce0771SMatthew G. Knepley                 }
764ddce0771SMatthew G. Knepley                 if (dim == 2) {
7659566063dSJacob Faibussowitsch                   PetscCall(DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects));
766ddce0771SMatthew G. Knepley                 } else if (dim == 3) {
7679566063dSJacob Faibussowitsch                   PetscCall(DMPlexGetLinePlaneIntersection_3D_Internal(segA, segB, segC, NULL, &intersects));
768ddce0771SMatthew G. Knepley                 }
769ddce0771SMatthew G. Knepley                 if (intersects) {
7709371c9d4SSatish Balay                   if (debug)
7719371c9d4SSatish 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]));
7729371c9d4SSatish Balay                   PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box));
7739371c9d4SSatish Balay                   edge = Ne;
7749371c9d4SSatish Balay                   break;
775ddce0771SMatthew G. Knepley                 }
776ddce0771SMatthew G. Knepley               }
777ddce0771SMatthew G. Knepley             }
778cafe43deSMatthew G. Knepley           }
779fea14342SMatthew G. Knepley         }
780fea14342SMatthew G. Knepley       }
781fea14342SMatthew G. Knepley     }
7829566063dSJacob Faibussowitsch     PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords));
783fea14342SMatthew G. Knepley   }
7849566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dboxes, boxes, edgeCoords));
7859566063dSJacob Faibussowitsch   if (debug) PetscCall(DMLabelView(lbox->cellsSparse, PETSC_VIEWER_STDOUT_SELF));
7869566063dSJacob Faibussowitsch   PetscCall(DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells));
7879566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&lbox->cellsSparse));
788cafe43deSMatthew G. Knepley   *localBox = lbox;
789cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
790cafe43deSMatthew G. Knepley }
791cafe43deSMatthew G. Knepley 
792d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
793d71ae5a4SJacob Faibussowitsch {
794f5867de0SMatthew G. Knepley   PetscInt        debug = ((DM_Plex *)dm->data)->printLocate;
795cafe43deSMatthew G. Knepley   DM_Plex        *mesh  = (DM_Plex *)dm->data;
796af74b616SDave May   PetscBool       hash = mesh->useHashLocation, reuse = PETSC_FALSE;
7973a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
798d8206211SMatthew G. Knepley   PetscInt        dim, Nl = 0, cStart, cEnd, numCells, c, d;
799d8206211SMatthew G. Knepley   PetscSF         sf;
800d8206211SMatthew G. Knepley   const PetscInt *leaves;
801cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
8023a93e3b7SToby Isaac   PetscSFNode    *cells;
803ccd2543fSMatthew G Knepley   PetscScalar    *a;
8043a93e3b7SToby Isaac   PetscMPIInt     result;
805af74b616SDave May   PetscLogDouble  t0, t1;
8069cb35068SDave May   PetscReal       gmin[3], gmax[3];
8079cb35068SDave May   PetscInt        terminating_query_type[] = {0, 0, 0};
808ccd2543fSMatthew G Knepley 
809ccd2543fSMatthew G Knepley   PetscFunctionBegin;
8109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_LocatePoints, 0, 0, 0, 0));
8119566063dSJacob Faibussowitsch   PetscCall(PetscTime(&t0));
8121dca8a05SBarry 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.");
8139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
8149566063dSJacob Faibussowitsch   PetscCall(VecGetBlockSize(v, &bs));
8159566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF), PETSC_COMM_SELF, &result));
8161dca8a05SBarry Smith   PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT, PetscObjectComm((PetscObject)cellSF), PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
81763a3b9bcSJacob 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);
8186858538eSMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalSetUp(dm));
8199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
820d8206211SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &sf));
821d8206211SMatthew G. Knepley   if (sf) PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
822d8206211SMatthew G. Knepley   Nl = PetscMax(Nl, 0);
8239566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(v, &numPoints));
8249566063dSJacob Faibussowitsch   PetscCall(VecGetArray(v, &a));
825ccd2543fSMatthew G Knepley   numPoints /= bs;
826af74b616SDave May   {
827af74b616SDave May     const PetscSFNode *sf_cells;
828af74b616SDave May 
8299566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(cellSF, NULL, NULL, NULL, &sf_cells));
830af74b616SDave May     if (sf_cells) {
8319566063dSJacob Faibussowitsch       PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Re-using existing StarForest node list\n"));
832af74b616SDave May       cells = (PetscSFNode *)sf_cells;
833af74b616SDave May       reuse = PETSC_TRUE;
834af74b616SDave May     } else {
8359566063dSJacob Faibussowitsch       PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n"));
8369566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numPoints, &cells));
837af74b616SDave May       /* initialize cells if created */
838af74b616SDave May       for (p = 0; p < numPoints; p++) {
839af74b616SDave May         cells[p].rank  = 0;
840af74b616SDave May         cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
841af74b616SDave May       }
842af74b616SDave May     }
843af74b616SDave May   }
84476b3799dSMatthew G. Knepley   PetscCall(DMGetBoundingBox(dm, gmin, gmax));
845953fc75cSMatthew G. Knepley   if (hash) {
8469371c9d4SSatish Balay     if (!mesh->lbox) {
8479371c9d4SSatish Balay       PetscCall(PetscInfo(dm, "Initializing grid hashing"));
8489371c9d4SSatish Balay       PetscCall(DMPlexComputeGridHash_Internal(dm, &mesh->lbox));
8499371c9d4SSatish Balay     }
850cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
851cafe43deSMatthew G. Knepley     /* Send points to correct process */
852cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
853cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
8549566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(mesh->lbox->cells, &boxCells));
855953fc75cSMatthew G. Knepley   }
8563a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
857ccd2543fSMatthew G Knepley     const PetscScalar *point   = &a[p * bs];
858e56f9228SJed Brown     PetscInt           dbin[3] = {-1, -1, -1}, bin, cell = -1, cellOffset;
8599cb35068SDave May     PetscBool          point_outside_domain = PETSC_FALSE;
860ccd2543fSMatthew G Knepley 
8619cb35068SDave May     /* check bounding box of domain */
8629cb35068SDave May     for (d = 0; d < dim; d++) {
8639371c9d4SSatish Balay       if (PetscRealPart(point[d]) < gmin[d]) {
8649371c9d4SSatish Balay         point_outside_domain = PETSC_TRUE;
8659371c9d4SSatish Balay         break;
8669371c9d4SSatish Balay       }
8679371c9d4SSatish Balay       if (PetscRealPart(point[d]) > gmax[d]) {
8689371c9d4SSatish Balay         point_outside_domain = PETSC_TRUE;
8699371c9d4SSatish Balay         break;
8709371c9d4SSatish Balay       }
8719cb35068SDave May     }
8729cb35068SDave May     if (point_outside_domain) {
873e9b685f5SMatthew G. Knepley       cells[p].rank  = 0;
874e9b685f5SMatthew G. Knepley       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
8759cb35068SDave May       terminating_query_type[0]++;
8769cb35068SDave May       continue;
8779cb35068SDave May     }
878ccd2543fSMatthew G Knepley 
879af74b616SDave May     /* check initial values in cells[].index - abort early if found */
880af74b616SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
881af74b616SDave May       c              = cells[p].index;
8823a93e3b7SToby Isaac       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
8839566063dSJacob Faibussowitsch       PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell));
884af74b616SDave May       if (cell >= 0) {
885af74b616SDave May         cells[p].rank  = 0;
886af74b616SDave May         cells[p].index = cell;
887af74b616SDave May         numFound++;
888af74b616SDave May       }
889af74b616SDave May     }
8909cb35068SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
8919cb35068SDave May       terminating_query_type[1]++;
8929cb35068SDave May       continue;
8939cb35068SDave May     }
894af74b616SDave May 
895953fc75cSMatthew G. Knepley     if (hash) {
896af74b616SDave May       PetscBool found_box;
897af74b616SDave May 
89863a3b9bcSJacob 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])));
899af74b616SDave May       /* allow for case that point is outside box - abort early */
900f5867de0SMatthew G. Knepley       PetscCall(PetscGridHashGetEnclosingBoxQuery(mesh->lbox, mesh->lbox->cellSection, 1, point, dbin, &bin, &found_box));
901af74b616SDave May       if (found_box) {
90263a3b9bcSJacob 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]));
903cafe43deSMatthew G. Knepley         /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
9049566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells));
9059566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset));
906cafe43deSMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
90763a3b9bcSJacob Faibussowitsch           if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "    Checking for point in cell %" PetscInt_FMT "\n", boxCells[c]));
9089566063dSJacob Faibussowitsch           PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell));
9093a93e3b7SToby Isaac           if (cell >= 0) {
91063a3b9bcSJacob Faibussowitsch             if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "      FOUND in cell %" PetscInt_FMT "\n", cell));
9113a93e3b7SToby Isaac             cells[p].rank  = 0;
9123a93e3b7SToby Isaac             cells[p].index = cell;
9133a93e3b7SToby Isaac             numFound++;
9149cb35068SDave May             terminating_query_type[2]++;
9153a93e3b7SToby Isaac             break;
916ccd2543fSMatthew G Knepley           }
9173a93e3b7SToby Isaac         }
918af74b616SDave May       }
919953fc75cSMatthew G. Knepley     } else {
920953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
921d8206211SMatthew G. Knepley         PetscInt idx;
922d8206211SMatthew G. Knepley 
923d8206211SMatthew G. Knepley         PetscCall(PetscFindInt(c, Nl, leaves, &idx));
924d8206211SMatthew G. Knepley         if (idx >= 0) continue;
9259566063dSJacob Faibussowitsch         PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell));
9263a93e3b7SToby Isaac         if (cell >= 0) {
9273a93e3b7SToby Isaac           cells[p].rank  = 0;
9283a93e3b7SToby Isaac           cells[p].index = cell;
9293a93e3b7SToby Isaac           numFound++;
9309cb35068SDave May           terminating_query_type[2]++;
9313a93e3b7SToby Isaac           break;
932953fc75cSMatthew G. Knepley         }
933953fc75cSMatthew G. Knepley       }
9343a93e3b7SToby Isaac     }
935ccd2543fSMatthew G Knepley   }
9369566063dSJacob Faibussowitsch   if (hash) PetscCall(ISRestoreIndices(mesh->lbox->cells, &boxCells));
93762a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
93862a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
93962a38674SMatthew G. Knepley       const PetscScalar *point = &a[p * bs];
940d92c4b9fSToby Isaac       PetscReal          cpoint[3], diff[3], best[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL}, dist, distMax = PETSC_MAX_REAL;
941d92c4b9fSToby Isaac       PetscInt           dbin[3] = {-1, -1, -1}, bin, cellOffset, d, bestc = -1;
94262a38674SMatthew G. Knepley 
943e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
9449566063dSJacob Faibussowitsch         PetscCall(PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin));
9459566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells));
9469566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset));
94762a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
9489566063dSJacob Faibussowitsch           PetscCall(DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint));
949b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
95062a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
95162a38674SMatthew G. Knepley           if (dist < distMax) {
952d92c4b9fSToby Isaac             for (d = 0; d < dim; ++d) best[d] = cpoint[d];
953d92c4b9fSToby Isaac             bestc   = boxCells[c];
95462a38674SMatthew G. Knepley             distMax = dist;
95562a38674SMatthew G. Knepley           }
95662a38674SMatthew G. Knepley         }
957d92c4b9fSToby Isaac         if (distMax < PETSC_MAX_REAL) {
958d92c4b9fSToby Isaac           ++numFound;
959d92c4b9fSToby Isaac           cells[p].rank  = 0;
960d92c4b9fSToby Isaac           cells[p].index = bestc;
961d92c4b9fSToby Isaac           for (d = 0; d < dim; ++d) a[p * bs + d] = best[d];
962d92c4b9fSToby Isaac         }
96362a38674SMatthew G. Knepley       }
96462a38674SMatthew G. Knepley     }
96562a38674SMatthew G. Knepley   }
96662a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
967cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
9682d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
9699566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numFound, &found));
9703a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
9713a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
972ad540459SPierre Jolivet         if (numFound < p) cells[numFound] = cells[p];
9733a93e3b7SToby Isaac         found[numFound++] = p;
9743a93e3b7SToby Isaac       }
9753a93e3b7SToby Isaac     }
9763a93e3b7SToby Isaac   }
9779566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(v, &a));
97848a46eb9SPierre Jolivet   if (!reuse) PetscCall(PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER));
9799566063dSJacob Faibussowitsch   PetscCall(PetscTime(&t1));
9809cb35068SDave May   if (hash) {
98163a3b9bcSJacob 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]));
9829cb35068SDave May   } else {
98363a3b9bcSJacob 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]));
9849cb35068SDave May   }
98563a3b9bcSJacob 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))));
9869566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_LocatePoints, 0, 0, 0, 0));
987ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
988ccd2543fSMatthew G Knepley }
989ccd2543fSMatthew G Knepley 
990741bfc07SMatthew G. Knepley /*@C
991741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
992741bfc07SMatthew G. Knepley 
993741bfc07SMatthew G. Knepley   Not collective
994741bfc07SMatthew G. Knepley 
9956b867d5aSJose E. Roman   Input/Output Parameter:
9966b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x
997741bfc07SMatthew G. Knepley 
9986b867d5aSJose E. Roman   Output Parameter:
9996b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
1000741bfc07SMatthew G. Knepley 
1001741bfc07SMatthew G. Knepley   Level: developer
1002741bfc07SMatthew G. Knepley 
1003db781477SPatrick Sanan .seealso: `DMPlexComputeProjection3Dto1D()`, `DMPlexComputeProjection3Dto2D()`
1004741bfc07SMatthew G. Knepley @*/
1005d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
1006d71ae5a4SJacob Faibussowitsch {
100717fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
100817fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
10098b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x * x + y * y), c = x / r, s = y / r;
101017fe8556SMatthew G. Knepley 
101117fe8556SMatthew G. Knepley   PetscFunctionBegin;
10129371c9d4SSatish Balay   R[0]      = c;
10139371c9d4SSatish Balay   R[1]      = -s;
10149371c9d4SSatish Balay   R[2]      = s;
10159371c9d4SSatish Balay   R[3]      = c;
101617fe8556SMatthew G. Knepley   coords[0] = 0.0;
10177f07f362SMatthew G. Knepley   coords[1] = r;
101817fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
101917fe8556SMatthew G. Knepley }
102017fe8556SMatthew G. Knepley 
1021741bfc07SMatthew G. Knepley /*@C
1022741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
102328dbe442SToby Isaac 
1024741bfc07SMatthew G. Knepley   Not collective
102528dbe442SToby Isaac 
10266b867d5aSJose E. Roman   Input/Output Parameter:
10276b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z
1028741bfc07SMatthew G. Knepley 
10296b867d5aSJose E. Roman   Output Parameter:
10306b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
1031741bfc07SMatthew G. Knepley 
1032741bfc07SMatthew 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
1033741bfc07SMatthew G. Knepley 
1034741bfc07SMatthew G. Knepley   Level: developer
1035741bfc07SMatthew G. Knepley 
1036db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto2D()`
1037741bfc07SMatthew G. Knepley @*/
1038d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
1039d71ae5a4SJacob Faibussowitsch {
104028dbe442SToby Isaac   PetscReal x    = PetscRealPart(coords[3] - coords[0]);
104128dbe442SToby Isaac   PetscReal y    = PetscRealPart(coords[4] - coords[1]);
104228dbe442SToby Isaac   PetscReal z    = PetscRealPart(coords[5] - coords[2]);
104328dbe442SToby Isaac   PetscReal r    = PetscSqrtReal(x * x + y * y + z * z);
104428dbe442SToby Isaac   PetscReal rinv = 1. / r;
104528dbe442SToby Isaac   PetscFunctionBegin;
104628dbe442SToby Isaac 
10479371c9d4SSatish Balay   x *= rinv;
10489371c9d4SSatish Balay   y *= rinv;
10499371c9d4SSatish Balay   z *= rinv;
105028dbe442SToby Isaac   if (x > 0.) {
105128dbe442SToby Isaac     PetscReal inv1pX = 1. / (1. + x);
105228dbe442SToby Isaac 
10539371c9d4SSatish Balay     R[0] = x;
10549371c9d4SSatish Balay     R[1] = -y;
10559371c9d4SSatish Balay     R[2] = -z;
10569371c9d4SSatish Balay     R[3] = y;
10579371c9d4SSatish Balay     R[4] = 1. - y * y * inv1pX;
10589371c9d4SSatish Balay     R[5] = -y * z * inv1pX;
10599371c9d4SSatish Balay     R[6] = z;
10609371c9d4SSatish Balay     R[7] = -y * z * inv1pX;
10619371c9d4SSatish Balay     R[8] = 1. - z * z * inv1pX;
10629371c9d4SSatish Balay   } else {
106328dbe442SToby Isaac     PetscReal inv1mX = 1. / (1. - x);
106428dbe442SToby Isaac 
10659371c9d4SSatish Balay     R[0] = x;
10669371c9d4SSatish Balay     R[1] = z;
10679371c9d4SSatish Balay     R[2] = y;
10689371c9d4SSatish Balay     R[3] = y;
10699371c9d4SSatish Balay     R[4] = -y * z * inv1mX;
10709371c9d4SSatish Balay     R[5] = 1. - y * y * inv1mX;
10719371c9d4SSatish Balay     R[6] = z;
10729371c9d4SSatish Balay     R[7] = 1. - z * z * inv1mX;
10739371c9d4SSatish Balay     R[8] = -y * z * inv1mX;
107428dbe442SToby Isaac   }
107528dbe442SToby Isaac   coords[0] = 0.0;
107628dbe442SToby Isaac   coords[1] = r;
107728dbe442SToby Isaac   PetscFunctionReturn(0);
107828dbe442SToby Isaac }
107928dbe442SToby Isaac 
1080741bfc07SMatthew G. Knepley /*@
1081c871b86eSJed Brown   DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the
1082c871b86eSJed Brown     plane.  The normal is defined by positive orientation of the first 3 points.
1083741bfc07SMatthew G. Knepley 
1084741bfc07SMatthew G. Knepley   Not collective
1085741bfc07SMatthew G. Knepley 
1086741bfc07SMatthew G. Knepley   Input Parameter:
10876b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points)
1088741bfc07SMatthew G. Knepley 
10896b867d5aSJose E. Roman   Input/Output Parameter:
10906b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first
10916b867d5aSJose E. Roman            2*coordSize/3 entries contain interlaced 2D points, with the rest undefined
10926b867d5aSJose E. Roman 
10936b867d5aSJose E. Roman   Output Parameter:
10946b867d5aSJose 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.
1095741bfc07SMatthew G. Knepley 
1096741bfc07SMatthew G. Knepley   Level: developer
1097741bfc07SMatthew G. Knepley 
1098db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto1D()`
1099741bfc07SMatthew G. Knepley @*/
1100d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
1101d71ae5a4SJacob Faibussowitsch {
1102c871b86eSJed Brown   PetscReal      x1[3], x2[3], n[3], c[3], norm;
1103ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1104c871b86eSJed Brown   PetscInt       d, p;
1105ccd2543fSMatthew G Knepley 
1106ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1107ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
1108ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
11091ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1 * dim + d] - coords[0 * dim + d]);
11101ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2 * dim + d] - coords[0 * dim + d]);
1111ccd2543fSMatthew G Knepley   }
1112c871b86eSJed Brown   // n = x1 \otimes x2
1113ccd2543fSMatthew G Knepley   n[0] = x1[1] * x2[2] - x1[2] * x2[1];
1114ccd2543fSMatthew G Knepley   n[1] = x1[2] * x2[0] - x1[0] * x2[2];
1115ccd2543fSMatthew G Knepley   n[2] = x1[0] * x2[1] - x1[1] * x2[0];
11168b49ba18SBarry Smith   norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
1117c871b86eSJed Brown   for (d = 0; d < dim; d++) n[d] /= norm;
1118c871b86eSJed Brown   norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]);
1119c871b86eSJed Brown   for (d = 0; d < dim; d++) x1[d] /= norm;
1120c871b86eSJed Brown   // x2 = n \otimes x1
1121c871b86eSJed Brown   x2[0] = n[1] * x1[2] - n[2] * x1[1];
1122c871b86eSJed Brown   x2[1] = n[2] * x1[0] - n[0] * x1[2];
1123c871b86eSJed Brown   x2[2] = n[0] * x1[1] - n[1] * x1[0];
1124c871b86eSJed Brown   for (d = 0; d < dim; d++) {
1125c871b86eSJed Brown     R[d * dim + 0] = x1[d];
1126c871b86eSJed Brown     R[d * dim + 1] = x2[d];
1127c871b86eSJed Brown     R[d * dim + 2] = n[d];
1128c871b86eSJed Brown     c[d]           = PetscRealPart(coords[0 * dim + d]);
112973868372SMatthew G. Knepley   }
1130c871b86eSJed Brown   for (p = 0; p < coordSize / dim; p++) {
1131c871b86eSJed Brown     PetscReal y[3];
1132c871b86eSJed Brown     for (d = 0; d < dim; d++) y[d] = PetscRealPart(coords[p * dim + d]) - c[d];
1133c871b86eSJed 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];
11347f07f362SMatthew G. Knepley   }
1135ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1136ccd2543fSMatthew G Knepley }
1137ccd2543fSMatthew G Knepley 
1138d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
1139d71ae5a4SJacob Faibussowitsch {
1140834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
1141834e62ceSMatthew G. Knepley 
1142834e62ceSMatthew G. Knepley    |  1  1  1 |
1143834e62ceSMatthew G. Knepley    | x0 x1 x2 |
1144834e62ceSMatthew G. Knepley    | y0 y1 y2 |
1145834e62ceSMatthew G. Knepley 
1146834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
1147834e62ceSMatthew G. Knepley 
1148834e62ceSMatthew G. Knepley    | x1 x2 |
1149834e62ceSMatthew G. Knepley    | y1 y2 |
1150834e62ceSMatthew G. Knepley   */
1151834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
1152834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
1153834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
11549371c9d4SSatish Balay   M[0] = x1;
11559371c9d4SSatish Balay   M[1] = x2;
11569371c9d4SSatish Balay   M[2] = y1;
11579371c9d4SSatish Balay   M[3] = y2;
1158923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
1159834e62ceSMatthew G. Knepley   *vol = 0.5 * detM;
11603bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
1161834e62ceSMatthew G. Knepley }
1162834e62ceSMatthew G. Knepley 
1163d71ae5a4SJacob Faibussowitsch PETSC_UNUSED static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
1164d71ae5a4SJacob Faibussowitsch {
1165834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
1166834e62ceSMatthew G. Knepley 
1167834e62ceSMatthew G. Knepley    |  1  1  1  1 |
1168834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
1169834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
1170834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
1171834e62ceSMatthew G. Knepley 
1172834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
1173834e62ceSMatthew G. Knepley 
1174834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1175834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1176834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1177834e62ceSMatthew G. Knepley   */
1178834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2];
1179834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2];
1180834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
11810a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1182834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
11839371c9d4SSatish Balay   M[0] = x1;
11849371c9d4SSatish Balay   M[1] = x2;
11859371c9d4SSatish Balay   M[2] = x3;
11869371c9d4SSatish Balay   M[3] = y1;
11879371c9d4SSatish Balay   M[4] = y2;
11889371c9d4SSatish Balay   M[5] = y3;
11899371c9d4SSatish Balay   M[6] = z1;
11909371c9d4SSatish Balay   M[7] = z2;
11919371c9d4SSatish Balay   M[8] = z3;
1192923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
11930a3da2c2SToby Isaac   *vol = -onesixth * detM;
11943bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1195834e62ceSMatthew G. Knepley }
1196834e62ceSMatthew G. Knepley 
1197d71ae5a4SJacob Faibussowitsch static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
1198d71ae5a4SJacob Faibussowitsch {
11990a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1200923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
12010a3da2c2SToby Isaac   *vol *= -onesixth;
12020ec8681fSMatthew G. Knepley }
12030ec8681fSMatthew G. Knepley 
1204d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1205d71ae5a4SJacob Faibussowitsch {
1206cb92db44SToby Isaac   PetscSection       coordSection;
1207cb92db44SToby Isaac   Vec                coordinates;
1208cb92db44SToby Isaac   const PetscScalar *coords;
1209cb92db44SToby Isaac   PetscInt           dim, d, off;
1210cb92db44SToby Isaac 
1211cb92db44SToby Isaac   PetscFunctionBegin;
12129566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
12139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
12149566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(coordSection, e, &dim));
1215cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
12169566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(coordSection, e, &off));
12179566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
12189371c9d4SSatish Balay   if (v0) {
12199371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);
12209371c9d4SSatish Balay   }
12219566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
1222cb92db44SToby Isaac   *detJ = 1.;
1223cb92db44SToby Isaac   if (J) {
1224cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1225cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1226cb92db44SToby Isaac     if (invJ) {
1227cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1228cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1229cb92db44SToby Isaac     }
1230cb92db44SToby Isaac   }
1231cb92db44SToby Isaac   PetscFunctionReturn(0);
1232cb92db44SToby Isaac }
1233cb92db44SToby Isaac 
12346858538eSMatthew G. Knepley /*@C
12356858538eSMatthew G. Knepley   DMPlexGetCellCoordinates - Get coordinates for a cell, taking into account periodicity
12366858538eSMatthew G. Knepley 
12376858538eSMatthew G. Knepley   Not collective
12386858538eSMatthew G. Knepley 
12396858538eSMatthew G. Knepley   Input Parameters:
12406858538eSMatthew G. Knepley + dm   - The DM
12416858538eSMatthew G. Knepley - cell - The cell number
12426858538eSMatthew G. Knepley 
12436858538eSMatthew G. Knepley   Output Parameters:
12446858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
12456858538eSMatthew G. Knepley . Nc     - The number of coordinates
12466858538eSMatthew G. Knepley . array  - The coordinate array
12476858538eSMatthew G. Knepley - coords - The cell coordinates
12486858538eSMatthew G. Knepley 
12496858538eSMatthew G. Knepley   Level: developer
12506858538eSMatthew G. Knepley 
12516858538eSMatthew G. Knepley .seealso: DMPlexRestoreCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal()
12526858538eSMatthew G. Knepley @*/
1253d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[])
1254d71ae5a4SJacob Faibussowitsch {
12556858538eSMatthew G. Knepley   DM                 cdm;
12566858538eSMatthew G. Knepley   Vec                coordinates;
12576858538eSMatthew G. Knepley   PetscSection       cs;
12586858538eSMatthew G. Knepley   const PetscScalar *ccoords;
12596858538eSMatthew G. Knepley   PetscInt           pStart, pEnd;
12606858538eSMatthew G. Knepley 
12616858538eSMatthew G. Knepley   PetscFunctionBeginHot;
12626858538eSMatthew G. Knepley   *isDG   = PETSC_FALSE;
12636858538eSMatthew G. Knepley   *Nc     = 0;
12646858538eSMatthew G. Knepley   *array  = NULL;
12656858538eSMatthew G. Knepley   *coords = NULL;
12666858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12676858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateSection(dm, &cs));
12686858538eSMatthew G. Knepley   if (!cs) goto cg;
12696858538eSMatthew G. Knepley   /* Check that the cell exists in the cellwise section */
12706858538eSMatthew G. Knepley   PetscCall(PetscSectionGetChart(cs, &pStart, &pEnd));
12716858538eSMatthew G. Knepley   if (cell < pStart || cell >= pEnd) goto cg;
12726858538eSMatthew G. Knepley   /* Check for cellwise coordinates for this cell */
12736858538eSMatthew G. Knepley   PetscCall(PetscSectionGetDof(cs, cell, Nc));
12746858538eSMatthew G. Knepley   if (!*Nc) goto cg;
12756858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12766858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocalNoncollective(dm, &coordinates));
12776858538eSMatthew G. Knepley   if (!coordinates) goto cg;
12786858538eSMatthew G. Knepley   /* Get cellwise coordinates */
12796858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dm, &cdm));
12806858538eSMatthew G. Knepley   PetscCall(VecGetArrayRead(coordinates, array));
12816858538eSMatthew G. Knepley   PetscCall(DMPlexPointLocalRead(cdm, cell, *array, &ccoords));
12826858538eSMatthew G. Knepley   PetscCall(DMGetWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
12836858538eSMatthew G. Knepley   PetscCall(PetscArraycpy(*coords, ccoords, *Nc));
12846858538eSMatthew G. Knepley   PetscCall(VecRestoreArrayRead(coordinates, array));
12856858538eSMatthew G. Knepley   *isDG = PETSC_TRUE;
12866858538eSMatthew G. Knepley   PetscFunctionReturn(0);
12876858538eSMatthew G. Knepley cg:
12886858538eSMatthew G. Knepley   /* Use continuous coordinates */
12896858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
12906858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &cs));
12916858538eSMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
12926858538eSMatthew G. Knepley   PetscCall(DMPlexVecGetClosure(cdm, cs, coordinates, cell, Nc, coords));
12936858538eSMatthew G. Knepley   PetscFunctionReturn(0);
12946858538eSMatthew G. Knepley }
12956858538eSMatthew G. Knepley 
12966858538eSMatthew G. Knepley /*@C
12976858538eSMatthew G. Knepley   DMPlexRestoreCellCoordinates - Get coordinates for a cell, taking into account periodicity
12986858538eSMatthew G. Knepley 
12996858538eSMatthew G. Knepley   Not collective
13006858538eSMatthew G. Knepley 
13016858538eSMatthew G. Knepley   Input Parameters:
13026858538eSMatthew G. Knepley + dm   - The DM
13036858538eSMatthew G. Knepley - cell - The cell number
13046858538eSMatthew G. Knepley 
13056858538eSMatthew G. Knepley   Output Parameters:
13066858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
13076858538eSMatthew G. Knepley . Nc     - The number of coordinates
13086858538eSMatthew G. Knepley . array  - The coordinate array
13096858538eSMatthew G. Knepley - coords - The cell coordinates
13106858538eSMatthew G. Knepley 
13116858538eSMatthew G. Knepley   Level: developer
13126858538eSMatthew G. Knepley 
13136858538eSMatthew G. Knepley .seealso: DMPlexGetCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal()
13146858538eSMatthew G. Knepley @*/
1315d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexRestoreCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[])
1316d71ae5a4SJacob Faibussowitsch {
13176858538eSMatthew G. Knepley   DM           cdm;
13186858538eSMatthew G. Knepley   PetscSection cs;
13196858538eSMatthew G. Knepley   Vec          coordinates;
13206858538eSMatthew G. Knepley 
13216858538eSMatthew G. Knepley   PetscFunctionBeginHot;
13226858538eSMatthew G. Knepley   if (*isDG) {
13236858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinateDM(dm, &cdm));
13246858538eSMatthew G. Knepley     PetscCall(DMRestoreWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
13256858538eSMatthew G. Knepley   } else {
13266858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
13276858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateSection(dm, &cs));
13286858538eSMatthew G. Knepley     PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
13296858538eSMatthew G. Knepley     PetscCall(DMPlexVecRestoreClosure(cdm, cs, coordinates, cell, Nc, (PetscScalar **)coords));
13306858538eSMatthew G. Knepley   }
13316858538eSMatthew G. Knepley   PetscFunctionReturn(0);
13326858538eSMatthew G. Knepley }
13336858538eSMatthew G. Knepley 
1334d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1335d71ae5a4SJacob Faibussowitsch {
13366858538eSMatthew G. Knepley   const PetscScalar *array;
1337a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
13386858538eSMatthew G. Knepley   PetscInt           numCoords, d;
13396858538eSMatthew G. Knepley   PetscBool          isDG;
134017fe8556SMatthew G. Knepley 
134117fe8556SMatthew G. Knepley   PetscFunctionBegin;
13426858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
134308401ef6SPierre Jolivet   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
13447f07f362SMatthew G. Knepley   *detJ = 0.0;
134528dbe442SToby Isaac   if (numCoords == 6) {
134628dbe442SToby Isaac     const PetscInt dim = 3;
134728dbe442SToby Isaac     PetscReal      R[9], J0;
134828dbe442SToby Isaac 
13499371c9d4SSatish Balay     if (v0) {
13509371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13519371c9d4SSatish Balay     }
13529566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto1D(coords, R));
135328dbe442SToby Isaac     if (J) {
135428dbe442SToby Isaac       J0   = 0.5 * PetscRealPart(coords[1]);
13559371c9d4SSatish Balay       J[0] = R[0] * J0;
13569371c9d4SSatish Balay       J[1] = R[1];
13579371c9d4SSatish Balay       J[2] = R[2];
13589371c9d4SSatish Balay       J[3] = R[3] * J0;
13599371c9d4SSatish Balay       J[4] = R[4];
13609371c9d4SSatish Balay       J[5] = R[5];
13619371c9d4SSatish Balay       J[6] = R[6] * J0;
13629371c9d4SSatish Balay       J[7] = R[7];
13639371c9d4SSatish Balay       J[8] = R[8];
136428dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
1365ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
1366adac9986SMatthew G. Knepley     }
136728dbe442SToby Isaac   } else if (numCoords == 4) {
13687f07f362SMatthew G. Knepley     const PetscInt dim = 2;
13697f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
13707f07f362SMatthew G. Knepley 
13719371c9d4SSatish Balay     if (v0) {
13729371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13739371c9d4SSatish Balay     }
13749566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection2Dto1D(coords, R));
137517fe8556SMatthew G. Knepley     if (J) {
13767f07f362SMatthew G. Knepley       J0   = 0.5 * PetscRealPart(coords[1]);
13779371c9d4SSatish Balay       J[0] = R[0] * J0;
13789371c9d4SSatish Balay       J[1] = R[1];
13799371c9d4SSatish Balay       J[2] = R[2] * J0;
13809371c9d4SSatish Balay       J[3] = R[3];
1381923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1382ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
1383adac9986SMatthew G. Knepley     }
13847f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
13857f07f362SMatthew G. Knepley     const PetscInt dim = 1;
13867f07f362SMatthew G. Knepley 
13879371c9d4SSatish Balay     if (v0) {
13889371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13899371c9d4SSatish Balay     }
13907f07f362SMatthew G. Knepley     if (J) {
13917f07f362SMatthew G. Knepley       J[0]  = 0.5 * (PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
139217fe8556SMatthew G. Knepley       *detJ = J[0];
13939566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(2.0));
13949371c9d4SSatish Balay       if (invJ) {
13959371c9d4SSatish Balay         invJ[0] = 1.0 / J[0];
13969371c9d4SSatish Balay         PetscCall(PetscLogFlops(1.0));
13979371c9d4SSatish Balay       }
1398adac9986SMatthew G. Knepley     }
13996858538eSMatthew 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);
14006858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
140117fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
140217fe8556SMatthew G. Knepley }
140317fe8556SMatthew G. Knepley 
1404d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1405d71ae5a4SJacob Faibussowitsch {
14066858538eSMatthew G. Knepley   const PetscScalar *array;
1407a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
14086858538eSMatthew G. Knepley   PetscInt           numCoords, d;
14096858538eSMatthew G. Knepley   PetscBool          isDG;
1410ccd2543fSMatthew G Knepley 
1411ccd2543fSMatthew G Knepley   PetscFunctionBegin;
14126858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14136858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
14147f07f362SMatthew G. Knepley   *detJ = 0.0;
1415ccd2543fSMatthew G Knepley   if (numCoords == 9) {
14167f07f362SMatthew G. Knepley     const PetscInt dim = 3;
14177f07f362SMatthew 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};
14187f07f362SMatthew G. Knepley 
14199371c9d4SSatish Balay     if (v0) {
14209371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
14219371c9d4SSatish Balay     }
14229566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
14237f07f362SMatthew G. Knepley     if (J) {
1424b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1425b7ad821dSMatthew G. Knepley 
1426b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1427ad540459SPierre 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]));
14287f07f362SMatthew G. Knepley       }
14299566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1430923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
14317f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
14326858538eSMatthew G. Knepley         for (PetscInt f = 0; f < dim; f++) {
14337f07f362SMatthew G. Knepley           J[d * dim + f] = 0.0;
1434ad540459SPierre Jolivet           for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f];
14357f07f362SMatthew G. Knepley         }
14367f07f362SMatthew G. Knepley       }
14379566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
14387f07f362SMatthew G. Knepley     }
1439ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
14407f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
14417f07f362SMatthew G. Knepley     const PetscInt dim = 2;
14427f07f362SMatthew G. Knepley 
14439371c9d4SSatish Balay     if (v0) {
14449371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
14459371c9d4SSatish Balay     }
1446ccd2543fSMatthew G Knepley     if (J) {
1447ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1448ad540459SPierre 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]));
1449ccd2543fSMatthew G Knepley       }
14509566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1451923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1452ccd2543fSMatthew G Knepley     }
1453ad540459SPierre Jolivet     if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
145463a3b9bcSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %" PetscInt_FMT " != 6 or 9", numCoords);
14556858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1456ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1457ccd2543fSMatthew G Knepley }
1458ccd2543fSMatthew G Knepley 
1459d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1460d71ae5a4SJacob Faibussowitsch {
14616858538eSMatthew G. Knepley   const PetscScalar *array;
1462a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
14636858538eSMatthew G. Knepley   PetscInt           numCoords, d;
14646858538eSMatthew G. Knepley   PetscBool          isDG;
1465ccd2543fSMatthew G Knepley 
1466ccd2543fSMatthew G Knepley   PetscFunctionBegin;
14676858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14686858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1469dfccc68fSToby Isaac   if (!Nq) {
1470412e9a14SMatthew G. Knepley     PetscInt vorder[4] = {0, 1, 2, 3};
1471412e9a14SMatthew G. Knepley 
14729371c9d4SSatish Balay     if (isTensor) {
14739371c9d4SSatish Balay       vorder[2] = 3;
14749371c9d4SSatish Balay       vorder[3] = 2;
14759371c9d4SSatish Balay     }
14767f07f362SMatthew G. Knepley     *detJ = 0.0;
147799dec3a6SMatthew G. Knepley     if (numCoords == 12) {
147899dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
147999dec3a6SMatthew 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};
148099dec3a6SMatthew G. Knepley 
14819371c9d4SSatish Balay       if (v) {
14829371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
14839371c9d4SSatish Balay       }
14849566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
148599dec3a6SMatthew G. Knepley       if (J) {
148699dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
148799dec3a6SMatthew G. Knepley 
148899dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
1489412e9a14SMatthew G. Knepley           J0[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * pdim + d]) - PetscRealPart(coords[vorder[0] * pdim + d]));
1490412e9a14SMatthew G. Knepley           J0[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[2] * pdim + d]) - PetscRealPart(coords[vorder[1] * pdim + d]));
149199dec3a6SMatthew G. Knepley         }
14929566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1493923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
149499dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
14956858538eSMatthew G. Knepley           for (PetscInt f = 0; f < dim; f++) {
149699dec3a6SMatthew G. Knepley             J[d * dim + f] = 0.0;
1497ad540459SPierre Jolivet             for (PetscInt g = 0; g < dim; g++) J[d * dim + f] += R[d * dim + g] * J0[g * dim + f];
149899dec3a6SMatthew G. Knepley           }
149999dec3a6SMatthew G. Knepley         }
15009566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(18.0));
150199dec3a6SMatthew G. Knepley       }
1502ad540459SPierre Jolivet       if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
150371f58de1SToby Isaac     } else if (numCoords == 8) {
150499dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
150599dec3a6SMatthew G. Knepley 
15069371c9d4SSatish Balay       if (v) {
15079371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
15089371c9d4SSatish Balay       }
1509ccd2543fSMatthew G Knepley       if (J) {
1510ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
1511412e9a14SMatthew G. Knepley           J[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1512412e9a14SMatthew G. Knepley           J[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[3] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1513ccd2543fSMatthew G Knepley         }
15149566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1515923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1516ccd2543fSMatthew G Knepley       }
1517ad540459SPierre Jolivet       if (invJ) DMPlex_Invert2D_Internal(invJ, J, *detJ);
151863a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1519dfccc68fSToby Isaac   } else {
1520dfccc68fSToby Isaac     const PetscInt Nv         = 4;
1521dfccc68fSToby Isaac     const PetscInt dimR       = 2;
1522412e9a14SMatthew G. Knepley     PetscInt       zToPlex[4] = {0, 1, 3, 2};
1523dfccc68fSToby Isaac     PetscReal      zOrder[12];
1524dfccc68fSToby Isaac     PetscReal      zCoeff[12];
1525dfccc68fSToby Isaac     PetscInt       i, j, k, l, dim;
1526dfccc68fSToby Isaac 
15279371c9d4SSatish Balay     if (isTensor) {
15289371c9d4SSatish Balay       zToPlex[2] = 2;
15299371c9d4SSatish Balay       zToPlex[3] = 3;
15309371c9d4SSatish Balay     }
1531dfccc68fSToby Isaac     if (numCoords == 12) {
1532dfccc68fSToby Isaac       dim = 3;
1533dfccc68fSToby Isaac     } else if (numCoords == 8) {
1534dfccc68fSToby Isaac       dim = 2;
153563a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1536dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1537dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1538dfccc68fSToby Isaac 
1539ad540459SPierre Jolivet       for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1540dfccc68fSToby Isaac     }
1541dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
15422df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta):
15432df84da0SMatthew G. Knepley            \phi^0 = (1 - xi - eta + xi eta) --> 1      = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3)
15442df84da0SMatthew G. Knepley            \phi^1 = (1 + xi - eta - xi eta) --> xi     = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3)
15452df84da0SMatthew G. Knepley            \phi^2 = (1 - xi + eta - xi eta) --> eta    = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3)
15462df84da0SMatthew G. Knepley            \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3)
15472df84da0SMatthew G. Knepley       */
1548dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1549dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1550dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1551dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1552dfccc68fSToby Isaac     }
1553dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1554dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1555dfccc68fSToby Isaac 
1556dfccc68fSToby Isaac       if (v) {
1557dfccc68fSToby Isaac         PetscReal extPoint[4];
1558dfccc68fSToby Isaac 
1559dfccc68fSToby Isaac         extPoint[0] = 1.;
1560dfccc68fSToby Isaac         extPoint[1] = xi;
1561dfccc68fSToby Isaac         extPoint[2] = eta;
1562dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1563dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1564dfccc68fSToby Isaac           PetscReal val = 0.;
1565dfccc68fSToby Isaac 
1566ad540459SPierre Jolivet           for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j];
1567dfccc68fSToby Isaac           v[i * dim + j] = val;
1568dfccc68fSToby Isaac         }
1569dfccc68fSToby Isaac       }
1570dfccc68fSToby Isaac       if (J) {
1571dfccc68fSToby Isaac         PetscReal extJ[8];
1572dfccc68fSToby Isaac 
1573dfccc68fSToby Isaac         extJ[0] = 0.;
1574dfccc68fSToby Isaac         extJ[1] = 0.;
1575dfccc68fSToby Isaac         extJ[2] = 1.;
1576dfccc68fSToby Isaac         extJ[3] = 0.;
1577dfccc68fSToby Isaac         extJ[4] = 0.;
1578dfccc68fSToby Isaac         extJ[5] = 1.;
1579dfccc68fSToby Isaac         extJ[6] = eta;
1580dfccc68fSToby Isaac         extJ[7] = xi;
1581dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1582dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1583dfccc68fSToby Isaac             PetscReal val = 0.;
1584dfccc68fSToby Isaac 
1585ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1586dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1587dfccc68fSToby Isaac           }
1588dfccc68fSToby Isaac         }
1589dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1590dfccc68fSToby Isaac           PetscReal  x, y, z;
1591dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1592dfccc68fSToby Isaac           PetscReal  norm;
1593dfccc68fSToby Isaac 
1594dfccc68fSToby Isaac           x     = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1595dfccc68fSToby Isaac           y     = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1596dfccc68fSToby Isaac           z     = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1597dfccc68fSToby Isaac           norm  = PetscSqrtReal(x * x + y * y + z * z);
1598dfccc68fSToby Isaac           iJ[2] = x / norm;
1599dfccc68fSToby Isaac           iJ[5] = y / norm;
1600dfccc68fSToby Isaac           iJ[8] = z / norm;
1601dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1602ad540459SPierre Jolivet           if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1603dfccc68fSToby Isaac         } else {
1604dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1605ad540459SPierre Jolivet           if (invJ) DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1606dfccc68fSToby Isaac         }
1607dfccc68fSToby Isaac       }
1608dfccc68fSToby Isaac     }
1609dfccc68fSToby Isaac   }
16106858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1611ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1612ccd2543fSMatthew G Knepley }
1613ccd2543fSMatthew G Knepley 
1614d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1615d71ae5a4SJacob Faibussowitsch {
16166858538eSMatthew G. Knepley   const PetscScalar *array;
1617a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1618ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
16196858538eSMatthew G. Knepley   PetscInt           numCoords, d;
16206858538eSMatthew G. Knepley   PetscBool          isDG;
1621ccd2543fSMatthew G Knepley 
1622ccd2543fSMatthew G Knepley   PetscFunctionBegin;
16236858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16246858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
16257f07f362SMatthew G. Knepley   *detJ = 0.0;
16269371c9d4SSatish Balay   if (v0) {
16279371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
16289371c9d4SSatish Balay   }
1629ccd2543fSMatthew G Knepley   if (J) {
1630ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1631f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1632f0df753eSMatthew G. Knepley       J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1633f0df753eSMatthew G. Knepley       J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1634f0df753eSMatthew G. Knepley       J[d * dim + 2] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1635ccd2543fSMatthew G Knepley     }
16369566063dSJacob Faibussowitsch     PetscCall(PetscLogFlops(18.0));
1637923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1638ccd2543fSMatthew G Knepley   }
1639ad540459SPierre Jolivet   if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
16406858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1641ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1642ccd2543fSMatthew G Knepley }
1643ccd2543fSMatthew G Knepley 
1644d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1645d71ae5a4SJacob Faibussowitsch {
16466858538eSMatthew G. Knepley   const PetscScalar *array;
1647a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1648ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
16496858538eSMatthew G. Knepley   PetscInt           numCoords, d;
16506858538eSMatthew G. Knepley   PetscBool          isDG;
1651ccd2543fSMatthew G Knepley 
1652ccd2543fSMatthew G Knepley   PetscFunctionBegin;
16536858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16546858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1655dfccc68fSToby Isaac   if (!Nq) {
16567f07f362SMatthew G. Knepley     *detJ = 0.0;
16579371c9d4SSatish Balay     if (v) {
16589371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
16599371c9d4SSatish Balay     }
1660ccd2543fSMatthew G Knepley     if (J) {
1661ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1662f0df753eSMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1663f0df753eSMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1664f0df753eSMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1665ccd2543fSMatthew G Knepley       }
16669566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
1667923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1668ccd2543fSMatthew G Knepley     }
1669ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
1670dfccc68fSToby Isaac   } else {
1671dfccc68fSToby Isaac     const PetscInt Nv         = 8;
1672dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1673dfccc68fSToby Isaac     const PetscInt dim        = 3;
1674dfccc68fSToby Isaac     const PetscInt dimR       = 3;
1675dfccc68fSToby Isaac     PetscReal      zOrder[24];
1676dfccc68fSToby Isaac     PetscReal      zCoeff[24];
1677dfccc68fSToby Isaac     PetscInt       i, j, k, l;
1678dfccc68fSToby Isaac 
1679dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1680dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1681dfccc68fSToby Isaac 
1682ad540459SPierre Jolivet       for (j = 0; j < dim; j++) zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1683dfccc68fSToby Isaac     }
1684dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1685dfccc68fSToby 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]);
1686dfccc68fSToby 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]);
1687dfccc68fSToby 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]);
1688dfccc68fSToby 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]);
1689dfccc68fSToby 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]);
1690dfccc68fSToby 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]);
1691dfccc68fSToby 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]);
1692dfccc68fSToby 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]);
1693dfccc68fSToby Isaac     }
1694dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1695dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1696dfccc68fSToby Isaac 
1697dfccc68fSToby Isaac       if (v) {
169891d2b7ceSToby Isaac         PetscReal extPoint[8];
1699dfccc68fSToby Isaac 
1700dfccc68fSToby Isaac         extPoint[0] = 1.;
1701dfccc68fSToby Isaac         extPoint[1] = xi;
1702dfccc68fSToby Isaac         extPoint[2] = eta;
1703dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1704dfccc68fSToby Isaac         extPoint[4] = theta;
1705dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1706dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1707dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1708dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1709dfccc68fSToby Isaac           PetscReal val = 0.;
1710dfccc68fSToby Isaac 
1711ad540459SPierre Jolivet           for (k = 0; k < Nv; k++) val += extPoint[k] * zCoeff[dim * k + j];
1712dfccc68fSToby Isaac           v[i * dim + j] = val;
1713dfccc68fSToby Isaac         }
1714dfccc68fSToby Isaac       }
1715dfccc68fSToby Isaac       if (J) {
1716dfccc68fSToby Isaac         PetscReal extJ[24];
1717dfccc68fSToby Isaac 
17189371c9d4SSatish Balay         extJ[0]  = 0.;
17199371c9d4SSatish Balay         extJ[1]  = 0.;
17209371c9d4SSatish Balay         extJ[2]  = 0.;
17219371c9d4SSatish Balay         extJ[3]  = 1.;
17229371c9d4SSatish Balay         extJ[4]  = 0.;
17239371c9d4SSatish Balay         extJ[5]  = 0.;
17249371c9d4SSatish Balay         extJ[6]  = 0.;
17259371c9d4SSatish Balay         extJ[7]  = 1.;
17269371c9d4SSatish Balay         extJ[8]  = 0.;
17279371c9d4SSatish Balay         extJ[9]  = eta;
17289371c9d4SSatish Balay         extJ[10] = xi;
17299371c9d4SSatish Balay         extJ[11] = 0.;
17309371c9d4SSatish Balay         extJ[12] = 0.;
17319371c9d4SSatish Balay         extJ[13] = 0.;
17329371c9d4SSatish Balay         extJ[14] = 1.;
17339371c9d4SSatish Balay         extJ[15] = theta;
17349371c9d4SSatish Balay         extJ[16] = 0.;
17359371c9d4SSatish Balay         extJ[17] = xi;
17369371c9d4SSatish Balay         extJ[18] = 0.;
17379371c9d4SSatish Balay         extJ[19] = theta;
17389371c9d4SSatish Balay         extJ[20] = eta;
17399371c9d4SSatish Balay         extJ[21] = theta * eta;
17409371c9d4SSatish Balay         extJ[22] = theta * xi;
17419371c9d4SSatish Balay         extJ[23] = eta * xi;
1742dfccc68fSToby Isaac 
1743dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1744dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1745dfccc68fSToby Isaac             PetscReal val = 0.;
1746dfccc68fSToby Isaac 
1747ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1748dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1749dfccc68fSToby Isaac           }
1750dfccc68fSToby Isaac         }
1751dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1752ad540459SPierre Jolivet         if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
1753dfccc68fSToby Isaac       }
1754dfccc68fSToby Isaac     }
1755dfccc68fSToby Isaac   }
17566858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1757ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1758ccd2543fSMatthew G Knepley }
1759ccd2543fSMatthew G Knepley 
1760d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1761d71ae5a4SJacob Faibussowitsch {
17626858538eSMatthew G. Knepley   const PetscScalar *array;
17632df84da0SMatthew G. Knepley   PetscScalar       *coords = NULL;
17642df84da0SMatthew G. Knepley   const PetscInt     dim    = 3;
17656858538eSMatthew G. Knepley   PetscInt           numCoords, d;
17666858538eSMatthew G. Knepley   PetscBool          isDG;
17672df84da0SMatthew G. Knepley 
17682df84da0SMatthew G. Knepley   PetscFunctionBegin;
17696858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
17706858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
17712df84da0SMatthew G. Knepley   if (!Nq) {
17722df84da0SMatthew G. Knepley     /* Assume that the map to the reference is affine */
17732df84da0SMatthew G. Knepley     *detJ = 0.0;
17749371c9d4SSatish Balay     if (v) {
17759371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
17769371c9d4SSatish Balay     }
17772df84da0SMatthew G. Knepley     if (J) {
17782df84da0SMatthew G. Knepley       for (d = 0; d < dim; d++) {
17792df84da0SMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17802df84da0SMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17812df84da0SMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17822df84da0SMatthew G. Knepley       }
17839566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
17842df84da0SMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
17852df84da0SMatthew G. Knepley     }
1786ad540459SPierre Jolivet     if (invJ) DMPlex_Invert3D_Internal(invJ, J, *detJ);
17872df84da0SMatthew G. Knepley   } else {
17882df84da0SMatthew G. Knepley     const PetscInt dim  = 3;
17892df84da0SMatthew G. Knepley     const PetscInt dimR = 3;
17902df84da0SMatthew G. Knepley     const PetscInt Nv   = 6;
17912df84da0SMatthew G. Knepley     PetscReal      verts[18];
17922df84da0SMatthew G. Knepley     PetscReal      coeff[18];
17932df84da0SMatthew G. Knepley     PetscInt       i, j, k, l;
17942df84da0SMatthew G. Knepley 
17959371c9d4SSatish Balay     for (i = 0; i < Nv; ++i)
17969371c9d4SSatish Balay       for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]);
17972df84da0SMatthew G. Knepley     for (j = 0; j < dim; ++j) {
17982df84da0SMatthew G. Knepley       /* Check for triangle,
17992df84da0SMatthew 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)
18002df84da0SMatthew 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)
18012df84da0SMatthew G. Knepley            phi^2 =  1/2 (1 + eta)   chi^2 = delta(-1,  1)
18022df84da0SMatthew G. Knepley 
18032df84da0SMatthew G. Knepley            phi^0 + phi^1 + phi^2 = 1    coef_1   = 1/2 (         chi^1 + chi^2)
18042df84da0SMatthew G. Knepley           -phi^0 + phi^1 - phi^2 = xi   coef_xi  = 1/2 (-chi^0 + chi^1)
18052df84da0SMatthew G. Knepley           -phi^0 - phi^1 + phi^2 = eta  coef_eta = 1/2 (-chi^0         + chi^2)
18062df84da0SMatthew G. Knepley 
18072df84da0SMatthew G. Knepley           < chi_0 chi_1 chi_2> A /  1  1  1 \ / phi_0 \   <chi> I <phi>^T  so we need the inverse transpose
18082df84da0SMatthew G. Knepley                                  | -1  1 -1 | | phi_1 | =
18092df84da0SMatthew G. Knepley                                  \ -1 -1  1 / \ phi_2 /
18102df84da0SMatthew G. Knepley 
18112df84da0SMatthew 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
18122df84da0SMatthew G. Knepley       */
18132df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta):
18142df84da0SMatthew G. Knepley            \phi^0 = 1/4 (   -xi - eta        + xi zeta + eta zeta) --> /  1  1  1  1  1  1 \ 1
18152df84da0SMatthew G. Knepley            \phi^1 = 1/4 (1      + eta - zeta           - eta zeta) --> | -1  1 -1 -1 -1  1 | eta
18162df84da0SMatthew G. Knepley            \phi^2 = 1/4 (1 + xi       - zeta - xi zeta)            --> | -1 -1  1 -1  1 -1 | xi
18172df84da0SMatthew G. Knepley            \phi^3 = 1/4 (   -xi - eta        - xi zeta - eta zeta) --> | -1 -1 -1  1  1  1 | zeta
18182df84da0SMatthew G. Knepley            \phi^4 = 1/4 (1 + xi       + zeta + xi zeta)            --> |  1  1 -1 -1  1 -1 | xi zeta
18192df84da0SMatthew G. Knepley            \phi^5 = 1/4 (1      + eta + zeta           + eta zeta) --> \  1 -1  1 -1 -1  1 / eta zeta
18202df84da0SMatthew G. Knepley            1/4 /  0  1  1  0  1  1 \
18212df84da0SMatthew G. Knepley                | -1  1  0 -1  0  1 |
18222df84da0SMatthew G. Knepley                | -1  0  1 -1  1  0 |
18232df84da0SMatthew G. Knepley                |  0 -1 -1  0  1  1 |
18242df84da0SMatthew G. Knepley                |  1  0 -1 -1  1  0 |
18252df84da0SMatthew G. Knepley                \  1 -1  0 -1  0  1 /
18262df84da0SMatthew G. Knepley       */
18272df84da0SMatthew G. Knepley       coeff[dim * 0 + j] = (1. / 4.) * (verts[dim * 1 + j] + verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
18282df84da0SMatthew G. Knepley       coeff[dim * 1 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
18292df84da0SMatthew G. Knepley       coeff[dim * 2 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
18302df84da0SMatthew G. Knepley       coeff[dim * 3 + j] = (1. / 4.) * (-verts[dim * 1 + j] - verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
18312df84da0SMatthew G. Knepley       coeff[dim * 4 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
18322df84da0SMatthew G. Knepley       coeff[dim * 5 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
18332df84da0SMatthew G. Knepley       /* For reference prism:
18342df84da0SMatthew G. Knepley       {0, 0, 0}
18352df84da0SMatthew G. Knepley       {0, 1, 0}
18362df84da0SMatthew G. Knepley       {1, 0, 0}
18372df84da0SMatthew G. Knepley       {0, 0, 1}
18382df84da0SMatthew G. Knepley       {0, 0, 0}
18392df84da0SMatthew G. Knepley       {0, 0, 0}
18402df84da0SMatthew G. Knepley       */
18412df84da0SMatthew G. Knepley     }
18422df84da0SMatthew G. Knepley     for (i = 0; i < Nq; ++i) {
18432df84da0SMatthew G. Knepley       const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2];
18442df84da0SMatthew G. Knepley 
18452df84da0SMatthew G. Knepley       if (v) {
18462df84da0SMatthew G. Knepley         PetscReal extPoint[6];
18472df84da0SMatthew G. Knepley         PetscInt  c;
18482df84da0SMatthew G. Knepley 
18492df84da0SMatthew G. Knepley         extPoint[0] = 1.;
18502df84da0SMatthew G. Knepley         extPoint[1] = eta;
18512df84da0SMatthew G. Knepley         extPoint[2] = xi;
18522df84da0SMatthew G. Knepley         extPoint[3] = zeta;
18532df84da0SMatthew G. Knepley         extPoint[4] = xi * zeta;
18542df84da0SMatthew G. Knepley         extPoint[5] = eta * zeta;
18552df84da0SMatthew G. Knepley         for (c = 0; c < dim; ++c) {
18562df84da0SMatthew G. Knepley           PetscReal val = 0.;
18572df84da0SMatthew G. Knepley 
1858ad540459SPierre Jolivet           for (k = 0; k < Nv; ++k) val += extPoint[k] * coeff[k * dim + c];
18592df84da0SMatthew G. Knepley           v[i * dim + c] = val;
18602df84da0SMatthew G. Knepley         }
18612df84da0SMatthew G. Knepley       }
18622df84da0SMatthew G. Knepley       if (J) {
18632df84da0SMatthew G. Knepley         PetscReal extJ[18];
18642df84da0SMatthew G. Knepley 
18659371c9d4SSatish Balay         extJ[0]  = 0.;
18669371c9d4SSatish Balay         extJ[1]  = 0.;
18679371c9d4SSatish Balay         extJ[2]  = 0.;
18689371c9d4SSatish Balay         extJ[3]  = 0.;
18699371c9d4SSatish Balay         extJ[4]  = 1.;
18709371c9d4SSatish Balay         extJ[5]  = 0.;
18719371c9d4SSatish Balay         extJ[6]  = 1.;
18729371c9d4SSatish Balay         extJ[7]  = 0.;
18739371c9d4SSatish Balay         extJ[8]  = 0.;
18749371c9d4SSatish Balay         extJ[9]  = 0.;
18759371c9d4SSatish Balay         extJ[10] = 0.;
18769371c9d4SSatish Balay         extJ[11] = 1.;
18779371c9d4SSatish Balay         extJ[12] = zeta;
18789371c9d4SSatish Balay         extJ[13] = 0.;
18799371c9d4SSatish Balay         extJ[14] = xi;
18809371c9d4SSatish Balay         extJ[15] = 0.;
18819371c9d4SSatish Balay         extJ[16] = zeta;
18829371c9d4SSatish Balay         extJ[17] = eta;
18832df84da0SMatthew G. Knepley 
18842df84da0SMatthew G. Knepley         for (j = 0; j < dim; j++) {
18852df84da0SMatthew G. Knepley           for (k = 0; k < dimR; k++) {
18862df84da0SMatthew G. Knepley             PetscReal val = 0.;
18872df84da0SMatthew G. Knepley 
1888ad540459SPierre Jolivet             for (l = 0; l < Nv; l++) val += coeff[dim * l + j] * extJ[dimR * l + k];
18892df84da0SMatthew G. Knepley             J[i * dim * dim + dim * j + k] = val;
18902df84da0SMatthew G. Knepley           }
18912df84da0SMatthew G. Knepley         }
18922df84da0SMatthew G. Knepley         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1893ad540459SPierre Jolivet         if (invJ) DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);
18942df84da0SMatthew G. Knepley       }
18952df84da0SMatthew G. Knepley     }
18962df84da0SMatthew G. Knepley   }
18976858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
18982df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
18992df84da0SMatthew G. Knepley }
19002df84da0SMatthew G. Knepley 
1901d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1902d71ae5a4SJacob Faibussowitsch {
1903ba2698f1SMatthew G. Knepley   DMPolytopeType   ct;
1904dfccc68fSToby Isaac   PetscInt         depth, dim, coordDim, coneSize, i;
1905dfccc68fSToby Isaac   PetscInt         Nq     = 0;
1906dfccc68fSToby Isaac   const PetscReal *points = NULL;
1907dfccc68fSToby Isaac   DMLabel          depthLabel;
1908c330f8ffSToby Isaac   PetscReal        xi0[3]   = {-1., -1., -1.}, v0[3], J0[9], detJ0;
1909dfccc68fSToby Isaac   PetscBool        isAffine = PETSC_TRUE;
1910dfccc68fSToby Isaac 
1911dfccc68fSToby Isaac   PetscFunctionBegin;
19129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
19139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
19149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
19159566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(depthLabel, cell, &dim));
191648a46eb9SPierre Jolivet   if (depth == 1 && dim == 1) PetscCall(DMGetDimension(dm, &dim));
19179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &coordDim));
191863a3b9bcSJacob Faibussowitsch   PetscCheck(coordDim <= 3, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %" PetscInt_FMT " > 3", coordDim);
19199566063dSJacob Faibussowitsch   if (quad) PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL));
19209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
1921ba2698f1SMatthew G. Knepley   switch (ct) {
1922ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_POINT:
19239566063dSJacob Faibussowitsch     PetscCall(DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ));
1924dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1925dfccc68fSToby Isaac     break;
1926ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
1927412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
19289566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19299566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ));
1930dfccc68fSToby Isaac     break;
1931ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
19329566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19339566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ));
1934dfccc68fSToby Isaac     break;
1935ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
19369566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ));
1937412e9a14SMatthew G. Knepley     isAffine = PETSC_FALSE;
1938412e9a14SMatthew G. Knepley     break;
1939412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
19409566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ));
1941dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1942dfccc68fSToby Isaac     break;
1943ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
19449566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19459566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ));
1946dfccc68fSToby Isaac     break;
1947ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
19489566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
1949dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1950dfccc68fSToby Isaac     break;
19512df84da0SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM:
19529566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
19532df84da0SMatthew G. Knepley     isAffine = PETSC_FALSE;
19542df84da0SMatthew G. Knepley     break;
1955d71ae5a4SJacob Faibussowitsch   default:
1956d71ae5a4SJacob 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))]);
1957dfccc68fSToby Isaac   }
19587318780aSToby Isaac   if (isAffine && Nq) {
1959dfccc68fSToby Isaac     if (v) {
1960ad540459SPierre Jolivet       for (i = 0; i < Nq; i++) CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]);
1961dfccc68fSToby Isaac     }
19627318780aSToby Isaac     if (detJ) {
1963ad540459SPierre Jolivet       for (i = 0; i < Nq; i++) detJ[i] = detJ0;
19647318780aSToby Isaac     }
19657318780aSToby Isaac     if (J) {
19667318780aSToby Isaac       PetscInt k;
19677318780aSToby Isaac 
19687318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1969dfccc68fSToby Isaac         PetscInt j;
1970dfccc68fSToby Isaac 
1971ad540459SPierre Jolivet         for (j = 0; j < coordDim * coordDim; j++, k++) J[k] = J0[j];
19727318780aSToby Isaac       }
19737318780aSToby Isaac     }
19747318780aSToby Isaac     if (invJ) {
19757318780aSToby Isaac       PetscInt k;
19767318780aSToby Isaac       switch (coordDim) {
1977d71ae5a4SJacob Faibussowitsch       case 0:
1978d71ae5a4SJacob Faibussowitsch         break;
1979d71ae5a4SJacob Faibussowitsch       case 1:
1980d71ae5a4SJacob Faibussowitsch         invJ[0] = 1. / J0[0];
1981d71ae5a4SJacob Faibussowitsch         break;
1982d71ae5a4SJacob Faibussowitsch       case 2:
1983d71ae5a4SJacob Faibussowitsch         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
1984d71ae5a4SJacob Faibussowitsch         break;
1985d71ae5a4SJacob Faibussowitsch       case 3:
1986d71ae5a4SJacob Faibussowitsch         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
1987d71ae5a4SJacob Faibussowitsch         break;
19887318780aSToby Isaac       }
19897318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
19907318780aSToby Isaac         PetscInt j;
19917318780aSToby Isaac 
1992ad540459SPierre Jolivet         for (j = 0; j < coordDim * coordDim; j++, k++) invJ[k] = invJ[j];
1993dfccc68fSToby Isaac       }
1994dfccc68fSToby Isaac     }
1995dfccc68fSToby Isaac   }
1996dfccc68fSToby Isaac   PetscFunctionReturn(0);
1997dfccc68fSToby Isaac }
1998dfccc68fSToby Isaac 
1999ccd2543fSMatthew G Knepley /*@C
20008e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
2001ccd2543fSMatthew G Knepley 
2002d083f849SBarry Smith   Collective on dm
2003ccd2543fSMatthew G Knepley 
20044165533cSJose E. Roman   Input Parameters:
2005ccd2543fSMatthew G Knepley + dm   - the DM
2006ccd2543fSMatthew G Knepley - cell - the cell
2007ccd2543fSMatthew G Knepley 
20084165533cSJose E. Roman   Output Parameters:
20099b172b3aSMatthew Knepley + v0   - the translation part of this affine transform, meaning the translation to the origin (not the first vertex of the reference cell)
2010ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
2011ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
2012ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
2013ccd2543fSMatthew G Knepley 
2014ccd2543fSMatthew G Knepley   Level: advanced
2015ccd2543fSMatthew G Knepley 
2016ccd2543fSMatthew G Knepley   Fortran Notes:
2017ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2018ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
2019ccd2543fSMatthew G Knepley 
2020db781477SPatrick Sanan .seealso: `DMPlexComputeCellGeometryFEM()`, `DMGetCoordinateSection()`, `DMGetCoordinates()`
2021ccd2543fSMatthew G Knepley @*/
2022d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
2023d71ae5a4SJacob Faibussowitsch {
2024ccd2543fSMatthew G Knepley   PetscFunctionBegin;
20259566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, NULL, v0, J, invJ, detJ));
20268e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
20278e0841e0SMatthew G. Knepley }
20288e0841e0SMatthew G. Knepley 
2029d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
2030d71ae5a4SJacob Faibussowitsch {
20316858538eSMatthew G. Knepley   const PetscScalar *array;
20328e0841e0SMatthew G. Knepley   PetscScalar       *coords = NULL;
20336858538eSMatthew G. Knepley   PetscInt           numCoords;
20346858538eSMatthew G. Knepley   PetscBool          isDG;
20356858538eSMatthew G. Knepley   PetscQuadrature    feQuad;
20368e0841e0SMatthew G. Knepley   const PetscReal   *quadPoints;
2037ef0bb6c7SMatthew G. Knepley   PetscTabulation    T;
20386858538eSMatthew G. Knepley   PetscInt           dim, cdim, pdim, qdim, Nq, q;
20398e0841e0SMatthew G. Knepley 
20408e0841e0SMatthew G. Knepley   PetscFunctionBegin;
20419566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
20429566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
20436858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
2044dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
2045dfccc68fSToby Isaac     PetscDualSpace dsp;
2046dfccc68fSToby Isaac 
20479566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(fe, &dsp));
20489566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetFunctional(dsp, 0, &quad));
20499566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2050dfccc68fSToby Isaac     Nq = 1;
2051dfccc68fSToby Isaac   } else {
20529566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2053dfccc68fSToby Isaac   }
20549566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
20559566063dSJacob Faibussowitsch   PetscCall(PetscFEGetQuadrature(fe, &feQuad));
2056dfccc68fSToby Isaac   if (feQuad == quad) {
20579566063dSJacob Faibussowitsch     PetscCall(PetscFEGetCellTabulation(fe, J ? 1 : 0, &T));
205863a3b9bcSJacob 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);
2059dfccc68fSToby Isaac   } else {
20609566063dSJacob Faibussowitsch     PetscCall(PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T));
2061dfccc68fSToby Isaac   }
206263a3b9bcSJacob Faibussowitsch   PetscCheck(qdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %" PetscInt_FMT " != quadrature dimension %" PetscInt_FMT, dim, qdim);
2063ef0bb6c7SMatthew G. Knepley   {
2064ef0bb6c7SMatthew G. Knepley     const PetscReal *basis    = T->T[0];
2065ef0bb6c7SMatthew G. Knepley     const PetscReal *basisDer = T->T[1];
2066ef0bb6c7SMatthew G. Knepley     PetscReal        detJt;
2067ef0bb6c7SMatthew G. Knepley 
2068a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
206963a3b9bcSJacob Faibussowitsch     PetscCheck(Nq == T->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %" PetscInt_FMT " != %" PetscInt_FMT, Nq, T->Np);
207063a3b9bcSJacob Faibussowitsch     PetscCheck(pdim == T->Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %" PetscInt_FMT " != %" PetscInt_FMT, pdim, T->Nb);
207163a3b9bcSJacob Faibussowitsch     PetscCheck(dim == T->Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %" PetscInt_FMT " != %" PetscInt_FMT, dim, T->Nc);
207263a3b9bcSJacob Faibussowitsch     PetscCheck(cdim == T->cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %" PetscInt_FMT " != %" PetscInt_FMT, cdim, T->cdim);
2073a2a9e04cSMatthew G. Knepley #endif
2074dfccc68fSToby Isaac     if (v) {
20759566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(v, Nq * cdim));
2076f960e424SToby Isaac       for (q = 0; q < Nq; ++q) {
2077f960e424SToby Isaac         PetscInt i, k;
2078f960e424SToby Isaac 
2079301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2080301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
2081ad540459SPierre Jolivet           for (i = 0; i < cdim; ++i) v[q * cdim + i] += basis[(q * pdim + k) * cdim + i] * PetscRealPart(coords[vertex * cdim + i]);
2082301b184aSMatthew G. Knepley         }
20839566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * cdim));
2084f960e424SToby Isaac       }
2085f960e424SToby Isaac     }
20868e0841e0SMatthew G. Knepley     if (J) {
20879566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(J, Nq * cdim * cdim));
20888e0841e0SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
20898e0841e0SMatthew G. Knepley         PetscInt i, j, k, c, r;
20908e0841e0SMatthew G. Knepley 
20918e0841e0SMatthew G. Knepley         /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
2092301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2093301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
2094301b184aSMatthew G. Knepley           for (j = 0; j < dim; ++j) {
2095ad540459SPierre 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]);
2096301b184aSMatthew G. Knepley           }
2097301b184aSMatthew G. Knepley         }
20989566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * dim * cdim));
20998e0841e0SMatthew G. Knepley         if (cdim > dim) {
21008e0841e0SMatthew G. Knepley           for (c = dim; c < cdim; ++c)
21019371c9d4SSatish Balay             for (r = 0; r < cdim; ++r) J[r * cdim + c] = r == c ? 1.0 : 0.0;
21028e0841e0SMatthew G. Knepley         }
2103f960e424SToby Isaac         if (!detJ && !invJ) continue;
2104a63b72c6SToby Isaac         detJt = 0.;
21058e0841e0SMatthew G. Knepley         switch (cdim) {
21068e0841e0SMatthew G. Knepley         case 3:
2107037dc194SToby Isaac           DMPlex_Det3D_Internal(&detJt, &J[q * cdim * dim]);
2108ad540459SPierre Jolivet           if (invJ) DMPlex_Invert3D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt);
210917fe8556SMatthew G. Knepley           break;
211049dc4407SMatthew G. Knepley         case 2:
21119f328543SToby Isaac           DMPlex_Det2D_Internal(&detJt, &J[q * cdim * dim]);
2112ad540459SPierre Jolivet           if (invJ) DMPlex_Invert2D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt);
211349dc4407SMatthew G. Knepley           break;
21148e0841e0SMatthew G. Knepley         case 1:
2115037dc194SToby Isaac           detJt = J[q * cdim * dim];
2116037dc194SToby Isaac           if (invJ) invJ[q * cdim * dim] = 1.0 / detJt;
211749dc4407SMatthew G. Knepley         }
2118f960e424SToby Isaac         if (detJ) detJ[q] = detJt;
211949dc4407SMatthew G. Knepley       }
212008401ef6SPierre Jolivet     } else PetscCheck(!detJ && !invJ, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
212149dc4407SMatthew G. Knepley   }
21229566063dSJacob Faibussowitsch   if (feQuad != quad) PetscCall(PetscTabulationDestroy(&T));
21236858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
21248e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
21258e0841e0SMatthew G. Knepley }
21268e0841e0SMatthew G. Knepley 
21278e0841e0SMatthew G. Knepley /*@C
21288e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
21298e0841e0SMatthew G. Knepley 
2130d083f849SBarry Smith   Collective on dm
21318e0841e0SMatthew G. Knepley 
21324165533cSJose E. Roman   Input Parameters:
21338e0841e0SMatthew G. Knepley + dm   - the DM
21348e0841e0SMatthew G. Knepley . cell - the cell
2135dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
2136dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
21378e0841e0SMatthew G. Knepley 
21384165533cSJose E. Roman   Output Parameters:
2139dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
21408e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
21418e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
21428e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
21438e0841e0SMatthew G. Knepley 
21448e0841e0SMatthew G. Knepley   Level: advanced
21458e0841e0SMatthew G. Knepley 
21468e0841e0SMatthew G. Knepley   Fortran Notes:
21478e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
21488e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
21498e0841e0SMatthew G. Knepley 
2150db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()`
21518e0841e0SMatthew G. Knepley @*/
2152d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
2153d71ae5a4SJacob Faibussowitsch {
2154bb4a5db5SMatthew G. Knepley   DM      cdm;
2155dfccc68fSToby Isaac   PetscFE fe = NULL;
21568e0841e0SMatthew G. Knepley 
21578e0841e0SMatthew G. Knepley   PetscFunctionBegin;
2158dadcf809SJacob Faibussowitsch   PetscValidRealPointer(detJ, 7);
21599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
2160bb4a5db5SMatthew G. Knepley   if (cdm) {
2161dfccc68fSToby Isaac     PetscClassId id;
2162dfccc68fSToby Isaac     PetscInt     numFields;
2163e5e52638SMatthew G. Knepley     PetscDS      prob;
2164dfccc68fSToby Isaac     PetscObject  disc;
2165dfccc68fSToby Isaac 
21669566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(cdm, &numFields));
2167dfccc68fSToby Isaac     if (numFields) {
21689566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &prob));
21699566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(prob, 0, &disc));
21709566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
2171ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
2172dfccc68fSToby Isaac     }
2173dfccc68fSToby Isaac   }
21749566063dSJacob Faibussowitsch   if (!fe) PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ));
21759566063dSJacob Faibussowitsch   else PetscCall(DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ));
2176ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
2177ccd2543fSMatthew G Knepley }
2178834e62ceSMatthew G. Knepley 
2179d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2180d71ae5a4SJacob Faibussowitsch {
21819bf2564aSMatt McGurn   PetscSection       coordSection;
21829bf2564aSMatt McGurn   Vec                coordinates;
21839bf2564aSMatt McGurn   const PetscScalar *coords = NULL;
21849bf2564aSMatt McGurn   PetscInt           d, dof, off;
21859bf2564aSMatt McGurn 
21869bf2564aSMatt McGurn   PetscFunctionBegin;
21879566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
21889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
21899566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
21909bf2564aSMatt McGurn 
21919bf2564aSMatt McGurn   /* for a point the centroid is just the coord */
21929bf2564aSMatt McGurn   if (centroid) {
21939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
21949566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
2195ad540459SPierre Jolivet     for (d = 0; d < dof; d++) centroid[d] = PetscRealPart(coords[off + d]);
21969bf2564aSMatt McGurn   }
21979bf2564aSMatt McGurn   if (normal) {
21989bf2564aSMatt McGurn     const PetscInt *support, *cones;
21999bf2564aSMatt McGurn     PetscInt        supportSize;
22009bf2564aSMatt McGurn     PetscReal       norm, sign;
22019bf2564aSMatt McGurn 
22029bf2564aSMatt McGurn     /* compute the norm based upon the support centroids */
22039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, cell, &supportSize));
22049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, cell, &support));
22059566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL));
22069bf2564aSMatt McGurn 
22079bf2564aSMatt McGurn     /* Take the normal from the centroid of the support to the vertex*/
22089566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
22099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
2210ad540459SPierre Jolivet     for (d = 0; d < dof; d++) normal[d] -= PetscRealPart(coords[off + d]);
22119bf2564aSMatt McGurn 
22129bf2564aSMatt McGurn     /* Determine the sign of the normal based upon its location in the support */
22139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, support[0], &cones));
22149bf2564aSMatt McGurn     sign = cones[0] == cell ? 1.0 : -1.0;
22159bf2564aSMatt McGurn 
22169bf2564aSMatt McGurn     norm = DMPlex_NormD_Internal(dim, normal);
22179bf2564aSMatt McGurn     for (d = 0; d < dim; ++d) normal[d] /= (norm * sign);
22189bf2564aSMatt McGurn   }
2219ad540459SPierre Jolivet   if (vol) *vol = 1.0;
22209566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
22219bf2564aSMatt McGurn   PetscFunctionReturn(0);
22229bf2564aSMatt McGurn }
22239bf2564aSMatt McGurn 
2224d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2225d71ae5a4SJacob Faibussowitsch {
22266858538eSMatthew G. Knepley   const PetscScalar *array;
2227a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
222821d6a034SMatthew G. Knepley   PetscInt           cdim, coordSize, d;
22296858538eSMatthew G. Knepley   PetscBool          isDG;
2230cc08537eSMatthew G. Knepley 
2231cc08537eSMatthew G. Knepley   PetscFunctionBegin;
223221d6a034SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
22336858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
223421d6a034SMatthew G. Knepley   PetscCheck(coordSize == cdim * 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Edge has %" PetscInt_FMT " coordinates != %" PetscInt_FMT, coordSize, cdim * 2);
2235cc08537eSMatthew G. Knepley   if (centroid) {
223621d6a034SMatthew G. Knepley     for (d = 0; d < cdim; ++d) centroid[d] = 0.5 * PetscRealPart(coords[d] + coords[cdim + d]);
2237cc08537eSMatthew G. Knepley   }
2238cc08537eSMatthew G. Knepley   if (normal) {
2239a60a936bSMatthew G. Knepley     PetscReal norm;
2240a60a936bSMatthew G. Knepley 
224121d6a034SMatthew G. Knepley     switch (cdim) {
224221d6a034SMatthew G. Knepley     case 3:
2243*f315e28eSPierre Jolivet       normal[2] = 0.; /* fall through */
224421d6a034SMatthew G. Knepley     case 2:
224521d6a034SMatthew G. Knepley       normal[0] = -PetscRealPart(coords[1] - coords[cdim + 1]);
224621d6a034SMatthew G. Knepley       normal[1] = PetscRealPart(coords[0] - coords[cdim + 0]);
224721d6a034SMatthew G. Knepley       break;
224821d6a034SMatthew G. Knepley     case 1:
224921d6a034SMatthew G. Knepley       normal[0] = 1.0;
225021d6a034SMatthew G. Knepley       break;
225121d6a034SMatthew G. Knepley     default:
225221d6a034SMatthew G. Knepley       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Dimension %" PetscInt_FMT " not supported", cdim);
225321d6a034SMatthew G. Knepley     }
225421d6a034SMatthew G. Knepley     norm = DMPlex_NormD_Internal(cdim, normal);
225521d6a034SMatthew G. Knepley     for (d = 0; d < cdim; ++d) normal[d] /= norm;
2256cc08537eSMatthew G. Knepley   }
2257cc08537eSMatthew G. Knepley   if (vol) {
2258714b99b6SMatthew G. Knepley     *vol = 0.0;
225921d6a034SMatthew G. Knepley     for (d = 0; d < cdim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - coords[cdim + d]));
2260714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
2261cc08537eSMatthew G. Knepley   }
22626858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2263cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
2264cc08537eSMatthew G. Knepley }
2265cc08537eSMatthew G. Knepley 
2266cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */
2267d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2268d71ae5a4SJacob Faibussowitsch {
2269412e9a14SMatthew G. Knepley   DMPolytopeType     ct;
22706858538eSMatthew G. Knepley   const PetscScalar *array;
2271cc08537eSMatthew G. Knepley   PetscScalar       *coords = NULL;
22726858538eSMatthew G. Knepley   PetscInt           coordSize;
22736858538eSMatthew G. Knepley   PetscBool          isDG;
2274793a2a13SMatthew G. Knepley   PetscInt           fv[4] = {0, 1, 2, 3};
22756858538eSMatthew G. Knepley   PetscInt           cdim, numCorners, p, d;
2276cc08537eSMatthew G. Knepley 
2277cc08537eSMatthew G. Knepley   PetscFunctionBegin;
2278793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
22799566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2280412e9a14SMatthew G. Knepley   switch (ct) {
22819371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR:
22829371c9d4SSatish Balay     fv[2] = 3;
22839371c9d4SSatish Balay     fv[3] = 2;
22849371c9d4SSatish Balay     break;
2285d71ae5a4SJacob Faibussowitsch   default:
2286d71ae5a4SJacob Faibussowitsch     break;
2287412e9a14SMatthew G. Knepley   }
22889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
22896858538eSMatthew G. Knepley   PetscCall(DMPlexGetConeSize(dm, cell, &numCorners));
22906858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
22913f27a4e6SJed Brown   {
22923f27a4e6SJed Brown     PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm;
2293793a2a13SMatthew G. Knepley 
22943f27a4e6SJed Brown     for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]);
22954f99dae5SMatthew G. Knepley     for (p = 0; p < numCorners - 2; ++p) {
22963f27a4e6SJed Brown       PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.};
22973f27a4e6SJed Brown       for (d = 0; d < cdim; d++) {
22983f27a4e6SJed Brown         e0[d] = PetscRealPart(coords[cdim * fv[p + 1] + d]) - origin[d];
22993f27a4e6SJed Brown         e1[d] = PetscRealPart(coords[cdim * fv[p + 2] + d]) - origin[d];
23003f27a4e6SJed Brown       }
23013f27a4e6SJed Brown       const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1];
23023f27a4e6SJed Brown       const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2];
23033f27a4e6SJed Brown       const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0];
23043f27a4e6SJed Brown       const PetscReal a  = PetscSqrtReal(dx * dx + dy * dy + dz * dz);
23054f99dae5SMatthew G. Knepley 
23064f99dae5SMatthew G. Knepley       n[0] += dx;
23074f99dae5SMatthew G. Knepley       n[1] += dy;
23084f99dae5SMatthew G. Knepley       n[2] += dz;
2309ad540459SPierre Jolivet       for (d = 0; d < cdim; d++) c[d] += a * PetscRealPart(origin[d] + coords[cdim * fv[p + 1] + d] + coords[cdim * fv[p + 2] + d]) / 3.;
2310ceee4971SMatthew G. Knepley     }
23114f99dae5SMatthew G. Knepley     norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
23124f99dae5SMatthew G. Knepley     n[0] /= norm;
23134f99dae5SMatthew G. Knepley     n[1] /= norm;
23144f99dae5SMatthew G. Knepley     n[2] /= norm;
23154f99dae5SMatthew G. Knepley     c[0] /= norm;
23164f99dae5SMatthew G. Knepley     c[1] /= norm;
23174f99dae5SMatthew G. Knepley     c[2] /= norm;
23184f99dae5SMatthew G. Knepley     if (vol) *vol = 0.5 * norm;
23199371c9d4SSatish Balay     if (centroid)
23209371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) centroid[d] = c[d];
23219371c9d4SSatish Balay     if (normal)
23229371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) normal[d] = n[d];
23230a1d6728SMatthew G. Knepley   }
23246858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2325cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
2326cc08537eSMatthew G. Knepley }
2327cc08537eSMatthew G. Knepley 
23280ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */
2329d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2330d71ae5a4SJacob Faibussowitsch {
2331412e9a14SMatthew G. Knepley   DMPolytopeType        ct;
23326858538eSMatthew G. Knepley   const PetscScalar    *array;
23330ec8681fSMatthew G. Knepley   PetscScalar          *coords = NULL;
23346858538eSMatthew G. Knepley   PetscInt              coordSize;
23356858538eSMatthew G. Knepley   PetscBool             isDG;
23363f27a4e6SJed Brown   PetscReal             vsum      = 0.0, vtmp, coordsTmp[3 * 3], origin[3];
23376858538eSMatthew G. Knepley   const PetscInt        order[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
23386858538eSMatthew G. Knepley   const PetscInt       *cone, *faceSizes, *faces;
23396858538eSMatthew G. Knepley   const DMPolytopeType *faceTypes;
2340793a2a13SMatthew G. Knepley   PetscBool             isHybrid = PETSC_FALSE;
23416858538eSMatthew G. Knepley   PetscInt              numFaces, f, fOff = 0, p, d;
23420ec8681fSMatthew G. Knepley 
23430ec8681fSMatthew G. Knepley   PetscFunctionBegin;
234463a3b9bcSJacob Faibussowitsch   PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No support for dim %" PetscInt_FMT " > 3", dim);
2345793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
23469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2347412e9a14SMatthew G. Knepley   switch (ct) {
2348412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
2349412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
2350412e9a14SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR:
2351d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
2352d71ae5a4SJacob Faibussowitsch     isHybrid = PETSC_TRUE;
2353d71ae5a4SJacob Faibussowitsch   default:
2354d71ae5a4SJacob Faibussowitsch     break;
2355412e9a14SMatthew G. Knepley   }
2356793a2a13SMatthew G. Knepley 
23579371c9d4SSatish Balay   if (centroid)
23589371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = 0.0;
23596858538eSMatthew G. Knepley   PetscCall(DMPlexGetCone(dm, cell, &cone));
23606858538eSMatthew G. Knepley 
23616858538eSMatthew G. Knepley   // Using the closure of faces for coordinates does not work in periodic geometries, so we index into the cell coordinates
23626858538eSMatthew G. Knepley   PetscCall(DMPlexGetRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
23636858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
23640ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
2365793a2a13SMatthew G. Knepley     PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
2366793a2a13SMatthew G. Knepley 
23673f27a4e6SJed Brown     // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and
23683f27a4e6SJed Brown     // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex
23693f27a4e6SJed Brown     // so that all tetrahedra have positive volume.
23709371c9d4SSatish Balay     if (f == 0)
23719371c9d4SSatish Balay       for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]);
23726858538eSMatthew G. Knepley     switch (faceTypes[f]) {
2373ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
23740ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
23756858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + 0] * dim + d]) - origin[d];
23766858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + 1] * dim + d]) - origin[d];
23776858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + 2] * dim + d]) - origin[d];
23780ec8681fSMatthew G. Knepley       }
23790ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
23806858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
23810ec8681fSMatthew G. Knepley       vsum += vtmp;
23824f25033aSJed Brown       if (centroid) { /* Centroid of OABC = (a+b+c)/4 */
23830ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
23841ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
23850ec8681fSMatthew G. Knepley         }
23860ec8681fSMatthew G. Knepley       }
23870ec8681fSMatthew G. Knepley       break;
2388ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
23899371c9d4SSatish Balay     case DM_POLYTOPE_SEG_PRISM_TENSOR: {
2390793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
2391793a2a13SMatthew G. Knepley 
2392793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
23939371c9d4SSatish Balay       if (isHybrid && f > 1) {
23949371c9d4SSatish Balay         fv[2] = 3;
23959371c9d4SSatish Balay         fv[3] = 2;
23969371c9d4SSatish Balay       }
23970ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
23980ec8681fSMatthew G. Knepley       /* First tet */
23990ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
24006858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[0]] * dim + d]) - origin[d];
24016858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
24026858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
24030ec8681fSMatthew G. Knepley       }
24040ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
24056858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
24060ec8681fSMatthew G. Knepley       vsum += vtmp;
24070ec8681fSMatthew G. Knepley       if (centroid) {
24080ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
24090ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
24100ec8681fSMatthew G. Knepley         }
24110ec8681fSMatthew G. Knepley       }
24120ec8681fSMatthew G. Knepley       /* Second tet */
24130ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
24146858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
24156858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[2]] * dim + d]) - origin[d];
24166858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
24170ec8681fSMatthew G. Knepley       }
24180ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
24196858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
24200ec8681fSMatthew G. Knepley       vsum += vtmp;
24210ec8681fSMatthew G. Knepley       if (centroid) {
24220ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
24230ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
24240ec8681fSMatthew G. Knepley         }
24250ec8681fSMatthew G. Knepley       }
24260ec8681fSMatthew G. Knepley       break;
2427793a2a13SMatthew G. Knepley     }
2428d71ae5a4SJacob Faibussowitsch     default:
2429d71ae5a4SJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %" PetscInt_FMT " of type %s", cone[f], DMPolytopeTypes[ct]);
24300ec8681fSMatthew G. Knepley     }
24316858538eSMatthew G. Knepley     fOff += faceSizes[f];
24320ec8681fSMatthew G. Knepley   }
24336858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
24346858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
24358763be8eSMatthew G. Knepley   if (vol) *vol = PetscAbsReal(vsum);
24369371c9d4SSatish Balay   if (normal)
24379371c9d4SSatish Balay     for (d = 0; d < dim; ++d) normal[d] = 0.0;
24389371c9d4SSatish Balay   if (centroid)
24399371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum * 4) + origin[d];
24400ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
24410ec8681fSMatthew G. Knepley }
24420ec8681fSMatthew G. Knepley 
2443834e62ceSMatthew G. Knepley /*@C
2444834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
2445834e62ceSMatthew G. Knepley 
2446d083f849SBarry Smith   Collective on dm
2447834e62ceSMatthew G. Knepley 
24484165533cSJose E. Roman   Input Parameters:
2449834e62ceSMatthew G. Knepley + dm   - the DM
2450834e62ceSMatthew G. Knepley - cell - the cell
2451834e62ceSMatthew G. Knepley 
24524165533cSJose E. Roman   Output Parameters:
2453834e62ceSMatthew G. Knepley + volume   - the cell volume
2454cc08537eSMatthew G. Knepley . centroid - the cell centroid
2455cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
2456834e62ceSMatthew G. Knepley 
2457834e62ceSMatthew G. Knepley   Level: advanced
2458834e62ceSMatthew G. Knepley 
2459834e62ceSMatthew G. Knepley   Fortran Notes:
2460834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2461834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
2462834e62ceSMatthew G. Knepley 
2463db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()`
2464834e62ceSMatthew G. Knepley @*/
2465d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2466d71ae5a4SJacob Faibussowitsch {
24670ec8681fSMatthew G. Knepley   PetscInt depth, dim;
2468834e62ceSMatthew G. Knepley 
2469834e62ceSMatthew G. Knepley   PetscFunctionBegin;
24709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24719566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
247208401ef6SPierre Jolivet   PetscCheck(depth == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
24739566063dSJacob Faibussowitsch   PetscCall(DMPlexGetPointDepth(dm, cell, &depth));
2474011ea5d8SMatthew G. Knepley   switch (depth) {
2475d71ae5a4SJacob Faibussowitsch   case 0:
2476d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal));
2477d71ae5a4SJacob Faibussowitsch     break;
2478d71ae5a4SJacob Faibussowitsch   case 1:
2479d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal));
2480d71ae5a4SJacob Faibussowitsch     break;
2481d71ae5a4SJacob Faibussowitsch   case 2:
2482d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal));
2483d71ae5a4SJacob Faibussowitsch     break;
2484d71ae5a4SJacob Faibussowitsch   case 3:
2485d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal));
2486d71ae5a4SJacob Faibussowitsch     break;
2487d71ae5a4SJacob Faibussowitsch   default:
2488d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT " (depth %" PetscInt_FMT ") for element geometry computation", dim, depth);
2489834e62ceSMatthew G. Knepley   }
2490834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
2491834e62ceSMatthew G. Knepley }
2492113c68e6SMatthew G. Knepley 
2493c501906fSMatthew G. Knepley /*@
2494c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
2495c501906fSMatthew G. Knepley 
2496c501906fSMatthew G. Knepley   Collective on dm
2497c501906fSMatthew G. Knepley 
2498c501906fSMatthew G. Knepley   Input Parameter:
2499c501906fSMatthew G. Knepley . dm - The DMPlex
2500c501906fSMatthew G. Knepley 
2501c501906fSMatthew G. Knepley   Output Parameter:
2502c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
2503c501906fSMatthew G. Knepley 
2504c501906fSMatthew G. Knepley   Level: beginner
2505c501906fSMatthew G. Knepley 
2506c501906fSMatthew G. Knepley @*/
2507d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
2508d71ae5a4SJacob Faibussowitsch {
2509c0d900a5SMatthew G. Knepley   DM           dmCell;
2510c0d900a5SMatthew G. Knepley   Vec          coordinates;
2511c0d900a5SMatthew G. Knepley   PetscSection coordSection, sectionCell;
2512c0d900a5SMatthew G. Knepley   PetscScalar *cgeom;
2513412e9a14SMatthew G. Knepley   PetscInt     cStart, cEnd, c;
2514c0d900a5SMatthew G. Knepley 
2515c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
25169566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmCell));
25179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
25189566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
25199566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection));
25209566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmCell, coordinates));
25219566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionCell));
25229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
25239566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd));
2524c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
25259566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFEGeom)) / sizeof(PetscScalar))));
25269566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionCell));
25279566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmCell, sectionCell));
25289566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionCell));
25299566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmCell, cellgeom));
25309566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*cellgeom, &cgeom));
2531c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2532cf0b7c11SKarl Rupp     PetscFEGeom *cg;
2533c0d900a5SMatthew G. Knepley 
25349566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg));
25359566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(cg, 1));
25369566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ));
253763a3b9bcSJacob Faibussowitsch     PetscCheck(*cg->detJ > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %" PetscInt_FMT, (double)*cg->detJ, c);
2538c0d900a5SMatthew G. Knepley   }
2539c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
2540c0d900a5SMatthew G. Knepley }
2541c0d900a5SMatthew G. Knepley 
2542891a9168SMatthew G. Knepley /*@
2543891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2544891a9168SMatthew G. Knepley 
2545891a9168SMatthew G. Knepley   Input Parameter:
2546891a9168SMatthew G. Knepley . dm - The DM
2547891a9168SMatthew G. Knepley 
2548891a9168SMatthew G. Knepley   Output Parameters:
2549891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2550a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data
2551891a9168SMatthew G. Knepley 
2552891a9168SMatthew G. Knepley   Level: developer
2553891a9168SMatthew G. Knepley 
2554db781477SPatrick Sanan .seealso: `PetscFVFaceGeom`, `PetscFVCellGeom`, `DMPlexComputeGeometryFEM()`
2555891a9168SMatthew G. Knepley @*/
2556d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2557d71ae5a4SJacob Faibussowitsch {
2558113c68e6SMatthew G. Knepley   DM           dmFace, dmCell;
2559113c68e6SMatthew G. Knepley   DMLabel      ghostLabel;
2560113c68e6SMatthew G. Knepley   PetscSection sectionFace, sectionCell;
2561113c68e6SMatthew G. Knepley   PetscSection coordSection;
2562113c68e6SMatthew G. Knepley   Vec          coordinates;
2563113c68e6SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2564113c68e6SMatthew G. Knepley   PetscReal    minradius, gminradius;
2565113c68e6SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2566113c68e6SMatthew G. Knepley 
2567113c68e6SMatthew G. Knepley   PetscFunctionBegin;
25689566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25699566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
25709566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
2571113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
25729566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmCell));
25739566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection));
25749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmCell, coordinates));
25759566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionCell));
25769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
25779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
25789566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd));
25799566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVCellGeom)) / sizeof(PetscScalar))));
25809566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionCell));
25819566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmCell, sectionCell));
25829566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionCell));
25839566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmCell, cellgeom));
2584485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
25859566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*cellgeom, &cgeom));
2586113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2587113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2588113c68e6SMatthew G. Knepley 
25899566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg));
25909566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(cg, 1));
25919566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL));
2592113c68e6SMatthew G. Knepley   }
2593113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
25949566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmFace));
25959566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionFace));
25969566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
25979566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionFace, fStart, fEnd));
25989566063dSJacob Faibussowitsch   for (f = fStart; f < fEnd; ++f) PetscCall(PetscSectionSetDof(sectionFace, f, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVFaceGeom)) / sizeof(PetscScalar))));
25999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionFace));
26009566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmFace, sectionFace));
26019566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionFace));
26029566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmFace, facegeom));
26039566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*facegeom, &fgeom));
26049566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2605113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2606113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2607113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2608113c68e6SMatthew G. Knepley     PetscReal        area;
2609412e9a14SMatthew G. Knepley     const PetscInt  *cells;
2610412e9a14SMatthew G. Knepley     PetscInt         ncells, ghost = -1, d, numChildren;
2611113c68e6SMatthew G. Knepley 
26129566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
26139566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
26149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, f, &cells));
26159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &ncells));
2616412e9a14SMatthew G. Knepley     /* It is possible to get a face with no support when using partition overlap */
2617412e9a14SMatthew G. Knepley     if (!ncells || ghost >= 0 || numChildren) continue;
26189566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, f, fgeom, &fg));
26199566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal));
2620113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2621113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2622113c68e6SMatthew G. Knepley     {
2623113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
2624113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
26250453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2626113c68e6SMatthew G. Knepley 
26279566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL));
2628113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
262906348e87SToby Isaac       if (ncells > 1) {
26309566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR));
2631113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
26329371c9d4SSatish Balay       } else {
263306348e87SToby Isaac         rcentroid = fg->centroid;
263406348e87SToby Isaac       }
26359566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l));
26369566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r));
26370453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2638113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2639113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2640113c68e6SMatthew G. Knepley       }
2641113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
264263a3b9bcSJacob 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]);
264363a3b9bcSJacob 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]);
264463a3b9bcSJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " could not be fixed", f);
2645113c68e6SMatthew G. Knepley       }
2646113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2647113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2648113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2649113c68e6SMatthew G. Knepley       }
265006348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2651113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2652113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2653113c68e6SMatthew G. Knepley       }
2654113c68e6SMatthew G. Knepley     }
2655113c68e6SMatthew G. Knepley   }
26561c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm)));
26579566063dSJacob Faibussowitsch   PetscCall(DMPlexSetMinRadius(dm, gminradius));
2658113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2659113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2660113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2661113c68e6SMatthew G. Knepley     const PetscInt  *cone, *support;
2662113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2663113c68e6SMatthew G. Knepley 
26649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dmCell, c, &coneSize));
266563a3b9bcSJacob Faibussowitsch     PetscCheck(coneSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %" PetscInt_FMT " has cone size %" PetscInt_FMT " != 1", c, coneSize);
26669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dmCell, c, &cone));
26679566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmCell, cone[0], &supportSize));
266863a3b9bcSJacob Faibussowitsch     PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " has support size %" PetscInt_FMT " != 2", cone[0], supportSize);
26699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dmCell, cone[0], &support));
26709566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg));
2671113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2672113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2673113c68e6SMatthew G. Knepley       if (support[s] == c) {
2674640bce14SSatish Balay         PetscFVCellGeom *ci;
2675113c68e6SMatthew G. Knepley         PetscFVCellGeom *cg;
2676113c68e6SMatthew G. Knepley         PetscReal        c2f[3], a;
2677113c68e6SMatthew G. Knepley 
26789566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, support[(s + 1) % 2], cgeom, &ci));
2679113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2680113c68e6SMatthew G. Knepley         a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal) / DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
26819566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg));
2682113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2 * a, fg->normal, ci->centroid, cg->centroid);
2683113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2684113c68e6SMatthew G. Knepley       }
2685113c68e6SMatthew G. Knepley     }
2686113c68e6SMatthew G. Knepley   }
26879566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*facegeom, &fgeom));
26889566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*cellgeom, &cgeom));
26899566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmCell));
26909566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmFace));
2691113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2692113c68e6SMatthew G. Knepley }
2693113c68e6SMatthew G. Knepley 
2694113c68e6SMatthew G. Knepley /*@C
2695113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2696113c68e6SMatthew G. Knepley 
2697113c68e6SMatthew G. Knepley   Not collective
2698113c68e6SMatthew G. Knepley 
26994165533cSJose E. Roman   Input Parameter:
2700113c68e6SMatthew G. Knepley . dm - the DM
2701113c68e6SMatthew G. Knepley 
27024165533cSJose E. Roman   Output Parameter:
2703a5b23f4aSJose E. Roman . minradius - the minimum cell radius
2704113c68e6SMatthew G. Knepley 
2705113c68e6SMatthew G. Knepley   Level: developer
2706113c68e6SMatthew G. Knepley 
2707db781477SPatrick Sanan .seealso: `DMGetCoordinates()`
2708113c68e6SMatthew G. Knepley @*/
2709d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2710d71ae5a4SJacob Faibussowitsch {
2711113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2712113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2713dadcf809SJacob Faibussowitsch   PetscValidRealPointer(minradius, 2);
2714113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex *)dm->data)->minradius;
2715113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2716113c68e6SMatthew G. Knepley }
2717113c68e6SMatthew G. Knepley 
2718113c68e6SMatthew G. Knepley /*@C
2719113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2720113c68e6SMatthew G. Knepley 
2721113c68e6SMatthew G. Knepley   Logically collective
2722113c68e6SMatthew G. Knepley 
27234165533cSJose E. Roman   Input Parameters:
2724113c68e6SMatthew G. Knepley + dm - the DM
2725a5b23f4aSJose E. Roman - minradius - the minimum cell radius
2726113c68e6SMatthew G. Knepley 
2727113c68e6SMatthew G. Knepley   Level: developer
2728113c68e6SMatthew G. Knepley 
2729db781477SPatrick Sanan .seealso: `DMSetCoordinates()`
2730113c68e6SMatthew G. Knepley @*/
2731d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2732d71ae5a4SJacob Faibussowitsch {
2733113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2734113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2735113c68e6SMatthew G. Knepley   ((DM_Plex *)dm->data)->minradius = minradius;
2736113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2737113c68e6SMatthew G. Knepley }
2738856ac710SMatthew G. Knepley 
2739d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2740d71ae5a4SJacob Faibussowitsch {
2741856ac710SMatthew G. Knepley   DMLabel      ghostLabel;
2742856ac710SMatthew G. Knepley   PetscScalar *dx, *grad, **gref;
2743856ac710SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2744856ac710SMatthew G. Knepley 
2745856ac710SMatthew G. Knepley   PetscFunctionBegin;
27469566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
27479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
27489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2749089217ebSMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
27509566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxNumFaces, NULL));
27519566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
27529566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
27539566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2754856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2755856ac710SMatthew G. Knepley     const PetscInt  *faces;
2756856ac710SMatthew G. Knepley     PetscInt         numFaces, usedFaces, f, d;
2757640bce14SSatish Balay     PetscFVCellGeom *cg;
2758856ac710SMatthew G. Knepley     PetscBool        boundary;
2759856ac710SMatthew G. Knepley     PetscInt         ghost;
2760856ac710SMatthew G. Knepley 
2761a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
2762a79418b7SMatt McGurn     PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2763a79418b7SMatt McGurn     if (ghost >= 0) continue;
2764a79418b7SMatt McGurn 
27659566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
27669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &numFaces));
27679566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &faces));
276863a3b9bcSJacob 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);
2769856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2770640bce14SSatish Balay       PetscFVCellGeom *cg1;
2771856ac710SMatthew G. Knepley       PetscFVFaceGeom *fg;
2772856ac710SMatthew G. Knepley       const PetscInt  *fcells;
2773856ac710SMatthew G. Knepley       PetscInt         ncell, side;
2774856ac710SMatthew G. Knepley 
27759566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
27769566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2777856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
27789566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, faces[f], &fcells));
2779856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2780856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
27819566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg));
27829566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2783856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces * dim + d] = cg1->centroid[d] - cg->centroid[d];
2784856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */
2785856ac710SMatthew G. Knepley     }
278628b400f6SJacob Faibussowitsch     PetscCheck(usedFaces, PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
27879566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, usedFaces, dx, grad));
2788856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
27899566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
27909566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2791856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2792856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces * dim + d];
2793856ac710SMatthew G. Knepley       ++usedFaces;
2794856ac710SMatthew G. Knepley     }
2795856ac710SMatthew G. Knepley   }
27969566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
2797856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2798856ac710SMatthew G. Knepley }
2799856ac710SMatthew G. Knepley 
2800d71ae5a4SJacob Faibussowitsch static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2801d71ae5a4SJacob Faibussowitsch {
2802b81db932SToby Isaac   DMLabel      ghostLabel;
2803b81db932SToby Isaac   PetscScalar *dx, *grad, **gref;
2804b81db932SToby Isaac   PetscInt     dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2805b81db932SToby Isaac   PetscSection neighSec;
2806b81db932SToby Isaac   PetscInt(*neighbors)[2];
2807b81db932SToby Isaac   PetscInt *counter;
2808b81db932SToby Isaac 
2809b81db932SToby Isaac   PetscFunctionBegin;
28109566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
28119566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
28129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2813485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
28149566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &neighSec));
28159566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(neighSec, cStart, cEndInterior));
28169566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
28179566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2818b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2819b81db932SToby Isaac     const PetscInt *fcells;
2820b81db932SToby Isaac     PetscBool       boundary;
28215bc680faSToby Isaac     PetscInt        ghost = -1;
2822b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2823b81db932SToby Isaac 
28249566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
28259566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
28269566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2827b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
28289566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
282906348e87SToby Isaac     if (numCells == 2) {
28309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2831b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2832b81db932SToby Isaac         PetscInt cell = fcells[c];
2833b81db932SToby Isaac 
283448a46eb9SPierre Jolivet         if (cell >= cStart && cell < cEndInterior) PetscCall(PetscSectionAddDof(neighSec, cell, 1));
2835b81db932SToby Isaac       }
2836b81db932SToby Isaac     }
283706348e87SToby Isaac   }
28389566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(neighSec));
28399566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetMaxDof(neighSec, &maxNumFaces));
28409566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
2841b81db932SToby Isaac   nStart = 0;
28429566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(neighSec, &nEnd));
28439566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1((nEnd - nStart), &neighbors));
28449566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1((cEndInterior - cStart), &counter));
2845b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2846b81db932SToby Isaac     const PetscInt *fcells;
2847b81db932SToby Isaac     PetscBool       boundary;
28485bc680faSToby Isaac     PetscInt        ghost = -1;
2849b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2850b81db932SToby Isaac 
28519566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
28529566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
28539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2854b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
28559566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
285606348e87SToby Isaac     if (numCells == 2) {
28579566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2858b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2859b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2860b81db932SToby Isaac 
2861e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
28629566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(neighSec, cell, &off));
2863b81db932SToby Isaac           off += counter[cell - cStart]++;
2864b81db932SToby Isaac           neighbors[off][0] = f;
2865b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2866b81db932SToby Isaac         }
2867b81db932SToby Isaac       }
2868b81db932SToby Isaac     }
286906348e87SToby Isaac   }
28709566063dSJacob Faibussowitsch   PetscCall(PetscFree(counter));
28719566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2872b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2873317218b9SToby Isaac     PetscInt         numFaces, f, d, off, ghost = -1;
2874640bce14SSatish Balay     PetscFVCellGeom *cg;
2875b81db932SToby Isaac 
28769566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
28779566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(neighSec, c, &numFaces));
28789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(neighSec, c, &off));
2879a79418b7SMatt McGurn 
2880a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
28819566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2882a79418b7SMatt McGurn     if (ghost >= 0) continue;
2883a79418b7SMatt McGurn 
288463a3b9bcSJacob 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);
2885b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2886640bce14SSatish Balay       PetscFVCellGeom *cg1;
2887b81db932SToby Isaac       PetscFVFaceGeom *fg;
2888b81db932SToby Isaac       const PetscInt  *fcells;
2889b81db932SToby Isaac       PetscInt         ncell, side, nface;
2890b81db932SToby Isaac 
2891b81db932SToby Isaac       nface = neighbors[off + f][0];
2892b81db932SToby Isaac       ncell = neighbors[off + f][1];
28939566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, nface, &fcells));
2894b81db932SToby Isaac       side = (c != fcells[0]);
28959566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, nface, fgeom, &fg));
28969566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2897b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f * dim + d] = cg1->centroid[d] - cg->centroid[d];
2898b81db932SToby Isaac       gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */
2899b81db932SToby Isaac     }
29009566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, numFaces, dx, grad));
2901b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2902b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f * dim + d];
2903b81db932SToby Isaac     }
2904b81db932SToby Isaac   }
29059566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
29069566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&neighSec));
29079566063dSJacob Faibussowitsch   PetscCall(PetscFree(neighbors));
2908b81db932SToby Isaac   PetscFunctionReturn(0);
2909b81db932SToby Isaac }
2910b81db932SToby Isaac 
2911856ac710SMatthew G. Knepley /*@
2912856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2913856ac710SMatthew G. Knepley 
2914d083f849SBarry Smith   Collective on dm
2915856ac710SMatthew G. Knepley 
29164165533cSJose E. Roman   Input Parameters:
2917856ac710SMatthew G. Knepley + dm  - The DM
2918856ac710SMatthew G. Knepley . fvm - The PetscFV
29198f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2920856ac710SMatthew G. Knepley 
29216b867d5aSJose E. Roman   Input/Output Parameter:
29226b867d5aSJose E. Roman . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM(); on output
29236b867d5aSJose E. Roman                  the geometric factors for gradient calculation are inserted
29246b867d5aSJose E. Roman 
29256b867d5aSJose E. Roman   Output Parameter:
29266b867d5aSJose E. Roman . dmGrad - The DM describing the layout of gradient data
2927856ac710SMatthew G. Knepley 
2928856ac710SMatthew G. Knepley   Level: developer
2929856ac710SMatthew G. Knepley 
2930db781477SPatrick Sanan .seealso: `DMPlexGetFaceGeometryFVM()`, `DMPlexGetCellGeometryFVM()`
2931856ac710SMatthew G. Knepley @*/
2932d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2933d71ae5a4SJacob Faibussowitsch {
2934856ac710SMatthew G. Knepley   DM           dmFace, dmCell;
2935856ac710SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2936b81db932SToby Isaac   PetscSection sectionGrad, parentSection;
2937856ac710SMatthew G. Knepley   PetscInt     dim, pdim, cStart, cEnd, cEndInterior, c;
2938856ac710SMatthew G. Knepley 
2939856ac710SMatthew G. Knepley   PetscFunctionBegin;
29409566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
29419566063dSJacob Faibussowitsch   PetscCall(PetscFVGetNumComponents(fvm, &pdim));
29429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
29439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2944856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
29459566063dSJacob Faibussowitsch   PetscCall(VecGetDM(faceGeometry, &dmFace));
29469566063dSJacob Faibussowitsch   PetscCall(VecGetDM(cellGeometry, &dmCell));
29479566063dSJacob Faibussowitsch   PetscCall(VecGetArray(faceGeometry, &fgeom));
29489566063dSJacob Faibussowitsch   PetscCall(VecGetArray(cellGeometry, &cgeom));
29499566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &parentSection, NULL, NULL, NULL, NULL));
2950b81db932SToby Isaac   if (!parentSection) {
29519566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2952b5a3613cSMatthew G. Knepley   } else {
29539566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2954b81db932SToby Isaac   }
29559566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(faceGeometry, &fgeom));
29569566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(cellGeometry, &cgeom));
2957856ac710SMatthew G. Knepley   /* Create storage for gradients */
29589566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, dmGrad));
29599566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionGrad));
29609566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionGrad, cStart, cEnd));
29619566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionGrad, c, pdim * dim));
29629566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionGrad));
29639566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(*dmGrad, sectionGrad));
29649566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionGrad));
2965856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2966856ac710SMatthew G. Knepley }
2967b27d5b9eSToby Isaac 
2968c501906fSMatthew G. Knepley /*@
2969c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2970c501906fSMatthew G. Knepley 
2971d083f849SBarry Smith   Collective on dm
2972c501906fSMatthew G. Knepley 
29734165533cSJose E. Roman   Input Parameters:
2974c501906fSMatthew G. Knepley + dm  - The DM
29756b867d5aSJose E. Roman - fv  - The PetscFV
2976c501906fSMatthew G. Knepley 
2977c501906fSMatthew G. Knepley   Output Parameters:
2978c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2979c501906fSMatthew G. Knepley . faceGeometry - The face geometry
29806b867d5aSJose E. Roman - gradDM       - The gradient matrices
2981c501906fSMatthew G. Knepley 
2982c501906fSMatthew G. Knepley   Level: developer
2983c501906fSMatthew G. Knepley 
2984db781477SPatrick Sanan .seealso: `DMPlexComputeGeometryFVM()`
2985c501906fSMatthew G. Knepley @*/
2986d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2987d71ae5a4SJacob Faibussowitsch {
2988b27d5b9eSToby Isaac   PetscObject cellgeomobj, facegeomobj;
2989b27d5b9eSToby Isaac 
2990b27d5b9eSToby Isaac   PetscFunctionBegin;
29919566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
2992b27d5b9eSToby Isaac   if (!cellgeomobj) {
2993b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2994b27d5b9eSToby Isaac 
29959566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt));
29969566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_cellgeom_fvm", (PetscObject)cellgeomInt));
29979566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_facegeom_fvm", (PetscObject)facegeomInt));
29989566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cellgeomInt));
29999566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&facegeomInt));
30009566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
3001b27d5b9eSToby Isaac   }
30029566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_facegeom_fvm", &facegeomobj));
3003b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec)cellgeomobj;
3004b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec)facegeomobj;
3005b27d5b9eSToby Isaac   if (gradDM) {
3006b27d5b9eSToby Isaac     PetscObject gradobj;
3007b27d5b9eSToby Isaac     PetscBool   computeGradients;
3008b27d5b9eSToby Isaac 
30099566063dSJacob Faibussowitsch     PetscCall(PetscFVGetComputeGradients(fv, &computeGradients));
3010b27d5b9eSToby Isaac     if (!computeGradients) {
3011b27d5b9eSToby Isaac       *gradDM = NULL;
3012b27d5b9eSToby Isaac       PetscFunctionReturn(0);
3013b27d5b9eSToby Isaac     }
30149566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
3015b27d5b9eSToby Isaac     if (!gradobj) {
3016b27d5b9eSToby Isaac       DM dmGradInt;
3017b27d5b9eSToby Isaac 
30189566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeGradientFVM(dm, fv, (Vec)facegeomobj, (Vec)cellgeomobj, &dmGradInt));
30199566063dSJacob Faibussowitsch       PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt));
30209566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dmGradInt));
30219566063dSJacob Faibussowitsch       PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
3022b27d5b9eSToby Isaac     }
3023b27d5b9eSToby Isaac     *gradDM = (DM)gradobj;
3024b27d5b9eSToby Isaac   }
3025b27d5b9eSToby Isaac   PetscFunctionReturn(0);
3026b27d5b9eSToby Isaac }
3027d6143a4eSToby Isaac 
3028d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess)
3029d71ae5a4SJacob Faibussowitsch {
30309d150b73SToby Isaac   PetscInt l, m;
30319d150b73SToby Isaac 
3032cd345991SToby Isaac   PetscFunctionBeginHot;
30339d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
30349d150b73SToby Isaac     /* invert Jacobian, multiply */
30359d150b73SToby Isaac     PetscScalar det, idet;
30369d150b73SToby Isaac 
30379d150b73SToby Isaac     switch (dimR) {
3038d71ae5a4SJacob Faibussowitsch     case 1:
3039d71ae5a4SJacob Faibussowitsch       invJ[0] = 1. / J[0];
3040d71ae5a4SJacob Faibussowitsch       break;
30419d150b73SToby Isaac     case 2:
30429d150b73SToby Isaac       det     = J[0] * J[3] - J[1] * J[2];
30439d150b73SToby Isaac       idet    = 1. / det;
30449d150b73SToby Isaac       invJ[0] = J[3] * idet;
30459d150b73SToby Isaac       invJ[1] = -J[1] * idet;
30469d150b73SToby Isaac       invJ[2] = -J[2] * idet;
30479d150b73SToby Isaac       invJ[3] = J[0] * idet;
30489d150b73SToby Isaac       break;
30499371c9d4SSatish Balay     case 3: {
30509d150b73SToby Isaac       invJ[0] = J[4] * J[8] - J[5] * J[7];
30519d150b73SToby Isaac       invJ[1] = J[2] * J[7] - J[1] * J[8];
30529d150b73SToby Isaac       invJ[2] = J[1] * J[5] - J[2] * J[4];
30539d150b73SToby Isaac       det     = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
30549d150b73SToby Isaac       idet    = 1. / det;
30559d150b73SToby Isaac       invJ[0] *= idet;
30569d150b73SToby Isaac       invJ[1] *= idet;
30579d150b73SToby Isaac       invJ[2] *= idet;
30589d150b73SToby Isaac       invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]);
30599d150b73SToby Isaac       invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]);
30609d150b73SToby Isaac       invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]);
30619d150b73SToby Isaac       invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]);
30629d150b73SToby Isaac       invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]);
30639d150b73SToby Isaac       invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]);
30649371c9d4SSatish Balay     } break;
30659d150b73SToby Isaac     }
30669d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
3067ad540459SPierre Jolivet       for (m = 0; m < dimC; m++) guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
30689d150b73SToby Isaac     }
30699d150b73SToby Isaac   } else {
30709d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
30719d150b73SToby Isaac     char transpose = 'C';
30729d150b73SToby Isaac #else
30739d150b73SToby Isaac     char transpose = 'T';
30749d150b73SToby Isaac #endif
30759d150b73SToby Isaac     PetscBLASInt m        = dimR;
30769d150b73SToby Isaac     PetscBLASInt n        = dimC;
30779d150b73SToby Isaac     PetscBLASInt one      = 1;
30789d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
30799d150b73SToby Isaac 
3080ad540459SPierre Jolivet     for (l = 0; l < dimC; l++) invJ[l] = resNeg[l];
30819d150b73SToby Isaac 
3082792fecdfSBarry Smith     PetscCallBLAS("LAPACKgels", LAPACKgels_(&transpose, &m, &n, &one, J, &m, invJ, &n, work, &worksize, &info));
308308401ef6SPierre Jolivet     PetscCheck(info == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Bad argument to GELS");
30849d150b73SToby Isaac 
3085ad540459SPierre Jolivet     for (l = 0; l < dimR; l++) guess[l] += PetscRealPart(invJ[l]);
30869d150b73SToby Isaac   }
30879d150b73SToby Isaac   PetscFunctionReturn(0);
30889d150b73SToby Isaac }
30899d150b73SToby Isaac 
3090d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
3091d71ae5a4SJacob Faibussowitsch {
3092c0cbe899SToby Isaac   PetscInt     coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
30939d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
30949d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
30959d150b73SToby Isaac   PetscScalar *J, *invJ, *work;
30969d150b73SToby Isaac 
30979d150b73SToby Isaac   PetscFunctionBegin;
30989d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30999566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
31001dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
31019566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
31029566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
31039d150b73SToby Isaac   cellCoords = &cellData[0];
31049d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
31059d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
31069d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
31079d150b73SToby Isaac   invJ       = &J[dimR * dimC];
31089d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
31099d150b73SToby Isaac   if (dimR == 2) {
31109d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
31119d150b73SToby Isaac 
31129d150b73SToby Isaac     for (i = 0; i < 4; i++) {
31139d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31149d150b73SToby Isaac 
3115ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
31169d150b73SToby Isaac     }
31179d150b73SToby Isaac   } else if (dimR == 3) {
31189d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
31199d150b73SToby Isaac 
31209d150b73SToby Isaac     for (i = 0; i < 8; i++) {
31219d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31229d150b73SToby Isaac 
3123ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
31249d150b73SToby Isaac     }
31259d150b73SToby Isaac   } else {
3126ad540459SPierre Jolivet     for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]);
31279d150b73SToby Isaac   }
31289d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
31299d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
31309d150b73SToby Isaac     PetscReal *swap;
31319d150b73SToby Isaac 
31329d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
31339d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
31349d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
31359d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
31369d150b73SToby Isaac       }
31379d150b73SToby Isaac     }
31389d150b73SToby Isaac 
31399d150b73SToby Isaac     if (i < dimR - 1) {
31409d150b73SToby Isaac       swap       = cellCoeffs;
31419d150b73SToby Isaac       cellCoeffs = cellCoords;
31429d150b73SToby Isaac       cellCoords = swap;
31439d150b73SToby Isaac     }
31449d150b73SToby Isaac   }
31459566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(refCoords, numPoints * dimR));
31469d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
31479d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
31489d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
31499d150b73SToby Isaac 
31509d150b73SToby Isaac       /* compute -residual and Jacobian */
3151ad540459SPierre Jolivet       for (k = 0; k < dimC; k++) resNeg[k] = realCoords[dimC * j + k];
3152ad540459SPierre Jolivet       for (k = 0; k < dimC * dimR; k++) J[k] = 0.;
31539d150b73SToby Isaac       for (k = 0; k < numV; k++) {
31549d150b73SToby Isaac         PetscReal extCoord = 1.;
31559d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
31569d150b73SToby Isaac           PetscReal coord = guess[l];
31579d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
31589d150b73SToby Isaac 
31599d150b73SToby Isaac           extCoord *= dep * coord + !dep;
31609d150b73SToby Isaac           extJ[l] = dep;
31619d150b73SToby Isaac 
31629d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
31639d150b73SToby Isaac             PetscReal coord = guess[m];
31649d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
31659d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
31669d150b73SToby Isaac 
31679d150b73SToby Isaac             extJ[l] *= mult;
31689d150b73SToby Isaac           }
31699d150b73SToby Isaac         }
31709d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
31719d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
31729d150b73SToby Isaac 
31739d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
3174ad540459SPierre Jolivet           for (m = 0; m < dimR; m++) J[dimR * l + m] += coeff * extJ[m];
31759d150b73SToby Isaac         }
31769d150b73SToby Isaac       }
317776bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
31780611203eSToby Isaac         PetscReal maxAbs = 0.;
31790611203eSToby Isaac 
3180ad540459SPierre Jolivet         for (l = 0; l < dimC; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l]));
318163a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
31820611203eSToby Isaac       }
31839d150b73SToby Isaac 
31849566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(dimC, dimR, J, invJ, work, resNeg, guess));
31859d150b73SToby Isaac     }
31869d150b73SToby Isaac   }
31879566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
31889566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
31899566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
31909d150b73SToby Isaac   PetscFunctionReturn(0);
31919d150b73SToby Isaac }
31929d150b73SToby Isaac 
3193d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
3194d71ae5a4SJacob Faibussowitsch {
31959d150b73SToby Isaac   PetscInt     coordSize, i, j, k, l, numV = (1 << dimR);
31969d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
31979d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs;
31989d150b73SToby Isaac 
31999d150b73SToby Isaac   PetscFunctionBegin;
32009d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32019566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
32021dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
32039566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
32049d150b73SToby Isaac   cellCoords = &cellData[0];
32059d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
32069d150b73SToby Isaac   if (dimR == 2) {
32079d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
32089d150b73SToby Isaac 
32099d150b73SToby Isaac     for (i = 0; i < 4; i++) {
32109d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
32119d150b73SToby Isaac 
3212ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
32139d150b73SToby Isaac     }
32149d150b73SToby Isaac   } else if (dimR == 3) {
32159d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
32169d150b73SToby Isaac 
32179d150b73SToby Isaac     for (i = 0; i < 8; i++) {
32189d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
32199d150b73SToby Isaac 
3220ad540459SPierre Jolivet       for (j = 0; j < dimC; j++) cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
32219d150b73SToby Isaac     }
32229d150b73SToby Isaac   } else {
3223ad540459SPierre Jolivet     for (i = 0; i < coordSize; i++) cellCoords[i] = PetscRealPart(coordsScalar[i]);
32249d150b73SToby Isaac   }
32259d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
32269d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
32279d150b73SToby Isaac     PetscReal *swap;
32289d150b73SToby Isaac 
32299d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
32309d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
32319d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
32329d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
32339d150b73SToby Isaac       }
32349d150b73SToby Isaac     }
32359d150b73SToby Isaac 
32369d150b73SToby Isaac     if (i < dimR - 1) {
32379d150b73SToby Isaac       swap       = cellCoeffs;
32389d150b73SToby Isaac       cellCoeffs = cellCoords;
32399d150b73SToby Isaac       cellCoords = swap;
32409d150b73SToby Isaac     }
32419d150b73SToby Isaac   }
32429566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(realCoords, numPoints * dimC));
32439d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32449d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
32459d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
32469d150b73SToby Isaac 
32479d150b73SToby Isaac     for (k = 0; k < numV; k++) {
32489d150b73SToby Isaac       PetscReal extCoord = 1.;
32499d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
32509d150b73SToby Isaac         PetscReal coord = guess[l];
32519d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
32529d150b73SToby Isaac 
32539d150b73SToby Isaac         extCoord *= dep * coord + !dep;
32549d150b73SToby Isaac       }
32559d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
32569d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
32579d150b73SToby Isaac 
32589d150b73SToby Isaac         mapped[l] += coeff * extCoord;
32599d150b73SToby Isaac       }
32609d150b73SToby Isaac     }
32619d150b73SToby Isaac   }
32629566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
32639566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
32649d150b73SToby Isaac   PetscFunctionReturn(0);
32659d150b73SToby Isaac }
32669d150b73SToby Isaac 
32679c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
3268d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
3269d71ae5a4SJacob Faibussowitsch {
32709c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
3271c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3272c6e120d1SToby Isaac   PetscReal   *invV, *modes;
3273c6e120d1SToby Isaac   PetscReal   *B, *D, *resNeg;
3274c6e120d1SToby Isaac   PetscScalar *J, *invJ, *work;
32759d150b73SToby Isaac 
32769d150b73SToby Isaac   PetscFunctionBegin;
32779566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
32789566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
327963a3b9bcSJacob 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);
32809566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32819d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
32829566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
32839d150b73SToby Isaac   invV = fe->invV;
3284012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3285012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3286ad540459SPierre Jolivet     for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
32879d150b73SToby Isaac   }
32889566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
32899c3cf19fSMatthew G. Knepley   D      = &B[pdim * Nc];
32909c3cf19fSMatthew G. Knepley   resNeg = &D[pdim * Nc * dimR];
32919566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
32929c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
32939c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
3294ad540459SPierre Jolivet   for (i = 0; i < numPoints * dimR; i++) refCoords[i] = 0.;
32959d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32969b1f03cbSToby 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 */
32979d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
32989566063dSJacob Faibussowitsch       PetscCall(PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL));
3299ad540459SPierre Jolivet       for (k = 0; k < Nc; k++) resNeg[k] = realCoords[j * Nc + k];
3300ad540459SPierre Jolivet       for (k = 0; k < Nc * dimR; k++) J[k] = 0.;
33019c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
33029c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
3303012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
3304ad540459SPierre Jolivet           for (m = 0; m < dimR; m++) J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
33059d150b73SToby Isaac         }
33069d150b73SToby Isaac       }
330776bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
33080611203eSToby Isaac         PetscReal maxAbs = 0.;
33090611203eSToby Isaac 
3310ad540459SPierre Jolivet         for (l = 0; l < Nc; l++) maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l]));
331163a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
33120611203eSToby Isaac       }
33139566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(Nc, dimR, J, invJ, work, resNeg, guess));
33149d150b73SToby Isaac     }
33159d150b73SToby Isaac   }
33169566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
33179566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
33189566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
33199566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
33209d150b73SToby Isaac   PetscFunctionReturn(0);
33219d150b73SToby Isaac }
33229d150b73SToby Isaac 
33239c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
3324d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
3325d71ae5a4SJacob Faibussowitsch {
33269c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, coordSize;
3327c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3328c6e120d1SToby Isaac   PetscReal   *invV, *modes;
33299d150b73SToby Isaac   PetscReal   *B;
33309d150b73SToby Isaac 
33319d150b73SToby Isaac   PetscFunctionBegin;
33329566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
33339566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
333463a3b9bcSJacob 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);
33359566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
33369d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
33379566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
33389d150b73SToby Isaac   invV = fe->invV;
3339012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3340012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3341ad540459SPierre Jolivet     for (j = 0; j < pdim; ++j) modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
33429d150b73SToby Isaac   }
33439566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
33449566063dSJacob Faibussowitsch   PetscCall(PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL));
3345ad540459SPierre Jolivet   for (i = 0; i < numPoints * Nc; i++) realCoords[i] = 0.;
33469d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
33479c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
33489d150b73SToby Isaac 
33499c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
3350ad540459SPierre Jolivet       for (l = 0; l < Nc; l++) mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
33519d150b73SToby Isaac     }
33529d150b73SToby Isaac   }
33539566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
33549566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
33559566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
33569d150b73SToby Isaac   PetscFunctionReturn(0);
33579d150b73SToby Isaac }
33589d150b73SToby Isaac 
3359d6143a4eSToby Isaac /*@
3360d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
3361d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
3362d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
3363d6143a4eSToby Isaac 
3364d6143a4eSToby Isaac   Not collective
3365d6143a4eSToby Isaac 
3366d6143a4eSToby Isaac   Input Parameters:
3367d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
3368d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
3369d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
3370d6143a4eSToby Isaac . cell       - the cell whose map is used.
3371d6143a4eSToby Isaac . numPoints  - the number of points to locate
33721b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
3373d6143a4eSToby Isaac 
3374d6143a4eSToby Isaac   Output Parameters:
3375d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
33761b266c99SBarry Smith 
33771b266c99SBarry Smith   Level: intermediate
337873c9229bSMatthew Knepley 
3379db781477SPatrick Sanan .seealso: `DMPlexReferenceToCoordinates()`
3380d6143a4eSToby Isaac @*/
3381d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
3382d71ae5a4SJacob Faibussowitsch {
3383485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
33849d150b73SToby Isaac   DM       coordDM = NULL;
33859d150b73SToby Isaac   Vec      coords;
33869d150b73SToby Isaac   PetscFE  fe = NULL;
33879d150b73SToby Isaac 
3388d6143a4eSToby Isaac   PetscFunctionBegin;
33899d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33909566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
33919566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
33929d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
33939566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
33949566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
33959566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
33969d150b73SToby Isaac   if (coordDM) {
33979d150b73SToby Isaac     PetscInt coordFields;
33989d150b73SToby Isaac 
33999566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
34009d150b73SToby Isaac     if (coordFields) {
34019d150b73SToby Isaac       PetscClassId id;
34029d150b73SToby Isaac       PetscObject  disc;
34039d150b73SToby Isaac 
34049566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
34059566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
3406ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
34079d150b73SToby Isaac     }
34089d150b73SToby Isaac   }
34099566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
34101dca8a05SBarry 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);
34119d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
34129d150b73SToby Isaac     PetscInt  coneSize;
34139d150b73SToby Isaac     PetscBool isSimplex, isTensor;
34149d150b73SToby Isaac 
34159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
34169d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
34179d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
34189d150b73SToby Isaac     if (isSimplex) {
34199d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
34209d150b73SToby Isaac 
34219566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34229d150b73SToby Isaac       J    = &v0[dimC];
34239d150b73SToby Isaac       invJ = &J[dimC * dimC];
34249566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ));
34259d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3426c330f8ffSToby Isaac         const PetscReal x0[3] = {-1., -1., -1.};
3427c330f8ffSToby Isaac 
3428c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
34299d150b73SToby Isaac       }
34309566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34319d150b73SToby Isaac     } else if (isTensor) {
34329566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
343363a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
34349d150b73SToby Isaac   } else {
34359566063dSJacob Faibussowitsch     PetscCall(DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
34369d150b73SToby Isaac   }
34379d150b73SToby Isaac   PetscFunctionReturn(0);
34389d150b73SToby Isaac }
34399d150b73SToby Isaac 
34409d150b73SToby Isaac /*@
34419d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
34429d150b73SToby Isaac 
34439d150b73SToby Isaac   Not collective
34449d150b73SToby Isaac 
34459d150b73SToby Isaac   Input Parameters:
34469d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
34479d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
34489d150b73SToby Isaac                as a multilinear map for tensor-product elements
34499d150b73SToby Isaac . cell       - the cell whose map is used.
34509d150b73SToby Isaac . numPoints  - the number of points to locate
3451a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
34529d150b73SToby Isaac 
34539d150b73SToby Isaac   Output Parameters:
34549d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
34551b266c99SBarry Smith 
34561b266c99SBarry Smith    Level: intermediate
345773c9229bSMatthew Knepley 
3458db781477SPatrick Sanan .seealso: `DMPlexCoordinatesToReference()`
34599d150b73SToby Isaac @*/
3460d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
3461d71ae5a4SJacob Faibussowitsch {
3462485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
34639d150b73SToby Isaac   DM       coordDM = NULL;
34649d150b73SToby Isaac   Vec      coords;
34659d150b73SToby Isaac   PetscFE  fe = NULL;
34669d150b73SToby Isaac 
34679d150b73SToby Isaac   PetscFunctionBegin;
34689d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34699566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
34709566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
34719d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
34729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
34739566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
34749566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
34759d150b73SToby Isaac   if (coordDM) {
34769d150b73SToby Isaac     PetscInt coordFields;
34779d150b73SToby Isaac 
34789566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
34799d150b73SToby Isaac     if (coordFields) {
34809d150b73SToby Isaac       PetscClassId id;
34819d150b73SToby Isaac       PetscObject  disc;
34829d150b73SToby Isaac 
34839566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
34849566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
3485ad540459SPierre Jolivet       if (id == PETSCFE_CLASSID) fe = (PetscFE)disc;
34869d150b73SToby Isaac     }
34879d150b73SToby Isaac   }
34889566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
34891dca8a05SBarry 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);
34909d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
34919d150b73SToby Isaac     PetscInt  coneSize;
34929d150b73SToby Isaac     PetscBool isSimplex, isTensor;
34939d150b73SToby Isaac 
34949566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
34959d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
34969d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
34979d150b73SToby Isaac     if (isSimplex) {
34989d150b73SToby Isaac       PetscReal detJ, *v0, *J;
34999d150b73SToby Isaac 
35009566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
35019d150b73SToby Isaac       J = &v0[dimC];
35029566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ));
3503c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3504c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3505c330f8ffSToby Isaac 
3506c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
35079d150b73SToby Isaac       }
35089566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
35099d150b73SToby Isaac     } else if (isTensor) {
35109566063dSJacob Faibussowitsch       PetscCall(DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
351163a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
35129d150b73SToby Isaac   } else {
35139566063dSJacob Faibussowitsch     PetscCall(DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
35149d150b73SToby Isaac   }
3515d6143a4eSToby Isaac   PetscFunctionReturn(0);
3516d6143a4eSToby Isaac }
35170139fca9SMatthew G. Knepley 
35180139fca9SMatthew G. Knepley /*@C
35190139fca9SMatthew G. Knepley   DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates.
35200139fca9SMatthew G. Knepley 
35210139fca9SMatthew G. Knepley   Not collective
35220139fca9SMatthew G. Knepley 
35230139fca9SMatthew G. Knepley   Input Parameters:
35240139fca9SMatthew G. Knepley + dm      - The DM
35250139fca9SMatthew G. Knepley . time    - The time
35260139fca9SMatthew G. Knepley - func    - The function transforming current coordinates to new coordaintes
35270139fca9SMatthew G. Knepley 
35280139fca9SMatthew G. Knepley    Calling sequence of func:
35290139fca9SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
35300139fca9SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
35310139fca9SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
35320139fca9SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
35330139fca9SMatthew G. Knepley 
35340139fca9SMatthew G. Knepley +  dim          - The spatial dimension
35350139fca9SMatthew G. Knepley .  Nf           - The number of input fields (here 1)
35360139fca9SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
35370139fca9SMatthew G. Knepley .  uOff         - The offset of the coordinates in u[] (here 0)
35380139fca9SMatthew G. Knepley .  uOff_x       - The offset of the coordinates in u_x[] (here 0)
35390139fca9SMatthew G. Knepley .  u            - The coordinate values at this point in space
35400139fca9SMatthew G. Knepley .  u_t          - The coordinate time derivative at this point in space (here NULL)
35410139fca9SMatthew G. Knepley .  u_x          - The coordinate derivatives at this point in space
35420139fca9SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
35430139fca9SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
35440139fca9SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
35450139fca9SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
35460139fca9SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
35470139fca9SMatthew G. Knepley .  t            - The current time
35480139fca9SMatthew G. Knepley .  x            - The coordinates of this point (here not used)
35490139fca9SMatthew G. Knepley .  numConstants - The number of constants
35500139fca9SMatthew G. Knepley .  constants    - The value of each constant
35510139fca9SMatthew G. Knepley -  f            - The new coordinates at this point in space
35520139fca9SMatthew G. Knepley 
35530139fca9SMatthew G. Knepley   Level: intermediate
35540139fca9SMatthew G. Knepley 
3555db781477SPatrick Sanan .seealso: `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMProjectFieldLocal()`, `DMProjectFieldLabelLocal()`
35560139fca9SMatthew G. Knepley @*/
3557d71ae5a4SJacob 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[]))
3558d71ae5a4SJacob Faibussowitsch {
35590139fca9SMatthew G. Knepley   DM      cdm;
35608bf1a49fSMatthew G. Knepley   DMField cf;
35610139fca9SMatthew G. Knepley   Vec     lCoords, tmpCoords;
35620139fca9SMatthew G. Knepley 
35630139fca9SMatthew G. Knepley   PetscFunctionBegin;
35649566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
35659566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
35669566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(cdm, &tmpCoords));
35679566063dSJacob Faibussowitsch   PetscCall(VecCopy(lCoords, tmpCoords));
35688bf1a49fSMatthew G. Knepley   /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */
35699566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateField(dm, &cf));
35706858538eSMatthew G. Knepley   cdm->coordinates[0].field = cf;
35719566063dSJacob Faibussowitsch   PetscCall(DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords));
35726858538eSMatthew G. Knepley   cdm->coordinates[0].field = NULL;
35739566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(cdm, &tmpCoords));
35749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, lCoords));
35750139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
35760139fca9SMatthew G. Knepley }
35770139fca9SMatthew G. Knepley 
35780139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z,
35790139fca9SMatthew G. Knepley   / 1  0  m_0 \
35800139fca9SMatthew G. Knepley   | 0  1  m_1 |
35810139fca9SMatthew G. Knepley   \ 0  0   1  /
35820139fca9SMatthew G. Knepley */
3583d71ae5a4SJacob 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[])
3584d71ae5a4SJacob Faibussowitsch {
35850139fca9SMatthew G. Knepley   const PetscInt Nc = uOff[1] - uOff[0];
3586c1f1bd54SMatthew G. Knepley   const PetscInt ax = (PetscInt)PetscRealPart(constants[0]);
35870139fca9SMatthew G. Knepley   PetscInt       c;
35880139fca9SMatthew G. Knepley 
3589ad540459SPierre Jolivet   for (c = 0; c < Nc; ++c) coords[c] = u[c] + constants[c + 1] * u[ax];
35900139fca9SMatthew G. Knepley }
35910139fca9SMatthew G. Knepley 
35920139fca9SMatthew G. Knepley /*@C
35930139fca9SMatthew G. Knepley   DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates.
35940139fca9SMatthew G. Knepley 
35950139fca9SMatthew G. Knepley   Not collective
35960139fca9SMatthew G. Knepley 
35970139fca9SMatthew G. Knepley   Input Parameters:
35980139fca9SMatthew G. Knepley + dm          - The DM
35993ee9839eSMatthew G. Knepley . direction   - The shear coordinate direction, e.g. 0 is the x-axis
36000139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction
36010139fca9SMatthew G. Knepley 
36020139fca9SMatthew G. Knepley   Level: intermediate
36030139fca9SMatthew G. Knepley 
3604db781477SPatrick Sanan .seealso: `DMPlexRemapGeometry()`
36050139fca9SMatthew G. Knepley @*/
3606d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[])
3607d71ae5a4SJacob Faibussowitsch {
36080139fca9SMatthew G. Knepley   DM             cdm;
36090139fca9SMatthew G. Knepley   PetscDS        cds;
36100139fca9SMatthew G. Knepley   PetscObject    obj;
36110139fca9SMatthew G. Knepley   PetscClassId   id;
36120139fca9SMatthew G. Knepley   PetscScalar   *moduli;
36133ee9839eSMatthew G. Knepley   const PetscInt dir = (PetscInt)direction;
36140139fca9SMatthew G. Knepley   PetscInt       dE, d, e;
36150139fca9SMatthew G. Knepley 
36160139fca9SMatthew G. Knepley   PetscFunctionBegin;
36179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
36189566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
36199566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(dE + 1, &moduli));
36200139fca9SMatthew G. Knepley   moduli[0] = dir;
3621cdaaecf7SMatthew G. Knepley   for (d = 0, e = 0; d < dE; ++d) moduli[d + 1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0);
36229566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
36239566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
36249566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(obj, &id));
36250139fca9SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
36260139fca9SMatthew G. Knepley     Vec          lCoords;
36270139fca9SMatthew G. Knepley     PetscSection cSection;
36280139fca9SMatthew G. Knepley     PetscScalar *coords;
36290139fca9SMatthew G. Knepley     PetscInt     vStart, vEnd, v;
36300139fca9SMatthew G. Knepley 
36319566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
36329566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cSection));
36339566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
36349566063dSJacob Faibussowitsch     PetscCall(VecGetArray(lCoords, &coords));
36350139fca9SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
36360139fca9SMatthew G. Knepley       PetscReal ds;
36370139fca9SMatthew G. Knepley       PetscInt  off, c;
36380139fca9SMatthew G. Knepley 
36399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(cSection, v, &off));
36400139fca9SMatthew G. Knepley       ds = PetscRealPart(coords[off + dir]);
36410139fca9SMatthew G. Knepley       for (c = 0; c < dE; ++c) coords[off + c] += moduli[c] * ds;
36420139fca9SMatthew G. Knepley     }
36439566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(lCoords, &coords));
36440139fca9SMatthew G. Knepley   } else {
36459566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, dE + 1, moduli));
36469566063dSJacob Faibussowitsch     PetscCall(DMPlexRemapGeometry(dm, 0.0, f0_shear));
36470139fca9SMatthew G. Knepley   }
36489566063dSJacob Faibussowitsch   PetscCall(PetscFree(moduli));
36490139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
36500139fca9SMatthew G. Knepley }
3651