xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 9bf2564a90984229938de886a261077a99cf6a07)
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
133985bb02SVaclav Hapla . npoints - The number of sought points
143985bb02SVaclav Hapla . coords - The array of coordinates of the sought points
153985bb02SVaclav Hapla - eps - The tolerance or PETSC_DEFAULT
163985bb02SVaclav Hapla 
173985bb02SVaclav Hapla   Output Parameters:
183985bb02SVaclav Hapla . dagPoints - The array of found DAG points, or -1 if not found
193985bb02SVaclav Hapla 
203985bb02SVaclav Hapla   Level: intermediate
213985bb02SVaclav Hapla 
223985bb02SVaclav Hapla   Notes:
233985bb02SVaclav Hapla   The length of the array coords must be npoints * dim where dim is the spatial dimension returned by DMGetDimension().
243985bb02SVaclav Hapla 
253985bb02SVaclav Hapla   The output array dagPoints is NOT newly allocated; the user must pass an array of length npoints.
263985bb02SVaclav Hapla 
273985bb02SVaclav Hapla   Each rank does the search independently; a nonnegative value is returned only if this rank's local DMPlex portion contains the point.
283985bb02SVaclav Hapla 
293985bb02SVaclav Hapla   The tolerance is interpreted as the maximum Euclidean (L2) distance of the sought point from the specified coordinates.
303985bb02SVaclav Hapla 
31335ef845SVaclav 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.
32335ef845SVaclav Hapla 
3337900f7dSMatthew G. Knepley .seealso: DMPlexCreate(), DMGetCoordinatesLocal()
343985bb02SVaclav Hapla @*/
353985bb02SVaclav Hapla PetscErrorCode DMPlexFindVertices(DM dm, PetscInt npoints, const PetscReal coord[], PetscReal eps, PetscInt dagPoints[])
363985bb02SVaclav Hapla {
3737900f7dSMatthew G. Knepley   PetscInt          c, cdim, i, j, o, p, vStart, vEnd;
383985bb02SVaclav Hapla   Vec               allCoordsVec;
393985bb02SVaclav Hapla   const PetscScalar *allCoords;
403985bb02SVaclav Hapla   PetscReal         norm;
413985bb02SVaclav Hapla   PetscErrorCode    ierr;
423985bb02SVaclav Hapla 
433985bb02SVaclav Hapla   PetscFunctionBegin;
443985bb02SVaclav Hapla   if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON;
4537900f7dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
463985bb02SVaclav Hapla   ierr = DMGetCoordinatesLocal(dm, &allCoordsVec);CHKERRQ(ierr);
473985bb02SVaclav Hapla   ierr = VecGetArrayRead(allCoordsVec, &allCoords);CHKERRQ(ierr);
483985bb02SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
4976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
50335ef845SVaclav Hapla     /* check coordinate section is consistent with DM dimension */
51335ef845SVaclav Hapla     PetscSection      cs;
52335ef845SVaclav Hapla     PetscInt          ndof;
53335ef845SVaclav Hapla 
54335ef845SVaclav Hapla     ierr = DMGetCoordinateSection(dm, &cs);CHKERRQ(ierr);
553985bb02SVaclav Hapla     for (p = vStart; p < vEnd; p++) {
563985bb02SVaclav Hapla       ierr = PetscSectionGetDof(cs, p, &ndof);CHKERRQ(ierr);
5737900f7dSMatthew G. Knepley       if (PetscUnlikely(ndof != cdim)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %D: ndof = %D != %D = cdim", p, ndof, cdim);
58335ef845SVaclav Hapla     }
59335ef845SVaclav Hapla   }
60eca9f518SVaclav Hapla   if (eps == 0.0) {
6137900f7dSMatthew G. Knepley     for (i=0,j=0; i < npoints; i++,j+=cdim) {
62eca9f518SVaclav Hapla       dagPoints[i] = -1;
6337900f7dSMatthew G. Knepley       for (p = vStart,o=0; p < vEnd; p++,o+=cdim) {
6437900f7dSMatthew G. Knepley         for (c = 0; c < cdim; c++) {
65eca9f518SVaclav Hapla           if (coord[j+c] != PetscRealPart(allCoords[o+c])) break;
66eca9f518SVaclav Hapla         }
6737900f7dSMatthew G. Knepley         if (c == cdim) {
68eca9f518SVaclav Hapla           dagPoints[i] = p;
69eca9f518SVaclav Hapla           break;
70eca9f518SVaclav Hapla         }
71eca9f518SVaclav Hapla       }
72eca9f518SVaclav Hapla     }
73eca9f518SVaclav Hapla     ierr = VecRestoreArrayRead(allCoordsVec, &allCoords);CHKERRQ(ierr);
74eca9f518SVaclav Hapla     PetscFunctionReturn(0);
75eca9f518SVaclav Hapla   }
7637900f7dSMatthew G. Knepley   for (i=0,j=0; i < npoints; i++,j+=cdim) {
77335ef845SVaclav Hapla     dagPoints[i] = -1;
7837900f7dSMatthew G. Knepley     for (p = vStart,o=0; p < vEnd; p++,o+=cdim) {
793985bb02SVaclav Hapla       norm = 0.0;
8037900f7dSMatthew G. Knepley       for (c = 0; c < cdim; c++) {
813985bb02SVaclav Hapla         norm += PetscSqr(coord[j+c] - PetscRealPart(allCoords[o+c]));
823985bb02SVaclav Hapla       }
833985bb02SVaclav Hapla       norm = PetscSqrtReal(norm);
843985bb02SVaclav Hapla       if (norm <= eps) {
853985bb02SVaclav Hapla         dagPoints[i] = p;
863985bb02SVaclav Hapla         break;
873985bb02SVaclav Hapla       }
883985bb02SVaclav Hapla     }
893985bb02SVaclav Hapla   }
903985bb02SVaclav Hapla   ierr = VecRestoreArrayRead(allCoordsVec, &allCoords);CHKERRQ(ierr);
913985bb02SVaclav Hapla   PetscFunctionReturn(0);
923985bb02SVaclav Hapla }
933985bb02SVaclav Hapla 
94fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
95fea14342SMatthew G. Knepley {
96fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*2+0];
97fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*2+1];
98fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*2+0];
99fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*2+1];
100fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0*2+0];
101fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0*2+1];
102fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1*2+0];
103fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1*2+1];
104fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
105fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
106fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
107fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
108fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
109fea14342SMatthew G. Knepley 
110fea14342SMatthew G. Knepley   PetscFunctionBegin;
111fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
112fea14342SMatthew G. Knepley   /* Non-parallel lines */
113fea14342SMatthew G. Knepley   if (denom != 0.0) {
114fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
115fea14342SMatthew G. Knepley     const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
116fea14342SMatthew G. Knepley 
117fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
118fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
119fea14342SMatthew G. Knepley       if (intersection) {
120fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
121fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
122fea14342SMatthew G. Knepley       }
123fea14342SMatthew G. Knepley     }
124fea14342SMatthew G. Knepley   }
125fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
126fea14342SMatthew G. Knepley }
127fea14342SMatthew G. Knepley 
128ddce0771SMatthew G. Knepley /* The plane is segmentB x segmentC: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */
129ddce0771SMatthew G. Knepley static PetscErrorCode DMPlexGetLinePlaneIntersection_3D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], const PetscReal segmentC[], PetscReal intersection[], PetscBool *hasIntersection)
130ddce0771SMatthew G. Knepley {
131ddce0771SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*3+0];
132ddce0771SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*3+1];
133ddce0771SMatthew G. Knepley   const PetscReal p0_z  = segmentA[0*3+2];
134ddce0771SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*3+0];
135ddce0771SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*3+1];
136ddce0771SMatthew G. Knepley   const PetscReal p1_z  = segmentA[1*3+2];
137ddce0771SMatthew G. Knepley   const PetscReal q0_x  = segmentB[0*3+0];
138ddce0771SMatthew G. Knepley   const PetscReal q0_y  = segmentB[0*3+1];
139ddce0771SMatthew G. Knepley   const PetscReal q0_z  = segmentB[0*3+2];
140ddce0771SMatthew G. Knepley   const PetscReal q1_x  = segmentB[1*3+0];
141ddce0771SMatthew G. Knepley   const PetscReal q1_y  = segmentB[1*3+1];
142ddce0771SMatthew G. Knepley   const PetscReal q1_z  = segmentB[1*3+2];
143ddce0771SMatthew G. Knepley   const PetscReal r0_x  = segmentC[0*3+0];
144ddce0771SMatthew G. Knepley   const PetscReal r0_y  = segmentC[0*3+1];
145ddce0771SMatthew G. Knepley   const PetscReal r0_z  = segmentC[0*3+2];
146ddce0771SMatthew G. Knepley   const PetscReal r1_x  = segmentC[1*3+0];
147ddce0771SMatthew G. Knepley   const PetscReal r1_y  = segmentC[1*3+1];
148ddce0771SMatthew G. Knepley   const PetscReal r1_z  = segmentC[1*3+2];
149ddce0771SMatthew G. Knepley   const PetscReal s0_x  = p1_x - p0_x;
150ddce0771SMatthew G. Knepley   const PetscReal s0_y  = p1_y - p0_y;
151ddce0771SMatthew G. Knepley   const PetscReal s0_z  = p1_z - p0_z;
152ddce0771SMatthew G. Knepley   const PetscReal s1_x  = q1_x - q0_x;
153ddce0771SMatthew G. Knepley   const PetscReal s1_y  = q1_y - q0_y;
154ddce0771SMatthew G. Knepley   const PetscReal s1_z  = q1_z - q0_z;
155ddce0771SMatthew G. Knepley   const PetscReal s2_x  = r1_x - r0_x;
156ddce0771SMatthew G. Knepley   const PetscReal s2_y  = r1_y - r0_y;
157ddce0771SMatthew G. Knepley   const PetscReal s2_z  = r1_z - r0_z;
158ddce0771SMatthew G. Knepley   const PetscReal s3_x  = s1_y*s2_z - s1_z*s2_y; /* s1 x s2 */
159ddce0771SMatthew G. Knepley   const PetscReal s3_y  = s1_z*s2_x - s1_x*s2_z;
160ddce0771SMatthew G. Knepley   const PetscReal s3_z  = s1_x*s2_y - s1_y*s2_x;
161ddce0771SMatthew G. Knepley   const PetscReal s4_x  = s0_y*s2_z - s0_z*s2_y; /* s0 x s2 */
162ddce0771SMatthew G. Knepley   const PetscReal s4_y  = s0_z*s2_x - s0_x*s2_z;
163ddce0771SMatthew G. Knepley   const PetscReal s4_z  = s0_x*s2_y - s0_y*s2_x;
164ddce0771SMatthew G. Knepley   const PetscReal s5_x  = s1_y*s0_z - s1_z*s0_y; /* s1 x s0 */
165ddce0771SMatthew G. Knepley   const PetscReal s5_y  = s1_z*s0_x - s1_x*s0_z;
166ddce0771SMatthew G. Knepley   const PetscReal s5_z  = s1_x*s0_y - s1_y*s0_x;
167ddce0771SMatthew G. Knepley   const PetscReal denom = -(s0_x*s3_x + s0_y*s3_y + s0_z*s3_z); /* -s0 . (s1 x s2) */
168ddce0771SMatthew G. Knepley 
169ddce0771SMatthew G. Knepley   PetscFunctionBegin;
170ddce0771SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
171ddce0771SMatthew G. Knepley   /* Line not parallel to plane */
172ddce0771SMatthew G. Knepley   if (denom != 0.0) {
173ddce0771SMatthew G. Knepley     const PetscReal t = (s3_x * (p0_x - q0_x) + s3_y * (p0_y - q0_y) + s3_z * (p0_z - q0_z)) / denom;
174ddce0771SMatthew G. Knepley     const PetscReal u = (s4_x * (p0_x - q0_x) + s4_y * (p0_y - q0_y) + s4_z * (p0_z - q0_z)) / denom;
175ddce0771SMatthew G. Knepley     const PetscReal v = (s5_x * (p0_x - q0_x) + s5_y * (p0_y - q0_y) + s5_z * (p0_z - q0_z)) / denom;
176ddce0771SMatthew G. Knepley 
177ddce0771SMatthew G. Knepley     if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && v >= 0 && v <= 1) {
178ddce0771SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
179ddce0771SMatthew G. Knepley       if (intersection) {
180ddce0771SMatthew G. Knepley         intersection[0] = p0_x + (t * s0_x);
181ddce0771SMatthew G. Knepley         intersection[1] = p0_y + (t * s0_y);
182ddce0771SMatthew G. Knepley         intersection[2] = p0_z + (t * s0_z);
183ddce0771SMatthew G. Knepley       }
184ddce0771SMatthew G. Knepley     }
185ddce0771SMatthew G. Knepley   }
186ddce0771SMatthew G. Knepley   PetscFunctionReturn(0);
187ddce0771SMatthew G. Knepley }
188ddce0771SMatthew G. Knepley 
18914bbb9f0SLawrence Mitchell static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
19014bbb9f0SLawrence Mitchell {
19114bbb9f0SLawrence Mitchell   const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON;
19214bbb9f0SLawrence Mitchell   const PetscReal x   = PetscRealPart(point[0]);
19314bbb9f0SLawrence Mitchell   PetscReal       v0, J, invJ, detJ;
19414bbb9f0SLawrence Mitchell   PetscReal       xi;
19514bbb9f0SLawrence Mitchell   PetscErrorCode  ierr;
19614bbb9f0SLawrence Mitchell 
19714bbb9f0SLawrence Mitchell   PetscFunctionBegin;
19814bbb9f0SLawrence Mitchell   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ);CHKERRQ(ierr);
19914bbb9f0SLawrence Mitchell   xi   = invJ*(x - v0);
20014bbb9f0SLawrence Mitchell 
20114bbb9f0SLawrence Mitchell   if ((xi >= -eps) && (xi <= 2.+eps)) *cell = c;
20214bbb9f0SLawrence Mitchell   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
20314bbb9f0SLawrence Mitchell   PetscFunctionReturn(0);
20414bbb9f0SLawrence Mitchell }
20514bbb9f0SLawrence Mitchell 
206ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
207ccd2543fSMatthew G Knepley {
208ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
209f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
210ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
211ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
212ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
213ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
214ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
215ccd2543fSMatthew G Knepley 
216ccd2543fSMatthew G Knepley   PetscFunctionBegin;
2178e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
218ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
219ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
220ccd2543fSMatthew G Knepley 
221f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
222c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
223ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
224ccd2543fSMatthew G Knepley }
225ccd2543fSMatthew G Knepley 
22662a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
22762a38674SMatthew G. Knepley {
22862a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
22962a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
23062a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
23162a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
23262a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
23362a38674SMatthew G. Knepley   PetscErrorCode  ierr;
23462a38674SMatthew G. Knepley 
23562a38674SMatthew G. Knepley   PetscFunctionBegin;
23662a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
23762a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
23862a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
23962a38674SMatthew G. Knepley 
24062a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
24162a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
24262a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
24362a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
24462a38674SMatthew G. Knepley     xi  /= r;
24562a38674SMatthew G. Knepley     eta /= r;
24662a38674SMatthew G. Knepley   }
24762a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
24862a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
24962a38674SMatthew G. Knepley   PetscFunctionReturn(0);
25062a38674SMatthew G. Knepley }
25162a38674SMatthew G. Knepley 
252ba2698f1SMatthew G. Knepley static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
253ccd2543fSMatthew G Knepley {
254ccd2543fSMatthew G Knepley   PetscSection       coordSection;
255ccd2543fSMatthew G Knepley   Vec             coordsLocal;
256a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
257ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
258ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
259ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
260ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
261ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
262ccd2543fSMatthew G Knepley 
263ccd2543fSMatthew G Knepley   PetscFunctionBegin;
264ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
26569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
266ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
267ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
268ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
269ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
270ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
271ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
272ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
273ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
274ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
275ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
276ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
277ccd2543fSMatthew G Knepley   }
278ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
279c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
280ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
281ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
282ccd2543fSMatthew G Knepley }
283ccd2543fSMatthew G Knepley 
284ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
285ccd2543fSMatthew G Knepley {
286ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 3;
28737900f7dSMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
288ccd2543fSMatthew G Knepley   PetscReal       v0[3], J[9], invJ[9], detJ;
289ccd2543fSMatthew G Knepley   PetscReal       x = PetscRealPart(point[0]);
290ccd2543fSMatthew G Knepley   PetscReal       y = PetscRealPart(point[1]);
291ccd2543fSMatthew G Knepley   PetscReal       z = PetscRealPart(point[2]);
292ccd2543fSMatthew G Knepley   PetscReal       xi, eta, zeta;
293ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
294ccd2543fSMatthew G Knepley 
295ccd2543fSMatthew G Knepley   PetscFunctionBegin;
2968e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
297ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
298ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
299ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
300ccd2543fSMatthew G Knepley 
30137900f7dSMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0+eps)) *cell = c;
302c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
303ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
304ccd2543fSMatthew G Knepley }
305ccd2543fSMatthew G Knepley 
306ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
307ccd2543fSMatthew G Knepley {
308ccd2543fSMatthew G Knepley   PetscSection   coordSection;
309ccd2543fSMatthew G Knepley   Vec            coordsLocal;
310872a9804SMatthew G. Knepley   PetscScalar   *coords = NULL;
311fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
312fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
313ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
314ccd2543fSMatthew G Knepley   PetscInt       f;
315ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
316ccd2543fSMatthew G Knepley 
317ccd2543fSMatthew G Knepley   PetscFunctionBegin;
318ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
31969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
320ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
321ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
322ccd2543fSMatthew G Knepley     /* Check the point is under plane */
323ccd2543fSMatthew G Knepley     /*   Get face normal */
324ccd2543fSMatthew G Knepley     PetscReal v_i[3];
325ccd2543fSMatthew G Knepley     PetscReal v_j[3];
326ccd2543fSMatthew G Knepley     PetscReal normal[3];
327ccd2543fSMatthew G Knepley     PetscReal pp[3];
328ccd2543fSMatthew G Knepley     PetscReal dot;
329ccd2543fSMatthew G Knepley 
330ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
331ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
332ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
333ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
334ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
335ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
336ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
337ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
338ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
339ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
340ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
341ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
342ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
343ccd2543fSMatthew G Knepley 
344ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
345ccd2543fSMatthew G Knepley     if (dot < 0.0) {
346ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
347ccd2543fSMatthew G Knepley       break;
348ccd2543fSMatthew G Knepley     }
349ccd2543fSMatthew G Knepley   }
350ccd2543fSMatthew G Knepley   if (found) *cell = c;
351c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
352ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
353ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
354ccd2543fSMatthew G Knepley }
355ccd2543fSMatthew G Knepley 
356c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
357c4eade1cSMatthew G. Knepley {
358c4eade1cSMatthew G. Knepley   PetscInt d;
359c4eade1cSMatthew G. Knepley 
360c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
361c4eade1cSMatthew G. Knepley   box->dim = dim;
362c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
363c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
364c4eade1cSMatthew G. Knepley }
365c4eade1cSMatthew G. Knepley 
366c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
367c4eade1cSMatthew G. Knepley {
368c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
369c4eade1cSMatthew G. Knepley 
370c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
371c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
372c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
373c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
374c4eade1cSMatthew G. Knepley }
375c4eade1cSMatthew G. Knepley 
376c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
377c4eade1cSMatthew G. Knepley {
378c4eade1cSMatthew G. Knepley   PetscInt d;
379c4eade1cSMatthew G. Knepley 
380c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
381c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
382c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
383c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
384c4eade1cSMatthew G. Knepley   }
385c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
386c4eade1cSMatthew G. Knepley }
387c4eade1cSMatthew G. Knepley 
38862a38674SMatthew G. Knepley /*
38962a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
39062a38674SMatthew G. Knepley 
39162a38674SMatthew G. Knepley   Not collective
39262a38674SMatthew G. Knepley 
39362a38674SMatthew G. Knepley   Input Parameters:
39462a38674SMatthew G. Knepley + box - The grid hash object
39562a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
39662a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
39762a38674SMatthew G. Knepley 
39862a38674SMatthew G. Knepley   Level: developer
39962a38674SMatthew G. Knepley 
40062a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
40162a38674SMatthew G. Knepley */
402c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
403c4eade1cSMatthew G. Knepley {
404c4eade1cSMatthew G. Knepley   PetscInt d;
405c4eade1cSMatthew G. Knepley 
406c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
407c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
408c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
409c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
410c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
411c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
412c4eade1cSMatthew G. Knepley     } else {
413c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
414c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
415c4eade1cSMatthew G. Knepley     }
416c4eade1cSMatthew G. Knepley   }
417c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
418c4eade1cSMatthew G. Knepley }
419c4eade1cSMatthew G. Knepley 
42062a38674SMatthew G. Knepley /*
42162a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
42262a38674SMatthew G. Knepley 
42362a38674SMatthew G. Knepley   Not collective
42462a38674SMatthew G. Knepley 
42562a38674SMatthew G. Knepley   Input Parameters:
42662a38674SMatthew G. Knepley + box       - The grid hash object
42762a38674SMatthew G. Knepley . numPoints - The number of input points
42862a38674SMatthew G. Knepley - points    - The input point coordinates
42962a38674SMatthew G. Knepley 
43062a38674SMatthew G. Knepley   Output Parameters:
43162a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
43262a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
43362a38674SMatthew G. Knepley 
43462a38674SMatthew G. Knepley   Level: developer
43562a38674SMatthew G. Knepley 
43662a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
43762a38674SMatthew G. Knepley */
4381c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
439c4eade1cSMatthew G. Knepley {
440c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
441c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
442c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
443c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
444c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
445c4eade1cSMatthew G. Knepley   PetscInt         d, p;
446c4eade1cSMatthew G. Knepley 
447c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
448c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
449c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4501c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
451c4eade1cSMatthew G. Knepley 
4521c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
4532a705cacSMatthew G. Knepley       if (dbox == -1   && PetscAbsReal(PetscRealPart(points[p*dim+d]) - lower[d]) < 1.0e-9) dbox = 0;
454c4eade1cSMatthew G. Knepley       if (dbox < 0 || dbox >= n[d]) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %d (%g, %g, %g) is outside of our bounding box",
455087ef6b2SMatthew 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);
456c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
457c4eade1cSMatthew G. Knepley     }
458ddce0771SMatthew 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];
459c4eade1cSMatthew G. Knepley   }
460c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
461c4eade1cSMatthew G. Knepley }
462c4eade1cSMatthew G. Knepley 
463af74b616SDave May /*
464af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
465af74b616SDave May 
466af74b616SDave May  Not collective
467af74b616SDave May 
468af74b616SDave May   Input Parameters:
469af74b616SDave May + box       - The grid hash object
470af74b616SDave May . numPoints - The number of input points
471af74b616SDave May - points    - The input point coordinates
472af74b616SDave May 
473af74b616SDave May   Output Parameters:
474af74b616SDave May + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
475af74b616SDave May . boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
476af74b616SDave May - found     - Flag indicating if point was located within a box
477af74b616SDave May 
478af74b616SDave May   Level: developer
479af74b616SDave May 
480af74b616SDave May .seealso: PetscGridHashGetEnclosingBox()
481af74b616SDave May */
482af74b616SDave May PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[],PetscBool *found)
483af74b616SDave May {
484af74b616SDave May   const PetscReal *lower = box->lower;
485af74b616SDave May   const PetscReal *upper = box->upper;
486af74b616SDave May   const PetscReal *h     = box->h;
487af74b616SDave May   const PetscInt  *n     = box->n;
488af74b616SDave May   const PetscInt   dim   = box->dim;
489af74b616SDave May   PetscInt         d, p;
490af74b616SDave May 
491af74b616SDave May   PetscFunctionBegin;
492af74b616SDave May   *found = PETSC_FALSE;
493af74b616SDave May   for (p = 0; p < numPoints; ++p) {
494af74b616SDave May     for (d = 0; d < dim; ++d) {
495af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
496af74b616SDave May 
497af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
498af74b616SDave May       if (dbox < 0 || dbox >= n[d]) {
499af74b616SDave May         PetscFunctionReturn(0);
500af74b616SDave May       }
501af74b616SDave May       dboxes[p*dim+d] = dbox;
502af74b616SDave May     }
503ddce0771SMatthew 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];
504af74b616SDave May   }
505af74b616SDave May   *found = PETSC_TRUE;
506af74b616SDave May   PetscFunctionReturn(0);
507af74b616SDave May }
508af74b616SDave May 
509c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
510c4eade1cSMatthew G. Knepley {
511c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
512c4eade1cSMatthew G. Knepley 
513c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
514c4eade1cSMatthew G. Knepley   if (*box) {
515c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
516c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
517c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
518c4eade1cSMatthew G. Knepley   }
519c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
520c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
521c4eade1cSMatthew G. Knepley }
522c4eade1cSMatthew G. Knepley 
523cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
524cafe43deSMatthew G. Knepley {
525ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
526cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
527cafe43deSMatthew G. Knepley 
528cafe43deSMatthew G. Knepley   PetscFunctionBegin;
529ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cellStart, &ct);CHKERRQ(ierr);
530ba2698f1SMatthew G. Knepley   switch (ct) {
53114bbb9f0SLawrence Mitchell     case DM_POLYTOPE_SEGMENT:
53214bbb9f0SLawrence Mitchell     ierr = DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
533ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
534ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
535ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
536ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
537ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
538ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
539ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
540ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
541ba2698f1SMatthew G. Knepley     default: SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %D with type %s", cellStart, DMPolytopeTypes[ct]);
542cafe43deSMatthew G. Knepley   }
543cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
544cafe43deSMatthew G. Knepley }
545cafe43deSMatthew G. Knepley 
54662a38674SMatthew G. Knepley /*
54762a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
54862a38674SMatthew G. Knepley */
54962a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
55062a38674SMatthew G. Knepley {
551ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
55262a38674SMatthew G. Knepley   PetscErrorCode ierr;
55362a38674SMatthew G. Knepley 
55462a38674SMatthew G. Knepley   PetscFunctionBegin;
555ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
556ba2698f1SMatthew G. Knepley   switch (ct) {
557ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
558ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
55962a38674SMatthew G. Knepley #if 0
560ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
561ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
562ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
563ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
564ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
565ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
56662a38674SMatthew G. Knepley #endif
567ba2698f1SMatthew G. Knepley     default: SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %D with type %s", cell, DMPolytopeTypes[ct]);
56862a38674SMatthew G. Knepley   }
56962a38674SMatthew G. Knepley   PetscFunctionReturn(0);
57062a38674SMatthew G. Knepley }
57162a38674SMatthew G. Knepley 
57262a38674SMatthew G. Knepley /*
57362a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
57462a38674SMatthew G. Knepley 
575d083f849SBarry Smith   Collective on dm
57662a38674SMatthew G. Knepley 
57762a38674SMatthew G. Knepley   Input Parameter:
57862a38674SMatthew G. Knepley . dm - The Plex
57962a38674SMatthew G. Knepley 
58062a38674SMatthew G. Knepley   Output Parameter:
58162a38674SMatthew G. Knepley . localBox - The grid hash object
58262a38674SMatthew G. Knepley 
58362a38674SMatthew G. Knepley   Level: developer
58462a38674SMatthew G. Knepley 
58562a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
58662a38674SMatthew G. Knepley */
587cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
588cafe43deSMatthew G. Knepley {
589ddce0771SMatthew G. Knepley   const PetscInt     debug = 0;
590cafe43deSMatthew G. Knepley   MPI_Comm           comm;
591cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
592cafe43deSMatthew G. Knepley   Vec                coordinates;
593cafe43deSMatthew G. Knepley   PetscSection       coordSection;
594cafe43deSMatthew G. Knepley   Vec                coordsLocal;
595cafe43deSMatthew G. Knepley   const PetscScalar *coords;
596ddce0771SMatthew G. Knepley   PetscScalar       *edgeCoords;
597722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
598ddce0771SMatthew G. Knepley   PetscInt           n[3] = {2, 2, 2};
599ddce0771SMatthew G. Knepley   PetscInt           dim, N, maxConeSize, cStart, cEnd, c, eStart, eEnd, i;
600ddce0771SMatthew G. Knepley   PetscBool          flg;
601cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
602cafe43deSMatthew G. Knepley 
603cafe43deSMatthew G. Knepley   PetscFunctionBegin;
604cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
605cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
606cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
607ddce0771SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
608ddce0771SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
609cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
610cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
611cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
612cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
613cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
614ddce0771SMatthew G. Knepley   c    = dim;
615ddce0771SMatthew G. Knepley   ierr = PetscOptionsGetIntArray(NULL, ((PetscObject) dm)->prefix, "-dm_plex_hash_box_faces", n, &c, &flg);CHKERRQ(ierr);
616ddce0771SMatthew G. Knepley   if (flg) {for (i = c; i < dim; ++i) n[i] = n[c-1];}
617ddce0771SMatthew G. Knepley   else     {for (i = 0; i < dim; ++i) n[i] = PetscMax(2, PetscFloorReal(PetscPowReal((PetscReal) (cEnd - cStart), 1.0/dim) * 0.8));}
618cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
619cafe43deSMatthew G. Knepley #if 0
620cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
621820f2d46SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRMPI(ierr);
622820f2d46SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRMPI(ierr);
623cafe43deSMatthew G. Knepley #endif
624cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
625cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
626cafe43deSMatthew G. Knepley   /* Create label */
627ddce0771SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
628b26b5bf9SMatthew G. Knepley   if (dim < 2) eStart = eEnd = -1;
629d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse);CHKERRQ(ierr);
630cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
631a8d69d7bSBarry Smith   /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
632cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
633cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
634ddce0771SMatthew G. Knepley   ierr = PetscCalloc3(16 * dim, &dboxes, 16, &boxes, PetscPowInt(maxConeSize, dim) * dim, &edgeCoords);CHKERRQ(ierr);
635cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
636cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
637cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
63838353de4SMatthew G. Knepley     PetscInt         csize   = 0;
639ddce0771SMatthew G. Knepley     PetscInt        *closure = NULL;
640ddce0771SMatthew G. Knepley     PetscInt         Ncl, cl, Ne = 0;
641cafe43deSMatthew G. Knepley     PetscScalar      point[3];
642cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
643cafe43deSMatthew G. Knepley 
644ddce0771SMatthew G. Knepley     /* Get all edges in cell */
645ddce0771SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure);CHKERRQ(ierr);
646ddce0771SMatthew G. Knepley     for (cl = 0; cl < Ncl*2; ++cl) {
647ddce0771SMatthew G. Knepley       if ((closure[cl] >= eStart) && (closure[cl] < eEnd)) {
648ddce0771SMatthew G. Knepley         PetscScalar *ecoords = &edgeCoords[Ne*dim*2];
649ddce0771SMatthew G. Knepley         PetscInt     ecsize  = dim*2;
650ddce0771SMatthew G. Knepley 
651e032ffc8SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, closure[cl], &ecsize, &ecoords);CHKERRQ(ierr);
652ddce0771SMatthew G. Knepley         if (ecsize != dim*2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Got %D coords for edge, instead of %D", ecsize, dim*2);
653ddce0771SMatthew G. Knepley         ++Ne;
654ddce0771SMatthew G. Knepley       }
655ddce0771SMatthew G. Knepley     }
656ddce0771SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &Ncl, &closure);CHKERRQ(ierr);
657cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
65838353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
65938353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
660722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
661ddce0771SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {
662ddce0771SMatthew 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);}
663ddce0771SMatthew G. Knepley       ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);
664ddce0771SMatthew G. Knepley     }
665cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
666cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
6672291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
668cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
669cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
670cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
671cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
672cafe43deSMatthew G. Knepley       }
673cafe43deSMatthew G. Knepley     }
674fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
675cafe43deSMatthew 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]) {
676cafe43deSMatthew 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]) {
677cafe43deSMatthew 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]) {
678cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
679cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
680fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
681cafe43deSMatthew G. Knepley 
682ddce0771SMatthew 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);}
683ddce0771SMatthew G. Knepley           /* Check whether cell contains any vertex of this subbox TODO vectorize this */
684cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
685cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
686cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
687cafe43deSMatthew G. Knepley 
688cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
6890b6bfacdSStefano Zampini                 if (cell >= 0) {
690ddce0771SMatthew 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);}
6910b6bfacdSStefano Zampini                   ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr);
6920b6bfacdSStefano Zampini                   jj = kk = 2;
6930b6bfacdSStefano Zampini                   break;
6940b6bfacdSStefano Zampini                 }
695cafe43deSMatthew G. Knepley               }
696cafe43deSMatthew G. Knepley             }
697cafe43deSMatthew G. Knepley           }
698ddce0771SMatthew G. Knepley           /* Check whether cell edge intersects any face of these subboxes TODO vectorize this */
699ddce0771SMatthew G. Knepley           for (edge = 0; edge < Ne; ++edge) {
700a5cae605SSatish Balay             PetscReal segA[6] = {0.,0.,0.,0.,0.,0.};
701a5cae605SSatish Balay             PetscReal segB[6] = {0.,0.,0.,0.,0.,0.};
702a5cae605SSatish Balay             PetscReal segC[6] = {0.,0.,0.,0.,0.,0.};
703fea14342SMatthew G. Knepley 
704aab5bcd8SJed Brown             if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected dim %d > 3",dim);
705ddce0771SMatthew G. Knepley             for (d = 0; d < dim*2; ++d) segA[d] = PetscRealPart(edgeCoords[edge*dim*2+d]);
706ddce0771SMatthew G. Knepley             /* 1D: (x) -- (x+h)               0 -- 1
707ddce0771SMatthew G. Knepley                2D: (x,   y)   -- (x,   y+h)   (0, 0) -- (0, 1)
708ddce0771SMatthew G. Knepley                    (x+h, y)   -- (x+h, y+h)   (1, 0) -- (1, 1)
709ddce0771SMatthew G. Knepley                    (x,   y)   -- (x+h, y)     (0, 0) -- (1, 0)
710ddce0771SMatthew G. Knepley                    (x,   y+h) -- (x+h, y+h)   (0, 1) -- (1, 1)
711ddce0771SMatthew 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)
712ddce0771SMatthew 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)
713ddce0771SMatthew 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)
714ddce0771SMatthew 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)
715ddce0771SMatthew 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)
716ddce0771SMatthew 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)
717ddce0771SMatthew G. Knepley              */
718ddce0771SMatthew G. Knepley             /* Loop over faces with normal in direction d */
719ddce0771SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
720ddce0771SMatthew G. Knepley               PetscBool intersects = PETSC_FALSE;
721ddce0771SMatthew G. Knepley               PetscInt  e = (d+1)%dim;
722ddce0771SMatthew G. Knepley               PetscInt  f = (d+2)%dim;
723ddce0771SMatthew G. Knepley 
724ddce0771SMatthew G. Knepley               /* There are two faces in each dimension */
725ddce0771SMatthew G. Knepley               for (ii = 0; ii < 2; ++ii) {
726ddce0771SMatthew G. Knepley                 segB[d]     = PetscRealPart(point[d] + ii*h[d]);
727ddce0771SMatthew G. Knepley                 segB[dim+d] = PetscRealPart(point[d] + ii*h[d]);
728ddce0771SMatthew G. Knepley                 segC[d]     = PetscRealPart(point[d] + ii*h[d]);
729ddce0771SMatthew G. Knepley                 segC[dim+d] = PetscRealPart(point[d] + ii*h[d]);
730ddce0771SMatthew G. Knepley                 if (dim > 1) {
731ddce0771SMatthew G. Knepley                   segB[e]     = PetscRealPart(point[e] + 0*h[e]);
732ddce0771SMatthew G. Knepley                   segB[dim+e] = PetscRealPart(point[e] + 1*h[e]);
733ddce0771SMatthew G. Knepley                   segC[e]     = PetscRealPart(point[e] + 0*h[e]);
734ddce0771SMatthew G. Knepley                   segC[dim+e] = PetscRealPart(point[e] + 0*h[e]);
735ddce0771SMatthew G. Knepley                 }
736ddce0771SMatthew G. Knepley                 if (dim > 2) {
737ddce0771SMatthew G. Knepley                   segB[f]     = PetscRealPart(point[f] + 0*h[f]);
738ddce0771SMatthew G. Knepley                   segB[dim+f] = PetscRealPart(point[f] + 0*h[f]);
739ddce0771SMatthew G. Knepley                   segC[f]     = PetscRealPart(point[f] + 0*h[f]);
740ddce0771SMatthew G. Knepley                   segC[dim+f] = PetscRealPart(point[f] + 1*h[f]);
741ddce0771SMatthew G. Knepley                 }
742ddce0771SMatthew G. Knepley                 if (dim == 2) {
743ddce0771SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
744ddce0771SMatthew G. Knepley                 } else if (dim == 3) {
745ddce0771SMatthew G. Knepley                   ierr = DMPlexGetLinePlaneIntersection_3D_Internal(segA, segB, segC, NULL, &intersects);CHKERRQ(ierr);
746ddce0771SMatthew G. Knepley                 }
747ddce0771SMatthew G. Knepley                 if (intersects) {
748ddce0771SMatthew 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);}
749ddce0771SMatthew G. Knepley                   ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = Ne; break;
750ddce0771SMatthew G. Knepley                 }
751ddce0771SMatthew G. Knepley               }
752ddce0771SMatthew G. Knepley             }
753cafe43deSMatthew G. Knepley           }
754fea14342SMatthew G. Knepley         }
755fea14342SMatthew G. Knepley       }
756fea14342SMatthew G. Knepley     }
757fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
758fea14342SMatthew G. Knepley   }
759ddce0771SMatthew G. Knepley   ierr = PetscFree3(dboxes, boxes, edgeCoords);CHKERRQ(ierr);
760ddce0771SMatthew G. Knepley   if (debug) {ierr = DMLabelView(lbox->cellsSparse, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);}
761cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
762cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
763cafe43deSMatthew G. Knepley   *localBox = lbox;
764cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
765cafe43deSMatthew G. Knepley }
766cafe43deSMatthew G. Knepley 
76762a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
768ccd2543fSMatthew G Knepley {
769ddce0771SMatthew G. Knepley   const PetscInt  debug = 0;
770cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
771af74b616SDave May   PetscBool       hash = mesh->useHashLocation, reuse = PETSC_FALSE;
7723a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
773412e9a14SMatthew G. Knepley   PetscInt        dim, cStart, cEnd, numCells, c, d;
774cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
7753a93e3b7SToby Isaac   PetscSFNode    *cells;
776ccd2543fSMatthew G Knepley   PetscScalar    *a;
7773a93e3b7SToby Isaac   PetscMPIInt     result;
778af74b616SDave May   PetscLogDouble  t0,t1;
7799cb35068SDave May   PetscReal       gmin[3],gmax[3];
7809cb35068SDave May   PetscInt        terminating_query_type[] = { 0, 0, 0 };
781ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
782ccd2543fSMatthew G Knepley 
783ccd2543fSMatthew G Knepley   PetscFunctionBegin;
784cadf77a0SMark Adams   ierr = PetscLogEventBegin(DMPLEX_LocatePoints,0,0,0,0);CHKERRQ(ierr);
785af74b616SDave May   ierr = PetscTime(&t0);CHKERRQ(ierr);
786080342d1SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && !hash) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Nearest point location only supported with grid hashing. Use -dm_plex_hash_location to enable it.");
787cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
788cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
789ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRMPI(ierr);
7903a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
791cafe43deSMatthew G. Knepley   if (bs != dim) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Block size for point vector %D must be the mesh coordinate dimension %D", bs, dim);
792412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
793ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
794ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
795ccd2543fSMatthew G Knepley   numPoints /= bs;
796af74b616SDave May   {
797af74b616SDave May     const PetscSFNode *sf_cells;
798af74b616SDave May 
799af74b616SDave May     ierr = PetscSFGetGraph(cellSF,NULL,NULL,NULL,&sf_cells);CHKERRQ(ierr);
800af74b616SDave May     if (sf_cells) {
801af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Re-using existing StarForest node list\n");CHKERRQ(ierr);
802af74b616SDave May       cells = (PetscSFNode*)sf_cells;
803af74b616SDave May       reuse = PETSC_TRUE;
804af74b616SDave May     } else {
805af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n");CHKERRQ(ierr);
806785e854fSJed Brown       ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
807af74b616SDave May       /* initialize cells if created */
808af74b616SDave May       for (p=0; p<numPoints; p++) {
809af74b616SDave May         cells[p].rank  = 0;
810af74b616SDave May         cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
811af74b616SDave May       }
812af74b616SDave May     }
813af74b616SDave May   }
8149cb35068SDave May   /* define domain bounding box */
8159cb35068SDave May   {
8169cb35068SDave May     Vec coorglobal;
8179cb35068SDave May 
8189cb35068SDave May     ierr = DMGetCoordinates(dm,&coorglobal);CHKERRQ(ierr);
8199cb35068SDave May     ierr = VecStrideMaxAll(coorglobal,NULL,gmax);CHKERRQ(ierr);
8209cb35068SDave May     ierr = VecStrideMinAll(coorglobal,NULL,gmin);CHKERRQ(ierr);
8219cb35068SDave May   }
822953fc75cSMatthew G. Knepley   if (hash) {
823ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
824cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
825cafe43deSMatthew G. Knepley     /* Send points to correct process */
826cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
827cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
828cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
829953fc75cSMatthew G. Knepley   }
8303a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
831ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
832e56f9228SJed Brown     PetscInt           dbin[3] = {-1,-1,-1}, bin, cell = -1, cellOffset;
8339cb35068SDave May     PetscBool          point_outside_domain = PETSC_FALSE;
834ccd2543fSMatthew G Knepley 
8359cb35068SDave May     /* check bounding box of domain */
8369cb35068SDave May     for (d=0; d<dim; d++) {
837a5f152d1SDave May       if (PetscRealPart(point[d]) < gmin[d]) { point_outside_domain = PETSC_TRUE; break; }
838a5f152d1SDave May       if (PetscRealPart(point[d]) > gmax[d]) { point_outside_domain = PETSC_TRUE; break; }
8399cb35068SDave May     }
8409cb35068SDave May     if (point_outside_domain) {
841e9b685f5SMatthew G. Knepley       cells[p].rank = 0;
842e9b685f5SMatthew G. Knepley       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
8439cb35068SDave May       terminating_query_type[0]++;
8449cb35068SDave May       continue;
8459cb35068SDave May     }
846ccd2543fSMatthew G Knepley 
847af74b616SDave May     /* check initial values in cells[].index - abort early if found */
848af74b616SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
849af74b616SDave May       c = cells[p].index;
8503a93e3b7SToby Isaac       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
851af74b616SDave May       ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
852af74b616SDave May       if (cell >= 0) {
853af74b616SDave May         cells[p].rank = 0;
854af74b616SDave May         cells[p].index = cell;
855af74b616SDave May         numFound++;
856af74b616SDave May       }
857af74b616SDave May     }
8589cb35068SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
8599cb35068SDave May       terminating_query_type[1]++;
8609cb35068SDave May       continue;
8619cb35068SDave May     }
862af74b616SDave May 
863953fc75cSMatthew G. Knepley     if (hash) {
864af74b616SDave May       PetscBool found_box;
865af74b616SDave May 
866ddce0771SMatthew 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);}
867af74b616SDave May       /* allow for case that point is outside box - abort early */
868af74b616SDave May       ierr = PetscGridHashGetEnclosingBoxQuery(mesh->lbox, 1, point, dbin, &bin,&found_box);CHKERRQ(ierr);
869af74b616SDave May       if (found_box) {
870ddce0771SMatthew 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);}
871cafe43deSMatthew G. Knepley         /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
872cafe43deSMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
873cafe43deSMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
874cafe43deSMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
875ddce0771SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    Checking for point in cell %D\n", boxCells[c]);CHKERRQ(ierr);}
876cafe43deSMatthew G. Knepley           ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
8773a93e3b7SToby Isaac           if (cell >= 0) {
878ddce0771SMatthew G. Knepley             if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "      FOUND in cell %D\n", cell);CHKERRQ(ierr);}
8793a93e3b7SToby Isaac             cells[p].rank = 0;
8803a93e3b7SToby Isaac             cells[p].index = cell;
8813a93e3b7SToby Isaac             numFound++;
8829cb35068SDave May             terminating_query_type[2]++;
8833a93e3b7SToby Isaac             break;
884ccd2543fSMatthew G Knepley           }
8853a93e3b7SToby Isaac         }
886af74b616SDave May       }
887953fc75cSMatthew G. Knepley     } else {
888953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
889953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
8903a93e3b7SToby Isaac         if (cell >= 0) {
8913a93e3b7SToby Isaac           cells[p].rank = 0;
8923a93e3b7SToby Isaac           cells[p].index = cell;
8933a93e3b7SToby Isaac           numFound++;
8949cb35068SDave May           terminating_query_type[2]++;
8953a93e3b7SToby Isaac           break;
896953fc75cSMatthew G. Knepley         }
897953fc75cSMatthew G. Knepley       }
8983a93e3b7SToby Isaac     }
899ccd2543fSMatthew G Knepley   }
900953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
90162a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
90262a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
90362a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
90462a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
905e56f9228SJed Brown       PetscInt           dbin[3] = {-1,-1,-1}, bin, cellOffset, d;
90662a38674SMatthew G. Knepley 
907e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
90862a38674SMatthew G. Knepley         ++numFound;
90962a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
91062a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
91162a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
91262a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
91362a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
914b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
91562a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
91662a38674SMatthew G. Knepley           if (dist < distMax) {
91762a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
91862a38674SMatthew G. Knepley             cells[p].rank  = 0;
91962a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
92062a38674SMatthew G. Knepley             distMax = dist;
92162a38674SMatthew G. Knepley             break;
92262a38674SMatthew G. Knepley           }
92362a38674SMatthew G. Knepley         }
92462a38674SMatthew G. Knepley       }
92562a38674SMatthew G. Knepley     }
92662a38674SMatthew G. Knepley   }
92762a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
928cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
9292d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
9303a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
9313a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
9323a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
9333a93e3b7SToby Isaac         if (numFound < p) {
9343a93e3b7SToby Isaac           cells[numFound] = cells[p];
9353a93e3b7SToby Isaac         }
9363a93e3b7SToby Isaac         found[numFound++] = p;
9373a93e3b7SToby Isaac       }
9383a93e3b7SToby Isaac     }
9393a93e3b7SToby Isaac   }
94062a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
941af74b616SDave May   if (!reuse) {
9423a93e3b7SToby Isaac     ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
943af74b616SDave May   }
944af74b616SDave May   ierr = PetscTime(&t1);CHKERRQ(ierr);
9459cb35068SDave May   if (hash) {
9462d4ee042Sprj-     ierr = PetscInfo3(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);
9479cb35068SDave May   } else {
9482d4ee042Sprj-     ierr = PetscInfo3(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);
9499cb35068SDave May   }
950af74b616SDave May   ierr = PetscInfo3(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);
951cadf77a0SMark Adams   ierr = PetscLogEventEnd(DMPLEX_LocatePoints,0,0,0,0);CHKERRQ(ierr);
952ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
953ccd2543fSMatthew G Knepley }
954ccd2543fSMatthew G Knepley 
955741bfc07SMatthew G. Knepley /*@C
956741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
957741bfc07SMatthew G. Knepley 
958741bfc07SMatthew G. Knepley   Not collective
959741bfc07SMatthew G. Knepley 
9606b867d5aSJose E. Roman   Input/Output Parameter:
9616b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x
962741bfc07SMatthew G. Knepley 
9636b867d5aSJose E. Roman   Output Parameter:
9646b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
965741bfc07SMatthew G. Knepley 
966741bfc07SMatthew G. Knepley   Level: developer
967741bfc07SMatthew G. Knepley 
968741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
969741bfc07SMatthew G. Knepley @*/
970741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
97117fe8556SMatthew G. Knepley {
97217fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
97317fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
9748b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
97517fe8556SMatthew G. Knepley 
97617fe8556SMatthew G. Knepley   PetscFunctionBegin;
9771c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
9781c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
97917fe8556SMatthew G. Knepley   coords[0] = 0.0;
9807f07f362SMatthew G. Knepley   coords[1] = r;
98117fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
98217fe8556SMatthew G. Knepley }
98317fe8556SMatthew G. Knepley 
984741bfc07SMatthew G. Knepley /*@C
985741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
98628dbe442SToby Isaac 
987741bfc07SMatthew G. Knepley   Not collective
98828dbe442SToby Isaac 
9896b867d5aSJose E. Roman   Input/Output Parameter:
9906b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z
991741bfc07SMatthew G. Knepley 
9926b867d5aSJose E. Roman   Output Parameter:
9936b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
994741bfc07SMatthew G. Knepley 
995741bfc07SMatthew 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
996741bfc07SMatthew G. Knepley 
997741bfc07SMatthew G. Knepley   Level: developer
998741bfc07SMatthew G. Knepley 
999741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
1000741bfc07SMatthew G. Knepley @*/
1001741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
100228dbe442SToby Isaac {
100328dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
100428dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
100528dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
100628dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
100728dbe442SToby Isaac   PetscReal      rinv = 1. / r;
100828dbe442SToby Isaac   PetscFunctionBegin;
100928dbe442SToby Isaac 
101028dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
101128dbe442SToby Isaac   if (x > 0.) {
101228dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
101328dbe442SToby Isaac 
101428dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
101528dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
101628dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
101728dbe442SToby Isaac   }
101828dbe442SToby Isaac   else {
101928dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
102028dbe442SToby Isaac 
102128dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
102228dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
102328dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
102428dbe442SToby Isaac   }
102528dbe442SToby Isaac   coords[0] = 0.0;
102628dbe442SToby Isaac   coords[1] = r;
102728dbe442SToby Isaac   PetscFunctionReturn(0);
102828dbe442SToby Isaac }
102928dbe442SToby Isaac 
1030741bfc07SMatthew G. Knepley /*@
1031c871b86eSJed Brown   DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the
1032c871b86eSJed Brown     plane.  The normal is defined by positive orientation of the first 3 points.
1033741bfc07SMatthew G. Knepley 
1034741bfc07SMatthew G. Knepley   Not collective
1035741bfc07SMatthew G. Knepley 
1036741bfc07SMatthew G. Knepley   Input Parameter:
10376b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points)
1038741bfc07SMatthew G. Knepley 
10396b867d5aSJose E. Roman   Input/Output Parameter:
10406b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first
10416b867d5aSJose E. Roman            2*coordSize/3 entries contain interlaced 2D points, with the rest undefined
10426b867d5aSJose E. Roman 
10436b867d5aSJose E. Roman   Output Parameter:
10446b867d5aSJose 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.
1045741bfc07SMatthew G. Knepley 
1046741bfc07SMatthew G. Knepley   Level: developer
1047741bfc07SMatthew G. Knepley 
1048741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
1049741bfc07SMatthew G. Knepley @*/
1050741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
1051ccd2543fSMatthew G Knepley {
1052c871b86eSJed Brown   PetscReal x1[3], x2[3], n[3], c[3], norm;
1053ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1054c871b86eSJed Brown   PetscInt       d, p;
1055ccd2543fSMatthew G Knepley 
1056ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1057ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
1058ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
10591ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
10601ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
1061ccd2543fSMatthew G Knepley   }
1062c871b86eSJed Brown   // n = x1 \otimes x2
1063ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
1064ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
1065ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
10668b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
1067c871b86eSJed Brown   for (d = 0; d < dim; d++) n[d] /= norm;
1068c871b86eSJed Brown   norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]);
1069c871b86eSJed Brown   for (d = 0; d < dim; d++) x1[d] /= norm;
1070c871b86eSJed Brown   // x2 = n \otimes x1
1071c871b86eSJed Brown   x2[0] = n[1] * x1[2] - n[2] * x1[1];
1072c871b86eSJed Brown   x2[1] = n[2] * x1[0] - n[0] * x1[2];
1073c871b86eSJed Brown   x2[2] = n[0] * x1[1] - n[1] * x1[0];
1074c871b86eSJed Brown   for (d=0; d<dim; d++) {
1075c871b86eSJed Brown     R[d * dim + 0] = x1[d];
1076c871b86eSJed Brown     R[d * dim + 1] = x2[d];
1077c871b86eSJed Brown     R[d * dim + 2] = n[d];
1078c871b86eSJed Brown     c[d] = PetscRealPart(coords[0*dim + d]);
107973868372SMatthew G. Knepley   }
1080c871b86eSJed Brown   for (p=0; p<coordSize/dim; p++) {
1081c871b86eSJed Brown     PetscReal y[3];
1082c871b86eSJed Brown     for (d=0; d<dim; d++) y[d] = PetscRealPart(coords[p*dim + d]) - c[d];
1083c871b86eSJed 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];
10847f07f362SMatthew G. Knepley   }
1085ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1086ccd2543fSMatthew G Knepley }
1087ccd2543fSMatthew G Knepley 
10886322fe33SJed Brown PETSC_UNUSED
1089834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
1090834e62ceSMatthew G. Knepley {
1091834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
1092834e62ceSMatthew G. Knepley 
1093834e62ceSMatthew G. Knepley    |  1  1  1 |
1094834e62ceSMatthew G. Knepley    | x0 x1 x2 |
1095834e62ceSMatthew G. Knepley    | y0 y1 y2 |
1096834e62ceSMatthew G. Knepley 
1097834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
1098834e62ceSMatthew G. Knepley 
1099834e62ceSMatthew G. Knepley    | x1 x2 |
1100834e62ceSMatthew G. Knepley    | y1 y2 |
1101834e62ceSMatthew G. Knepley   */
1102834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
1103834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
1104834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
1105834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
110686623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
1107923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
1108834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
11093bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
1110834e62ceSMatthew G. Knepley }
1111834e62ceSMatthew G. Knepley 
1112834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
1113834e62ceSMatthew G. Knepley {
1114923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
1115834e62ceSMatthew G. Knepley   *vol *= 0.5;
1116834e62ceSMatthew G. Knepley }
1117834e62ceSMatthew G. Knepley 
11186322fe33SJed Brown PETSC_UNUSED
1119834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
1120834e62ceSMatthew G. Knepley {
1121834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
1122834e62ceSMatthew G. Knepley 
1123834e62ceSMatthew G. Knepley    |  1  1  1  1 |
1124834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
1125834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
1126834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
1127834e62ceSMatthew G. Knepley 
1128834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
1129834e62ceSMatthew G. Knepley 
1130834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1131834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1132834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1133834e62ceSMatthew G. Knepley   */
1134834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
1135834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
1136834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
11370a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1138834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
1139834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
1140834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
1141834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
1142923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
11430a3da2c2SToby Isaac   *vol = -onesixth*detM;
11443bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1145834e62ceSMatthew G. Knepley }
1146834e62ceSMatthew G. Knepley 
11470ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
11480ec8681fSMatthew G. Knepley {
11490a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1150923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
11510a3da2c2SToby Isaac   *vol *= -onesixth;
11520ec8681fSMatthew G. Knepley }
11530ec8681fSMatthew G. Knepley 
1154cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1155cb92db44SToby Isaac {
1156cb92db44SToby Isaac   PetscSection   coordSection;
1157cb92db44SToby Isaac   Vec            coordinates;
1158cb92db44SToby Isaac   const PetscScalar *coords;
1159cb92db44SToby Isaac   PetscInt       dim, d, off;
1160cb92db44SToby Isaac   PetscErrorCode ierr;
1161cb92db44SToby Isaac 
1162cb92db44SToby Isaac   PetscFunctionBegin;
1163cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1164cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1165cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
1166cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
1167cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
1168cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
1169cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
1170cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
1171cb92db44SToby Isaac   *detJ = 1.;
1172cb92db44SToby Isaac   if (J) {
1173cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1174cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1175cb92db44SToby Isaac     if (invJ) {
1176cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1177cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1178cb92db44SToby Isaac     }
1179cb92db44SToby Isaac   }
1180cb92db44SToby Isaac   PetscFunctionReturn(0);
1181cb92db44SToby Isaac }
1182cb92db44SToby Isaac 
118317fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
118417fe8556SMatthew G. Knepley {
118517fe8556SMatthew G. Knepley   PetscSection   coordSection;
118617fe8556SMatthew G. Knepley   Vec            coordinates;
1187a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
11888bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
118917fe8556SMatthew G. Knepley   PetscErrorCode ierr;
119017fe8556SMatthew G. Knepley 
119117fe8556SMatthew G. Knepley   PetscFunctionBegin;
119217fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
119369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
11948bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
11958bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
119617fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
11978bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1198adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
11997f07f362SMatthew G. Knepley   *detJ = 0.0;
120028dbe442SToby Isaac   if (numCoords == 6) {
120128dbe442SToby Isaac     const PetscInt dim = 3;
120228dbe442SToby Isaac     PetscReal      R[9], J0;
120328dbe442SToby Isaac 
120428dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1205741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
120628dbe442SToby Isaac     if (J)    {
120728dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
120828dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
120928dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
121028dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
121128dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
121228dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1213adac9986SMatthew G. Knepley     }
121428dbe442SToby Isaac   } else if (numCoords == 4) {
12157f07f362SMatthew G. Knepley     const PetscInt dim = 2;
12167f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
12177f07f362SMatthew G. Knepley 
12187f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1219741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
122017fe8556SMatthew G. Knepley     if (J)    {
12217f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
12227f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
12237f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
1224923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1225923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1226adac9986SMatthew G. Knepley     }
12277f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
12287f07f362SMatthew G. Knepley     const PetscInt dim = 1;
12297f07f362SMatthew G. Knepley 
12307f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
12317f07f362SMatthew G. Knepley     if (J)    {
12327f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
123317fe8556SMatthew G. Knepley       *detJ = J[0];
12343bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
12353bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
1236adac9986SMatthew G. Knepley     }
1237796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
123817fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
123917fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
124017fe8556SMatthew G. Knepley }
124117fe8556SMatthew G. Knepley 
1242ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1243ccd2543fSMatthew G Knepley {
1244ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1245ccd2543fSMatthew G Knepley   Vec            coordinates;
1246a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
124703d7ed2eSStefano Zampini   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1248ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1249ccd2543fSMatthew G Knepley 
1250ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1251ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
125269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
125303d7ed2eSStefano Zampini   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
125403d7ed2eSStefano Zampini   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
1255ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
125603d7ed2eSStefano Zampini   numCoords = numSelfCoords ? numSelfCoords : numCoords;
12577f07f362SMatthew G. Knepley   *detJ = 0.0;
1258ccd2543fSMatthew G Knepley   if (numCoords == 9) {
12597f07f362SMatthew G. Knepley     const PetscInt dim = 3;
12607f07f362SMatthew 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};
12617f07f362SMatthew G. Knepley 
12627f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1263741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
12647f07f362SMatthew G. Knepley     if (J)    {
1265b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1266b7ad821dSMatthew G. Knepley 
1267b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1268b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1269b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1270ccd2543fSMatthew G Knepley         }
12717f07f362SMatthew G. Knepley       }
12723bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1273923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
12747f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
12757f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
12767f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
12777f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
12787f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
12797f07f362SMatthew G. Knepley           }
12807f07f362SMatthew G. Knepley         }
12817f07f362SMatthew G. Knepley       }
12823bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
12837f07f362SMatthew G. Knepley     }
1284923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
12857f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
12867f07f362SMatthew G. Knepley     const PetscInt dim = 2;
12877f07f362SMatthew G. Knepley 
12887f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1289ccd2543fSMatthew G Knepley     if (J)    {
1290ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1291ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1292ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1293ccd2543fSMatthew G Knepley         }
1294ccd2543fSMatthew G Knepley       }
12953bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1296923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1297ccd2543fSMatthew G Knepley     }
1298923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1299796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1300ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1301ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1302ccd2543fSMatthew G Knepley }
1303ccd2543fSMatthew G Knepley 
1304412e9a14SMatthew 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)
1305ccd2543fSMatthew G Knepley {
1306ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1307ccd2543fSMatthew G Knepley   Vec            coordinates;
1308a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
13090d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1310ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1311ccd2543fSMatthew G Knepley 
1312ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1313ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
131469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
13150d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
13160d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
131799dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
131871f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1319dfccc68fSToby Isaac   if (!Nq) {
1320412e9a14SMatthew G. Knepley     PetscInt vorder[4] = {0, 1, 2, 3};
1321412e9a14SMatthew G. Knepley 
1322412e9a14SMatthew G. Knepley     if (isTensor) {vorder[2] = 3; vorder[3] = 2;}
13237f07f362SMatthew G. Knepley     *detJ = 0.0;
132499dec3a6SMatthew G. Knepley     if (numCoords == 12) {
132599dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
132699dec3a6SMatthew 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};
132799dec3a6SMatthew G. Knepley 
1328dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1329741bfc07SMatthew G. Knepley       ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
133099dec3a6SMatthew G. Knepley       if (J)    {
133199dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
133299dec3a6SMatthew G. Knepley 
133399dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
1334412e9a14SMatthew G. Knepley           J0[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*pdim+d]) - PetscRealPart(coords[vorder[0]*pdim+d]));
1335412e9a14SMatthew G. Knepley           J0[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[2]*pdim+d]) - PetscRealPart(coords[vorder[1]*pdim+d]));
133699dec3a6SMatthew G. Knepley         }
13373bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1338923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
133999dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
134099dec3a6SMatthew G. Knepley           for (f = 0; f < dim; f++) {
134199dec3a6SMatthew G. Knepley             J[d*dim+f] = 0.0;
134299dec3a6SMatthew G. Knepley             for (g = 0; g < dim; g++) {
134399dec3a6SMatthew G. Knepley               J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
134499dec3a6SMatthew G. Knepley             }
134599dec3a6SMatthew G. Knepley           }
134699dec3a6SMatthew G. Knepley         }
13473bc0b13bSBarry Smith         ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
134899dec3a6SMatthew G. Knepley       }
1349923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
135071f58de1SToby Isaac     } else if (numCoords == 8) {
135199dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
135299dec3a6SMatthew G. Knepley 
1353dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1354ccd2543fSMatthew G Knepley       if (J)    {
1355ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
1356412e9a14SMatthew G. Knepley           J[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d]));
1357412e9a14SMatthew G. Knepley           J[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[3]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d]));
1358ccd2543fSMatthew G Knepley         }
13593bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1360923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1361ccd2543fSMatthew G Knepley       }
1362923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1363796f034aSJed Brown     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1364dfccc68fSToby Isaac   } else {
1365dfccc68fSToby Isaac     const PetscInt Nv = 4;
1366dfccc68fSToby Isaac     const PetscInt dimR = 2;
1367412e9a14SMatthew G. Knepley     PetscInt  zToPlex[4] = {0, 1, 3, 2};
1368dfccc68fSToby Isaac     PetscReal zOrder[12];
1369dfccc68fSToby Isaac     PetscReal zCoeff[12];
1370dfccc68fSToby Isaac     PetscInt  i, j, k, l, dim;
1371dfccc68fSToby Isaac 
1372412e9a14SMatthew G. Knepley     if (isTensor) {zToPlex[2] = 2; zToPlex[3] = 3;}
1373dfccc68fSToby Isaac     if (numCoords == 12) {
1374dfccc68fSToby Isaac       dim = 3;
1375dfccc68fSToby Isaac     } else if (numCoords == 8) {
1376dfccc68fSToby Isaac       dim = 2;
1377dfccc68fSToby Isaac     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1378dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1379dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1380dfccc68fSToby Isaac 
1381dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1382dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1383dfccc68fSToby Isaac       }
1384dfccc68fSToby Isaac     }
1385dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1386dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1387dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1388dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1389dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1390dfccc68fSToby Isaac     }
1391dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1392dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1393dfccc68fSToby Isaac 
1394dfccc68fSToby Isaac       if (v) {
1395dfccc68fSToby Isaac         PetscReal extPoint[4];
1396dfccc68fSToby Isaac 
1397dfccc68fSToby Isaac         extPoint[0] = 1.;
1398dfccc68fSToby Isaac         extPoint[1] = xi;
1399dfccc68fSToby Isaac         extPoint[2] = eta;
1400dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1401dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1402dfccc68fSToby Isaac           PetscReal val = 0.;
1403dfccc68fSToby Isaac 
1404dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1405dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1406dfccc68fSToby Isaac           }
1407dfccc68fSToby Isaac           v[i * dim + j] = val;
1408dfccc68fSToby Isaac         }
1409dfccc68fSToby Isaac       }
1410dfccc68fSToby Isaac       if (J) {
1411dfccc68fSToby Isaac         PetscReal extJ[8];
1412dfccc68fSToby Isaac 
1413dfccc68fSToby Isaac         extJ[0] = 0.;
1414dfccc68fSToby Isaac         extJ[1] = 0.;
1415dfccc68fSToby Isaac         extJ[2] = 1.;
1416dfccc68fSToby Isaac         extJ[3] = 0.;
1417dfccc68fSToby Isaac         extJ[4] = 0.;
1418dfccc68fSToby Isaac         extJ[5] = 1.;
1419dfccc68fSToby Isaac         extJ[6] = eta;
1420dfccc68fSToby Isaac         extJ[7] = xi;
1421dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1422dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1423dfccc68fSToby Isaac             PetscReal val = 0.;
1424dfccc68fSToby Isaac 
1425dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1426dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1427dfccc68fSToby Isaac             }
1428dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1429dfccc68fSToby Isaac           }
1430dfccc68fSToby Isaac         }
1431dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1432dfccc68fSToby Isaac           PetscReal x, y, z;
1433dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1434dfccc68fSToby Isaac           PetscReal norm;
1435dfccc68fSToby Isaac 
1436dfccc68fSToby Isaac           x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1437dfccc68fSToby Isaac           y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1438dfccc68fSToby Isaac           z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1439dfccc68fSToby Isaac           norm = PetscSqrtReal(x * x + y * y + z * z);
1440dfccc68fSToby Isaac           iJ[2] = x / norm;
1441dfccc68fSToby Isaac           iJ[5] = y / norm;
1442dfccc68fSToby Isaac           iJ[8] = z / norm;
1443dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1444dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1445dfccc68fSToby Isaac         } else {
1446dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1447dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1448dfccc68fSToby Isaac         }
1449dfccc68fSToby Isaac       }
1450dfccc68fSToby Isaac     }
1451dfccc68fSToby Isaac   }
145299dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1453ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1454ccd2543fSMatthew G Knepley }
1455ccd2543fSMatthew G Knepley 
1456ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1457ccd2543fSMatthew G Knepley {
1458ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1459ccd2543fSMatthew G Knepley   Vec            coordinates;
1460a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1461ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
146299dec3a6SMatthew G. Knepley   PetscInt       d;
1463ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1464ccd2543fSMatthew G Knepley 
1465ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1466ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
146769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1468ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
14697f07f362SMatthew G. Knepley   *detJ = 0.0;
14707f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1471ccd2543fSMatthew G Knepley   if (J)    {
1472ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1473f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1474f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1475f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1476f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1477ccd2543fSMatthew G Knepley     }
14783bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1479923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1480ccd2543fSMatthew G Knepley   }
1481923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1482ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1483ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1484ccd2543fSMatthew G Knepley }
1485ccd2543fSMatthew G Knepley 
1486dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1487ccd2543fSMatthew G Knepley {
1488ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1489ccd2543fSMatthew G Knepley   Vec            coordinates;
1490a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1491ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1492ccd2543fSMatthew G Knepley   PetscInt       d;
1493ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1494ccd2543fSMatthew G Knepley 
1495ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1496ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
149769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1498ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1499dfccc68fSToby Isaac   if (!Nq) {
15007f07f362SMatthew G. Knepley     *detJ = 0.0;
1501dfccc68fSToby Isaac     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1502ccd2543fSMatthew G Knepley     if (J)    {
1503ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1504f0df753eSMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1505f0df753eSMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1506f0df753eSMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1507ccd2543fSMatthew G Knepley       }
15083bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1509923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1510ccd2543fSMatthew G Knepley     }
1511923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1512dfccc68fSToby Isaac   } else {
1513dfccc68fSToby Isaac     const PetscInt Nv = 8;
1514dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1515dfccc68fSToby Isaac     const PetscInt dim = 3;
1516dfccc68fSToby Isaac     const PetscInt dimR = 3;
1517dfccc68fSToby Isaac     PetscReal zOrder[24];
1518dfccc68fSToby Isaac     PetscReal zCoeff[24];
1519dfccc68fSToby Isaac     PetscInt  i, j, k, l;
1520dfccc68fSToby Isaac 
1521dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1522dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1523dfccc68fSToby Isaac 
1524dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1525dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1526dfccc68fSToby Isaac       }
1527dfccc68fSToby Isaac     }
1528dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1529dfccc68fSToby 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]);
1530dfccc68fSToby 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]);
1531dfccc68fSToby 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]);
1532dfccc68fSToby 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]);
1533dfccc68fSToby 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]);
1534dfccc68fSToby 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]);
1535dfccc68fSToby 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]);
1536dfccc68fSToby 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]);
1537dfccc68fSToby Isaac     }
1538dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1539dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1540dfccc68fSToby Isaac 
1541dfccc68fSToby Isaac       if (v) {
154291d2b7ceSToby Isaac         PetscReal extPoint[8];
1543dfccc68fSToby Isaac 
1544dfccc68fSToby Isaac         extPoint[0] = 1.;
1545dfccc68fSToby Isaac         extPoint[1] = xi;
1546dfccc68fSToby Isaac         extPoint[2] = eta;
1547dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1548dfccc68fSToby Isaac         extPoint[4] = theta;
1549dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1550dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1551dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1552dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1553dfccc68fSToby Isaac           PetscReal val = 0.;
1554dfccc68fSToby Isaac 
1555dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1556dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1557dfccc68fSToby Isaac           }
1558dfccc68fSToby Isaac           v[i * dim + j] = val;
1559dfccc68fSToby Isaac         }
1560dfccc68fSToby Isaac       }
1561dfccc68fSToby Isaac       if (J) {
1562dfccc68fSToby Isaac         PetscReal extJ[24];
1563dfccc68fSToby Isaac 
1564dfccc68fSToby Isaac         extJ[0]  = 0.         ; extJ[1]  = 0.        ; extJ[2]  = 0.      ;
1565dfccc68fSToby Isaac         extJ[3]  = 1.         ; extJ[4]  = 0.        ; extJ[5]  = 0.      ;
1566dfccc68fSToby Isaac         extJ[6]  = 0.         ; extJ[7]  = 1.        ; extJ[8]  = 0.      ;
1567dfccc68fSToby Isaac         extJ[9]  = eta        ; extJ[10] = xi        ; extJ[11] = 0.      ;
1568dfccc68fSToby Isaac         extJ[12] = 0.         ; extJ[13] = 0.        ; extJ[14] = 1.      ;
1569dfccc68fSToby Isaac         extJ[15] = theta      ; extJ[16] = 0.        ; extJ[17] = xi      ;
1570dfccc68fSToby Isaac         extJ[18] = 0.         ; extJ[19] = theta     ; extJ[20] = eta     ;
1571dfccc68fSToby Isaac         extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi;
1572dfccc68fSToby Isaac 
1573dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1574dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1575dfccc68fSToby Isaac             PetscReal val = 0.;
1576dfccc68fSToby Isaac 
1577dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1578dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1579dfccc68fSToby Isaac             }
1580dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1581dfccc68fSToby Isaac           }
1582dfccc68fSToby Isaac         }
1583dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1584dfccc68fSToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1585dfccc68fSToby Isaac       }
1586dfccc68fSToby Isaac     }
1587dfccc68fSToby Isaac   }
1588ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1589ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1590ccd2543fSMatthew G Knepley }
1591ccd2543fSMatthew G Knepley 
1592dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1593dfccc68fSToby Isaac {
1594ba2698f1SMatthew G. Knepley   DMPolytopeType  ct;
1595dfccc68fSToby Isaac   PetscInt        depth, dim, coordDim, coneSize, i;
1596dfccc68fSToby Isaac   PetscInt        Nq = 0;
1597dfccc68fSToby Isaac   const PetscReal *points = NULL;
1598dfccc68fSToby Isaac   DMLabel         depthLabel;
1599c330f8ffSToby Isaac   PetscReal       xi0[3] = {-1.,-1.,-1.}, v0[3], J0[9], detJ0;
1600dfccc68fSToby Isaac   PetscBool       isAffine = PETSC_TRUE;
1601dfccc68fSToby Isaac   PetscErrorCode  ierr;
1602dfccc68fSToby Isaac 
1603dfccc68fSToby Isaac   PetscFunctionBegin;
1604dfccc68fSToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1605dfccc68fSToby Isaac   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1606dfccc68fSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1607dfccc68fSToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1608dfccc68fSToby Isaac   if (depth == 1 && dim == 1) {
1609dfccc68fSToby Isaac     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1610dfccc68fSToby Isaac   }
1611dfccc68fSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1612dfccc68fSToby Isaac   if (coordDim > 3) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim);
16139c3cf19fSMatthew G. Knepley   if (quad) {ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL);CHKERRQ(ierr);}
1614ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1615ba2698f1SMatthew G. Knepley   switch (ct) {
1616ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_POINT:
1617dfccc68fSToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1618dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1619dfccc68fSToby Isaac     break;
1620ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
1621412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
1622ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1623ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1624dfccc68fSToby Isaac     break;
1625ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
1626ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1627ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1628dfccc68fSToby Isaac     break;
1629ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
1630412e9a14SMatthew G. Knepley     ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1631412e9a14SMatthew G. Knepley     isAffine = PETSC_FALSE;
1632412e9a14SMatthew G. Knepley     break;
1633412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1634412e9a14SMatthew G. Knepley     ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1635dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1636dfccc68fSToby Isaac     break;
1637ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
1638ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1639ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1640dfccc68fSToby Isaac     break;
1641ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
1642dfccc68fSToby Isaac     ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1643dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1644dfccc68fSToby Isaac     break;
1645ba2698f1SMatthew G. Knepley     default: SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No element geometry for cell %D with type %s", cell, DMPolytopeTypes[ct]);
1646dfccc68fSToby Isaac   }
16477318780aSToby Isaac   if (isAffine && Nq) {
1648dfccc68fSToby Isaac     if (v) {
1649dfccc68fSToby Isaac       for (i = 0; i < Nq; i++) {
1650c330f8ffSToby Isaac         CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]);
1651dfccc68fSToby Isaac       }
1652dfccc68fSToby Isaac     }
16537318780aSToby Isaac     if (detJ) {
16547318780aSToby Isaac       for (i = 0; i < Nq; i++) {
16557318780aSToby Isaac         detJ[i] = detJ0;
1656dfccc68fSToby Isaac       }
16577318780aSToby Isaac     }
16587318780aSToby Isaac     if (J) {
16597318780aSToby Isaac       PetscInt k;
16607318780aSToby Isaac 
16617318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1662dfccc68fSToby Isaac         PetscInt j;
1663dfccc68fSToby Isaac 
16647318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
16657318780aSToby Isaac           J[k] = J0[j];
16667318780aSToby Isaac         }
16677318780aSToby Isaac       }
16687318780aSToby Isaac     }
16697318780aSToby Isaac     if (invJ) {
16707318780aSToby Isaac       PetscInt k;
16717318780aSToby Isaac       switch (coordDim) {
16727318780aSToby Isaac       case 0:
16737318780aSToby Isaac         break;
16747318780aSToby Isaac       case 1:
16757318780aSToby Isaac         invJ[0] = 1./J0[0];
16767318780aSToby Isaac         break;
16777318780aSToby Isaac       case 2:
16787318780aSToby Isaac         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
16797318780aSToby Isaac         break;
16807318780aSToby Isaac       case 3:
16817318780aSToby Isaac         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
16827318780aSToby Isaac         break;
16837318780aSToby Isaac       }
16847318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
16857318780aSToby Isaac         PetscInt j;
16867318780aSToby Isaac 
16877318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
16887318780aSToby Isaac           invJ[k] = invJ[j];
16897318780aSToby Isaac         }
1690dfccc68fSToby Isaac       }
1691dfccc68fSToby Isaac     }
1692dfccc68fSToby Isaac   }
1693dfccc68fSToby Isaac   PetscFunctionReturn(0);
1694dfccc68fSToby Isaac }
1695dfccc68fSToby Isaac 
1696ccd2543fSMatthew G Knepley /*@C
16978e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1698ccd2543fSMatthew G Knepley 
1699d083f849SBarry Smith   Collective on dm
1700ccd2543fSMatthew G Knepley 
17014165533cSJose E. Roman   Input Parameters:
1702ccd2543fSMatthew G Knepley + dm   - the DM
1703ccd2543fSMatthew G Knepley - cell - the cell
1704ccd2543fSMatthew G Knepley 
17054165533cSJose E. Roman   Output Parameters:
1706ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1707ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1708ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1709ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1710ccd2543fSMatthew G Knepley 
1711ccd2543fSMatthew G Knepley   Level: advanced
1712ccd2543fSMatthew G Knepley 
1713ccd2543fSMatthew G Knepley   Fortran Notes:
1714ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1715ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1716ccd2543fSMatthew G Knepley 
1717e8964c0aSStefano Zampini .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinates()
1718ccd2543fSMatthew G Knepley @*/
17198e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1720ccd2543fSMatthew G Knepley {
1721ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1722ccd2543fSMatthew G Knepley 
1723ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1724dfccc68fSToby Isaac   ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr);
17258e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
17268e0841e0SMatthew G. Knepley }
17278e0841e0SMatthew G. Knepley 
1728dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
17298e0841e0SMatthew G. Knepley {
1730dfccc68fSToby Isaac   PetscQuadrature   feQuad;
17318e0841e0SMatthew G. Knepley   PetscSection      coordSection;
17328e0841e0SMatthew G. Knepley   Vec               coordinates;
17338e0841e0SMatthew G. Knepley   PetscScalar      *coords = NULL;
17348e0841e0SMatthew G. Knepley   const PetscReal  *quadPoints;
1735ef0bb6c7SMatthew G. Knepley   PetscTabulation T;
1736f960e424SToby Isaac   PetscInt          dim, cdim, pdim, qdim, Nq, numCoords, q;
17378e0841e0SMatthew G. Knepley   PetscErrorCode    ierr;
17388e0841e0SMatthew G. Knepley 
17398e0841e0SMatthew G. Knepley   PetscFunctionBegin;
17408e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
17418e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
17428e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
17438e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
17448e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1745dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
1746dfccc68fSToby Isaac     PetscDualSpace dsp;
1747dfccc68fSToby Isaac 
1748dfccc68fSToby Isaac     ierr = PetscFEGetDualSpace(fe, &dsp);CHKERRQ(ierr);
1749dfccc68fSToby Isaac     ierr = PetscDualSpaceGetFunctional(dsp, 0, &quad);CHKERRQ(ierr);
17509c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1751dfccc68fSToby Isaac     Nq = 1;
1752dfccc68fSToby Isaac   } else {
17539c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1754dfccc68fSToby Isaac   }
175591d2b7ceSToby Isaac   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
1756dfccc68fSToby Isaac   ierr = PetscFEGetQuadrature(fe, &feQuad);CHKERRQ(ierr);
1757dfccc68fSToby Isaac   if (feQuad == quad) {
1758f9244615SMatthew G. Knepley     ierr = PetscFEGetCellTabulation(fe, J ? 1 : 0, &T);CHKERRQ(ierr);
17598e0841e0SMatthew G. Knepley     if (numCoords != pdim*cdim) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "There are %d coordinates for point %d != %d*%d", numCoords, point, pdim, cdim);
1760dfccc68fSToby Isaac   } else {
1761ef0bb6c7SMatthew G. Knepley     ierr = PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T);CHKERRQ(ierr);
1762dfccc68fSToby Isaac   }
1763dfccc68fSToby Isaac   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
1764ef0bb6c7SMatthew G. Knepley   {
1765ef0bb6c7SMatthew G. Knepley     const PetscReal *basis    = T->T[0];
1766ef0bb6c7SMatthew G. Knepley     const PetscReal *basisDer = T->T[1];
1767ef0bb6c7SMatthew G. Knepley     PetscReal        detJt;
1768ef0bb6c7SMatthew G. Knepley 
1769a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
1770a2a9e04cSMatthew G. Knepley     if (Nq != T->Np)     SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %D != %D", Nq, T->Np);
1771a2a9e04cSMatthew G. Knepley     if (pdim != T->Nb)   SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %D != %D", pdim, T->Nb);
1772a2a9e04cSMatthew G. Knepley     if (dim != T->Nc)    SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %D != %D", dim, T->Nc);
1773a2a9e04cSMatthew G. Knepley     if (cdim != T->cdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %D != %D", cdim, T->cdim);
1774a2a9e04cSMatthew G. Knepley #endif
1775dfccc68fSToby Isaac     if (v) {
1776580bdb30SBarry Smith       ierr = PetscArrayzero(v, Nq*cdim);CHKERRQ(ierr);
1777f960e424SToby Isaac       for (q = 0; q < Nq; ++q) {
1778f960e424SToby Isaac         PetscInt i, k;
1779f960e424SToby Isaac 
1780301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
1781301b184aSMatthew G. Knepley           const PetscInt vertex = k/cdim;
1782301b184aSMatthew G. Knepley           for (i = 0; i < cdim; ++i) {
1783301b184aSMatthew G. Knepley             v[q*cdim + i] += basis[(q*pdim + k)*cdim + i] * PetscRealPart(coords[vertex*cdim + i]);
1784301b184aSMatthew G. Knepley           }
1785301b184aSMatthew G. Knepley         }
1786f960e424SToby Isaac         ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1787f960e424SToby Isaac       }
1788f960e424SToby Isaac     }
17898e0841e0SMatthew G. Knepley     if (J) {
1790580bdb30SBarry Smith       ierr = PetscArrayzero(J, Nq*cdim*cdim);CHKERRQ(ierr);
17918e0841e0SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
17928e0841e0SMatthew G. Knepley         PetscInt i, j, k, c, r;
17938e0841e0SMatthew G. Knepley 
17948e0841e0SMatthew G. Knepley         /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
1795301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
1796301b184aSMatthew G. Knepley           const PetscInt vertex = k/cdim;
1797301b184aSMatthew G. Knepley           for (j = 0; j < dim; ++j) {
1798301b184aSMatthew G. Knepley             for (i = 0; i < cdim; ++i) {
1799301b184aSMatthew G. Knepley               J[(q*cdim + i)*cdim + j] += basisDer[((q*pdim + k)*cdim + i)*dim + j] * PetscRealPart(coords[vertex*cdim + i]);
1800301b184aSMatthew G. Knepley             }
1801301b184aSMatthew G. Knepley           }
1802301b184aSMatthew G. Knepley         }
18033bc0b13bSBarry Smith         ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
18048e0841e0SMatthew G. Knepley         if (cdim > dim) {
18058e0841e0SMatthew G. Knepley           for (c = dim; c < cdim; ++c)
18068e0841e0SMatthew G. Knepley             for (r = 0; r < cdim; ++r)
18078e0841e0SMatthew G. Knepley               J[r*cdim+c] = r == c ? 1.0 : 0.0;
18088e0841e0SMatthew G. Knepley         }
1809f960e424SToby Isaac         if (!detJ && !invJ) continue;
1810a63b72c6SToby Isaac         detJt = 0.;
18118e0841e0SMatthew G. Knepley         switch (cdim) {
18128e0841e0SMatthew G. Knepley         case 3:
1813037dc194SToby Isaac           DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]);
1814037dc194SToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
181517fe8556SMatthew G. Knepley           break;
181649dc4407SMatthew G. Knepley         case 2:
18179f328543SToby Isaac           DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]);
1818037dc194SToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
181949dc4407SMatthew G. Knepley           break;
18208e0841e0SMatthew G. Knepley         case 1:
1821037dc194SToby Isaac           detJt = J[q*cdim*dim];
1822037dc194SToby Isaac           if (invJ) invJ[q*cdim*dim] = 1.0/detJt;
182349dc4407SMatthew G. Knepley         }
1824f960e424SToby Isaac         if (detJ) detJ[q] = detJt;
182549dc4407SMatthew G. Knepley       }
1826ef0bb6c7SMatthew G. Knepley     } else if (detJ || invJ) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
182749dc4407SMatthew G. Knepley   }
1828ef0bb6c7SMatthew G. Knepley   if (feQuad != quad) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
18298e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
18308e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
18318e0841e0SMatthew G. Knepley }
18328e0841e0SMatthew G. Knepley 
18338e0841e0SMatthew G. Knepley /*@C
18348e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
18358e0841e0SMatthew G. Knepley 
1836d083f849SBarry Smith   Collective on dm
18378e0841e0SMatthew G. Knepley 
18384165533cSJose E. Roman   Input Parameters:
18398e0841e0SMatthew G. Knepley + dm   - the DM
18408e0841e0SMatthew G. Knepley . cell - the cell
1841dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
1842dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
18438e0841e0SMatthew G. Knepley 
18444165533cSJose E. Roman   Output Parameters:
1845dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
18468e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
18478e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
18488e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
18498e0841e0SMatthew G. Knepley 
18508e0841e0SMatthew G. Knepley   Level: advanced
18518e0841e0SMatthew G. Knepley 
18528e0841e0SMatthew G. Knepley   Fortran Notes:
18538e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
18548e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
18558e0841e0SMatthew G. Knepley 
1856e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
18578e0841e0SMatthew G. Knepley @*/
1858dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
18598e0841e0SMatthew G. Knepley {
1860bb4a5db5SMatthew G. Knepley   DM             cdm;
1861dfccc68fSToby Isaac   PetscFE        fe = NULL;
18628e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
18638e0841e0SMatthew G. Knepley 
18648e0841e0SMatthew G. Knepley   PetscFunctionBegin;
18652d89661fSMatthew G. Knepley   PetscValidPointer(detJ, 7);
1866bb4a5db5SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1867bb4a5db5SMatthew G. Knepley   if (cdm) {
1868dfccc68fSToby Isaac     PetscClassId id;
1869dfccc68fSToby Isaac     PetscInt     numFields;
1870e5e52638SMatthew G. Knepley     PetscDS      prob;
1871dfccc68fSToby Isaac     PetscObject  disc;
1872dfccc68fSToby Isaac 
1873bb4a5db5SMatthew G. Knepley     ierr = DMGetNumFields(cdm, &numFields);CHKERRQ(ierr);
1874dfccc68fSToby Isaac     if (numFields) {
1875bb4a5db5SMatthew G. Knepley       ierr = DMGetDS(cdm, &prob);CHKERRQ(ierr);
1876dfccc68fSToby Isaac       ierr = PetscDSGetDiscretization(prob,0,&disc);CHKERRQ(ierr);
1877dfccc68fSToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1878dfccc68fSToby Isaac       if (id == PETSCFE_CLASSID) {
1879dfccc68fSToby Isaac         fe = (PetscFE) disc;
1880dfccc68fSToby Isaac       }
1881dfccc68fSToby Isaac     }
1882dfccc68fSToby Isaac   }
1883dfccc68fSToby Isaac   if (!fe) {ierr = DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1884dfccc68fSToby Isaac   else     {ierr = DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1885ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1886ccd2543fSMatthew G Knepley }
1887834e62ceSMatthew G. Knepley 
1888*9bf2564aSMatt McGurn static PetscErrorCode DMPlexComputeGeometryFVM_0D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1889*9bf2564aSMatt McGurn {
1890*9bf2564aSMatt McGurn   PetscSection        coordSection;
1891*9bf2564aSMatt McGurn   Vec                 coordinates;
1892*9bf2564aSMatt McGurn   const PetscScalar  *coords = NULL;
1893*9bf2564aSMatt McGurn   PetscInt            d, dof, off;
1894*9bf2564aSMatt McGurn   PetscErrorCode      ierr;
1895*9bf2564aSMatt McGurn 
1896*9bf2564aSMatt McGurn   PetscFunctionBegin;
1897*9bf2564aSMatt McGurn   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1898*9bf2564aSMatt McGurn   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1899*9bf2564aSMatt McGurn   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1900*9bf2564aSMatt McGurn 
1901*9bf2564aSMatt McGurn   /* for a point the centroid is just the coord */
1902*9bf2564aSMatt McGurn   if (centroid) {
1903*9bf2564aSMatt McGurn     ierr = PetscSectionGetDof(coordSection, cell, &dof);CHKERRQ(ierr);
1904*9bf2564aSMatt McGurn     ierr = PetscSectionGetOffset(coordSection, cell, &off);CHKERRQ(ierr);
1905*9bf2564aSMatt McGurn     for (d = 0; d < dof; d++){
1906*9bf2564aSMatt McGurn       centroid[d] = PetscRealPart(coords[off + d]);
1907*9bf2564aSMatt McGurn     }
1908*9bf2564aSMatt McGurn   }
1909*9bf2564aSMatt McGurn   if (normal) {
1910*9bf2564aSMatt McGurn     const PetscInt *support, *cones;
1911*9bf2564aSMatt McGurn     PetscInt        supportSize;
1912*9bf2564aSMatt McGurn     PetscReal       norm, sign;
1913*9bf2564aSMatt McGurn 
1914*9bf2564aSMatt McGurn     /* compute the norm based upon the support centroids */
1915*9bf2564aSMatt McGurn     ierr = DMPlexGetSupportSize(dm, cell, &supportSize);CHKERRQ(ierr);
1916*9bf2564aSMatt McGurn     ierr = DMPlexGetSupport(dm, cell, &support);CHKERRQ(ierr);
1917*9bf2564aSMatt McGurn     ierr = DMPlexComputeCellGeometryFVM(dm, support[0], NULL, normal, NULL);CHKERRQ(ierr);
1918*9bf2564aSMatt McGurn 
1919*9bf2564aSMatt McGurn     /* Take the normal from the centroid of the support to the vertex*/
1920*9bf2564aSMatt McGurn     ierr = PetscSectionGetDof(coordSection, cell, &dof);CHKERRQ(ierr);
1921*9bf2564aSMatt McGurn     ierr = PetscSectionGetOffset(coordSection, cell, &off);CHKERRQ(ierr);
1922*9bf2564aSMatt McGurn     for (d = 0; d < dof; d++){
1923*9bf2564aSMatt McGurn       normal[d] -= PetscRealPart(coords[off + d]);
1924*9bf2564aSMatt McGurn     }
1925*9bf2564aSMatt McGurn 
1926*9bf2564aSMatt McGurn     /* Determine the sign of the normal based upon its location in the support */
1927*9bf2564aSMatt McGurn     ierr = DMPlexGetCone(dm, support[0], &cones);CHKERRQ(ierr);
1928*9bf2564aSMatt McGurn     sign = cones[0] == cell ? 1.0 : -1.0;
1929*9bf2564aSMatt McGurn 
1930*9bf2564aSMatt McGurn     norm = DMPlex_NormD_Internal(dim, normal);
1931*9bf2564aSMatt McGurn     for (d = 0; d < dim; ++d) normal[d] /= (norm*sign);
1932*9bf2564aSMatt McGurn   }
1933*9bf2564aSMatt McGurn   if (vol) {
1934*9bf2564aSMatt McGurn     *vol = 1.0;
1935*9bf2564aSMatt McGurn   }
1936*9bf2564aSMatt McGurn   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
1937*9bf2564aSMatt McGurn   PetscFunctionReturn(0);
1938*9bf2564aSMatt McGurn }
1939*9bf2564aSMatt McGurn 
1940011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1941cc08537eSMatthew G. Knepley {
1942cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1943cc08537eSMatthew G. Knepley   Vec            coordinates;
1944a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
194506e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1946714b99b6SMatthew G. Knepley   PetscInt       coordSize, d;
1947cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1948cc08537eSMatthew G. Knepley 
1949cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1950cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
195169d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1952cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
19532e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1954cc08537eSMatthew G. Knepley   if (centroid) {
1955714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) centroid[d] = 0.5*PetscRealPart(coords[d] + tmp[d]);
1956cc08537eSMatthew G. Knepley   }
1957cc08537eSMatthew G. Knepley   if (normal) {
1958a60a936bSMatthew G. Knepley     PetscReal norm;
1959a60a936bSMatthew G. Knepley 
1960714b99b6SMatthew G. Knepley     if (dim != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "We only support 2D edges right now");
196106e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
196206e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1963714b99b6SMatthew G. Knepley     norm       = DMPlex_NormD_Internal(dim, normal);
1964714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) normal[d] /= norm;
1965cc08537eSMatthew G. Knepley   }
1966cc08537eSMatthew G. Knepley   if (vol) {
1967714b99b6SMatthew G. Knepley     *vol = 0.0;
1968714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - tmp[d]));
1969714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
1970cc08537eSMatthew G. Knepley   }
1971cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1972cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1973cc08537eSMatthew G. Knepley }
1974cc08537eSMatthew G. Knepley 
1975cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */
1976011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1977cc08537eSMatthew G. Knepley {
1978412e9a14SMatthew G. Knepley   DMPolytopeType ct;
1979cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1980cc08537eSMatthew G. Knepley   Vec            coordinates;
1981cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
1982793a2a13SMatthew G. Knepley   PetscInt       fv[4] = {0, 1, 2, 3};
19834f99dae5SMatthew G. Knepley   PetscInt       cdim, coordSize, numCorners, p, d;
1984cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1985cc08537eSMatthew G. Knepley 
1986cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1987793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
1988412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1989412e9a14SMatthew G. Knepley   switch (ct) {
19904f99dae5SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR: fv[2] = 3; fv[3] = 2;break;
1991412e9a14SMatthew G. Knepley     default: break;
1992412e9a14SMatthew G. Knepley   }
1993cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
19940a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
199569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1996cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
19974f99dae5SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1998793a2a13SMatthew G. Knepley 
19994f99dae5SMatthew G. Knepley   if (cdim > 2) {
20004f99dae5SMatthew G. Knepley     PetscReal c[3] = {0., 0., 0.}, n[3] = {0., 0., 0.}, norm;
20014f99dae5SMatthew G. Knepley 
20024f99dae5SMatthew G. Knepley     for (p = 0; p < numCorners-2; ++p) {
20034f99dae5SMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[cdim*fv[p+1]+0] - coords[0]), x1 = PetscRealPart(coords[cdim*fv[p+2]+0] - coords[0]);
20044f99dae5SMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[cdim*fv[p+1]+1] - coords[1]), y1 = PetscRealPart(coords[cdim*fv[p+2]+1] - coords[1]);
20054f99dae5SMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[cdim*fv[p+1]+2] - coords[2]), z1 = PetscRealPart(coords[cdim*fv[p+2]+2] - coords[2]);
20064f99dae5SMatthew G. Knepley       const PetscReal dx = y0*z1 - z0*y1;
20074f99dae5SMatthew G. Knepley       const PetscReal dy = z0*x1 - x0*z1;
20084f99dae5SMatthew G. Knepley       const PetscReal dz = x0*y1 - y0*x1;
20094f99dae5SMatthew G. Knepley       PetscReal       a  = PetscSqrtReal(dx*dx + dy*dy + dz*dz);
20104f99dae5SMatthew G. Knepley 
20114f99dae5SMatthew G. Knepley       n[0] += dx;
20124f99dae5SMatthew G. Knepley       n[1] += dy;
20134f99dae5SMatthew G. Knepley       n[2] += dz;
20144f99dae5SMatthew G. Knepley       c[0] += a * PetscRealPart(coords[0] + coords[cdim*fv[p+1]+0] + coords[cdim*fv[p+2]+0])/3.;
20154f99dae5SMatthew G. Knepley       c[1] += a * PetscRealPart(coords[1] + coords[cdim*fv[p+1]+1] + coords[cdim*fv[p+2]+1])/3.;
20164f99dae5SMatthew G. Knepley       c[2] += a * PetscRealPart(coords[2] + coords[cdim*fv[p+1]+2] + coords[cdim*fv[p+2]+2])/3.;
2017ceee4971SMatthew G. Knepley     }
20184f99dae5SMatthew G. Knepley     norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
20194f99dae5SMatthew G. Knepley     n[0] /= norm;
20204f99dae5SMatthew G. Knepley     n[1] /= norm;
20214f99dae5SMatthew G. Knepley     n[2] /= norm;
20224f99dae5SMatthew G. Knepley     c[0] /= norm;
20234f99dae5SMatthew G. Knepley     c[1] /= norm;
20244f99dae5SMatthew G. Knepley     c[2] /= norm;
20254f99dae5SMatthew G. Knepley     if (vol) *vol = 0.5*norm;
20264f99dae5SMatthew G. Knepley     if (centroid) for (d = 0; d < cdim; ++d) centroid[d] = c[d];
20274f99dae5SMatthew G. Knepley     if (normal) for (d = 0; d < cdim; ++d) normal[d] = n[d];
2028011ea5d8SMatthew G. Knepley   } else {
20294f99dae5SMatthew G. Knepley     PetscReal vsum = 0.0, csum[2] = {0.0, 0.0}, vtmp, ctmp[4] = {0., 0., 0., 0.};
20304f99dae5SMatthew G. Knepley 
20310a1d6728SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
2032793a2a13SMatthew G. Knepley       const PetscInt pi  = p < 4 ? fv[p] : p;
2033793a2a13SMatthew G. Knepley       const PetscInt pin = p < 3 ? fv[(p+1)%numCorners] : (p+1)%numCorners;
20340a1d6728SMatthew G. Knepley       /* Need to do this copy to get types right */
20354f99dae5SMatthew G. Knepley       for (d = 0; d < cdim; ++d) {
20364f99dae5SMatthew G. Knepley         ctmp[d]      = PetscRealPart(coords[pi*cdim+d]);
20374f99dae5SMatthew G. Knepley         ctmp[cdim+d] = PetscRealPart(coords[pin*cdim+d]);
20380a1d6728SMatthew G. Knepley       }
20390a1d6728SMatthew G. Knepley       Volume_Triangle_Origin_Internal(&vtmp, ctmp);
20400a1d6728SMatthew G. Knepley       vsum += vtmp;
20414f99dae5SMatthew G. Knepley       for (d = 0; d < cdim; ++d) csum[d] += (ctmp[d] + ctmp[cdim+d])*vtmp;
20420a1d6728SMatthew G. Knepley     }
20434f99dae5SMatthew G. Knepley     if (vol) *vol = PetscAbsReal(vsum);
20444f99dae5SMatthew G. Knepley     if (centroid) for (d = 0; d < cdim; ++d) centroid[d] = csum[d] / ((cdim+1)*vsum);
20454f99dae5SMatthew G. Knepley     if (normal) for (d = 0; d < cdim; ++d) normal[d] = 0.0;
20460a1d6728SMatthew G. Knepley   }
20470a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
2048cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
2049cc08537eSMatthew G. Knepley }
2050cc08537eSMatthew G. Knepley 
20510ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */
2052011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
20530ec8681fSMatthew G. Knepley {
2054412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
20550ec8681fSMatthew G. Knepley   PetscSection    coordSection;
20560ec8681fSMatthew G. Knepley   Vec             coordinates;
20570ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
205886623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
2059a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
2060793a2a13SMatthew G. Knepley   PetscBool       isHybrid = PETSC_FALSE;
2061412e9a14SMatthew G. Knepley   PetscInt        numFaces, f, coordSize, p, d;
20620ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
20630ec8681fSMatthew G. Knepley 
20640ec8681fSMatthew G. Knepley   PetscFunctionBegin;
2065f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
2066793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
2067412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
2068412e9a14SMatthew G. Knepley   switch (ct) {
2069412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
2070412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
2071412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
2072412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
2073412e9a14SMatthew G. Knepley       isHybrid = PETSC_TRUE;
2074412e9a14SMatthew G. Knepley     default: break;
2075412e9a14SMatthew G. Knepley   }
2076793a2a13SMatthew G. Knepley 
20770ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
207869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
20790ec8681fSMatthew G. Knepley 
2080d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
20810ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
20820ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
2083a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
20840ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
2085793a2a13SMatthew G. Knepley     PetscBool      flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
2086ba2698f1SMatthew G. Knepley     DMPolytopeType ct;
2087793a2a13SMatthew G. Knepley 
2088011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
2089ba2698f1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, faces[f], &ct);CHKERRQ(ierr);
2090ba2698f1SMatthew G. Knepley     switch (ct) {
2091ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
20920ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
20931ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
20941ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
20951ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
20960ec8681fSMatthew G. Knepley       }
20970ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
2098793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
20990ec8681fSMatthew G. Knepley       vsum += vtmp;
21004f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
21010ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
21021ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
21030ec8681fSMatthew G. Knepley         }
21040ec8681fSMatthew G. Knepley       }
21050ec8681fSMatthew G. Knepley       break;
2106ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
2107412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
2108793a2a13SMatthew G. Knepley     {
2109793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
2110793a2a13SMatthew G. Knepley 
2111793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
2112793a2a13SMatthew G. Knepley       if (isHybrid && f > 1) {fv[2] = 3; fv[3] = 2;}
21130ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
21140ec8681fSMatthew G. Knepley       /* First tet */
21150ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
2116793a2a13SMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[0]*dim+d]);
2117793a2a13SMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[1]*dim+d]);
2118793a2a13SMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]);
21190ec8681fSMatthew G. Knepley       }
21200ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
2121793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
21220ec8681fSMatthew G. Knepley       vsum += vtmp;
21230ec8681fSMatthew G. Knepley       if (centroid) {
21240ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
21250ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
21260ec8681fSMatthew G. Knepley         }
21270ec8681fSMatthew G. Knepley       }
21280ec8681fSMatthew G. Knepley       /* Second tet */
21290ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
2130793a2a13SMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[1]*dim+d]);
2131793a2a13SMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[2]*dim+d]);
2132793a2a13SMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]);
21330ec8681fSMatthew G. Knepley       }
21340ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
2135793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
21360ec8681fSMatthew G. Knepley       vsum += vtmp;
21370ec8681fSMatthew G. Knepley       if (centroid) {
21380ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
21390ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
21400ec8681fSMatthew G. Knepley         }
21410ec8681fSMatthew G. Knepley       }
21420ec8681fSMatthew G. Knepley       break;
2143793a2a13SMatthew G. Knepley     }
21440ec8681fSMatthew G. Knepley     default:
2145ba2698f1SMatthew G. Knepley       SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %D of type %s", faces[f], DMPolytopeTypes[ct]);
21460ec8681fSMatthew G. Knepley     }
21474f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
21480ec8681fSMatthew G. Knepley   }
21498763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
21500ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
2151d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
21520ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
21530ec8681fSMatthew G. Knepley }
21540ec8681fSMatthew G. Knepley 
2155834e62ceSMatthew G. Knepley /*@C
2156834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
2157834e62ceSMatthew G. Knepley 
2158d083f849SBarry Smith   Collective on dm
2159834e62ceSMatthew G. Knepley 
21604165533cSJose E. Roman   Input Parameters:
2161834e62ceSMatthew G. Knepley + dm   - the DM
2162834e62ceSMatthew G. Knepley - cell - the cell
2163834e62ceSMatthew G. Knepley 
21644165533cSJose E. Roman   Output Parameters:
2165834e62ceSMatthew G. Knepley + volume   - the cell volume
2166cc08537eSMatthew G. Knepley . centroid - the cell centroid
2167cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
2168834e62ceSMatthew G. Knepley 
2169834e62ceSMatthew G. Knepley   Level: advanced
2170834e62ceSMatthew G. Knepley 
2171834e62ceSMatthew G. Knepley   Fortran Notes:
2172834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2173834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
2174834e62ceSMatthew G. Knepley 
2175e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
2176834e62ceSMatthew G. Knepley @*/
2177cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2178834e62ceSMatthew G. Knepley {
21790ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
2180834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
2181834e62ceSMatthew G. Knepley 
2182834e62ceSMatthew G. Knepley   PetscFunctionBegin;
2183834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2184c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2185834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
2186ba2698f1SMatthew G. Knepley   ierr = DMPlexGetPointDepth(dm, cell, &depth);CHKERRQ(ierr);
2187011ea5d8SMatthew G. Knepley   switch (depth) {
2188*9bf2564aSMatt McGurn   case 0:
2189*9bf2564aSMatt McGurn     ierr = DMPlexComputeGeometryFVM_0D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2190*9bf2564aSMatt McGurn     break;
2191cc08537eSMatthew G. Knepley   case 1:
2192011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2193cc08537eSMatthew G. Knepley     break;
2194834e62ceSMatthew G. Knepley   case 2:
2195011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2196834e62ceSMatthew G. Knepley     break;
2197834e62ceSMatthew G. Knepley   case 3:
2198011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2199834e62ceSMatthew G. Knepley     break;
2200834e62ceSMatthew G. Knepley   default:
2201b81cf158SMatthew G. Knepley     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D (depth %D) for element geometry computation", dim, depth);
2202834e62ceSMatthew G. Knepley   }
2203834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
2204834e62ceSMatthew G. Knepley }
2205113c68e6SMatthew G. Knepley 
2206c501906fSMatthew G. Knepley /*@
2207c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
2208c501906fSMatthew G. Knepley 
2209c501906fSMatthew G. Knepley   Collective on dm
2210c501906fSMatthew G. Knepley 
2211c501906fSMatthew G. Knepley   Input Parameter:
2212c501906fSMatthew G. Knepley . dm - The DMPlex
2213c501906fSMatthew G. Knepley 
2214c501906fSMatthew G. Knepley   Output Parameter:
2215c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
2216c501906fSMatthew G. Knepley 
2217c501906fSMatthew G. Knepley   Level: beginner
2218c501906fSMatthew G. Knepley 
2219c501906fSMatthew G. Knepley @*/
2220c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
2221c0d900a5SMatthew G. Knepley {
2222c0d900a5SMatthew G. Knepley   DM             dmCell;
2223c0d900a5SMatthew G. Knepley   Vec            coordinates;
2224c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
2225c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
2226412e9a14SMatthew G. Knepley   PetscInt       cStart, cEnd, c;
2227c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
2228c0d900a5SMatthew G. Knepley 
2229c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
2230c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2231c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2232c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2233c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2234c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2235c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2236412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2237c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
2238c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
2239cf0b7c11SKarl Rupp   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFEGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2240c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
224192fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2242c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2243c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2244c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2245c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2246cf0b7c11SKarl Rupp     PetscFEGeom *cg;
2247c0d900a5SMatthew G. Knepley 
2248c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2249580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2250cf0b7c11SKarl Rupp     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ);CHKERRQ(ierr);
2251087ef6b2SMatthew G. Knepley     if (*cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D", (double) *cg->detJ, c);
2252c0d900a5SMatthew G. Knepley   }
2253c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
2254c0d900a5SMatthew G. Knepley }
2255c0d900a5SMatthew G. Knepley 
2256891a9168SMatthew G. Knepley /*@
2257891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2258891a9168SMatthew G. Knepley 
2259891a9168SMatthew G. Knepley   Input Parameter:
2260891a9168SMatthew G. Knepley . dm - The DM
2261891a9168SMatthew G. Knepley 
2262891a9168SMatthew G. Knepley   Output Parameters:
2263891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2264a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data
2265891a9168SMatthew G. Knepley 
2266891a9168SMatthew G. Knepley   Level: developer
2267891a9168SMatthew G. Knepley 
2268891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
2269891a9168SMatthew G. Knepley @*/
2270113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2271113c68e6SMatthew G. Knepley {
2272113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
2273113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
2274113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
2275113c68e6SMatthew G. Knepley   PetscSection   coordSection;
2276113c68e6SMatthew G. Knepley   Vec            coordinates;
2277113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2278113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
2279113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2280113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
2281113c68e6SMatthew G. Knepley 
2282113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2283113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2284113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2285113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2286113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
2287113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2288113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2289113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2290113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2291113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2292485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2293113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
22949e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2295113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
229692fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2297113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2298113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2299485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2300113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2301113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2302113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2303113c68e6SMatthew G. Knepley 
2304113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2305580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2306113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
2307113c68e6SMatthew G. Knepley   }
2308113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
2309113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
2310113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
2311113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2312113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
23139e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2314113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
231592fd8e1eSJed Brown   ierr = DMSetLocalSection(dmFace, sectionFace);CHKERRQ(ierr);
2316113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
2317113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
2318113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
2319c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2320113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2321113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2322113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2323113c68e6SMatthew G. Knepley     PetscReal        area;
2324412e9a14SMatthew G. Knepley     const PetscInt  *cells;
2325412e9a14SMatthew G. Knepley     PetscInt         ncells, ghost = -1, d, numChildren;
2326113c68e6SMatthew G. Knepley 
23279ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
232850d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2329412e9a14SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
2330412e9a14SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
2331412e9a14SMatthew G. Knepley     /* It is possible to get a face with no support when using partition overlap */
2332412e9a14SMatthew G. Knepley     if (!ncells || ghost >= 0 || numChildren) continue;
2333113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
2334113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
2335113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2336113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2337113c68e6SMatthew G. Knepley     {
2338113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
2339113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
23400453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2341113c68e6SMatthew G. Knepley 
2342113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
2343113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
234406348e87SToby Isaac       if (ncells > 1) {
234506348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
2346113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
234706348e87SToby Isaac       }
234806348e87SToby Isaac       else {
234906348e87SToby Isaac         rcentroid = fg->centroid;
235006348e87SToby Isaac       }
23512e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
23522e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
23530453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2354113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2355113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2356113c68e6SMatthew G. Knepley       }
2357113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
2358113c68e6SMatthew G. Knepley         if (dim == 2) SETERRQ5(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]);
2359113c68e6SMatthew G. Knepley         if (dim == 3) SETERRQ7(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]);
2360113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
2361113c68e6SMatthew G. Knepley       }
2362113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2363113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2364113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2365113c68e6SMatthew G. Knepley       }
236606348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2367113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2368113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2369113c68e6SMatthew G. Knepley       }
2370113c68e6SMatthew G. Knepley     }
2371113c68e6SMatthew G. Knepley   }
2372820f2d46SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
2373113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
2374113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2375113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2376113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2377113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
2378113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2379113c68e6SMatthew G. Knepley 
2380113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
2381113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
2382113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
2383113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
238450d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
2385113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
2386113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
2387113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2388113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2389113c68e6SMatthew G. Knepley       if (support[s] == c) {
2390640bce14SSatish Balay         PetscFVCellGeom       *ci;
2391113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
2392113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
2393113c68e6SMatthew G. Knepley 
2394113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
2395113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2396113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
2397113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
2398113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
2399113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2400113c68e6SMatthew G. Knepley       }
2401113c68e6SMatthew G. Knepley     }
2402113c68e6SMatthew G. Knepley   }
2403113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
2404113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2405113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2406113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
2407113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2408113c68e6SMatthew G. Knepley }
2409113c68e6SMatthew G. Knepley 
2410113c68e6SMatthew G. Knepley /*@C
2411113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2412113c68e6SMatthew G. Knepley 
2413113c68e6SMatthew G. Knepley   Not collective
2414113c68e6SMatthew G. Knepley 
24154165533cSJose E. Roman   Input Parameter:
2416113c68e6SMatthew G. Knepley . dm - the DM
2417113c68e6SMatthew G. Knepley 
24184165533cSJose E. Roman   Output Parameter:
2419a5b23f4aSJose E. Roman . minradius - the minimum cell radius
2420113c68e6SMatthew G. Knepley 
2421113c68e6SMatthew G. Knepley   Level: developer
2422113c68e6SMatthew G. Knepley 
2423113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
2424113c68e6SMatthew G. Knepley @*/
2425113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2426113c68e6SMatthew G. Knepley {
2427113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2428113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2429113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
2430113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
2431113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2432113c68e6SMatthew G. Knepley }
2433113c68e6SMatthew G. Knepley 
2434113c68e6SMatthew G. Knepley /*@C
2435113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2436113c68e6SMatthew G. Knepley 
2437113c68e6SMatthew G. Knepley   Logically collective
2438113c68e6SMatthew G. Knepley 
24394165533cSJose E. Roman   Input Parameters:
2440113c68e6SMatthew G. Knepley + dm - the DM
2441a5b23f4aSJose E. Roman - minradius - the minimum cell radius
2442113c68e6SMatthew G. Knepley 
2443113c68e6SMatthew G. Knepley   Level: developer
2444113c68e6SMatthew G. Knepley 
2445113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
2446113c68e6SMatthew G. Knepley @*/
2447113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2448113c68e6SMatthew G. Knepley {
2449113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2450113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2451113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
2452113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2453113c68e6SMatthew G. Knepley }
2454856ac710SMatthew G. Knepley 
2455856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2456856ac710SMatthew G. Knepley {
2457856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
2458856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
2459856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2460856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2461856ac710SMatthew G. Knepley 
2462856ac710SMatthew G. Knepley   PetscFunctionBegin;
2463856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2464856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2465485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2466089217ebSMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
2467856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
2468856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2469c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2470856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2471856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2472856ac710SMatthew G. Knepley     const PetscInt        *faces;
2473856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
2474640bce14SSatish Balay     PetscFVCellGeom        *cg;
2475856ac710SMatthew G. Knepley     PetscBool              boundary;
2476856ac710SMatthew G. Knepley     PetscInt               ghost;
2477856ac710SMatthew G. Knepley 
2478856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2479856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
2480856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
2481856ac710SMatthew G. Knepley     if (numFaces < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cell %D has only %D faces, not enough for gradient reconstruction", c, numFaces);
2482856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2483640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2484856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
2485856ac710SMatthew G. Knepley       const PetscInt        *fcells;
2486856ac710SMatthew G. Knepley       PetscInt               ncell, side;
2487856ac710SMatthew G. Knepley 
2488856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2489a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2490856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2491856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
2492856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2493856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
2494856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
2495856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2496856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
2497856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
2498856ac710SMatthew G. Knepley     }
2499856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
2500856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
2501856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2502856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2503a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2504856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2505856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
2506856ac710SMatthew G. Knepley       ++usedFaces;
2507856ac710SMatthew G. Knepley     }
2508856ac710SMatthew G. Knepley   }
2509856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
2510856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2511856ac710SMatthew G. Knepley }
2512856ac710SMatthew G. Knepley 
2513b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2514b81db932SToby Isaac {
2515b81db932SToby Isaac   DMLabel        ghostLabel;
2516b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
2517b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2518b81db932SToby Isaac   PetscSection   neighSec;
2519b81db932SToby Isaac   PetscInt     (*neighbors)[2];
2520b81db932SToby Isaac   PetscInt      *counter;
2521b81db932SToby Isaac   PetscErrorCode ierr;
2522b81db932SToby Isaac 
2523b81db932SToby Isaac   PetscFunctionBegin;
2524b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2525b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2526485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2527485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2528b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
2529b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
2530b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2531c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2532b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2533b81db932SToby Isaac     const PetscInt        *fcells;
2534b81db932SToby Isaac     PetscBool              boundary;
25355bc680faSToby Isaac     PetscInt               ghost = -1;
2536b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2537b81db932SToby Isaac 
253806348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2539a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2540b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2541b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2542b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
254306348e87SToby Isaac     if (numCells == 2) {
2544b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2545b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2546b81db932SToby Isaac         PetscInt cell = fcells[c];
2547b81db932SToby Isaac 
2548e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2549b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
2550b81db932SToby Isaac         }
2551b81db932SToby Isaac       }
2552b81db932SToby Isaac     }
255306348e87SToby Isaac   }
2554b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
2555b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
2556b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2557b81db932SToby Isaac   nStart = 0;
2558b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2559b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2560b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2561b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2562b81db932SToby Isaac     const PetscInt        *fcells;
2563b81db932SToby Isaac     PetscBool              boundary;
25645bc680faSToby Isaac     PetscInt               ghost = -1;
2565b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2566b81db932SToby Isaac 
256706348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2568a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2569b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2570b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2571b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
257206348e87SToby Isaac     if (numCells == 2) {
2573b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2574b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2575b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2576b81db932SToby Isaac 
2577e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2578b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2579b81db932SToby Isaac           off += counter[cell - cStart]++;
2580b81db932SToby Isaac           neighbors[off][0] = f;
2581b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2582b81db932SToby Isaac         }
2583b81db932SToby Isaac       }
2584b81db932SToby Isaac     }
258506348e87SToby Isaac   }
2586b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2587b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2588b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2589317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2590640bce14SSatish Balay     PetscFVCellGeom        *cg;
2591b81db932SToby Isaac 
2592b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2593b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2594b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2595317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2596317218b9SToby Isaac     if (ghost < 0 && numFaces < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cell %D has only %D faces, not enough for gradient reconstruction", c, numFaces);
2597b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2598640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2599b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2600b81db932SToby Isaac       const PetscInt        *fcells;
2601b81db932SToby Isaac       PetscInt               ncell, side, nface;
2602b81db932SToby Isaac 
2603b81db932SToby Isaac       nface = neighbors[off + f][0];
2604b81db932SToby Isaac       ncell = neighbors[off + f][1];
2605b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2606b81db932SToby Isaac       side  = (c != fcells[0]);
2607b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2608b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2609b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2610b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2611b81db932SToby Isaac     }
2612b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2613b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2614b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2615b81db932SToby Isaac     }
2616b81db932SToby Isaac   }
2617b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
26185fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2619b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2620b81db932SToby Isaac   PetscFunctionReturn(0);
2621b81db932SToby Isaac }
2622b81db932SToby Isaac 
2623856ac710SMatthew G. Knepley /*@
2624856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2625856ac710SMatthew G. Knepley 
2626d083f849SBarry Smith   Collective on dm
2627856ac710SMatthew G. Knepley 
26284165533cSJose E. Roman   Input Parameters:
2629856ac710SMatthew G. Knepley + dm  - The DM
2630856ac710SMatthew G. Knepley . fvm - The PetscFV
26318f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2632856ac710SMatthew G. Knepley 
26336b867d5aSJose E. Roman   Input/Output Parameter:
26346b867d5aSJose E. Roman . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM(); on output
26356b867d5aSJose E. Roman                  the geometric factors for gradient calculation are inserted
26366b867d5aSJose E. Roman 
26376b867d5aSJose E. Roman   Output Parameter:
26386b867d5aSJose E. Roman . dmGrad - The DM describing the layout of gradient data
2639856ac710SMatthew G. Knepley 
2640856ac710SMatthew G. Knepley   Level: developer
2641856ac710SMatthew G. Knepley 
2642856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2643856ac710SMatthew G. Knepley @*/
2644856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2645856ac710SMatthew G. Knepley {
2646856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2647856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2648b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2649856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2650856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2651856ac710SMatthew G. Knepley 
2652856ac710SMatthew G. Knepley   PetscFunctionBegin;
2653856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2654856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2655856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2656485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2657856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2658856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2659856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2660856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2661856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2662b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2663b81db932SToby Isaac   if (!parentSection) {
2664856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2665b5a3613cSMatthew G. Knepley   } else {
2666b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2667b81db932SToby Isaac   }
2668856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2669856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2670856ac710SMatthew G. Knepley   /* Create storage for gradients */
2671856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2672856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2673856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2674856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2675856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
267692fd8e1eSJed Brown   ierr = DMSetLocalSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2677856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2678856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2679856ac710SMatthew G. Knepley }
2680b27d5b9eSToby Isaac 
2681c501906fSMatthew G. Knepley /*@
2682c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2683c501906fSMatthew G. Knepley 
2684d083f849SBarry Smith   Collective on dm
2685c501906fSMatthew G. Knepley 
26864165533cSJose E. Roman   Input Parameters:
2687c501906fSMatthew G. Knepley + dm  - The DM
26886b867d5aSJose E. Roman - fv  - The PetscFV
2689c501906fSMatthew G. Knepley 
2690c501906fSMatthew G. Knepley   Output Parameters:
2691c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2692c501906fSMatthew G. Knepley . faceGeometry - The face geometry
26936b867d5aSJose E. Roman - gradDM       - The gradient matrices
2694c501906fSMatthew G. Knepley 
2695c501906fSMatthew G. Knepley   Level: developer
2696c501906fSMatthew G. Knepley 
2697c501906fSMatthew G. Knepley .seealso: DMPlexComputeGeometryFVM()
2698c501906fSMatthew G. Knepley @*/
2699b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2700b27d5b9eSToby Isaac {
2701b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2702b27d5b9eSToby Isaac   PetscErrorCode ierr;
2703b27d5b9eSToby Isaac 
2704b27d5b9eSToby Isaac   PetscFunctionBegin;
2705b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2706b27d5b9eSToby Isaac   if (!cellgeomobj) {
2707b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2708b27d5b9eSToby Isaac 
2709b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2710b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2711b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2712b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2713b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2714b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2715b27d5b9eSToby Isaac   }
2716b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2717b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2718b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2719b27d5b9eSToby Isaac   if (gradDM) {
2720b27d5b9eSToby Isaac     PetscObject gradobj;
2721b27d5b9eSToby Isaac     PetscBool   computeGradients;
2722b27d5b9eSToby Isaac 
2723b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2724b27d5b9eSToby Isaac     if (!computeGradients) {
2725b27d5b9eSToby Isaac       *gradDM = NULL;
2726b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2727b27d5b9eSToby Isaac     }
2728b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2729b27d5b9eSToby Isaac     if (!gradobj) {
2730b27d5b9eSToby Isaac       DM dmGradInt;
2731b27d5b9eSToby Isaac 
2732b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2733b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2734b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2735b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2736b27d5b9eSToby Isaac     }
2737b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2738b27d5b9eSToby Isaac   }
2739b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2740b27d5b9eSToby Isaac }
2741d6143a4eSToby Isaac 
27429d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
27439d150b73SToby Isaac {
27449d150b73SToby Isaac   PetscInt l, m;
27459d150b73SToby Isaac 
2746cd345991SToby Isaac   PetscFunctionBeginHot;
27479d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
27489d150b73SToby Isaac     /* invert Jacobian, multiply */
27499d150b73SToby Isaac     PetscScalar det, idet;
27509d150b73SToby Isaac 
27519d150b73SToby Isaac     switch (dimR) {
27529d150b73SToby Isaac     case 1:
27539d150b73SToby Isaac       invJ[0] = 1./ J[0];
27549d150b73SToby Isaac       break;
27559d150b73SToby Isaac     case 2:
27569d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
27579d150b73SToby Isaac       idet = 1./det;
27589d150b73SToby Isaac       invJ[0] =  J[3] * idet;
27599d150b73SToby Isaac       invJ[1] = -J[1] * idet;
27609d150b73SToby Isaac       invJ[2] = -J[2] * idet;
27619d150b73SToby Isaac       invJ[3] =  J[0] * idet;
27629d150b73SToby Isaac       break;
27639d150b73SToby Isaac     case 3:
27649d150b73SToby Isaac       {
27659d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
27669d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
27679d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
27689d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
27699d150b73SToby Isaac         idet = 1./det;
27709d150b73SToby Isaac         invJ[0] *= idet;
27719d150b73SToby Isaac         invJ[1] *= idet;
27729d150b73SToby Isaac         invJ[2] *= idet;
27739d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
27749d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
27759d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
27769d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
27779d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
27789d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
27799d150b73SToby Isaac       }
27809d150b73SToby Isaac       break;
27819d150b73SToby Isaac     }
27829d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
27839d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2784c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
27859d150b73SToby Isaac       }
27869d150b73SToby Isaac     }
27879d150b73SToby Isaac   } else {
27889d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
27899d150b73SToby Isaac     char transpose = 'C';
27909d150b73SToby Isaac #else
27919d150b73SToby Isaac     char transpose = 'T';
27929d150b73SToby Isaac #endif
27939d150b73SToby Isaac     PetscBLASInt m = dimR;
27949d150b73SToby Isaac     PetscBLASInt n = dimC;
27959d150b73SToby Isaac     PetscBLASInt one = 1;
27969d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
27979d150b73SToby Isaac 
27989d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
27999d150b73SToby Isaac 
28009d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
28019d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
28029d150b73SToby Isaac 
2803c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
28049d150b73SToby Isaac   }
28059d150b73SToby Isaac   PetscFunctionReturn(0);
28069d150b73SToby Isaac }
28079d150b73SToby Isaac 
28089d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
28099d150b73SToby Isaac {
2810c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
28119d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
28129d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
28139d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
28149d150b73SToby Isaac   PetscErrorCode ierr;
28159d150b73SToby Isaac 
28169d150b73SToby Isaac   PetscFunctionBegin;
28179d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28189d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
281913903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
282069291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
282169291d52SBarry Smith   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
28229d150b73SToby Isaac   cellCoords = &cellData[0];
28239d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
28249d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
28259d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
28269d150b73SToby Isaac   invJ       = &J[dimR * dimC];
28279d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
28289d150b73SToby Isaac   if (dimR == 2) {
28299d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
28309d150b73SToby Isaac 
28319d150b73SToby Isaac     for (i = 0; i < 4; i++) {
28329d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
28339d150b73SToby Isaac 
28349d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
28359d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
28369d150b73SToby Isaac       }
28379d150b73SToby Isaac     }
28389d150b73SToby Isaac   } else if (dimR == 3) {
28399d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
28409d150b73SToby Isaac 
28419d150b73SToby Isaac     for (i = 0; i < 8; i++) {
28429d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
28439d150b73SToby Isaac 
28449d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
28459d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
28469d150b73SToby Isaac       }
28479d150b73SToby Isaac     }
28489d150b73SToby Isaac   } else {
28499d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
28509d150b73SToby Isaac   }
28519d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
28529d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
28539d150b73SToby Isaac     PetscReal *swap;
28549d150b73SToby Isaac 
28559d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
28569d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
28579d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
28589d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
28599d150b73SToby Isaac       }
28609d150b73SToby Isaac     }
28619d150b73SToby Isaac 
28629d150b73SToby Isaac     if (i < dimR - 1) {
28639d150b73SToby Isaac       swap = cellCoeffs;
28649d150b73SToby Isaac       cellCoeffs = cellCoords;
28659d150b73SToby Isaac       cellCoords = swap;
28669d150b73SToby Isaac     }
28679d150b73SToby Isaac   }
2868580bdb30SBarry Smith   ierr = PetscArrayzero(refCoords,numPoints * dimR);CHKERRQ(ierr);
28699d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28709d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
28719d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
28729d150b73SToby Isaac 
28739d150b73SToby Isaac       /* compute -residual and Jacobian */
28749d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
28759d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
28769d150b73SToby Isaac       for (k = 0; k < numV; k++) {
28779d150b73SToby Isaac         PetscReal extCoord = 1.;
28789d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
28799d150b73SToby Isaac           PetscReal coord = guess[l];
28809d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
28819d150b73SToby Isaac 
28829d150b73SToby Isaac           extCoord *= dep * coord + !dep;
28839d150b73SToby Isaac           extJ[l] = dep;
28849d150b73SToby Isaac 
28859d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
28869d150b73SToby Isaac             PetscReal coord = guess[m];
28879d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
28889d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
28899d150b73SToby Isaac 
28909d150b73SToby Isaac             extJ[l] *= mult;
28919d150b73SToby Isaac           }
28929d150b73SToby Isaac         }
28939d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
28949d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
28959d150b73SToby Isaac 
28969d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
28979d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
28989d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
28999d150b73SToby Isaac           }
29009d150b73SToby Isaac         }
29019d150b73SToby Isaac       }
290276bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
29030611203eSToby Isaac         PetscReal maxAbs = 0.;
29040611203eSToby Isaac 
29050611203eSToby Isaac         for (l = 0; l < dimC; l++) {
29060611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
29070611203eSToby Isaac         }
2908087ef6b2SMatthew G. Knepley         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
29090611203eSToby Isaac       }
29109d150b73SToby Isaac 
29119d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
29129d150b73SToby Isaac     }
29139d150b73SToby Isaac   }
291469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
291569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
29169d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
29179d150b73SToby Isaac   PetscFunctionReturn(0);
29189d150b73SToby Isaac }
29199d150b73SToby Isaac 
29209d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
29219d150b73SToby Isaac {
29229d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
29239d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
29249d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
29259d150b73SToby Isaac   PetscErrorCode ierr;
29269d150b73SToby Isaac 
29279d150b73SToby Isaac   PetscFunctionBegin;
29289d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29299d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
293013903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
293169291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
29329d150b73SToby Isaac   cellCoords = &cellData[0];
29339d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
29349d150b73SToby Isaac   if (dimR == 2) {
29359d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
29369d150b73SToby Isaac 
29379d150b73SToby Isaac     for (i = 0; i < 4; i++) {
29389d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
29399d150b73SToby Isaac 
29409d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
29419d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
29429d150b73SToby Isaac       }
29439d150b73SToby Isaac     }
29449d150b73SToby Isaac   } else if (dimR == 3) {
29459d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
29469d150b73SToby Isaac 
29479d150b73SToby Isaac     for (i = 0; i < 8; i++) {
29489d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
29499d150b73SToby Isaac 
29509d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
29519d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
29529d150b73SToby Isaac       }
29539d150b73SToby Isaac     }
29549d150b73SToby Isaac   } else {
29559d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
29569d150b73SToby Isaac   }
29579d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
29589d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
29599d150b73SToby Isaac     PetscReal *swap;
29609d150b73SToby Isaac 
29619d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
29629d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
29639d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
29649d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
29659d150b73SToby Isaac       }
29669d150b73SToby Isaac     }
29679d150b73SToby Isaac 
29689d150b73SToby Isaac     if (i < dimR - 1) {
29699d150b73SToby Isaac       swap = cellCoeffs;
29709d150b73SToby Isaac       cellCoeffs = cellCoords;
29719d150b73SToby Isaac       cellCoords = swap;
29729d150b73SToby Isaac     }
29739d150b73SToby Isaac   }
2974580bdb30SBarry Smith   ierr = PetscArrayzero(realCoords,numPoints * dimC);CHKERRQ(ierr);
29759d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
29769d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
29779d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
29789d150b73SToby Isaac 
29799d150b73SToby Isaac     for (k = 0; k < numV; k++) {
29809d150b73SToby Isaac       PetscReal extCoord = 1.;
29819d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
29829d150b73SToby Isaac         PetscReal coord = guess[l];
29839d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
29849d150b73SToby Isaac 
29859d150b73SToby Isaac         extCoord *= dep * coord + !dep;
29869d150b73SToby Isaac       }
29879d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
29889d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
29899d150b73SToby Isaac 
29909d150b73SToby Isaac         mapped[l] += coeff * extCoord;
29919d150b73SToby Isaac       }
29929d150b73SToby Isaac     }
29939d150b73SToby Isaac   }
299469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
29959d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
29969d150b73SToby Isaac   PetscFunctionReturn(0);
29979d150b73SToby Isaac }
29989d150b73SToby Isaac 
29999c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
30009c3cf19fSMatthew 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)
30019d150b73SToby Isaac {
30029c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
3003c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
3004c6e120d1SToby Isaac   PetscReal      *invV, *modes;
3005c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
3006c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
30079d150b73SToby Isaac   PetscErrorCode ierr;
30089d150b73SToby Isaac 
30099d150b73SToby Isaac   PetscFunctionBegin;
30109c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
30119d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
30129c3cf19fSMatthew G. Knepley   if (numComp != Nc) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc);
30139d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
30149d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
301569291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
30169d150b73SToby Isaac   invV = fe->invV;
3017012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3018012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3019012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
3020012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
30219d150b73SToby Isaac     }
30229d150b73SToby Isaac   }
302369291d52SBarry Smith   ierr   = DMGetWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
30249c3cf19fSMatthew G. Knepley   D      = &B[pdim*Nc];
30259c3cf19fSMatthew G. Knepley   resNeg = &D[pdim*Nc * dimR];
302669291d52SBarry Smith   ierr = DMGetWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
30279c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
30289c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
30299d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
30309d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
30319b1f03cbSToby 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 */
30329d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
30339d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
30349c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc; k++) {resNeg[k] = realCoords[j * Nc + k];}
30359c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc * dimR; k++) {J[k] = 0.;}
30369c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
30379c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
3038012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
30399d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
3040012b7cc6SMatthew G. Knepley             J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
30419d150b73SToby Isaac           }
30429d150b73SToby Isaac         }
30439d150b73SToby Isaac       }
304476bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
30450611203eSToby Isaac         PetscReal maxAbs = 0.;
30460611203eSToby Isaac 
30479c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
30480611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
30490611203eSToby Isaac         }
3050087ef6b2SMatthew G. Knepley         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
30510611203eSToby Isaac       }
30529c3cf19fSMatthew G. Knepley       ierr = DMPlexCoordinatesToReference_NewtonUpdate(Nc,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
30539d150b73SToby Isaac     }
30549d150b73SToby Isaac   }
305569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
305669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
305769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
30589d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
30599d150b73SToby Isaac   PetscFunctionReturn(0);
30609d150b73SToby Isaac }
30619d150b73SToby Isaac 
30629c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
30639c3cf19fSMatthew 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)
30649d150b73SToby Isaac {
30659c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, coordSize;
3066c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
3067c6e120d1SToby Isaac   PetscReal      *invV, *modes;
30689d150b73SToby Isaac   PetscReal      *B;
30699d150b73SToby Isaac   PetscErrorCode ierr;
30709d150b73SToby Isaac 
30719d150b73SToby Isaac   PetscFunctionBegin;
30729c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
30739d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
30749c3cf19fSMatthew G. Knepley   if (numComp != Nc) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc);
30759d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
30769d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
307769291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
30789d150b73SToby Isaac   invV = fe->invV;
3079012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
3080012b7cc6SMatthew G. Knepley     modes[i] = 0.;
3081012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
3082012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
30839d150b73SToby Isaac     }
30849d150b73SToby Isaac   }
308569291d52SBarry Smith   ierr = DMGetWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
3086012b7cc6SMatthew G. Knepley   ierr = PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL);CHKERRQ(ierr);
30879c3cf19fSMatthew G. Knepley   for (i = 0; i < numPoints * Nc; i++) {realCoords[i] = 0.;}
30889d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
30899c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
30909d150b73SToby Isaac 
30919c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
30929c3cf19fSMatthew G. Knepley       for (l = 0; l < Nc; l++) {
309340cf36b3SToby Isaac         mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
30949d150b73SToby Isaac       }
30959d150b73SToby Isaac     }
30969d150b73SToby Isaac   }
309769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
309869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
30999d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
31009d150b73SToby Isaac   PetscFunctionReturn(0);
31019d150b73SToby Isaac }
31029d150b73SToby Isaac 
3103d6143a4eSToby Isaac /*@
3104d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
3105d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
3106d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
3107d6143a4eSToby Isaac 
3108d6143a4eSToby Isaac   Not collective
3109d6143a4eSToby Isaac 
3110d6143a4eSToby Isaac   Input Parameters:
3111d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
3112d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
3113d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
3114d6143a4eSToby Isaac . cell       - the cell whose map is used.
3115d6143a4eSToby Isaac . numPoints  - the number of points to locate
31161b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
3117d6143a4eSToby Isaac 
3118d6143a4eSToby Isaac   Output Parameters:
3119d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
31201b266c99SBarry Smith 
31211b266c99SBarry Smith   Level: intermediate
312273c9229bSMatthew Knepley 
312373c9229bSMatthew Knepley .seealso: DMPlexReferenceToCoordinates()
3124d6143a4eSToby Isaac @*/
3125d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
3126d6143a4eSToby Isaac {
3127485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
31289d150b73SToby Isaac   DM             coordDM = NULL;
31299d150b73SToby Isaac   Vec            coords;
31309d150b73SToby Isaac   PetscFE        fe = NULL;
31319d150b73SToby Isaac   PetscErrorCode ierr;
31329d150b73SToby Isaac 
3133d6143a4eSToby Isaac   PetscFunctionBegin;
31349d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31359d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
31369d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
31379d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
31389d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
31399d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
31409d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
31419d150b73SToby Isaac   if (coordDM) {
31429d150b73SToby Isaac     PetscInt coordFields;
31439d150b73SToby Isaac 
31449d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
31459d150b73SToby Isaac     if (coordFields) {
31469d150b73SToby Isaac       PetscClassId id;
31479d150b73SToby Isaac       PetscObject  disc;
31489d150b73SToby Isaac 
314944a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
31509d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
31519d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
31529d150b73SToby Isaac         fe = (PetscFE) disc;
31539d150b73SToby Isaac       }
31549d150b73SToby Isaac     }
31559d150b73SToby Isaac   }
3156412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
315713903a91SSatish Balay   if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);
31589d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
31599d150b73SToby Isaac     PetscInt  coneSize;
31609d150b73SToby Isaac     PetscBool isSimplex, isTensor;
31619d150b73SToby Isaac 
31629d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
31639d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
31649d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
31659d150b73SToby Isaac     if (isSimplex) {
31669d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
31679d150b73SToby Isaac 
316869291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
31699d150b73SToby Isaac       J    = &v0[dimC];
31709d150b73SToby Isaac       invJ = &J[dimC * dimC];
31719d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
31729d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3173c330f8ffSToby Isaac         const PetscReal x0[3] = {-1.,-1.,-1.};
3174c330f8ffSToby Isaac 
3175c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
31769d150b73SToby Isaac       }
317769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
31789d150b73SToby Isaac     } else if (isTensor) {
31799d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
31809d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
31819d150b73SToby Isaac   } else {
31829d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
31839d150b73SToby Isaac   }
31849d150b73SToby Isaac   PetscFunctionReturn(0);
31859d150b73SToby Isaac }
31869d150b73SToby Isaac 
31879d150b73SToby Isaac /*@
31889d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
31899d150b73SToby Isaac 
31909d150b73SToby Isaac   Not collective
31919d150b73SToby Isaac 
31929d150b73SToby Isaac   Input Parameters:
31939d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
31949d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
31959d150b73SToby Isaac                as a multilinear map for tensor-product elements
31969d150b73SToby Isaac . cell       - the cell whose map is used.
31979d150b73SToby Isaac . numPoints  - the number of points to locate
3198a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
31999d150b73SToby Isaac 
32009d150b73SToby Isaac   Output Parameters:
32019d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
32021b266c99SBarry Smith 
32031b266c99SBarry Smith    Level: intermediate
320473c9229bSMatthew Knepley 
320573c9229bSMatthew Knepley .seealso: DMPlexCoordinatesToReference()
32069d150b73SToby Isaac @*/
32079d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
32089d150b73SToby Isaac {
3209485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
32109d150b73SToby Isaac   DM             coordDM = NULL;
32119d150b73SToby Isaac   Vec            coords;
32129d150b73SToby Isaac   PetscFE        fe = NULL;
32139d150b73SToby Isaac   PetscErrorCode ierr;
32149d150b73SToby Isaac 
32159d150b73SToby Isaac   PetscFunctionBegin;
32169d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32179d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
32189d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
32199d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
32209d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
32219d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
32229d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
32239d150b73SToby Isaac   if (coordDM) {
32249d150b73SToby Isaac     PetscInt coordFields;
32259d150b73SToby Isaac 
32269d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
32279d150b73SToby Isaac     if (coordFields) {
32289d150b73SToby Isaac       PetscClassId id;
32299d150b73SToby Isaac       PetscObject  disc;
32309d150b73SToby Isaac 
323144a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
32329d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
32339d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
32349d150b73SToby Isaac         fe = (PetscFE) disc;
32359d150b73SToby Isaac       }
32369d150b73SToby Isaac     }
32379d150b73SToby Isaac   }
3238412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
323913903a91SSatish Balay   if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);
32409d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
32419d150b73SToby Isaac     PetscInt  coneSize;
32429d150b73SToby Isaac     PetscBool isSimplex, isTensor;
32439d150b73SToby Isaac 
32449d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
32459d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
32469d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
32479d150b73SToby Isaac     if (isSimplex) {
32489d150b73SToby Isaac       PetscReal detJ, *v0, *J;
32499d150b73SToby Isaac 
325069291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
32519d150b73SToby Isaac       J    = &v0[dimC];
32529d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
3253c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3254c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1.,-1.,-1.};
3255c330f8ffSToby Isaac 
3256c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
32579d150b73SToby Isaac       }
325869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
32599d150b73SToby Isaac     } else if (isTensor) {
32609d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
32619d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
32629d150b73SToby Isaac   } else {
32639d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
32649d150b73SToby Isaac   }
3265d6143a4eSToby Isaac   PetscFunctionReturn(0);
3266d6143a4eSToby Isaac }
32670139fca9SMatthew G. Knepley 
32680139fca9SMatthew G. Knepley /*@C
32690139fca9SMatthew G. Knepley   DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates.
32700139fca9SMatthew G. Knepley 
32710139fca9SMatthew G. Knepley   Not collective
32720139fca9SMatthew G. Knepley 
32730139fca9SMatthew G. Knepley   Input Parameters:
32740139fca9SMatthew G. Knepley + dm      - The DM
32750139fca9SMatthew G. Knepley . time    - The time
32760139fca9SMatthew G. Knepley - func    - The function transforming current coordinates to new coordaintes
32770139fca9SMatthew G. Knepley 
32780139fca9SMatthew G. Knepley    Calling sequence of func:
32790139fca9SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
32800139fca9SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
32810139fca9SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
32820139fca9SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
32830139fca9SMatthew G. Knepley 
32840139fca9SMatthew G. Knepley +  dim          - The spatial dimension
32850139fca9SMatthew G. Knepley .  Nf           - The number of input fields (here 1)
32860139fca9SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
32870139fca9SMatthew G. Knepley .  uOff         - The offset of the coordinates in u[] (here 0)
32880139fca9SMatthew G. Knepley .  uOff_x       - The offset of the coordinates in u_x[] (here 0)
32890139fca9SMatthew G. Knepley .  u            - The coordinate values at this point in space
32900139fca9SMatthew G. Knepley .  u_t          - The coordinate time derivative at this point in space (here NULL)
32910139fca9SMatthew G. Knepley .  u_x          - The coordinate derivatives at this point in space
32920139fca9SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
32930139fca9SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
32940139fca9SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
32950139fca9SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
32960139fca9SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
32970139fca9SMatthew G. Knepley .  t            - The current time
32980139fca9SMatthew G. Knepley .  x            - The coordinates of this point (here not used)
32990139fca9SMatthew G. Knepley .  numConstants - The number of constants
33000139fca9SMatthew G. Knepley .  constants    - The value of each constant
33010139fca9SMatthew G. Knepley -  f            - The new coordinates at this point in space
33020139fca9SMatthew G. Knepley 
33030139fca9SMatthew G. Knepley   Level: intermediate
33040139fca9SMatthew G. Knepley 
33050139fca9SMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMProjectFieldLocal(), DMProjectFieldLabelLocal()
33060139fca9SMatthew G. Knepley @*/
33070139fca9SMatthew G. Knepley PetscErrorCode DMPlexRemapGeometry(DM dm, PetscReal time,
33080139fca9SMatthew G. Knepley                                    void (*func)(PetscInt, PetscInt, PetscInt,
33090139fca9SMatthew G. Knepley                                                 const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
33100139fca9SMatthew G. Knepley                                                 const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
33110139fca9SMatthew G. Knepley                                                 PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]))
33120139fca9SMatthew G. Knepley {
33130139fca9SMatthew G. Knepley   DM             cdm;
33148bf1a49fSMatthew G. Knepley   DMField        cf;
33150139fca9SMatthew G. Knepley   Vec            lCoords, tmpCoords;
33160139fca9SMatthew G. Knepley   PetscErrorCode ierr;
33170139fca9SMatthew G. Knepley 
33180139fca9SMatthew G. Knepley   PetscFunctionBegin;
33190139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
33200139fca9SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &lCoords);CHKERRQ(ierr);
33210139fca9SMatthew G. Knepley   ierr = DMGetLocalVector(cdm, &tmpCoords);CHKERRQ(ierr);
33220139fca9SMatthew G. Knepley   ierr = VecCopy(lCoords, tmpCoords);CHKERRQ(ierr);
33238bf1a49fSMatthew G. Knepley   /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */
33248bf1a49fSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &cf);CHKERRQ(ierr);
33258bf1a49fSMatthew G. Knepley   cdm->coordinateField = cf;
33260139fca9SMatthew G. Knepley   ierr = DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords);CHKERRQ(ierr);
33278bf1a49fSMatthew G. Knepley   cdm->coordinateField = NULL;
33280139fca9SMatthew G. Knepley   ierr = DMRestoreLocalVector(cdm, &tmpCoords);CHKERRQ(ierr);
3329cdaaecf7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, lCoords);CHKERRQ(ierr);
33300139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
33310139fca9SMatthew G. Knepley }
33320139fca9SMatthew G. Knepley 
33330139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z,
33340139fca9SMatthew G. Knepley   / 1  0  m_0 \
33350139fca9SMatthew G. Knepley   | 0  1  m_1 |
33360139fca9SMatthew G. Knepley   \ 0  0   1  /
33370139fca9SMatthew G. Knepley */
33380139fca9SMatthew G. Knepley static void f0_shear(PetscInt dim, PetscInt Nf, PetscInt NfAux,
33390139fca9SMatthew G. Knepley                      const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
33400139fca9SMatthew G. Knepley                      const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
33410139fca9SMatthew G. Knepley                      PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar coords[])
33420139fca9SMatthew G. Knepley {
33430139fca9SMatthew G. Knepley   const PetscInt Nc = uOff[1]-uOff[0];
3344c1f1bd54SMatthew G. Knepley   const PetscInt ax = (PetscInt) PetscRealPart(constants[0]);
33450139fca9SMatthew G. Knepley   PetscInt       c;
33460139fca9SMatthew G. Knepley 
33470139fca9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
33480139fca9SMatthew G. Knepley     coords[c] = u[c] + constants[c+1]*u[ax];
33490139fca9SMatthew G. Knepley   }
33500139fca9SMatthew G. Knepley }
33510139fca9SMatthew G. Knepley 
33520139fca9SMatthew G. Knepley /*@C
33530139fca9SMatthew G. Knepley   DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates.
33540139fca9SMatthew G. Knepley 
33550139fca9SMatthew G. Knepley   Not collective
33560139fca9SMatthew G. Knepley 
33570139fca9SMatthew G. Knepley   Input Parameters:
33580139fca9SMatthew G. Knepley + dm          - The DM
33593ee9839eSMatthew G. Knepley . direction   - The shear coordinate direction, e.g. 0 is the x-axis
33600139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction
33610139fca9SMatthew G. Knepley 
33620139fca9SMatthew G. Knepley   Level: intermediate
33630139fca9SMatthew G. Knepley 
33640139fca9SMatthew G. Knepley .seealso: DMPlexRemapGeometry()
33650139fca9SMatthew G. Knepley @*/
33663ee9839eSMatthew G. Knepley PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[])
33670139fca9SMatthew G. Knepley {
33680139fca9SMatthew G. Knepley   DM             cdm;
33690139fca9SMatthew G. Knepley   PetscDS        cds;
33700139fca9SMatthew G. Knepley   PetscObject    obj;
33710139fca9SMatthew G. Knepley   PetscClassId   id;
33720139fca9SMatthew G. Knepley   PetscScalar   *moduli;
33733ee9839eSMatthew G. Knepley   const PetscInt dir = (PetscInt) direction;
33740139fca9SMatthew G. Knepley   PetscInt       dE, d, e;
33750139fca9SMatthew G. Knepley   PetscErrorCode ierr;
33760139fca9SMatthew G. Knepley 
33770139fca9SMatthew G. Knepley   PetscFunctionBegin;
33780139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
33790139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
33800139fca9SMatthew G. Knepley   ierr = PetscMalloc1(dE+1, &moduli);CHKERRQ(ierr);
33810139fca9SMatthew G. Knepley   moduli[0] = dir;
3382cdaaecf7SMatthew G. Knepley   for (d = 0, e = 0; d < dE; ++d) moduli[d+1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0);
33830139fca9SMatthew G. Knepley   ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
33840139fca9SMatthew G. Knepley   ierr = PetscDSGetDiscretization(cds, 0, &obj);CHKERRQ(ierr);
33850139fca9SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
33860139fca9SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
33870139fca9SMatthew G. Knepley     Vec           lCoords;
33880139fca9SMatthew G. Knepley     PetscSection  cSection;
33890139fca9SMatthew G. Knepley     PetscScalar  *coords;
33900139fca9SMatthew G. Knepley     PetscInt      vStart, vEnd, v;
33910139fca9SMatthew G. Knepley 
33920139fca9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
33930139fca9SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &cSection);CHKERRQ(ierr);
33940139fca9SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &lCoords);CHKERRQ(ierr);
33950139fca9SMatthew G. Knepley     ierr = VecGetArray(lCoords, &coords);CHKERRQ(ierr);
33960139fca9SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
33970139fca9SMatthew G. Knepley       PetscReal ds;
33980139fca9SMatthew G. Knepley       PetscInt  off, c;
33990139fca9SMatthew G. Knepley 
34000139fca9SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, v, &off);CHKERRQ(ierr);
34010139fca9SMatthew G. Knepley       ds   = PetscRealPart(coords[off+dir]);
34020139fca9SMatthew G. Knepley       for (c = 0; c < dE; ++c) coords[off+c] += moduli[c]*ds;
34030139fca9SMatthew G. Knepley     }
34040139fca9SMatthew G. Knepley     ierr = VecRestoreArray(lCoords, &coords);CHKERRQ(ierr);
34050139fca9SMatthew G. Knepley   } else {
34060139fca9SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, dE+1, moduli);CHKERRQ(ierr);
34070139fca9SMatthew G. Knepley     ierr = DMPlexRemapGeometry(dm, 0.0, f0_shear);CHKERRQ(ierr);
34080139fca9SMatthew G. Knepley   }
34090139fca9SMatthew G. Knepley   ierr = PetscFree(moduli);CHKERRQ(ierr);
34100139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
34110139fca9SMatthew G. Knepley }
3412