xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision d8206211e04f62f2d49dcc8cb83d25d5854cb079)
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 @*/
369371c9d4SSatish Balay PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points) {
3737900f7dSMatthew G. Knepley   PetscInt           c, cdim, i, j, o, p, vStart, vEnd;
38d3e1f4ccSVaclav Hapla   PetscInt           npoints;
39d3e1f4ccSVaclav Hapla   const PetscScalar *coord;
403985bb02SVaclav Hapla   Vec                allCoordsVec;
413985bb02SVaclav Hapla   const PetscScalar *allCoords;
42d3e1f4ccSVaclav Hapla   PetscInt          *dagPoints;
433985bb02SVaclav Hapla 
443985bb02SVaclav Hapla   PetscFunctionBegin;
453985bb02SVaclav Hapla   if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON;
469566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
47d3e1f4ccSVaclav Hapla   {
48d3e1f4ccSVaclav Hapla     PetscInt n;
49d3e1f4ccSVaclav Hapla 
509566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(coordinates, &n));
5163a3b9bcSJacob 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);
52d3e1f4ccSVaclav Hapla     npoints = n / cdim;
53d3e1f4ccSVaclav Hapla   }
549566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &allCoordsVec));
559566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(allCoordsVec, &allCoords));
569566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coord));
579566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
5876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
59335ef845SVaclav Hapla     /* check coordinate section is consistent with DM dimension */
60335ef845SVaclav Hapla     PetscSection cs;
61335ef845SVaclav Hapla     PetscInt     ndof;
62335ef845SVaclav Hapla 
639566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cs));
643985bb02SVaclav Hapla     for (p = vStart; p < vEnd; p++) {
659566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cs, p, &ndof));
6663a3b9bcSJacob Faibussowitsch       PetscCheck(ndof == cdim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %" PetscInt_FMT ": ndof = %" PetscInt_FMT " != %" PetscInt_FMT " = cdim", p, ndof, cdim);
67335ef845SVaclav Hapla     }
68335ef845SVaclav Hapla   }
699566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npoints, &dagPoints));
70eca9f518SVaclav Hapla   if (eps == 0.0) {
7137900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
72eca9f518SVaclav Hapla       dagPoints[i] = -1;
7337900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
7437900f7dSMatthew G. Knepley         for (c = 0; c < cdim; c++) {
75d3e1f4ccSVaclav Hapla           if (coord[j + c] != allCoords[o + c]) break;
76eca9f518SVaclav Hapla         }
7737900f7dSMatthew G. Knepley         if (c == cdim) {
78eca9f518SVaclav Hapla           dagPoints[i] = p;
79eca9f518SVaclav Hapla           break;
80eca9f518SVaclav Hapla         }
81eca9f518SVaclav Hapla       }
82eca9f518SVaclav Hapla     }
83d3e1f4ccSVaclav Hapla   } else {
8437900f7dSMatthew G. Knepley     for (i = 0, j = 0; i < npoints; i++, j += cdim) {
85d3e1f4ccSVaclav Hapla       PetscReal norm;
86d3e1f4ccSVaclav Hapla 
87335ef845SVaclav Hapla       dagPoints[i] = -1;
8837900f7dSMatthew G. Knepley       for (p = vStart, o = 0; p < vEnd; p++, o += cdim) {
893985bb02SVaclav Hapla         norm = 0.0;
909371c9d4SSatish Balay         for (c = 0; c < cdim; c++) { norm += PetscRealPart(PetscSqr(coord[j + c] - allCoords[o + c])); }
913985bb02SVaclav Hapla         norm = PetscSqrtReal(norm);
923985bb02SVaclav Hapla         if (norm <= eps) {
933985bb02SVaclav Hapla           dagPoints[i] = p;
943985bb02SVaclav Hapla           break;
953985bb02SVaclav Hapla         }
963985bb02SVaclav Hapla       }
973985bb02SVaclav Hapla     }
98d3e1f4ccSVaclav Hapla   }
999566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(allCoordsVec, &allCoords));
1009566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coord));
1019566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points));
1023985bb02SVaclav Hapla   PetscFunctionReturn(0);
1033985bb02SVaclav Hapla }
1043985bb02SVaclav Hapla 
1059371c9d4SSatish Balay static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection) {
106fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0 * 2 + 0];
107fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0 * 2 + 1];
108fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1 * 2 + 0];
109fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1 * 2 + 1];
110fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0 * 2 + 0];
111fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0 * 2 + 1];
112fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1 * 2 + 0];
113fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1 * 2 + 1];
114fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
115fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
116fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
117fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
118fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
119fea14342SMatthew G. Knepley 
120fea14342SMatthew G. Knepley   PetscFunctionBegin;
121fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
122fea14342SMatthew G. Knepley   /* Non-parallel lines */
123fea14342SMatthew G. Knepley   if (denom != 0.0) {
124fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
125fea14342SMatthew G. Knepley     const PetscReal t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
126fea14342SMatthew G. Knepley 
127fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
128fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
129fea14342SMatthew G. Knepley       if (intersection) {
130fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
131fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
132fea14342SMatthew G. Knepley       }
133fea14342SMatthew G. Knepley     }
134fea14342SMatthew G. Knepley   }
135fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
136fea14342SMatthew G. Knepley }
137fea14342SMatthew G. Knepley 
138ddce0771SMatthew G. Knepley /* The plane is segmentB x segmentC: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */
1399371c9d4SSatish Balay static PetscErrorCode DMPlexGetLinePlaneIntersection_3D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], const PetscReal segmentC[], PetscReal intersection[], PetscBool *hasIntersection) {
140ddce0771SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0 * 3 + 0];
141ddce0771SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0 * 3 + 1];
142ddce0771SMatthew G. Knepley   const PetscReal p0_z  = segmentA[0 * 3 + 2];
143ddce0771SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1 * 3 + 0];
144ddce0771SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1 * 3 + 1];
145ddce0771SMatthew G. Knepley   const PetscReal p1_z  = segmentA[1 * 3 + 2];
146ddce0771SMatthew G. Knepley   const PetscReal q0_x  = segmentB[0 * 3 + 0];
147ddce0771SMatthew G. Knepley   const PetscReal q0_y  = segmentB[0 * 3 + 1];
148ddce0771SMatthew G. Knepley   const PetscReal q0_z  = segmentB[0 * 3 + 2];
149ddce0771SMatthew G. Knepley   const PetscReal q1_x  = segmentB[1 * 3 + 0];
150ddce0771SMatthew G. Knepley   const PetscReal q1_y  = segmentB[1 * 3 + 1];
151ddce0771SMatthew G. Knepley   const PetscReal q1_z  = segmentB[1 * 3 + 2];
152ddce0771SMatthew G. Knepley   const PetscReal r0_x  = segmentC[0 * 3 + 0];
153ddce0771SMatthew G. Knepley   const PetscReal r0_y  = segmentC[0 * 3 + 1];
154ddce0771SMatthew G. Knepley   const PetscReal r0_z  = segmentC[0 * 3 + 2];
155ddce0771SMatthew G. Knepley   const PetscReal r1_x  = segmentC[1 * 3 + 0];
156ddce0771SMatthew G. Knepley   const PetscReal r1_y  = segmentC[1 * 3 + 1];
157ddce0771SMatthew G. Knepley   const PetscReal r1_z  = segmentC[1 * 3 + 2];
158ddce0771SMatthew G. Knepley   const PetscReal s0_x  = p1_x - p0_x;
159ddce0771SMatthew G. Knepley   const PetscReal s0_y  = p1_y - p0_y;
160ddce0771SMatthew G. Knepley   const PetscReal s0_z  = p1_z - p0_z;
161ddce0771SMatthew G. Knepley   const PetscReal s1_x  = q1_x - q0_x;
162ddce0771SMatthew G. Knepley   const PetscReal s1_y  = q1_y - q0_y;
163ddce0771SMatthew G. Knepley   const PetscReal s1_z  = q1_z - q0_z;
164ddce0771SMatthew G. Knepley   const PetscReal s2_x  = r1_x - r0_x;
165ddce0771SMatthew G. Knepley   const PetscReal s2_y  = r1_y - r0_y;
166ddce0771SMatthew G. Knepley   const PetscReal s2_z  = r1_z - r0_z;
167ddce0771SMatthew G. Knepley   const PetscReal s3_x  = s1_y * s2_z - s1_z * s2_y; /* s1 x s2 */
168ddce0771SMatthew G. Knepley   const PetscReal s3_y  = s1_z * s2_x - s1_x * s2_z;
169ddce0771SMatthew G. Knepley   const PetscReal s3_z  = s1_x * s2_y - s1_y * s2_x;
170ddce0771SMatthew G. Knepley   const PetscReal s4_x  = s0_y * s2_z - s0_z * s2_y; /* s0 x s2 */
171ddce0771SMatthew G. Knepley   const PetscReal s4_y  = s0_z * s2_x - s0_x * s2_z;
172ddce0771SMatthew G. Knepley   const PetscReal s4_z  = s0_x * s2_y - s0_y * s2_x;
173ddce0771SMatthew G. Knepley   const PetscReal s5_x  = s1_y * s0_z - s1_z * s0_y; /* s1 x s0 */
174ddce0771SMatthew G. Knepley   const PetscReal s5_y  = s1_z * s0_x - s1_x * s0_z;
175ddce0771SMatthew G. Knepley   const PetscReal s5_z  = s1_x * s0_y - s1_y * s0_x;
176ddce0771SMatthew G. Knepley   const PetscReal denom = -(s0_x * s3_x + s0_y * s3_y + s0_z * s3_z); /* -s0 . (s1 x s2) */
177ddce0771SMatthew G. Knepley 
178ddce0771SMatthew G. Knepley   PetscFunctionBegin;
179ddce0771SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
180ddce0771SMatthew G. Knepley   /* Line not parallel to plane */
181ddce0771SMatthew G. Knepley   if (denom != 0.0) {
182ddce0771SMatthew G. Knepley     const PetscReal t = (s3_x * (p0_x - q0_x) + s3_y * (p0_y - q0_y) + s3_z * (p0_z - q0_z)) / denom;
183ddce0771SMatthew G. Knepley     const PetscReal u = (s4_x * (p0_x - q0_x) + s4_y * (p0_y - q0_y) + s4_z * (p0_z - q0_z)) / denom;
184ddce0771SMatthew G. Knepley     const PetscReal v = (s5_x * (p0_x - q0_x) + s5_y * (p0_y - q0_y) + s5_z * (p0_z - q0_z)) / denom;
185ddce0771SMatthew G. Knepley 
186ddce0771SMatthew G. Knepley     if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && v >= 0 && v <= 1) {
187ddce0771SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
188ddce0771SMatthew G. Knepley       if (intersection) {
189ddce0771SMatthew G. Knepley         intersection[0] = p0_x + (t * s0_x);
190ddce0771SMatthew G. Knepley         intersection[1] = p0_y + (t * s0_y);
191ddce0771SMatthew G. Knepley         intersection[2] = p0_z + (t * s0_z);
192ddce0771SMatthew G. Knepley       }
193ddce0771SMatthew G. Knepley     }
194ddce0771SMatthew G. Knepley   }
195ddce0771SMatthew G. Knepley   PetscFunctionReturn(0);
196ddce0771SMatthew G. Knepley }
197ddce0771SMatthew G. Knepley 
1989371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) {
19914bbb9f0SLawrence Mitchell   const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON;
20014bbb9f0SLawrence Mitchell   const PetscReal x   = PetscRealPart(point[0]);
20114bbb9f0SLawrence Mitchell   PetscReal       v0, J, invJ, detJ;
20214bbb9f0SLawrence Mitchell   PetscReal       xi;
20314bbb9f0SLawrence Mitchell 
20414bbb9f0SLawrence Mitchell   PetscFunctionBegin;
2059566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ));
20614bbb9f0SLawrence Mitchell   xi = invJ * (x - v0);
20714bbb9f0SLawrence Mitchell 
20814bbb9f0SLawrence Mitchell   if ((xi >= -eps) && (xi <= 2. + eps)) *cell = c;
20914bbb9f0SLawrence Mitchell   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
21014bbb9f0SLawrence Mitchell   PetscFunctionReturn(0);
21114bbb9f0SLawrence Mitchell }
21214bbb9f0SLawrence Mitchell 
2139371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) {
214ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
215f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
216ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
217ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
218ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
219ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
220ccd2543fSMatthew G Knepley 
221ccd2543fSMatthew G Knepley   PetscFunctionBegin;
2229566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
223ccd2543fSMatthew G Knepley   xi  = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]);
224ccd2543fSMatthew G Knepley   eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]);
225ccd2543fSMatthew G Knepley 
226f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0 + eps)) *cell = c;
227c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
228ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
229ccd2543fSMatthew G Knepley }
230ccd2543fSMatthew G Knepley 
2319371c9d4SSatish Balay static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[]) {
23262a38674SMatthew G. Knepley   const PetscInt embedDim = 2;
23362a38674SMatthew G. Knepley   PetscReal      x        = PetscRealPart(point[0]);
23462a38674SMatthew G. Knepley   PetscReal      y        = PetscRealPart(point[1]);
23562a38674SMatthew G. Knepley   PetscReal      v0[2], J[4], invJ[4], detJ;
23662a38674SMatthew G. Knepley   PetscReal      xi, eta, r;
23762a38674SMatthew G. Knepley 
23862a38674SMatthew G. Knepley   PetscFunctionBegin;
2399566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
24062a38674SMatthew G. Knepley   xi  = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]);
24162a38674SMatthew G. Knepley   eta = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]);
24262a38674SMatthew G. Knepley 
24362a38674SMatthew G. Knepley   xi  = PetscMax(xi, 0.0);
24462a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
24562a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
24662a38674SMatthew G. Knepley     r = (xi + eta) / 2.0;
24762a38674SMatthew G. Knepley     xi /= r;
24862a38674SMatthew G. Knepley     eta /= r;
24962a38674SMatthew G. Knepley   }
25062a38674SMatthew G. Knepley   cpoint[0] = J[0 * embedDim + 0] * xi + J[0 * embedDim + 1] * eta + v0[0];
25162a38674SMatthew G. Knepley   cpoint[1] = J[1 * embedDim + 0] * xi + J[1 * embedDim + 1] * eta + v0[1];
25262a38674SMatthew G. Knepley   PetscFunctionReturn(0);
25362a38674SMatthew G. Knepley }
25462a38674SMatthew G. Knepley 
2559371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) {
256ccd2543fSMatthew G Knepley   PetscSection   coordSection;
257ccd2543fSMatthew G Knepley   Vec            coordsLocal;
258a1e44745SMatthew G. Knepley   PetscScalar   *coords    = NULL;
259ccd2543fSMatthew G Knepley   const PetscInt faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
260ccd2543fSMatthew G Knepley   PetscReal      x         = PetscRealPart(point[0]);
261ccd2543fSMatthew G Knepley   PetscReal      y         = PetscRealPart(point[1]);
262ccd2543fSMatthew G Knepley   PetscInt       crossings = 0, f;
263ccd2543fSMatthew G Knepley 
264ccd2543fSMatthew G Knepley   PetscFunctionBegin;
2659566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
2669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
2679566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords));
268ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
269ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2 * f + 0] * 2 + 0]);
270ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2 * f + 0] * 2 + 1]);
271ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2 * f + 1] * 2 + 0]);
272ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2 * f + 1] * 2 + 1]);
273ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
274ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
275ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
276ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
277ccd2543fSMatthew G Knepley     if ((cond1 || cond2) && above) ++crossings;
278ccd2543fSMatthew G Knepley   }
279ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
280c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
2819566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords));
282ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
283ccd2543fSMatthew G Knepley }
284ccd2543fSMatthew G Knepley 
2859371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) {
286ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 3;
28737900f7dSMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
288ccd2543fSMatthew G Knepley   PetscReal       v0[3], J[9], invJ[9], detJ;
289ccd2543fSMatthew G Knepley   PetscReal       x = PetscRealPart(point[0]);
290ccd2543fSMatthew G Knepley   PetscReal       y = PetscRealPart(point[1]);
291ccd2543fSMatthew G Knepley   PetscReal       z = PetscRealPart(point[2]);
292ccd2543fSMatthew G Knepley   PetscReal       xi, eta, zeta;
293ccd2543fSMatthew G Knepley 
294ccd2543fSMatthew G Knepley   PetscFunctionBegin;
2959566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
296ccd2543fSMatthew G Knepley   xi   = invJ[0 * embedDim + 0] * (x - v0[0]) + invJ[0 * embedDim + 1] * (y - v0[1]) + invJ[0 * embedDim + 2] * (z - v0[2]);
297ccd2543fSMatthew G Knepley   eta  = invJ[1 * embedDim + 0] * (x - v0[0]) + invJ[1 * embedDim + 1] * (y - v0[1]) + invJ[1 * embedDim + 2] * (z - v0[2]);
298ccd2543fSMatthew G Knepley   zeta = invJ[2 * embedDim + 0] * (x - v0[0]) + invJ[2 * embedDim + 1] * (y - v0[1]) + invJ[2 * embedDim + 2] * (z - v0[2]);
299ccd2543fSMatthew G Knepley 
30037900f7dSMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0 + eps)) *cell = c;
301c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
302ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
303ccd2543fSMatthew G Knepley }
304ccd2543fSMatthew G Knepley 
3059371c9d4SSatish Balay static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) {
306ccd2543fSMatthew G Knepley   PetscSection   coordSection;
307ccd2543fSMatthew G Knepley   Vec            coordsLocal;
308872a9804SMatthew G. Knepley   PetscScalar   *coords    = NULL;
3099371c9d4SSatish 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};
310ccd2543fSMatthew G Knepley   PetscBool      found     = PETSC_TRUE;
311ccd2543fSMatthew G Knepley   PetscInt       f;
312ccd2543fSMatthew G Knepley 
313ccd2543fSMatthew G Knepley   PetscFunctionBegin;
3149566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
3159566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
3169566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords));
317ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
318ccd2543fSMatthew G Knepley     /* Check the point is under plane */
319ccd2543fSMatthew G Knepley     /*   Get face normal */
320ccd2543fSMatthew G Knepley     PetscReal v_i[3];
321ccd2543fSMatthew G Knepley     PetscReal v_j[3];
322ccd2543fSMatthew G Knepley     PetscReal normal[3];
323ccd2543fSMatthew G Knepley     PetscReal pp[3];
324ccd2543fSMatthew G Knepley     PetscReal dot;
325ccd2543fSMatthew G Knepley 
326ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]);
327ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]);
328ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f * 4 + 3] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]);
329ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 0] - coords[faces[f * 4 + 0] * 3 + 0]);
330ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 1] - coords[faces[f * 4 + 0] * 3 + 1]);
331ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f * 4 + 1] * 3 + 2] - coords[faces[f * 4 + 0] * 3 + 2]);
332ccd2543fSMatthew G Knepley     normal[0] = v_i[1] * v_j[2] - v_i[2] * v_j[1];
333ccd2543fSMatthew G Knepley     normal[1] = v_i[2] * v_j[0] - v_i[0] * v_j[2];
334ccd2543fSMatthew G Knepley     normal[2] = v_i[0] * v_j[1] - v_i[1] * v_j[0];
335ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 0] - point[0]);
336ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 1] - point[1]);
337ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f * 4 + 0] * 3 + 2] - point[2]);
338ccd2543fSMatthew G Knepley     dot       = normal[0] * pp[0] + normal[1] * pp[1] + normal[2] * pp[2];
339ccd2543fSMatthew G Knepley 
340ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
341ccd2543fSMatthew G Knepley     if (dot < 0.0) {
342ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
343ccd2543fSMatthew G Knepley       break;
344ccd2543fSMatthew G Knepley     }
345ccd2543fSMatthew G Knepley   }
346ccd2543fSMatthew G Knepley   if (found) *cell = c;
347c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
3489566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords));
349ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
350ccd2543fSMatthew G Knepley }
351ccd2543fSMatthew G Knepley 
3529371c9d4SSatish Balay static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[]) {
353c4eade1cSMatthew G. Knepley   PetscInt d;
354c4eade1cSMatthew G. Knepley 
355c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
356c4eade1cSMatthew G. Knepley   box->dim = dim;
357c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
358c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
359c4eade1cSMatthew G. Knepley }
360c4eade1cSMatthew G. Knepley 
3619371c9d4SSatish Balay PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box) {
362c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
3639566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, box));
3649566063dSJacob Faibussowitsch   PetscCall(PetscGridHashInitialize_Internal(*box, dim, point));
365c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
366c4eade1cSMatthew G. Knepley }
367c4eade1cSMatthew G. Knepley 
3689371c9d4SSatish Balay PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[]) {
369c4eade1cSMatthew G. Knepley   PetscInt d;
370c4eade1cSMatthew G. Knepley 
371c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
372c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
373c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
374c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
375c4eade1cSMatthew G. Knepley   }
376c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
377c4eade1cSMatthew G. Knepley }
378c4eade1cSMatthew G. Knepley 
37962a38674SMatthew G. Knepley /*
38062a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
38162a38674SMatthew G. Knepley 
38262a38674SMatthew G. Knepley   Not collective
38362a38674SMatthew G. Knepley 
38462a38674SMatthew G. Knepley   Input Parameters:
38562a38674SMatthew G. Knepley + box - The grid hash object
38662a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
38762a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
38862a38674SMatthew G. Knepley 
38962a38674SMatthew G. Knepley   Level: developer
39062a38674SMatthew G. Knepley 
391db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`
39262a38674SMatthew G. Knepley */
3939371c9d4SSatish Balay PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[]) {
394c4eade1cSMatthew G. Knepley   PetscInt d;
395c4eade1cSMatthew G. Knepley 
396c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
397c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
398c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
399c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
400c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
401c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d] / h[d]);
402c4eade1cSMatthew G. Knepley     } else {
403c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
404c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d] / n[d];
405c4eade1cSMatthew G. Knepley     }
406c4eade1cSMatthew G. Knepley   }
407c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
408c4eade1cSMatthew G. Knepley }
409c4eade1cSMatthew G. Knepley 
41062a38674SMatthew G. Knepley /*
41162a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
41262a38674SMatthew G. Knepley 
41362a38674SMatthew G. Knepley   Not collective
41462a38674SMatthew G. Knepley 
41562a38674SMatthew G. Knepley   Input Parameters:
41662a38674SMatthew G. Knepley + box       - The grid hash object
41762a38674SMatthew G. Knepley . numPoints - The number of input points
41862a38674SMatthew G. Knepley - points    - The input point coordinates
41962a38674SMatthew G. Knepley 
42062a38674SMatthew G. Knepley   Output Parameters:
42162a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
42262a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
42362a38674SMatthew G. Knepley 
42462a38674SMatthew G. Knepley   Level: developer
42562a38674SMatthew G. Knepley 
426f5867de0SMatthew G. Knepley   Note:
427f5867de0SMatthew G. Knepley   This only guarantees that a box contains a point, not that a cell does.
428f5867de0SMatthew G. Knepley 
429db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`
43062a38674SMatthew G. Knepley */
4319371c9d4SSatish Balay PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[]) {
432c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
433c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
434c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
435c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
436c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
437c4eade1cSMatthew G. Knepley   PetscInt         d, p;
438c4eade1cSMatthew G. Knepley 
439c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
440c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
441c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4421c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]);
443c4eade1cSMatthew G. Knepley 
4441c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1;
4452a705cacSMatthew G. Knepley       if (dbox == -1 && PetscAbsReal(PetscRealPart(points[p * dim + d]) - lower[d]) < 1.0e-9) dbox = 0;
4469371c9d4SSatish 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);
447c4eade1cSMatthew G. Knepley       dboxes[p * dim + d] = dbox;
448c4eade1cSMatthew G. Knepley     }
4499371c9d4SSatish Balay     if (boxes)
4509371c9d4SSatish 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];
451c4eade1cSMatthew G. Knepley   }
452c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
453c4eade1cSMatthew G. Knepley }
454c4eade1cSMatthew G. Knepley 
455af74b616SDave May /*
456af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
457af74b616SDave May 
458af74b616SDave May  Not collective
459af74b616SDave May 
460af74b616SDave May   Input Parameters:
461af74b616SDave May + box         - The grid hash object
462f5867de0SMatthew G. Knepley . cellSection - The PetscSection mapping cells to boxes
463af74b616SDave May . numPoints   - The number of input points
464af74b616SDave May - points      - The input point coordinates
465af74b616SDave May 
466af74b616SDave May   Output Parameters:
467af74b616SDave May + dboxes - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
468af74b616SDave May . boxes  - An array of numPoints integers expressing the enclosing box as single number, or NULL
469af74b616SDave May - found  - Flag indicating if point was located within a box
470af74b616SDave May 
471af74b616SDave May   Level: developer
472af74b616SDave May 
473f5867de0SMatthew G. Knepley   Note:
474f5867de0SMatthew 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.
475f5867de0SMatthew G. Knepley 
476db781477SPatrick Sanan .seealso: `PetscGridHashGetEnclosingBox()`
477af74b616SDave May */
4789371c9d4SSatish Balay PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscSection cellSection, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[], PetscBool *found) {
479af74b616SDave May   const PetscReal *lower = box->lower;
480af74b616SDave May   const PetscReal *upper = box->upper;
481af74b616SDave May   const PetscReal *h     = box->h;
482af74b616SDave May   const PetscInt  *n     = box->n;
483af74b616SDave May   const PetscInt   dim   = box->dim;
484f5867de0SMatthew G. Knepley   PetscInt         bStart, bEnd, d, p;
485af74b616SDave May 
486af74b616SDave May   PetscFunctionBegin;
487f5867de0SMatthew G. Knepley   PetscValidHeaderSpecific(cellSection, PETSC_SECTION_CLASSID, 2);
488af74b616SDave May   *found = PETSC_FALSE;
489f5867de0SMatthew G. Knepley   PetscCall(PetscSectionGetChart(box->cellSection, &bStart, &bEnd));
490af74b616SDave May   for (p = 0; p < numPoints; ++p) {
491af74b616SDave May     for (d = 0; d < dim; ++d) {
492af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p * dim + d]) - lower[d]) / h[d]);
493af74b616SDave May 
494af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p * dim + d]) - upper[d]) < 1.0e-9) dbox = n[d] - 1;
495f5867de0SMatthew G. Knepley       if (dbox < 0 || dbox >= n[d]) PetscFunctionReturn(0);
496af74b616SDave May       dboxes[p * dim + d] = dbox;
497af74b616SDave May     }
4989371c9d4SSatish Balay     if (boxes)
4999371c9d4SSatish 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];
500f5867de0SMatthew G. Knepley     // It is possible for a box to overlap no grid cells
501f5867de0SMatthew G. Knepley     if (boxes[p] < bStart || boxes[p] >= bEnd) PetscFunctionReturn(0);
502af74b616SDave May   }
503af74b616SDave May   *found = PETSC_TRUE;
504af74b616SDave May   PetscFunctionReturn(0);
505af74b616SDave May }
506af74b616SDave May 
5079371c9d4SSatish Balay PetscErrorCode PetscGridHashDestroy(PetscGridHash *box) {
508c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
509c4eade1cSMatthew G. Knepley   if (*box) {
5109566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&(*box)->cellSection));
5119566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&(*box)->cells));
5129566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&(*box)->cellsSparse));
513c4eade1cSMatthew G. Knepley   }
5149566063dSJacob Faibussowitsch   PetscCall(PetscFree(*box));
515c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
516c4eade1cSMatthew G. Knepley }
517c4eade1cSMatthew G. Knepley 
5189371c9d4SSatish Balay PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell) {
519ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
520cafe43deSMatthew G. Knepley 
521cafe43deSMatthew G. Knepley   PetscFunctionBegin;
5229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cellStart, &ct));
523ba2698f1SMatthew G. Knepley   switch (ct) {
5249371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: PetscCall(DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell)); break;
5259371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: PetscCall(DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell)); break;
5269371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: PetscCall(DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell)); break;
5279371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: PetscCall(DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell)); break;
5289371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: PetscCall(DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell)); break;
52963a3b9bcSJacob Faibussowitsch   default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %" PetscInt_FMT " with type %s", cellStart, DMPolytopeTypes[ct]);
530cafe43deSMatthew G. Knepley   }
531cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
532cafe43deSMatthew G. Knepley }
533cafe43deSMatthew G. Knepley 
53462a38674SMatthew G. Knepley /*
53562a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
53662a38674SMatthew G. Knepley */
5379371c9d4SSatish Balay PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[]) {
538ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
53962a38674SMatthew G. Knepley 
54062a38674SMatthew G. Knepley   PetscFunctionBegin;
5419566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
542ba2698f1SMatthew G. Knepley   switch (ct) {
5439371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: PetscCall(DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint)); break;
54462a38674SMatthew G. Knepley #if 0
545ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
5469566063dSJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint));break;
547ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
5489566063dSJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint));break;
549ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
5509566063dSJacob Faibussowitsch     PetscCall(DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint));break;
55162a38674SMatthew G. Knepley #endif
55263a3b9bcSJacob Faibussowitsch   default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %" PetscInt_FMT " with type %s", cell, DMPolytopeTypes[ct]);
55362a38674SMatthew G. Knepley   }
55462a38674SMatthew G. Knepley   PetscFunctionReturn(0);
55562a38674SMatthew G. Knepley }
55662a38674SMatthew G. Knepley 
55762a38674SMatthew G. Knepley /*
55862a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
55962a38674SMatthew G. Knepley 
560d083f849SBarry Smith   Collective on dm
56162a38674SMatthew G. Knepley 
56262a38674SMatthew G. Knepley   Input Parameter:
56362a38674SMatthew G. Knepley . dm - The Plex
56462a38674SMatthew G. Knepley 
56562a38674SMatthew G. Knepley   Output Parameter:
56662a38674SMatthew G. Knepley . localBox - The grid hash object
56762a38674SMatthew G. Knepley 
56862a38674SMatthew G. Knepley   Level: developer
56962a38674SMatthew G. Knepley 
570db781477SPatrick Sanan .seealso: `PetscGridHashCreate()`, `PetscGridHashGetEnclosingBox()`
57162a38674SMatthew G. Knepley */
5729371c9d4SSatish Balay PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox) {
573f5867de0SMatthew G. Knepley   PetscInt           debug = ((DM_Plex *)dm->data)->printLocate;
574cafe43deSMatthew G. Knepley   MPI_Comm           comm;
575cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
576cafe43deSMatthew G. Knepley   Vec                coordinates;
577cafe43deSMatthew G. Knepley   PetscSection       coordSection;
578cafe43deSMatthew G. Knepley   Vec                coordsLocal;
579cafe43deSMatthew G. Knepley   const PetscScalar *coords;
580ddce0771SMatthew G. Knepley   PetscScalar       *edgeCoords;
581722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
582ddce0771SMatthew G. Knepley   PetscInt           n[3] = {2, 2, 2};
583ddce0771SMatthew G. Knepley   PetscInt           dim, N, maxConeSize, cStart, cEnd, c, eStart, eEnd, i;
584ddce0771SMatthew G. Knepley   PetscBool          flg;
585cafe43deSMatthew G. Knepley 
586cafe43deSMatthew G. Knepley   PetscFunctionBegin;
5879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
5899566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
5909566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
5919566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
5929566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(coordinates, &N));
5939566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
5949566063dSJacob Faibussowitsch   PetscCall(PetscGridHashCreate(comm, dim, coords, &lbox));
5959566063dSJacob Faibussowitsch   for (i = 0; i < N; i += dim) PetscCall(PetscGridHashEnlarge(lbox, &coords[i]));
5969566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
597ddce0771SMatthew G. Knepley   c = dim;
5989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, "-dm_plex_hash_box_faces", n, &c, &flg));
5999371c9d4SSatish Balay   if (flg) {
6009371c9d4SSatish Balay     for (i = c; i < dim; ++i) n[i] = n[c - 1];
6019371c9d4SSatish Balay   } else {
6029371c9d4SSatish Balay     for (i = 0; i < dim; ++i) n[i] = PetscMax(2, PetscFloorReal(PetscPowReal((PetscReal)(cEnd - cStart), 1.0 / dim) * 0.8));
6039371c9d4SSatish Balay   }
6049566063dSJacob Faibussowitsch   PetscCall(PetscGridHashSetGrid(lbox, n, NULL));
6059371c9d4SSatish Balay   if (debug)
6069371c9d4SSatish 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],
6079371c9d4SSatish 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]));
608cafe43deSMatthew G. Knepley #if 0
609cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
6101c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm));
6111c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm));
612cafe43deSMatthew G. Knepley #endif
613cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
614cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
615cafe43deSMatthew G. Knepley   /* Create label */
6169566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
617b26b5bf9SMatthew G. Knepley   if (dim < 2) eStart = eEnd = -1;
6189566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse));
6199566063dSJacob Faibussowitsch   PetscCall(DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd));
620a8d69d7bSBarry Smith   /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
6219566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
6229566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6239566063dSJacob Faibussowitsch   PetscCall(PetscCalloc3(16 * dim, &dboxes, 16, &boxes, PetscPowInt(maxConeSize, dim) * dim, &edgeCoords));
624cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
625cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
626cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
62738353de4SMatthew G. Knepley     PetscInt         csize   = 0;
628ddce0771SMatthew G. Knepley     PetscInt        *closure = NULL;
629ddce0771SMatthew G. Knepley     PetscInt         Ncl, cl, Ne = 0;
630cafe43deSMatthew G. Knepley     PetscScalar      point[3];
631cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
632cafe43deSMatthew G. Knepley 
633ddce0771SMatthew G. Knepley     /* Get all edges in cell */
6349566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure));
635ddce0771SMatthew G. Knepley     for (cl = 0; cl < Ncl * 2; ++cl) {
636ddce0771SMatthew G. Knepley       if ((closure[cl] >= eStart) && (closure[cl] < eEnd)) {
637ddce0771SMatthew G. Knepley         PetscScalar *ecoords = &edgeCoords[Ne * dim * 2];
638ddce0771SMatthew G. Knepley         PetscInt     ecsize  = dim * 2;
639ddce0771SMatthew G. Knepley 
6409566063dSJacob Faibussowitsch         PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, closure[cl], &ecsize, &ecoords));
64163a3b9bcSJacob 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);
642ddce0771SMatthew G. Knepley         ++Ne;
643ddce0771SMatthew G. Knepley       }
644ddce0771SMatthew G. Knepley     }
6459566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure));
646cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
6479566063dSJacob Faibussowitsch     PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords));
6489566063dSJacob Faibussowitsch     PetscCall(PetscGridHashGetEnclosingBox(lbox, csize / dim, ccoords, dboxes, boxes));
649722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
650ddce0771SMatthew G. Knepley     for (e = 0; e < csize / dim; ++e) {
6519371c9d4SSatish Balay       if (debug)
6529371c9d4SSatish Balay         PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " has vertex in box %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ")\n", c, boxes[e], dboxes[e * dim + 0], dim > 1 ? dboxes[e * dim + 1] : -1, dim > 2 ? dboxes[e * dim + 2] : -1));
6539566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(lbox->cellsSparse, c, boxes[e]));
654ddce0771SMatthew G. Knepley     }
655cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
656cafe43deSMatthew G. Knepley     for (d = 0; d < dim; ++d) { dlim[d * 2 + 0] = dlim[d * 2 + 1] = dboxes[d]; }
6572291669eSMatthew G. Knepley     for (d = dim; d < 3; ++d) { dlim[d * 2 + 0] = dlim[d * 2 + 1] = 0; }
658cafe43deSMatthew G. Knepley     for (e = 1; e < dim + 1; ++e) {
659cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
660cafe43deSMatthew G. Knepley         dlim[d * 2 + 0] = PetscMin(dlim[d * 2 + 0], dboxes[e * dim + d]);
661cafe43deSMatthew G. Knepley         dlim[d * 2 + 1] = PetscMax(dlim[d * 2 + 1], dboxes[e * dim + d]);
662cafe43deSMatthew G. Knepley       }
663cafe43deSMatthew G. Knepley     }
664fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
665cafe43deSMatthew 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]) {
666cafe43deSMatthew 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]) {
667cafe43deSMatthew 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]) {
668cafe43deSMatthew G. Knepley           const PetscInt box = (k * lbox->n[1] + j) * lbox->n[0] + i;
669cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
670fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
671cafe43deSMatthew G. Knepley 
6729371c9d4SSatish Balay           if (debug)
6739371c9d4SSatish 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])));
674ddce0771SMatthew G. Knepley           /* Check whether cell contains any vertex of this subbox TODO vectorize this */
675cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
676cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
677cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
6789566063dSJacob Faibussowitsch                 PetscCall(DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell));
6790b6bfacdSStefano Zampini                 if (cell >= 0) {
6809371c9d4SSatish Balay                   if (debug)
6819371c9d4SSatish 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));
6829566063dSJacob Faibussowitsch                   PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box));
6830b6bfacdSStefano Zampini                   jj = kk = 2;
6840b6bfacdSStefano Zampini                   break;
6850b6bfacdSStefano Zampini                 }
686cafe43deSMatthew G. Knepley               }
687cafe43deSMatthew G. Knepley             }
688cafe43deSMatthew G. Knepley           }
689ddce0771SMatthew G. Knepley           /* Check whether cell edge intersects any face of these subboxes TODO vectorize this */
690ddce0771SMatthew G. Knepley           for (edge = 0; edge < Ne; ++edge) {
691a5cae605SSatish Balay             PetscReal segA[6] = {0., 0., 0., 0., 0., 0.};
692a5cae605SSatish Balay             PetscReal segB[6] = {0., 0., 0., 0., 0., 0.};
693a5cae605SSatish Balay             PetscReal segC[6] = {0., 0., 0., 0., 0., 0.};
694fea14342SMatthew G. Knepley 
69563a3b9bcSJacob Faibussowitsch             PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected dim %" PetscInt_FMT " > 3", dim);
696ddce0771SMatthew G. Knepley             for (d = 0; d < dim * 2; ++d) segA[d] = PetscRealPart(edgeCoords[edge * dim * 2 + d]);
697ddce0771SMatthew G. Knepley             /* 1D: (x) -- (x+h)               0 -- 1
698ddce0771SMatthew G. Knepley                2D: (x,   y)   -- (x,   y+h)   (0, 0) -- (0, 1)
699ddce0771SMatthew G. Knepley                    (x+h, y)   -- (x+h, y+h)   (1, 0) -- (1, 1)
700ddce0771SMatthew G. Knepley                    (x,   y)   -- (x+h, y)     (0, 0) -- (1, 0)
701ddce0771SMatthew G. Knepley                    (x,   y+h) -- (x+h, y+h)   (0, 1) -- (1, 1)
702ddce0771SMatthew 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)
703ddce0771SMatthew 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)
704ddce0771SMatthew 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)
705ddce0771SMatthew 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)
706ddce0771SMatthew 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)
707ddce0771SMatthew 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)
708ddce0771SMatthew G. Knepley              */
709ddce0771SMatthew G. Knepley             /* Loop over faces with normal in direction d */
710ddce0771SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
711ddce0771SMatthew G. Knepley               PetscBool intersects = PETSC_FALSE;
712ddce0771SMatthew G. Knepley               PetscInt  e          = (d + 1) % dim;
713ddce0771SMatthew G. Knepley               PetscInt  f          = (d + 2) % dim;
714ddce0771SMatthew G. Knepley 
715ddce0771SMatthew G. Knepley               /* There are two faces in each dimension */
716ddce0771SMatthew G. Knepley               for (ii = 0; ii < 2; ++ii) {
717ddce0771SMatthew G. Knepley                 segB[d]       = PetscRealPart(point[d] + ii * h[d]);
718ddce0771SMatthew G. Knepley                 segB[dim + d] = PetscRealPart(point[d] + ii * h[d]);
719ddce0771SMatthew G. Knepley                 segC[d]       = PetscRealPart(point[d] + ii * h[d]);
720ddce0771SMatthew G. Knepley                 segC[dim + d] = PetscRealPart(point[d] + ii * h[d]);
721ddce0771SMatthew G. Knepley                 if (dim > 1) {
722ddce0771SMatthew G. Knepley                   segB[e]       = PetscRealPart(point[e] + 0 * h[e]);
723ddce0771SMatthew G. Knepley                   segB[dim + e] = PetscRealPart(point[e] + 1 * h[e]);
724ddce0771SMatthew G. Knepley                   segC[e]       = PetscRealPart(point[e] + 0 * h[e]);
725ddce0771SMatthew G. Knepley                   segC[dim + e] = PetscRealPart(point[e] + 0 * h[e]);
726ddce0771SMatthew G. Knepley                 }
727ddce0771SMatthew G. Knepley                 if (dim > 2) {
728ddce0771SMatthew G. Knepley                   segB[f]       = PetscRealPart(point[f] + 0 * h[f]);
729ddce0771SMatthew G. Knepley                   segB[dim + f] = PetscRealPart(point[f] + 0 * h[f]);
730ddce0771SMatthew G. Knepley                   segC[f]       = PetscRealPart(point[f] + 0 * h[f]);
731ddce0771SMatthew G. Knepley                   segC[dim + f] = PetscRealPart(point[f] + 1 * h[f]);
732ddce0771SMatthew G. Knepley                 }
733ddce0771SMatthew G. Knepley                 if (dim == 2) {
7349566063dSJacob Faibussowitsch                   PetscCall(DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects));
735ddce0771SMatthew G. Knepley                 } else if (dim == 3) {
7369566063dSJacob Faibussowitsch                   PetscCall(DMPlexGetLinePlaneIntersection_3D_Internal(segA, segB, segC, NULL, &intersects));
737ddce0771SMatthew G. Knepley                 }
738ddce0771SMatthew G. Knepley                 if (intersects) {
7399371c9d4SSatish Balay                   if (debug)
7409371c9d4SSatish 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]));
7419371c9d4SSatish Balay                   PetscCall(DMLabelSetValue(lbox->cellsSparse, c, box));
7429371c9d4SSatish Balay                   edge = Ne;
7439371c9d4SSatish Balay                   break;
744ddce0771SMatthew G. Knepley                 }
745ddce0771SMatthew G. Knepley               }
746ddce0771SMatthew G. Knepley             }
747cafe43deSMatthew G. Knepley           }
748fea14342SMatthew G. Knepley         }
749fea14342SMatthew G. Knepley       }
750fea14342SMatthew G. Knepley     }
7519566063dSJacob Faibussowitsch     PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords));
752fea14342SMatthew G. Knepley   }
7539566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dboxes, boxes, edgeCoords));
7549566063dSJacob Faibussowitsch   if (debug) PetscCall(DMLabelView(lbox->cellsSparse, PETSC_VIEWER_STDOUT_SELF));
7559566063dSJacob Faibussowitsch   PetscCall(DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells));
7569566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&lbox->cellsSparse));
757cafe43deSMatthew G. Knepley   *localBox = lbox;
758cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
759cafe43deSMatthew G. Knepley }
760cafe43deSMatthew G. Knepley 
7619371c9d4SSatish Balay PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF) {
762f5867de0SMatthew G. Knepley   PetscInt        debug = ((DM_Plex *)dm->data)->printLocate;
763cafe43deSMatthew G. Knepley   DM_Plex        *mesh  = (DM_Plex *)dm->data;
764af74b616SDave May   PetscBool       hash = mesh->useHashLocation, reuse = PETSC_FALSE;
7653a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
766*d8206211SMatthew G. Knepley   PetscInt        dim, Nl = 0, cStart, cEnd, numCells, c, d;
767*d8206211SMatthew G. Knepley   PetscSF         sf;
768*d8206211SMatthew G. Knepley   const PetscInt *leaves;
769cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
7703a93e3b7SToby Isaac   PetscSFNode    *cells;
771ccd2543fSMatthew G Knepley   PetscScalar    *a;
7723a93e3b7SToby Isaac   PetscMPIInt     result;
773af74b616SDave May   PetscLogDouble  t0, t1;
7749cb35068SDave May   PetscReal       gmin[3], gmax[3];
7759cb35068SDave May   PetscInt        terminating_query_type[] = {0, 0, 0};
776ccd2543fSMatthew G Knepley 
777ccd2543fSMatthew G Knepley   PetscFunctionBegin;
7789566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_LocatePoints, 0, 0, 0, 0));
7799566063dSJacob Faibussowitsch   PetscCall(PetscTime(&t0));
7801dca8a05SBarry 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.");
7819566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
7829566063dSJacob Faibussowitsch   PetscCall(VecGetBlockSize(v, &bs));
7839566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF), PETSC_COMM_SELF, &result));
7841dca8a05SBarry Smith   PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT, PetscObjectComm((PetscObject)cellSF), PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
78563a3b9bcSJacob 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);
7866858538eSMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalSetUp(dm));
7879566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
788*d8206211SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &sf));
789*d8206211SMatthew G. Knepley   if (sf) PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
790*d8206211SMatthew G. Knepley   Nl = PetscMax(Nl, 0);
7919566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(v, &numPoints));
7929566063dSJacob Faibussowitsch   PetscCall(VecGetArray(v, &a));
793ccd2543fSMatthew G Knepley   numPoints /= bs;
794af74b616SDave May   {
795af74b616SDave May     const PetscSFNode *sf_cells;
796af74b616SDave May 
7979566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(cellSF, NULL, NULL, NULL, &sf_cells));
798af74b616SDave May     if (sf_cells) {
7999566063dSJacob Faibussowitsch       PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Re-using existing StarForest node list\n"));
800af74b616SDave May       cells = (PetscSFNode *)sf_cells;
801af74b616SDave May       reuse = PETSC_TRUE;
802af74b616SDave May     } else {
8039566063dSJacob Faibussowitsch       PetscCall(PetscInfo(dm, "[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n"));
8049566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numPoints, &cells));
805af74b616SDave May       /* initialize cells if created */
806af74b616SDave May       for (p = 0; p < numPoints; p++) {
807af74b616SDave May         cells[p].rank  = 0;
808af74b616SDave May         cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
809af74b616SDave May       }
810af74b616SDave May     }
811af74b616SDave May   }
8129cb35068SDave May   /* define domain bounding box */
8139cb35068SDave May   {
8149cb35068SDave May     Vec coorglobal;
8159cb35068SDave May 
8169566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coorglobal));
8179566063dSJacob Faibussowitsch     PetscCall(VecStrideMaxAll(coorglobal, NULL, gmax));
8189566063dSJacob Faibussowitsch     PetscCall(VecStrideMinAll(coorglobal, NULL, gmin));
8199cb35068SDave May   }
820953fc75cSMatthew G. Knepley   if (hash) {
8219371c9d4SSatish Balay     if (!mesh->lbox) {
8229371c9d4SSatish Balay       PetscCall(PetscInfo(dm, "Initializing grid hashing"));
8239371c9d4SSatish Balay       PetscCall(DMPlexComputeGridHash_Internal(dm, &mesh->lbox));
8249371c9d4SSatish Balay     }
825cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
826cafe43deSMatthew G. Knepley     /* Send points to correct process */
827cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
828cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
8299566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(mesh->lbox->cells, &boxCells));
830953fc75cSMatthew G. Knepley   }
8313a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
832ccd2543fSMatthew G Knepley     const PetscScalar *point   = &a[p * bs];
833e56f9228SJed Brown     PetscInt           dbin[3] = {-1, -1, -1}, bin, cell = -1, cellOffset;
8349cb35068SDave May     PetscBool          point_outside_domain = PETSC_FALSE;
835ccd2543fSMatthew G Knepley 
8369cb35068SDave May     /* check bounding box of domain */
8379cb35068SDave May     for (d = 0; d < dim; d++) {
8389371c9d4SSatish Balay       if (PetscRealPart(point[d]) < gmin[d]) {
8399371c9d4SSatish Balay         point_outside_domain = PETSC_TRUE;
8409371c9d4SSatish Balay         break;
8419371c9d4SSatish Balay       }
8429371c9d4SSatish Balay       if (PetscRealPart(point[d]) > gmax[d]) {
8439371c9d4SSatish Balay         point_outside_domain = PETSC_TRUE;
8449371c9d4SSatish Balay         break;
8459371c9d4SSatish Balay       }
8469cb35068SDave May     }
8479cb35068SDave May     if (point_outside_domain) {
848e9b685f5SMatthew G. Knepley       cells[p].rank  = 0;
849e9b685f5SMatthew G. Knepley       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
8509cb35068SDave May       terminating_query_type[0]++;
8519cb35068SDave May       continue;
8529cb35068SDave May     }
853ccd2543fSMatthew G Knepley 
854af74b616SDave May     /* check initial values in cells[].index - abort early if found */
855af74b616SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
856af74b616SDave May       c              = cells[p].index;
8573a93e3b7SToby Isaac       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
8589566063dSJacob Faibussowitsch       PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell));
859af74b616SDave May       if (cell >= 0) {
860af74b616SDave May         cells[p].rank  = 0;
861af74b616SDave May         cells[p].index = cell;
862af74b616SDave May         numFound++;
863af74b616SDave May       }
864af74b616SDave May     }
8659cb35068SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
8669cb35068SDave May       terminating_query_type[1]++;
8679cb35068SDave May       continue;
8689cb35068SDave May     }
869af74b616SDave May 
870953fc75cSMatthew G. Knepley     if (hash) {
871af74b616SDave May       PetscBool found_box;
872af74b616SDave May 
87363a3b9bcSJacob 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])));
874af74b616SDave May       /* allow for case that point is outside box - abort early */
875f5867de0SMatthew G. Knepley       PetscCall(PetscGridHashGetEnclosingBoxQuery(mesh->lbox, mesh->lbox->cellSection, 1, point, dbin, &bin, &found_box));
876af74b616SDave May       if (found_box) {
87763a3b9bcSJacob 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]));
878cafe43deSMatthew G. Knepley         /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
8799566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells));
8809566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset));
881cafe43deSMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
88263a3b9bcSJacob Faibussowitsch           if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "    Checking for point in cell %" PetscInt_FMT "\n", boxCells[c]));
8839566063dSJacob Faibussowitsch           PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell));
8843a93e3b7SToby Isaac           if (cell >= 0) {
88563a3b9bcSJacob Faibussowitsch             if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "      FOUND in cell %" PetscInt_FMT "\n", cell));
8863a93e3b7SToby Isaac             cells[p].rank  = 0;
8873a93e3b7SToby Isaac             cells[p].index = cell;
8883a93e3b7SToby Isaac             numFound++;
8899cb35068SDave May             terminating_query_type[2]++;
8903a93e3b7SToby Isaac             break;
891ccd2543fSMatthew G Knepley           }
8923a93e3b7SToby Isaac         }
893af74b616SDave May       }
894953fc75cSMatthew G. Knepley     } else {
895953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
896*d8206211SMatthew G. Knepley         PetscInt idx;
897*d8206211SMatthew G. Knepley 
898*d8206211SMatthew G. Knepley         PetscCall(PetscFindInt(c, Nl, leaves, &idx));
899*d8206211SMatthew G. Knepley         if (idx >= 0) continue;
9009566063dSJacob Faibussowitsch         PetscCall(DMPlexLocatePoint_Internal(dm, dim, point, c, &cell));
9013a93e3b7SToby Isaac         if (cell >= 0) {
9023a93e3b7SToby Isaac           cells[p].rank  = 0;
9033a93e3b7SToby Isaac           cells[p].index = cell;
9043a93e3b7SToby Isaac           numFound++;
9059cb35068SDave May           terminating_query_type[2]++;
9063a93e3b7SToby Isaac           break;
907953fc75cSMatthew G. Knepley         }
908953fc75cSMatthew G. Knepley       }
9093a93e3b7SToby Isaac     }
910ccd2543fSMatthew G Knepley   }
9119566063dSJacob Faibussowitsch   if (hash) PetscCall(ISRestoreIndices(mesh->lbox->cells, &boxCells));
91262a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
91362a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
91462a38674SMatthew G. Knepley       const PetscScalar *point = &a[p * bs];
915d92c4b9fSToby Isaac       PetscReal          cpoint[3], diff[3], best[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL}, dist, distMax = PETSC_MAX_REAL;
916d92c4b9fSToby Isaac       PetscInt           dbin[3] = {-1, -1, -1}, bin, cellOffset, d, bestc = -1;
91762a38674SMatthew G. Knepley 
918e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
9199566063dSJacob Faibussowitsch         PetscCall(PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin));
9209566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells));
9219566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset));
92262a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
9239566063dSJacob Faibussowitsch           PetscCall(DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint));
924b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
92562a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
92662a38674SMatthew G. Knepley           if (dist < distMax) {
927d92c4b9fSToby Isaac             for (d = 0; d < dim; ++d) best[d] = cpoint[d];
928d92c4b9fSToby Isaac             bestc   = boxCells[c];
92962a38674SMatthew G. Knepley             distMax = dist;
93062a38674SMatthew G. Knepley           }
93162a38674SMatthew G. Knepley         }
932d92c4b9fSToby Isaac         if (distMax < PETSC_MAX_REAL) {
933d92c4b9fSToby Isaac           ++numFound;
934d92c4b9fSToby Isaac           cells[p].rank  = 0;
935d92c4b9fSToby Isaac           cells[p].index = bestc;
936d92c4b9fSToby Isaac           for (d = 0; d < dim; ++d) a[p * bs + d] = best[d];
937d92c4b9fSToby Isaac         }
93862a38674SMatthew G. Knepley       }
93962a38674SMatthew G. Knepley     }
94062a38674SMatthew G. Knepley   }
94162a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
942cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
9432d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
9449566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numFound, &found));
9453a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
9463a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
9479371c9d4SSatish Balay         if (numFound < p) { cells[numFound] = cells[p]; }
9483a93e3b7SToby Isaac         found[numFound++] = p;
9493a93e3b7SToby Isaac       }
9503a93e3b7SToby Isaac     }
9513a93e3b7SToby Isaac   }
9529566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(v, &a));
953*d8206211SMatthew G. Knepley   if (!reuse) PetscCall(PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER));
9549566063dSJacob Faibussowitsch   PetscCall(PetscTime(&t1));
9559cb35068SDave May   if (hash) {
95663a3b9bcSJacob 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]));
9579cb35068SDave May   } else {
95863a3b9bcSJacob 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]));
9599cb35068SDave May   }
96063a3b9bcSJacob 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))));
9619566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_LocatePoints, 0, 0, 0, 0));
962ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
963ccd2543fSMatthew G Knepley }
964ccd2543fSMatthew G Knepley 
965741bfc07SMatthew G. Knepley /*@C
966741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
967741bfc07SMatthew G. Knepley 
968741bfc07SMatthew G. Knepley   Not collective
969741bfc07SMatthew G. Knepley 
9706b867d5aSJose E. Roman   Input/Output Parameter:
9716b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x
972741bfc07SMatthew G. Knepley 
9736b867d5aSJose E. Roman   Output Parameter:
9746b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
975741bfc07SMatthew G. Knepley 
976741bfc07SMatthew G. Knepley   Level: developer
977741bfc07SMatthew G. Knepley 
978db781477SPatrick Sanan .seealso: `DMPlexComputeProjection3Dto1D()`, `DMPlexComputeProjection3Dto2D()`
979741bfc07SMatthew G. Knepley @*/
9809371c9d4SSatish Balay PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[]) {
98117fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
98217fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
9838b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x * x + y * y), c = x / r, s = y / r;
98417fe8556SMatthew G. Knepley 
98517fe8556SMatthew G. Knepley   PetscFunctionBegin;
9869371c9d4SSatish Balay   R[0]      = c;
9879371c9d4SSatish Balay   R[1]      = -s;
9889371c9d4SSatish Balay   R[2]      = s;
9899371c9d4SSatish Balay   R[3]      = c;
99017fe8556SMatthew G. Knepley   coords[0] = 0.0;
9917f07f362SMatthew G. Knepley   coords[1] = r;
99217fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
99317fe8556SMatthew G. Knepley }
99417fe8556SMatthew G. Knepley 
995741bfc07SMatthew G. Knepley /*@C
996741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
99728dbe442SToby Isaac 
998741bfc07SMatthew G. Knepley   Not collective
99928dbe442SToby Isaac 
10006b867d5aSJose E. Roman   Input/Output Parameter:
10016b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z
1002741bfc07SMatthew G. Knepley 
10036b867d5aSJose E. Roman   Output Parameter:
10046b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
1005741bfc07SMatthew G. Knepley 
1006741bfc07SMatthew 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
1007741bfc07SMatthew G. Knepley 
1008741bfc07SMatthew G. Knepley   Level: developer
1009741bfc07SMatthew G. Knepley 
1010db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto2D()`
1011741bfc07SMatthew G. Knepley @*/
10129371c9d4SSatish Balay PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[]) {
101328dbe442SToby Isaac   PetscReal x    = PetscRealPart(coords[3] - coords[0]);
101428dbe442SToby Isaac   PetscReal y    = PetscRealPart(coords[4] - coords[1]);
101528dbe442SToby Isaac   PetscReal z    = PetscRealPart(coords[5] - coords[2]);
101628dbe442SToby Isaac   PetscReal r    = PetscSqrtReal(x * x + y * y + z * z);
101728dbe442SToby Isaac   PetscReal rinv = 1. / r;
101828dbe442SToby Isaac   PetscFunctionBegin;
101928dbe442SToby Isaac 
10209371c9d4SSatish Balay   x *= rinv;
10219371c9d4SSatish Balay   y *= rinv;
10229371c9d4SSatish Balay   z *= rinv;
102328dbe442SToby Isaac   if (x > 0.) {
102428dbe442SToby Isaac     PetscReal inv1pX = 1. / (1. + x);
102528dbe442SToby Isaac 
10269371c9d4SSatish Balay     R[0] = x;
10279371c9d4SSatish Balay     R[1] = -y;
10289371c9d4SSatish Balay     R[2] = -z;
10299371c9d4SSatish Balay     R[3] = y;
10309371c9d4SSatish Balay     R[4] = 1. - y * y * inv1pX;
10319371c9d4SSatish Balay     R[5] = -y * z * inv1pX;
10329371c9d4SSatish Balay     R[6] = z;
10339371c9d4SSatish Balay     R[7] = -y * z * inv1pX;
10349371c9d4SSatish Balay     R[8] = 1. - z * z * inv1pX;
10359371c9d4SSatish Balay   } else {
103628dbe442SToby Isaac     PetscReal inv1mX = 1. / (1. - x);
103728dbe442SToby Isaac 
10389371c9d4SSatish Balay     R[0] = x;
10399371c9d4SSatish Balay     R[1] = z;
10409371c9d4SSatish Balay     R[2] = y;
10419371c9d4SSatish Balay     R[3] = y;
10429371c9d4SSatish Balay     R[4] = -y * z * inv1mX;
10439371c9d4SSatish Balay     R[5] = 1. - y * y * inv1mX;
10449371c9d4SSatish Balay     R[6] = z;
10459371c9d4SSatish Balay     R[7] = 1. - z * z * inv1mX;
10469371c9d4SSatish Balay     R[8] = -y * z * inv1mX;
104728dbe442SToby Isaac   }
104828dbe442SToby Isaac   coords[0] = 0.0;
104928dbe442SToby Isaac   coords[1] = r;
105028dbe442SToby Isaac   PetscFunctionReturn(0);
105128dbe442SToby Isaac }
105228dbe442SToby Isaac 
1053741bfc07SMatthew G. Knepley /*@
1054c871b86eSJed Brown   DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the
1055c871b86eSJed Brown     plane.  The normal is defined by positive orientation of the first 3 points.
1056741bfc07SMatthew G. Knepley 
1057741bfc07SMatthew G. Knepley   Not collective
1058741bfc07SMatthew G. Knepley 
1059741bfc07SMatthew G. Knepley   Input Parameter:
10606b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points)
1061741bfc07SMatthew G. Knepley 
10626b867d5aSJose E. Roman   Input/Output Parameter:
10636b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first
10646b867d5aSJose E. Roman            2*coordSize/3 entries contain interlaced 2D points, with the rest undefined
10656b867d5aSJose E. Roman 
10666b867d5aSJose E. Roman   Output Parameter:
10676b867d5aSJose 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.
1068741bfc07SMatthew G. Knepley 
1069741bfc07SMatthew G. Knepley   Level: developer
1070741bfc07SMatthew G. Knepley 
1071db781477SPatrick Sanan .seealso: `DMPlexComputeProjection2Dto1D()`, `DMPlexComputeProjection3Dto1D()`
1072741bfc07SMatthew G. Knepley @*/
10739371c9d4SSatish Balay PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) {
1074c871b86eSJed Brown   PetscReal      x1[3], x2[3], n[3], c[3], norm;
1075ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1076c871b86eSJed Brown   PetscInt       d, p;
1077ccd2543fSMatthew G Knepley 
1078ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1079ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
1080ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
10811ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1 * dim + d] - coords[0 * dim + d]);
10821ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2 * dim + d] - coords[0 * dim + d]);
1083ccd2543fSMatthew G Knepley   }
1084c871b86eSJed Brown   // n = x1 \otimes x2
1085ccd2543fSMatthew G Knepley   n[0] = x1[1] * x2[2] - x1[2] * x2[1];
1086ccd2543fSMatthew G Knepley   n[1] = x1[2] * x2[0] - x1[0] * x2[2];
1087ccd2543fSMatthew G Knepley   n[2] = x1[0] * x2[1] - x1[1] * x2[0];
10888b49ba18SBarry Smith   norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
1089c871b86eSJed Brown   for (d = 0; d < dim; d++) n[d] /= norm;
1090c871b86eSJed Brown   norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]);
1091c871b86eSJed Brown   for (d = 0; d < dim; d++) x1[d] /= norm;
1092c871b86eSJed Brown   // x2 = n \otimes x1
1093c871b86eSJed Brown   x2[0] = n[1] * x1[2] - n[2] * x1[1];
1094c871b86eSJed Brown   x2[1] = n[2] * x1[0] - n[0] * x1[2];
1095c871b86eSJed Brown   x2[2] = n[0] * x1[1] - n[1] * x1[0];
1096c871b86eSJed Brown   for (d = 0; d < dim; d++) {
1097c871b86eSJed Brown     R[d * dim + 0] = x1[d];
1098c871b86eSJed Brown     R[d * dim + 1] = x2[d];
1099c871b86eSJed Brown     R[d * dim + 2] = n[d];
1100c871b86eSJed Brown     c[d]           = PetscRealPart(coords[0 * dim + d]);
110173868372SMatthew G. Knepley   }
1102c871b86eSJed Brown   for (p = 0; p < coordSize / dim; p++) {
1103c871b86eSJed Brown     PetscReal y[3];
1104c871b86eSJed Brown     for (d = 0; d < dim; d++) y[d] = PetscRealPart(coords[p * dim + d]) - c[d];
1105c871b86eSJed 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];
11067f07f362SMatthew G. Knepley   }
1107ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1108ccd2543fSMatthew G Knepley }
1109ccd2543fSMatthew G Knepley 
11106322fe33SJed Brown PETSC_UNUSED
11119371c9d4SSatish Balay static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) {
1112834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
1113834e62ceSMatthew G. Knepley 
1114834e62ceSMatthew G. Knepley    |  1  1  1 |
1115834e62ceSMatthew G. Knepley    | x0 x1 x2 |
1116834e62ceSMatthew G. Knepley    | y0 y1 y2 |
1117834e62ceSMatthew G. Knepley 
1118834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
1119834e62ceSMatthew G. Knepley 
1120834e62ceSMatthew G. Knepley    | x1 x2 |
1121834e62ceSMatthew G. Knepley    | y1 y2 |
1122834e62ceSMatthew G. Knepley   */
1123834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
1124834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
1125834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
11269371c9d4SSatish Balay   M[0] = x1;
11279371c9d4SSatish Balay   M[1] = x2;
11289371c9d4SSatish Balay   M[2] = y1;
11299371c9d4SSatish Balay   M[3] = y2;
1130923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
1131834e62ceSMatthew G. Knepley   *vol = 0.5 * detM;
11323bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
1133834e62ceSMatthew G. Knepley }
1134834e62ceSMatthew G. Knepley 
11356322fe33SJed Brown PETSC_UNUSED
11369371c9d4SSatish Balay static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) {
1137834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
1138834e62ceSMatthew G. Knepley 
1139834e62ceSMatthew G. Knepley    |  1  1  1  1 |
1140834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
1141834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
1142834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
1143834e62ceSMatthew G. Knepley 
1144834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
1145834e62ceSMatthew G. Knepley 
1146834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1147834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1148834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1149834e62ceSMatthew G. Knepley   */
1150834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2];
1151834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2];
1152834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
11530a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1154834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
11559371c9d4SSatish Balay   M[0] = x1;
11569371c9d4SSatish Balay   M[1] = x2;
11579371c9d4SSatish Balay   M[2] = x3;
11589371c9d4SSatish Balay   M[3] = y1;
11599371c9d4SSatish Balay   M[4] = y2;
11609371c9d4SSatish Balay   M[5] = y3;
11619371c9d4SSatish Balay   M[6] = z1;
11629371c9d4SSatish Balay   M[7] = z2;
11639371c9d4SSatish Balay   M[8] = z3;
1164923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
11650a3da2c2SToby Isaac   *vol = -onesixth * detM;
11663bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1167834e62ceSMatthew G. Knepley }
1168834e62ceSMatthew G. Knepley 
11699371c9d4SSatish Balay static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) {
11700a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1. / (PetscReal)6.);
1171923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
11720a3da2c2SToby Isaac   *vol *= -onesixth;
11730ec8681fSMatthew G. Knepley }
11740ec8681fSMatthew G. Knepley 
11759371c9d4SSatish Balay static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) {
1176cb92db44SToby Isaac   PetscSection       coordSection;
1177cb92db44SToby Isaac   Vec                coordinates;
1178cb92db44SToby Isaac   const PetscScalar *coords;
1179cb92db44SToby Isaac   PetscInt           dim, d, off;
1180cb92db44SToby Isaac 
1181cb92db44SToby Isaac   PetscFunctionBegin;
11829566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
11839566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
11849566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(coordSection, e, &dim));
1185cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
11869566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(coordSection, e, &off));
11879566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
11889371c9d4SSatish Balay   if (v0) {
11899371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);
11909371c9d4SSatish Balay   }
11919566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
1192cb92db44SToby Isaac   *detJ = 1.;
1193cb92db44SToby Isaac   if (J) {
1194cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1195cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1196cb92db44SToby Isaac     if (invJ) {
1197cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1198cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1199cb92db44SToby Isaac     }
1200cb92db44SToby Isaac   }
1201cb92db44SToby Isaac   PetscFunctionReturn(0);
1202cb92db44SToby Isaac }
1203cb92db44SToby Isaac 
12046858538eSMatthew G. Knepley /*@C
12056858538eSMatthew G. Knepley   DMPlexGetCellCoordinates - Get coordinates for a cell, taking into account periodicity
12066858538eSMatthew G. Knepley 
12076858538eSMatthew G. Knepley   Not collective
12086858538eSMatthew G. Knepley 
12096858538eSMatthew G. Knepley   Input Parameters:
12106858538eSMatthew G. Knepley + dm   - The DM
12116858538eSMatthew G. Knepley - cell - The cell number
12126858538eSMatthew G. Knepley 
12136858538eSMatthew G. Knepley   Output Parameters:
12146858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
12156858538eSMatthew G. Knepley . Nc     - The number of coordinates
12166858538eSMatthew G. Knepley . array  - The coordinate array
12176858538eSMatthew G. Knepley - coords - The cell coordinates
12186858538eSMatthew G. Knepley 
12196858538eSMatthew G. Knepley   Level: developer
12206858538eSMatthew G. Knepley 
12216858538eSMatthew G. Knepley .seealso: DMPlexRestoreCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal()
12226858538eSMatthew G. Knepley @*/
12239371c9d4SSatish Balay PetscErrorCode DMPlexGetCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[]) {
12246858538eSMatthew G. Knepley   DM                 cdm;
12256858538eSMatthew G. Knepley   Vec                coordinates;
12266858538eSMatthew G. Knepley   PetscSection       cs;
12276858538eSMatthew G. Knepley   const PetscScalar *ccoords;
12286858538eSMatthew G. Knepley   PetscInt           pStart, pEnd;
12296858538eSMatthew G. Knepley 
12306858538eSMatthew G. Knepley   PetscFunctionBeginHot;
12316858538eSMatthew G. Knepley   *isDG   = PETSC_FALSE;
12326858538eSMatthew G. Knepley   *Nc     = 0;
12336858538eSMatthew G. Knepley   *array  = NULL;
12346858538eSMatthew G. Knepley   *coords = NULL;
12356858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12366858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateSection(dm, &cs));
12376858538eSMatthew G. Knepley   if (!cs) goto cg;
12386858538eSMatthew G. Knepley   /* Check that the cell exists in the cellwise section */
12396858538eSMatthew G. Knepley   PetscCall(PetscSectionGetChart(cs, &pStart, &pEnd));
12406858538eSMatthew G. Knepley   if (cell < pStart || cell >= pEnd) goto cg;
12416858538eSMatthew G. Knepley   /* Check for cellwise coordinates for this cell */
12426858538eSMatthew G. Knepley   PetscCall(PetscSectionGetDof(cs, cell, Nc));
12436858538eSMatthew G. Knepley   if (!*Nc) goto cg;
12446858538eSMatthew G. Knepley   /* Check for cellwise coordinates */
12456858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocalNoncollective(dm, &coordinates));
12466858538eSMatthew G. Knepley   if (!coordinates) goto cg;
12476858538eSMatthew G. Knepley   /* Get cellwise coordinates */
12486858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dm, &cdm));
12496858538eSMatthew G. Knepley   PetscCall(VecGetArrayRead(coordinates, array));
12506858538eSMatthew G. Knepley   PetscCall(DMPlexPointLocalRead(cdm, cell, *array, &ccoords));
12516858538eSMatthew G. Knepley   PetscCall(DMGetWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
12526858538eSMatthew G. Knepley   PetscCall(PetscArraycpy(*coords, ccoords, *Nc));
12536858538eSMatthew G. Knepley   PetscCall(VecRestoreArrayRead(coordinates, array));
12546858538eSMatthew G. Knepley   *isDG = PETSC_TRUE;
12556858538eSMatthew G. Knepley   PetscFunctionReturn(0);
12566858538eSMatthew G. Knepley cg:
12576858538eSMatthew G. Knepley   /* Use continuous coordinates */
12586858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
12596858538eSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &cs));
12606858538eSMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
12616858538eSMatthew G. Knepley   PetscCall(DMPlexVecGetClosure(cdm, cs, coordinates, cell, Nc, coords));
12626858538eSMatthew G. Knepley   PetscFunctionReturn(0);
12636858538eSMatthew G. Knepley }
12646858538eSMatthew G. Knepley 
12656858538eSMatthew G. Knepley /*@C
12666858538eSMatthew G. Knepley   DMPlexRestoreCellCoordinates - Get coordinates for a cell, taking into account periodicity
12676858538eSMatthew G. Knepley 
12686858538eSMatthew G. Knepley   Not collective
12696858538eSMatthew G. Knepley 
12706858538eSMatthew G. Knepley   Input Parameters:
12716858538eSMatthew G. Knepley + dm   - The DM
12726858538eSMatthew G. Knepley - cell - The cell number
12736858538eSMatthew G. Knepley 
12746858538eSMatthew G. Knepley   Output Parameters:
12756858538eSMatthew G. Knepley + isDG   - Using cellwise coordinates
12766858538eSMatthew G. Knepley . Nc     - The number of coordinates
12776858538eSMatthew G. Knepley . array  - The coordinate array
12786858538eSMatthew G. Knepley - coords - The cell coordinates
12796858538eSMatthew G. Knepley 
12806858538eSMatthew G. Knepley   Level: developer
12816858538eSMatthew G. Knepley 
12826858538eSMatthew G. Knepley .seealso: DMPlexGetCellCoordinates(), DMGetCoordinatesLocal(), DMGetCellCoordinatesLocal()
12836858538eSMatthew G. Knepley @*/
12849371c9d4SSatish Balay PetscErrorCode DMPlexRestoreCellCoordinates(DM dm, PetscInt cell, PetscBool *isDG, PetscInt *Nc, const PetscScalar *array[], PetscScalar *coords[]) {
12856858538eSMatthew G. Knepley   DM           cdm;
12866858538eSMatthew G. Knepley   PetscSection cs;
12876858538eSMatthew G. Knepley   Vec          coordinates;
12886858538eSMatthew G. Knepley 
12896858538eSMatthew G. Knepley   PetscFunctionBeginHot;
12906858538eSMatthew G. Knepley   if (*isDG) {
12916858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinateDM(dm, &cdm));
12926858538eSMatthew G. Knepley     PetscCall(DMRestoreWorkArray(cdm, *Nc, MPIU_SCALAR, coords));
12936858538eSMatthew G. Knepley   } else {
12946858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
12956858538eSMatthew G. Knepley     PetscCall(DMGetCoordinateSection(dm, &cs));
12966858538eSMatthew G. Knepley     PetscCall(DMGetCoordinatesLocalNoncollective(dm, &coordinates));
12976858538eSMatthew G. Knepley     PetscCall(DMPlexVecRestoreClosure(cdm, cs, coordinates, cell, Nc, (PetscScalar **)coords));
12986858538eSMatthew G. Knepley   }
12996858538eSMatthew G. Knepley   PetscFunctionReturn(0);
13006858538eSMatthew G. Knepley }
13016858538eSMatthew G. Knepley 
13029371c9d4SSatish Balay static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) {
13036858538eSMatthew G. Knepley   const PetscScalar *array;
1304a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
13056858538eSMatthew G. Knepley   PetscInt           numCoords, d;
13066858538eSMatthew G. Knepley   PetscBool          isDG;
130717fe8556SMatthew G. Knepley 
130817fe8556SMatthew G. Knepley   PetscFunctionBegin;
13096858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
131008401ef6SPierre Jolivet   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
13117f07f362SMatthew G. Knepley   *detJ = 0.0;
131228dbe442SToby Isaac   if (numCoords == 6) {
131328dbe442SToby Isaac     const PetscInt dim = 3;
131428dbe442SToby Isaac     PetscReal      R[9], J0;
131528dbe442SToby Isaac 
13169371c9d4SSatish Balay     if (v0) {
13179371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13189371c9d4SSatish Balay     }
13199566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto1D(coords, R));
132028dbe442SToby Isaac     if (J) {
132128dbe442SToby Isaac       J0   = 0.5 * PetscRealPart(coords[1]);
13229371c9d4SSatish Balay       J[0] = R[0] * J0;
13239371c9d4SSatish Balay       J[1] = R[1];
13249371c9d4SSatish Balay       J[2] = R[2];
13259371c9d4SSatish Balay       J[3] = R[3] * J0;
13269371c9d4SSatish Balay       J[4] = R[4];
13279371c9d4SSatish Balay       J[5] = R[5];
13289371c9d4SSatish Balay       J[6] = R[6] * J0;
13299371c9d4SSatish Balay       J[7] = R[7];
13309371c9d4SSatish Balay       J[8] = R[8];
133128dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
133228dbe442SToby Isaac       if (invJ) { DMPlex_Invert2D_Internal(invJ, J, *detJ); }
1333adac9986SMatthew G. Knepley     }
133428dbe442SToby Isaac   } else if (numCoords == 4) {
13357f07f362SMatthew G. Knepley     const PetscInt dim = 2;
13367f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
13377f07f362SMatthew G. Knepley 
13389371c9d4SSatish Balay     if (v0) {
13399371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13409371c9d4SSatish Balay     }
13419566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection2Dto1D(coords, R));
134217fe8556SMatthew G. Knepley     if (J) {
13437f07f362SMatthew G. Knepley       J0   = 0.5 * PetscRealPart(coords[1]);
13449371c9d4SSatish Balay       J[0] = R[0] * J0;
13459371c9d4SSatish Balay       J[1] = R[1];
13469371c9d4SSatish Balay       J[2] = R[2] * J0;
13479371c9d4SSatish Balay       J[3] = R[3];
1348923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1349923591dfSMatthew G. Knepley       if (invJ) { DMPlex_Invert2D_Internal(invJ, J, *detJ); }
1350adac9986SMatthew G. Knepley     }
13517f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
13527f07f362SMatthew G. Knepley     const PetscInt dim = 1;
13537f07f362SMatthew G. Knepley 
13549371c9d4SSatish Balay     if (v0) {
13559371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13569371c9d4SSatish Balay     }
13577f07f362SMatthew G. Knepley     if (J) {
13587f07f362SMatthew G. Knepley       J[0]  = 0.5 * (PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
135917fe8556SMatthew G. Knepley       *detJ = J[0];
13609566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(2.0));
13619371c9d4SSatish Balay       if (invJ) {
13629371c9d4SSatish Balay         invJ[0] = 1.0 / J[0];
13639371c9d4SSatish Balay         PetscCall(PetscLogFlops(1.0));
13649371c9d4SSatish Balay       }
1365adac9986SMatthew G. Knepley     }
13666858538eSMatthew 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);
13676858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
136817fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
136917fe8556SMatthew G. Knepley }
137017fe8556SMatthew G. Knepley 
13719371c9d4SSatish Balay static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) {
13726858538eSMatthew G. Knepley   const PetscScalar *array;
1373a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
13746858538eSMatthew G. Knepley   PetscInt           numCoords, d;
13756858538eSMatthew G. Knepley   PetscBool          isDG;
1376ccd2543fSMatthew G Knepley 
1377ccd2543fSMatthew G Knepley   PetscFunctionBegin;
13786858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
13796858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
13807f07f362SMatthew G. Knepley   *detJ = 0.0;
1381ccd2543fSMatthew G Knepley   if (numCoords == 9) {
13827f07f362SMatthew G. Knepley     const PetscInt dim = 3;
13837f07f362SMatthew 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};
13847f07f362SMatthew G. Knepley 
13859371c9d4SSatish Balay     if (v0) {
13869371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
13879371c9d4SSatish Balay     }
13889566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
13897f07f362SMatthew G. Knepley     if (J) {
1390b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1391b7ad821dSMatthew G. Knepley 
1392b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
13939371c9d4SSatish Balay         for (PetscInt f = 0; f < pdim; f++) { J0[d * dim + f] = 0.5 * (PetscRealPart(coords[(f + 1) * pdim + d]) - PetscRealPart(coords[0 * pdim + d])); }
13947f07f362SMatthew G. Knepley       }
13959566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1396923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
13977f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
13986858538eSMatthew G. Knepley         for (PetscInt f = 0; f < dim; f++) {
13997f07f362SMatthew G. Knepley           J[d * dim + f] = 0.0;
14009371c9d4SSatish Balay           for (PetscInt g = 0; g < dim; g++) { J[d * dim + f] += R[d * dim + g] * J0[g * dim + f]; }
14017f07f362SMatthew G. Knepley         }
14027f07f362SMatthew G. Knepley       }
14039566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
14047f07f362SMatthew G. Knepley     }
1405923591dfSMatthew G. Knepley     if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); }
14067f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
14077f07f362SMatthew G. Knepley     const PetscInt dim = 2;
14087f07f362SMatthew G. Knepley 
14099371c9d4SSatish Balay     if (v0) {
14109371c9d4SSatish Balay       for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
14119371c9d4SSatish Balay     }
1412ccd2543fSMatthew G Knepley     if (J) {
1413ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
14149371c9d4SSatish Balay         for (PetscInt f = 0; f < dim; f++) { J[d * dim + f] = 0.5 * (PetscRealPart(coords[(f + 1) * dim + d]) - PetscRealPart(coords[0 * dim + d])); }
1415ccd2543fSMatthew G Knepley       }
14169566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(8.0));
1417923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1418ccd2543fSMatthew G Knepley     }
1419923591dfSMatthew G. Knepley     if (invJ) { DMPlex_Invert2D_Internal(invJ, J, *detJ); }
142063a3b9bcSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %" PetscInt_FMT " != 6 or 9", numCoords);
14216858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1422ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1423ccd2543fSMatthew G Knepley }
1424ccd2543fSMatthew G Knepley 
14259371c9d4SSatish Balay static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) {
14266858538eSMatthew G. Knepley   const PetscScalar *array;
1427a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
14286858538eSMatthew G. Knepley   PetscInt           numCoords, d;
14296858538eSMatthew G. Knepley   PetscBool          isDG;
1430ccd2543fSMatthew G Knepley 
1431ccd2543fSMatthew G Knepley   PetscFunctionBegin;
14326858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
14336858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1434dfccc68fSToby Isaac   if (!Nq) {
1435412e9a14SMatthew G. Knepley     PetscInt vorder[4] = {0, 1, 2, 3};
1436412e9a14SMatthew G. Knepley 
14379371c9d4SSatish Balay     if (isTensor) {
14389371c9d4SSatish Balay       vorder[2] = 3;
14399371c9d4SSatish Balay       vorder[3] = 2;
14409371c9d4SSatish Balay     }
14417f07f362SMatthew G. Knepley     *detJ = 0.0;
144299dec3a6SMatthew G. Knepley     if (numCoords == 12) {
144399dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
144499dec3a6SMatthew 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};
144599dec3a6SMatthew G. Knepley 
14469371c9d4SSatish Balay       if (v) {
14479371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
14489371c9d4SSatish Balay       }
14499566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeProjection3Dto2D(numCoords, coords, R));
145099dec3a6SMatthew G. Knepley       if (J) {
145199dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
145299dec3a6SMatthew G. Knepley 
145399dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
1454412e9a14SMatthew G. Knepley           J0[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * pdim + d]) - PetscRealPart(coords[vorder[0] * pdim + d]));
1455412e9a14SMatthew G. Knepley           J0[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[2] * pdim + d]) - PetscRealPart(coords[vorder[1] * pdim + d]));
145699dec3a6SMatthew G. Knepley         }
14579566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1458923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
145999dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
14606858538eSMatthew G. Knepley           for (PetscInt f = 0; f < dim; f++) {
146199dec3a6SMatthew G. Knepley             J[d * dim + f] = 0.0;
14629371c9d4SSatish Balay             for (PetscInt g = 0; g < dim; g++) { J[d * dim + f] += R[d * dim + g] * J0[g * dim + f]; }
146399dec3a6SMatthew G. Knepley           }
146499dec3a6SMatthew G. Knepley         }
14659566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(18.0));
146699dec3a6SMatthew G. Knepley       }
1467923591dfSMatthew G. Knepley       if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); }
146871f58de1SToby Isaac     } else if (numCoords == 8) {
146999dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
147099dec3a6SMatthew G. Knepley 
14719371c9d4SSatish Balay       if (v) {
14729371c9d4SSatish Balay         for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
14739371c9d4SSatish Balay       }
1474ccd2543fSMatthew G Knepley       if (J) {
1475ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
1476412e9a14SMatthew G. Knepley           J[d * dim + 0] = 0.5 * (PetscRealPart(coords[vorder[1] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1477412e9a14SMatthew G. Knepley           J[d * dim + 1] = 0.5 * (PetscRealPart(coords[vorder[3] * dim + d]) - PetscRealPart(coords[vorder[0] * dim + d]));
1478ccd2543fSMatthew G Knepley         }
14799566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(8.0));
1480923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1481ccd2543fSMatthew G Knepley       }
1482923591dfSMatthew G. Knepley       if (invJ) { DMPlex_Invert2D_Internal(invJ, J, *detJ); }
148363a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1484dfccc68fSToby Isaac   } else {
1485dfccc68fSToby Isaac     const PetscInt Nv         = 4;
1486dfccc68fSToby Isaac     const PetscInt dimR       = 2;
1487412e9a14SMatthew G. Knepley     PetscInt       zToPlex[4] = {0, 1, 3, 2};
1488dfccc68fSToby Isaac     PetscReal      zOrder[12];
1489dfccc68fSToby Isaac     PetscReal      zCoeff[12];
1490dfccc68fSToby Isaac     PetscInt       i, j, k, l, dim;
1491dfccc68fSToby Isaac 
14929371c9d4SSatish Balay     if (isTensor) {
14939371c9d4SSatish Balay       zToPlex[2] = 2;
14949371c9d4SSatish Balay       zToPlex[3] = 3;
14959371c9d4SSatish Balay     }
1496dfccc68fSToby Isaac     if (numCoords == 12) {
1497dfccc68fSToby Isaac       dim = 3;
1498dfccc68fSToby Isaac     } else if (numCoords == 8) {
1499dfccc68fSToby Isaac       dim = 2;
150063a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %" PetscInt_FMT " != 8 or 12", numCoords);
1501dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1502dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1503dfccc68fSToby Isaac 
15049371c9d4SSatish Balay       for (j = 0; j < dim; j++) { zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); }
1505dfccc68fSToby Isaac     }
1506dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
15072df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta):
15082df84da0SMatthew G. Knepley            \phi^0 = (1 - xi - eta + xi eta) --> 1      = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3)
15092df84da0SMatthew G. Knepley            \phi^1 = (1 + xi - eta - xi eta) --> xi     = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3)
15102df84da0SMatthew G. Knepley            \phi^2 = (1 - xi + eta - xi eta) --> eta    = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3)
15112df84da0SMatthew G. Knepley            \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3)
15122df84da0SMatthew G. Knepley       */
1513dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1514dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (-zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1515dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (-zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1516dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1517dfccc68fSToby Isaac     }
1518dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1519dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1520dfccc68fSToby Isaac 
1521dfccc68fSToby Isaac       if (v) {
1522dfccc68fSToby Isaac         PetscReal extPoint[4];
1523dfccc68fSToby Isaac 
1524dfccc68fSToby Isaac         extPoint[0] = 1.;
1525dfccc68fSToby Isaac         extPoint[1] = xi;
1526dfccc68fSToby Isaac         extPoint[2] = eta;
1527dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1528dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1529dfccc68fSToby Isaac           PetscReal val = 0.;
1530dfccc68fSToby Isaac 
15319371c9d4SSatish Balay           for (k = 0; k < Nv; k++) { val += extPoint[k] * zCoeff[dim * k + j]; }
1532dfccc68fSToby Isaac           v[i * dim + j] = val;
1533dfccc68fSToby Isaac         }
1534dfccc68fSToby Isaac       }
1535dfccc68fSToby Isaac       if (J) {
1536dfccc68fSToby Isaac         PetscReal extJ[8];
1537dfccc68fSToby Isaac 
1538dfccc68fSToby Isaac         extJ[0] = 0.;
1539dfccc68fSToby Isaac         extJ[1] = 0.;
1540dfccc68fSToby Isaac         extJ[2] = 1.;
1541dfccc68fSToby Isaac         extJ[3] = 0.;
1542dfccc68fSToby Isaac         extJ[4] = 0.;
1543dfccc68fSToby Isaac         extJ[5] = 1.;
1544dfccc68fSToby Isaac         extJ[6] = eta;
1545dfccc68fSToby Isaac         extJ[7] = xi;
1546dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1547dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1548dfccc68fSToby Isaac             PetscReal val = 0.;
1549dfccc68fSToby Isaac 
15509371c9d4SSatish Balay             for (l = 0; l < Nv; l++) { val += zCoeff[dim * l + j] * extJ[dimR * l + k]; }
1551dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1552dfccc68fSToby Isaac           }
1553dfccc68fSToby Isaac         }
1554dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1555dfccc68fSToby Isaac           PetscReal  x, y, z;
1556dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1557dfccc68fSToby Isaac           PetscReal  norm;
1558dfccc68fSToby Isaac 
1559dfccc68fSToby Isaac           x     = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1560dfccc68fSToby Isaac           y     = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1561dfccc68fSToby Isaac           z     = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1562dfccc68fSToby Isaac           norm  = PetscSqrtReal(x * x + y * y + z * z);
1563dfccc68fSToby Isaac           iJ[2] = x / norm;
1564dfccc68fSToby Isaac           iJ[5] = y / norm;
1565dfccc68fSToby Isaac           iJ[8] = z / norm;
1566dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1567dfccc68fSToby Isaac           if (invJ) { DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); }
1568dfccc68fSToby Isaac         } else {
1569dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1570dfccc68fSToby Isaac           if (invJ) { DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); }
1571dfccc68fSToby Isaac         }
1572dfccc68fSToby Isaac       }
1573dfccc68fSToby Isaac     }
1574dfccc68fSToby Isaac   }
15756858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1576ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1577ccd2543fSMatthew G Knepley }
1578ccd2543fSMatthew G Knepley 
15799371c9d4SSatish Balay static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) {
15806858538eSMatthew G. Knepley   const PetscScalar *array;
1581a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1582ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
15836858538eSMatthew G. Knepley   PetscInt           numCoords, d;
15846858538eSMatthew G. Knepley   PetscBool          isDG;
1585ccd2543fSMatthew G Knepley 
1586ccd2543fSMatthew G Knepley   PetscFunctionBegin;
15876858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
15886858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
15897f07f362SMatthew G. Knepley   *detJ = 0.0;
15909371c9d4SSatish Balay   if (v0) {
15919371c9d4SSatish Balay     for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);
15929371c9d4SSatish Balay   }
1593ccd2543fSMatthew G Knepley   if (J) {
1594ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1595f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1596f0df753eSMatthew G. Knepley       J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1597f0df753eSMatthew G. Knepley       J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1598f0df753eSMatthew G. Knepley       J[d * dim + 2] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1599ccd2543fSMatthew G Knepley     }
16009566063dSJacob Faibussowitsch     PetscCall(PetscLogFlops(18.0));
1601923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1602ccd2543fSMatthew G Knepley   }
1603923591dfSMatthew G. Knepley   if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); }
16046858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1605ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1606ccd2543fSMatthew G Knepley }
1607ccd2543fSMatthew G Knepley 
16089371c9d4SSatish Balay static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) {
16096858538eSMatthew G. Knepley   const PetscScalar *array;
1610a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
1611ccd2543fSMatthew G Knepley   const PetscInt     dim    = 3;
16126858538eSMatthew G. Knepley   PetscInt           numCoords, d;
16136858538eSMatthew G. Knepley   PetscBool          isDG;
1614ccd2543fSMatthew G Knepley 
1615ccd2543fSMatthew G Knepley   PetscFunctionBegin;
16166858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
16176858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
1618dfccc68fSToby Isaac   if (!Nq) {
16197f07f362SMatthew G. Knepley     *detJ = 0.0;
16209371c9d4SSatish Balay     if (v) {
16219371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
16229371c9d4SSatish Balay     }
1623ccd2543fSMatthew G Knepley     if (J) {
1624ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1625f0df753eSMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[3 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1626f0df753eSMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1627f0df753eSMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
1628ccd2543fSMatthew G Knepley       }
16299566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
1630923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1631ccd2543fSMatthew G Knepley     }
1632923591dfSMatthew G. Knepley     if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); }
1633dfccc68fSToby Isaac   } else {
1634dfccc68fSToby Isaac     const PetscInt Nv         = 8;
1635dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1636dfccc68fSToby Isaac     const PetscInt dim        = 3;
1637dfccc68fSToby Isaac     const PetscInt dimR       = 3;
1638dfccc68fSToby Isaac     PetscReal      zOrder[24];
1639dfccc68fSToby Isaac     PetscReal      zCoeff[24];
1640dfccc68fSToby Isaac     PetscInt       i, j, k, l;
1641dfccc68fSToby Isaac 
1642dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1643dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1644dfccc68fSToby Isaac 
16459371c9d4SSatish Balay       for (j = 0; j < dim; j++) { zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]); }
1646dfccc68fSToby Isaac     }
1647dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1648dfccc68fSToby 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]);
1649dfccc68fSToby 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]);
1650dfccc68fSToby 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]);
1651dfccc68fSToby 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]);
1652dfccc68fSToby 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]);
1653dfccc68fSToby 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]);
1654dfccc68fSToby 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]);
1655dfccc68fSToby 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]);
1656dfccc68fSToby Isaac     }
1657dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1658dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1659dfccc68fSToby Isaac 
1660dfccc68fSToby Isaac       if (v) {
166191d2b7ceSToby Isaac         PetscReal extPoint[8];
1662dfccc68fSToby Isaac 
1663dfccc68fSToby Isaac         extPoint[0] = 1.;
1664dfccc68fSToby Isaac         extPoint[1] = xi;
1665dfccc68fSToby Isaac         extPoint[2] = eta;
1666dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1667dfccc68fSToby Isaac         extPoint[4] = theta;
1668dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1669dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1670dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1671dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1672dfccc68fSToby Isaac           PetscReal val = 0.;
1673dfccc68fSToby Isaac 
16749371c9d4SSatish Balay           for (k = 0; k < Nv; k++) { val += extPoint[k] * zCoeff[dim * k + j]; }
1675dfccc68fSToby Isaac           v[i * dim + j] = val;
1676dfccc68fSToby Isaac         }
1677dfccc68fSToby Isaac       }
1678dfccc68fSToby Isaac       if (J) {
1679dfccc68fSToby Isaac         PetscReal extJ[24];
1680dfccc68fSToby Isaac 
16819371c9d4SSatish Balay         extJ[0]  = 0.;
16829371c9d4SSatish Balay         extJ[1]  = 0.;
16839371c9d4SSatish Balay         extJ[2]  = 0.;
16849371c9d4SSatish Balay         extJ[3]  = 1.;
16859371c9d4SSatish Balay         extJ[4]  = 0.;
16869371c9d4SSatish Balay         extJ[5]  = 0.;
16879371c9d4SSatish Balay         extJ[6]  = 0.;
16889371c9d4SSatish Balay         extJ[7]  = 1.;
16899371c9d4SSatish Balay         extJ[8]  = 0.;
16909371c9d4SSatish Balay         extJ[9]  = eta;
16919371c9d4SSatish Balay         extJ[10] = xi;
16929371c9d4SSatish Balay         extJ[11] = 0.;
16939371c9d4SSatish Balay         extJ[12] = 0.;
16949371c9d4SSatish Balay         extJ[13] = 0.;
16959371c9d4SSatish Balay         extJ[14] = 1.;
16969371c9d4SSatish Balay         extJ[15] = theta;
16979371c9d4SSatish Balay         extJ[16] = 0.;
16989371c9d4SSatish Balay         extJ[17] = xi;
16999371c9d4SSatish Balay         extJ[18] = 0.;
17009371c9d4SSatish Balay         extJ[19] = theta;
17019371c9d4SSatish Balay         extJ[20] = eta;
17029371c9d4SSatish Balay         extJ[21] = theta * eta;
17039371c9d4SSatish Balay         extJ[22] = theta * xi;
17049371c9d4SSatish Balay         extJ[23] = eta * xi;
1705dfccc68fSToby Isaac 
1706dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1707dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1708dfccc68fSToby Isaac             PetscReal val = 0.;
1709dfccc68fSToby Isaac 
17109371c9d4SSatish Balay             for (l = 0; l < Nv; l++) { val += zCoeff[dim * l + j] * extJ[dimR * l + k]; }
1711dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1712dfccc68fSToby Isaac           }
1713dfccc68fSToby Isaac         }
1714dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1715dfccc68fSToby Isaac         if (invJ) { DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); }
1716dfccc68fSToby Isaac       }
1717dfccc68fSToby Isaac     }
1718dfccc68fSToby Isaac   }
17196858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
1720ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1721ccd2543fSMatthew G Knepley }
1722ccd2543fSMatthew G Knepley 
17239371c9d4SSatish Balay static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) {
17246858538eSMatthew G. Knepley   const PetscScalar *array;
17252df84da0SMatthew G. Knepley   PetscScalar       *coords = NULL;
17262df84da0SMatthew G. Knepley   const PetscInt     dim    = 3;
17276858538eSMatthew G. Knepley   PetscInt           numCoords, d;
17286858538eSMatthew G. Knepley   PetscBool          isDG;
17292df84da0SMatthew G. Knepley 
17302df84da0SMatthew G. Knepley   PetscFunctionBegin;
17316858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
17326858538eSMatthew G. Knepley   PetscCheck(!invJ || J, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
17332df84da0SMatthew G. Knepley   if (!Nq) {
17342df84da0SMatthew G. Knepley     /* Assume that the map to the reference is affine */
17352df84da0SMatthew G. Knepley     *detJ = 0.0;
17369371c9d4SSatish Balay     if (v) {
17379371c9d4SSatish Balay       for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);
17389371c9d4SSatish Balay     }
17392df84da0SMatthew G. Knepley     if (J) {
17402df84da0SMatthew G. Knepley       for (d = 0; d < dim; d++) {
17412df84da0SMatthew G. Knepley         J[d * dim + 0] = 0.5 * (PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17422df84da0SMatthew G. Knepley         J[d * dim + 1] = 0.5 * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17432df84da0SMatthew G. Knepley         J[d * dim + 2] = 0.5 * (PetscRealPart(coords[4 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
17442df84da0SMatthew G. Knepley       }
17459566063dSJacob Faibussowitsch       PetscCall(PetscLogFlops(18.0));
17462df84da0SMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
17472df84da0SMatthew G. Knepley     }
17482df84da0SMatthew G. Knepley     if (invJ) { DMPlex_Invert3D_Internal(invJ, J, *detJ); }
17492df84da0SMatthew G. Knepley   } else {
17502df84da0SMatthew G. Knepley     const PetscInt dim  = 3;
17512df84da0SMatthew G. Knepley     const PetscInt dimR = 3;
17522df84da0SMatthew G. Knepley     const PetscInt Nv   = 6;
17532df84da0SMatthew G. Knepley     PetscReal      verts[18];
17542df84da0SMatthew G. Knepley     PetscReal      coeff[18];
17552df84da0SMatthew G. Knepley     PetscInt       i, j, k, l;
17562df84da0SMatthew G. Knepley 
17579371c9d4SSatish Balay     for (i = 0; i < Nv; ++i)
17589371c9d4SSatish Balay       for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]);
17592df84da0SMatthew G. Knepley     for (j = 0; j < dim; ++j) {
17602df84da0SMatthew G. Knepley       /* Check for triangle,
17612df84da0SMatthew 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)
17622df84da0SMatthew 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)
17632df84da0SMatthew G. Knepley            phi^2 =  1/2 (1 + eta)   chi^2 = delta(-1,  1)
17642df84da0SMatthew G. Knepley 
17652df84da0SMatthew G. Knepley            phi^0 + phi^1 + phi^2 = 1    coef_1   = 1/2 (         chi^1 + chi^2)
17662df84da0SMatthew G. Knepley           -phi^0 + phi^1 - phi^2 = xi   coef_xi  = 1/2 (-chi^0 + chi^1)
17672df84da0SMatthew G. Knepley           -phi^0 - phi^1 + phi^2 = eta  coef_eta = 1/2 (-chi^0         + chi^2)
17682df84da0SMatthew G. Knepley 
17692df84da0SMatthew G. Knepley           < chi_0 chi_1 chi_2> A /  1  1  1 \ / phi_0 \   <chi> I <phi>^T  so we need the inverse transpose
17702df84da0SMatthew G. Knepley                                  | -1  1 -1 | | phi_1 | =
17712df84da0SMatthew G. Knepley                                  \ -1 -1  1 / \ phi_2 /
17722df84da0SMatthew G. Knepley 
17732df84da0SMatthew 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
17742df84da0SMatthew G. Knepley       */
17752df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta):
17762df84da0SMatthew G. Knepley            \phi^0 = 1/4 (   -xi - eta        + xi zeta + eta zeta) --> /  1  1  1  1  1  1 \ 1
17772df84da0SMatthew G. Knepley            \phi^1 = 1/4 (1      + eta - zeta           - eta zeta) --> | -1  1 -1 -1 -1  1 | eta
17782df84da0SMatthew G. Knepley            \phi^2 = 1/4 (1 + xi       - zeta - xi zeta)            --> | -1 -1  1 -1  1 -1 | xi
17792df84da0SMatthew G. Knepley            \phi^3 = 1/4 (   -xi - eta        - xi zeta - eta zeta) --> | -1 -1 -1  1  1  1 | zeta
17802df84da0SMatthew G. Knepley            \phi^4 = 1/4 (1 + xi       + zeta + xi zeta)            --> |  1  1 -1 -1  1 -1 | xi zeta
17812df84da0SMatthew G. Knepley            \phi^5 = 1/4 (1      + eta + zeta           + eta zeta) --> \  1 -1  1 -1 -1  1 / eta zeta
17822df84da0SMatthew G. Knepley            1/4 /  0  1  1  0  1  1 \
17832df84da0SMatthew G. Knepley                | -1  1  0 -1  0  1 |
17842df84da0SMatthew G. Knepley                | -1  0  1 -1  1  0 |
17852df84da0SMatthew G. Knepley                |  0 -1 -1  0  1  1 |
17862df84da0SMatthew G. Knepley                |  1  0 -1 -1  1  0 |
17872df84da0SMatthew G. Knepley                \  1 -1  0 -1  0  1 /
17882df84da0SMatthew G. Knepley       */
17892df84da0SMatthew G. Knepley       coeff[dim * 0 + j] = (1. / 4.) * (verts[dim * 1 + j] + verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
17902df84da0SMatthew G. Knepley       coeff[dim * 1 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
17912df84da0SMatthew G. Knepley       coeff[dim * 2 + j] = (1. / 4.) * (-verts[dim * 0 + j] + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
17922df84da0SMatthew G. Knepley       coeff[dim * 3 + j] = (1. / 4.) * (-verts[dim * 1 + j] - verts[dim * 2 + j] + verts[dim * 4 + j] + verts[dim * 5 + j]);
17932df84da0SMatthew G. Knepley       coeff[dim * 4 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
17942df84da0SMatthew G. Knepley       coeff[dim * 5 + j] = (1. / 4.) * (verts[dim * 0 + j] - verts[dim * 1 + j] - verts[dim * 3 + j] + verts[dim * 5 + j]);
17952df84da0SMatthew G. Knepley       /* For reference prism:
17962df84da0SMatthew G. Knepley       {0, 0, 0}
17972df84da0SMatthew G. Knepley       {0, 1, 0}
17982df84da0SMatthew G. Knepley       {1, 0, 0}
17992df84da0SMatthew G. Knepley       {0, 0, 1}
18002df84da0SMatthew G. Knepley       {0, 0, 0}
18012df84da0SMatthew G. Knepley       {0, 0, 0}
18022df84da0SMatthew G. Knepley       */
18032df84da0SMatthew G. Knepley     }
18042df84da0SMatthew G. Knepley     for (i = 0; i < Nq; ++i) {
18052df84da0SMatthew G. Knepley       const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2];
18062df84da0SMatthew G. Knepley 
18072df84da0SMatthew G. Knepley       if (v) {
18082df84da0SMatthew G. Knepley         PetscReal extPoint[6];
18092df84da0SMatthew G. Knepley         PetscInt  c;
18102df84da0SMatthew G. Knepley 
18112df84da0SMatthew G. Knepley         extPoint[0] = 1.;
18122df84da0SMatthew G. Knepley         extPoint[1] = eta;
18132df84da0SMatthew G. Knepley         extPoint[2] = xi;
18142df84da0SMatthew G. Knepley         extPoint[3] = zeta;
18152df84da0SMatthew G. Knepley         extPoint[4] = xi * zeta;
18162df84da0SMatthew G. Knepley         extPoint[5] = eta * zeta;
18172df84da0SMatthew G. Knepley         for (c = 0; c < dim; ++c) {
18182df84da0SMatthew G. Knepley           PetscReal val = 0.;
18192df84da0SMatthew G. Knepley 
18209371c9d4SSatish Balay           for (k = 0; k < Nv; ++k) { val += extPoint[k] * coeff[k * dim + c]; }
18212df84da0SMatthew G. Knepley           v[i * dim + c] = val;
18222df84da0SMatthew G. Knepley         }
18232df84da0SMatthew G. Knepley       }
18242df84da0SMatthew G. Knepley       if (J) {
18252df84da0SMatthew G. Knepley         PetscReal extJ[18];
18262df84da0SMatthew G. Knepley 
18279371c9d4SSatish Balay         extJ[0]  = 0.;
18289371c9d4SSatish Balay         extJ[1]  = 0.;
18299371c9d4SSatish Balay         extJ[2]  = 0.;
18309371c9d4SSatish Balay         extJ[3]  = 0.;
18319371c9d4SSatish Balay         extJ[4]  = 1.;
18329371c9d4SSatish Balay         extJ[5]  = 0.;
18339371c9d4SSatish Balay         extJ[6]  = 1.;
18349371c9d4SSatish Balay         extJ[7]  = 0.;
18359371c9d4SSatish Balay         extJ[8]  = 0.;
18369371c9d4SSatish Balay         extJ[9]  = 0.;
18379371c9d4SSatish Balay         extJ[10] = 0.;
18389371c9d4SSatish Balay         extJ[11] = 1.;
18399371c9d4SSatish Balay         extJ[12] = zeta;
18409371c9d4SSatish Balay         extJ[13] = 0.;
18419371c9d4SSatish Balay         extJ[14] = xi;
18429371c9d4SSatish Balay         extJ[15] = 0.;
18439371c9d4SSatish Balay         extJ[16] = zeta;
18449371c9d4SSatish Balay         extJ[17] = eta;
18452df84da0SMatthew G. Knepley 
18462df84da0SMatthew G. Knepley         for (j = 0; j < dim; j++) {
18472df84da0SMatthew G. Knepley           for (k = 0; k < dimR; k++) {
18482df84da0SMatthew G. Knepley             PetscReal val = 0.;
18492df84da0SMatthew G. Knepley 
18509371c9d4SSatish Balay             for (l = 0; l < Nv; l++) { val += coeff[dim * l + j] * extJ[dimR * l + k]; }
18512df84da0SMatthew G. Knepley             J[i * dim * dim + dim * j + k] = val;
18522df84da0SMatthew G. Knepley           }
18532df84da0SMatthew G. Knepley         }
18542df84da0SMatthew G. Knepley         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
18552df84da0SMatthew G. Knepley         if (invJ) { DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]); }
18562df84da0SMatthew G. Knepley       }
18572df84da0SMatthew G. Knepley     }
18582df84da0SMatthew G. Knepley   }
18596858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, e, &isDG, &numCoords, &array, &coords));
18602df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
18612df84da0SMatthew G. Knepley }
18622df84da0SMatthew G. Knepley 
18639371c9d4SSatish Balay static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) {
1864ba2698f1SMatthew G. Knepley   DMPolytopeType   ct;
1865dfccc68fSToby Isaac   PetscInt         depth, dim, coordDim, coneSize, i;
1866dfccc68fSToby Isaac   PetscInt         Nq     = 0;
1867dfccc68fSToby Isaac   const PetscReal *points = NULL;
1868dfccc68fSToby Isaac   DMLabel          depthLabel;
1869c330f8ffSToby Isaac   PetscReal        xi0[3]   = {-1., -1., -1.}, v0[3], J0[9], detJ0;
1870dfccc68fSToby Isaac   PetscBool        isAffine = PETSC_TRUE;
1871dfccc68fSToby Isaac 
1872dfccc68fSToby Isaac   PetscFunctionBegin;
18739566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
18749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
18759566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
18769566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(depthLabel, cell, &dim));
18779371c9d4SSatish Balay   if (depth == 1 && dim == 1) { PetscCall(DMGetDimension(dm, &dim)); }
18789566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &coordDim));
187963a3b9bcSJacob Faibussowitsch   PetscCheck(coordDim <= 3, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %" PetscInt_FMT " > 3", coordDim);
18809566063dSJacob Faibussowitsch   if (quad) PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL));
18819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
1882ba2698f1SMatthew G. Knepley   switch (ct) {
1883ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_POINT:
18849566063dSJacob Faibussowitsch     PetscCall(DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ));
1885dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1886dfccc68fSToby Isaac     break;
1887ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
1888412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
18899566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
18909566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ));
1891dfccc68fSToby Isaac     break;
1892ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
18939566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
18949566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ));
1895dfccc68fSToby Isaac     break;
1896ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
18979566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ));
1898412e9a14SMatthew G. Knepley     isAffine = PETSC_FALSE;
1899412e9a14SMatthew G. Knepley     break;
1900412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
19019566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ));
1902dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1903dfccc68fSToby Isaac     break;
1904ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
19059566063dSJacob Faibussowitsch     if (Nq) PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0));
19069566063dSJacob Faibussowitsch     else PetscCall(DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ));
1907dfccc68fSToby Isaac     break;
1908ba2698f1SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
19099566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
1910dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1911dfccc68fSToby Isaac     break;
19122df84da0SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM:
19139566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ));
19142df84da0SMatthew G. Knepley     isAffine = PETSC_FALSE;
19152df84da0SMatthew G. Knepley     break;
19162df84da0SMatthew G. Knepley   default: 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))]);
1917dfccc68fSToby Isaac   }
19187318780aSToby Isaac   if (isAffine && Nq) {
1919dfccc68fSToby Isaac     if (v) {
19209371c9d4SSatish Balay       for (i = 0; i < Nq; i++) { CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]); }
1921dfccc68fSToby Isaac     }
19227318780aSToby Isaac     if (detJ) {
19239371c9d4SSatish Balay       for (i = 0; i < Nq; i++) { detJ[i] = detJ0; }
19247318780aSToby Isaac     }
19257318780aSToby Isaac     if (J) {
19267318780aSToby Isaac       PetscInt k;
19277318780aSToby Isaac 
19287318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1929dfccc68fSToby Isaac         PetscInt j;
1930dfccc68fSToby Isaac 
19319371c9d4SSatish Balay         for (j = 0; j < coordDim * coordDim; j++, k++) { J[k] = J0[j]; }
19327318780aSToby Isaac       }
19337318780aSToby Isaac     }
19347318780aSToby Isaac     if (invJ) {
19357318780aSToby Isaac       PetscInt k;
19367318780aSToby Isaac       switch (coordDim) {
19379371c9d4SSatish Balay       case 0: break;
19389371c9d4SSatish Balay       case 1: invJ[0] = 1. / J0[0]; break;
19399371c9d4SSatish Balay       case 2: DMPlex_Invert2D_Internal(invJ, J0, detJ0); break;
19409371c9d4SSatish Balay       case 3: DMPlex_Invert3D_Internal(invJ, J0, detJ0); break;
19417318780aSToby Isaac       }
19427318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
19437318780aSToby Isaac         PetscInt j;
19447318780aSToby Isaac 
19459371c9d4SSatish Balay         for (j = 0; j < coordDim * coordDim; j++, k++) { invJ[k] = invJ[j]; }
1946dfccc68fSToby Isaac       }
1947dfccc68fSToby Isaac     }
1948dfccc68fSToby Isaac   }
1949dfccc68fSToby Isaac   PetscFunctionReturn(0);
1950dfccc68fSToby Isaac }
1951dfccc68fSToby Isaac 
1952ccd2543fSMatthew G Knepley /*@C
19538e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1954ccd2543fSMatthew G Knepley 
1955d083f849SBarry Smith   Collective on dm
1956ccd2543fSMatthew G Knepley 
19574165533cSJose E. Roman   Input Parameters:
1958ccd2543fSMatthew G Knepley + dm   - the DM
1959ccd2543fSMatthew G Knepley - cell - the cell
1960ccd2543fSMatthew G Knepley 
19614165533cSJose E. Roman   Output Parameters:
19629b172b3aSMatthew Knepley + v0   - the translation part of this affine transform, meaning the translation to the origin (not the first vertex of the reference cell)
1963ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1964ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1965ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1966ccd2543fSMatthew G Knepley 
1967ccd2543fSMatthew G Knepley   Level: advanced
1968ccd2543fSMatthew G Knepley 
1969ccd2543fSMatthew G Knepley   Fortran Notes:
1970ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1971ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1972ccd2543fSMatthew G Knepley 
1973db781477SPatrick Sanan .seealso: `DMPlexComputeCellGeometryFEM()`, `DMGetCoordinateSection()`, `DMGetCoordinates()`
1974ccd2543fSMatthew G Knepley @*/
19759371c9d4SSatish Balay PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) {
1976ccd2543fSMatthew G Knepley   PetscFunctionBegin;
19779566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, NULL, v0, J, invJ, detJ));
19788e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
19798e0841e0SMatthew G. Knepley }
19808e0841e0SMatthew G. Knepley 
19819371c9d4SSatish Balay static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) {
19826858538eSMatthew G. Knepley   const PetscScalar *array;
19838e0841e0SMatthew G. Knepley   PetscScalar       *coords = NULL;
19846858538eSMatthew G. Knepley   PetscInt           numCoords;
19856858538eSMatthew G. Knepley   PetscBool          isDG;
19866858538eSMatthew G. Knepley   PetscQuadrature    feQuad;
19878e0841e0SMatthew G. Knepley   const PetscReal   *quadPoints;
1988ef0bb6c7SMatthew G. Knepley   PetscTabulation    T;
19896858538eSMatthew G. Knepley   PetscInt           dim, cdim, pdim, qdim, Nq, q;
19908e0841e0SMatthew G. Knepley 
19918e0841e0SMatthew G. Knepley   PetscFunctionBegin;
19929566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19939566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
19946858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
1995dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
1996dfccc68fSToby Isaac     PetscDualSpace dsp;
1997dfccc68fSToby Isaac 
19989566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(fe, &dsp));
19999566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetFunctional(dsp, 0, &quad));
20009566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2001dfccc68fSToby Isaac     Nq = 1;
2002dfccc68fSToby Isaac   } else {
20039566063dSJacob Faibussowitsch     PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL));
2004dfccc68fSToby Isaac   }
20059566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
20069566063dSJacob Faibussowitsch   PetscCall(PetscFEGetQuadrature(fe, &feQuad));
2007dfccc68fSToby Isaac   if (feQuad == quad) {
20089566063dSJacob Faibussowitsch     PetscCall(PetscFEGetCellTabulation(fe, J ? 1 : 0, &T));
200963a3b9bcSJacob 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);
2010dfccc68fSToby Isaac   } else {
20119566063dSJacob Faibussowitsch     PetscCall(PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T));
2012dfccc68fSToby Isaac   }
201363a3b9bcSJacob Faibussowitsch   PetscCheck(qdim == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %" PetscInt_FMT " != quadrature dimension %" PetscInt_FMT, dim, qdim);
2014ef0bb6c7SMatthew G. Knepley   {
2015ef0bb6c7SMatthew G. Knepley     const PetscReal *basis    = T->T[0];
2016ef0bb6c7SMatthew G. Knepley     const PetscReal *basisDer = T->T[1];
2017ef0bb6c7SMatthew G. Knepley     PetscReal        detJt;
2018ef0bb6c7SMatthew G. Knepley 
2019a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
202063a3b9bcSJacob Faibussowitsch     PetscCheck(Nq == T->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %" PetscInt_FMT " != %" PetscInt_FMT, Nq, T->Np);
202163a3b9bcSJacob Faibussowitsch     PetscCheck(pdim == T->Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %" PetscInt_FMT " != %" PetscInt_FMT, pdim, T->Nb);
202263a3b9bcSJacob Faibussowitsch     PetscCheck(dim == T->Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %" PetscInt_FMT " != %" PetscInt_FMT, dim, T->Nc);
202363a3b9bcSJacob Faibussowitsch     PetscCheck(cdim == T->cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %" PetscInt_FMT " != %" PetscInt_FMT, cdim, T->cdim);
2024a2a9e04cSMatthew G. Knepley #endif
2025dfccc68fSToby Isaac     if (v) {
20269566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(v, Nq * cdim));
2027f960e424SToby Isaac       for (q = 0; q < Nq; ++q) {
2028f960e424SToby Isaac         PetscInt i, k;
2029f960e424SToby Isaac 
2030301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2031301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
20329371c9d4SSatish Balay           for (i = 0; i < cdim; ++i) { v[q * cdim + i] += basis[(q * pdim + k) * cdim + i] * PetscRealPart(coords[vertex * cdim + i]); }
2033301b184aSMatthew G. Knepley         }
20349566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * cdim));
2035f960e424SToby Isaac       }
2036f960e424SToby Isaac     }
20378e0841e0SMatthew G. Knepley     if (J) {
20389566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(J, Nq * cdim * cdim));
20398e0841e0SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
20408e0841e0SMatthew G. Knepley         PetscInt i, j, k, c, r;
20418e0841e0SMatthew G. Knepley 
20428e0841e0SMatthew G. Knepley         /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
2043301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
2044301b184aSMatthew G. Knepley           const PetscInt vertex = k / cdim;
2045301b184aSMatthew G. Knepley           for (j = 0; j < dim; ++j) {
20469371c9d4SSatish Balay             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]); }
2047301b184aSMatthew G. Knepley           }
2048301b184aSMatthew G. Knepley         }
20499566063dSJacob Faibussowitsch         PetscCall(PetscLogFlops(2.0 * pdim * dim * cdim));
20508e0841e0SMatthew G. Knepley         if (cdim > dim) {
20518e0841e0SMatthew G. Knepley           for (c = dim; c < cdim; ++c)
20529371c9d4SSatish Balay             for (r = 0; r < cdim; ++r) J[r * cdim + c] = r == c ? 1.0 : 0.0;
20538e0841e0SMatthew G. Knepley         }
2054f960e424SToby Isaac         if (!detJ && !invJ) continue;
2055a63b72c6SToby Isaac         detJt = 0.;
20568e0841e0SMatthew G. Knepley         switch (cdim) {
20578e0841e0SMatthew G. Knepley         case 3:
2058037dc194SToby Isaac           DMPlex_Det3D_Internal(&detJt, &J[q * cdim * dim]);
2059037dc194SToby Isaac           if (invJ) { DMPlex_Invert3D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt); }
206017fe8556SMatthew G. Knepley           break;
206149dc4407SMatthew G. Knepley         case 2:
20629f328543SToby Isaac           DMPlex_Det2D_Internal(&detJt, &J[q * cdim * dim]);
2063037dc194SToby Isaac           if (invJ) { DMPlex_Invert2D_Internal(&invJ[q * cdim * dim], &J[q * cdim * dim], detJt); }
206449dc4407SMatthew G. Knepley           break;
20658e0841e0SMatthew G. Knepley         case 1:
2066037dc194SToby Isaac           detJt = J[q * cdim * dim];
2067037dc194SToby Isaac           if (invJ) invJ[q * cdim * dim] = 1.0 / detJt;
206849dc4407SMatthew G. Knepley         }
2069f960e424SToby Isaac         if (detJ) detJ[q] = detJt;
207049dc4407SMatthew G. Knepley       }
207108401ef6SPierre Jolivet     } else PetscCheck(!detJ && !invJ, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
207249dc4407SMatthew G. Knepley   }
20739566063dSJacob Faibussowitsch   if (feQuad != quad) PetscCall(PetscTabulationDestroy(&T));
20746858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, point, &isDG, &numCoords, &array, &coords));
20758e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
20768e0841e0SMatthew G. Knepley }
20778e0841e0SMatthew G. Knepley 
20788e0841e0SMatthew G. Knepley /*@C
20798e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
20808e0841e0SMatthew G. Knepley 
2081d083f849SBarry Smith   Collective on dm
20828e0841e0SMatthew G. Knepley 
20834165533cSJose E. Roman   Input Parameters:
20848e0841e0SMatthew G. Knepley + dm   - the DM
20858e0841e0SMatthew G. Knepley . cell - the cell
2086dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
2087dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
20888e0841e0SMatthew G. Knepley 
20894165533cSJose E. Roman   Output Parameters:
2090dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
20918e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
20928e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
20938e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
20948e0841e0SMatthew G. Knepley 
20958e0841e0SMatthew G. Knepley   Level: advanced
20968e0841e0SMatthew G. Knepley 
20978e0841e0SMatthew G. Knepley   Fortran Notes:
20988e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
20998e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
21008e0841e0SMatthew G. Knepley 
2101db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()`
21028e0841e0SMatthew G. Knepley @*/
21039371c9d4SSatish Balay PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ) {
2104bb4a5db5SMatthew G. Knepley   DM      cdm;
2105dfccc68fSToby Isaac   PetscFE fe = NULL;
21068e0841e0SMatthew G. Knepley 
21078e0841e0SMatthew G. Knepley   PetscFunctionBegin;
2108dadcf809SJacob Faibussowitsch   PetscValidRealPointer(detJ, 7);
21099566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
2110bb4a5db5SMatthew G. Knepley   if (cdm) {
2111dfccc68fSToby Isaac     PetscClassId id;
2112dfccc68fSToby Isaac     PetscInt     numFields;
2113e5e52638SMatthew G. Knepley     PetscDS      prob;
2114dfccc68fSToby Isaac     PetscObject  disc;
2115dfccc68fSToby Isaac 
21169566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(cdm, &numFields));
2117dfccc68fSToby Isaac     if (numFields) {
21189566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &prob));
21199566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(prob, 0, &disc));
21209566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
21219371c9d4SSatish Balay       if (id == PETSCFE_CLASSID) { fe = (PetscFE)disc; }
2122dfccc68fSToby Isaac     }
2123dfccc68fSToby Isaac   }
21249566063dSJacob Faibussowitsch   if (!fe) PetscCall(DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ));
21259566063dSJacob Faibussowitsch   else PetscCall(DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ));
2126ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
2127ccd2543fSMatthew G Knepley }
2128834e62ceSMatthew G. Knepley 
21299371c9d4SSatish Balay static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) {
21309bf2564aSMatt McGurn   PetscSection       coordSection;
21319bf2564aSMatt McGurn   Vec                coordinates;
21329bf2564aSMatt McGurn   const PetscScalar *coords = NULL;
21339bf2564aSMatt McGurn   PetscInt           d, dof, off;
21349bf2564aSMatt McGurn 
21359bf2564aSMatt McGurn   PetscFunctionBegin;
21369566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
21379566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
21389566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, &coords));
21399bf2564aSMatt McGurn 
21409bf2564aSMatt McGurn   /* for a point the centroid is just the coord */
21419bf2564aSMatt McGurn   if (centroid) {
21429566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
21439566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
21449371c9d4SSatish Balay     for (d = 0; d < dof; d++) { centroid[d] = PetscRealPart(coords[off + d]); }
21459bf2564aSMatt McGurn   }
21469bf2564aSMatt McGurn   if (normal) {
21479bf2564aSMatt McGurn     const PetscInt *support, *cones;
21489bf2564aSMatt McGurn     PetscInt        supportSize;
21499bf2564aSMatt McGurn     PetscReal       norm, sign;
21509bf2564aSMatt McGurn 
21519bf2564aSMatt McGurn     /* compute the norm based upon the support centroids */
21529566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, cell, &supportSize));
21539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, cell, &support));
21549566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL));
21559bf2564aSMatt McGurn 
21569bf2564aSMatt McGurn     /* Take the normal from the centroid of the support to the vertex*/
21579566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, cell, &dof));
21589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, cell, &off));
21599371c9d4SSatish Balay     for (d = 0; d < dof; d++) { normal[d] -= PetscRealPart(coords[off + d]); }
21609bf2564aSMatt McGurn 
21619bf2564aSMatt McGurn     /* Determine the sign of the normal based upon its location in the support */
21629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, support[0], &cones));
21639bf2564aSMatt McGurn     sign = cones[0] == cell ? 1.0 : -1.0;
21649bf2564aSMatt McGurn 
21659bf2564aSMatt McGurn     norm = DMPlex_NormD_Internal(dim, normal);
21669bf2564aSMatt McGurn     for (d = 0; d < dim; ++d) normal[d] /= (norm * sign);
21679bf2564aSMatt McGurn   }
21689371c9d4SSatish Balay   if (vol) { *vol = 1.0; }
21699566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, &coords));
21709bf2564aSMatt McGurn   PetscFunctionReturn(0);
21719bf2564aSMatt McGurn }
21729bf2564aSMatt McGurn 
21739371c9d4SSatish Balay static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) {
21746858538eSMatthew G. Knepley   const PetscScalar *array;
2175a1e44745SMatthew G. Knepley   PetscScalar       *coords = NULL;
2176714b99b6SMatthew G. Knepley   PetscInt           coordSize, d;
21776858538eSMatthew G. Knepley   PetscBool          isDG;
2178cc08537eSMatthew G. Knepley 
2179cc08537eSMatthew G. Knepley   PetscFunctionBegin;
21806858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2181cc08537eSMatthew G. Knepley   if (centroid) {
21826858538eSMatthew G. Knepley     for (d = 0; d < dim; ++d) centroid[d] = 0.5 * PetscRealPart(coords[d] + coords[dim + d]);
2183cc08537eSMatthew G. Knepley   }
2184cc08537eSMatthew G. Knepley   if (normal) {
2185a60a936bSMatthew G. Knepley     PetscReal norm;
2186a60a936bSMatthew G. Knepley 
218708401ef6SPierre Jolivet     PetscCheck(dim == 2, PETSC_COMM_SELF, PETSC_ERR_SUP, "We only support 2D edges right now");
21886858538eSMatthew G. Knepley     normal[0] = -PetscRealPart(coords[1] - coords[dim + 1]);
21896858538eSMatthew G. Knepley     normal[1] = PetscRealPart(coords[0] - coords[dim + 0]);
2190714b99b6SMatthew G. Knepley     norm      = DMPlex_NormD_Internal(dim, normal);
2191714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) normal[d] /= norm;
2192cc08537eSMatthew G. Knepley   }
2193cc08537eSMatthew G. Knepley   if (vol) {
2194714b99b6SMatthew G. Knepley     *vol = 0.0;
21956858538eSMatthew G. Knepley     for (d = 0; d < dim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - coords[dim + d]));
2196714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
2197cc08537eSMatthew G. Knepley   }
21986858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2199cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
2200cc08537eSMatthew G. Knepley }
2201cc08537eSMatthew G. Knepley 
2202cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */
22039371c9d4SSatish Balay static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) {
2204412e9a14SMatthew G. Knepley   DMPolytopeType     ct;
22056858538eSMatthew G. Knepley   const PetscScalar *array;
2206cc08537eSMatthew G. Knepley   PetscScalar       *coords = NULL;
22076858538eSMatthew G. Knepley   PetscInt           coordSize;
22086858538eSMatthew G. Knepley   PetscBool          isDG;
2209793a2a13SMatthew G. Knepley   PetscInt           fv[4] = {0, 1, 2, 3};
22106858538eSMatthew G. Knepley   PetscInt           cdim, numCorners, p, d;
2211cc08537eSMatthew G. Knepley 
2212cc08537eSMatthew G. Knepley   PetscFunctionBegin;
2213793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
22149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2215412e9a14SMatthew G. Knepley   switch (ct) {
22169371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR:
22179371c9d4SSatish Balay     fv[2] = 3;
22189371c9d4SSatish Balay     fv[3] = 2;
22199371c9d4SSatish Balay     break;
2220412e9a14SMatthew G. Knepley   default: break;
2221412e9a14SMatthew G. Knepley   }
22229566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
22236858538eSMatthew G. Knepley   PetscCall(DMPlexGetConeSize(dm, cell, &numCorners));
22246858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
22253f27a4e6SJed Brown   {
22263f27a4e6SJed Brown     PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm;
2227793a2a13SMatthew G. Knepley 
22283f27a4e6SJed Brown     for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]);
22294f99dae5SMatthew G. Knepley     for (p = 0; p < numCorners - 2; ++p) {
22303f27a4e6SJed Brown       PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.};
22313f27a4e6SJed Brown       for (d = 0; d < cdim; d++) {
22323f27a4e6SJed Brown         e0[d] = PetscRealPart(coords[cdim * fv[p + 1] + d]) - origin[d];
22333f27a4e6SJed Brown         e1[d] = PetscRealPart(coords[cdim * fv[p + 2] + d]) - origin[d];
22343f27a4e6SJed Brown       }
22353f27a4e6SJed Brown       const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1];
22363f27a4e6SJed Brown       const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2];
22373f27a4e6SJed Brown       const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0];
22383f27a4e6SJed Brown       const PetscReal a  = PetscSqrtReal(dx * dx + dy * dy + dz * dz);
22394f99dae5SMatthew G. Knepley 
22404f99dae5SMatthew G. Knepley       n[0] += dx;
22414f99dae5SMatthew G. Knepley       n[1] += dy;
22424f99dae5SMatthew G. Knepley       n[2] += dz;
22439371c9d4SSatish Balay       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.; }
2244ceee4971SMatthew G. Knepley     }
22454f99dae5SMatthew G. Knepley     norm = PetscSqrtReal(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
22464f99dae5SMatthew G. Knepley     n[0] /= norm;
22474f99dae5SMatthew G. Knepley     n[1] /= norm;
22484f99dae5SMatthew G. Knepley     n[2] /= norm;
22494f99dae5SMatthew G. Knepley     c[0] /= norm;
22504f99dae5SMatthew G. Knepley     c[1] /= norm;
22514f99dae5SMatthew G. Knepley     c[2] /= norm;
22524f99dae5SMatthew G. Knepley     if (vol) *vol = 0.5 * norm;
22539371c9d4SSatish Balay     if (centroid)
22549371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) centroid[d] = c[d];
22559371c9d4SSatish Balay     if (normal)
22569371c9d4SSatish Balay       for (d = 0; d < cdim; ++d) normal[d] = n[d];
22570a1d6728SMatthew G. Knepley   }
22586858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
2259cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
2260cc08537eSMatthew G. Knepley }
2261cc08537eSMatthew G. Knepley 
22620ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */
22639371c9d4SSatish Balay static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) {
2264412e9a14SMatthew G. Knepley   DMPolytopeType        ct;
22656858538eSMatthew G. Knepley   const PetscScalar    *array;
22660ec8681fSMatthew G. Knepley   PetscScalar          *coords = NULL;
22676858538eSMatthew G. Knepley   PetscInt              coordSize;
22686858538eSMatthew G. Knepley   PetscBool             isDG;
22693f27a4e6SJed Brown   PetscReal             vsum      = 0.0, vtmp, coordsTmp[3 * 3], origin[3];
22706858538eSMatthew G. Knepley   const PetscInt        order[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
22716858538eSMatthew G. Knepley   const PetscInt       *cone, *faceSizes, *faces;
22726858538eSMatthew G. Knepley   const DMPolytopeType *faceTypes;
2273793a2a13SMatthew G. Knepley   PetscBool             isHybrid = PETSC_FALSE;
22746858538eSMatthew G. Knepley   PetscInt              numFaces, f, fOff = 0, p, d;
22750ec8681fSMatthew G. Knepley 
22760ec8681fSMatthew G. Knepley   PetscFunctionBegin;
227763a3b9bcSJacob Faibussowitsch   PetscCheck(dim <= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No support for dim %" PetscInt_FMT " > 3", dim);
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) {
2281412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
2282412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
2283412e9a14SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR:
22849371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: isHybrid = PETSC_TRUE;
2285412e9a14SMatthew G. Knepley   default: break;
2286412e9a14SMatthew G. Knepley   }
2287793a2a13SMatthew G. Knepley 
22889371c9d4SSatish Balay   if (centroid)
22899371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = 0.0;
22906858538eSMatthew G. Knepley   PetscCall(DMPlexGetCone(dm, cell, &cone));
22916858538eSMatthew G. Knepley 
22926858538eSMatthew G. Knepley   // Using the closure of faces for coordinates does not work in periodic geometries, so we index into the cell coordinates
22936858538eSMatthew G. Knepley   PetscCall(DMPlexGetRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
22946858538eSMatthew G. Knepley   PetscCall(DMPlexGetCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
22950ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
2296793a2a13SMatthew G. Knepley     PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
2297793a2a13SMatthew G. Knepley 
22983f27a4e6SJed Brown     // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and
22993f27a4e6SJed Brown     // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex
23003f27a4e6SJed Brown     // so that all tetrahedra have positive volume.
23019371c9d4SSatish Balay     if (f == 0)
23029371c9d4SSatish Balay       for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]);
23036858538eSMatthew G. Knepley     switch (faceTypes[f]) {
2304ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
23050ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
23066858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + 0] * dim + d]) - origin[d];
23076858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + 1] * dim + d]) - origin[d];
23086858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + 2] * dim + d]) - origin[d];
23090ec8681fSMatthew G. Knepley       }
23100ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
23116858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
23120ec8681fSMatthew G. Knepley       vsum += vtmp;
23134f25033aSJed Brown       if (centroid) { /* Centroid of OABC = (a+b+c)/4 */
23140ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
23151ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
23160ec8681fSMatthew G. Knepley         }
23170ec8681fSMatthew G. Knepley       }
23180ec8681fSMatthew G. Knepley       break;
2319ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
23209371c9d4SSatish Balay     case DM_POLYTOPE_SEG_PRISM_TENSOR: {
2321793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
2322793a2a13SMatthew G. Knepley 
2323793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
23249371c9d4SSatish Balay       if (isHybrid && f > 1) {
23259371c9d4SSatish Balay         fv[2] = 3;
23269371c9d4SSatish Balay         fv[3] = 2;
23279371c9d4SSatish Balay       }
23280ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
23290ec8681fSMatthew G. Knepley       /* First tet */
23300ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
23316858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[0]] * dim + d]) - origin[d];
23326858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
23336858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
23340ec8681fSMatthew G. Knepley       }
23350ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
23366858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
23370ec8681fSMatthew G. Knepley       vsum += vtmp;
23380ec8681fSMatthew G. Knepley       if (centroid) {
23390ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
23400ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
23410ec8681fSMatthew G. Knepley         }
23420ec8681fSMatthew G. Knepley       }
23430ec8681fSMatthew G. Knepley       /* Second tet */
23440ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
23456858538eSMatthew G. Knepley         coordsTmp[0 * dim + d] = PetscRealPart(coords[faces[fOff + fv[1]] * dim + d]) - origin[d];
23466858538eSMatthew G. Knepley         coordsTmp[1 * dim + d] = PetscRealPart(coords[faces[fOff + fv[2]] * dim + d]) - origin[d];
23476858538eSMatthew G. Knepley         coordsTmp[2 * dim + d] = PetscRealPart(coords[faces[fOff + fv[3]] * dim + d]) - origin[d];
23480ec8681fSMatthew G. Knepley       }
23490ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
23506858538eSMatthew G. Knepley       if (flip) vtmp = -vtmp;
23510ec8681fSMatthew G. Knepley       vsum += vtmp;
23520ec8681fSMatthew G. Knepley       if (centroid) {
23530ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
23540ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p * dim + d] * vtmp;
23550ec8681fSMatthew G. Knepley         }
23560ec8681fSMatthew G. Knepley       }
23570ec8681fSMatthew G. Knepley       break;
2358793a2a13SMatthew G. Knepley     }
23599371c9d4SSatish Balay     default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %" PetscInt_FMT " of type %s", cone[f], DMPolytopeTypes[ct]);
23600ec8681fSMatthew G. Knepley     }
23616858538eSMatthew G. Knepley     fOff += faceSizes[f];
23620ec8681fSMatthew G. Knepley   }
23636858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, order, &numFaces, &faceTypes, &faceSizes, &faces));
23646858538eSMatthew G. Knepley   PetscCall(DMPlexRestoreCellCoordinates(dm, cell, &isDG, &coordSize, &array, &coords));
23658763be8eSMatthew G. Knepley   if (vol) *vol = PetscAbsReal(vsum);
23669371c9d4SSatish Balay   if (normal)
23679371c9d4SSatish Balay     for (d = 0; d < dim; ++d) normal[d] = 0.0;
23689371c9d4SSatish Balay   if (centroid)
23699371c9d4SSatish Balay     for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum * 4) + origin[d];
23700ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
23710ec8681fSMatthew G. Knepley }
23720ec8681fSMatthew G. Knepley 
2373834e62ceSMatthew G. Knepley /*@C
2374834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
2375834e62ceSMatthew G. Knepley 
2376d083f849SBarry Smith   Collective on dm
2377834e62ceSMatthew G. Knepley 
23784165533cSJose E. Roman   Input Parameters:
2379834e62ceSMatthew G. Knepley + dm   - the DM
2380834e62ceSMatthew G. Knepley - cell - the cell
2381834e62ceSMatthew G. Knepley 
23824165533cSJose E. Roman   Output Parameters:
2383834e62ceSMatthew G. Knepley + volume   - the cell volume
2384cc08537eSMatthew G. Knepley . centroid - the cell centroid
2385cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
2386834e62ceSMatthew G. Knepley 
2387834e62ceSMatthew G. Knepley   Level: advanced
2388834e62ceSMatthew G. Knepley 
2389834e62ceSMatthew G. Knepley   Fortran Notes:
2390834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2391834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
2392834e62ceSMatthew G. Knepley 
2393db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetCoordinates()`
2394834e62ceSMatthew G. Knepley @*/
23959371c9d4SSatish Balay PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) {
23960ec8681fSMatthew G. Knepley   PetscInt depth, dim;
2397834e62ceSMatthew G. Knepley 
2398834e62ceSMatthew G. Knepley   PetscFunctionBegin;
23999566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
24009566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
240108401ef6SPierre Jolivet   PetscCheck(depth == dim, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
24029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetPointDepth(dm, cell, &depth));
2403011ea5d8SMatthew G. Knepley   switch (depth) {
24049371c9d4SSatish Balay   case 0: PetscCall(DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal)); break;
24059371c9d4SSatish Balay   case 1: PetscCall(DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal)); break;
24069371c9d4SSatish Balay   case 2: PetscCall(DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal)); break;
24079371c9d4SSatish Balay   case 3: PetscCall(DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal)); break;
24089371c9d4SSatish Balay   default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT " (depth %" PetscInt_FMT ") for element geometry computation", dim, depth);
2409834e62ceSMatthew G. Knepley   }
2410834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
2411834e62ceSMatthew G. Knepley }
2412113c68e6SMatthew G. Knepley 
2413c501906fSMatthew G. Knepley /*@
2414c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
2415c501906fSMatthew G. Knepley 
2416c501906fSMatthew G. Knepley   Collective on dm
2417c501906fSMatthew G. Knepley 
2418c501906fSMatthew G. Knepley   Input Parameter:
2419c501906fSMatthew G. Knepley . dm - The DMPlex
2420c501906fSMatthew G. Knepley 
2421c501906fSMatthew G. Knepley   Output Parameter:
2422c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
2423c501906fSMatthew G. Knepley 
2424c501906fSMatthew G. Knepley   Level: beginner
2425c501906fSMatthew G. Knepley 
2426c501906fSMatthew G. Knepley @*/
24279371c9d4SSatish Balay PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom) {
2428c0d900a5SMatthew G. Knepley   DM           dmCell;
2429c0d900a5SMatthew G. Knepley   Vec          coordinates;
2430c0d900a5SMatthew G. Knepley   PetscSection coordSection, sectionCell;
2431c0d900a5SMatthew G. Knepley   PetscScalar *cgeom;
2432412e9a14SMatthew G. Knepley   PetscInt     cStart, cEnd, c;
2433c0d900a5SMatthew G. Knepley 
2434c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
24359566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmCell));
24369566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
24379566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
24389566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection));
24399566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmCell, coordinates));
24409566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionCell));
24419566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
24429566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd));
2443c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
24449566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFEGeom)) / sizeof(PetscScalar))));
24459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionCell));
24469566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmCell, sectionCell));
24479566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionCell));
24489566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmCell, cellgeom));
24499566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*cellgeom, &cgeom));
2450c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2451cf0b7c11SKarl Rupp     PetscFEGeom *cg;
2452c0d900a5SMatthew G. Knepley 
24539566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg));
24549566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(cg, 1));
24559566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ));
245663a3b9bcSJacob Faibussowitsch     PetscCheck(*cg->detJ > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %" PetscInt_FMT, (double)*cg->detJ, c);
2457c0d900a5SMatthew G. Knepley   }
2458c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
2459c0d900a5SMatthew G. Knepley }
2460c0d900a5SMatthew G. Knepley 
2461891a9168SMatthew G. Knepley /*@
2462891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2463891a9168SMatthew G. Knepley 
2464891a9168SMatthew G. Knepley   Input Parameter:
2465891a9168SMatthew G. Knepley . dm - The DM
2466891a9168SMatthew G. Knepley 
2467891a9168SMatthew G. Knepley   Output Parameters:
2468891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2469a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data
2470891a9168SMatthew G. Knepley 
2471891a9168SMatthew G. Knepley   Level: developer
2472891a9168SMatthew G. Knepley 
2473db781477SPatrick Sanan .seealso: `PetscFVFaceGeom`, `PetscFVCellGeom`, `DMPlexComputeGeometryFEM()`
2474891a9168SMatthew G. Knepley @*/
24759371c9d4SSatish Balay PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) {
2476113c68e6SMatthew G. Knepley   DM           dmFace, dmCell;
2477113c68e6SMatthew G. Knepley   DMLabel      ghostLabel;
2478113c68e6SMatthew G. Knepley   PetscSection sectionFace, sectionCell;
2479113c68e6SMatthew G. Knepley   PetscSection coordSection;
2480113c68e6SMatthew G. Knepley   Vec          coordinates;
2481113c68e6SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2482113c68e6SMatthew G. Knepley   PetscReal    minradius, gminradius;
2483113c68e6SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2484113c68e6SMatthew G. Knepley 
2485113c68e6SMatthew G. Knepley   PetscFunctionBegin;
24869566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24879566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
24889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
2489113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
24909566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmCell));
24919566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection));
24929566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmCell, coordinates));
24939566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionCell));
24949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
24959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
24969566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionCell, cStart, cEnd));
24979566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionCell, c, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVCellGeom)) / sizeof(PetscScalar))));
24989566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionCell));
24999566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmCell, sectionCell));
25009566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionCell));
25019566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmCell, cellgeom));
2502485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
25039566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*cellgeom, &cgeom));
2504113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2505113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2506113c68e6SMatthew G. Knepley 
25079566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmCell, c, cgeom, &cg));
25089566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(cg, 1));
25099566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL));
2510113c68e6SMatthew G. Knepley   }
2511113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
25129566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dmFace));
25139566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionFace));
25149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
25159566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionFace, fStart, fEnd));
25169566063dSJacob Faibussowitsch   for (f = fStart; f < fEnd; ++f) PetscCall(PetscSectionSetDof(sectionFace, f, (PetscInt)PetscCeilReal(((PetscReal)sizeof(PetscFVFaceGeom)) / sizeof(PetscScalar))));
25179566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionFace));
25189566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dmFace, sectionFace));
25199566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionFace));
25209566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dmFace, facegeom));
25219566063dSJacob Faibussowitsch   PetscCall(VecGetArray(*facegeom, &fgeom));
25229566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2523113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2524113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2525113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2526113c68e6SMatthew G. Knepley     PetscReal        area;
2527412e9a14SMatthew G. Knepley     const PetscInt  *cells;
2528412e9a14SMatthew G. Knepley     PetscInt         ncells, ghost = -1, d, numChildren;
2529113c68e6SMatthew G. Knepley 
25309566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
25319566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
25329566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, f, &cells));
25339566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &ncells));
2534412e9a14SMatthew G. Knepley     /* It is possible to get a face with no support when using partition overlap */
2535412e9a14SMatthew G. Knepley     if (!ncells || ghost >= 0 || numChildren) continue;
25369566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, f, fgeom, &fg));
25379566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal));
2538113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2539113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2540113c68e6SMatthew G. Knepley     {
2541113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
2542113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
25430453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2544113c68e6SMatthew G. Knepley 
25459566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL));
2546113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
254706348e87SToby Isaac       if (ncells > 1) {
25489566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR));
2549113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
25509371c9d4SSatish Balay       } else {
255106348e87SToby Isaac         rcentroid = fg->centroid;
255206348e87SToby Isaac       }
25539566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l));
25549566063dSJacob Faibussowitsch       PetscCall(DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r));
25550453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2556113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2557113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2558113c68e6SMatthew G. Knepley       }
2559113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
256063a3b9bcSJacob 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]);
256163a3b9bcSJacob 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]);
256263a3b9bcSJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Direction for face %" PetscInt_FMT " could not be fixed", f);
2563113c68e6SMatthew G. Knepley       }
2564113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2565113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2566113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2567113c68e6SMatthew G. Knepley       }
256806348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2569113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2570113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2571113c68e6SMatthew G. Knepley       }
2572113c68e6SMatthew G. Knepley     }
2573113c68e6SMatthew G. Knepley   }
25741c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm)));
25759566063dSJacob Faibussowitsch   PetscCall(DMPlexSetMinRadius(dm, gminradius));
2576113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2577113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2578113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2579113c68e6SMatthew G. Knepley     const PetscInt  *cone, *support;
2580113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2581113c68e6SMatthew G. Knepley 
25829566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dmCell, c, &coneSize));
258363a3b9bcSJacob Faibussowitsch     PetscCheck(coneSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %" PetscInt_FMT " has cone size %" PetscInt_FMT " != 1", c, coneSize);
25849566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dmCell, c, &cone));
25859566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmCell, cone[0], &supportSize));
258663a3b9bcSJacob Faibussowitsch     PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " has support size %" PetscInt_FMT " != 2", cone[0], supportSize);
25879566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dmCell, cone[0], &support));
25889566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg));
2589113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2590113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2591113c68e6SMatthew G. Knepley       if (support[s] == c) {
2592640bce14SSatish Balay         PetscFVCellGeom *ci;
2593113c68e6SMatthew G. Knepley         PetscFVCellGeom *cg;
2594113c68e6SMatthew G. Knepley         PetscReal        c2f[3], a;
2595113c68e6SMatthew G. Knepley 
25969566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRead(dmCell, support[(s + 1) % 2], cgeom, &ci));
2597113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2598113c68e6SMatthew G. Knepley         a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal) / DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
25999566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg));
2600113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2 * a, fg->normal, ci->centroid, cg->centroid);
2601113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2602113c68e6SMatthew G. Knepley       }
2603113c68e6SMatthew G. Knepley     }
2604113c68e6SMatthew G. Knepley   }
26059566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*facegeom, &fgeom));
26069566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(*cellgeom, &cgeom));
26079566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmCell));
26089566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dmFace));
2609113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2610113c68e6SMatthew G. Knepley }
2611113c68e6SMatthew G. Knepley 
2612113c68e6SMatthew G. Knepley /*@C
2613113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2614113c68e6SMatthew G. Knepley 
2615113c68e6SMatthew G. Knepley   Not collective
2616113c68e6SMatthew G. Knepley 
26174165533cSJose E. Roman   Input Parameter:
2618113c68e6SMatthew G. Knepley . dm - the DM
2619113c68e6SMatthew G. Knepley 
26204165533cSJose E. Roman   Output Parameter:
2621a5b23f4aSJose E. Roman . minradius - the minimum cell radius
2622113c68e6SMatthew G. Knepley 
2623113c68e6SMatthew G. Knepley   Level: developer
2624113c68e6SMatthew G. Knepley 
2625db781477SPatrick Sanan .seealso: `DMGetCoordinates()`
2626113c68e6SMatthew G. Knepley @*/
26279371c9d4SSatish Balay PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) {
2628113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2629113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2630dadcf809SJacob Faibussowitsch   PetscValidRealPointer(minradius, 2);
2631113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex *)dm->data)->minradius;
2632113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2633113c68e6SMatthew G. Knepley }
2634113c68e6SMatthew G. Knepley 
2635113c68e6SMatthew G. Knepley /*@C
2636113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2637113c68e6SMatthew G. Knepley 
2638113c68e6SMatthew G. Knepley   Logically collective
2639113c68e6SMatthew G. Knepley 
26404165533cSJose E. Roman   Input Parameters:
2641113c68e6SMatthew G. Knepley + dm - the DM
2642a5b23f4aSJose E. Roman - minradius - the minimum cell radius
2643113c68e6SMatthew G. Knepley 
2644113c68e6SMatthew G. Knepley   Level: developer
2645113c68e6SMatthew G. Knepley 
2646db781477SPatrick Sanan .seealso: `DMSetCoordinates()`
2647113c68e6SMatthew G. Knepley @*/
26489371c9d4SSatish Balay PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) {
2649113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2650113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2651113c68e6SMatthew G. Knepley   ((DM_Plex *)dm->data)->minradius = minradius;
2652113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2653113c68e6SMatthew G. Knepley }
2654856ac710SMatthew G. Knepley 
26559371c9d4SSatish Balay static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) {
2656856ac710SMatthew G. Knepley   DMLabel      ghostLabel;
2657856ac710SMatthew G. Knepley   PetscScalar *dx, *grad, **gref;
2658856ac710SMatthew G. Knepley   PetscInt     dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2659856ac710SMatthew G. Knepley 
2660856ac710SMatthew G. Knepley   PetscFunctionBegin;
26619566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
26629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
26639566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2664089217ebSMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
26659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxNumFaces, NULL));
26669566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
26679566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
26689566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2669856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2670856ac710SMatthew G. Knepley     const PetscInt  *faces;
2671856ac710SMatthew G. Knepley     PetscInt         numFaces, usedFaces, f, d;
2672640bce14SSatish Balay     PetscFVCellGeom *cg;
2673856ac710SMatthew G. Knepley     PetscBool        boundary;
2674856ac710SMatthew G. Knepley     PetscInt         ghost;
2675856ac710SMatthew G. Knepley 
2676a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
2677a79418b7SMatt McGurn     PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2678a79418b7SMatt McGurn     if (ghost >= 0) continue;
2679a79418b7SMatt McGurn 
26809566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
26819566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &numFaces));
26829566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &faces));
268363a3b9bcSJacob 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);
2684856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2685640bce14SSatish Balay       PetscFVCellGeom *cg1;
2686856ac710SMatthew G. Knepley       PetscFVFaceGeom *fg;
2687856ac710SMatthew G. Knepley       const PetscInt  *fcells;
2688856ac710SMatthew G. Knepley       PetscInt         ncell, side;
2689856ac710SMatthew G. Knepley 
26909566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
26919566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2692856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
26939566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, faces[f], &fcells));
2694856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2695856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
26969566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg));
26979566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2698856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces * dim + d] = cg1->centroid[d] - cg->centroid[d];
2699856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */
2700856ac710SMatthew G. Knepley     }
270128b400f6SJacob Faibussowitsch     PetscCheck(usedFaces, PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
27029566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, usedFaces, dx, grad));
2703856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
27049566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(ghostLabel, faces[f], &ghost));
27059566063dSJacob Faibussowitsch       PetscCall(DMIsBoundaryPoint(dm, faces[f], &boundary));
2706856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2707856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces * dim + d];
2708856ac710SMatthew G. Knepley       ++usedFaces;
2709856ac710SMatthew G. Knepley     }
2710856ac710SMatthew G. Knepley   }
27119566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
2712856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2713856ac710SMatthew G. Knepley }
2714856ac710SMatthew G. Knepley 
27159371c9d4SSatish Balay static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) {
2716b81db932SToby Isaac   DMLabel      ghostLabel;
2717b81db932SToby Isaac   PetscScalar *dx, *grad, **gref;
2718b81db932SToby Isaac   PetscInt     dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2719b81db932SToby Isaac   PetscSection neighSec;
2720b81db932SToby Isaac   PetscInt(*neighbors)[2];
2721b81db932SToby Isaac   PetscInt *counter;
2722b81db932SToby Isaac 
2723b81db932SToby Isaac   PetscFunctionBegin;
27249566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
27259566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
27269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2727485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
27289566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &neighSec));
27299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(neighSec, cStart, cEndInterior));
27309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
27319566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "ghost", &ghostLabel));
2732b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2733b81db932SToby Isaac     const PetscInt *fcells;
2734b81db932SToby Isaac     PetscBool       boundary;
27355bc680faSToby Isaac     PetscInt        ghost = -1;
2736b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2737b81db932SToby Isaac 
27389566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
27399566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
27409566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2741b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
27429566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
274306348e87SToby Isaac     if (numCells == 2) {
27449566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2745b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2746b81db932SToby Isaac         PetscInt cell = fcells[c];
2747b81db932SToby Isaac 
27489371c9d4SSatish Balay         if (cell >= cStart && cell < cEndInterior) { PetscCall(PetscSectionAddDof(neighSec, cell, 1)); }
2749b81db932SToby Isaac       }
2750b81db932SToby Isaac     }
275106348e87SToby Isaac   }
27529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(neighSec));
27539566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetMaxDof(neighSec, &maxNumFaces));
27549566063dSJacob Faibussowitsch   PetscCall(PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces));
2755b81db932SToby Isaac   nStart = 0;
27569566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(neighSec, &nEnd));
27579566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1((nEnd - nStart), &neighbors));
27589566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1((cEndInterior - cStart), &counter));
2759b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2760b81db932SToby Isaac     const PetscInt *fcells;
2761b81db932SToby Isaac     PetscBool       boundary;
27625bc680faSToby Isaac     PetscInt        ghost = -1;
2763b81db932SToby Isaac     PetscInt        numChildren, numCells, c;
2764b81db932SToby Isaac 
27659566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, f, &ghost));
27669566063dSJacob Faibussowitsch     PetscCall(DMIsBoundaryPoint(dm, f, &boundary));
27679566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTreeChildren(dm, f, &numChildren, NULL));
2768b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
27699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &numCells));
277006348e87SToby Isaac     if (numCells == 2) {
27719566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, f, &fcells));
2772b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2773b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2774b81db932SToby Isaac 
2775e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
27769566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(neighSec, cell, &off));
2777b81db932SToby Isaac           off += counter[cell - cStart]++;
2778b81db932SToby Isaac           neighbors[off][0] = f;
2779b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2780b81db932SToby Isaac         }
2781b81db932SToby Isaac       }
2782b81db932SToby Isaac     }
278306348e87SToby Isaac   }
27849566063dSJacob Faibussowitsch   PetscCall(PetscFree(counter));
27859566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(maxNumFaces * dim, &dx, maxNumFaces * dim, &grad, maxNumFaces, &gref));
2786b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2787317218b9SToby Isaac     PetscInt         numFaces, f, d, off, ghost = -1;
2788640bce14SSatish Balay     PetscFVCellGeom *cg;
2789b81db932SToby Isaac 
27909566063dSJacob Faibussowitsch     PetscCall(DMPlexPointLocalRead(dmCell, c, cgeom, &cg));
27919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(neighSec, c, &numFaces));
27929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(neighSec, c, &off));
2793a79418b7SMatt McGurn 
2794a79418b7SMatt McGurn     // do not attempt to compute a gradient reconstruction stencil in a ghost cell.  It will never be used
27959566063dSJacob Faibussowitsch     if (ghostLabel) PetscCall(DMLabelGetValue(ghostLabel, c, &ghost));
2796a79418b7SMatt McGurn     if (ghost >= 0) continue;
2797a79418b7SMatt McGurn 
279863a3b9bcSJacob 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);
2799b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2800640bce14SSatish Balay       PetscFVCellGeom *cg1;
2801b81db932SToby Isaac       PetscFVFaceGeom *fg;
2802b81db932SToby Isaac       const PetscInt  *fcells;
2803b81db932SToby Isaac       PetscInt         ncell, side, nface;
2804b81db932SToby Isaac 
2805b81db932SToby Isaac       nface = neighbors[off + f][0];
2806b81db932SToby Isaac       ncell = neighbors[off + f][1];
28079566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, nface, &fcells));
2808b81db932SToby Isaac       side = (c != fcells[0]);
28099566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRef(dmFace, nface, fgeom, &fg));
28109566063dSJacob Faibussowitsch       PetscCall(DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1));
2811b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f * dim + d] = cg1->centroid[d] - cg->centroid[d];
2812b81db932SToby Isaac       gref[f] = fg->grad[side]; /* Gradient reconstruction term will go here */
2813b81db932SToby Isaac     }
28149566063dSJacob Faibussowitsch     PetscCall(PetscFVComputeGradient(fvm, numFaces, dx, grad));
2815b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2816b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f * dim + d];
2817b81db932SToby Isaac     }
2818b81db932SToby Isaac   }
28199566063dSJacob Faibussowitsch   PetscCall(PetscFree3(dx, grad, gref));
28209566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&neighSec));
28219566063dSJacob Faibussowitsch   PetscCall(PetscFree(neighbors));
2822b81db932SToby Isaac   PetscFunctionReturn(0);
2823b81db932SToby Isaac }
2824b81db932SToby Isaac 
2825856ac710SMatthew G. Knepley /*@
2826856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2827856ac710SMatthew G. Knepley 
2828d083f849SBarry Smith   Collective on dm
2829856ac710SMatthew G. Knepley 
28304165533cSJose E. Roman   Input Parameters:
2831856ac710SMatthew G. Knepley + dm  - The DM
2832856ac710SMatthew G. Knepley . fvm - The PetscFV
28338f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2834856ac710SMatthew G. Knepley 
28356b867d5aSJose E. Roman   Input/Output Parameter:
28366b867d5aSJose E. Roman . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM(); on output
28376b867d5aSJose E. Roman                  the geometric factors for gradient calculation are inserted
28386b867d5aSJose E. Roman 
28396b867d5aSJose E. Roman   Output Parameter:
28406b867d5aSJose E. Roman . dmGrad - The DM describing the layout of gradient data
2841856ac710SMatthew G. Knepley 
2842856ac710SMatthew G. Knepley   Level: developer
2843856ac710SMatthew G. Knepley 
2844db781477SPatrick Sanan .seealso: `DMPlexGetFaceGeometryFVM()`, `DMPlexGetCellGeometryFVM()`
2845856ac710SMatthew G. Knepley @*/
28469371c9d4SSatish Balay PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) {
2847856ac710SMatthew G. Knepley   DM           dmFace, dmCell;
2848856ac710SMatthew G. Knepley   PetscScalar *fgeom, *cgeom;
2849b81db932SToby Isaac   PetscSection sectionGrad, parentSection;
2850856ac710SMatthew G. Knepley   PetscInt     dim, pdim, cStart, cEnd, cEndInterior, c;
2851856ac710SMatthew G. Knepley 
2852856ac710SMatthew G. Knepley   PetscFunctionBegin;
28539566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
28549566063dSJacob Faibussowitsch   PetscCall(PetscFVGetNumComponents(fvm, &pdim));
28559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
28569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL));
2857856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
28589566063dSJacob Faibussowitsch   PetscCall(VecGetDM(faceGeometry, &dmFace));
28599566063dSJacob Faibussowitsch   PetscCall(VecGetDM(cellGeometry, &dmCell));
28609566063dSJacob Faibussowitsch   PetscCall(VecGetArray(faceGeometry, &fgeom));
28619566063dSJacob Faibussowitsch   PetscCall(VecGetArray(cellGeometry, &cgeom));
28629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &parentSection, NULL, NULL, NULL, NULL));
2863b81db932SToby Isaac   if (!parentSection) {
28649566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2865b5a3613cSMatthew G. Knepley   } else {
28669566063dSJacob Faibussowitsch     PetscCall(BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom));
2867b81db932SToby Isaac   }
28689566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(faceGeometry, &fgeom));
28699566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(cellGeometry, &cgeom));
2870856ac710SMatthew G. Knepley   /* Create storage for gradients */
28719566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, dmGrad));
28729566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &sectionGrad));
28739566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sectionGrad, cStart, cEnd));
28749566063dSJacob Faibussowitsch   for (c = cStart; c < cEnd; ++c) PetscCall(PetscSectionSetDof(sectionGrad, c, pdim * dim));
28759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sectionGrad));
28769566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(*dmGrad, sectionGrad));
28779566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&sectionGrad));
2878856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2879856ac710SMatthew G. Knepley }
2880b27d5b9eSToby Isaac 
2881c501906fSMatthew G. Knepley /*@
2882c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2883c501906fSMatthew G. Knepley 
2884d083f849SBarry Smith   Collective on dm
2885c501906fSMatthew G. Knepley 
28864165533cSJose E. Roman   Input Parameters:
2887c501906fSMatthew G. Knepley + dm  - The DM
28886b867d5aSJose E. Roman - fv  - The PetscFV
2889c501906fSMatthew G. Knepley 
2890c501906fSMatthew G. Knepley   Output Parameters:
2891c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2892c501906fSMatthew G. Knepley . faceGeometry - The face geometry
28936b867d5aSJose E. Roman - gradDM       - The gradient matrices
2894c501906fSMatthew G. Knepley 
2895c501906fSMatthew G. Knepley   Level: developer
2896c501906fSMatthew G. Knepley 
2897db781477SPatrick Sanan .seealso: `DMPlexComputeGeometryFVM()`
2898c501906fSMatthew G. Knepley @*/
28999371c9d4SSatish Balay PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM) {
2900b27d5b9eSToby Isaac   PetscObject cellgeomobj, facegeomobj;
2901b27d5b9eSToby Isaac 
2902b27d5b9eSToby Isaac   PetscFunctionBegin;
29039566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
2904b27d5b9eSToby Isaac   if (!cellgeomobj) {
2905b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2906b27d5b9eSToby Isaac 
29079566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt));
29089566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_cellgeom_fvm", (PetscObject)cellgeomInt));
29099566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_facegeom_fvm", (PetscObject)facegeomInt));
29109566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cellgeomInt));
29119566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&facegeomInt));
29129566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_cellgeom_fvm", &cellgeomobj));
2913b27d5b9eSToby Isaac   }
29149566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_facegeom_fvm", &facegeomobj));
2915b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec)cellgeomobj;
2916b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec)facegeomobj;
2917b27d5b9eSToby Isaac   if (gradDM) {
2918b27d5b9eSToby Isaac     PetscObject gradobj;
2919b27d5b9eSToby Isaac     PetscBool   computeGradients;
2920b27d5b9eSToby Isaac 
29219566063dSJacob Faibussowitsch     PetscCall(PetscFVGetComputeGradients(fv, &computeGradients));
2922b27d5b9eSToby Isaac     if (!computeGradients) {
2923b27d5b9eSToby Isaac       *gradDM = NULL;
2924b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2925b27d5b9eSToby Isaac     }
29269566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
2927b27d5b9eSToby Isaac     if (!gradobj) {
2928b27d5b9eSToby Isaac       DM dmGradInt;
2929b27d5b9eSToby Isaac 
29309566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeGradientFVM(dm, fv, (Vec)facegeomobj, (Vec)cellgeomobj, &dmGradInt));
29319566063dSJacob Faibussowitsch       PetscCall(PetscObjectCompose((PetscObject)dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt));
29329566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dmGradInt));
29339566063dSJacob Faibussowitsch       PetscCall(PetscObjectQuery((PetscObject)dm, "DMPlex_dmgrad_fvm", &gradobj));
2934b27d5b9eSToby Isaac     }
2935b27d5b9eSToby Isaac     *gradDM = (DM)gradobj;
2936b27d5b9eSToby Isaac   }
2937b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2938b27d5b9eSToby Isaac }
2939d6143a4eSToby Isaac 
29409371c9d4SSatish Balay static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work, PetscReal *resNeg, PetscReal *guess) {
29419d150b73SToby Isaac   PetscInt l, m;
29429d150b73SToby Isaac 
2943cd345991SToby Isaac   PetscFunctionBeginHot;
29449d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
29459d150b73SToby Isaac     /* invert Jacobian, multiply */
29469d150b73SToby Isaac     PetscScalar det, idet;
29479d150b73SToby Isaac 
29489d150b73SToby Isaac     switch (dimR) {
29499371c9d4SSatish Balay     case 1: invJ[0] = 1. / J[0]; break;
29509d150b73SToby Isaac     case 2:
29519d150b73SToby Isaac       det     = J[0] * J[3] - J[1] * J[2];
29529d150b73SToby Isaac       idet    = 1. / det;
29539d150b73SToby Isaac       invJ[0] = J[3] * idet;
29549d150b73SToby Isaac       invJ[1] = -J[1] * idet;
29559d150b73SToby Isaac       invJ[2] = -J[2] * idet;
29569d150b73SToby Isaac       invJ[3] = J[0] * idet;
29579d150b73SToby Isaac       break;
29589371c9d4SSatish Balay     case 3: {
29599d150b73SToby Isaac       invJ[0] = J[4] * J[8] - J[5] * J[7];
29609d150b73SToby Isaac       invJ[1] = J[2] * J[7] - J[1] * J[8];
29619d150b73SToby Isaac       invJ[2] = J[1] * J[5] - J[2] * J[4];
29629d150b73SToby Isaac       det     = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
29639d150b73SToby Isaac       idet    = 1. / det;
29649d150b73SToby Isaac       invJ[0] *= idet;
29659d150b73SToby Isaac       invJ[1] *= idet;
29669d150b73SToby Isaac       invJ[2] *= idet;
29679d150b73SToby Isaac       invJ[3] = idet * (J[5] * J[6] - J[3] * J[8]);
29689d150b73SToby Isaac       invJ[4] = idet * (J[0] * J[8] - J[2] * J[6]);
29699d150b73SToby Isaac       invJ[5] = idet * (J[2] * J[3] - J[0] * J[5]);
29709d150b73SToby Isaac       invJ[6] = idet * (J[3] * J[7] - J[4] * J[6]);
29719d150b73SToby Isaac       invJ[7] = idet * (J[1] * J[6] - J[0] * J[7]);
29729d150b73SToby Isaac       invJ[8] = idet * (J[0] * J[4] - J[1] * J[3]);
29739371c9d4SSatish Balay     } break;
29749d150b73SToby Isaac     }
29759d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
29769371c9d4SSatish Balay       for (m = 0; m < dimC; m++) { guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m]; }
29779d150b73SToby Isaac     }
29789d150b73SToby Isaac   } else {
29799d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
29809d150b73SToby Isaac     char transpose = 'C';
29819d150b73SToby Isaac #else
29829d150b73SToby Isaac     char transpose = 'T';
29839d150b73SToby Isaac #endif
29849d150b73SToby Isaac     PetscBLASInt m        = dimR;
29859d150b73SToby Isaac     PetscBLASInt n        = dimC;
29869d150b73SToby Isaac     PetscBLASInt one      = 1;
29879d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
29889d150b73SToby Isaac 
29899d150b73SToby Isaac     for (l = 0; l < dimC; l++) { invJ[l] = resNeg[l]; }
29909d150b73SToby Isaac 
2991792fecdfSBarry Smith     PetscCallBLAS("LAPACKgels", LAPACKgels_(&transpose, &m, &n, &one, J, &m, invJ, &n, work, &worksize, &info));
299208401ef6SPierre Jolivet     PetscCheck(info == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Bad argument to GELS");
29939d150b73SToby Isaac 
2994c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) { guess[l] += PetscRealPart(invJ[l]); }
29959d150b73SToby Isaac   }
29969d150b73SToby Isaac   PetscFunctionReturn(0);
29979d150b73SToby Isaac }
29989d150b73SToby Isaac 
29999371c9d4SSatish Balay static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR) {
3000c0cbe899SToby Isaac   PetscInt     coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
30019d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
30029d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
30039d150b73SToby Isaac   PetscScalar *J, *invJ, *work;
30049d150b73SToby Isaac 
30059d150b73SToby Isaac   PetscFunctionBegin;
30069d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30079566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
30081dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
30099566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
30109566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
30119d150b73SToby Isaac   cellCoords = &cellData[0];
30129d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
30139d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
30149d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
30159d150b73SToby Isaac   invJ       = &J[dimR * dimC];
30169d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
30179d150b73SToby Isaac   if (dimR == 2) {
30189d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
30199d150b73SToby Isaac 
30209d150b73SToby Isaac     for (i = 0; i < 4; i++) {
30219d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
30229d150b73SToby Isaac 
30239371c9d4SSatish Balay       for (j = 0; j < dimC; j++) { cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); }
30249d150b73SToby Isaac     }
30259d150b73SToby Isaac   } else if (dimR == 3) {
30269d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
30279d150b73SToby Isaac 
30289d150b73SToby Isaac     for (i = 0; i < 8; i++) {
30299d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
30309d150b73SToby Isaac 
30319371c9d4SSatish Balay       for (j = 0; j < dimC; j++) { cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); }
30329d150b73SToby Isaac     }
30339d150b73SToby Isaac   } else {
30349d150b73SToby Isaac     for (i = 0; i < coordSize; i++) { cellCoords[i] = PetscRealPart(coordsScalar[i]); }
30359d150b73SToby Isaac   }
30369d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
30379d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
30389d150b73SToby Isaac     PetscReal *swap;
30399d150b73SToby Isaac 
30409d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
30419d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
30429d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
30439d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
30449d150b73SToby Isaac       }
30459d150b73SToby Isaac     }
30469d150b73SToby Isaac 
30479d150b73SToby Isaac     if (i < dimR - 1) {
30489d150b73SToby Isaac       swap       = cellCoeffs;
30499d150b73SToby Isaac       cellCoeffs = cellCoords;
30509d150b73SToby Isaac       cellCoords = swap;
30519d150b73SToby Isaac     }
30529d150b73SToby Isaac   }
30539566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(refCoords, numPoints * dimR));
30549d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
30559d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
30569d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
30579d150b73SToby Isaac 
30589d150b73SToby Isaac       /* compute -residual and Jacobian */
30599d150b73SToby Isaac       for (k = 0; k < dimC; k++) { resNeg[k] = realCoords[dimC * j + k]; }
30609d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) { J[k] = 0.; }
30619d150b73SToby Isaac       for (k = 0; k < numV; k++) {
30629d150b73SToby Isaac         PetscReal extCoord = 1.;
30639d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
30649d150b73SToby Isaac           PetscReal coord = guess[l];
30659d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
30669d150b73SToby Isaac 
30679d150b73SToby Isaac           extCoord *= dep * coord + !dep;
30689d150b73SToby Isaac           extJ[l] = dep;
30699d150b73SToby Isaac 
30709d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
30719d150b73SToby Isaac             PetscReal coord = guess[m];
30729d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
30739d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
30749d150b73SToby Isaac 
30759d150b73SToby Isaac             extJ[l] *= mult;
30769d150b73SToby Isaac           }
30779d150b73SToby Isaac         }
30789d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
30799d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
30809d150b73SToby Isaac 
30819d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
30829371c9d4SSatish Balay           for (m = 0; m < dimR; m++) { J[dimR * l + m] += coeff * extJ[m]; }
30839d150b73SToby Isaac         }
30849d150b73SToby Isaac       }
308576bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
30860611203eSToby Isaac         PetscReal maxAbs = 0.;
30870611203eSToby Isaac 
30889371c9d4SSatish Balay         for (l = 0; l < dimC; l++) { maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l])); }
308963a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
30900611203eSToby Isaac       }
30919d150b73SToby Isaac 
30929566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(dimC, dimR, J, invJ, work, resNeg, guess));
30939d150b73SToby Isaac     }
30949d150b73SToby Isaac   }
30959566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J));
30969566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData));
30979566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
30989d150b73SToby Isaac   PetscFunctionReturn(0);
30999d150b73SToby Isaac }
31009d150b73SToby Isaac 
31019371c9d4SSatish Balay static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR) {
31029d150b73SToby Isaac   PetscInt     coordSize, i, j, k, l, numV = (1 << dimR);
31039d150b73SToby Isaac   PetscScalar *coordsScalar = NULL;
31049d150b73SToby Isaac   PetscReal   *cellData, *cellCoords, *cellCoeffs;
31059d150b73SToby Isaac 
31069d150b73SToby Isaac   PetscFunctionBegin;
31079d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31089566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
31091dca8a05SBarry Smith   PetscCheck(coordSize >= dimC * numV, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expecting at least %" PetscInt_FMT " coordinates, got %" PetscInt_FMT, dimC * (1 << dimR), coordSize);
31109566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
31119d150b73SToby Isaac   cellCoords = &cellData[0];
31129d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
31139d150b73SToby Isaac   if (dimR == 2) {
31149d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
31159d150b73SToby Isaac 
31169d150b73SToby Isaac     for (i = 0; i < 4; i++) {
31179d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31189d150b73SToby Isaac 
31199371c9d4SSatish Balay       for (j = 0; j < dimC; j++) { cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); }
31209d150b73SToby Isaac     }
31219d150b73SToby Isaac   } else if (dimR == 3) {
31229d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
31239d150b73SToby Isaac 
31249d150b73SToby Isaac     for (i = 0; i < 8; i++) {
31259d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
31269d150b73SToby Isaac 
31279371c9d4SSatish Balay       for (j = 0; j < dimC; j++) { cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]); }
31289d150b73SToby Isaac     }
31299d150b73SToby Isaac   } else {
31309d150b73SToby Isaac     for (i = 0; i < coordSize; i++) { cellCoords[i] = PetscRealPart(coordsScalar[i]); }
31319d150b73SToby Isaac   }
31329d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
31339d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
31349d150b73SToby Isaac     PetscReal *swap;
31359d150b73SToby Isaac 
31369d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
31379d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
31389d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
31399d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
31409d150b73SToby Isaac       }
31419d150b73SToby Isaac     }
31429d150b73SToby Isaac 
31439d150b73SToby Isaac     if (i < dimR - 1) {
31449d150b73SToby Isaac       swap       = cellCoeffs;
31459d150b73SToby Isaac       cellCoeffs = cellCoords;
31469d150b73SToby Isaac       cellCoords = swap;
31479d150b73SToby Isaac     }
31489d150b73SToby Isaac   }
31499566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(realCoords, numPoints * dimC));
31509d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
31519d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
31529d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
31539d150b73SToby Isaac 
31549d150b73SToby Isaac     for (k = 0; k < numV; k++) {
31559d150b73SToby Isaac       PetscReal extCoord = 1.;
31569d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
31579d150b73SToby Isaac         PetscReal coord = guess[l];
31589d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
31599d150b73SToby Isaac 
31609d150b73SToby Isaac         extCoord *= dep * coord + !dep;
31619d150b73SToby Isaac       }
31629d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
31639d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
31649d150b73SToby Isaac 
31659d150b73SToby Isaac         mapped[l] += coeff * extCoord;
31669d150b73SToby Isaac       }
31679d150b73SToby Isaac     }
31689d150b73SToby Isaac   }
31699566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData));
31709566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar));
31719d150b73SToby Isaac   PetscFunctionReturn(0);
31729d150b73SToby Isaac }
31739d150b73SToby Isaac 
31749c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
31759371c9d4SSatish Balay static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR) {
31769c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
3177c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3178c6e120d1SToby Isaac   PetscReal   *invV, *modes;
3179c6e120d1SToby Isaac   PetscReal   *B, *D, *resNeg;
3180c6e120d1SToby Isaac   PetscScalar *J, *invJ, *work;
31819d150b73SToby Isaac 
31829d150b73SToby Isaac   PetscFunctionBegin;
31839566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
31849566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
318563a3b9bcSJacob 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);
31869566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
31879d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
31889566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
31899d150b73SToby Isaac   invV = fe->invV;
3190012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3191012b7cc6SMatthew G. Knepley     modes[i] = 0.;
31929371c9d4SSatish Balay     for (j = 0; j < pdim; ++j) { modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); }
31939d150b73SToby Isaac   }
31949566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
31959c3cf19fSMatthew G. Knepley   D      = &B[pdim * Nc];
31969c3cf19fSMatthew G. Knepley   resNeg = &D[pdim * Nc * dimR];
31979566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
31989c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
31999c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
32009d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) { refCoords[i] = 0.; }
32019d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32029b1f03cbSToby 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 */
32039d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
32049566063dSJacob Faibussowitsch       PetscCall(PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL));
32059c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc; k++) { resNeg[k] = realCoords[j * Nc + k]; }
32069c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc * dimR; k++) { J[k] = 0.; }
32079c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
32089c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
3209012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
32109371c9d4SSatish Balay           for (m = 0; m < dimR; m++) { J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m]; }
32119d150b73SToby Isaac         }
32129d150b73SToby Isaac       }
321376bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
32140611203eSToby Isaac         PetscReal maxAbs = 0.;
32150611203eSToby Isaac 
32169371c9d4SSatish Balay         for (l = 0; l < Nc; l++) { maxAbs = PetscMax(maxAbs, PetscAbsReal(resNeg[l])); }
321763a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(dm, "cell %" PetscInt_FMT ", point %" PetscInt_FMT ", iter %" PetscInt_FMT ": res %g\n", cell, j, i, (double)maxAbs));
32180611203eSToby Isaac       }
32199566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_NewtonUpdate(Nc, dimR, J, invJ, work, resNeg, guess));
32209d150b73SToby Isaac     }
32219d150b73SToby Isaac   }
32229566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 3 * Nc * dimR, MPIU_SCALAR, &J));
32239566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim * Nc + pdim * Nc * dimR + Nc, MPIU_REAL, &B));
32249566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
32259566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32269d150b73SToby Isaac   PetscFunctionReturn(0);
32279d150b73SToby Isaac }
32289d150b73SToby Isaac 
32299c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
32309371c9d4SSatish Balay static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR) {
32319c3cf19fSMatthew G. Knepley   PetscInt     numComp, pdim, i, j, k, l, coordSize;
3232c6e120d1SToby Isaac   PetscScalar *nodes = NULL;
3233c6e120d1SToby Isaac   PetscReal   *invV, *modes;
32349d150b73SToby Isaac   PetscReal   *B;
32359d150b73SToby Isaac 
32369d150b73SToby Isaac   PetscFunctionBegin;
32379566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDimension(fe, &pdim));
32389566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
323963a3b9bcSJacob 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);
32409566063dSJacob Faibussowitsch   PetscCall(DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32419d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
32429566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, pdim, MPIU_REAL, &modes));
32439d150b73SToby Isaac   invV = fe->invV;
3244012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3245012b7cc6SMatthew G. Knepley     modes[i] = 0.;
32469371c9d4SSatish Balay     for (j = 0; j < pdim; ++j) { modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]); }
32479d150b73SToby Isaac   }
32489566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
32499566063dSJacob Faibussowitsch   PetscCall(PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL));
32509c3cf19fSMatthew G. Knepley   for (i = 0; i < numPoints * Nc; i++) { realCoords[i] = 0.; }
32519d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32529c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
32539d150b73SToby Isaac 
32549c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
32559371c9d4SSatish Balay       for (l = 0; l < Nc; l++) { mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l]; }
32569d150b73SToby Isaac     }
32579d150b73SToby Isaac   }
32589566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, numPoints * pdim * Nc, MPIU_REAL, &B));
32599566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, pdim, MPIU_REAL, &modes));
32609566063dSJacob Faibussowitsch   PetscCall(DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes));
32619d150b73SToby Isaac   PetscFunctionReturn(0);
32629d150b73SToby Isaac }
32639d150b73SToby Isaac 
3264d6143a4eSToby Isaac /*@
3265d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
3266d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
3267d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
3268d6143a4eSToby Isaac 
3269d6143a4eSToby Isaac   Not collective
3270d6143a4eSToby Isaac 
3271d6143a4eSToby Isaac   Input Parameters:
3272d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
3273d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
3274d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
3275d6143a4eSToby Isaac . cell       - the cell whose map is used.
3276d6143a4eSToby Isaac . numPoints  - the number of points to locate
32771b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
3278d6143a4eSToby Isaac 
3279d6143a4eSToby Isaac   Output Parameters:
3280d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
32811b266c99SBarry Smith 
32821b266c99SBarry Smith   Level: intermediate
328373c9229bSMatthew Knepley 
3284db781477SPatrick Sanan .seealso: `DMPlexReferenceToCoordinates()`
3285d6143a4eSToby Isaac @*/
32869371c9d4SSatish Balay PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[]) {
3287485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
32889d150b73SToby Isaac   DM       coordDM = NULL;
32899d150b73SToby Isaac   Vec      coords;
32909d150b73SToby Isaac   PetscFE  fe = NULL;
32919d150b73SToby Isaac 
3292d6143a4eSToby Isaac   PetscFunctionBegin;
32939d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32949566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
32959566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
32969d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
32979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
32989566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
32999566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
33009d150b73SToby Isaac   if (coordDM) {
33019d150b73SToby Isaac     PetscInt coordFields;
33029d150b73SToby Isaac 
33039566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
33049d150b73SToby Isaac     if (coordFields) {
33059d150b73SToby Isaac       PetscClassId id;
33069d150b73SToby Isaac       PetscObject  disc;
33079d150b73SToby Isaac 
33089566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
33099566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
33109371c9d4SSatish Balay       if (id == PETSCFE_CLASSID) { fe = (PetscFE)disc; }
33119d150b73SToby Isaac     }
33129d150b73SToby Isaac   }
33139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
33141dca8a05SBarry 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);
33159d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
33169d150b73SToby Isaac     PetscInt  coneSize;
33179d150b73SToby Isaac     PetscBool isSimplex, isTensor;
33189d150b73SToby Isaac 
33199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
33209d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
33219d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
33229d150b73SToby Isaac     if (isSimplex) {
33239d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
33249d150b73SToby Isaac 
33259566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
33269d150b73SToby Isaac       J    = &v0[dimC];
33279d150b73SToby Isaac       invJ = &J[dimC * dimC];
33289566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ));
33299d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3330c330f8ffSToby Isaac         const PetscReal x0[3] = {-1., -1., -1.};
3331c330f8ffSToby Isaac 
3332c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
33339d150b73SToby Isaac       }
33349566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
33359d150b73SToby Isaac     } else if (isTensor) {
33369566063dSJacob Faibussowitsch       PetscCall(DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
333763a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
33389d150b73SToby Isaac   } else {
33399566063dSJacob Faibussowitsch     PetscCall(DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR));
33409d150b73SToby Isaac   }
33419d150b73SToby Isaac   PetscFunctionReturn(0);
33429d150b73SToby Isaac }
33439d150b73SToby Isaac 
33449d150b73SToby Isaac /*@
33459d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
33469d150b73SToby Isaac 
33479d150b73SToby Isaac   Not collective
33489d150b73SToby Isaac 
33499d150b73SToby Isaac   Input Parameters:
33509d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
33519d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
33529d150b73SToby Isaac                as a multilinear map for tensor-product elements
33539d150b73SToby Isaac . cell       - the cell whose map is used.
33549d150b73SToby Isaac . numPoints  - the number of points to locate
3355a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
33569d150b73SToby Isaac 
33579d150b73SToby Isaac   Output Parameters:
33589d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
33591b266c99SBarry Smith 
33601b266c99SBarry Smith    Level: intermediate
336173c9229bSMatthew Knepley 
3362db781477SPatrick Sanan .seealso: `DMPlexCoordinatesToReference()`
33639d150b73SToby Isaac @*/
33649371c9d4SSatish Balay PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[]) {
3365485ad865SMatthew G. Knepley   PetscInt dimC, dimR, depth, cStart, cEnd, i;
33669d150b73SToby Isaac   DM       coordDM = NULL;
33679d150b73SToby Isaac   Vec      coords;
33689d150b73SToby Isaac   PetscFE  fe = NULL;
33699d150b73SToby Isaac 
33709d150b73SToby Isaac   PetscFunctionBegin;
33719d150b73SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33729566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dimR));
33739566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimC));
33749d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
33759566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
33769566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
33779566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &coordDM));
33789d150b73SToby Isaac   if (coordDM) {
33799d150b73SToby Isaac     PetscInt coordFields;
33809d150b73SToby Isaac 
33819566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(coordDM, &coordFields));
33829d150b73SToby Isaac     if (coordFields) {
33839d150b73SToby Isaac       PetscClassId id;
33849d150b73SToby Isaac       PetscObject  disc;
33859d150b73SToby Isaac 
33869566063dSJacob Faibussowitsch       PetscCall(DMGetField(coordDM, 0, NULL, &disc));
33879566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
33889371c9d4SSatish Balay       if (id == PETSCFE_CLASSID) { fe = (PetscFE)disc; }
33899d150b73SToby Isaac     }
33909d150b73SToby Isaac   }
33919566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
33921dca8a05SBarry 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);
33939d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
33949d150b73SToby Isaac     PetscInt  coneSize;
33959d150b73SToby Isaac     PetscBool isSimplex, isTensor;
33969d150b73SToby Isaac 
33979566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
33989d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
33999d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
34009d150b73SToby Isaac     if (isSimplex) {
34019d150b73SToby Isaac       PetscReal detJ, *v0, *J;
34029d150b73SToby Isaac 
34039566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34049d150b73SToby Isaac       J = &v0[dimC];
34059566063dSJacob Faibussowitsch       PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ));
3406c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3407c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3408c330f8ffSToby Isaac 
3409c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
34109d150b73SToby Isaac       }
34119566063dSJacob Faibussowitsch       PetscCall(DMRestoreWorkArray(dm, dimC + 2 * dimC * dimC, MPIU_REAL, &v0));
34129d150b73SToby Isaac     } else if (isTensor) {
34139566063dSJacob Faibussowitsch       PetscCall(DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
341463a3b9bcSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unrecognized cone size %" PetscInt_FMT, coneSize);
34159d150b73SToby Isaac   } else {
34169566063dSJacob Faibussowitsch     PetscCall(DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR));
34179d150b73SToby Isaac   }
3418d6143a4eSToby Isaac   PetscFunctionReturn(0);
3419d6143a4eSToby Isaac }
34200139fca9SMatthew G. Knepley 
34210139fca9SMatthew G. Knepley /*@C
34220139fca9SMatthew G. Knepley   DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates.
34230139fca9SMatthew G. Knepley 
34240139fca9SMatthew G. Knepley   Not collective
34250139fca9SMatthew G. Knepley 
34260139fca9SMatthew G. Knepley   Input Parameters:
34270139fca9SMatthew G. Knepley + dm      - The DM
34280139fca9SMatthew G. Knepley . time    - The time
34290139fca9SMatthew G. Knepley - func    - The function transforming current coordinates to new coordaintes
34300139fca9SMatthew G. Knepley 
34310139fca9SMatthew G. Knepley    Calling sequence of func:
34320139fca9SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
34330139fca9SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
34340139fca9SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
34350139fca9SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
34360139fca9SMatthew G. Knepley 
34370139fca9SMatthew G. Knepley +  dim          - The spatial dimension
34380139fca9SMatthew G. Knepley .  Nf           - The number of input fields (here 1)
34390139fca9SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
34400139fca9SMatthew G. Knepley .  uOff         - The offset of the coordinates in u[] (here 0)
34410139fca9SMatthew G. Knepley .  uOff_x       - The offset of the coordinates in u_x[] (here 0)
34420139fca9SMatthew G. Knepley .  u            - The coordinate values at this point in space
34430139fca9SMatthew G. Knepley .  u_t          - The coordinate time derivative at this point in space (here NULL)
34440139fca9SMatthew G. Knepley .  u_x          - The coordinate derivatives at this point in space
34450139fca9SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
34460139fca9SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
34470139fca9SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
34480139fca9SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
34490139fca9SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
34500139fca9SMatthew G. Knepley .  t            - The current time
34510139fca9SMatthew G. Knepley .  x            - The coordinates of this point (here not used)
34520139fca9SMatthew G. Knepley .  numConstants - The number of constants
34530139fca9SMatthew G. Knepley .  constants    - The value of each constant
34540139fca9SMatthew G. Knepley -  f            - The new coordinates at this point in space
34550139fca9SMatthew G. Knepley 
34560139fca9SMatthew G. Knepley   Level: intermediate
34570139fca9SMatthew G. Knepley 
3458db781477SPatrick Sanan .seealso: `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMProjectFieldLocal()`, `DMProjectFieldLabelLocal()`
34590139fca9SMatthew G. Knepley @*/
34609371c9d4SSatish Balay 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[])) {
34610139fca9SMatthew G. Knepley   DM      cdm;
34628bf1a49fSMatthew G. Knepley   DMField cf;
34630139fca9SMatthew G. Knepley   Vec     lCoords, tmpCoords;
34640139fca9SMatthew G. Knepley 
34650139fca9SMatthew G. Knepley   PetscFunctionBegin;
34669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
34679566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
34689566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(cdm, &tmpCoords));
34699566063dSJacob Faibussowitsch   PetscCall(VecCopy(lCoords, tmpCoords));
34708bf1a49fSMatthew G. Knepley   /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */
34719566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateField(dm, &cf));
34726858538eSMatthew G. Knepley   cdm->coordinates[0].field = cf;
34739566063dSJacob Faibussowitsch   PetscCall(DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords));
34746858538eSMatthew G. Knepley   cdm->coordinates[0].field = NULL;
34759566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(cdm, &tmpCoords));
34769566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, lCoords));
34770139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
34780139fca9SMatthew G. Knepley }
34790139fca9SMatthew G. Knepley 
34800139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z,
34810139fca9SMatthew G. Knepley   / 1  0  m_0 \
34820139fca9SMatthew G. Knepley   | 0  1  m_1 |
34830139fca9SMatthew G. Knepley   \ 0  0   1  /
34840139fca9SMatthew G. Knepley */
34859371c9d4SSatish Balay 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[]) {
34860139fca9SMatthew G. Knepley   const PetscInt Nc = uOff[1] - uOff[0];
3487c1f1bd54SMatthew G. Knepley   const PetscInt ax = (PetscInt)PetscRealPart(constants[0]);
34880139fca9SMatthew G. Knepley   PetscInt       c;
34890139fca9SMatthew G. Knepley 
34909371c9d4SSatish Balay   for (c = 0; c < Nc; ++c) { coords[c] = u[c] + constants[c + 1] * u[ax]; }
34910139fca9SMatthew G. Knepley }
34920139fca9SMatthew G. Knepley 
34930139fca9SMatthew G. Knepley /*@C
34940139fca9SMatthew G. Knepley   DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates.
34950139fca9SMatthew G. Knepley 
34960139fca9SMatthew G. Knepley   Not collective
34970139fca9SMatthew G. Knepley 
34980139fca9SMatthew G. Knepley   Input Parameters:
34990139fca9SMatthew G. Knepley + dm          - The DM
35003ee9839eSMatthew G. Knepley . direction   - The shear coordinate direction, e.g. 0 is the x-axis
35010139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction
35020139fca9SMatthew G. Knepley 
35030139fca9SMatthew G. Knepley   Level: intermediate
35040139fca9SMatthew G. Knepley 
3505db781477SPatrick Sanan .seealso: `DMPlexRemapGeometry()`
35060139fca9SMatthew G. Knepley @*/
35079371c9d4SSatish Balay PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[]) {
35080139fca9SMatthew G. Knepley   DM             cdm;
35090139fca9SMatthew G. Knepley   PetscDS        cds;
35100139fca9SMatthew G. Knepley   PetscObject    obj;
35110139fca9SMatthew G. Knepley   PetscClassId   id;
35120139fca9SMatthew G. Knepley   PetscScalar   *moduli;
35133ee9839eSMatthew G. Knepley   const PetscInt dir = (PetscInt)direction;
35140139fca9SMatthew G. Knepley   PetscInt       dE, d, e;
35150139fca9SMatthew G. Knepley 
35160139fca9SMatthew G. Knepley   PetscFunctionBegin;
35179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
35189566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
35199566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(dE + 1, &moduli));
35200139fca9SMatthew G. Knepley   moduli[0] = dir;
3521cdaaecf7SMatthew G. Knepley   for (d = 0, e = 0; d < dE; ++d) moduli[d + 1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0);
35229566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
35239566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
35249566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(obj, &id));
35250139fca9SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
35260139fca9SMatthew G. Knepley     Vec          lCoords;
35270139fca9SMatthew G. Knepley     PetscSection cSection;
35280139fca9SMatthew G. Knepley     PetscScalar *coords;
35290139fca9SMatthew G. Knepley     PetscInt     vStart, vEnd, v;
35300139fca9SMatthew G. Knepley 
35319566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
35329566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &cSection));
35339566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &lCoords));
35349566063dSJacob Faibussowitsch     PetscCall(VecGetArray(lCoords, &coords));
35350139fca9SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
35360139fca9SMatthew G. Knepley       PetscReal ds;
35370139fca9SMatthew G. Knepley       PetscInt  off, c;
35380139fca9SMatthew G. Knepley 
35399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(cSection, v, &off));
35400139fca9SMatthew G. Knepley       ds = PetscRealPart(coords[off + dir]);
35410139fca9SMatthew G. Knepley       for (c = 0; c < dE; ++c) coords[off + c] += moduli[c] * ds;
35420139fca9SMatthew G. Knepley     }
35439566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(lCoords, &coords));
35440139fca9SMatthew G. Knepley   } else {
35459566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, dE + 1, moduli));
35469566063dSJacob Faibussowitsch     PetscCall(DMPlexRemapGeometry(dm, 0.0, f0_shear));
35470139fca9SMatthew G. Knepley   }
35489566063dSJacob Faibussowitsch   PetscCall(PetscFree(moduli));
35490139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
35500139fca9SMatthew G. Knepley }
3551