xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 2df84da033b910034e6fb854e845895811af1702)
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 
3437900f7dSMatthew G. Knepley .seealso: DMPlexCreate(), DMGetCoordinatesLocal()
353985bb02SVaclav Hapla @*/
36d3e1f4ccSVaclav Hapla PetscErrorCode DMPlexFindVertices(DM dm, Vec coordinates, PetscReal eps, IS *points)
373985bb02SVaclav Hapla {
3837900f7dSMatthew G. Knepley   PetscInt          c, cdim, i, j, o, p, vStart, vEnd;
39d3e1f4ccSVaclav Hapla   PetscInt          npoints;
40d3e1f4ccSVaclav Hapla   const PetscScalar *coord;
413985bb02SVaclav Hapla   Vec               allCoordsVec;
423985bb02SVaclav Hapla   const PetscScalar *allCoords;
43d3e1f4ccSVaclav Hapla   PetscInt          *dagPoints;
443985bb02SVaclav Hapla   PetscErrorCode    ierr;
453985bb02SVaclav Hapla 
463985bb02SVaclav Hapla   PetscFunctionBegin;
473985bb02SVaclav Hapla   if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON;
4837900f7dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
49d3e1f4ccSVaclav Hapla   {
50d3e1f4ccSVaclav Hapla     PetscInt n;
51d3e1f4ccSVaclav Hapla 
52d3e1f4ccSVaclav Hapla     ierr = VecGetLocalSize(coordinates, &n);CHKERRQ(ierr);
532c71b3e2SJacob Faibussowitsch     PetscCheckFalse(n % cdim,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Given coordinates Vec has local length %D not divisible by coordinate dimension %D of given DM", n, cdim);
54d3e1f4ccSVaclav Hapla     npoints = n / cdim;
55d3e1f4ccSVaclav Hapla   }
563985bb02SVaclav Hapla   ierr = DMGetCoordinatesLocal(dm, &allCoordsVec);CHKERRQ(ierr);
573985bb02SVaclav Hapla   ierr = VecGetArrayRead(allCoordsVec, &allCoords);CHKERRQ(ierr);
58d3e1f4ccSVaclav Hapla   ierr = VecGetArrayRead(coordinates, &coord);CHKERRQ(ierr);
593985bb02SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
6076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
61335ef845SVaclav Hapla     /* check coordinate section is consistent with DM dimension */
62335ef845SVaclav Hapla     PetscSection      cs;
63335ef845SVaclav Hapla     PetscInt          ndof;
64335ef845SVaclav Hapla 
65335ef845SVaclav Hapla     ierr = DMGetCoordinateSection(dm, &cs);CHKERRQ(ierr);
663985bb02SVaclav Hapla     for (p = vStart; p < vEnd; p++) {
673985bb02SVaclav Hapla       ierr = PetscSectionGetDof(cs, p, &ndof);CHKERRQ(ierr);
682c71b3e2SJacob Faibussowitsch       PetscCheckFalse(ndof != cdim,PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %D: ndof = %D != %D = cdim", p, ndof, cdim);
69335ef845SVaclav Hapla     }
70335ef845SVaclav Hapla   }
71d3e1f4ccSVaclav Hapla   ierr = PetscMalloc1(npoints, &dagPoints);CHKERRQ(ierr);
72eca9f518SVaclav Hapla   if (eps == 0.0) {
7337900f7dSMatthew G. Knepley     for (i=0,j=0; i < npoints; i++,j+=cdim) {
74eca9f518SVaclav Hapla       dagPoints[i] = -1;
7537900f7dSMatthew G. Knepley       for (p = vStart,o=0; p < vEnd; p++,o+=cdim) {
7637900f7dSMatthew G. Knepley         for (c = 0; c < cdim; c++) {
77d3e1f4ccSVaclav Hapla           if (coord[j+c] != allCoords[o+c]) break;
78eca9f518SVaclav Hapla         }
7937900f7dSMatthew G. Knepley         if (c == cdim) {
80eca9f518SVaclav Hapla           dagPoints[i] = p;
81eca9f518SVaclav Hapla           break;
82eca9f518SVaclav Hapla         }
83eca9f518SVaclav Hapla       }
84eca9f518SVaclav Hapla     }
85d3e1f4ccSVaclav Hapla   } else {
8637900f7dSMatthew G. Knepley     for (i=0,j=0; i < npoints; i++,j+=cdim) {
87d3e1f4ccSVaclav Hapla       PetscReal         norm;
88d3e1f4ccSVaclav Hapla 
89335ef845SVaclav Hapla       dagPoints[i] = -1;
9037900f7dSMatthew G. Knepley       for (p = vStart,o=0; p < vEnd; p++,o+=cdim) {
913985bb02SVaclav Hapla         norm = 0.0;
9237900f7dSMatthew G. Knepley         for (c = 0; c < cdim; c++) {
93d3e1f4ccSVaclav Hapla           norm += PetscRealPart(PetscSqr(coord[j+c] - allCoords[o+c]));
943985bb02SVaclav Hapla         }
953985bb02SVaclav Hapla         norm = PetscSqrtReal(norm);
963985bb02SVaclav Hapla         if (norm <= eps) {
973985bb02SVaclav Hapla           dagPoints[i] = p;
983985bb02SVaclav Hapla           break;
993985bb02SVaclav Hapla         }
1003985bb02SVaclav Hapla       }
1013985bb02SVaclav Hapla     }
102d3e1f4ccSVaclav Hapla   }
1033985bb02SVaclav Hapla   ierr = VecRestoreArrayRead(allCoordsVec, &allCoords);CHKERRQ(ierr);
104d3e1f4ccSVaclav Hapla   ierr = VecRestoreArrayRead(coordinates, &coord);CHKERRQ(ierr);
105d3e1f4ccSVaclav Hapla   ierr = ISCreateGeneral(PETSC_COMM_SELF, npoints, dagPoints, PETSC_OWN_POINTER, points);CHKERRQ(ierr);
1063985bb02SVaclav Hapla   PetscFunctionReturn(0);
1073985bb02SVaclav Hapla }
1083985bb02SVaclav Hapla 
109fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
110fea14342SMatthew G. Knepley {
111fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*2+0];
112fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*2+1];
113fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*2+0];
114fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*2+1];
115fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0*2+0];
116fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0*2+1];
117fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1*2+0];
118fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1*2+1];
119fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
120fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
121fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
122fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
123fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
124fea14342SMatthew G. Knepley 
125fea14342SMatthew G. Knepley   PetscFunctionBegin;
126fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
127fea14342SMatthew G. Knepley   /* Non-parallel lines */
128fea14342SMatthew G. Knepley   if (denom != 0.0) {
129fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
130fea14342SMatthew G. Knepley     const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
131fea14342SMatthew G. Knepley 
132fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
133fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
134fea14342SMatthew G. Knepley       if (intersection) {
135fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
136fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
137fea14342SMatthew G. Knepley       }
138fea14342SMatthew G. Knepley     }
139fea14342SMatthew G. Knepley   }
140fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
141fea14342SMatthew G. Knepley }
142fea14342SMatthew G. Knepley 
143ddce0771SMatthew G. Knepley /* The plane is segmentB x segmentC: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */
144ddce0771SMatthew G. Knepley static PetscErrorCode DMPlexGetLinePlaneIntersection_3D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], const PetscReal segmentC[], PetscReal intersection[], PetscBool *hasIntersection)
145ddce0771SMatthew G. Knepley {
146ddce0771SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*3+0];
147ddce0771SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*3+1];
148ddce0771SMatthew G. Knepley   const PetscReal p0_z  = segmentA[0*3+2];
149ddce0771SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*3+0];
150ddce0771SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*3+1];
151ddce0771SMatthew G. Knepley   const PetscReal p1_z  = segmentA[1*3+2];
152ddce0771SMatthew G. Knepley   const PetscReal q0_x  = segmentB[0*3+0];
153ddce0771SMatthew G. Knepley   const PetscReal q0_y  = segmentB[0*3+1];
154ddce0771SMatthew G. Knepley   const PetscReal q0_z  = segmentB[0*3+2];
155ddce0771SMatthew G. Knepley   const PetscReal q1_x  = segmentB[1*3+0];
156ddce0771SMatthew G. Knepley   const PetscReal q1_y  = segmentB[1*3+1];
157ddce0771SMatthew G. Knepley   const PetscReal q1_z  = segmentB[1*3+2];
158ddce0771SMatthew G. Knepley   const PetscReal r0_x  = segmentC[0*3+0];
159ddce0771SMatthew G. Knepley   const PetscReal r0_y  = segmentC[0*3+1];
160ddce0771SMatthew G. Knepley   const PetscReal r0_z  = segmentC[0*3+2];
161ddce0771SMatthew G. Knepley   const PetscReal r1_x  = segmentC[1*3+0];
162ddce0771SMatthew G. Knepley   const PetscReal r1_y  = segmentC[1*3+1];
163ddce0771SMatthew G. Knepley   const PetscReal r1_z  = segmentC[1*3+2];
164ddce0771SMatthew G. Knepley   const PetscReal s0_x  = p1_x - p0_x;
165ddce0771SMatthew G. Knepley   const PetscReal s0_y  = p1_y - p0_y;
166ddce0771SMatthew G. Knepley   const PetscReal s0_z  = p1_z - p0_z;
167ddce0771SMatthew G. Knepley   const PetscReal s1_x  = q1_x - q0_x;
168ddce0771SMatthew G. Knepley   const PetscReal s1_y  = q1_y - q0_y;
169ddce0771SMatthew G. Knepley   const PetscReal s1_z  = q1_z - q0_z;
170ddce0771SMatthew G. Knepley   const PetscReal s2_x  = r1_x - r0_x;
171ddce0771SMatthew G. Knepley   const PetscReal s2_y  = r1_y - r0_y;
172ddce0771SMatthew G. Knepley   const PetscReal s2_z  = r1_z - r0_z;
173ddce0771SMatthew G. Knepley   const PetscReal s3_x  = s1_y*s2_z - s1_z*s2_y; /* s1 x s2 */
174ddce0771SMatthew G. Knepley   const PetscReal s3_y  = s1_z*s2_x - s1_x*s2_z;
175ddce0771SMatthew G. Knepley   const PetscReal s3_z  = s1_x*s2_y - s1_y*s2_x;
176ddce0771SMatthew G. Knepley   const PetscReal s4_x  = s0_y*s2_z - s0_z*s2_y; /* s0 x s2 */
177ddce0771SMatthew G. Knepley   const PetscReal s4_y  = s0_z*s2_x - s0_x*s2_z;
178ddce0771SMatthew G. Knepley   const PetscReal s4_z  = s0_x*s2_y - s0_y*s2_x;
179ddce0771SMatthew G. Knepley   const PetscReal s5_x  = s1_y*s0_z - s1_z*s0_y; /* s1 x s0 */
180ddce0771SMatthew G. Knepley   const PetscReal s5_y  = s1_z*s0_x - s1_x*s0_z;
181ddce0771SMatthew G. Knepley   const PetscReal s5_z  = s1_x*s0_y - s1_y*s0_x;
182ddce0771SMatthew G. Knepley   const PetscReal denom = -(s0_x*s3_x + s0_y*s3_y + s0_z*s3_z); /* -s0 . (s1 x s2) */
183ddce0771SMatthew G. Knepley 
184ddce0771SMatthew G. Knepley   PetscFunctionBegin;
185ddce0771SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
186ddce0771SMatthew G. Knepley   /* Line not parallel to plane */
187ddce0771SMatthew G. Knepley   if (denom != 0.0) {
188ddce0771SMatthew G. Knepley     const PetscReal t = (s3_x * (p0_x - q0_x) + s3_y * (p0_y - q0_y) + s3_z * (p0_z - q0_z)) / denom;
189ddce0771SMatthew G. Knepley     const PetscReal u = (s4_x * (p0_x - q0_x) + s4_y * (p0_y - q0_y) + s4_z * (p0_z - q0_z)) / denom;
190ddce0771SMatthew G. Knepley     const PetscReal v = (s5_x * (p0_x - q0_x) + s5_y * (p0_y - q0_y) + s5_z * (p0_z - q0_z)) / denom;
191ddce0771SMatthew G. Knepley 
192ddce0771SMatthew G. Knepley     if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && v >= 0 && v <= 1) {
193ddce0771SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
194ddce0771SMatthew G. Knepley       if (intersection) {
195ddce0771SMatthew G. Knepley         intersection[0] = p0_x + (t * s0_x);
196ddce0771SMatthew G. Knepley         intersection[1] = p0_y + (t * s0_y);
197ddce0771SMatthew G. Knepley         intersection[2] = p0_z + (t * s0_z);
198ddce0771SMatthew G. Knepley       }
199ddce0771SMatthew G. Knepley     }
200ddce0771SMatthew G. Knepley   }
201ddce0771SMatthew G. Knepley   PetscFunctionReturn(0);
202ddce0771SMatthew G. Knepley }
203ddce0771SMatthew G. Knepley 
20414bbb9f0SLawrence Mitchell static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
20514bbb9f0SLawrence Mitchell {
20614bbb9f0SLawrence Mitchell   const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON;
20714bbb9f0SLawrence Mitchell   const PetscReal x   = PetscRealPart(point[0]);
20814bbb9f0SLawrence Mitchell   PetscReal       v0, J, invJ, detJ;
20914bbb9f0SLawrence Mitchell   PetscReal       xi;
21014bbb9f0SLawrence Mitchell   PetscErrorCode  ierr;
21114bbb9f0SLawrence Mitchell 
21214bbb9f0SLawrence Mitchell   PetscFunctionBegin;
21314bbb9f0SLawrence Mitchell   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ);CHKERRQ(ierr);
21414bbb9f0SLawrence Mitchell   xi   = invJ*(x - v0);
21514bbb9f0SLawrence Mitchell 
21614bbb9f0SLawrence Mitchell   if ((xi >= -eps) && (xi <= 2.+eps)) *cell = c;
21714bbb9f0SLawrence Mitchell   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
21814bbb9f0SLawrence Mitchell   PetscFunctionReturn(0);
21914bbb9f0SLawrence Mitchell }
22014bbb9f0SLawrence Mitchell 
221ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
222ccd2543fSMatthew G Knepley {
223ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
224f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
225ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
226ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
227ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
228ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
229ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
230ccd2543fSMatthew G Knepley 
231ccd2543fSMatthew G Knepley   PetscFunctionBegin;
2328e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
233ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
234ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
235ccd2543fSMatthew G Knepley 
236f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
237c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
238ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
239ccd2543fSMatthew G Knepley }
240ccd2543fSMatthew G Knepley 
24162a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
24262a38674SMatthew G. Knepley {
24362a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
24462a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
24562a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
24662a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
24762a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
24862a38674SMatthew G. Knepley   PetscErrorCode  ierr;
24962a38674SMatthew G. Knepley 
25062a38674SMatthew G. Knepley   PetscFunctionBegin;
25162a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
25262a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
25362a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
25462a38674SMatthew G. Knepley 
25562a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
25662a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
25762a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
25862a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
25962a38674SMatthew G. Knepley     xi  /= r;
26062a38674SMatthew G. Knepley     eta /= r;
26162a38674SMatthew G. Knepley   }
26262a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
26362a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
26462a38674SMatthew G. Knepley   PetscFunctionReturn(0);
26562a38674SMatthew G. Knepley }
26662a38674SMatthew G. Knepley 
267ba2698f1SMatthew G. Knepley static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
268ccd2543fSMatthew G Knepley {
269ccd2543fSMatthew G Knepley   PetscSection       coordSection;
270ccd2543fSMatthew G Knepley   Vec             coordsLocal;
271a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
272ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
273ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
274ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
275ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
276ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
277ccd2543fSMatthew G Knepley 
278ccd2543fSMatthew G Knepley   PetscFunctionBegin;
279ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
28069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
281ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
282ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
283ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
284ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
285ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
286ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
287ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
288ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
289ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
290ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
291ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
292ccd2543fSMatthew G Knepley   }
293ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
294c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
295ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
296ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
297ccd2543fSMatthew G Knepley }
298ccd2543fSMatthew G Knepley 
299ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
300ccd2543fSMatthew G Knepley {
301ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 3;
30237900f7dSMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
303ccd2543fSMatthew G Knepley   PetscReal       v0[3], J[9], invJ[9], detJ;
304ccd2543fSMatthew G Knepley   PetscReal       x = PetscRealPart(point[0]);
305ccd2543fSMatthew G Knepley   PetscReal       y = PetscRealPart(point[1]);
306ccd2543fSMatthew G Knepley   PetscReal       z = PetscRealPart(point[2]);
307ccd2543fSMatthew G Knepley   PetscReal       xi, eta, zeta;
308ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
309ccd2543fSMatthew G Knepley 
310ccd2543fSMatthew G Knepley   PetscFunctionBegin;
3118e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
312ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
313ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
314ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
315ccd2543fSMatthew G Knepley 
31637900f7dSMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0+eps)) *cell = c;
317c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
318ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
319ccd2543fSMatthew G Knepley }
320ccd2543fSMatthew G Knepley 
321ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
322ccd2543fSMatthew G Knepley {
323ccd2543fSMatthew G Knepley   PetscSection   coordSection;
324ccd2543fSMatthew G Knepley   Vec            coordsLocal;
325872a9804SMatthew G. Knepley   PetscScalar   *coords = NULL;
326fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
327fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
328ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
329ccd2543fSMatthew G Knepley   PetscInt       f;
330ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
331ccd2543fSMatthew G Knepley 
332ccd2543fSMatthew G Knepley   PetscFunctionBegin;
333ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
33469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
335ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
336ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
337ccd2543fSMatthew G Knepley     /* Check the point is under plane */
338ccd2543fSMatthew G Knepley     /*   Get face normal */
339ccd2543fSMatthew G Knepley     PetscReal v_i[3];
340ccd2543fSMatthew G Knepley     PetscReal v_j[3];
341ccd2543fSMatthew G Knepley     PetscReal normal[3];
342ccd2543fSMatthew G Knepley     PetscReal pp[3];
343ccd2543fSMatthew G Knepley     PetscReal dot;
344ccd2543fSMatthew G Knepley 
345ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
346ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
347ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
348ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
349ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
350ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
351ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
352ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
353ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
354ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
355ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
356ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
357ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
358ccd2543fSMatthew G Knepley 
359ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
360ccd2543fSMatthew G Knepley     if (dot < 0.0) {
361ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
362ccd2543fSMatthew G Knepley       break;
363ccd2543fSMatthew G Knepley     }
364ccd2543fSMatthew G Knepley   }
365ccd2543fSMatthew G Knepley   if (found) *cell = c;
366c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
367ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
368ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
369ccd2543fSMatthew G Knepley }
370ccd2543fSMatthew G Knepley 
371c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
372c4eade1cSMatthew G. Knepley {
373c4eade1cSMatthew G. Knepley   PetscInt d;
374c4eade1cSMatthew G. Knepley 
375c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
376c4eade1cSMatthew G. Knepley   box->dim = dim;
377c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
378c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
379c4eade1cSMatthew G. Knepley }
380c4eade1cSMatthew G. Knepley 
381c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
382c4eade1cSMatthew G. Knepley {
383c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
384c4eade1cSMatthew G. Knepley 
385c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
386c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
387c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
388c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
389c4eade1cSMatthew G. Knepley }
390c4eade1cSMatthew G. Knepley 
391c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
392c4eade1cSMatthew G. Knepley {
393c4eade1cSMatthew G. Knepley   PetscInt d;
394c4eade1cSMatthew G. Knepley 
395c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
396c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
397c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
398c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
399c4eade1cSMatthew G. Knepley   }
400c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
401c4eade1cSMatthew G. Knepley }
402c4eade1cSMatthew G. Knepley 
40362a38674SMatthew G. Knepley /*
40462a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
40562a38674SMatthew G. Knepley 
40662a38674SMatthew G. Knepley   Not collective
40762a38674SMatthew G. Knepley 
40862a38674SMatthew G. Knepley   Input Parameters:
40962a38674SMatthew G. Knepley + box - The grid hash object
41062a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
41162a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
41262a38674SMatthew G. Knepley 
41362a38674SMatthew G. Knepley   Level: developer
41462a38674SMatthew G. Knepley 
41562a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
41662a38674SMatthew G. Knepley */
417c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
418c4eade1cSMatthew G. Knepley {
419c4eade1cSMatthew G. Knepley   PetscInt d;
420c4eade1cSMatthew G. Knepley 
421c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
422c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
423c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
424c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
425c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
426c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
427c4eade1cSMatthew G. Knepley     } else {
428c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
429c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
430c4eade1cSMatthew G. Knepley     }
431c4eade1cSMatthew G. Knepley   }
432c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
433c4eade1cSMatthew G. Knepley }
434c4eade1cSMatthew G. Knepley 
43562a38674SMatthew G. Knepley /*
43662a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
43762a38674SMatthew G. Knepley 
43862a38674SMatthew G. Knepley   Not collective
43962a38674SMatthew G. Knepley 
44062a38674SMatthew G. Knepley   Input Parameters:
44162a38674SMatthew G. Knepley + box       - The grid hash object
44262a38674SMatthew G. Knepley . numPoints - The number of input points
44362a38674SMatthew G. Knepley - points    - The input point coordinates
44462a38674SMatthew G. Knepley 
44562a38674SMatthew G. Knepley   Output Parameters:
44662a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
44762a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
44862a38674SMatthew G. Knepley 
44962a38674SMatthew G. Knepley   Level: developer
45062a38674SMatthew G. Knepley 
45162a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
45262a38674SMatthew G. Knepley */
4531c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
454c4eade1cSMatthew G. Knepley {
455c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
456c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
457c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
458c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
459c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
460c4eade1cSMatthew G. Knepley   PetscInt         d, p;
461c4eade1cSMatthew G. Knepley 
462c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
463c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
464c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4651c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
466c4eade1cSMatthew G. Knepley 
4671c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
4682a705cacSMatthew G. Knepley       if (dbox == -1   && PetscAbsReal(PetscRealPart(points[p*dim+d]) - lower[d]) < 1.0e-9) dbox = 0;
4692c71b3e2SJacob Faibussowitsch       PetscCheckFalse(dbox < 0 || dbox >= n[d],PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %d (%g, %g, %g) is outside of our bounding box",
470087ef6b2SMatthew G. Knepley                                              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);
471c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
472c4eade1cSMatthew G. Knepley     }
473ddce0771SMatthew G. Knepley     if (boxes) for (d = dim-2, boxes[p] = dboxes[p*dim+dim-1]; d >= 0; --d) boxes[p] = boxes[p]*n[d] + dboxes[p*dim+d];
474c4eade1cSMatthew G. Knepley   }
475c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
476c4eade1cSMatthew G. Knepley }
477c4eade1cSMatthew G. Knepley 
478af74b616SDave May /*
479af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
480af74b616SDave May 
481af74b616SDave May  Not collective
482af74b616SDave May 
483af74b616SDave May   Input Parameters:
484af74b616SDave May + box       - The grid hash object
485af74b616SDave May . numPoints - The number of input points
486af74b616SDave May - points    - The input point coordinates
487af74b616SDave May 
488af74b616SDave May   Output Parameters:
489af74b616SDave May + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
490af74b616SDave May . boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
491af74b616SDave May - found     - Flag indicating if point was located within a box
492af74b616SDave May 
493af74b616SDave May   Level: developer
494af74b616SDave May 
495af74b616SDave May .seealso: PetscGridHashGetEnclosingBox()
496af74b616SDave May */
497af74b616SDave May PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[],PetscBool *found)
498af74b616SDave May {
499af74b616SDave May   const PetscReal *lower = box->lower;
500af74b616SDave May   const PetscReal *upper = box->upper;
501af74b616SDave May   const PetscReal *h     = box->h;
502af74b616SDave May   const PetscInt  *n     = box->n;
503af74b616SDave May   const PetscInt   dim   = box->dim;
504af74b616SDave May   PetscInt         d, p;
505af74b616SDave May 
506af74b616SDave May   PetscFunctionBegin;
507af74b616SDave May   *found = PETSC_FALSE;
508af74b616SDave May   for (p = 0; p < numPoints; ++p) {
509af74b616SDave May     for (d = 0; d < dim; ++d) {
510af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
511af74b616SDave May 
512af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
513af74b616SDave May       if (dbox < 0 || dbox >= n[d]) {
514af74b616SDave May         PetscFunctionReturn(0);
515af74b616SDave May       }
516af74b616SDave May       dboxes[p*dim+d] = dbox;
517af74b616SDave May     }
518ddce0771SMatthew G. Knepley     if (boxes) for (d = dim-2, boxes[p] = dboxes[p*dim+dim-1]; d >= 0; --d) boxes[p] = boxes[p]*n[d] + dboxes[p*dim+d];
519af74b616SDave May   }
520af74b616SDave May   *found = PETSC_TRUE;
521af74b616SDave May   PetscFunctionReturn(0);
522af74b616SDave May }
523af74b616SDave May 
524c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
525c4eade1cSMatthew G. Knepley {
526c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
527c4eade1cSMatthew G. Knepley 
528c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
529c4eade1cSMatthew G. Knepley   if (*box) {
530c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
531c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
532c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
533c4eade1cSMatthew G. Knepley   }
534c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
535c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
536c4eade1cSMatthew G. Knepley }
537c4eade1cSMatthew G. Knepley 
538cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
539cafe43deSMatthew G. Knepley {
540ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
541cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
542cafe43deSMatthew G. Knepley 
543cafe43deSMatthew G. Knepley   PetscFunctionBegin;
544ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cellStart, &ct);CHKERRQ(ierr);
545ba2698f1SMatthew G. Knepley   switch (ct) {
54614bbb9f0SLawrence Mitchell     case DM_POLYTOPE_SEGMENT:
54714bbb9f0SLawrence Mitchell     ierr = DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
548ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
549ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
550ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
551ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
552ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
553ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
554ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
555ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
55698921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %D with type %s", cellStart, DMPolytopeTypes[ct]);
557cafe43deSMatthew G. Knepley   }
558cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
559cafe43deSMatthew G. Knepley }
560cafe43deSMatthew G. Knepley 
56162a38674SMatthew G. Knepley /*
56262a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
56362a38674SMatthew G. Knepley */
56462a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
56562a38674SMatthew G. Knepley {
566ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
56762a38674SMatthew G. Knepley   PetscErrorCode ierr;
56862a38674SMatthew G. Knepley 
56962a38674SMatthew G. Knepley   PetscFunctionBegin;
570ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
571ba2698f1SMatthew G. Knepley   switch (ct) {
572ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
573ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
57462a38674SMatthew G. Knepley #if 0
575ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
576ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
577ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
578ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
579ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
580ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
58162a38674SMatthew G. Knepley #endif
58298921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %D with type %s", cell, DMPolytopeTypes[ct]);
58362a38674SMatthew G. Knepley   }
58462a38674SMatthew G. Knepley   PetscFunctionReturn(0);
58562a38674SMatthew G. Knepley }
58662a38674SMatthew G. Knepley 
58762a38674SMatthew G. Knepley /*
58862a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
58962a38674SMatthew G. Knepley 
590d083f849SBarry Smith   Collective on dm
59162a38674SMatthew G. Knepley 
59262a38674SMatthew G. Knepley   Input Parameter:
59362a38674SMatthew G. Knepley . dm - The Plex
59462a38674SMatthew G. Knepley 
59562a38674SMatthew G. Knepley   Output Parameter:
59662a38674SMatthew G. Knepley . localBox - The grid hash object
59762a38674SMatthew G. Knepley 
59862a38674SMatthew G. Knepley   Level: developer
59962a38674SMatthew G. Knepley 
60062a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
60162a38674SMatthew G. Knepley */
602cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
603cafe43deSMatthew G. Knepley {
604ddce0771SMatthew G. Knepley   const PetscInt     debug = 0;
605cafe43deSMatthew G. Knepley   MPI_Comm           comm;
606cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
607cafe43deSMatthew G. Knepley   Vec                coordinates;
608cafe43deSMatthew G. Knepley   PetscSection       coordSection;
609cafe43deSMatthew G. Knepley   Vec                coordsLocal;
610cafe43deSMatthew G. Knepley   const PetscScalar *coords;
611ddce0771SMatthew G. Knepley   PetscScalar       *edgeCoords;
612722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
613ddce0771SMatthew G. Knepley   PetscInt           n[3] = {2, 2, 2};
614ddce0771SMatthew G. Knepley   PetscInt           dim, N, maxConeSize, cStart, cEnd, c, eStart, eEnd, i;
615ddce0771SMatthew G. Knepley   PetscBool          flg;
616cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
617cafe43deSMatthew G. Knepley 
618cafe43deSMatthew G. Knepley   PetscFunctionBegin;
619cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
620cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
621cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
622ddce0771SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
623ddce0771SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
624cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
625cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
626cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
627cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
628cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
629ddce0771SMatthew G. Knepley   c    = dim;
630ddce0771SMatthew G. Knepley   ierr = PetscOptionsGetIntArray(NULL, ((PetscObject) dm)->prefix, "-dm_plex_hash_box_faces", n, &c, &flg);CHKERRQ(ierr);
631ddce0771SMatthew G. Knepley   if (flg) {for (i = c; i < dim; ++i) n[i] = n[c-1];}
632ddce0771SMatthew G. Knepley   else     {for (i = 0; i < dim; ++i) n[i] = PetscMax(2, PetscFloorReal(PetscPowReal((PetscReal) (cEnd - cStart), 1.0/dim) * 0.8));}
633cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
634cafe43deSMatthew G. Knepley #if 0
635cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
636820f2d46SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRMPI(ierr);
637820f2d46SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRMPI(ierr);
638cafe43deSMatthew G. Knepley #endif
639cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
640cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
641cafe43deSMatthew G. Knepley   /* Create label */
642ddce0771SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
643b26b5bf9SMatthew G. Knepley   if (dim < 2) eStart = eEnd = -1;
644d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse);CHKERRQ(ierr);
645cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
646a8d69d7bSBarry Smith   /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
647cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
648cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
649ddce0771SMatthew G. Knepley   ierr = PetscCalloc3(16 * dim, &dboxes, 16, &boxes, PetscPowInt(maxConeSize, dim) * dim, &edgeCoords);CHKERRQ(ierr);
650cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
651cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
652cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
65338353de4SMatthew G. Knepley     PetscInt         csize   = 0;
654ddce0771SMatthew G. Knepley     PetscInt        *closure = NULL;
655ddce0771SMatthew G. Knepley     PetscInt         Ncl, cl, Ne = 0;
656cafe43deSMatthew G. Knepley     PetscScalar      point[3];
657cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
658cafe43deSMatthew G. Knepley 
659ddce0771SMatthew G. Knepley     /* Get all edges in cell */
660ddce0771SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure);CHKERRQ(ierr);
661ddce0771SMatthew G. Knepley     for (cl = 0; cl < Ncl*2; ++cl) {
662ddce0771SMatthew G. Knepley       if ((closure[cl] >= eStart) && (closure[cl] < eEnd)) {
663ddce0771SMatthew G. Knepley         PetscScalar *ecoords = &edgeCoords[Ne*dim*2];
664ddce0771SMatthew G. Knepley         PetscInt     ecsize  = dim*2;
665ddce0771SMatthew G. Knepley 
666e032ffc8SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, closure[cl], &ecsize, &ecoords);CHKERRQ(ierr);
6672c71b3e2SJacob Faibussowitsch         PetscCheckFalse(ecsize != dim*2,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Got %D coords for edge, instead of %D", ecsize, dim*2);
668ddce0771SMatthew G. Knepley         ++Ne;
669ddce0771SMatthew G. Knepley       }
670ddce0771SMatthew G. Knepley     }
671ddce0771SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure);CHKERRQ(ierr);
672cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
67338353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
67438353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
675722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
676ddce0771SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {
677ddce0771SMatthew G. Knepley       if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D has vertex in box %D (%D, %D, %D)\n", c, boxes[e], dboxes[e*dim+0], dim > 1 ? dboxes[e*dim+1] : -1, dim > 2 ? dboxes[e*dim+2] : -1);CHKERRQ(ierr);}
678ddce0771SMatthew G. Knepley       ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);
679ddce0771SMatthew G. Knepley     }
680cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
681cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
6822291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
683cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
684cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
685cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
686cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
687cafe43deSMatthew G. Knepley       }
688cafe43deSMatthew G. Knepley     }
689fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
690cafe43deSMatthew 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]) {
691cafe43deSMatthew 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]) {
692cafe43deSMatthew 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]) {
693cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
694cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
695fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
696cafe43deSMatthew G. Knepley 
697ddce0771SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "Box %D: (%.2g, %.2g, %.2g) -- (%.2g, %.2g, %.2g)\n", box, point[0], point[1], point[2], point[0] + h[0], point[1] + h[1], point[2] + h[2]);CHKERRQ(ierr);}
698ddce0771SMatthew G. Knepley           /* Check whether cell contains any vertex of this subbox TODO vectorize this */
699cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
700cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
701cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
702cafe43deSMatthew G. Knepley 
703cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
7040b6bfacdSStefano Zampini                 if (cell >= 0) {
705ddce0771SMatthew G. Knepley                   if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  Cell %D contains vertex (%.2g, %.2g, %.2g) of box %D\n", c, cpoint[0], cpoint[1], cpoint[2], box);CHKERRQ(ierr);}
7060b6bfacdSStefano Zampini                   ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr);
7070b6bfacdSStefano Zampini                   jj = kk = 2;
7080b6bfacdSStefano Zampini                   break;
7090b6bfacdSStefano Zampini                 }
710cafe43deSMatthew G. Knepley               }
711cafe43deSMatthew G. Knepley             }
712cafe43deSMatthew G. Knepley           }
713ddce0771SMatthew G. Knepley           /* Check whether cell edge intersects any face of these subboxes TODO vectorize this */
714ddce0771SMatthew G. Knepley           for (edge = 0; edge < Ne; ++edge) {
715a5cae605SSatish Balay             PetscReal segA[6] = {0.,0.,0.,0.,0.,0.};
716a5cae605SSatish Balay             PetscReal segB[6] = {0.,0.,0.,0.,0.,0.};
717a5cae605SSatish Balay             PetscReal segC[6] = {0.,0.,0.,0.,0.,0.};
718fea14342SMatthew G. Knepley 
7192c71b3e2SJacob Faibussowitsch             PetscCheckFalse(dim > 3,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected dim %d > 3",dim);
720ddce0771SMatthew G. Knepley             for (d = 0; d < dim*2; ++d) segA[d] = PetscRealPart(edgeCoords[edge*dim*2+d]);
721ddce0771SMatthew G. Knepley             /* 1D: (x) -- (x+h)               0 -- 1
722ddce0771SMatthew G. Knepley                2D: (x,   y)   -- (x,   y+h)   (0, 0) -- (0, 1)
723ddce0771SMatthew G. Knepley                    (x+h, y)   -- (x+h, y+h)   (1, 0) -- (1, 1)
724ddce0771SMatthew G. Knepley                    (x,   y)   -- (x+h, y)     (0, 0) -- (1, 0)
725ddce0771SMatthew G. Knepley                    (x,   y+h) -- (x+h, y+h)   (0, 1) -- (1, 1)
726ddce0771SMatthew 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)
727ddce0771SMatthew 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)
728ddce0771SMatthew 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)
729ddce0771SMatthew 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)
730ddce0771SMatthew 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)
731ddce0771SMatthew 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)
732ddce0771SMatthew G. Knepley              */
733ddce0771SMatthew G. Knepley             /* Loop over faces with normal in direction d */
734ddce0771SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
735ddce0771SMatthew G. Knepley               PetscBool intersects = PETSC_FALSE;
736ddce0771SMatthew G. Knepley               PetscInt  e = (d+1)%dim;
737ddce0771SMatthew G. Knepley               PetscInt  f = (d+2)%dim;
738ddce0771SMatthew G. Knepley 
739ddce0771SMatthew G. Knepley               /* There are two faces in each dimension */
740ddce0771SMatthew G. Knepley               for (ii = 0; ii < 2; ++ii) {
741ddce0771SMatthew G. Knepley                 segB[d]     = PetscRealPart(point[d] + ii*h[d]);
742ddce0771SMatthew G. Knepley                 segB[dim+d] = PetscRealPart(point[d] + ii*h[d]);
743ddce0771SMatthew G. Knepley                 segC[d]     = PetscRealPart(point[d] + ii*h[d]);
744ddce0771SMatthew G. Knepley                 segC[dim+d] = PetscRealPart(point[d] + ii*h[d]);
745ddce0771SMatthew G. Knepley                 if (dim > 1) {
746ddce0771SMatthew G. Knepley                   segB[e]     = PetscRealPart(point[e] + 0*h[e]);
747ddce0771SMatthew G. Knepley                   segB[dim+e] = PetscRealPart(point[e] + 1*h[e]);
748ddce0771SMatthew G. Knepley                   segC[e]     = PetscRealPart(point[e] + 0*h[e]);
749ddce0771SMatthew G. Knepley                   segC[dim+e] = PetscRealPart(point[e] + 0*h[e]);
750ddce0771SMatthew G. Knepley                 }
751ddce0771SMatthew G. Knepley                 if (dim > 2) {
752ddce0771SMatthew G. Knepley                   segB[f]     = PetscRealPart(point[f] + 0*h[f]);
753ddce0771SMatthew G. Knepley                   segB[dim+f] = PetscRealPart(point[f] + 0*h[f]);
754ddce0771SMatthew G. Knepley                   segC[f]     = PetscRealPart(point[f] + 0*h[f]);
755ddce0771SMatthew G. Knepley                   segC[dim+f] = PetscRealPart(point[f] + 1*h[f]);
756ddce0771SMatthew G. Knepley                 }
757ddce0771SMatthew G. Knepley                 if (dim == 2) {
758ddce0771SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
759ddce0771SMatthew G. Knepley                 } else if (dim == 3) {
760ddce0771SMatthew G. Knepley                   ierr = DMPlexGetLinePlaneIntersection_3D_Internal(segA, segB, segC, NULL, &intersects);CHKERRQ(ierr);
761ddce0771SMatthew G. Knepley                 }
762ddce0771SMatthew G. Knepley                 if (intersects) {
763ddce0771SMatthew G. Knepley                   if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  Cell %D edge %D (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g) intersects box %D, face (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g) (%.2g, %.2g, %.2g)--(%.2g, %.2g, %.2g)\n", c, edge, segA[0], segA[1], segA[2], segA[3], segA[4], segA[5], box, segB[0], segB[1], segB[2], segB[3], segB[4], segB[5], segC[0], segC[1], segC[2], segC[3], segC[4], segC[5]);CHKERRQ(ierr);}
764ddce0771SMatthew G. Knepley                   ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = Ne; break;
765ddce0771SMatthew G. Knepley                 }
766ddce0771SMatthew G. Knepley               }
767ddce0771SMatthew G. Knepley             }
768cafe43deSMatthew G. Knepley           }
769fea14342SMatthew G. Knepley         }
770fea14342SMatthew G. Knepley       }
771fea14342SMatthew G. Knepley     }
772fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
773fea14342SMatthew G. Knepley   }
774ddce0771SMatthew G. Knepley   ierr = PetscFree3(dboxes, boxes, edgeCoords);CHKERRQ(ierr);
775ddce0771SMatthew G. Knepley   if (debug) {ierr = DMLabelView(lbox->cellsSparse, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);}
776cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
777cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
778cafe43deSMatthew G. Knepley   *localBox = lbox;
779cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
780cafe43deSMatthew G. Knepley }
781cafe43deSMatthew G. Knepley 
78262a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
783ccd2543fSMatthew G Knepley {
784ddce0771SMatthew G. Knepley   const PetscInt  debug = 0;
785cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
786af74b616SDave May   PetscBool       hash = mesh->useHashLocation, reuse = PETSC_FALSE;
7873a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
788412e9a14SMatthew G. Knepley   PetscInt        dim, cStart, cEnd, numCells, c, d;
789cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
7903a93e3b7SToby Isaac   PetscSFNode    *cells;
791ccd2543fSMatthew G Knepley   PetscScalar    *a;
7923a93e3b7SToby Isaac   PetscMPIInt     result;
793af74b616SDave May   PetscLogDouble  t0,t1;
7949cb35068SDave May   PetscReal       gmin[3],gmax[3];
7959cb35068SDave May   PetscInt        terminating_query_type[] = { 0, 0, 0 };
796ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
797ccd2543fSMatthew G Knepley 
798ccd2543fSMatthew G Knepley   PetscFunctionBegin;
799cadf77a0SMark Adams   ierr = PetscLogEventBegin(DMPLEX_LocatePoints,0,0,0,0);CHKERRQ(ierr);
800af74b616SDave May   ierr = PetscTime(&t0);CHKERRQ(ierr);
8012c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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.");
802cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
803cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
804ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRMPI(ierr);
8052c71b3e2SJacob Faibussowitsch   PetscCheckFalse(result != MPI_IDENT && result != MPI_CONGRUENT,PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
8062c71b3e2SJacob Faibussowitsch   PetscCheckFalse(bs != dim,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Block size for point vector %D must be the mesh coordinate dimension %D", bs, dim);
807412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
808ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
809ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
810ccd2543fSMatthew G Knepley   numPoints /= bs;
811af74b616SDave May   {
812af74b616SDave May     const PetscSFNode *sf_cells;
813af74b616SDave May 
814af74b616SDave May     ierr = PetscSFGetGraph(cellSF,NULL,NULL,NULL,&sf_cells);CHKERRQ(ierr);
815af74b616SDave May     if (sf_cells) {
816af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Re-using existing StarForest node list\n");CHKERRQ(ierr);
817af74b616SDave May       cells = (PetscSFNode*)sf_cells;
818af74b616SDave May       reuse = PETSC_TRUE;
819af74b616SDave May     } else {
820af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n");CHKERRQ(ierr);
821785e854fSJed Brown       ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
822af74b616SDave May       /* initialize cells if created */
823af74b616SDave May       for (p=0; p<numPoints; p++) {
824af74b616SDave May         cells[p].rank  = 0;
825af74b616SDave May         cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
826af74b616SDave May       }
827af74b616SDave May     }
828af74b616SDave May   }
8299cb35068SDave May   /* define domain bounding box */
8309cb35068SDave May   {
8319cb35068SDave May     Vec coorglobal;
8329cb35068SDave May 
8339cb35068SDave May     ierr = DMGetCoordinates(dm,&coorglobal);CHKERRQ(ierr);
8349cb35068SDave May     ierr = VecStrideMaxAll(coorglobal,NULL,gmax);CHKERRQ(ierr);
8359cb35068SDave May     ierr = VecStrideMinAll(coorglobal,NULL,gmin);CHKERRQ(ierr);
8369cb35068SDave May   }
837953fc75cSMatthew G. Knepley   if (hash) {
838ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
839cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
840cafe43deSMatthew G. Knepley     /* Send points to correct process */
841cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
842cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
843cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
844953fc75cSMatthew G. Knepley   }
8453a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
846ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
847e56f9228SJed Brown     PetscInt           dbin[3] = {-1,-1,-1}, bin, cell = -1, cellOffset;
8489cb35068SDave May     PetscBool          point_outside_domain = PETSC_FALSE;
849ccd2543fSMatthew G Knepley 
8509cb35068SDave May     /* check bounding box of domain */
8519cb35068SDave May     for (d=0; d<dim; d++) {
852a5f152d1SDave May       if (PetscRealPart(point[d]) < gmin[d]) { point_outside_domain = PETSC_TRUE; break; }
853a5f152d1SDave May       if (PetscRealPart(point[d]) > gmax[d]) { point_outside_domain = PETSC_TRUE; break; }
8549cb35068SDave May     }
8559cb35068SDave May     if (point_outside_domain) {
856e9b685f5SMatthew G. Knepley       cells[p].rank = 0;
857e9b685f5SMatthew G. Knepley       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
8589cb35068SDave May       terminating_query_type[0]++;
8599cb35068SDave May       continue;
8609cb35068SDave May     }
861ccd2543fSMatthew G Knepley 
862af74b616SDave May     /* check initial values in cells[].index - abort early if found */
863af74b616SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
864af74b616SDave May       c = cells[p].index;
8653a93e3b7SToby Isaac       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
866af74b616SDave May       ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
867af74b616SDave May       if (cell >= 0) {
868af74b616SDave May         cells[p].rank = 0;
869af74b616SDave May         cells[p].index = cell;
870af74b616SDave May         numFound++;
871af74b616SDave May       }
872af74b616SDave May     }
8739cb35068SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
8749cb35068SDave May       terminating_query_type[1]++;
8759cb35068SDave May       continue;
8769cb35068SDave May     }
877af74b616SDave May 
878953fc75cSMatthew G. Knepley     if (hash) {
879af74b616SDave May       PetscBool found_box;
880af74b616SDave May 
881ddce0771SMatthew G. Knepley       if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "Checking point %D (%.2g, %.2g, %.2g)\n", p, point[0], point[1], point[2]);CHKERRQ(ierr);}
882af74b616SDave May       /* allow for case that point is outside box - abort early */
883af74b616SDave May       ierr = PetscGridHashGetEnclosingBoxQuery(mesh->lbox, 1, point, dbin, &bin,&found_box);CHKERRQ(ierr);
884af74b616SDave May       if (found_box) {
885ddce0771SMatthew G. Knepley         if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  Found point in box %D (%D, %D, %D)\n", bin, dbin[0], dbin[1], dbin[2]);CHKERRQ(ierr);}
886cafe43deSMatthew G. Knepley         /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
887cafe43deSMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
888cafe43deSMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
889cafe43deSMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
890ddce0771SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    Checking for point in cell %D\n", boxCells[c]);CHKERRQ(ierr);}
891cafe43deSMatthew G. Knepley           ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
8923a93e3b7SToby Isaac           if (cell >= 0) {
893ddce0771SMatthew G. Knepley             if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "      FOUND in cell %D\n", cell);CHKERRQ(ierr);}
8943a93e3b7SToby Isaac             cells[p].rank = 0;
8953a93e3b7SToby Isaac             cells[p].index = cell;
8963a93e3b7SToby Isaac             numFound++;
8979cb35068SDave May             terminating_query_type[2]++;
8983a93e3b7SToby Isaac             break;
899ccd2543fSMatthew G Knepley           }
9003a93e3b7SToby Isaac         }
901af74b616SDave May       }
902953fc75cSMatthew G. Knepley     } else {
903953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
904953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
9053a93e3b7SToby Isaac         if (cell >= 0) {
9063a93e3b7SToby Isaac           cells[p].rank = 0;
9073a93e3b7SToby Isaac           cells[p].index = cell;
9083a93e3b7SToby Isaac           numFound++;
9099cb35068SDave May           terminating_query_type[2]++;
9103a93e3b7SToby Isaac           break;
911953fc75cSMatthew G. Knepley         }
912953fc75cSMatthew G. Knepley       }
9133a93e3b7SToby Isaac     }
914ccd2543fSMatthew G Knepley   }
915953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
91662a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
91762a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
91862a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
919d92c4b9fSToby Isaac       PetscReal          cpoint[3], diff[3], best[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL}, dist, distMax = PETSC_MAX_REAL;
920d92c4b9fSToby Isaac       PetscInt           dbin[3] = {-1,-1,-1}, bin, cellOffset, d, bestc = -1;
92162a38674SMatthew G. Knepley 
922e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
92362a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
92462a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
92562a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
92662a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
92762a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
928b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
92962a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
93062a38674SMatthew G. Knepley           if (dist < distMax) {
931d92c4b9fSToby Isaac             for (d = 0; d < dim; ++d) best[d] = cpoint[d];
932d92c4b9fSToby Isaac             bestc = boxCells[c];
93362a38674SMatthew G. Knepley             distMax = dist;
93462a38674SMatthew G. Knepley           }
93562a38674SMatthew G. Knepley         }
936d92c4b9fSToby Isaac         if (distMax < PETSC_MAX_REAL) {
937d92c4b9fSToby Isaac           ++numFound;
938d92c4b9fSToby Isaac           cells[p].rank  = 0;
939d92c4b9fSToby Isaac           cells[p].index = bestc;
940d92c4b9fSToby Isaac           for (d = 0; d < dim; ++d) a[p*bs+d] = best[d];
941d92c4b9fSToby Isaac         }
94262a38674SMatthew G. Knepley       }
94362a38674SMatthew G. Knepley     }
94462a38674SMatthew G. Knepley   }
94562a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
946cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
9472d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
9483a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
9493a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
9503a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
9513a93e3b7SToby Isaac         if (numFound < p) {
9523a93e3b7SToby Isaac           cells[numFound] = cells[p];
9533a93e3b7SToby Isaac         }
9543a93e3b7SToby Isaac         found[numFound++] = p;
9553a93e3b7SToby Isaac       }
9563a93e3b7SToby Isaac     }
9573a93e3b7SToby Isaac   }
95862a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
959af74b616SDave May   if (!reuse) {
9603a93e3b7SToby Isaac     ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
961af74b616SDave May   }
962af74b616SDave May   ierr = PetscTime(&t1);CHKERRQ(ierr);
9639cb35068SDave May   if (hash) {
9647d3de750SJacob Faibussowitsch     ierr = PetscInfo(dm,"[DMLocatePoints_Plex] terminating_query_type : %D [outside domain] : %D [inside initial cell] : %D [hash]\n",terminating_query_type[0],terminating_query_type[1],terminating_query_type[2]);CHKERRQ(ierr);
9659cb35068SDave May   } else {
9667d3de750SJacob Faibussowitsch     ierr = PetscInfo(dm,"[DMLocatePoints_Plex] terminating_query_type : %D [outside domain] : %D [inside initial cell] : %D [brute-force]\n",terminating_query_type[0],terminating_query_type[1],terminating_query_type[2]);CHKERRQ(ierr);
9679cb35068SDave May   }
9687d3de750SJacob Faibussowitsch   ierr = PetscInfo(dm,"[DMLocatePoints_Plex] npoints %D : time(rank0) %1.2e (sec): points/sec %1.4e\n",numPoints,t1-t0,(double)((double)numPoints/(t1-t0)));CHKERRQ(ierr);
969cadf77a0SMark Adams   ierr = PetscLogEventEnd(DMPLEX_LocatePoints,0,0,0,0);CHKERRQ(ierr);
970ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
971ccd2543fSMatthew G Knepley }
972ccd2543fSMatthew G Knepley 
973741bfc07SMatthew G. Knepley /*@C
974741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
975741bfc07SMatthew G. Knepley 
976741bfc07SMatthew G. Knepley   Not collective
977741bfc07SMatthew G. Knepley 
9786b867d5aSJose E. Roman   Input/Output Parameter:
9796b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x
980741bfc07SMatthew G. Knepley 
9816b867d5aSJose E. Roman   Output Parameter:
9826b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
983741bfc07SMatthew G. Knepley 
984741bfc07SMatthew G. Knepley   Level: developer
985741bfc07SMatthew G. Knepley 
986741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
987741bfc07SMatthew G. Knepley @*/
988741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
98917fe8556SMatthew G. Knepley {
99017fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
99117fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
9928b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
99317fe8556SMatthew G. Knepley 
99417fe8556SMatthew G. Knepley   PetscFunctionBegin;
9951c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
9961c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
99717fe8556SMatthew G. Knepley   coords[0] = 0.0;
9987f07f362SMatthew G. Knepley   coords[1] = r;
99917fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
100017fe8556SMatthew G. Knepley }
100117fe8556SMatthew G. Knepley 
1002741bfc07SMatthew G. Knepley /*@C
1003741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
100428dbe442SToby Isaac 
1005741bfc07SMatthew G. Knepley   Not collective
100628dbe442SToby Isaac 
10076b867d5aSJose E. Roman   Input/Output Parameter:
10086b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z
1009741bfc07SMatthew G. Knepley 
10106b867d5aSJose E. Roman   Output Parameter:
10116b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
1012741bfc07SMatthew G. Knepley 
1013741bfc07SMatthew 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
1014741bfc07SMatthew G. Knepley 
1015741bfc07SMatthew G. Knepley   Level: developer
1016741bfc07SMatthew G. Knepley 
1017741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
1018741bfc07SMatthew G. Knepley @*/
1019741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
102028dbe442SToby Isaac {
102128dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
102228dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
102328dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
102428dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
102528dbe442SToby Isaac   PetscReal      rinv = 1. / r;
102628dbe442SToby Isaac   PetscFunctionBegin;
102728dbe442SToby Isaac 
102828dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
102928dbe442SToby Isaac   if (x > 0.) {
103028dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
103128dbe442SToby Isaac 
103228dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
103328dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
103428dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
103528dbe442SToby Isaac   }
103628dbe442SToby Isaac   else {
103728dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
103828dbe442SToby Isaac 
103928dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
104028dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
104128dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
104228dbe442SToby Isaac   }
104328dbe442SToby Isaac   coords[0] = 0.0;
104428dbe442SToby Isaac   coords[1] = r;
104528dbe442SToby Isaac   PetscFunctionReturn(0);
104628dbe442SToby Isaac }
104728dbe442SToby Isaac 
1048741bfc07SMatthew G. Knepley /*@
1049c871b86eSJed Brown   DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the
1050c871b86eSJed Brown     plane.  The normal is defined by positive orientation of the first 3 points.
1051741bfc07SMatthew G. Knepley 
1052741bfc07SMatthew G. Knepley   Not collective
1053741bfc07SMatthew G. Knepley 
1054741bfc07SMatthew G. Knepley   Input Parameter:
10556b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points)
1056741bfc07SMatthew G. Knepley 
10576b867d5aSJose E. Roman   Input/Output Parameter:
10586b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first
10596b867d5aSJose E. Roman            2*coordSize/3 entries contain interlaced 2D points, with the rest undefined
10606b867d5aSJose E. Roman 
10616b867d5aSJose E. Roman   Output Parameter:
10626b867d5aSJose 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.
1063741bfc07SMatthew G. Knepley 
1064741bfc07SMatthew G. Knepley   Level: developer
1065741bfc07SMatthew G. Knepley 
1066741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
1067741bfc07SMatthew G. Knepley @*/
1068741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
1069ccd2543fSMatthew G Knepley {
1070c871b86eSJed Brown   PetscReal x1[3], x2[3], n[3], c[3], norm;
1071ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1072c871b86eSJed Brown   PetscInt       d, p;
1073ccd2543fSMatthew G Knepley 
1074ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1075ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
1076ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
10771ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
10781ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
1079ccd2543fSMatthew G Knepley   }
1080c871b86eSJed Brown   // n = x1 \otimes x2
1081ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
1082ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
1083ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
10848b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
1085c871b86eSJed Brown   for (d = 0; d < dim; d++) n[d] /= norm;
1086c871b86eSJed Brown   norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]);
1087c871b86eSJed Brown   for (d = 0; d < dim; d++) x1[d] /= norm;
1088c871b86eSJed Brown   // x2 = n \otimes x1
1089c871b86eSJed Brown   x2[0] = n[1] * x1[2] - n[2] * x1[1];
1090c871b86eSJed Brown   x2[1] = n[2] * x1[0] - n[0] * x1[2];
1091c871b86eSJed Brown   x2[2] = n[0] * x1[1] - n[1] * x1[0];
1092c871b86eSJed Brown   for (d=0; d<dim; d++) {
1093c871b86eSJed Brown     R[d * dim + 0] = x1[d];
1094c871b86eSJed Brown     R[d * dim + 1] = x2[d];
1095c871b86eSJed Brown     R[d * dim + 2] = n[d];
1096c871b86eSJed Brown     c[d] = PetscRealPart(coords[0*dim + d]);
109773868372SMatthew G. Knepley   }
1098c871b86eSJed Brown   for (p=0; p<coordSize/dim; p++) {
1099c871b86eSJed Brown     PetscReal y[3];
1100c871b86eSJed Brown     for (d=0; d<dim; d++) y[d] = PetscRealPart(coords[p*dim + d]) - c[d];
1101c871b86eSJed 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];
11027f07f362SMatthew G. Knepley   }
1103ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1104ccd2543fSMatthew G Knepley }
1105ccd2543fSMatthew G Knepley 
11066322fe33SJed Brown PETSC_UNUSED
11079fbee547SJacob Faibussowitsch static inline void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
1108834e62ceSMatthew G. Knepley {
1109834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
1110834e62ceSMatthew G. Knepley 
1111834e62ceSMatthew G. Knepley    |  1  1  1 |
1112834e62ceSMatthew G. Knepley    | x0 x1 x2 |
1113834e62ceSMatthew G. Knepley    | y0 y1 y2 |
1114834e62ceSMatthew G. Knepley 
1115834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
1116834e62ceSMatthew G. Knepley 
1117834e62ceSMatthew G. Knepley    | x1 x2 |
1118834e62ceSMatthew G. Knepley    | y1 y2 |
1119834e62ceSMatthew G. Knepley   */
1120834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
1121834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
1122834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
1123834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
112486623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
1125923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
1126834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
11273bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
1128834e62ceSMatthew G. Knepley }
1129834e62ceSMatthew G. Knepley 
11306322fe33SJed Brown PETSC_UNUSED
11319fbee547SJacob Faibussowitsch static inline void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
1132834e62ceSMatthew G. Knepley {
1133834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
1134834e62ceSMatthew G. Knepley 
1135834e62ceSMatthew G. Knepley    |  1  1  1  1 |
1136834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
1137834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
1138834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
1139834e62ceSMatthew G. Knepley 
1140834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
1141834e62ceSMatthew G. Knepley 
1142834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1143834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1144834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1145834e62ceSMatthew G. Knepley   */
1146834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
1147834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
1148834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
11490a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1150834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
1151834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
1152834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
1153834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
1154923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
11550a3da2c2SToby Isaac   *vol = -onesixth*detM;
11563bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1157834e62ceSMatthew G. Knepley }
1158834e62ceSMatthew G. Knepley 
11599fbee547SJacob Faibussowitsch static inline void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
11600ec8681fSMatthew G. Knepley {
11610a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1162923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
11630a3da2c2SToby Isaac   *vol *= -onesixth;
11640ec8681fSMatthew G. Knepley }
11650ec8681fSMatthew G. Knepley 
1166cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1167cb92db44SToby Isaac {
1168cb92db44SToby Isaac   PetscSection   coordSection;
1169cb92db44SToby Isaac   Vec            coordinates;
1170cb92db44SToby Isaac   const PetscScalar *coords;
1171cb92db44SToby Isaac   PetscInt       dim, d, off;
1172cb92db44SToby Isaac   PetscErrorCode ierr;
1173cb92db44SToby Isaac 
1174cb92db44SToby Isaac   PetscFunctionBegin;
1175cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1176cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1177cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
1178cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
1179cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
1180cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
1181cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
1182cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
1183cb92db44SToby Isaac   *detJ = 1.;
1184cb92db44SToby Isaac   if (J) {
1185cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1186cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1187cb92db44SToby Isaac     if (invJ) {
1188cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1189cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1190cb92db44SToby Isaac     }
1191cb92db44SToby Isaac   }
1192cb92db44SToby Isaac   PetscFunctionReturn(0);
1193cb92db44SToby Isaac }
1194cb92db44SToby Isaac 
119517fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
119617fe8556SMatthew G. Knepley {
119717fe8556SMatthew G. Knepley   PetscSection   coordSection;
119817fe8556SMatthew G. Knepley   Vec            coordinates;
1199a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
12008bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
120117fe8556SMatthew G. Knepley   PetscErrorCode ierr;
120217fe8556SMatthew G. Knepley 
120317fe8556SMatthew G. Knepley   PetscFunctionBegin;
120417fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
120569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
12068bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
12078bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
120817fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
12098bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
12102c71b3e2SJacob Faibussowitsch   PetscCheckFalse(invJ && !J,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
12117f07f362SMatthew G. Knepley   *detJ = 0.0;
121228dbe442SToby Isaac   if (numCoords == 6) {
121328dbe442SToby Isaac     const PetscInt dim = 3;
121428dbe442SToby Isaac     PetscReal      R[9], J0;
121528dbe442SToby Isaac 
121628dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1217741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
121828dbe442SToby Isaac     if (J)    {
121928dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
122028dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
122128dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
122228dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
122328dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
122428dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1225adac9986SMatthew G. Knepley     }
122628dbe442SToby Isaac   } else if (numCoords == 4) {
12277f07f362SMatthew G. Knepley     const PetscInt dim = 2;
12287f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
12297f07f362SMatthew G. Knepley 
12307f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1231741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
123217fe8556SMatthew G. Knepley     if (J)    {
12337f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
12347f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
12357f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
1236923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1237923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1238adac9986SMatthew G. Knepley     }
12397f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
12407f07f362SMatthew G. Knepley     const PetscInt dim = 1;
12417f07f362SMatthew G. Knepley 
12427f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
12437f07f362SMatthew G. Knepley     if (J)    {
12447f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
124517fe8556SMatthew G. Knepley       *detJ = J[0];
12463bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
12473bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
1248adac9986SMatthew G. Knepley     }
124998921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
125017fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
125117fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
125217fe8556SMatthew G. Knepley }
125317fe8556SMatthew G. Knepley 
1254ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1255ccd2543fSMatthew G Knepley {
1256ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1257ccd2543fSMatthew G Knepley   Vec            coordinates;
1258a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
125903d7ed2eSStefano Zampini   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1260ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1261ccd2543fSMatthew G Knepley 
1262ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1263ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
126469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
126503d7ed2eSStefano Zampini   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
126603d7ed2eSStefano Zampini   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
1267ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
126803d7ed2eSStefano Zampini   numCoords = numSelfCoords ? numSelfCoords : numCoords;
12697f07f362SMatthew G. Knepley   *detJ = 0.0;
1270ccd2543fSMatthew G Knepley   if (numCoords == 9) {
12717f07f362SMatthew G. Knepley     const PetscInt dim = 3;
12727f07f362SMatthew 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};
12737f07f362SMatthew G. Knepley 
12747f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1275741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
12767f07f362SMatthew G. Knepley     if (J)    {
1277b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1278b7ad821dSMatthew G. Knepley 
1279b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1280b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1281b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1282ccd2543fSMatthew G Knepley         }
12837f07f362SMatthew G. Knepley       }
12843bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1285923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
12867f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
12877f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
12887f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
12897f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
12907f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
12917f07f362SMatthew G. Knepley           }
12927f07f362SMatthew G. Knepley         }
12937f07f362SMatthew G. Knepley       }
12943bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
12957f07f362SMatthew G. Knepley     }
1296923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
12977f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
12987f07f362SMatthew G. Knepley     const PetscInt dim = 2;
12997f07f362SMatthew G. Knepley 
13007f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1301ccd2543fSMatthew G Knepley     if (J)    {
1302ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1303ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1304ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1305ccd2543fSMatthew G Knepley         }
1306ccd2543fSMatthew G Knepley       }
13073bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1308923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1309ccd2543fSMatthew G Knepley     }
1310923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
131198921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1312ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1313ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1314ccd2543fSMatthew G Knepley }
1315ccd2543fSMatthew G Knepley 
1316412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscBool isTensor, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1317ccd2543fSMatthew G Knepley {
1318ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1319ccd2543fSMatthew G Knepley   Vec            coordinates;
1320a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
13210d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1322ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1323ccd2543fSMatthew G Knepley 
1324ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1325ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
132669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
13270d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
13280d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
132999dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
133071f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1331dfccc68fSToby Isaac   if (!Nq) {
1332412e9a14SMatthew G. Knepley     PetscInt vorder[4] = {0, 1, 2, 3};
1333412e9a14SMatthew G. Knepley 
1334412e9a14SMatthew G. Knepley     if (isTensor) {vorder[2] = 3; vorder[3] = 2;}
13357f07f362SMatthew G. Knepley     *detJ = 0.0;
133699dec3a6SMatthew G. Knepley     if (numCoords == 12) {
133799dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
133899dec3a6SMatthew 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};
133999dec3a6SMatthew G. Knepley 
1340dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1341741bfc07SMatthew G. Knepley       ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
134299dec3a6SMatthew G. Knepley       if (J)    {
134399dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
134499dec3a6SMatthew G. Knepley 
134599dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
1346412e9a14SMatthew G. Knepley           J0[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*pdim+d]) - PetscRealPart(coords[vorder[0]*pdim+d]));
1347412e9a14SMatthew G. Knepley           J0[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[2]*pdim+d]) - PetscRealPart(coords[vorder[1]*pdim+d]));
134899dec3a6SMatthew G. Knepley         }
13493bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1350923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
135199dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
135299dec3a6SMatthew G. Knepley           for (f = 0; f < dim; f++) {
135399dec3a6SMatthew G. Knepley             J[d*dim+f] = 0.0;
135499dec3a6SMatthew G. Knepley             for (g = 0; g < dim; g++) {
135599dec3a6SMatthew G. Knepley               J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
135699dec3a6SMatthew G. Knepley             }
135799dec3a6SMatthew G. Knepley           }
135899dec3a6SMatthew G. Knepley         }
13593bc0b13bSBarry Smith         ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
136099dec3a6SMatthew G. Knepley       }
1361923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
136271f58de1SToby Isaac     } else if (numCoords == 8) {
136399dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
136499dec3a6SMatthew G. Knepley 
1365dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1366ccd2543fSMatthew G Knepley       if (J)    {
1367ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
1368412e9a14SMatthew G. Knepley           J[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d]));
1369412e9a14SMatthew G. Knepley           J[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[3]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d]));
1370ccd2543fSMatthew G Knepley         }
13713bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1372923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1373ccd2543fSMatthew G Knepley       }
1374923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
137598921bdaSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1376dfccc68fSToby Isaac   } else {
1377dfccc68fSToby Isaac     const PetscInt Nv = 4;
1378dfccc68fSToby Isaac     const PetscInt dimR = 2;
1379412e9a14SMatthew G. Knepley     PetscInt  zToPlex[4] = {0, 1, 3, 2};
1380dfccc68fSToby Isaac     PetscReal zOrder[12];
1381dfccc68fSToby Isaac     PetscReal zCoeff[12];
1382dfccc68fSToby Isaac     PetscInt  i, j, k, l, dim;
1383dfccc68fSToby Isaac 
1384412e9a14SMatthew G. Knepley     if (isTensor) {zToPlex[2] = 2; zToPlex[3] = 3;}
1385dfccc68fSToby Isaac     if (numCoords == 12) {
1386dfccc68fSToby Isaac       dim = 3;
1387dfccc68fSToby Isaac     } else if (numCoords == 8) {
1388dfccc68fSToby Isaac       dim = 2;
138998921bdaSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1390dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1391dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1392dfccc68fSToby Isaac 
1393dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1394dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1395dfccc68fSToby Isaac       }
1396dfccc68fSToby Isaac     }
1397dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1398*2df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: (1 \mp xi) (1 \mp eta):
1399*2df84da0SMatthew G. Knepley            \phi^0 = (1 - xi - eta + xi eta) --> 1      = 1/4 ( \phi^0 + \phi^1 + \phi^2 + \phi^3)
1400*2df84da0SMatthew G. Knepley            \phi^1 = (1 + xi - eta - xi eta) --> xi     = 1/4 (-\phi^0 + \phi^1 - \phi^2 + \phi^3)
1401*2df84da0SMatthew G. Knepley            \phi^2 = (1 - xi + eta - xi eta) --> eta    = 1/4 (-\phi^0 - \phi^1 + \phi^2 + \phi^3)
1402*2df84da0SMatthew G. Knepley            \phi^3 = (1 + xi + eta + xi eta) --> xi eta = 1/4 ( \phi^0 - \phi^1 - \phi^2 + \phi^3)
1403*2df84da0SMatthew G. Knepley       */
1404dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1405dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1406dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1407dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1408dfccc68fSToby Isaac     }
1409dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1410dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1411dfccc68fSToby Isaac 
1412dfccc68fSToby Isaac       if (v) {
1413dfccc68fSToby Isaac         PetscReal extPoint[4];
1414dfccc68fSToby Isaac 
1415dfccc68fSToby Isaac         extPoint[0] = 1.;
1416dfccc68fSToby Isaac         extPoint[1] = xi;
1417dfccc68fSToby Isaac         extPoint[2] = eta;
1418dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1419dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1420dfccc68fSToby Isaac           PetscReal val = 0.;
1421dfccc68fSToby Isaac 
1422dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1423dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1424dfccc68fSToby Isaac           }
1425dfccc68fSToby Isaac           v[i * dim + j] = val;
1426dfccc68fSToby Isaac         }
1427dfccc68fSToby Isaac       }
1428dfccc68fSToby Isaac       if (J) {
1429dfccc68fSToby Isaac         PetscReal extJ[8];
1430dfccc68fSToby Isaac 
1431dfccc68fSToby Isaac         extJ[0] = 0.;
1432dfccc68fSToby Isaac         extJ[1] = 0.;
1433dfccc68fSToby Isaac         extJ[2] = 1.;
1434dfccc68fSToby Isaac         extJ[3] = 0.;
1435dfccc68fSToby Isaac         extJ[4] = 0.;
1436dfccc68fSToby Isaac         extJ[5] = 1.;
1437dfccc68fSToby Isaac         extJ[6] = eta;
1438dfccc68fSToby Isaac         extJ[7] = xi;
1439dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1440dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1441dfccc68fSToby Isaac             PetscReal val = 0.;
1442dfccc68fSToby Isaac 
1443dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1444dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1445dfccc68fSToby Isaac             }
1446dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1447dfccc68fSToby Isaac           }
1448dfccc68fSToby Isaac         }
1449dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1450dfccc68fSToby Isaac           PetscReal x, y, z;
1451dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1452dfccc68fSToby Isaac           PetscReal norm;
1453dfccc68fSToby Isaac 
1454dfccc68fSToby Isaac           x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1455dfccc68fSToby Isaac           y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1456dfccc68fSToby Isaac           z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1457dfccc68fSToby Isaac           norm = PetscSqrtReal(x * x + y * y + z * z);
1458dfccc68fSToby Isaac           iJ[2] = x / norm;
1459dfccc68fSToby Isaac           iJ[5] = y / norm;
1460dfccc68fSToby Isaac           iJ[8] = z / norm;
1461dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1462dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1463dfccc68fSToby Isaac         } else {
1464dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1465dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1466dfccc68fSToby Isaac         }
1467dfccc68fSToby Isaac       }
1468dfccc68fSToby Isaac     }
1469dfccc68fSToby Isaac   }
147099dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1471ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1472ccd2543fSMatthew G Knepley }
1473ccd2543fSMatthew G Knepley 
1474ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1475ccd2543fSMatthew G Knepley {
1476ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1477ccd2543fSMatthew G Knepley   Vec            coordinates;
1478a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1479ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
148099dec3a6SMatthew G. Knepley   PetscInt       d;
1481ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1482ccd2543fSMatthew G Knepley 
1483ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1484ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
148569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1486ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
14877f07f362SMatthew G. Knepley   *detJ = 0.0;
14887f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1489ccd2543fSMatthew G Knepley   if (J)    {
1490ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1491f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1492f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1493f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1494f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1495ccd2543fSMatthew G Knepley     }
14963bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1497923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1498ccd2543fSMatthew G Knepley   }
1499923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1500ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1501ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1502ccd2543fSMatthew G Knepley }
1503ccd2543fSMatthew G Knepley 
1504dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1505ccd2543fSMatthew G Knepley {
1506ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1507ccd2543fSMatthew G Knepley   Vec            coordinates;
1508a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1509ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1510ccd2543fSMatthew G Knepley   PetscInt       d;
1511ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1512ccd2543fSMatthew G Knepley 
1513ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1514ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
151569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1516ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1517dfccc68fSToby Isaac   if (!Nq) {
15187f07f362SMatthew G. Knepley     *detJ = 0.0;
1519dfccc68fSToby Isaac     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1520ccd2543fSMatthew G Knepley     if (J)    {
1521ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1522f0df753eSMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1523f0df753eSMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1524f0df753eSMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1525ccd2543fSMatthew G Knepley       }
15263bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1527923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1528ccd2543fSMatthew G Knepley     }
1529923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1530dfccc68fSToby Isaac   } else {
1531dfccc68fSToby Isaac     const PetscInt Nv = 8;
1532dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1533dfccc68fSToby Isaac     const PetscInt dim = 3;
1534dfccc68fSToby Isaac     const PetscInt dimR = 3;
1535dfccc68fSToby Isaac     PetscReal zOrder[24];
1536dfccc68fSToby Isaac     PetscReal zCoeff[24];
1537dfccc68fSToby Isaac     PetscInt  i, j, k, l;
1538dfccc68fSToby Isaac 
1539dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1540dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1541dfccc68fSToby Isaac 
1542dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1543dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1544dfccc68fSToby Isaac       }
1545dfccc68fSToby Isaac     }
1546dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1547dfccc68fSToby 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]);
1548dfccc68fSToby 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]);
1549dfccc68fSToby 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]);
1550dfccc68fSToby 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]);
1551dfccc68fSToby 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]);
1552dfccc68fSToby 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]);
1553dfccc68fSToby 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]);
1554dfccc68fSToby 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]);
1555dfccc68fSToby Isaac     }
1556dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1557dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1558dfccc68fSToby Isaac 
1559dfccc68fSToby Isaac       if (v) {
156091d2b7ceSToby Isaac         PetscReal extPoint[8];
1561dfccc68fSToby Isaac 
1562dfccc68fSToby Isaac         extPoint[0] = 1.;
1563dfccc68fSToby Isaac         extPoint[1] = xi;
1564dfccc68fSToby Isaac         extPoint[2] = eta;
1565dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1566dfccc68fSToby Isaac         extPoint[4] = theta;
1567dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1568dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1569dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1570dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1571dfccc68fSToby Isaac           PetscReal val = 0.;
1572dfccc68fSToby Isaac 
1573dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1574dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1575dfccc68fSToby Isaac           }
1576dfccc68fSToby Isaac           v[i * dim + j] = val;
1577dfccc68fSToby Isaac         }
1578dfccc68fSToby Isaac       }
1579dfccc68fSToby Isaac       if (J) {
1580dfccc68fSToby Isaac         PetscReal extJ[24];
1581dfccc68fSToby Isaac 
1582dfccc68fSToby Isaac         extJ[0]  = 0.         ; extJ[1]  = 0.        ; extJ[2]  = 0.      ;
1583dfccc68fSToby Isaac         extJ[3]  = 1.         ; extJ[4]  = 0.        ; extJ[5]  = 0.      ;
1584dfccc68fSToby Isaac         extJ[6]  = 0.         ; extJ[7]  = 1.        ; extJ[8]  = 0.      ;
1585dfccc68fSToby Isaac         extJ[9]  = eta        ; extJ[10] = xi        ; extJ[11] = 0.      ;
1586dfccc68fSToby Isaac         extJ[12] = 0.         ; extJ[13] = 0.        ; extJ[14] = 1.      ;
1587dfccc68fSToby Isaac         extJ[15] = theta      ; extJ[16] = 0.        ; extJ[17] = xi      ;
1588dfccc68fSToby Isaac         extJ[18] = 0.         ; extJ[19] = theta     ; extJ[20] = eta     ;
1589dfccc68fSToby Isaac         extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi;
1590dfccc68fSToby Isaac 
1591dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1592dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1593dfccc68fSToby Isaac             PetscReal val = 0.;
1594dfccc68fSToby Isaac 
1595dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1596dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1597dfccc68fSToby Isaac             }
1598dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1599dfccc68fSToby Isaac           }
1600dfccc68fSToby Isaac         }
1601dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1602dfccc68fSToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1603dfccc68fSToby Isaac       }
1604dfccc68fSToby Isaac     }
1605dfccc68fSToby Isaac   }
1606ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1607ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1608ccd2543fSMatthew G Knepley }
1609ccd2543fSMatthew G Knepley 
1610*2df84da0SMatthew G. Knepley static PetscErrorCode DMPlexComputeTriangularPrismGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1611*2df84da0SMatthew G. Knepley {
1612*2df84da0SMatthew G. Knepley   PetscSection   coordSection;
1613*2df84da0SMatthew G. Knepley   Vec            coordinates;
1614*2df84da0SMatthew G. Knepley   PetscScalar   *coords = NULL;
1615*2df84da0SMatthew G. Knepley   const PetscInt dim = 3;
1616*2df84da0SMatthew G. Knepley   PetscInt       d;
1617*2df84da0SMatthew G. Knepley   PetscErrorCode ierr;
1618*2df84da0SMatthew G. Knepley 
1619*2df84da0SMatthew G. Knepley   PetscFunctionBegin;
1620*2df84da0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1621*2df84da0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1622*2df84da0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1623*2df84da0SMatthew G. Knepley   if (!Nq) {
1624*2df84da0SMatthew G. Knepley     /* Assume that the map to the reference is affine */
1625*2df84da0SMatthew G. Knepley     *detJ = 0.0;
1626*2df84da0SMatthew G. Knepley     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1627*2df84da0SMatthew G. Knepley     if (J)    {
1628*2df84da0SMatthew G. Knepley       for (d = 0; d < dim; d++) {
1629*2df84da0SMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1630*2df84da0SMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1631*2df84da0SMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1632*2df84da0SMatthew G. Knepley       }
1633*2df84da0SMatthew G. Knepley       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1634*2df84da0SMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1635*2df84da0SMatthew G. Knepley     }
1636*2df84da0SMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1637*2df84da0SMatthew G. Knepley   } else {
1638*2df84da0SMatthew G. Knepley     const PetscInt dim  = 3;
1639*2df84da0SMatthew G. Knepley     const PetscInt dimR = 3;
1640*2df84da0SMatthew G. Knepley     const PetscInt Nv   = 6;
1641*2df84da0SMatthew G. Knepley     PetscReal verts[18];
1642*2df84da0SMatthew G. Knepley     PetscReal coeff[18];
1643*2df84da0SMatthew G. Knepley     PetscInt  i, j, k, l;
1644*2df84da0SMatthew G. Knepley 
1645*2df84da0SMatthew G. Knepley     for (i = 0; i < Nv; ++i) for (j = 0; j < dim; ++j) verts[dim * i + j] = PetscRealPart(coords[dim * i + j]);
1646*2df84da0SMatthew G. Knepley     for (j = 0; j < dim; ++j) {
1647*2df84da0SMatthew G. Knepley       /* Check for triangle,
1648*2df84da0SMatthew 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)
1649*2df84da0SMatthew 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)
1650*2df84da0SMatthew G. Knepley            phi^2 =  1/2 (1 + eta)   chi^2 = delta(-1,  1)
1651*2df84da0SMatthew G. Knepley 
1652*2df84da0SMatthew G. Knepley            phi^0 + phi^1 + phi^2 = 1    coef_1   = 1/2 (         chi^1 + chi^2)
1653*2df84da0SMatthew G. Knepley           -phi^0 + phi^1 - phi^2 = xi   coef_xi  = 1/2 (-chi^0 + chi^1)
1654*2df84da0SMatthew G. Knepley           -phi^0 - phi^1 + phi^2 = eta  coef_eta = 1/2 (-chi^0         + chi^2)
1655*2df84da0SMatthew G. Knepley 
1656*2df84da0SMatthew G. Knepley           < chi_0 chi_1 chi_2> A /  1  1  1 \ / phi_0 \   <chi> I <phi>^T  so we need the inverse transpose
1657*2df84da0SMatthew G. Knepley                                  | -1  1 -1 | | phi_1 | =
1658*2df84da0SMatthew G. Knepley                                  \ -1 -1  1 / \ phi_2 /
1659*2df84da0SMatthew G. Knepley 
1660*2df84da0SMatthew 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
1661*2df84da0SMatthew G. Knepley       */
1662*2df84da0SMatthew G. Knepley       /* Nodal basis for evaluation at the vertices: {-xi - eta, 1 + xi, 1 + eta} (1 \mp zeta):
1663*2df84da0SMatthew G. Knepley            \phi^0 = 1/4 (   -xi - eta        + xi zeta + eta zeta) --> /  1  1  1  1  1  1 \ 1
1664*2df84da0SMatthew G. Knepley            \phi^1 = 1/4 (1      + eta - zeta           - eta zeta) --> | -1  1 -1 -1 -1  1 | eta
1665*2df84da0SMatthew G. Knepley            \phi^2 = 1/4 (1 + xi       - zeta - xi zeta)            --> | -1 -1  1 -1  1 -1 | xi
1666*2df84da0SMatthew G. Knepley            \phi^3 = 1/4 (   -xi - eta        - xi zeta - eta zeta) --> | -1 -1 -1  1  1  1 | zeta
1667*2df84da0SMatthew G. Knepley            \phi^4 = 1/4 (1 + xi       + zeta + xi zeta)            --> |  1  1 -1 -1  1 -1 | xi zeta
1668*2df84da0SMatthew G. Knepley            \phi^5 = 1/4 (1      + eta + zeta           + eta zeta) --> \  1 -1  1 -1 -1  1 / eta zeta
1669*2df84da0SMatthew G. Knepley            1/4 /  0  1  1  0  1  1 \
1670*2df84da0SMatthew G. Knepley                | -1  1  0 -1  0  1 |
1671*2df84da0SMatthew G. Knepley                | -1  0  1 -1  1  0 |
1672*2df84da0SMatthew G. Knepley                |  0 -1 -1  0  1  1 |
1673*2df84da0SMatthew G. Knepley                |  1  0 -1 -1  1  0 |
1674*2df84da0SMatthew G. Knepley                \  1 -1  0 -1  0  1 /
1675*2df84da0SMatthew G. Knepley       */
1676*2df84da0SMatthew G. Knepley       coeff[dim * 0 + j] = (1./4.) * (                      verts[dim * 1 + j] + verts[dim * 2 + j]                      + verts[dim * 4 + j] + verts[dim * 5 + j]);
1677*2df84da0SMatthew G. Knepley       coeff[dim * 1 + j] = (1./4.) * (-verts[dim * 0 + j] + verts[dim * 1 + j]                      - verts[dim * 3 + j]                      + verts[dim * 5 + j]);
1678*2df84da0SMatthew G. Knepley       coeff[dim * 2 + j] = (1./4.) * (-verts[dim * 0 + j]                      + verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
1679*2df84da0SMatthew G. Knepley       coeff[dim * 3 + j] = (1./4.) * (                    - verts[dim * 1 + j] - verts[dim * 2 + j]                      + verts[dim * 4 + j] + verts[dim * 5 + j]);
1680*2df84da0SMatthew G. Knepley       coeff[dim * 4 + j] = (1./4.) * ( verts[dim * 0 + j]                      - verts[dim * 2 + j] - verts[dim * 3 + j] + verts[dim * 4 + j]);
1681*2df84da0SMatthew G. Knepley       coeff[dim * 5 + j] = (1./4.) * ( verts[dim * 0 + j] - verts[dim * 1 + j]                      - verts[dim * 3 + j]                      + verts[dim * 5 + j]);
1682*2df84da0SMatthew G. Knepley       /* For reference prism:
1683*2df84da0SMatthew G. Knepley       {0, 0, 0}
1684*2df84da0SMatthew G. Knepley       {0, 1, 0}
1685*2df84da0SMatthew G. Knepley       {1, 0, 0}
1686*2df84da0SMatthew G. Knepley       {0, 0, 1}
1687*2df84da0SMatthew G. Knepley       {0, 0, 0}
1688*2df84da0SMatthew G. Knepley       {0, 0, 0}
1689*2df84da0SMatthew G. Knepley       */
1690*2df84da0SMatthew G. Knepley     }
1691*2df84da0SMatthew G. Knepley     for (i = 0; i < Nq; ++i) {
1692*2df84da0SMatthew G. Knepley       const PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], zeta = points[dimR * i + 2];
1693*2df84da0SMatthew G. Knepley 
1694*2df84da0SMatthew G. Knepley       if (v) {
1695*2df84da0SMatthew G. Knepley         PetscReal extPoint[6];
1696*2df84da0SMatthew G. Knepley         PetscInt  c;
1697*2df84da0SMatthew G. Knepley 
1698*2df84da0SMatthew G. Knepley         extPoint[0] = 1.;
1699*2df84da0SMatthew G. Knepley         extPoint[1] = eta;
1700*2df84da0SMatthew G. Knepley         extPoint[2] = xi;
1701*2df84da0SMatthew G. Knepley         extPoint[3] = zeta;
1702*2df84da0SMatthew G. Knepley         extPoint[4] = xi * zeta;
1703*2df84da0SMatthew G. Knepley         extPoint[5] = eta * zeta;
1704*2df84da0SMatthew G. Knepley         for (c = 0; c < dim; ++c) {
1705*2df84da0SMatthew G. Knepley           PetscReal val = 0.;
1706*2df84da0SMatthew G. Knepley 
1707*2df84da0SMatthew G. Knepley           for (k = 0; k < Nv; ++k) {
1708*2df84da0SMatthew G. Knepley             val += extPoint[k] * coeff[k*dim + c];
1709*2df84da0SMatthew G. Knepley           }
1710*2df84da0SMatthew G. Knepley           v[i*dim + c] = val;
1711*2df84da0SMatthew G. Knepley         }
1712*2df84da0SMatthew G. Knepley       }
1713*2df84da0SMatthew G. Knepley       if (J) {
1714*2df84da0SMatthew G. Knepley         PetscReal extJ[18];
1715*2df84da0SMatthew G. Knepley 
1716*2df84da0SMatthew G. Knepley         extJ[0]  = 0.  ; extJ[1]  = 0.  ; extJ[2]  = 0. ;
1717*2df84da0SMatthew G. Knepley         extJ[3]  = 0.  ; extJ[4]  = 1.  ; extJ[5]  = 0. ;
1718*2df84da0SMatthew G. Knepley         extJ[6]  = 1.  ; extJ[7]  = 0.  ; extJ[8]  = 0. ;
1719*2df84da0SMatthew G. Knepley         extJ[9]  = 0.  ; extJ[10] = 0.  ; extJ[11] = 1. ;
1720*2df84da0SMatthew G. Knepley         extJ[12] = zeta; extJ[13] = 0.  ; extJ[14] = xi ;
1721*2df84da0SMatthew G. Knepley         extJ[15] = 0.  ; extJ[16] = zeta; extJ[17] = eta;
1722*2df84da0SMatthew G. Knepley 
1723*2df84da0SMatthew G. Knepley         for (j = 0; j < dim; j++) {
1724*2df84da0SMatthew G. Knepley           for (k = 0; k < dimR; k++) {
1725*2df84da0SMatthew G. Knepley             PetscReal val = 0.;
1726*2df84da0SMatthew G. Knepley 
1727*2df84da0SMatthew G. Knepley             for (l = 0; l < Nv; l++) {
1728*2df84da0SMatthew G. Knepley               val += coeff[dim * l + j] * extJ[dimR * l + k];
1729*2df84da0SMatthew G. Knepley             }
1730*2df84da0SMatthew G. Knepley             J[i * dim * dim + dim * j + k] = val;
1731*2df84da0SMatthew G. Knepley           }
1732*2df84da0SMatthew G. Knepley         }
1733*2df84da0SMatthew G. Knepley         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1734*2df84da0SMatthew G. Knepley         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1735*2df84da0SMatthew G. Knepley       }
1736*2df84da0SMatthew G. Knepley     }
1737*2df84da0SMatthew G. Knepley   }
1738*2df84da0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1739*2df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
1740*2df84da0SMatthew G. Knepley }
1741*2df84da0SMatthew G. Knepley 
1742dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1743dfccc68fSToby Isaac {
1744ba2698f1SMatthew G. Knepley   DMPolytopeType  ct;
1745dfccc68fSToby Isaac   PetscInt        depth, dim, coordDim, coneSize, i;
1746dfccc68fSToby Isaac   PetscInt        Nq = 0;
1747dfccc68fSToby Isaac   const PetscReal *points = NULL;
1748dfccc68fSToby Isaac   DMLabel         depthLabel;
1749c330f8ffSToby Isaac   PetscReal       xi0[3] = {-1.,-1.,-1.}, v0[3], J0[9], detJ0;
1750dfccc68fSToby Isaac   PetscBool       isAffine = PETSC_TRUE;
1751dfccc68fSToby Isaac   PetscErrorCode  ierr;
1752dfccc68fSToby Isaac 
1753dfccc68fSToby Isaac   PetscFunctionBegin;
1754dfccc68fSToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1755dfccc68fSToby Isaac   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1756dfccc68fSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1757dfccc68fSToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1758dfccc68fSToby Isaac   if (depth == 1 && dim == 1) {
1759dfccc68fSToby Isaac     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1760dfccc68fSToby Isaac   }
1761dfccc68fSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
17622c71b3e2SJacob Faibussowitsch   PetscCheckFalse(coordDim > 3,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim);
17639c3cf19fSMatthew G. Knepley   if (quad) {ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL);CHKERRQ(ierr);}
1764ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1765ba2698f1SMatthew G. Knepley   switch (ct) {
1766ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_POINT:
1767dfccc68fSToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1768dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1769dfccc68fSToby Isaac     break;
1770ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
1771412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
1772ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1773ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1774dfccc68fSToby Isaac     break;
1775ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
1776ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1777ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1778dfccc68fSToby Isaac     break;
1779ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
1780412e9a14SMatthew G. Knepley     ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1781412e9a14SMatthew G. Knepley     isAffine = PETSC_FALSE;
1782412e9a14SMatthew G. Knepley     break;
1783412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1784412e9a14SMatthew G. Knepley     ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1785dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1786dfccc68fSToby Isaac     break;
1787ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
1788ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1789ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1790dfccc68fSToby Isaac     break;
1791ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
1792dfccc68fSToby Isaac     ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1793dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1794dfccc68fSToby Isaac     break;
1795*2df84da0SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM:
1796*2df84da0SMatthew G. Knepley     ierr = DMPlexComputeTriangularPrismGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1797*2df84da0SMatthew G. Knepley     isAffine = PETSC_FALSE;
1798*2df84da0SMatthew G. Knepley     break;
1799*2df84da0SMatthew 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))]);
1800dfccc68fSToby Isaac   }
18017318780aSToby Isaac   if (isAffine && Nq) {
1802dfccc68fSToby Isaac     if (v) {
1803dfccc68fSToby Isaac       for (i = 0; i < Nq; i++) {
1804c330f8ffSToby Isaac         CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]);
1805dfccc68fSToby Isaac       }
1806dfccc68fSToby Isaac     }
18077318780aSToby Isaac     if (detJ) {
18087318780aSToby Isaac       for (i = 0; i < Nq; i++) {
18097318780aSToby Isaac         detJ[i] = detJ0;
1810dfccc68fSToby Isaac       }
18117318780aSToby Isaac     }
18127318780aSToby Isaac     if (J) {
18137318780aSToby Isaac       PetscInt k;
18147318780aSToby Isaac 
18157318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1816dfccc68fSToby Isaac         PetscInt j;
1817dfccc68fSToby Isaac 
18187318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
18197318780aSToby Isaac           J[k] = J0[j];
18207318780aSToby Isaac         }
18217318780aSToby Isaac       }
18227318780aSToby Isaac     }
18237318780aSToby Isaac     if (invJ) {
18247318780aSToby Isaac       PetscInt k;
18257318780aSToby Isaac       switch (coordDim) {
18267318780aSToby Isaac       case 0:
18277318780aSToby Isaac         break;
18287318780aSToby Isaac       case 1:
18297318780aSToby Isaac         invJ[0] = 1./J0[0];
18307318780aSToby Isaac         break;
18317318780aSToby Isaac       case 2:
18327318780aSToby Isaac         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
18337318780aSToby Isaac         break;
18347318780aSToby Isaac       case 3:
18357318780aSToby Isaac         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
18367318780aSToby Isaac         break;
18377318780aSToby Isaac       }
18387318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
18397318780aSToby Isaac         PetscInt j;
18407318780aSToby Isaac 
18417318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
18427318780aSToby Isaac           invJ[k] = invJ[j];
18437318780aSToby Isaac         }
1844dfccc68fSToby Isaac       }
1845dfccc68fSToby Isaac     }
1846dfccc68fSToby Isaac   }
1847dfccc68fSToby Isaac   PetscFunctionReturn(0);
1848dfccc68fSToby Isaac }
1849dfccc68fSToby Isaac 
1850ccd2543fSMatthew G Knepley /*@C
18518e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1852ccd2543fSMatthew G Knepley 
1853d083f849SBarry Smith   Collective on dm
1854ccd2543fSMatthew G Knepley 
18554165533cSJose E. Roman   Input Parameters:
1856ccd2543fSMatthew G Knepley + dm   - the DM
1857ccd2543fSMatthew G Knepley - cell - the cell
1858ccd2543fSMatthew G Knepley 
18594165533cSJose E. Roman   Output Parameters:
1860ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1861ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1862ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1863ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1864ccd2543fSMatthew G Knepley 
1865ccd2543fSMatthew G Knepley   Level: advanced
1866ccd2543fSMatthew G Knepley 
1867ccd2543fSMatthew G Knepley   Fortran Notes:
1868ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1869ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1870ccd2543fSMatthew G Knepley 
1871e8964c0aSStefano Zampini .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinates()
1872ccd2543fSMatthew G Knepley @*/
18738e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1874ccd2543fSMatthew G Knepley {
1875ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1876ccd2543fSMatthew G Knepley 
1877ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1878dfccc68fSToby Isaac   ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr);
18798e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
18808e0841e0SMatthew G. Knepley }
18818e0841e0SMatthew G. Knepley 
1882dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
18838e0841e0SMatthew G. Knepley {
1884dfccc68fSToby Isaac   PetscQuadrature   feQuad;
18858e0841e0SMatthew G. Knepley   PetscSection      coordSection;
18868e0841e0SMatthew G. Knepley   Vec               coordinates;
18878e0841e0SMatthew G. Knepley   PetscScalar      *coords = NULL;
18888e0841e0SMatthew G. Knepley   const PetscReal  *quadPoints;
1889ef0bb6c7SMatthew G. Knepley   PetscTabulation T;
1890f960e424SToby Isaac   PetscInt          dim, cdim, pdim, qdim, Nq, numCoords, q;
18918e0841e0SMatthew G. Knepley   PetscErrorCode    ierr;
18928e0841e0SMatthew G. Knepley 
18938e0841e0SMatthew G. Knepley   PetscFunctionBegin;
18948e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
18958e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
18968e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
18978e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
18988e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1899dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
1900dfccc68fSToby Isaac     PetscDualSpace dsp;
1901dfccc68fSToby Isaac 
1902dfccc68fSToby Isaac     ierr = PetscFEGetDualSpace(fe, &dsp);CHKERRQ(ierr);
1903dfccc68fSToby Isaac     ierr = PetscDualSpaceGetFunctional(dsp, 0, &quad);CHKERRQ(ierr);
19049c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1905dfccc68fSToby Isaac     Nq = 1;
1906dfccc68fSToby Isaac   } else {
19079c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1908dfccc68fSToby Isaac   }
190991d2b7ceSToby Isaac   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
1910dfccc68fSToby Isaac   ierr = PetscFEGetQuadrature(fe, &feQuad);CHKERRQ(ierr);
1911dfccc68fSToby Isaac   if (feQuad == quad) {
1912f9244615SMatthew G. Knepley     ierr = PetscFEGetCellTabulation(fe, J ? 1 : 0, &T);CHKERRQ(ierr);
19132c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numCoords != pdim*cdim,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "There are %d coordinates for point %d != %d*%d", numCoords, point, pdim, cdim);
1914dfccc68fSToby Isaac   } else {
1915ef0bb6c7SMatthew G. Knepley     ierr = PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T);CHKERRQ(ierr);
1916dfccc68fSToby Isaac   }
19172c71b3e2SJacob Faibussowitsch   PetscCheckFalse(qdim != dim,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
1918ef0bb6c7SMatthew G. Knepley   {
1919ef0bb6c7SMatthew G. Knepley     const PetscReal *basis    = T->T[0];
1920ef0bb6c7SMatthew G. Knepley     const PetscReal *basisDer = T->T[1];
1921ef0bb6c7SMatthew G. Knepley     PetscReal        detJt;
1922ef0bb6c7SMatthew G. Knepley 
1923a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
19242c71b3e2SJacob Faibussowitsch     PetscCheckFalse(Nq != T->Np,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %D != %D", Nq, T->Np);
19252c71b3e2SJacob Faibussowitsch     PetscCheckFalse(pdim != T->Nb,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %D != %D", pdim, T->Nb);
19262c71b3e2SJacob Faibussowitsch     PetscCheckFalse(dim != T->Nc,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %D != %D", dim, T->Nc);
19272c71b3e2SJacob Faibussowitsch     PetscCheckFalse(cdim != T->cdim,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %D != %D", cdim, T->cdim);
1928a2a9e04cSMatthew G. Knepley #endif
1929dfccc68fSToby Isaac     if (v) {
1930580bdb30SBarry Smith       ierr = PetscArrayzero(v, Nq*cdim);CHKERRQ(ierr);
1931f960e424SToby Isaac       for (q = 0; q < Nq; ++q) {
1932f960e424SToby Isaac         PetscInt i, k;
1933f960e424SToby Isaac 
1934301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
1935301b184aSMatthew G. Knepley           const PetscInt vertex = k/cdim;
1936301b184aSMatthew G. Knepley           for (i = 0; i < cdim; ++i) {
1937301b184aSMatthew G. Knepley             v[q*cdim + i] += basis[(q*pdim + k)*cdim + i] * PetscRealPart(coords[vertex*cdim + i]);
1938301b184aSMatthew G. Knepley           }
1939301b184aSMatthew G. Knepley         }
1940f960e424SToby Isaac         ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1941f960e424SToby Isaac       }
1942f960e424SToby Isaac     }
19438e0841e0SMatthew G. Knepley     if (J) {
1944580bdb30SBarry Smith       ierr = PetscArrayzero(J, Nq*cdim*cdim);CHKERRQ(ierr);
19458e0841e0SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
19468e0841e0SMatthew G. Knepley         PetscInt i, j, k, c, r;
19478e0841e0SMatthew G. Knepley 
19488e0841e0SMatthew G. Knepley         /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
1949301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
1950301b184aSMatthew G. Knepley           const PetscInt vertex = k/cdim;
1951301b184aSMatthew G. Knepley           for (j = 0; j < dim; ++j) {
1952301b184aSMatthew G. Knepley             for (i = 0; i < cdim; ++i) {
1953301b184aSMatthew G. Knepley               J[(q*cdim + i)*cdim + j] += basisDer[((q*pdim + k)*cdim + i)*dim + j] * PetscRealPart(coords[vertex*cdim + i]);
1954301b184aSMatthew G. Knepley             }
1955301b184aSMatthew G. Knepley           }
1956301b184aSMatthew G. Knepley         }
19573bc0b13bSBarry Smith         ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
19588e0841e0SMatthew G. Knepley         if (cdim > dim) {
19598e0841e0SMatthew G. Knepley           for (c = dim; c < cdim; ++c)
19608e0841e0SMatthew G. Knepley             for (r = 0; r < cdim; ++r)
19618e0841e0SMatthew G. Knepley               J[r*cdim+c] = r == c ? 1.0 : 0.0;
19628e0841e0SMatthew G. Knepley         }
1963f960e424SToby Isaac         if (!detJ && !invJ) continue;
1964a63b72c6SToby Isaac         detJt = 0.;
19658e0841e0SMatthew G. Knepley         switch (cdim) {
19668e0841e0SMatthew G. Knepley         case 3:
1967037dc194SToby Isaac           DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]);
1968037dc194SToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
196917fe8556SMatthew G. Knepley           break;
197049dc4407SMatthew G. Knepley         case 2:
19719f328543SToby Isaac           DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]);
1972037dc194SToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
197349dc4407SMatthew G. Knepley           break;
19748e0841e0SMatthew G. Knepley         case 1:
1975037dc194SToby Isaac           detJt = J[q*cdim*dim];
1976037dc194SToby Isaac           if (invJ) invJ[q*cdim*dim] = 1.0/detJt;
197749dc4407SMatthew G. Knepley         }
1978f960e424SToby Isaac         if (detJ) detJ[q] = detJt;
197949dc4407SMatthew G. Knepley       }
19802c71b3e2SJacob Faibussowitsch     } else PetscCheckFalse(detJ || invJ,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
198149dc4407SMatthew G. Knepley   }
1982ef0bb6c7SMatthew G. Knepley   if (feQuad != quad) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
19838e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
19848e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
19858e0841e0SMatthew G. Knepley }
19868e0841e0SMatthew G. Knepley 
19878e0841e0SMatthew G. Knepley /*@C
19888e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
19898e0841e0SMatthew G. Knepley 
1990d083f849SBarry Smith   Collective on dm
19918e0841e0SMatthew G. Knepley 
19924165533cSJose E. Roman   Input Parameters:
19938e0841e0SMatthew G. Knepley + dm   - the DM
19948e0841e0SMatthew G. Knepley . cell - the cell
1995dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
1996dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
19978e0841e0SMatthew G. Knepley 
19984165533cSJose E. Roman   Output Parameters:
1999dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
20008e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
20018e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
20028e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
20038e0841e0SMatthew G. Knepley 
20048e0841e0SMatthew G. Knepley   Level: advanced
20058e0841e0SMatthew G. Knepley 
20068e0841e0SMatthew G. Knepley   Fortran Notes:
20078e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
20088e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
20098e0841e0SMatthew G. Knepley 
2010e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
20118e0841e0SMatthew G. Knepley @*/
2012dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
20138e0841e0SMatthew G. Knepley {
2014bb4a5db5SMatthew G. Knepley   DM             cdm;
2015dfccc68fSToby Isaac   PetscFE        fe = NULL;
20168e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
20178e0841e0SMatthew G. Knepley 
20188e0841e0SMatthew G. Knepley   PetscFunctionBegin;
20192d89661fSMatthew G. Knepley   PetscValidPointer(detJ, 7);
2020bb4a5db5SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
2021bb4a5db5SMatthew G. Knepley   if (cdm) {
2022dfccc68fSToby Isaac     PetscClassId id;
2023dfccc68fSToby Isaac     PetscInt     numFields;
2024e5e52638SMatthew G. Knepley     PetscDS      prob;
2025dfccc68fSToby Isaac     PetscObject  disc;
2026dfccc68fSToby Isaac 
2027bb4a5db5SMatthew G. Knepley     ierr = DMGetNumFields(cdm, &numFields);CHKERRQ(ierr);
2028dfccc68fSToby Isaac     if (numFields) {
2029bb4a5db5SMatthew G. Knepley       ierr = DMGetDS(cdm, &prob);CHKERRQ(ierr);
2030dfccc68fSToby Isaac       ierr = PetscDSGetDiscretization(prob,0,&disc);CHKERRQ(ierr);
2031dfccc68fSToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
2032dfccc68fSToby Isaac       if (id == PETSCFE_CLASSID) {
2033dfccc68fSToby Isaac         fe = (PetscFE) disc;
2034dfccc68fSToby Isaac       }
2035dfccc68fSToby Isaac     }
2036dfccc68fSToby Isaac   }
2037dfccc68fSToby Isaac   if (!fe) {ierr = DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
2038dfccc68fSToby Isaac   else     {ierr = DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
2039ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
2040ccd2543fSMatthew G Knepley }
2041834e62ceSMatthew G. Knepley 
20429bf2564aSMatt McGurn static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
20439bf2564aSMatt McGurn {
20449bf2564aSMatt McGurn   PetscSection        coordSection;
20459bf2564aSMatt McGurn   Vec                 coordinates;
20469bf2564aSMatt McGurn   const PetscScalar  *coords = NULL;
20479bf2564aSMatt McGurn   PetscInt            d, dof, off;
20489bf2564aSMatt McGurn   PetscErrorCode      ierr;
20499bf2564aSMatt McGurn 
20509bf2564aSMatt McGurn   PetscFunctionBegin;
20519bf2564aSMatt McGurn   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
20529bf2564aSMatt McGurn   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
20539bf2564aSMatt McGurn   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
20549bf2564aSMatt McGurn 
20559bf2564aSMatt McGurn   /* for a point the centroid is just the coord */
20569bf2564aSMatt McGurn   if (centroid) {
20579bf2564aSMatt McGurn     ierr = PetscSectionGetDof(coordSection, cell, &dof);CHKERRQ(ierr);
20589bf2564aSMatt McGurn     ierr = PetscSectionGetOffset(coordSection, cell, &off);CHKERRQ(ierr);
20599bf2564aSMatt McGurn     for (d = 0; d < dof; d++){
20609bf2564aSMatt McGurn       centroid[d] = PetscRealPart(coords[off + d]);
20619bf2564aSMatt McGurn     }
20629bf2564aSMatt McGurn   }
20639bf2564aSMatt McGurn   if (normal) {
20649bf2564aSMatt McGurn     const PetscInt *support, *cones;
20659bf2564aSMatt McGurn     PetscInt        supportSize;
20669bf2564aSMatt McGurn     PetscReal       norm, sign;
20679bf2564aSMatt McGurn 
20689bf2564aSMatt McGurn     /* compute the norm based upon the support centroids */
20699bf2564aSMatt McGurn     ierr = DMPlexGetSupportSize(dm, cell, &supportSize);CHKERRQ(ierr);
20709bf2564aSMatt McGurn     ierr = DMPlexGetSupport(dm, cell, &support);CHKERRQ(ierr);
20719bf2564aSMatt McGurn     ierr = DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL);CHKERRQ(ierr);
20729bf2564aSMatt McGurn 
20739bf2564aSMatt McGurn     /* Take the normal from the centroid of the support to the vertex*/
20749bf2564aSMatt McGurn     ierr = PetscSectionGetDof(coordSection, cell, &dof);CHKERRQ(ierr);
20759bf2564aSMatt McGurn     ierr = PetscSectionGetOffset(coordSection, cell, &off);CHKERRQ(ierr);
20769bf2564aSMatt McGurn     for (d = 0; d < dof; d++){
20779bf2564aSMatt McGurn       normal[d] -= PetscRealPart(coords[off + d]);
20789bf2564aSMatt McGurn     }
20799bf2564aSMatt McGurn 
20809bf2564aSMatt McGurn     /* Determine the sign of the normal based upon its location in the support */
20819bf2564aSMatt McGurn     ierr = DMPlexGetCone(dm, support[0], &cones);CHKERRQ(ierr);
20829bf2564aSMatt McGurn     sign = cones[0] == cell ? 1.0 : -1.0;
20839bf2564aSMatt McGurn 
20849bf2564aSMatt McGurn     norm = DMPlex_NormD_Internal(dim, normal);
20859bf2564aSMatt McGurn     for (d = 0; d < dim; ++d) normal[d] /= (norm*sign);
20869bf2564aSMatt McGurn   }
20879bf2564aSMatt McGurn   if (vol) {
20889bf2564aSMatt McGurn     *vol = 1.0;
20899bf2564aSMatt McGurn   }
20909bf2564aSMatt McGurn   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
20919bf2564aSMatt McGurn   PetscFunctionReturn(0);
20929bf2564aSMatt McGurn }
20939bf2564aSMatt McGurn 
2094011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2095cc08537eSMatthew G. Knepley {
2096cc08537eSMatthew G. Knepley   PetscSection   coordSection;
2097cc08537eSMatthew G. Knepley   Vec            coordinates;
2098a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
209906e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
2100714b99b6SMatthew G. Knepley   PetscInt       coordSize, d;
2101cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
2102cc08537eSMatthew G. Knepley 
2103cc08537eSMatthew G. Knepley   PetscFunctionBegin;
2104cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
210569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2106cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
21072e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
2108cc08537eSMatthew G. Knepley   if (centroid) {
2109714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) centroid[d] = 0.5*PetscRealPart(coords[d] + tmp[d]);
2110cc08537eSMatthew G. Knepley   }
2111cc08537eSMatthew G. Knepley   if (normal) {
2112a60a936bSMatthew G. Knepley     PetscReal norm;
2113a60a936bSMatthew G. Knepley 
21142c71b3e2SJacob Faibussowitsch     PetscCheckFalse(dim != 2,PETSC_COMM_SELF, PETSC_ERR_SUP, "We only support 2D edges right now");
211506e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
211606e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
2117714b99b6SMatthew G. Knepley     norm       = DMPlex_NormD_Internal(dim, normal);
2118714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) normal[d] /= norm;
2119cc08537eSMatthew G. Knepley   }
2120cc08537eSMatthew G. Knepley   if (vol) {
2121714b99b6SMatthew G. Knepley     *vol = 0.0;
2122714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - tmp[d]));
2123714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
2124cc08537eSMatthew G. Knepley   }
2125cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
2126cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
2127cc08537eSMatthew G. Knepley }
2128cc08537eSMatthew G. Knepley 
2129cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */
2130011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2131cc08537eSMatthew G. Knepley {
2132412e9a14SMatthew G. Knepley   DMPolytopeType ct;
2133cc08537eSMatthew G. Knepley   PetscSection   coordSection;
2134cc08537eSMatthew G. Knepley   Vec            coordinates;
2135cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
2136793a2a13SMatthew G. Knepley   PetscInt       fv[4] = {0, 1, 2, 3};
21374f99dae5SMatthew G. Knepley   PetscInt       cdim, coordSize, numCorners, p, d;
2138cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
2139cc08537eSMatthew G. Knepley 
2140cc08537eSMatthew G. Knepley   PetscFunctionBegin;
2141793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
2142412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
2143412e9a14SMatthew G. Knepley   switch (ct) {
21444f99dae5SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR: fv[2] = 3; fv[3] = 2;break;
2145412e9a14SMatthew G. Knepley     default: break;
2146412e9a14SMatthew G. Knepley   }
2147cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
21480a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
214969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2150cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
21514f99dae5SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
21523f27a4e6SJed Brown   {
21533f27a4e6SJed Brown     PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, origin[3] = {0., 0., 0.}, norm;
2154793a2a13SMatthew G. Knepley 
21553f27a4e6SJed Brown     for (d = 0; d < cdim; d++) origin[d] = PetscRealPart(coords[d]);
21564f99dae5SMatthew G. Knepley     for (p = 0; p < numCorners-2; ++p) {
21573f27a4e6SJed Brown       PetscReal e0[3] = {0., 0., 0.}, e1[3] = {0., 0., 0.};
21583f27a4e6SJed Brown       for (d = 0; d < cdim; d++) {
21593f27a4e6SJed Brown         e0[d] = PetscRealPart(coords[cdim*fv[p+1]+d]) - origin[d];
21603f27a4e6SJed Brown         e1[d] = PetscRealPart(coords[cdim*fv[p+2]+d]) - origin[d];
21613f27a4e6SJed Brown       }
21623f27a4e6SJed Brown       const PetscReal dx = e0[1] * e1[2] - e0[2] * e1[1];
21633f27a4e6SJed Brown       const PetscReal dy = e0[2] * e1[0] - e0[0] * e1[2];
21643f27a4e6SJed Brown       const PetscReal dz = e0[0] * e1[1] - e0[1] * e1[0];
21653f27a4e6SJed Brown       const PetscReal a  = PetscSqrtReal(dx*dx + dy*dy + dz*dz);
21664f99dae5SMatthew G. Knepley 
21674f99dae5SMatthew G. Knepley       n[0] += dx;
21684f99dae5SMatthew G. Knepley       n[1] += dy;
21694f99dae5SMatthew G. Knepley       n[2] += dz;
21703f27a4e6SJed Brown       for (d = 0; d < cdim; d++) {
21713f27a4e6SJed Brown         c[d] += a * PetscRealPart(origin[d] + coords[cdim*fv[p+1]+d] + coords[cdim*fv[p+2]+d]) / 3.;
21723f27a4e6SJed Brown       }
2173ceee4971SMatthew G. Knepley     }
21744f99dae5SMatthew G. Knepley     norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
21754f99dae5SMatthew G. Knepley     n[0] /= norm;
21764f99dae5SMatthew G. Knepley     n[1] /= norm;
21774f99dae5SMatthew G. Knepley     n[2] /= norm;
21784f99dae5SMatthew G. Knepley     c[0] /= norm;
21794f99dae5SMatthew G. Knepley     c[1] /= norm;
21804f99dae5SMatthew G. Knepley     c[2] /= norm;
21814f99dae5SMatthew G. Knepley     if (vol) *vol = 0.5*norm;
21824f99dae5SMatthew G. Knepley     if (centroid) for (d = 0; d < cdim; ++d) centroid[d] = c[d];
21834f99dae5SMatthew G. Knepley     if (normal) for (d = 0; d < cdim; ++d) normal[d] = n[d];
21840a1d6728SMatthew G. Knepley   }
21850a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
2186cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
2187cc08537eSMatthew G. Knepley }
2188cc08537eSMatthew G. Knepley 
21890ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */
2190011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
21910ec8681fSMatthew G. Knepley {
2192412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
21930ec8681fSMatthew G. Knepley   PetscSection    coordSection;
21940ec8681fSMatthew G. Knepley   Vec             coordinates;
21950ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
21963f27a4e6SJed Brown   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3], origin[3];
2197a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
2198793a2a13SMatthew G. Knepley   PetscBool       isHybrid = PETSC_FALSE;
2199412e9a14SMatthew G. Knepley   PetscInt        numFaces, f, coordSize, p, d;
22000ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
22010ec8681fSMatthew G. Knepley 
22020ec8681fSMatthew G. Knepley   PetscFunctionBegin;
22032c71b3e2SJacob Faibussowitsch   PetscCheckFalse(dim > 3,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
2204793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
2205412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
2206412e9a14SMatthew G. Knepley   switch (ct) {
2207412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
2208412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
2209412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
2210412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
2211412e9a14SMatthew G. Knepley       isHybrid = PETSC_TRUE;
2212412e9a14SMatthew G. Knepley     default: break;
2213412e9a14SMatthew G. Knepley   }
2214793a2a13SMatthew G. Knepley 
22150ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
221669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
22170ec8681fSMatthew G. Knepley 
2218d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
22190ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
22200ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
2221a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
22220ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
2223793a2a13SMatthew G. Knepley     PetscBool      flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
2224ba2698f1SMatthew G. Knepley     DMPolytopeType ct;
2225793a2a13SMatthew G. Knepley 
2226011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
22273f27a4e6SJed Brown     // If using zero as the origin vertex for each tetrahedron, an element far from the origin will have positive and
22283f27a4e6SJed Brown     // negative volumes that nearly cancel, thus incurring rounding error. Here we define origin[] as the first vertex
22293f27a4e6SJed Brown     // so that all tetrahedra have positive volume.
22303f27a4e6SJed Brown     if (f == 0) for (d = 0; d < dim; d++) origin[d] = PetscRealPart(coords[d]);
2231ba2698f1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, faces[f], &ct);CHKERRQ(ierr);
2232ba2698f1SMatthew G. Knepley     switch (ct) {
2233ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
22340ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
22353f27a4e6SJed Brown         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]) - origin[d];
22363f27a4e6SJed Brown         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]) - origin[d];
22373f27a4e6SJed Brown         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]) - origin[d];
22380ec8681fSMatthew G. Knepley       }
22390ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
2240793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
22410ec8681fSMatthew G. Knepley       vsum += vtmp;
22424f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
22430ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
22441ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
22450ec8681fSMatthew G. Knepley         }
22460ec8681fSMatthew G. Knepley       }
22470ec8681fSMatthew G. Knepley       break;
2248ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
2249412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
2250793a2a13SMatthew G. Knepley     {
2251793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
2252793a2a13SMatthew G. Knepley 
2253793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
2254793a2a13SMatthew G. Knepley       if (isHybrid && f > 1) {fv[2] = 3; fv[3] = 2;}
22550ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
22560ec8681fSMatthew G. Knepley       /* First tet */
22570ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
22583f27a4e6SJed Brown         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[0]*dim+d]) - origin[d];
22593f27a4e6SJed Brown         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[1]*dim+d]) - origin[d];
22603f27a4e6SJed Brown         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]) - origin[d];
22610ec8681fSMatthew G. Knepley       }
22620ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
2263793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
22640ec8681fSMatthew G. Knepley       vsum += vtmp;
22650ec8681fSMatthew G. Knepley       if (centroid) {
22660ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
22670ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
22680ec8681fSMatthew G. Knepley         }
22690ec8681fSMatthew G. Knepley       }
22700ec8681fSMatthew G. Knepley       /* Second tet */
22710ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
22723f27a4e6SJed Brown         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[1]*dim+d]) - origin[d];
22733f27a4e6SJed Brown         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[2]*dim+d]) - origin[d];
22743f27a4e6SJed Brown         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]) - origin[d];
22750ec8681fSMatthew G. Knepley       }
22760ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
2277793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
22780ec8681fSMatthew G. Knepley       vsum += vtmp;
22790ec8681fSMatthew G. Knepley       if (centroid) {
22800ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
22810ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
22820ec8681fSMatthew G. Knepley         }
22830ec8681fSMatthew G. Knepley       }
22840ec8681fSMatthew G. Knepley       break;
2285793a2a13SMatthew G. Knepley     }
22860ec8681fSMatthew G. Knepley     default:
228798921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %D of type %s", faces[f], DMPolytopeTypes[ct]);
22880ec8681fSMatthew G. Knepley     }
22894f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
22900ec8681fSMatthew G. Knepley   }
22918763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
22920ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
22933f27a4e6SJed Brown   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = centroid[d] / (vsum*4) + origin[d];
22943f27a4e6SJed Brown ;
22950ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
22960ec8681fSMatthew G. Knepley }
22970ec8681fSMatthew G. Knepley 
2298834e62ceSMatthew G. Knepley /*@C
2299834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
2300834e62ceSMatthew G. Knepley 
2301d083f849SBarry Smith   Collective on dm
2302834e62ceSMatthew G. Knepley 
23034165533cSJose E. Roman   Input Parameters:
2304834e62ceSMatthew G. Knepley + dm   - the DM
2305834e62ceSMatthew G. Knepley - cell - the cell
2306834e62ceSMatthew G. Knepley 
23074165533cSJose E. Roman   Output Parameters:
2308834e62ceSMatthew G. Knepley + volume   - the cell volume
2309cc08537eSMatthew G. Knepley . centroid - the cell centroid
2310cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
2311834e62ceSMatthew G. Knepley 
2312834e62ceSMatthew G. Knepley   Level: advanced
2313834e62ceSMatthew G. Knepley 
2314834e62ceSMatthew G. Knepley   Fortran Notes:
2315834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2316834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
2317834e62ceSMatthew G. Knepley 
2318e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
2319834e62ceSMatthew G. Knepley @*/
2320cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2321834e62ceSMatthew G. Knepley {
23220ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
2323834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
2324834e62ceSMatthew G. Knepley 
2325834e62ceSMatthew G. Knepley   PetscFunctionBegin;
2326834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2327c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
23282c71b3e2SJacob Faibussowitsch   PetscCheckFalse(depth != dim,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
2329ba2698f1SMatthew G. Knepley   ierr = DMPlexGetPointDepth(dm, cell, &depth);CHKERRQ(ierr);
2330011ea5d8SMatthew G. Knepley   switch (depth) {
23319bf2564aSMatt McGurn   case 0:
23329bf2564aSMatt McGurn     ierr = DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
23339bf2564aSMatt McGurn     break;
2334cc08537eSMatthew G. Knepley   case 1:
2335011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2336cc08537eSMatthew G. Knepley     break;
2337834e62ceSMatthew G. Knepley   case 2:
2338011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2339834e62ceSMatthew G. Knepley     break;
2340834e62ceSMatthew G. Knepley   case 3:
2341011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2342834e62ceSMatthew G. Knepley     break;
2343834e62ceSMatthew G. Knepley   default:
234498921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D (depth %D) for element geometry computation", dim, depth);
2345834e62ceSMatthew G. Knepley   }
2346834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
2347834e62ceSMatthew G. Knepley }
2348113c68e6SMatthew G. Knepley 
2349c501906fSMatthew G. Knepley /*@
2350c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
2351c501906fSMatthew G. Knepley 
2352c501906fSMatthew G. Knepley   Collective on dm
2353c501906fSMatthew G. Knepley 
2354c501906fSMatthew G. Knepley   Input Parameter:
2355c501906fSMatthew G. Knepley . dm - The DMPlex
2356c501906fSMatthew G. Knepley 
2357c501906fSMatthew G. Knepley   Output Parameter:
2358c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
2359c501906fSMatthew G. Knepley 
2360c501906fSMatthew G. Knepley   Level: beginner
2361c501906fSMatthew G. Knepley 
2362c501906fSMatthew G. Knepley @*/
2363c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
2364c0d900a5SMatthew G. Knepley {
2365c0d900a5SMatthew G. Knepley   DM             dmCell;
2366c0d900a5SMatthew G. Knepley   Vec            coordinates;
2367c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
2368c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
2369412e9a14SMatthew G. Knepley   PetscInt       cStart, cEnd, c;
2370c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
2371c0d900a5SMatthew G. Knepley 
2372c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
2373c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2374c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2375c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2376c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2377c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2378c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2379412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2380c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
2381c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
2382cf0b7c11SKarl Rupp   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFEGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2383c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
238492fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2385c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2386c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2387c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2388c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2389cf0b7c11SKarl Rupp     PetscFEGeom *cg;
2390c0d900a5SMatthew G. Knepley 
2391c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2392580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2393cf0b7c11SKarl Rupp     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ);CHKERRQ(ierr);
23942c71b3e2SJacob Faibussowitsch     PetscCheckFalse(*cg->detJ <= 0.0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D", (double) *cg->detJ, c);
2395c0d900a5SMatthew G. Knepley   }
2396c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
2397c0d900a5SMatthew G. Knepley }
2398c0d900a5SMatthew G. Knepley 
2399891a9168SMatthew G. Knepley /*@
2400891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2401891a9168SMatthew G. Knepley 
2402891a9168SMatthew G. Knepley   Input Parameter:
2403891a9168SMatthew G. Knepley . dm - The DM
2404891a9168SMatthew G. Knepley 
2405891a9168SMatthew G. Knepley   Output Parameters:
2406891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2407a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data
2408891a9168SMatthew G. Knepley 
2409891a9168SMatthew G. Knepley   Level: developer
2410891a9168SMatthew G. Knepley 
2411891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
2412891a9168SMatthew G. Knepley @*/
2413113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2414113c68e6SMatthew G. Knepley {
2415113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
2416113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
2417113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
2418113c68e6SMatthew G. Knepley   PetscSection   coordSection;
2419113c68e6SMatthew G. Knepley   Vec            coordinates;
2420113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2421113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
2422113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2423113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
2424113c68e6SMatthew G. Knepley 
2425113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2426113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2427113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2428113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2429113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
2430113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2431113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2432113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2433113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2434113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2435485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2436113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
24379e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2438113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
243992fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2440113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2441113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2442485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2443113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2444113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2445113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2446113c68e6SMatthew G. Knepley 
2447113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2448580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2449113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
2450113c68e6SMatthew G. Knepley   }
2451113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
2452113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
2453113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
2454113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2455113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
24569e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2457113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
245892fd8e1eSJed Brown   ierr = DMSetLocalSection(dmFace, sectionFace);CHKERRQ(ierr);
2459113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
2460113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
2461113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
2462c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2463113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2464113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2465113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2466113c68e6SMatthew G. Knepley     PetscReal        area;
2467412e9a14SMatthew G. Knepley     const PetscInt  *cells;
2468412e9a14SMatthew G. Knepley     PetscInt         ncells, ghost = -1, d, numChildren;
2469113c68e6SMatthew G. Knepley 
24709ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
247150d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2472412e9a14SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
2473412e9a14SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
2474412e9a14SMatthew G. Knepley     /* It is possible to get a face with no support when using partition overlap */
2475412e9a14SMatthew G. Knepley     if (!ncells || ghost >= 0 || numChildren) continue;
2476113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
2477113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
2478113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2479113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2480113c68e6SMatthew G. Knepley     {
2481113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
2482113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
24830453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2484113c68e6SMatthew G. Knepley 
2485113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
2486113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
248706348e87SToby Isaac       if (ncells > 1) {
248806348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
2489113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
249006348e87SToby Isaac       }
249106348e87SToby Isaac       else {
249206348e87SToby Isaac         rcentroid = fg->centroid;
249306348e87SToby Isaac       }
24942e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
24952e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
24960453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2497113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2498113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2499113c68e6SMatthew G. Knepley       }
2500113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
25012c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dim == 2,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d 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]);
25022c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dim == 3,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d 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]);
250398921bdaSJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
2504113c68e6SMatthew G. Knepley       }
2505113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2506113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2507113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2508113c68e6SMatthew G. Knepley       }
250906348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2510113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2511113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2512113c68e6SMatthew G. Knepley       }
2513113c68e6SMatthew G. Knepley     }
2514113c68e6SMatthew G. Knepley   }
2515820f2d46SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
2516113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
2517113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2518113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2519113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2520113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
2521113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2522113c68e6SMatthew G. Knepley 
2523113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
25242c71b3e2SJacob Faibussowitsch     PetscCheckFalse(coneSize != 1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
2525113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
2526113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
25272c71b3e2SJacob Faibussowitsch     PetscCheckFalse(supportSize != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
2528113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
2529113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
2530113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2531113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2532113c68e6SMatthew G. Knepley       if (support[s] == c) {
2533640bce14SSatish Balay         PetscFVCellGeom       *ci;
2534113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
2535113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
2536113c68e6SMatthew G. Knepley 
2537113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
2538113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2539113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
2540113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
2541113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
2542113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2543113c68e6SMatthew G. Knepley       }
2544113c68e6SMatthew G. Knepley     }
2545113c68e6SMatthew G. Knepley   }
2546113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
2547113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2548113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2549113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
2550113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2551113c68e6SMatthew G. Knepley }
2552113c68e6SMatthew G. Knepley 
2553113c68e6SMatthew G. Knepley /*@C
2554113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2555113c68e6SMatthew G. Knepley 
2556113c68e6SMatthew G. Knepley   Not collective
2557113c68e6SMatthew G. Knepley 
25584165533cSJose E. Roman   Input Parameter:
2559113c68e6SMatthew G. Knepley . dm - the DM
2560113c68e6SMatthew G. Knepley 
25614165533cSJose E. Roman   Output Parameter:
2562a5b23f4aSJose E. Roman . minradius - the minimum cell radius
2563113c68e6SMatthew G. Knepley 
2564113c68e6SMatthew G. Knepley   Level: developer
2565113c68e6SMatthew G. Knepley 
2566113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
2567113c68e6SMatthew G. Knepley @*/
2568113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2569113c68e6SMatthew G. Knepley {
2570113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2571113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2572113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
2573113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
2574113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2575113c68e6SMatthew G. Knepley }
2576113c68e6SMatthew G. Knepley 
2577113c68e6SMatthew G. Knepley /*@C
2578113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2579113c68e6SMatthew G. Knepley 
2580113c68e6SMatthew G. Knepley   Logically collective
2581113c68e6SMatthew G. Knepley 
25824165533cSJose E. Roman   Input Parameters:
2583113c68e6SMatthew G. Knepley + dm - the DM
2584a5b23f4aSJose E. Roman - minradius - the minimum cell radius
2585113c68e6SMatthew G. Knepley 
2586113c68e6SMatthew G. Knepley   Level: developer
2587113c68e6SMatthew G. Knepley 
2588113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
2589113c68e6SMatthew G. Knepley @*/
2590113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2591113c68e6SMatthew G. Knepley {
2592113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2593113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2594113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
2595113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2596113c68e6SMatthew G. Knepley }
2597856ac710SMatthew G. Knepley 
2598856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2599856ac710SMatthew G. Knepley {
2600856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
2601856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
2602856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2603856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2604856ac710SMatthew G. Knepley 
2605856ac710SMatthew G. Knepley   PetscFunctionBegin;
2606856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2607856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2608485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2609089217ebSMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
2610856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
2611856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2612c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2613856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2614856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2615856ac710SMatthew G. Knepley     const PetscInt        *faces;
2616856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
2617640bce14SSatish Balay     PetscFVCellGeom        *cg;
2618856ac710SMatthew G. Knepley     PetscBool              boundary;
2619856ac710SMatthew G. Knepley     PetscInt               ghost;
2620856ac710SMatthew G. Knepley 
2621856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2622856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
2623856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
26242c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numFaces < dim,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cell %D has only %D faces, not enough for gradient reconstruction", c, numFaces);
2625856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2626640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2627856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
2628856ac710SMatthew G. Knepley       const PetscInt        *fcells;
2629856ac710SMatthew G. Knepley       PetscInt               ncell, side;
2630856ac710SMatthew G. Knepley 
2631856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2632a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2633856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2634856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
2635856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2636856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
2637856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
2638856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2639856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
2640856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
2641856ac710SMatthew G. Knepley     }
26422c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!usedFaces,PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
2643856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
2644856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2645856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2646a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2647856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2648856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
2649856ac710SMatthew G. Knepley       ++usedFaces;
2650856ac710SMatthew G. Knepley     }
2651856ac710SMatthew G. Knepley   }
2652856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
2653856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2654856ac710SMatthew G. Knepley }
2655856ac710SMatthew G. Knepley 
2656b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2657b81db932SToby Isaac {
2658b81db932SToby Isaac   DMLabel        ghostLabel;
2659b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
2660b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2661b81db932SToby Isaac   PetscSection   neighSec;
2662b81db932SToby Isaac   PetscInt     (*neighbors)[2];
2663b81db932SToby Isaac   PetscInt      *counter;
2664b81db932SToby Isaac   PetscErrorCode ierr;
2665b81db932SToby Isaac 
2666b81db932SToby Isaac   PetscFunctionBegin;
2667b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2668b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2669485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2670485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2671b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
2672b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
2673b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2674c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2675b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2676b81db932SToby Isaac     const PetscInt        *fcells;
2677b81db932SToby Isaac     PetscBool              boundary;
26785bc680faSToby Isaac     PetscInt               ghost = -1;
2679b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2680b81db932SToby Isaac 
268106348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2682a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2683b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2684b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2685b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
268606348e87SToby Isaac     if (numCells == 2) {
2687b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2688b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2689b81db932SToby Isaac         PetscInt cell = fcells[c];
2690b81db932SToby Isaac 
2691e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2692b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
2693b81db932SToby Isaac         }
2694b81db932SToby Isaac       }
2695b81db932SToby Isaac     }
269606348e87SToby Isaac   }
2697b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
2698b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
2699b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2700b81db932SToby Isaac   nStart = 0;
2701b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2702b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2703b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2704b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2705b81db932SToby Isaac     const PetscInt        *fcells;
2706b81db932SToby Isaac     PetscBool              boundary;
27075bc680faSToby Isaac     PetscInt               ghost = -1;
2708b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2709b81db932SToby Isaac 
271006348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2711a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2712b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2713b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2714b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
271506348e87SToby Isaac     if (numCells == 2) {
2716b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2717b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2718b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2719b81db932SToby Isaac 
2720e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2721b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2722b81db932SToby Isaac           off += counter[cell - cStart]++;
2723b81db932SToby Isaac           neighbors[off][0] = f;
2724b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2725b81db932SToby Isaac         }
2726b81db932SToby Isaac       }
2727b81db932SToby Isaac     }
272806348e87SToby Isaac   }
2729b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2730b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2731b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2732317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2733640bce14SSatish Balay     PetscFVCellGeom        *cg;
2734b81db932SToby Isaac 
2735b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2736b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2737b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2738317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
27392c71b3e2SJacob Faibussowitsch     PetscCheckFalse(ghost < 0 && numFaces < dim,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cell %D has only %D faces, not enough for gradient reconstruction", c, numFaces);
2740b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2741640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2742b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2743b81db932SToby Isaac       const PetscInt        *fcells;
2744b81db932SToby Isaac       PetscInt               ncell, side, nface;
2745b81db932SToby Isaac 
2746b81db932SToby Isaac       nface = neighbors[off + f][0];
2747b81db932SToby Isaac       ncell = neighbors[off + f][1];
2748b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2749b81db932SToby Isaac       side  = (c != fcells[0]);
2750b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2751b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2752b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2753b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2754b81db932SToby Isaac     }
2755b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2756b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2757b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2758b81db932SToby Isaac     }
2759b81db932SToby Isaac   }
2760b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
27615fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2762b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2763b81db932SToby Isaac   PetscFunctionReturn(0);
2764b81db932SToby Isaac }
2765b81db932SToby Isaac 
2766856ac710SMatthew G. Knepley /*@
2767856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2768856ac710SMatthew G. Knepley 
2769d083f849SBarry Smith   Collective on dm
2770856ac710SMatthew G. Knepley 
27714165533cSJose E. Roman   Input Parameters:
2772856ac710SMatthew G. Knepley + dm  - The DM
2773856ac710SMatthew G. Knepley . fvm - The PetscFV
27748f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2775856ac710SMatthew G. Knepley 
27766b867d5aSJose E. Roman   Input/Output Parameter:
27776b867d5aSJose E. Roman . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM(); on output
27786b867d5aSJose E. Roman                  the geometric factors for gradient calculation are inserted
27796b867d5aSJose E. Roman 
27806b867d5aSJose E. Roman   Output Parameter:
27816b867d5aSJose E. Roman . dmGrad - The DM describing the layout of gradient data
2782856ac710SMatthew G. Knepley 
2783856ac710SMatthew G. Knepley   Level: developer
2784856ac710SMatthew G. Knepley 
2785856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2786856ac710SMatthew G. Knepley @*/
2787856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2788856ac710SMatthew G. Knepley {
2789856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2790856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2791b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2792856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2793856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2794856ac710SMatthew G. Knepley 
2795856ac710SMatthew G. Knepley   PetscFunctionBegin;
2796856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2797856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2798856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2799485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2800856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2801856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2802856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2803856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2804856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2805b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2806b81db932SToby Isaac   if (!parentSection) {
2807856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2808b5a3613cSMatthew G. Knepley   } else {
2809b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2810b81db932SToby Isaac   }
2811856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2812856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2813856ac710SMatthew G. Knepley   /* Create storage for gradients */
2814856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2815856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2816856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2817856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2818856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
281992fd8e1eSJed Brown   ierr = DMSetLocalSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2820856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2821856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2822856ac710SMatthew G. Knepley }
2823b27d5b9eSToby Isaac 
2824c501906fSMatthew G. Knepley /*@
2825c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2826c501906fSMatthew G. Knepley 
2827d083f849SBarry Smith   Collective on dm
2828c501906fSMatthew G. Knepley 
28294165533cSJose E. Roman   Input Parameters:
2830c501906fSMatthew G. Knepley + dm  - The DM
28316b867d5aSJose E. Roman - fv  - The PetscFV
2832c501906fSMatthew G. Knepley 
2833c501906fSMatthew G. Knepley   Output Parameters:
2834c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2835c501906fSMatthew G. Knepley . faceGeometry - The face geometry
28366b867d5aSJose E. Roman - gradDM       - The gradient matrices
2837c501906fSMatthew G. Knepley 
2838c501906fSMatthew G. Knepley   Level: developer
2839c501906fSMatthew G. Knepley 
2840c501906fSMatthew G. Knepley .seealso: DMPlexComputeGeometryFVM()
2841c501906fSMatthew G. Knepley @*/
2842b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2843b27d5b9eSToby Isaac {
2844b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2845b27d5b9eSToby Isaac   PetscErrorCode ierr;
2846b27d5b9eSToby Isaac 
2847b27d5b9eSToby Isaac   PetscFunctionBegin;
2848b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2849b27d5b9eSToby Isaac   if (!cellgeomobj) {
2850b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2851b27d5b9eSToby Isaac 
2852b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2853b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2854b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2855b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2856b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2857b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2858b27d5b9eSToby Isaac   }
2859b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2860b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2861b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2862b27d5b9eSToby Isaac   if (gradDM) {
2863b27d5b9eSToby Isaac     PetscObject gradobj;
2864b27d5b9eSToby Isaac     PetscBool   computeGradients;
2865b27d5b9eSToby Isaac 
2866b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2867b27d5b9eSToby Isaac     if (!computeGradients) {
2868b27d5b9eSToby Isaac       *gradDM = NULL;
2869b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2870b27d5b9eSToby Isaac     }
2871b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2872b27d5b9eSToby Isaac     if (!gradobj) {
2873b27d5b9eSToby Isaac       DM dmGradInt;
2874b27d5b9eSToby Isaac 
2875b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2876b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2877b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2878b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2879b27d5b9eSToby Isaac     }
2880b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2881b27d5b9eSToby Isaac   }
2882b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2883b27d5b9eSToby Isaac }
2884d6143a4eSToby Isaac 
28859d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
28869d150b73SToby Isaac {
28879d150b73SToby Isaac   PetscInt l, m;
28889d150b73SToby Isaac 
2889cd345991SToby Isaac   PetscFunctionBeginHot;
28909d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
28919d150b73SToby Isaac     /* invert Jacobian, multiply */
28929d150b73SToby Isaac     PetscScalar det, idet;
28939d150b73SToby Isaac 
28949d150b73SToby Isaac     switch (dimR) {
28959d150b73SToby Isaac     case 1:
28969d150b73SToby Isaac       invJ[0] = 1./ J[0];
28979d150b73SToby Isaac       break;
28989d150b73SToby Isaac     case 2:
28999d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
29009d150b73SToby Isaac       idet = 1./det;
29019d150b73SToby Isaac       invJ[0] =  J[3] * idet;
29029d150b73SToby Isaac       invJ[1] = -J[1] * idet;
29039d150b73SToby Isaac       invJ[2] = -J[2] * idet;
29049d150b73SToby Isaac       invJ[3] =  J[0] * idet;
29059d150b73SToby Isaac       break;
29069d150b73SToby Isaac     case 3:
29079d150b73SToby Isaac       {
29089d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
29099d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
29109d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
29119d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
29129d150b73SToby Isaac         idet = 1./det;
29139d150b73SToby Isaac         invJ[0] *= idet;
29149d150b73SToby Isaac         invJ[1] *= idet;
29159d150b73SToby Isaac         invJ[2] *= idet;
29169d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
29179d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
29189d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
29199d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
29209d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
29219d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
29229d150b73SToby Isaac       }
29239d150b73SToby Isaac       break;
29249d150b73SToby Isaac     }
29259d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
29269d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2927c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
29289d150b73SToby Isaac       }
29299d150b73SToby Isaac     }
29309d150b73SToby Isaac   } else {
29319d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
29329d150b73SToby Isaac     char transpose = 'C';
29339d150b73SToby Isaac #else
29349d150b73SToby Isaac     char transpose = 'T';
29359d150b73SToby Isaac #endif
29369d150b73SToby Isaac     PetscBLASInt m = dimR;
29379d150b73SToby Isaac     PetscBLASInt n = dimC;
29389d150b73SToby Isaac     PetscBLASInt one = 1;
29399d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
29409d150b73SToby Isaac 
29419d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
29429d150b73SToby Isaac 
29439d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
29442c71b3e2SJacob Faibussowitsch     PetscCheckFalse(info != 0,PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
29459d150b73SToby Isaac 
2946c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
29479d150b73SToby Isaac   }
29489d150b73SToby Isaac   PetscFunctionReturn(0);
29499d150b73SToby Isaac }
29509d150b73SToby Isaac 
29519d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
29529d150b73SToby Isaac {
2953c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
29549d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
29559d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
29569d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
29579d150b73SToby Isaac   PetscErrorCode ierr;
29589d150b73SToby Isaac 
29599d150b73SToby Isaac   PetscFunctionBegin;
29609d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29619d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
29622c71b3e2SJacob Faibussowitsch   PetscCheckFalse(coordSize < dimC * numV,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
296369291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
296469291d52SBarry Smith   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
29659d150b73SToby Isaac   cellCoords = &cellData[0];
29669d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
29679d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
29689d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
29699d150b73SToby Isaac   invJ       = &J[dimR * dimC];
29709d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
29719d150b73SToby Isaac   if (dimR == 2) {
29729d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
29739d150b73SToby Isaac 
29749d150b73SToby Isaac     for (i = 0; i < 4; i++) {
29759d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
29769d150b73SToby Isaac 
29779d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
29789d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
29799d150b73SToby Isaac       }
29809d150b73SToby Isaac     }
29819d150b73SToby Isaac   } else if (dimR == 3) {
29829d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
29839d150b73SToby Isaac 
29849d150b73SToby Isaac     for (i = 0; i < 8; i++) {
29859d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
29869d150b73SToby Isaac 
29879d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
29889d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
29899d150b73SToby Isaac       }
29909d150b73SToby Isaac     }
29919d150b73SToby Isaac   } else {
29929d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
29939d150b73SToby Isaac   }
29949d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
29959d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
29969d150b73SToby Isaac     PetscReal *swap;
29979d150b73SToby Isaac 
29989d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
29999d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
30009d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
30019d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
30029d150b73SToby Isaac       }
30039d150b73SToby Isaac     }
30049d150b73SToby Isaac 
30059d150b73SToby Isaac     if (i < dimR - 1) {
30069d150b73SToby Isaac       swap = cellCoeffs;
30079d150b73SToby Isaac       cellCoeffs = cellCoords;
30089d150b73SToby Isaac       cellCoords = swap;
30099d150b73SToby Isaac     }
30109d150b73SToby Isaac   }
3011580bdb30SBarry Smith   ierr = PetscArrayzero(refCoords,numPoints * dimR);CHKERRQ(ierr);
30129d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
30139d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
30149d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
30159d150b73SToby Isaac 
30169d150b73SToby Isaac       /* compute -residual and Jacobian */
30179d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
30189d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
30199d150b73SToby Isaac       for (k = 0; k < numV; k++) {
30209d150b73SToby Isaac         PetscReal extCoord = 1.;
30219d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
30229d150b73SToby Isaac           PetscReal coord = guess[l];
30239d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
30249d150b73SToby Isaac 
30259d150b73SToby Isaac           extCoord *= dep * coord + !dep;
30269d150b73SToby Isaac           extJ[l] = dep;
30279d150b73SToby Isaac 
30289d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
30299d150b73SToby Isaac             PetscReal coord = guess[m];
30309d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
30319d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
30329d150b73SToby Isaac 
30339d150b73SToby Isaac             extJ[l] *= mult;
30349d150b73SToby Isaac           }
30359d150b73SToby Isaac         }
30369d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
30379d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
30389d150b73SToby Isaac 
30399d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
30409d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
30419d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
30429d150b73SToby Isaac           }
30439d150b73SToby Isaac         }
30449d150b73SToby Isaac       }
304576bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
30460611203eSToby Isaac         PetscReal maxAbs = 0.;
30470611203eSToby Isaac 
30480611203eSToby Isaac         for (l = 0; l < dimC; l++) {
30490611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
30500611203eSToby Isaac         }
30517d3de750SJacob Faibussowitsch         ierr = PetscInfo(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
30520611203eSToby Isaac       }
30539d150b73SToby Isaac 
30549d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
30559d150b73SToby Isaac     }
30569d150b73SToby Isaac   }
305769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
305869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
30599d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
30609d150b73SToby Isaac   PetscFunctionReturn(0);
30619d150b73SToby Isaac }
30629d150b73SToby Isaac 
30639d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
30649d150b73SToby Isaac {
30659d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
30669d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
30679d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
30689d150b73SToby Isaac   PetscErrorCode ierr;
30699d150b73SToby Isaac 
30709d150b73SToby Isaac   PetscFunctionBegin;
30719d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30729d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
30732c71b3e2SJacob Faibussowitsch   PetscCheckFalse(coordSize < dimC * numV,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
307469291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
30759d150b73SToby Isaac   cellCoords = &cellData[0];
30769d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
30779d150b73SToby Isaac   if (dimR == 2) {
30789d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
30799d150b73SToby Isaac 
30809d150b73SToby Isaac     for (i = 0; i < 4; i++) {
30819d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
30829d150b73SToby Isaac 
30839d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
30849d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
30859d150b73SToby Isaac       }
30869d150b73SToby Isaac     }
30879d150b73SToby Isaac   } else if (dimR == 3) {
30889d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
30899d150b73SToby Isaac 
30909d150b73SToby Isaac     for (i = 0; i < 8; i++) {
30919d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
30929d150b73SToby Isaac 
30939d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
30949d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
30959d150b73SToby Isaac       }
30969d150b73SToby Isaac     }
30979d150b73SToby Isaac   } else {
30989d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
30999d150b73SToby Isaac   }
31009d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
31019d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
31029d150b73SToby Isaac     PetscReal *swap;
31039d150b73SToby Isaac 
31049d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
31059d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
31069d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
31079d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
31089d150b73SToby Isaac       }
31099d150b73SToby Isaac     }
31109d150b73SToby Isaac 
31119d150b73SToby Isaac     if (i < dimR - 1) {
31129d150b73SToby Isaac       swap = cellCoeffs;
31139d150b73SToby Isaac       cellCoeffs = cellCoords;
31149d150b73SToby Isaac       cellCoords = swap;
31159d150b73SToby Isaac     }
31169d150b73SToby Isaac   }
3117580bdb30SBarry Smith   ierr = PetscArrayzero(realCoords,numPoints * dimC);CHKERRQ(ierr);
31189d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
31199d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
31209d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
31219d150b73SToby Isaac 
31229d150b73SToby Isaac     for (k = 0; k < numV; k++) {
31239d150b73SToby Isaac       PetscReal extCoord = 1.;
31249d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
31259d150b73SToby Isaac         PetscReal coord = guess[l];
31269d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
31279d150b73SToby Isaac 
31289d150b73SToby Isaac         extCoord *= dep * coord + !dep;
31299d150b73SToby Isaac       }
31309d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
31319d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
31329d150b73SToby Isaac 
31339d150b73SToby Isaac         mapped[l] += coeff * extCoord;
31349d150b73SToby Isaac       }
31359d150b73SToby Isaac     }
31369d150b73SToby Isaac   }
313769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
31389d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
31399d150b73SToby Isaac   PetscFunctionReturn(0);
31409d150b73SToby Isaac }
31419d150b73SToby Isaac 
31429c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
31439c3cf19fSMatthew G. Knepley static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
31449d150b73SToby Isaac {
31459c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
3146c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
3147c6e120d1SToby Isaac   PetscReal      *invV, *modes;
3148c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
3149c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
31509d150b73SToby Isaac   PetscErrorCode ierr;
31519d150b73SToby Isaac 
31529d150b73SToby Isaac   PetscFunctionBegin;
31539c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
31549d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
31552c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numComp != Nc,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc);
31569d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
31579d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
315869291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
31599d150b73SToby Isaac   invV = fe->invV;
3160012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3161012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3162012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
3163012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
31649d150b73SToby Isaac     }
31659d150b73SToby Isaac   }
316669291d52SBarry Smith   ierr   = DMGetWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
31679c3cf19fSMatthew G. Knepley   D      = &B[pdim*Nc];
31689c3cf19fSMatthew G. Knepley   resNeg = &D[pdim*Nc * dimR];
316969291d52SBarry Smith   ierr = DMGetWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
31709c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
31719c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
31729d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
31739d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
31749b1f03cbSToby 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 */
31759d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
31769d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
31779c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc; k++) {resNeg[k] = realCoords[j * Nc + k];}
31789c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc * dimR; k++) {J[k] = 0.;}
31799c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
31809c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
3181012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
31829d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
3183012b7cc6SMatthew G. Knepley             J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
31849d150b73SToby Isaac           }
31859d150b73SToby Isaac         }
31869d150b73SToby Isaac       }
318776bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
31880611203eSToby Isaac         PetscReal maxAbs = 0.;
31890611203eSToby Isaac 
31909c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
31910611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
31920611203eSToby Isaac         }
31937d3de750SJacob Faibussowitsch         ierr = PetscInfo(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
31940611203eSToby Isaac       }
31959c3cf19fSMatthew G. Knepley       ierr = DMPlexCoordinatesToReference_NewtonUpdate(Nc,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
31969d150b73SToby Isaac     }
31979d150b73SToby Isaac   }
319869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
319969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
320069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
32019d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
32029d150b73SToby Isaac   PetscFunctionReturn(0);
32039d150b73SToby Isaac }
32049d150b73SToby Isaac 
32059c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
32069c3cf19fSMatthew G. Knepley static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
32079d150b73SToby Isaac {
32089c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, coordSize;
3209c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
3210c6e120d1SToby Isaac   PetscReal      *invV, *modes;
32119d150b73SToby Isaac   PetscReal      *B;
32129d150b73SToby Isaac   PetscErrorCode ierr;
32139d150b73SToby Isaac 
32149d150b73SToby Isaac   PetscFunctionBegin;
32159c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
32169d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
32172c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numComp != Nc,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc);
32189d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
32199d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
322069291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
32219d150b73SToby Isaac   invV = fe->invV;
3222012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3223012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3224012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
3225012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
32269d150b73SToby Isaac     }
32279d150b73SToby Isaac   }
322869291d52SBarry Smith   ierr = DMGetWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
3229012b7cc6SMatthew G. Knepley   ierr = PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL);CHKERRQ(ierr);
32309c3cf19fSMatthew G. Knepley   for (i = 0; i < numPoints * Nc; i++) {realCoords[i] = 0.;}
32319d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
32329c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
32339d150b73SToby Isaac 
32349c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
32359c3cf19fSMatthew G. Knepley       for (l = 0; l < Nc; l++) {
323640cf36b3SToby Isaac         mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
32379d150b73SToby Isaac       }
32389d150b73SToby Isaac     }
32399d150b73SToby Isaac   }
324069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
324169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
32429d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
32439d150b73SToby Isaac   PetscFunctionReturn(0);
32449d150b73SToby Isaac }
32459d150b73SToby Isaac 
3246d6143a4eSToby Isaac /*@
3247d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
3248d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
3249d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
3250d6143a4eSToby Isaac 
3251d6143a4eSToby Isaac   Not collective
3252d6143a4eSToby Isaac 
3253d6143a4eSToby Isaac   Input Parameters:
3254d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
3255d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
3256d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
3257d6143a4eSToby Isaac . cell       - the cell whose map is used.
3258d6143a4eSToby Isaac . numPoints  - the number of points to locate
32591b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
3260d6143a4eSToby Isaac 
3261d6143a4eSToby Isaac   Output Parameters:
3262d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
32631b266c99SBarry Smith 
32641b266c99SBarry Smith   Level: intermediate
326573c9229bSMatthew Knepley 
326673c9229bSMatthew Knepley .seealso: DMPlexReferenceToCoordinates()
3267d6143a4eSToby Isaac @*/
3268d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
3269d6143a4eSToby Isaac {
3270485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
32719d150b73SToby Isaac   DM             coordDM = NULL;
32729d150b73SToby Isaac   Vec            coords;
32739d150b73SToby Isaac   PetscFE        fe = NULL;
32749d150b73SToby Isaac   PetscErrorCode ierr;
32759d150b73SToby Isaac 
3276d6143a4eSToby Isaac   PetscFunctionBegin;
32779d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32789d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
32799d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
32809d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
32819d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
32829d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
32839d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
32849d150b73SToby Isaac   if (coordDM) {
32859d150b73SToby Isaac     PetscInt coordFields;
32869d150b73SToby Isaac 
32879d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
32889d150b73SToby Isaac     if (coordFields) {
32899d150b73SToby Isaac       PetscClassId id;
32909d150b73SToby Isaac       PetscObject  disc;
32919d150b73SToby Isaac 
329244a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
32939d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
32949d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
32959d150b73SToby Isaac         fe = (PetscFE) disc;
32969d150b73SToby Isaac       }
32979d150b73SToby Isaac     }
32989d150b73SToby Isaac   }
3299412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
33002c71b3e2SJacob Faibussowitsch   PetscCheckFalse(cell < cStart || cell >= cEnd,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);
33019d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
33029d150b73SToby Isaac     PetscInt  coneSize;
33039d150b73SToby Isaac     PetscBool isSimplex, isTensor;
33049d150b73SToby Isaac 
33059d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
33069d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
33079d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
33089d150b73SToby Isaac     if (isSimplex) {
33099d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
33109d150b73SToby Isaac 
331169291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
33129d150b73SToby Isaac       J    = &v0[dimC];
33139d150b73SToby Isaac       invJ = &J[dimC * dimC];
33149d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
33159d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3316c330f8ffSToby Isaac         const PetscReal x0[3] = {-1.,-1.,-1.};
3317c330f8ffSToby Isaac 
3318c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
33199d150b73SToby Isaac       }
332069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
33219d150b73SToby Isaac     } else if (isTensor) {
33229d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
332398921bdaSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
33249d150b73SToby Isaac   } else {
33259d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
33269d150b73SToby Isaac   }
33279d150b73SToby Isaac   PetscFunctionReturn(0);
33289d150b73SToby Isaac }
33299d150b73SToby Isaac 
33309d150b73SToby Isaac /*@
33319d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
33329d150b73SToby Isaac 
33339d150b73SToby Isaac   Not collective
33349d150b73SToby Isaac 
33359d150b73SToby Isaac   Input Parameters:
33369d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
33379d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
33389d150b73SToby Isaac                as a multilinear map for tensor-product elements
33399d150b73SToby Isaac . cell       - the cell whose map is used.
33409d150b73SToby Isaac . numPoints  - the number of points to locate
3341a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
33429d150b73SToby Isaac 
33439d150b73SToby Isaac   Output Parameters:
33449d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
33451b266c99SBarry Smith 
33461b266c99SBarry Smith    Level: intermediate
334773c9229bSMatthew Knepley 
334873c9229bSMatthew Knepley .seealso: DMPlexCoordinatesToReference()
33499d150b73SToby Isaac @*/
33509d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
33519d150b73SToby Isaac {
3352485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
33539d150b73SToby Isaac   DM             coordDM = NULL;
33549d150b73SToby Isaac   Vec            coords;
33559d150b73SToby Isaac   PetscFE        fe = NULL;
33569d150b73SToby Isaac   PetscErrorCode ierr;
33579d150b73SToby Isaac 
33589d150b73SToby Isaac   PetscFunctionBegin;
33599d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33609d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
33619d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
33629d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
33639d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
33649d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
33659d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
33669d150b73SToby Isaac   if (coordDM) {
33679d150b73SToby Isaac     PetscInt coordFields;
33689d150b73SToby Isaac 
33699d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
33709d150b73SToby Isaac     if (coordFields) {
33719d150b73SToby Isaac       PetscClassId id;
33729d150b73SToby Isaac       PetscObject  disc;
33739d150b73SToby Isaac 
337444a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
33759d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
33769d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
33779d150b73SToby Isaac         fe = (PetscFE) disc;
33789d150b73SToby Isaac       }
33799d150b73SToby Isaac     }
33809d150b73SToby Isaac   }
3381412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
33822c71b3e2SJacob Faibussowitsch   PetscCheckFalse(cell < cStart || cell >= cEnd,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);
33839d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
33849d150b73SToby Isaac     PetscInt  coneSize;
33859d150b73SToby Isaac     PetscBool isSimplex, isTensor;
33869d150b73SToby Isaac 
33879d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
33889d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
33899d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
33909d150b73SToby Isaac     if (isSimplex) {
33919d150b73SToby Isaac       PetscReal detJ, *v0, *J;
33929d150b73SToby Isaac 
339369291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
33949d150b73SToby Isaac       J    = &v0[dimC];
33959d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
3396c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3397c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1.,-1.,-1.};
3398c330f8ffSToby Isaac 
3399c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
34009d150b73SToby Isaac       }
340169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
34029d150b73SToby Isaac     } else if (isTensor) {
34039d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
340498921bdaSJacob Faibussowitsch     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
34059d150b73SToby Isaac   } else {
34069d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
34079d150b73SToby Isaac   }
3408d6143a4eSToby Isaac   PetscFunctionReturn(0);
3409d6143a4eSToby Isaac }
34100139fca9SMatthew G. Knepley 
34110139fca9SMatthew G. Knepley /*@C
34120139fca9SMatthew G. Knepley   DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates.
34130139fca9SMatthew G. Knepley 
34140139fca9SMatthew G. Knepley   Not collective
34150139fca9SMatthew G. Knepley 
34160139fca9SMatthew G. Knepley   Input Parameters:
34170139fca9SMatthew G. Knepley + dm      - The DM
34180139fca9SMatthew G. Knepley . time    - The time
34190139fca9SMatthew G. Knepley - func    - The function transforming current coordinates to new coordaintes
34200139fca9SMatthew G. Knepley 
34210139fca9SMatthew G. Knepley    Calling sequence of func:
34220139fca9SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
34230139fca9SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
34240139fca9SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
34250139fca9SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
34260139fca9SMatthew G. Knepley 
34270139fca9SMatthew G. Knepley +  dim          - The spatial dimension
34280139fca9SMatthew G. Knepley .  Nf           - The number of input fields (here 1)
34290139fca9SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
34300139fca9SMatthew G. Knepley .  uOff         - The offset of the coordinates in u[] (here 0)
34310139fca9SMatthew G. Knepley .  uOff_x       - The offset of the coordinates in u_x[] (here 0)
34320139fca9SMatthew G. Knepley .  u            - The coordinate values at this point in space
34330139fca9SMatthew G. Knepley .  u_t          - The coordinate time derivative at this point in space (here NULL)
34340139fca9SMatthew G. Knepley .  u_x          - The coordinate derivatives at this point in space
34350139fca9SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
34360139fca9SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
34370139fca9SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
34380139fca9SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
34390139fca9SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
34400139fca9SMatthew G. Knepley .  t            - The current time
34410139fca9SMatthew G. Knepley .  x            - The coordinates of this point (here not used)
34420139fca9SMatthew G. Knepley .  numConstants - The number of constants
34430139fca9SMatthew G. Knepley .  constants    - The value of each constant
34440139fca9SMatthew G. Knepley -  f            - The new coordinates at this point in space
34450139fca9SMatthew G. Knepley 
34460139fca9SMatthew G. Knepley   Level: intermediate
34470139fca9SMatthew G. Knepley 
34480139fca9SMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMProjectFieldLocal(), DMProjectFieldLabelLocal()
34490139fca9SMatthew G. Knepley @*/
34500139fca9SMatthew G. Knepley PetscErrorCode DMPlexRemapGeometry(DM dm, PetscReal time,
34510139fca9SMatthew G. Knepley                                    void (*func)(PetscInt, PetscInt, PetscInt,
34520139fca9SMatthew G. Knepley                                                 const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
34530139fca9SMatthew G. Knepley                                                 const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
34540139fca9SMatthew G. Knepley                                                 PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]))
34550139fca9SMatthew G. Knepley {
34560139fca9SMatthew G. Knepley   DM             cdm;
34578bf1a49fSMatthew G. Knepley   DMField        cf;
34580139fca9SMatthew G. Knepley   Vec            lCoords, tmpCoords;
34590139fca9SMatthew G. Knepley   PetscErrorCode ierr;
34600139fca9SMatthew G. Knepley 
34610139fca9SMatthew G. Knepley   PetscFunctionBegin;
34620139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
34630139fca9SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &lCoords);CHKERRQ(ierr);
34640139fca9SMatthew G. Knepley   ierr = DMGetLocalVector(cdm, &tmpCoords);CHKERRQ(ierr);
34650139fca9SMatthew G. Knepley   ierr = VecCopy(lCoords, tmpCoords);CHKERRQ(ierr);
34668bf1a49fSMatthew G. Knepley   /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */
34678bf1a49fSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &cf);CHKERRQ(ierr);
34688bf1a49fSMatthew G. Knepley   cdm->coordinateField = cf;
34690139fca9SMatthew G. Knepley   ierr = DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords);CHKERRQ(ierr);
34708bf1a49fSMatthew G. Knepley   cdm->coordinateField = NULL;
34710139fca9SMatthew G. Knepley   ierr = DMRestoreLocalVector(cdm, &tmpCoords);CHKERRQ(ierr);
3472cdaaecf7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, lCoords);CHKERRQ(ierr);
34730139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
34740139fca9SMatthew G. Knepley }
34750139fca9SMatthew G. Knepley 
34760139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z,
34770139fca9SMatthew G. Knepley   / 1  0  m_0 \
34780139fca9SMatthew G. Knepley   | 0  1  m_1 |
34790139fca9SMatthew G. Knepley   \ 0  0   1  /
34800139fca9SMatthew G. Knepley */
34810139fca9SMatthew G. Knepley static void f0_shear(PetscInt dim, PetscInt Nf, PetscInt NfAux,
34820139fca9SMatthew G. Knepley                      const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
34830139fca9SMatthew G. Knepley                      const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
34840139fca9SMatthew G. Knepley                      PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar coords[])
34850139fca9SMatthew G. Knepley {
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 
34900139fca9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
34910139fca9SMatthew G. Knepley     coords[c] = u[c] + constants[c+1]*u[ax];
34920139fca9SMatthew G. Knepley   }
34930139fca9SMatthew G. Knepley }
34940139fca9SMatthew G. Knepley 
34950139fca9SMatthew G. Knepley /*@C
34960139fca9SMatthew G. Knepley   DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates.
34970139fca9SMatthew G. Knepley 
34980139fca9SMatthew G. Knepley   Not collective
34990139fca9SMatthew G. Knepley 
35000139fca9SMatthew G. Knepley   Input Parameters:
35010139fca9SMatthew G. Knepley + dm          - The DM
35023ee9839eSMatthew G. Knepley . direction   - The shear coordinate direction, e.g. 0 is the x-axis
35030139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction
35040139fca9SMatthew G. Knepley 
35050139fca9SMatthew G. Knepley   Level: intermediate
35060139fca9SMatthew G. Knepley 
35070139fca9SMatthew G. Knepley .seealso: DMPlexRemapGeometry()
35080139fca9SMatthew G. Knepley @*/
35093ee9839eSMatthew G. Knepley PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[])
35100139fca9SMatthew G. Knepley {
35110139fca9SMatthew G. Knepley   DM             cdm;
35120139fca9SMatthew G. Knepley   PetscDS        cds;
35130139fca9SMatthew G. Knepley   PetscObject    obj;
35140139fca9SMatthew G. Knepley   PetscClassId   id;
35150139fca9SMatthew G. Knepley   PetscScalar   *moduli;
35163ee9839eSMatthew G. Knepley   const PetscInt dir = (PetscInt) direction;
35170139fca9SMatthew G. Knepley   PetscInt       dE, d, e;
35180139fca9SMatthew G. Knepley   PetscErrorCode ierr;
35190139fca9SMatthew G. Knepley 
35200139fca9SMatthew G. Knepley   PetscFunctionBegin;
35210139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
35220139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
35230139fca9SMatthew G. Knepley   ierr = PetscMalloc1(dE+1, &moduli);CHKERRQ(ierr);
35240139fca9SMatthew G. Knepley   moduli[0] = dir;
3525cdaaecf7SMatthew G. Knepley   for (d = 0, e = 0; d < dE; ++d) moduli[d+1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0);
35260139fca9SMatthew G. Knepley   ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
35270139fca9SMatthew G. Knepley   ierr = PetscDSGetDiscretization(cds, 0, &obj);CHKERRQ(ierr);
35280139fca9SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
35290139fca9SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
35300139fca9SMatthew G. Knepley     Vec           lCoords;
35310139fca9SMatthew G. Knepley     PetscSection  cSection;
35320139fca9SMatthew G. Knepley     PetscScalar  *coords;
35330139fca9SMatthew G. Knepley     PetscInt      vStart, vEnd, v;
35340139fca9SMatthew G. Knepley 
35350139fca9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
35360139fca9SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &cSection);CHKERRQ(ierr);
35370139fca9SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &lCoords);CHKERRQ(ierr);
35380139fca9SMatthew G. Knepley     ierr = VecGetArray(lCoords, &coords);CHKERRQ(ierr);
35390139fca9SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
35400139fca9SMatthew G. Knepley       PetscReal ds;
35410139fca9SMatthew G. Knepley       PetscInt  off, c;
35420139fca9SMatthew G. Knepley 
35430139fca9SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, v, &off);CHKERRQ(ierr);
35440139fca9SMatthew G. Knepley       ds   = PetscRealPart(coords[off+dir]);
35450139fca9SMatthew G. Knepley       for (c = 0; c < dE; ++c) coords[off+c] += moduli[c]*ds;
35460139fca9SMatthew G. Knepley     }
35470139fca9SMatthew G. Knepley     ierr = VecRestoreArray(lCoords, &coords);CHKERRQ(ierr);
35480139fca9SMatthew G. Knepley   } else {
35490139fca9SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, dE+1, moduli);CHKERRQ(ierr);
35500139fca9SMatthew G. Knepley     ierr = DMPlexRemapGeometry(dm, 0.0, f0_shear);CHKERRQ(ierr);
35510139fca9SMatthew G. Knepley   }
35520139fca9SMatthew G. Knepley   ierr = PetscFree(moduli);CHKERRQ(ierr);
35530139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
35540139fca9SMatthew G. Knepley }
3555