xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 301b184a17e092099b2b3872d02c81740144e2c0)
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 
333985bb02SVaclav Hapla .seealso: DMPlexCreate(), DMGetCoordinates()
343985bb02SVaclav Hapla @*/
353985bb02SVaclav Hapla PetscErrorCode DMPlexFindVertices(DM dm, PetscInt npoints, const PetscReal coord[], PetscReal eps, PetscInt dagPoints[])
363985bb02SVaclav Hapla {
37335ef845SVaclav Hapla   PetscInt          c, dim, 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;
453985bb02SVaclav Hapla   ierr = DMGetDimension(dm, &dim);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);
573985bb02SVaclav Hapla       if (PetscUnlikely(ndof != dim)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %D: ndof = %D != %D = dim", p, ndof, dim);
58335ef845SVaclav Hapla     }
59335ef845SVaclav Hapla   }
60eca9f518SVaclav Hapla   if (eps == 0.0) {
61eca9f518SVaclav Hapla     for (i=0,j=0; i < npoints; i++,j+=dim) {
62eca9f518SVaclav Hapla       dagPoints[i] = -1;
63eca9f518SVaclav Hapla       for (p = vStart,o=0; p < vEnd; p++,o+=dim) {
64eca9f518SVaclav Hapla         for (c = 0; c < dim; c++) {
65eca9f518SVaclav Hapla           if (coord[j+c] != PetscRealPart(allCoords[o+c])) break;
66eca9f518SVaclav Hapla         }
67eca9f518SVaclav Hapla         if (c == dim) {
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   }
76335ef845SVaclav Hapla   for (i=0,j=0; i < npoints; i++,j+=dim) {
77335ef845SVaclav Hapla     dagPoints[i] = -1;
78335ef845SVaclav Hapla     for (p = vStart,o=0; p < vEnd; p++,o+=dim) {
793985bb02SVaclav Hapla       norm = 0.0;
803985bb02SVaclav Hapla       for (c = 0; c < dim; 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 
128ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
129ccd2543fSMatthew G Knepley {
130ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
131f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
132ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
133ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
134ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
135ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
136ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
137ccd2543fSMatthew G Knepley 
138ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1398e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
140ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
141ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
142ccd2543fSMatthew G Knepley 
143f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
144c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
145ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
146ccd2543fSMatthew G Knepley }
147ccd2543fSMatthew G Knepley 
14862a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
14962a38674SMatthew G. Knepley {
15062a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
15162a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
15262a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
15362a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
15462a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
15562a38674SMatthew G. Knepley   PetscErrorCode  ierr;
15662a38674SMatthew G. Knepley 
15762a38674SMatthew G. Knepley   PetscFunctionBegin;
15862a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
15962a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
16062a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
16162a38674SMatthew G. Knepley 
16262a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
16362a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
16462a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
16562a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
16662a38674SMatthew G. Knepley     xi  /= r;
16762a38674SMatthew G. Knepley     eta /= r;
16862a38674SMatthew G. Knepley   }
16962a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
17062a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
17162a38674SMatthew G. Knepley   PetscFunctionReturn(0);
17262a38674SMatthew G. Knepley }
17362a38674SMatthew G. Knepley 
174ba2698f1SMatthew G. Knepley static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
175ccd2543fSMatthew G Knepley {
176ccd2543fSMatthew G Knepley   PetscSection       coordSection;
177ccd2543fSMatthew G Knepley   Vec             coordsLocal;
178a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
179ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
180ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
181ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
182ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
183ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
184ccd2543fSMatthew G Knepley 
185ccd2543fSMatthew G Knepley   PetscFunctionBegin;
186ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
18769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
188ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
189ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
190ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
191ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
192ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
193ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
194ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
195ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
196ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
197ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
198ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
199ccd2543fSMatthew G Knepley   }
200ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
201c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
202ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
203ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
204ccd2543fSMatthew G Knepley }
205ccd2543fSMatthew G Knepley 
206ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
207ccd2543fSMatthew G Knepley {
208ccd2543fSMatthew G Knepley   const PetscInt embedDim = 3;
209ccd2543fSMatthew G Knepley   PetscReal      v0[3], J[9], invJ[9], detJ;
210ccd2543fSMatthew G Knepley   PetscReal      x = PetscRealPart(point[0]);
211ccd2543fSMatthew G Knepley   PetscReal      y = PetscRealPart(point[1]);
212ccd2543fSMatthew G Knepley   PetscReal      z = PetscRealPart(point[2]);
213ccd2543fSMatthew G Knepley   PetscReal      xi, eta, zeta;
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]) + invJ[0*embedDim+2]*(z - v0[2]);
219ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
220ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
221ccd2543fSMatthew G Knepley 
222ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c;
223c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
224ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
225ccd2543fSMatthew G Knepley }
226ccd2543fSMatthew G Knepley 
227ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
228ccd2543fSMatthew G Knepley {
229ccd2543fSMatthew G Knepley   PetscSection   coordSection;
230ccd2543fSMatthew G Knepley   Vec            coordsLocal;
231872a9804SMatthew G. Knepley   PetscScalar   *coords = NULL;
232fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
233fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
234ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
235ccd2543fSMatthew G Knepley   PetscInt       f;
236ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
237ccd2543fSMatthew G Knepley 
238ccd2543fSMatthew G Knepley   PetscFunctionBegin;
239ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
24069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
241ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
242ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
243ccd2543fSMatthew G Knepley     /* Check the point is under plane */
244ccd2543fSMatthew G Knepley     /*   Get face normal */
245ccd2543fSMatthew G Knepley     PetscReal v_i[3];
246ccd2543fSMatthew G Knepley     PetscReal v_j[3];
247ccd2543fSMatthew G Knepley     PetscReal normal[3];
248ccd2543fSMatthew G Knepley     PetscReal pp[3];
249ccd2543fSMatthew G Knepley     PetscReal dot;
250ccd2543fSMatthew G Knepley 
251ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
252ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
253ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
254ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
255ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
256ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
257ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
258ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
259ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
260ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
261ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
262ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
263ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
264ccd2543fSMatthew G Knepley 
265ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
266ccd2543fSMatthew G Knepley     if (dot < 0.0) {
267ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
268ccd2543fSMatthew G Knepley       break;
269ccd2543fSMatthew G Knepley     }
270ccd2543fSMatthew G Knepley   }
271ccd2543fSMatthew G Knepley   if (found) *cell = c;
272c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
273ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
274ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
275ccd2543fSMatthew G Knepley }
276ccd2543fSMatthew G Knepley 
277c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
278c4eade1cSMatthew G. Knepley {
279c4eade1cSMatthew G. Knepley   PetscInt d;
280c4eade1cSMatthew G. Knepley 
281c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
282c4eade1cSMatthew G. Knepley   box->dim = dim;
283c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
284c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
285c4eade1cSMatthew G. Knepley }
286c4eade1cSMatthew G. Knepley 
287c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
288c4eade1cSMatthew G. Knepley {
289c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
290c4eade1cSMatthew G. Knepley 
291c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
292c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
293c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
294c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
295c4eade1cSMatthew G. Knepley }
296c4eade1cSMatthew G. Knepley 
297c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
298c4eade1cSMatthew G. Knepley {
299c4eade1cSMatthew G. Knepley   PetscInt d;
300c4eade1cSMatthew G. Knepley 
301c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
302c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
303c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
304c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
305c4eade1cSMatthew G. Knepley   }
306c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
307c4eade1cSMatthew G. Knepley }
308c4eade1cSMatthew G. Knepley 
30962a38674SMatthew G. Knepley /*
31062a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
31162a38674SMatthew G. Knepley 
31262a38674SMatthew G. Knepley   Not collective
31362a38674SMatthew G. Knepley 
31462a38674SMatthew G. Knepley   Input Parameters:
31562a38674SMatthew G. Knepley + box - The grid hash object
31662a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
31762a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
31862a38674SMatthew G. Knepley 
31962a38674SMatthew G. Knepley   Level: developer
32062a38674SMatthew G. Knepley 
32162a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
32262a38674SMatthew G. Knepley */
323c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
324c4eade1cSMatthew G. Knepley {
325c4eade1cSMatthew G. Knepley   PetscInt d;
326c4eade1cSMatthew G. Knepley 
327c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
328c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
329c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
330c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
331c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
332c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
333c4eade1cSMatthew G. Knepley     } else {
334c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
335c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
336c4eade1cSMatthew G. Knepley     }
337c4eade1cSMatthew G. Knepley   }
338c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
339c4eade1cSMatthew G. Knepley }
340c4eade1cSMatthew G. Knepley 
34162a38674SMatthew G. Knepley /*
34262a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
34362a38674SMatthew G. Knepley 
34462a38674SMatthew G. Knepley   Not collective
34562a38674SMatthew G. Knepley 
34662a38674SMatthew G. Knepley   Input Parameters:
34762a38674SMatthew G. Knepley + box       - The grid hash object
34862a38674SMatthew G. Knepley . numPoints - The number of input points
34962a38674SMatthew G. Knepley - points    - The input point coordinates
35062a38674SMatthew G. Knepley 
35162a38674SMatthew G. Knepley   Output Parameters:
35262a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
35362a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
35462a38674SMatthew G. Knepley 
35562a38674SMatthew G. Knepley   Level: developer
35662a38674SMatthew G. Knepley 
35762a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
35862a38674SMatthew G. Knepley */
3591c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
360c4eade1cSMatthew G. Knepley {
361c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
362c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
363c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
364c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
365c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
366c4eade1cSMatthew G. Knepley   PetscInt         d, p;
367c4eade1cSMatthew G. Knepley 
368c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
369c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
370c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
3711c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
372c4eade1cSMatthew G. Knepley 
3731c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
3742a705cacSMatthew G. Knepley       if (dbox == -1   && PetscAbsReal(PetscRealPart(points[p*dim+d]) - lower[d]) < 1.0e-9) dbox = 0;
375c4eade1cSMatthew 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",
376087ef6b2SMatthew 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);
377c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
378c4eade1cSMatthew G. Knepley     }
379c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
380c4eade1cSMatthew G. Knepley   }
381c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
382c4eade1cSMatthew G. Knepley }
383c4eade1cSMatthew G. Knepley 
384af74b616SDave May /*
385af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
386af74b616SDave May 
387af74b616SDave May  Not collective
388af74b616SDave May 
389af74b616SDave May   Input Parameters:
390af74b616SDave May + box       - The grid hash object
391af74b616SDave May . numPoints - The number of input points
392af74b616SDave May - points    - The input point coordinates
393af74b616SDave May 
394af74b616SDave May   Output Parameters:
395af74b616SDave May + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
396af74b616SDave May . boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
397af74b616SDave May - found     - Flag indicating if point was located within a box
398af74b616SDave May 
399af74b616SDave May   Level: developer
400af74b616SDave May 
401af74b616SDave May .seealso: PetscGridHashGetEnclosingBox()
402af74b616SDave May */
403af74b616SDave May PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[],PetscBool *found)
404af74b616SDave May {
405af74b616SDave May   const PetscReal *lower = box->lower;
406af74b616SDave May   const PetscReal *upper = box->upper;
407af74b616SDave May   const PetscReal *h     = box->h;
408af74b616SDave May   const PetscInt  *n     = box->n;
409af74b616SDave May   const PetscInt   dim   = box->dim;
410af74b616SDave May   PetscInt         d, p;
411af74b616SDave May 
412af74b616SDave May   PetscFunctionBegin;
413af74b616SDave May   *found = PETSC_FALSE;
414af74b616SDave May   for (p = 0; p < numPoints; ++p) {
415af74b616SDave May     for (d = 0; d < dim; ++d) {
416af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
417af74b616SDave May 
418af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
419af74b616SDave May       if (dbox < 0 || dbox >= n[d]) {
420af74b616SDave May         PetscFunctionReturn(0);
421af74b616SDave May       }
422af74b616SDave May       dboxes[p*dim+d] = dbox;
423af74b616SDave May     }
424af74b616SDave May     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
425af74b616SDave May   }
426af74b616SDave May   *found = PETSC_TRUE;
427af74b616SDave May   PetscFunctionReturn(0);
428af74b616SDave May }
429af74b616SDave May 
430c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
431c4eade1cSMatthew G. Knepley {
432c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
433c4eade1cSMatthew G. Knepley 
434c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
435c4eade1cSMatthew G. Knepley   if (*box) {
436c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
437c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
438c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
439c4eade1cSMatthew G. Knepley   }
440c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
441c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
442c4eade1cSMatthew G. Knepley }
443c4eade1cSMatthew G. Knepley 
444cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
445cafe43deSMatthew G. Knepley {
446ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
447cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
448cafe43deSMatthew G. Knepley 
449cafe43deSMatthew G. Knepley   PetscFunctionBegin;
450ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cellStart, &ct);CHKERRQ(ierr);
451ba2698f1SMatthew G. Knepley   switch (ct) {
452ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
453ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
454ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
455ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
456ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
457ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
458ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
459ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
460ba2698f1SMatthew G. Knepley     default: SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %D with type %s", cellStart, DMPolytopeTypes[ct]);
461cafe43deSMatthew G. Knepley   }
462cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
463cafe43deSMatthew G. Knepley }
464cafe43deSMatthew G. Knepley 
46562a38674SMatthew G. Knepley /*
46662a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
46762a38674SMatthew G. Knepley */
46862a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
46962a38674SMatthew G. Knepley {
470ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
47162a38674SMatthew G. Knepley   PetscErrorCode ierr;
47262a38674SMatthew G. Knepley 
47362a38674SMatthew G. Knepley   PetscFunctionBegin;
474ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
475ba2698f1SMatthew G. Knepley   switch (ct) {
476ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
477ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
47862a38674SMatthew G. Knepley #if 0
479ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
480ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
481ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
482ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
483ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
484ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
48562a38674SMatthew G. Knepley #endif
486ba2698f1SMatthew G. Knepley     default: SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %D with type %s", cell, DMPolytopeTypes[ct]);
48762a38674SMatthew G. Knepley   }
48862a38674SMatthew G. Knepley   PetscFunctionReturn(0);
48962a38674SMatthew G. Knepley }
49062a38674SMatthew G. Knepley 
49162a38674SMatthew G. Knepley /*
49262a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
49362a38674SMatthew G. Knepley 
494d083f849SBarry Smith   Collective on dm
49562a38674SMatthew G. Knepley 
49662a38674SMatthew G. Knepley   Input Parameter:
49762a38674SMatthew G. Knepley . dm - The Plex
49862a38674SMatthew G. Knepley 
49962a38674SMatthew G. Knepley   Output Parameter:
50062a38674SMatthew G. Knepley . localBox - The grid hash object
50162a38674SMatthew G. Knepley 
50262a38674SMatthew G. Knepley   Level: developer
50362a38674SMatthew G. Knepley 
50462a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
50562a38674SMatthew G. Knepley */
506cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
507cafe43deSMatthew G. Knepley {
508cafe43deSMatthew G. Knepley   MPI_Comm           comm;
509cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
510cafe43deSMatthew G. Knepley   Vec                coordinates;
511cafe43deSMatthew G. Knepley   PetscSection       coordSection;
512cafe43deSMatthew G. Knepley   Vec                coordsLocal;
513cafe43deSMatthew G. Knepley   const PetscScalar *coords;
514722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
515cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
516412e9a14SMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, c, i;
517cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
518cafe43deSMatthew G. Knepley 
519cafe43deSMatthew G. Knepley   PetscFunctionBegin;
520cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
521cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
522cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
5235b3353d8SMatthew G. Knepley   if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D");
524cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
525cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
526cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
527cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
528cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
5299cb35068SDave May   ierr = PetscOptionsGetInt(NULL,NULL,"-dm_plex_hash_box_nijk",&n[0],NULL);CHKERRQ(ierr);
5309cb35068SDave May   n[1] = n[0];
5319cb35068SDave May   n[2] = n[0];
532cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
533cafe43deSMatthew G. Knepley #if 0
534cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
535b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
536b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
537cafe43deSMatthew G. Knepley #endif
538cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
539cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
540cafe43deSMatthew G. Knepley   /* Create label */
541412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
542d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse);CHKERRQ(ierr);
543cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
544a8d69d7bSBarry Smith   /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
545cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
546cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
54738353de4SMatthew G. Knepley   ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr);
548cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
549cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
550cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
55138353de4SMatthew G. Knepley     PetscInt         csize   = 0;
552cafe43deSMatthew G. Knepley     PetscScalar      point[3];
553cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
554cafe43deSMatthew G. Knepley 
555cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
55638353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
55738353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
558722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
55938353de4SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);}
560cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
561cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
5622291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
563cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
564cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
565cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
566cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
567cafe43deSMatthew G. Knepley       }
568cafe43deSMatthew G. Knepley     }
569fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
570cafe43deSMatthew 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]) {
571cafe43deSMatthew 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]) {
572cafe43deSMatthew 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]) {
573cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
574cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
575fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
576cafe43deSMatthew G. Knepley 
577fea14342SMatthew G. Knepley           /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
578cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
579cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
580cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
581cafe43deSMatthew G. Knepley 
582cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
583367003a6SStefano Zampini                 if (cell >= 0) { ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
584cafe43deSMatthew G. Knepley               }
585cafe43deSMatthew G. Knepley             }
586cafe43deSMatthew G. Knepley           }
587fea14342SMatthew G. Knepley           /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */
588fea14342SMatthew G. Knepley           for (edge = 0; edge < dim+1; ++edge) {
589fea14342SMatthew G. Knepley             PetscReal segA[6], segB[6];
590fea14342SMatthew G. Knepley 
591aab5bcd8SJed Brown             if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected dim %d > 3",dim);
592fea14342SMatthew G. Knepley             for (d = 0; d < dim; ++d) {segA[d] = PetscRealPart(ccoords[edge*dim+d]); segA[dim+d] = PetscRealPart(ccoords[((edge+1)%(dim+1))*dim+d]);}
593fea14342SMatthew G. Knepley             for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) {
5949a128ed2SMatthew G. Knepley               if (dim > 2) {segB[2]     = PetscRealPart(point[2]);
5959a128ed2SMatthew G. Knepley                             segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];}
596fea14342SMatthew G. Knepley               for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) {
5979a128ed2SMatthew G. Knepley                 if (dim > 1) {segB[1]     = PetscRealPart(point[1]);
5989a128ed2SMatthew G. Knepley                               segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];}
599fea14342SMatthew G. Knepley                 for (ii = 0; ii < 2; ++ii) {
600fea14342SMatthew G. Knepley                   PetscBool intersects;
601fea14342SMatthew G. Knepley 
6029a128ed2SMatthew G. Knepley                   segB[0]     = PetscRealPart(point[0]);
6039a128ed2SMatthew G. Knepley                   segB[dim+0] = PetscRealPart(point[0]) + ii*h[0];
604fea14342SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
605367003a6SStefano Zampini                   if (intersects) { ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;}
606cafe43deSMatthew G. Knepley                 }
607cafe43deSMatthew G. Knepley               }
608cafe43deSMatthew G. Knepley             }
609cafe43deSMatthew G. Knepley           }
610fea14342SMatthew G. Knepley         }
611fea14342SMatthew G. Knepley       }
612fea14342SMatthew G. Knepley     }
613fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
614fea14342SMatthew G. Knepley   }
615722d0f5cSMatthew G. Knepley   ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr);
616cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
617cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
618cafe43deSMatthew G. Knepley   *localBox = lbox;
619cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
620cafe43deSMatthew G. Knepley }
621cafe43deSMatthew G. Knepley 
62262a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
623ccd2543fSMatthew G Knepley {
624cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
625af74b616SDave May   PetscBool       hash = mesh->useHashLocation, reuse = PETSC_FALSE;
6263a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
627412e9a14SMatthew G. Knepley   PetscInt        dim, cStart, cEnd, numCells, c, d;
628cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
6293a93e3b7SToby Isaac   PetscSFNode    *cells;
630ccd2543fSMatthew G Knepley   PetscScalar    *a;
6313a93e3b7SToby Isaac   PetscMPIInt     result;
632af74b616SDave May   PetscLogDouble  t0,t1;
6339cb35068SDave May   PetscReal       gmin[3],gmax[3];
6349cb35068SDave May   PetscInt        terminating_query_type[] = { 0, 0, 0 };
635ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
636ccd2543fSMatthew G Knepley 
637ccd2543fSMatthew G Knepley   PetscFunctionBegin;
638af74b616SDave May   ierr = PetscTime(&t0);CHKERRQ(ierr);
639080342d1SMatthew 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.");
640cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
641cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
6423a93e3b7SToby Isaac   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr);
6433a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
644cafe43deSMatthew 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);
645412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
646ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
647ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
648ccd2543fSMatthew G Knepley   numPoints /= bs;
649af74b616SDave May   {
650af74b616SDave May     const PetscSFNode *sf_cells;
651af74b616SDave May 
652af74b616SDave May     ierr = PetscSFGetGraph(cellSF,NULL,NULL,NULL,&sf_cells);CHKERRQ(ierr);
653af74b616SDave May     if (sf_cells) {
654af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Re-using existing StarForest node list\n");CHKERRQ(ierr);
655af74b616SDave May       cells = (PetscSFNode*)sf_cells;
656af74b616SDave May       reuse = PETSC_TRUE;
657af74b616SDave May     } else {
658af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n");CHKERRQ(ierr);
659785e854fSJed Brown       ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
660af74b616SDave May       /* initialize cells if created */
661af74b616SDave May       for (p=0; p<numPoints; p++) {
662af74b616SDave May         cells[p].rank  = 0;
663af74b616SDave May         cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
664af74b616SDave May       }
665af74b616SDave May     }
666af74b616SDave May   }
6679cb35068SDave May   /* define domain bounding box */
6689cb35068SDave May   {
6699cb35068SDave May     Vec coorglobal;
6709cb35068SDave May 
6719cb35068SDave May     ierr = DMGetCoordinates(dm,&coorglobal);CHKERRQ(ierr);
6729cb35068SDave May     ierr = VecStrideMaxAll(coorglobal,NULL,gmax);CHKERRQ(ierr);
6739cb35068SDave May     ierr = VecStrideMinAll(coorglobal,NULL,gmin);CHKERRQ(ierr);
6749cb35068SDave May   }
675953fc75cSMatthew G. Knepley   if (hash) {
676ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
677cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
678cafe43deSMatthew G. Knepley     /* Send points to correct process */
679cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
680cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
681cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
682953fc75cSMatthew G. Knepley   }
6833a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
684ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
685e56f9228SJed Brown     PetscInt           dbin[3] = {-1,-1,-1}, bin, cell = -1, cellOffset;
6869cb35068SDave May     PetscBool          point_outside_domain = PETSC_FALSE;
687ccd2543fSMatthew G Knepley 
6889cb35068SDave May     /* check bounding box of domain */
6899cb35068SDave May     for (d=0; d<dim; d++) {
690a5f152d1SDave May       if (PetscRealPart(point[d]) < gmin[d]) { point_outside_domain = PETSC_TRUE; break; }
691a5f152d1SDave May       if (PetscRealPart(point[d]) > gmax[d]) { point_outside_domain = PETSC_TRUE; break; }
6929cb35068SDave May     }
6939cb35068SDave May     if (point_outside_domain) {
694e9b685f5SMatthew G. Knepley       cells[p].rank = 0;
695e9b685f5SMatthew G. Knepley       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
6969cb35068SDave May       terminating_query_type[0]++;
6979cb35068SDave May       continue;
6989cb35068SDave May     }
699ccd2543fSMatthew G Knepley 
700af74b616SDave May     /* check initial values in cells[].index - abort early if found */
701af74b616SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
702af74b616SDave May       c = cells[p].index;
7033a93e3b7SToby Isaac       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
704af74b616SDave May       ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
705af74b616SDave May       if (cell >= 0) {
706af74b616SDave May         cells[p].rank = 0;
707af74b616SDave May         cells[p].index = cell;
708af74b616SDave May         numFound++;
709af74b616SDave May       }
710af74b616SDave May     }
7119cb35068SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
7129cb35068SDave May       terminating_query_type[1]++;
7139cb35068SDave May       continue;
7149cb35068SDave May     }
715af74b616SDave May 
716953fc75cSMatthew G. Knepley     if (hash) {
717af74b616SDave May       PetscBool found_box;
718af74b616SDave May 
719af74b616SDave May       /* allow for case that point is outside box - abort early */
720af74b616SDave May       ierr = PetscGridHashGetEnclosingBoxQuery(mesh->lbox, 1, point, dbin, &bin,&found_box);CHKERRQ(ierr);
721af74b616SDave May       if (found_box) {
722cafe43deSMatthew G. Knepley         /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
723cafe43deSMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
724cafe43deSMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
725cafe43deSMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
726cafe43deSMatthew G. Knepley           ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
7273a93e3b7SToby Isaac           if (cell >= 0) {
7283a93e3b7SToby Isaac             cells[p].rank = 0;
7293a93e3b7SToby Isaac             cells[p].index = cell;
7303a93e3b7SToby Isaac             numFound++;
7319cb35068SDave May             terminating_query_type[2]++;
7323a93e3b7SToby Isaac             break;
733ccd2543fSMatthew G Knepley           }
7343a93e3b7SToby Isaac         }
735af74b616SDave May       }
736953fc75cSMatthew G. Knepley     } else {
737953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
738953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
7393a93e3b7SToby Isaac         if (cell >= 0) {
7403a93e3b7SToby Isaac           cells[p].rank = 0;
7413a93e3b7SToby Isaac           cells[p].index = cell;
7423a93e3b7SToby Isaac           numFound++;
7439cb35068SDave May           terminating_query_type[2]++;
7443a93e3b7SToby Isaac           break;
745953fc75cSMatthew G. Knepley         }
746953fc75cSMatthew G. Knepley       }
7473a93e3b7SToby Isaac     }
748ccd2543fSMatthew G Knepley   }
749953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
75062a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
75162a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
75262a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
75362a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
754e56f9228SJed Brown       PetscInt           dbin[3] = {-1,-1,-1}, bin, cellOffset, d;
75562a38674SMatthew G. Knepley 
756e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
75762a38674SMatthew G. Knepley         ++numFound;
75862a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
75962a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
76062a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
76162a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
76262a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
763b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
76462a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
76562a38674SMatthew G. Knepley           if (dist < distMax) {
76662a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
76762a38674SMatthew G. Knepley             cells[p].rank  = 0;
76862a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
76962a38674SMatthew G. Knepley             distMax = dist;
77062a38674SMatthew G. Knepley             break;
77162a38674SMatthew G. Knepley           }
77262a38674SMatthew G. Knepley         }
77362a38674SMatthew G. Knepley       }
77462a38674SMatthew G. Knepley     }
77562a38674SMatthew G. Knepley   }
77662a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
777cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
7782d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
7793a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
7803a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
7813a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
7823a93e3b7SToby Isaac         if (numFound < p) {
7833a93e3b7SToby Isaac           cells[numFound] = cells[p];
7843a93e3b7SToby Isaac         }
7853a93e3b7SToby Isaac         found[numFound++] = p;
7863a93e3b7SToby Isaac       }
7873a93e3b7SToby Isaac     }
7883a93e3b7SToby Isaac   }
78962a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
790af74b616SDave May   if (!reuse) {
7913a93e3b7SToby Isaac     ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
792af74b616SDave May   }
793af74b616SDave May   ierr = PetscTime(&t1);CHKERRQ(ierr);
7949cb35068SDave May   if (hash) {
7952d4ee042Sprj-     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);
7969cb35068SDave May   } else {
7972d4ee042Sprj-     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);
7989cb35068SDave May   }
799af74b616SDave 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);
800ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
801ccd2543fSMatthew G Knepley }
802ccd2543fSMatthew G Knepley 
803741bfc07SMatthew G. Knepley /*@C
804741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
805741bfc07SMatthew G. Knepley 
806741bfc07SMatthew G. Knepley   Not collective
807741bfc07SMatthew G. Knepley 
808741bfc07SMatthew G. Knepley   Input Parameter:
809741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
810741bfc07SMatthew G. Knepley 
811741bfc07SMatthew G. Knepley   Output Parameters:
812741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x
813741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
814741bfc07SMatthew G. Knepley 
815741bfc07SMatthew G. Knepley   Level: developer
816741bfc07SMatthew G. Knepley 
817741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
818741bfc07SMatthew G. Knepley @*/
819741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
82017fe8556SMatthew G. Knepley {
82117fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
82217fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
8238b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
82417fe8556SMatthew G. Knepley 
82517fe8556SMatthew G. Knepley   PetscFunctionBegin;
8261c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
8271c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
82817fe8556SMatthew G. Knepley   coords[0] = 0.0;
8297f07f362SMatthew G. Knepley   coords[1] = r;
83017fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
83117fe8556SMatthew G. Knepley }
83217fe8556SMatthew G. Knepley 
833741bfc07SMatthew G. Knepley /*@C
834741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
83528dbe442SToby Isaac 
836741bfc07SMatthew G. Knepley   Not collective
83728dbe442SToby Isaac 
838741bfc07SMatthew G. Knepley   Input Parameter:
839741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
840741bfc07SMatthew G. Knepley 
841741bfc07SMatthew G. Knepley   Output Parameters:
842741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x and z
843741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
844741bfc07SMatthew G. Knepley 
845741bfc07SMatthew 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
846741bfc07SMatthew G. Knepley 
847741bfc07SMatthew G. Knepley   Level: developer
848741bfc07SMatthew G. Knepley 
849741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
850741bfc07SMatthew G. Knepley @*/
851741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
85228dbe442SToby Isaac {
85328dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
85428dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
85528dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
85628dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
85728dbe442SToby Isaac   PetscReal      rinv = 1. / r;
85828dbe442SToby Isaac   PetscFunctionBegin;
85928dbe442SToby Isaac 
86028dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
86128dbe442SToby Isaac   if (x > 0.) {
86228dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
86328dbe442SToby Isaac 
86428dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
86528dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
86628dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
86728dbe442SToby Isaac   }
86828dbe442SToby Isaac   else {
86928dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
87028dbe442SToby Isaac 
87128dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
87228dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
87328dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
87428dbe442SToby Isaac   }
87528dbe442SToby Isaac   coords[0] = 0.0;
87628dbe442SToby Isaac   coords[1] = r;
87728dbe442SToby Isaac   PetscFunctionReturn(0);
87828dbe442SToby Isaac }
87928dbe442SToby Isaac 
880741bfc07SMatthew G. Knepley /*@
881741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto2D - Rewrite coordinates to be the 2D projection of the 3D coordinates
882741bfc07SMatthew G. Knepley 
883741bfc07SMatthew G. Knepley   Not collective
884741bfc07SMatthew G. Knepley 
885741bfc07SMatthew G. Knepley   Input Parameter:
886741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
887741bfc07SMatthew G. Knepley 
888741bfc07SMatthew G. Knepley   Output Parameters:
889741bfc07SMatthew G. Knepley + coords - The new y- and z-coordinates, and 0 for x
890741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
891741bfc07SMatthew G. Knepley 
892741bfc07SMatthew G. Knepley   Level: developer
893741bfc07SMatthew G. Knepley 
894741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
895741bfc07SMatthew G. Knepley @*/
896741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
897ccd2543fSMatthew G Knepley {
8981ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
89999dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
9004a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
901ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
90299dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
903ccd2543fSMatthew G Knepley 
904ccd2543fSMatthew G Knepley   PetscFunctionBegin;
905ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
906ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
9071ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
9081ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
909ccd2543fSMatthew G Knepley   }
910ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
911ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
912ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
9138b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
914ccd2543fSMatthew G Knepley   n[0] /= norm;
915ccd2543fSMatthew G Knepley   n[1] /= norm;
916ccd2543fSMatthew G Knepley   n[2] /= norm;
917ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
918ccd2543fSMatthew G Knepley 
919ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
920ccd2543fSMatthew G Knepley 
921ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
922ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
923ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
924ccd2543fSMatthew G Knepley 
925ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
926ccd2543fSMatthew G Knepley   */
9278b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
92873868372SMatthew G. Knepley   /* Check for n = z */
92973868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
9307df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
9317df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
93299dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
93399dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
9347df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
93573868372SMatthew G. Knepley     }
93699dec3a6SMatthew G. Knepley     coords[0] = 0.0;
93799dec3a6SMatthew G. Knepley     coords[1] = 0.0;
9387df32b8bSSanderA     coords[2] = x1[0];
9397df32b8bSSanderA     coords[3] = x1[1] * s;
9407df32b8bSSanderA     coords[4] = x2[0];
9417df32b8bSSanderA     coords[5] = x2[1] * s;
9427df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
9437df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
9447df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
94573868372SMatthew G. Knepley     PetscFunctionReturn(0);
94673868372SMatthew G. Knepley   }
947da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
948ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
949ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
950ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
951ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
952ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
953ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
954ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
955ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
956ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
957ccd2543fSMatthew G Knepley     }
958ccd2543fSMatthew G Knepley   }
95948120919SToby Isaac   if (PetscAbsReal(x1p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
96048120919SToby Isaac   if (PetscAbsReal(x2p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
961ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
96299dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
96399dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
96499dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
96599dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
96699dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
96799dec3a6SMatthew G. Knepley       }
96899dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
96999dec3a6SMatthew G. Knepley     }
97099dec3a6SMatthew G. Knepley   }
971ccd2543fSMatthew G Knepley   coords[0] = 0.0;
972ccd2543fSMatthew G Knepley   coords[1] = 0.0;
973ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
974ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
975ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
976ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
9777f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
9787f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
9797f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
9807f07f362SMatthew G. Knepley       PetscReal tmp;
9817f07f362SMatthew G. Knepley 
9827f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
9837f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
9847f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
9857f07f362SMatthew G. Knepley     }
9867f07f362SMatthew G. Knepley   }
987ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
988ccd2543fSMatthew G Knepley }
989ccd2543fSMatthew G Knepley 
9906322fe33SJed Brown PETSC_UNUSED
991834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
992834e62ceSMatthew G. Knepley {
993834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
994834e62ceSMatthew G. Knepley 
995834e62ceSMatthew G. Knepley    |  1  1  1 |
996834e62ceSMatthew G. Knepley    | x0 x1 x2 |
997834e62ceSMatthew G. Knepley    | y0 y1 y2 |
998834e62ceSMatthew G. Knepley 
999834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
1000834e62ceSMatthew G. Knepley 
1001834e62ceSMatthew G. Knepley    | x1 x2 |
1002834e62ceSMatthew G. Knepley    | y1 y2 |
1003834e62ceSMatthew G. Knepley   */
1004834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
1005834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
1006834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
1007834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
100886623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
1009923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
1010834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
10113bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
1012834e62ceSMatthew G. Knepley }
1013834e62ceSMatthew G. Knepley 
1014834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
1015834e62ceSMatthew G. Knepley {
1016923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
1017834e62ceSMatthew G. Knepley   *vol *= 0.5;
1018834e62ceSMatthew G. Knepley }
1019834e62ceSMatthew G. Knepley 
10206322fe33SJed Brown PETSC_UNUSED
1021834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
1022834e62ceSMatthew G. Knepley {
1023834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
1024834e62ceSMatthew G. Knepley 
1025834e62ceSMatthew G. Knepley    |  1  1  1  1 |
1026834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
1027834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
1028834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
1029834e62ceSMatthew G. Knepley 
1030834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
1031834e62ceSMatthew G. Knepley 
1032834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1033834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1034834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1035834e62ceSMatthew G. Knepley   */
1036834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
1037834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
1038834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
10390a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1040834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
1041834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
1042834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
1043834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
1044923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
10450a3da2c2SToby Isaac   *vol = -onesixth*detM;
10463bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1047834e62ceSMatthew G. Knepley }
1048834e62ceSMatthew G. Knepley 
10490ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
10500ec8681fSMatthew G. Knepley {
10510a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1052923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
10530a3da2c2SToby Isaac   *vol *= -onesixth;
10540ec8681fSMatthew G. Knepley }
10550ec8681fSMatthew G. Knepley 
1056cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1057cb92db44SToby Isaac {
1058cb92db44SToby Isaac   PetscSection   coordSection;
1059cb92db44SToby Isaac   Vec            coordinates;
1060cb92db44SToby Isaac   const PetscScalar *coords;
1061cb92db44SToby Isaac   PetscInt       dim, d, off;
1062cb92db44SToby Isaac   PetscErrorCode ierr;
1063cb92db44SToby Isaac 
1064cb92db44SToby Isaac   PetscFunctionBegin;
1065cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1066cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1067cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
1068cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
1069cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
1070cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
1071cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
1072cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
1073cb92db44SToby Isaac   *detJ = 1.;
1074cb92db44SToby Isaac   if (J) {
1075cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1076cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1077cb92db44SToby Isaac     if (invJ) {
1078cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1079cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1080cb92db44SToby Isaac     }
1081cb92db44SToby Isaac   }
1082cb92db44SToby Isaac   PetscFunctionReturn(0);
1083cb92db44SToby Isaac }
1084cb92db44SToby Isaac 
108517fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
108617fe8556SMatthew G. Knepley {
108717fe8556SMatthew G. Knepley   PetscSection   coordSection;
108817fe8556SMatthew G. Knepley   Vec            coordinates;
1089a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10908bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
109117fe8556SMatthew G. Knepley   PetscErrorCode ierr;
109217fe8556SMatthew G. Knepley 
109317fe8556SMatthew G. Knepley   PetscFunctionBegin;
109417fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
109569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10968bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10978bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
109817fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
10998bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1100adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
11017f07f362SMatthew G. Knepley   *detJ = 0.0;
110228dbe442SToby Isaac   if (numCoords == 6) {
110328dbe442SToby Isaac     const PetscInt dim = 3;
110428dbe442SToby Isaac     PetscReal      R[9], J0;
110528dbe442SToby Isaac 
110628dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1107741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
110828dbe442SToby Isaac     if (J)    {
110928dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
111028dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
111128dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
111228dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
111328dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
111428dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1115adac9986SMatthew G. Knepley     }
111628dbe442SToby Isaac   } else if (numCoords == 4) {
11177f07f362SMatthew G. Knepley     const PetscInt dim = 2;
11187f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
11197f07f362SMatthew G. Knepley 
11207f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1121741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
112217fe8556SMatthew G. Knepley     if (J)    {
11237f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
11247f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
11257f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
1126923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1127923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1128adac9986SMatthew G. Knepley     }
11297f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
11307f07f362SMatthew G. Knepley     const PetscInt dim = 1;
11317f07f362SMatthew G. Knepley 
11327f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
11337f07f362SMatthew G. Knepley     if (J)    {
11347f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
113517fe8556SMatthew G. Knepley       *detJ = J[0];
11363bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
11373bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
1138adac9986SMatthew G. Knepley     }
1139796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
114017fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
114117fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
114217fe8556SMatthew G. Knepley }
114317fe8556SMatthew G. Knepley 
1144ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1145ccd2543fSMatthew G Knepley {
1146ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1147ccd2543fSMatthew G Knepley   Vec            coordinates;
1148a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
114903d7ed2eSStefano Zampini   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1150ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1151ccd2543fSMatthew G Knepley 
1152ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1153ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
115469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
115503d7ed2eSStefano Zampini   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
115603d7ed2eSStefano Zampini   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
1157ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
115803d7ed2eSStefano Zampini   numCoords = numSelfCoords ? numSelfCoords : numCoords;
11597f07f362SMatthew G. Knepley   *detJ = 0.0;
1160ccd2543fSMatthew G Knepley   if (numCoords == 9) {
11617f07f362SMatthew G. Knepley     const PetscInt dim = 3;
11627f07f362SMatthew 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};
11637f07f362SMatthew G. Knepley 
11647f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1165741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
11667f07f362SMatthew G. Knepley     if (J)    {
1167b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1168b7ad821dSMatthew G. Knepley 
1169b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1170b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1171b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1172ccd2543fSMatthew G Knepley         }
11737f07f362SMatthew G. Knepley       }
11743bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1175923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
11767f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
11777f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
11787f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
11797f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
11807f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
11817f07f362SMatthew G. Knepley           }
11827f07f362SMatthew G. Knepley         }
11837f07f362SMatthew G. Knepley       }
11843bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
11857f07f362SMatthew G. Knepley     }
1186923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
11877f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
11887f07f362SMatthew G. Knepley     const PetscInt dim = 2;
11897f07f362SMatthew G. Knepley 
11907f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1191ccd2543fSMatthew G Knepley     if (J)    {
1192ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1193ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1194ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1195ccd2543fSMatthew G Knepley         }
1196ccd2543fSMatthew G Knepley       }
11973bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1198923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1199ccd2543fSMatthew G Knepley     }
1200923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1201796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1202ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1203ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1204ccd2543fSMatthew G Knepley }
1205ccd2543fSMatthew G Knepley 
1206412e9a14SMatthew 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)
1207ccd2543fSMatthew G Knepley {
1208ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1209ccd2543fSMatthew G Knepley   Vec            coordinates;
1210a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
12110d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1212ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1213ccd2543fSMatthew G Knepley 
1214ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1215ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
121669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
12170d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
12180d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
121999dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
122071f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1221dfccc68fSToby Isaac   if (!Nq) {
1222412e9a14SMatthew G. Knepley     PetscInt vorder[4] = {0, 1, 2, 3};
1223412e9a14SMatthew G. Knepley 
1224412e9a14SMatthew G. Knepley     if (isTensor) {vorder[2] = 3; vorder[3] = 2;}
12257f07f362SMatthew G. Knepley     *detJ = 0.0;
122699dec3a6SMatthew G. Knepley     if (numCoords == 12) {
122799dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
122899dec3a6SMatthew 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};
122999dec3a6SMatthew G. Knepley 
1230dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1231741bfc07SMatthew G. Knepley       ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
123299dec3a6SMatthew G. Knepley       if (J)    {
123399dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
123499dec3a6SMatthew G. Knepley 
123599dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
1236412e9a14SMatthew G. Knepley           J0[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*pdim+d]) - PetscRealPart(coords[vorder[0]*pdim+d]));
1237412e9a14SMatthew G. Knepley           J0[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[2]*pdim+d]) - PetscRealPart(coords[vorder[1]*pdim+d]));
123899dec3a6SMatthew G. Knepley         }
12393bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1240923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
124199dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
124299dec3a6SMatthew G. Knepley           for (f = 0; f < dim; f++) {
124399dec3a6SMatthew G. Knepley             J[d*dim+f] = 0.0;
124499dec3a6SMatthew G. Knepley             for (g = 0; g < dim; g++) {
124599dec3a6SMatthew G. Knepley               J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
124699dec3a6SMatthew G. Knepley             }
124799dec3a6SMatthew G. Knepley           }
124899dec3a6SMatthew G. Knepley         }
12493bc0b13bSBarry Smith         ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
125099dec3a6SMatthew G. Knepley       }
1251923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
125271f58de1SToby Isaac     } else if (numCoords == 8) {
125399dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
125499dec3a6SMatthew G. Knepley 
1255dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1256ccd2543fSMatthew G Knepley       if (J)    {
1257ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
1258412e9a14SMatthew G. Knepley           J[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d]));
1259412e9a14SMatthew G. Knepley           J[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[3]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d]));
1260ccd2543fSMatthew G Knepley         }
12613bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1262923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1263ccd2543fSMatthew G Knepley       }
1264923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1265796f034aSJed Brown     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1266dfccc68fSToby Isaac   } else {
1267dfccc68fSToby Isaac     const PetscInt Nv = 4;
1268dfccc68fSToby Isaac     const PetscInt dimR = 2;
1269412e9a14SMatthew G. Knepley     PetscInt  zToPlex[4] = {0, 1, 3, 2};
1270dfccc68fSToby Isaac     PetscReal zOrder[12];
1271dfccc68fSToby Isaac     PetscReal zCoeff[12];
1272dfccc68fSToby Isaac     PetscInt  i, j, k, l, dim;
1273dfccc68fSToby Isaac 
1274412e9a14SMatthew G. Knepley     if (isTensor) {zToPlex[2] = 2; zToPlex[3] = 3;}
1275dfccc68fSToby Isaac     if (numCoords == 12) {
1276dfccc68fSToby Isaac       dim = 3;
1277dfccc68fSToby Isaac     } else if (numCoords == 8) {
1278dfccc68fSToby Isaac       dim = 2;
1279dfccc68fSToby Isaac     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1280dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1281dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1282dfccc68fSToby Isaac 
1283dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1284dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1285dfccc68fSToby Isaac       }
1286dfccc68fSToby Isaac     }
1287dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1288dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1289dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1290dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1291dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1292dfccc68fSToby Isaac     }
1293dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1294dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1295dfccc68fSToby Isaac 
1296dfccc68fSToby Isaac       if (v) {
1297dfccc68fSToby Isaac         PetscReal extPoint[4];
1298dfccc68fSToby Isaac 
1299dfccc68fSToby Isaac         extPoint[0] = 1.;
1300dfccc68fSToby Isaac         extPoint[1] = xi;
1301dfccc68fSToby Isaac         extPoint[2] = eta;
1302dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1303dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1304dfccc68fSToby Isaac           PetscReal val = 0.;
1305dfccc68fSToby Isaac 
1306dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1307dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1308dfccc68fSToby Isaac           }
1309dfccc68fSToby Isaac           v[i * dim + j] = val;
1310dfccc68fSToby Isaac         }
1311dfccc68fSToby Isaac       }
1312dfccc68fSToby Isaac       if (J) {
1313dfccc68fSToby Isaac         PetscReal extJ[8];
1314dfccc68fSToby Isaac 
1315dfccc68fSToby Isaac         extJ[0] = 0.;
1316dfccc68fSToby Isaac         extJ[1] = 0.;
1317dfccc68fSToby Isaac         extJ[2] = 1.;
1318dfccc68fSToby Isaac         extJ[3] = 0.;
1319dfccc68fSToby Isaac         extJ[4] = 0.;
1320dfccc68fSToby Isaac         extJ[5] = 1.;
1321dfccc68fSToby Isaac         extJ[6] = eta;
1322dfccc68fSToby Isaac         extJ[7] = xi;
1323dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1324dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1325dfccc68fSToby Isaac             PetscReal val = 0.;
1326dfccc68fSToby Isaac 
1327dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1328dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1329dfccc68fSToby Isaac             }
1330dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1331dfccc68fSToby Isaac           }
1332dfccc68fSToby Isaac         }
1333dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1334dfccc68fSToby Isaac           PetscReal x, y, z;
1335dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1336dfccc68fSToby Isaac           PetscReal norm;
1337dfccc68fSToby Isaac 
1338dfccc68fSToby Isaac           x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1339dfccc68fSToby Isaac           y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1340dfccc68fSToby Isaac           z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1341dfccc68fSToby Isaac           norm = PetscSqrtReal(x * x + y * y + z * z);
1342dfccc68fSToby Isaac           iJ[2] = x / norm;
1343dfccc68fSToby Isaac           iJ[5] = y / norm;
1344dfccc68fSToby Isaac           iJ[8] = z / norm;
1345dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1346dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1347dfccc68fSToby Isaac         } else {
1348dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1349dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1350dfccc68fSToby Isaac         }
1351dfccc68fSToby Isaac       }
1352dfccc68fSToby Isaac     }
1353dfccc68fSToby Isaac   }
135499dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1355ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1356ccd2543fSMatthew G Knepley }
1357ccd2543fSMatthew G Knepley 
1358ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1359ccd2543fSMatthew G Knepley {
1360ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1361ccd2543fSMatthew G Knepley   Vec            coordinates;
1362a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1363ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
136499dec3a6SMatthew G. Knepley   PetscInt       d;
1365ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1366ccd2543fSMatthew G Knepley 
1367ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1368ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
136969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1370ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
13717f07f362SMatthew G. Knepley   *detJ = 0.0;
13727f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1373ccd2543fSMatthew G Knepley   if (J)    {
1374ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1375f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1376f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1377f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1378f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1379ccd2543fSMatthew G Knepley     }
13803bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1381923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1382ccd2543fSMatthew G Knepley   }
1383923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1384ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1385ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1386ccd2543fSMatthew G Knepley }
1387ccd2543fSMatthew G Knepley 
1388dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1389ccd2543fSMatthew G Knepley {
1390ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1391ccd2543fSMatthew G Knepley   Vec            coordinates;
1392a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1393ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1394ccd2543fSMatthew G Knepley   PetscInt       d;
1395ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1396ccd2543fSMatthew G Knepley 
1397ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1398ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
139969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1400ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1401dfccc68fSToby Isaac   if (!Nq) {
14027f07f362SMatthew G. Knepley     *detJ = 0.0;
1403dfccc68fSToby Isaac     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1404ccd2543fSMatthew G Knepley     if (J)    {
1405ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1406f0df753eSMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1407f0df753eSMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1408f0df753eSMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1409ccd2543fSMatthew G Knepley       }
14103bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1411923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1412ccd2543fSMatthew G Knepley     }
1413923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1414dfccc68fSToby Isaac   } else {
1415dfccc68fSToby Isaac     const PetscInt Nv = 8;
1416dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1417dfccc68fSToby Isaac     const PetscInt dim = 3;
1418dfccc68fSToby Isaac     const PetscInt dimR = 3;
1419dfccc68fSToby Isaac     PetscReal zOrder[24];
1420dfccc68fSToby Isaac     PetscReal zCoeff[24];
1421dfccc68fSToby Isaac     PetscInt  i, j, k, l;
1422dfccc68fSToby Isaac 
1423dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1424dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1425dfccc68fSToby Isaac 
1426dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1427dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1428dfccc68fSToby Isaac       }
1429dfccc68fSToby Isaac     }
1430dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1431dfccc68fSToby 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]);
1432dfccc68fSToby 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]);
1433dfccc68fSToby 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]);
1434dfccc68fSToby 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]);
1435dfccc68fSToby 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]);
1436dfccc68fSToby 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]);
1437dfccc68fSToby 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]);
1438dfccc68fSToby 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]);
1439dfccc68fSToby Isaac     }
1440dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1441dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1442dfccc68fSToby Isaac 
1443dfccc68fSToby Isaac       if (v) {
144491d2b7ceSToby Isaac         PetscReal extPoint[8];
1445dfccc68fSToby Isaac 
1446dfccc68fSToby Isaac         extPoint[0] = 1.;
1447dfccc68fSToby Isaac         extPoint[1] = xi;
1448dfccc68fSToby Isaac         extPoint[2] = eta;
1449dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1450dfccc68fSToby Isaac         extPoint[4] = theta;
1451dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1452dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1453dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1454dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1455dfccc68fSToby Isaac           PetscReal val = 0.;
1456dfccc68fSToby Isaac 
1457dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1458dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1459dfccc68fSToby Isaac           }
1460dfccc68fSToby Isaac           v[i * dim + j] = val;
1461dfccc68fSToby Isaac         }
1462dfccc68fSToby Isaac       }
1463dfccc68fSToby Isaac       if (J) {
1464dfccc68fSToby Isaac         PetscReal extJ[24];
1465dfccc68fSToby Isaac 
1466dfccc68fSToby Isaac         extJ[0]  = 0.         ; extJ[1]  = 0.        ; extJ[2]  = 0.      ;
1467dfccc68fSToby Isaac         extJ[3]  = 1.         ; extJ[4]  = 0.        ; extJ[5]  = 0.      ;
1468dfccc68fSToby Isaac         extJ[6]  = 0.         ; extJ[7]  = 1.        ; extJ[8]  = 0.      ;
1469dfccc68fSToby Isaac         extJ[9]  = eta        ; extJ[10] = xi        ; extJ[11] = 0.      ;
1470dfccc68fSToby Isaac         extJ[12] = 0.         ; extJ[13] = 0.        ; extJ[14] = 1.      ;
1471dfccc68fSToby Isaac         extJ[15] = theta      ; extJ[16] = 0.        ; extJ[17] = xi      ;
1472dfccc68fSToby Isaac         extJ[18] = 0.         ; extJ[19] = theta     ; extJ[20] = eta     ;
1473dfccc68fSToby Isaac         extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi;
1474dfccc68fSToby Isaac 
1475dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1476dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1477dfccc68fSToby Isaac             PetscReal val = 0.;
1478dfccc68fSToby Isaac 
1479dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1480dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1481dfccc68fSToby Isaac             }
1482dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1483dfccc68fSToby Isaac           }
1484dfccc68fSToby Isaac         }
1485dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1486dfccc68fSToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1487dfccc68fSToby Isaac       }
1488dfccc68fSToby Isaac     }
1489dfccc68fSToby Isaac   }
1490ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1491ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1492ccd2543fSMatthew G Knepley }
1493ccd2543fSMatthew G Knepley 
1494dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1495dfccc68fSToby Isaac {
1496ba2698f1SMatthew G. Knepley   DMPolytopeType  ct;
1497dfccc68fSToby Isaac   PetscInt        depth, dim, coordDim, coneSize, i;
1498dfccc68fSToby Isaac   PetscInt        Nq = 0;
1499dfccc68fSToby Isaac   const PetscReal *points = NULL;
1500dfccc68fSToby Isaac   DMLabel         depthLabel;
1501c330f8ffSToby Isaac   PetscReal       xi0[3] = {-1.,-1.,-1.}, v0[3], J0[9], detJ0;
1502dfccc68fSToby Isaac   PetscBool       isAffine = PETSC_TRUE;
1503dfccc68fSToby Isaac   PetscErrorCode  ierr;
1504dfccc68fSToby Isaac 
1505dfccc68fSToby Isaac   PetscFunctionBegin;
1506dfccc68fSToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1507dfccc68fSToby Isaac   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1508dfccc68fSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1509dfccc68fSToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1510dfccc68fSToby Isaac   if (depth == 1 && dim == 1) {
1511dfccc68fSToby Isaac     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1512dfccc68fSToby Isaac   }
1513dfccc68fSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1514dfccc68fSToby Isaac   if (coordDim > 3) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim);
15159c3cf19fSMatthew G. Knepley   if (quad) {ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL);CHKERRQ(ierr);}
1516ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1517ba2698f1SMatthew G. Knepley   switch (ct) {
1518ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_POINT:
1519dfccc68fSToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1520dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1521dfccc68fSToby Isaac     break;
1522ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
1523412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
1524ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1525ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1526dfccc68fSToby Isaac     break;
1527ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
1528ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1529ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1530dfccc68fSToby Isaac     break;
1531ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
1532412e9a14SMatthew G. Knepley     ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1533412e9a14SMatthew G. Knepley     isAffine = PETSC_FALSE;
1534412e9a14SMatthew G. Knepley     break;
1535412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1536412e9a14SMatthew G. Knepley     ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1537dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1538dfccc68fSToby Isaac     break;
1539ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
1540ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1541ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1542dfccc68fSToby Isaac     break;
1543ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
1544dfccc68fSToby Isaac     ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1545dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1546dfccc68fSToby Isaac     break;
1547ba2698f1SMatthew G. Knepley     default: SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No element geometry for cell %D with type %s", cell, DMPolytopeTypes[ct]);
1548dfccc68fSToby Isaac   }
15497318780aSToby Isaac   if (isAffine && Nq) {
1550dfccc68fSToby Isaac     if (v) {
1551dfccc68fSToby Isaac       for (i = 0; i < Nq; i++) {
1552c330f8ffSToby Isaac         CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]);
1553dfccc68fSToby Isaac       }
1554dfccc68fSToby Isaac     }
15557318780aSToby Isaac     if (detJ) {
15567318780aSToby Isaac       for (i = 0; i < Nq; i++) {
15577318780aSToby Isaac         detJ[i] = detJ0;
1558dfccc68fSToby Isaac       }
15597318780aSToby Isaac     }
15607318780aSToby Isaac     if (J) {
15617318780aSToby Isaac       PetscInt k;
15627318780aSToby Isaac 
15637318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1564dfccc68fSToby Isaac         PetscInt j;
1565dfccc68fSToby Isaac 
15667318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
15677318780aSToby Isaac           J[k] = J0[j];
15687318780aSToby Isaac         }
15697318780aSToby Isaac       }
15707318780aSToby Isaac     }
15717318780aSToby Isaac     if (invJ) {
15727318780aSToby Isaac       PetscInt k;
15737318780aSToby Isaac       switch (coordDim) {
15747318780aSToby Isaac       case 0:
15757318780aSToby Isaac         break;
15767318780aSToby Isaac       case 1:
15777318780aSToby Isaac         invJ[0] = 1./J0[0];
15787318780aSToby Isaac         break;
15797318780aSToby Isaac       case 2:
15807318780aSToby Isaac         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
15817318780aSToby Isaac         break;
15827318780aSToby Isaac       case 3:
15837318780aSToby Isaac         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
15847318780aSToby Isaac         break;
15857318780aSToby Isaac       }
15867318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
15877318780aSToby Isaac         PetscInt j;
15887318780aSToby Isaac 
15897318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
15907318780aSToby Isaac           invJ[k] = invJ[j];
15917318780aSToby Isaac         }
1592dfccc68fSToby Isaac       }
1593dfccc68fSToby Isaac     }
1594dfccc68fSToby Isaac   }
1595dfccc68fSToby Isaac   PetscFunctionReturn(0);
1596dfccc68fSToby Isaac }
1597dfccc68fSToby Isaac 
1598ccd2543fSMatthew G Knepley /*@C
15998e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1600ccd2543fSMatthew G Knepley 
1601d083f849SBarry Smith   Collective on dm
1602ccd2543fSMatthew G Knepley 
1603ccd2543fSMatthew G Knepley   Input Arguments:
1604ccd2543fSMatthew G Knepley + dm   - the DM
1605ccd2543fSMatthew G Knepley - cell - the cell
1606ccd2543fSMatthew G Knepley 
1607ccd2543fSMatthew G Knepley   Output Arguments:
1608ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1609ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1610ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1611ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1612ccd2543fSMatthew G Knepley 
1613ccd2543fSMatthew G Knepley   Level: advanced
1614ccd2543fSMatthew G Knepley 
1615ccd2543fSMatthew G Knepley   Fortran Notes:
1616ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1617ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1618ccd2543fSMatthew G Knepley 
1619e8964c0aSStefano Zampini .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinates()
1620ccd2543fSMatthew G Knepley @*/
16218e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1622ccd2543fSMatthew G Knepley {
1623ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1624ccd2543fSMatthew G Knepley 
1625ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1626dfccc68fSToby Isaac   ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr);
16278e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
16288e0841e0SMatthew G. Knepley }
16298e0841e0SMatthew G. Knepley 
1630dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
16318e0841e0SMatthew G. Knepley {
1632dfccc68fSToby Isaac   PetscQuadrature   feQuad;
16338e0841e0SMatthew G. Knepley   PetscSection      coordSection;
16348e0841e0SMatthew G. Knepley   Vec               coordinates;
16358e0841e0SMatthew G. Knepley   PetscScalar      *coords = NULL;
16368e0841e0SMatthew G. Knepley   const PetscReal  *quadPoints;
1637ef0bb6c7SMatthew G. Knepley   PetscTabulation T;
1638f960e424SToby Isaac   PetscInt          dim, cdim, pdim, qdim, Nq, numCoords, q;
16398e0841e0SMatthew G. Knepley   PetscErrorCode    ierr;
16408e0841e0SMatthew G. Knepley 
16418e0841e0SMatthew G. Knepley   PetscFunctionBegin;
16428e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
16438e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
16448e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
16458e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16468e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1647dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
1648dfccc68fSToby Isaac     PetscDualSpace dsp;
1649dfccc68fSToby Isaac 
1650dfccc68fSToby Isaac     ierr = PetscFEGetDualSpace(fe, &dsp);CHKERRQ(ierr);
1651dfccc68fSToby Isaac     ierr = PetscDualSpaceGetFunctional(dsp, 0, &quad);CHKERRQ(ierr);
16529c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1653dfccc68fSToby Isaac     Nq = 1;
1654dfccc68fSToby Isaac   } else {
16559c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1656dfccc68fSToby Isaac   }
165791d2b7ceSToby Isaac   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
1658dfccc68fSToby Isaac   ierr = PetscFEGetQuadrature(fe, &feQuad);CHKERRQ(ierr);
1659dfccc68fSToby Isaac   if (feQuad == quad) {
1660ef0bb6c7SMatthew G. Knepley     ierr = PetscFEGetCellTabulation(fe, &T);CHKERRQ(ierr);
16618e0841e0SMatthew 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);
1662dfccc68fSToby Isaac   } else {
1663ef0bb6c7SMatthew G. Knepley     ierr = PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T);CHKERRQ(ierr);
1664dfccc68fSToby Isaac   }
1665dfccc68fSToby Isaac   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
1666ef0bb6c7SMatthew G. Knepley   {
1667ef0bb6c7SMatthew G. Knepley     const PetscReal *basis    = T->T[0];
1668ef0bb6c7SMatthew G. Knepley     const PetscReal *basisDer = T->T[1];
1669ef0bb6c7SMatthew G. Knepley     PetscReal        detJt;
1670ef0bb6c7SMatthew G. Knepley 
1671dfccc68fSToby Isaac     if (v) {
1672580bdb30SBarry Smith       ierr = PetscArrayzero(v, Nq*cdim);CHKERRQ(ierr);
1673f960e424SToby Isaac       for (q = 0; q < Nq; ++q) {
1674f960e424SToby Isaac         PetscInt i, k;
1675f960e424SToby Isaac 
1676*301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
1677*301b184aSMatthew G. Knepley           const PetscInt vertex = k/cdim;
1678*301b184aSMatthew G. Knepley           for (i = 0; i < cdim; ++i) {
1679*301b184aSMatthew G. Knepley             v[q*cdim + i] += basis[(q*pdim + k)*cdim + i] * PetscRealPart(coords[vertex*cdim + i]);
1680*301b184aSMatthew G. Knepley           }
1681*301b184aSMatthew G. Knepley         }
1682f960e424SToby Isaac         ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1683f960e424SToby Isaac       }
1684f960e424SToby Isaac     }
16858e0841e0SMatthew G. Knepley     if (J) {
1686580bdb30SBarry Smith       ierr = PetscArrayzero(J, Nq*cdim*cdim);CHKERRQ(ierr);
16878e0841e0SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
16888e0841e0SMatthew G. Knepley         PetscInt i, j, k, c, r;
16898e0841e0SMatthew G. Knepley 
16908e0841e0SMatthew G. Knepley         /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
1691*301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
1692*301b184aSMatthew G. Knepley           const PetscInt vertex = k/cdim;
1693*301b184aSMatthew G. Knepley           for (j = 0; j < dim; ++j) {
1694*301b184aSMatthew G. Knepley             for (i = 0; i < cdim; ++i) {
1695*301b184aSMatthew G. Knepley               J[(q*cdim + i)*cdim + j] += basisDer[((q*pdim + k)*cdim + i)*dim + j] * PetscRealPart(coords[vertex*cdim + i]);
1696*301b184aSMatthew G. Knepley             }
1697*301b184aSMatthew G. Knepley           }
1698*301b184aSMatthew G. Knepley         }
16993bc0b13bSBarry Smith         ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
17008e0841e0SMatthew G. Knepley         if (cdim > dim) {
17018e0841e0SMatthew G. Knepley           for (c = dim; c < cdim; ++c)
17028e0841e0SMatthew G. Knepley             for (r = 0; r < cdim; ++r)
17038e0841e0SMatthew G. Knepley               J[r*cdim+c] = r == c ? 1.0 : 0.0;
17048e0841e0SMatthew G. Knepley         }
1705f960e424SToby Isaac         if (!detJ && !invJ) continue;
1706a63b72c6SToby Isaac         detJt = 0.;
17078e0841e0SMatthew G. Knepley         switch (cdim) {
17088e0841e0SMatthew G. Knepley         case 3:
1709037dc194SToby Isaac           DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]);
1710037dc194SToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
171117fe8556SMatthew G. Knepley           break;
171249dc4407SMatthew G. Knepley         case 2:
17139f328543SToby Isaac           DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]);
1714037dc194SToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
171549dc4407SMatthew G. Knepley           break;
17168e0841e0SMatthew G. Knepley         case 1:
1717037dc194SToby Isaac           detJt = J[q*cdim*dim];
1718037dc194SToby Isaac           if (invJ) invJ[q*cdim*dim] = 1.0/detJt;
171949dc4407SMatthew G. Knepley         }
1720f960e424SToby Isaac         if (detJ) detJ[q] = detJt;
172149dc4407SMatthew G. Knepley       }
1722ef0bb6c7SMatthew G. Knepley     } else if (detJ || invJ) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
172349dc4407SMatthew G. Knepley   }
1724ef0bb6c7SMatthew G. Knepley   if (feQuad != quad) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
17258e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
17268e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
17278e0841e0SMatthew G. Knepley }
17288e0841e0SMatthew G. Knepley 
17298e0841e0SMatthew G. Knepley /*@C
17308e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
17318e0841e0SMatthew G. Knepley 
1732d083f849SBarry Smith   Collective on dm
17338e0841e0SMatthew G. Knepley 
17348e0841e0SMatthew G. Knepley   Input Arguments:
17358e0841e0SMatthew G. Knepley + dm   - the DM
17368e0841e0SMatthew G. Knepley . cell - the cell
1737dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
1738dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
17398e0841e0SMatthew G. Knepley 
17408e0841e0SMatthew G. Knepley   Output Arguments:
1741dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
17428e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
17438e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
17448e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
17458e0841e0SMatthew G. Knepley 
17468e0841e0SMatthew G. Knepley   Level: advanced
17478e0841e0SMatthew G. Knepley 
17488e0841e0SMatthew G. Knepley   Fortran Notes:
17498e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
17508e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
17518e0841e0SMatthew G. Knepley 
1752e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
17538e0841e0SMatthew G. Knepley @*/
1754dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
17558e0841e0SMatthew G. Knepley {
1756bb4a5db5SMatthew G. Knepley   DM             cdm;
1757dfccc68fSToby Isaac   PetscFE        fe = NULL;
17588e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
17598e0841e0SMatthew G. Knepley 
17608e0841e0SMatthew G. Knepley   PetscFunctionBegin;
17612d89661fSMatthew G. Knepley   PetscValidPointer(detJ, 7);
1762bb4a5db5SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1763bb4a5db5SMatthew G. Knepley   if (cdm) {
1764dfccc68fSToby Isaac     PetscClassId id;
1765dfccc68fSToby Isaac     PetscInt     numFields;
1766e5e52638SMatthew G. Knepley     PetscDS      prob;
1767dfccc68fSToby Isaac     PetscObject  disc;
1768dfccc68fSToby Isaac 
1769bb4a5db5SMatthew G. Knepley     ierr = DMGetNumFields(cdm, &numFields);CHKERRQ(ierr);
1770dfccc68fSToby Isaac     if (numFields) {
1771bb4a5db5SMatthew G. Knepley       ierr = DMGetDS(cdm, &prob);CHKERRQ(ierr);
1772dfccc68fSToby Isaac       ierr = PetscDSGetDiscretization(prob,0,&disc);CHKERRQ(ierr);
1773dfccc68fSToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1774dfccc68fSToby Isaac       if (id == PETSCFE_CLASSID) {
1775dfccc68fSToby Isaac         fe = (PetscFE) disc;
1776dfccc68fSToby Isaac       }
1777dfccc68fSToby Isaac     }
1778dfccc68fSToby Isaac   }
1779dfccc68fSToby Isaac   if (!fe) {ierr = DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1780dfccc68fSToby Isaac   else     {ierr = DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1781ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1782ccd2543fSMatthew G Knepley }
1783834e62ceSMatthew G. Knepley 
1784011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1785cc08537eSMatthew G. Knepley {
1786cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1787cc08537eSMatthew G. Knepley   Vec            coordinates;
1788a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
178906e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1790714b99b6SMatthew G. Knepley   PetscInt       coordSize, d;
1791cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1792cc08537eSMatthew G. Knepley 
1793cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1794cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
179569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1796cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
17972e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1798cc08537eSMatthew G. Knepley   if (centroid) {
1799714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) centroid[d] = 0.5*PetscRealPart(coords[d] + tmp[d]);
1800cc08537eSMatthew G. Knepley   }
1801cc08537eSMatthew G. Knepley   if (normal) {
1802a60a936bSMatthew G. Knepley     PetscReal norm;
1803a60a936bSMatthew G. Knepley 
1804714b99b6SMatthew G. Knepley     if (dim != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "We only support 2D edges right now");
180506e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
180606e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1807714b99b6SMatthew G. Knepley     norm       = DMPlex_NormD_Internal(dim, normal);
1808714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) normal[d] /= norm;
1809cc08537eSMatthew G. Knepley   }
1810cc08537eSMatthew G. Knepley   if (vol) {
1811714b99b6SMatthew G. Knepley     *vol = 0.0;
1812714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - tmp[d]));
1813714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
1814cc08537eSMatthew G. Knepley   }
1815cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1816cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1817cc08537eSMatthew G. Knepley }
1818cc08537eSMatthew G. Knepley 
1819cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1820011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1821cc08537eSMatthew G. Knepley {
1822412e9a14SMatthew G. Knepley   DMPolytopeType ct;
1823cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1824cc08537eSMatthew G. Knepley   Vec            coordinates;
1825cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
18260a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
1827793a2a13SMatthew G. Knepley   PetscBool      isHybrid = PETSC_FALSE;
1828793a2a13SMatthew G. Knepley   PetscInt       fv[4] = {0, 1, 2, 3};
1829412e9a14SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1830cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1831cc08537eSMatthew G. Knepley 
1832cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1833793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
1834412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1835412e9a14SMatthew G. Knepley   switch (ct) {
1836412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
1837412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1838412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
1839412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
1840412e9a14SMatthew G. Knepley       isHybrid = PETSC_TRUE;
1841412e9a14SMatthew G. Knepley     default: break;
1842412e9a14SMatthew G. Knepley   }
1843cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
18440a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
184569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1846cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
18470bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1848793a2a13SMatthew G. Knepley   /* Side faces for hybrid cells are are stored as tensor products */
1849793a2a13SMatthew G. Knepley   if (isHybrid && numCorners == 4) {fv[2] = 3; fv[3] = 2;}
1850793a2a13SMatthew G. Knepley 
1851ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1852ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1853ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1854ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1855ceee4971SMatthew G. Knepley   }
1856011ea5d8SMatthew G. Knepley   if (normal) {
1857011ea5d8SMatthew G. Knepley     if (dim > 2) {
1858793a2a13SMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim*fv[1]+0] - coords[0]), x1 = PetscRealPart(coords[dim*fv[2]+0] - coords[0]);
1859793a2a13SMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim*fv[1]+1] - coords[1]), y1 = PetscRealPart(coords[dim*fv[2]+1] - coords[1]);
1860793a2a13SMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim*fv[1]+2] - coords[2]), z1 = PetscRealPart(coords[dim*fv[2]+2] - coords[2]);
18610a1d6728SMatthew G. Knepley       PetscReal       norm;
18620a1d6728SMatthew G. Knepley 
18630a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
18640a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
18650a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
18668b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
18670a1d6728SMatthew G. Knepley       normal[0] /= norm;
18680a1d6728SMatthew G. Knepley       normal[1] /= norm;
18690a1d6728SMatthew G. Knepley       normal[2] /= norm;
1870011ea5d8SMatthew G. Knepley     } else {
1871011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1872011ea5d8SMatthew G. Knepley     }
1873011ea5d8SMatthew G. Knepley   }
1874741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
18750a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
1876793a2a13SMatthew G. Knepley     const PetscInt pi  = p < 4 ? fv[p] : p;
1877793a2a13SMatthew G. Knepley     const PetscInt pin = p < 3 ? fv[(p+1)%numCorners] : (p+1)%numCorners;
18780a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
18790a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
1880793a2a13SMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[pi*tdim+d]);
1881793a2a13SMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[pin*tdim+d]);
18820a1d6728SMatthew G. Knepley     }
18830a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
18840a1d6728SMatthew G. Knepley     vsum += vtmp;
18850a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
18860a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
18870a1d6728SMatthew G. Knepley     }
18880a1d6728SMatthew G. Knepley   }
18890a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
18900a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
18910a1d6728SMatthew G. Knepley   }
18920a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1893ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
18940a1d6728SMatthew G. Knepley   if (centroid) {
18950a1d6728SMatthew G. Knepley     if (dim > 2) {
18960a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
18970a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
18980a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
18990a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
19000a1d6728SMatthew G. Knepley         }
19010a1d6728SMatthew G. Knepley       }
19020a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
19030a1d6728SMatthew G. Knepley   }
1904cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1905cc08537eSMatthew G. Knepley }
1906cc08537eSMatthew G. Knepley 
19070ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1908011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
19090ec8681fSMatthew G. Knepley {
1910412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
19110ec8681fSMatthew G. Knepley   PetscSection    coordSection;
19120ec8681fSMatthew G. Knepley   Vec             coordinates;
19130ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
191486623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1915a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
1916793a2a13SMatthew G. Knepley   PetscBool       isHybrid = PETSC_FALSE;
1917412e9a14SMatthew G. Knepley   PetscInt        numFaces, f, coordSize, p, d;
19180ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
19190ec8681fSMatthew G. Knepley 
19200ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1921f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
1922793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
1923412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1924412e9a14SMatthew G. Knepley   switch (ct) {
1925412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
1926412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1927412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
1928412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
1929412e9a14SMatthew G. Knepley       isHybrid = PETSC_TRUE;
1930412e9a14SMatthew G. Knepley     default: break;
1931412e9a14SMatthew G. Knepley   }
1932793a2a13SMatthew G. Knepley 
19330ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
193469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
19350ec8681fSMatthew G. Knepley 
1936d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
19370ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
19380ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1939a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
19400ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1941793a2a13SMatthew G. Knepley     PetscBool      flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
1942ba2698f1SMatthew G. Knepley     DMPolytopeType ct;
1943793a2a13SMatthew G. Knepley 
1944011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
1945ba2698f1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, faces[f], &ct);CHKERRQ(ierr);
1946ba2698f1SMatthew G. Knepley     switch (ct) {
1947ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
19480ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
19491ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
19501ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
19511ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
19520ec8681fSMatthew G. Knepley       }
19530ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1954793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
19550ec8681fSMatthew G. Knepley       vsum += vtmp;
19564f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
19570ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19581ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
19590ec8681fSMatthew G. Knepley         }
19600ec8681fSMatthew G. Knepley       }
19610ec8681fSMatthew G. Knepley       break;
1962ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
1963412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1964793a2a13SMatthew G. Knepley     {
1965793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
1966793a2a13SMatthew G. Knepley 
1967793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
1968793a2a13SMatthew G. Knepley       if (isHybrid && f > 1) {fv[2] = 3; fv[3] = 2;}
19690ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
19700ec8681fSMatthew G. Knepley       /* First tet */
19710ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
1972793a2a13SMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[0]*dim+d]);
1973793a2a13SMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[1]*dim+d]);
1974793a2a13SMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]);
19750ec8681fSMatthew G. Knepley       }
19760ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1977793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
19780ec8681fSMatthew G. Knepley       vsum += vtmp;
19790ec8681fSMatthew G. Knepley       if (centroid) {
19800ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19810ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
19820ec8681fSMatthew G. Knepley         }
19830ec8681fSMatthew G. Knepley       }
19840ec8681fSMatthew G. Knepley       /* Second tet */
19850ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
1986793a2a13SMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[1]*dim+d]);
1987793a2a13SMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[2]*dim+d]);
1988793a2a13SMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]);
19890ec8681fSMatthew G. Knepley       }
19900ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1991793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
19920ec8681fSMatthew G. Knepley       vsum += vtmp;
19930ec8681fSMatthew G. Knepley       if (centroid) {
19940ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19950ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
19960ec8681fSMatthew G. Knepley         }
19970ec8681fSMatthew G. Knepley       }
19980ec8681fSMatthew G. Knepley       break;
1999793a2a13SMatthew G. Knepley     }
20000ec8681fSMatthew G. Knepley     default:
2001ba2698f1SMatthew G. Knepley       SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %D of type %s", faces[f], DMPolytopeTypes[ct]);
20020ec8681fSMatthew G. Knepley     }
20034f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
20040ec8681fSMatthew G. Knepley   }
20058763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
20060ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
2007d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
20080ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
20090ec8681fSMatthew G. Knepley }
20100ec8681fSMatthew G. Knepley 
2011834e62ceSMatthew G. Knepley /*@C
2012834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
2013834e62ceSMatthew G. Knepley 
2014d083f849SBarry Smith   Collective on dm
2015834e62ceSMatthew G. Knepley 
2016834e62ceSMatthew G. Knepley   Input Arguments:
2017834e62ceSMatthew G. Knepley + dm   - the DM
2018834e62ceSMatthew G. Knepley - cell - the cell
2019834e62ceSMatthew G. Knepley 
2020834e62ceSMatthew G. Knepley   Output Arguments:
2021834e62ceSMatthew G. Knepley + volume   - the cell volume
2022cc08537eSMatthew G. Knepley . centroid - the cell centroid
2023cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
2024834e62ceSMatthew G. Knepley 
2025834e62ceSMatthew G. Knepley   Level: advanced
2026834e62ceSMatthew G. Knepley 
2027834e62ceSMatthew G. Knepley   Fortran Notes:
2028834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2029834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
2030834e62ceSMatthew G. Knepley 
2031e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
2032834e62ceSMatthew G. Knepley @*/
2033cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2034834e62ceSMatthew G. Knepley {
20350ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
2036834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
2037834e62ceSMatthew G. Knepley 
2038834e62ceSMatthew G. Knepley   PetscFunctionBegin;
2039834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2040c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2041834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
2042ba2698f1SMatthew G. Knepley   ierr = DMPlexGetPointDepth(dm, cell, &depth);CHKERRQ(ierr);
2043011ea5d8SMatthew G. Knepley   switch (depth) {
2044cc08537eSMatthew G. Knepley   case 1:
2045011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2046cc08537eSMatthew G. Knepley     break;
2047834e62ceSMatthew G. Knepley   case 2:
2048011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2049834e62ceSMatthew G. Knepley     break;
2050834e62ceSMatthew G. Knepley   case 3:
2051011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2052834e62ceSMatthew G. Knepley     break;
2053834e62ceSMatthew G. Knepley   default:
2054b81cf158SMatthew G. Knepley     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D (depth %D) for element geometry computation", dim, depth);
2055834e62ceSMatthew G. Knepley   }
2056834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
2057834e62ceSMatthew G. Knepley }
2058113c68e6SMatthew G. Knepley 
2059c501906fSMatthew G. Knepley /*@
2060c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
2061c501906fSMatthew G. Knepley 
2062c501906fSMatthew G. Knepley   Collective on dm
2063c501906fSMatthew G. Knepley 
2064c501906fSMatthew G. Knepley   Input Parameter:
2065c501906fSMatthew G. Knepley . dm - The DMPlex
2066c501906fSMatthew G. Knepley 
2067c501906fSMatthew G. Knepley   Output Parameter:
2068c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
2069c501906fSMatthew G. Knepley 
2070c501906fSMatthew G. Knepley   Level: beginner
2071c501906fSMatthew G. Knepley 
2072c501906fSMatthew G. Knepley @*/
2073c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
2074c0d900a5SMatthew G. Knepley {
2075c0d900a5SMatthew G. Knepley   DM             dmCell;
2076c0d900a5SMatthew G. Knepley   Vec            coordinates;
2077c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
2078c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
2079412e9a14SMatthew G. Knepley   PetscInt       cStart, cEnd, c;
2080c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
2081c0d900a5SMatthew G. Knepley 
2082c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
2083c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2084c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2085c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2086c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2087c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2088c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2089412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2090c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
2091c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
2092cf0b7c11SKarl Rupp   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFEGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2093c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
209492fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2095c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2096c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2097c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2098c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2099cf0b7c11SKarl Rupp     PetscFEGeom *cg;
2100c0d900a5SMatthew G. Knepley 
2101c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2102580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2103cf0b7c11SKarl Rupp     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ);CHKERRQ(ierr);
2104087ef6b2SMatthew 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);
2105c0d900a5SMatthew G. Knepley   }
2106c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
2107c0d900a5SMatthew G. Knepley }
2108c0d900a5SMatthew G. Knepley 
2109891a9168SMatthew G. Knepley /*@
2110891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2111891a9168SMatthew G. Knepley 
2112891a9168SMatthew G. Knepley   Input Parameter:
2113891a9168SMatthew G. Knepley . dm - The DM
2114891a9168SMatthew G. Knepley 
2115891a9168SMatthew G. Knepley   Output Parameters:
2116891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2117a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data
2118891a9168SMatthew G. Knepley 
2119891a9168SMatthew G. Knepley   Level: developer
2120891a9168SMatthew G. Knepley 
2121891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
2122891a9168SMatthew G. Knepley @*/
2123113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2124113c68e6SMatthew G. Knepley {
2125113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
2126113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
2127113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
2128113c68e6SMatthew G. Knepley   PetscSection   coordSection;
2129113c68e6SMatthew G. Knepley   Vec            coordinates;
2130113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2131113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
2132113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2133113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
2134113c68e6SMatthew G. Knepley 
2135113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2136113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2137113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2138113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2139113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
2140113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2141113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2142113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2143113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2144113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2145485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2146113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
21479e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2148113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
214992fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2150113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2151113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2152485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2153113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2154113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2155113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2156113c68e6SMatthew G. Knepley 
2157113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2158580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2159113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
2160113c68e6SMatthew G. Knepley   }
2161113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
2162113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
2163113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
2164113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2165113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
21669e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2167113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
216892fd8e1eSJed Brown   ierr = DMSetLocalSection(dmFace, sectionFace);CHKERRQ(ierr);
2169113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
2170113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
2171113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
2172c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2173113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2174113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2175113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2176113c68e6SMatthew G. Knepley     PetscReal        area;
2177412e9a14SMatthew G. Knepley     const PetscInt  *cells;
2178412e9a14SMatthew G. Knepley     PetscInt         ncells, ghost = -1, d, numChildren;
2179113c68e6SMatthew G. Knepley 
21809ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
218150d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2182412e9a14SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
2183412e9a14SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
2184412e9a14SMatthew G. Knepley     /* It is possible to get a face with no support when using partition overlap */
2185412e9a14SMatthew G. Knepley     if (!ncells || ghost >= 0 || numChildren) continue;
2186113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
2187113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
2188113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2189113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2190113c68e6SMatthew G. Knepley     {
2191113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
2192113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
21930453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2194113c68e6SMatthew G. Knepley 
2195113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
2196113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
219706348e87SToby Isaac       if (ncells > 1) {
219806348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
2199113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
220006348e87SToby Isaac       }
220106348e87SToby Isaac       else {
220206348e87SToby Isaac         rcentroid = fg->centroid;
220306348e87SToby Isaac       }
22042e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
22052e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
22060453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2207113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2208113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2209113c68e6SMatthew G. Knepley       }
2210113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
2211113c68e6SMatthew 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]);
2212113c68e6SMatthew 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]);
2213113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
2214113c68e6SMatthew G. Knepley       }
2215113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2216113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2217113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2218113c68e6SMatthew G. Knepley       }
221906348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2220113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2221113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2222113c68e6SMatthew G. Knepley       }
2223113c68e6SMatthew G. Knepley     }
2224113c68e6SMatthew G. Knepley   }
2225b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2226113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
2227113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2228113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2229113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2230113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
2231113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2232113c68e6SMatthew G. Knepley 
2233113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
2234113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
2235113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
2236113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
223750d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
2238113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
2239113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
2240113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2241113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2242113c68e6SMatthew G. Knepley       if (support[s] == c) {
2243640bce14SSatish Balay         PetscFVCellGeom       *ci;
2244113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
2245113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
2246113c68e6SMatthew G. Knepley 
2247113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
2248113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2249113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
2250113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
2251113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
2252113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2253113c68e6SMatthew G. Knepley       }
2254113c68e6SMatthew G. Knepley     }
2255113c68e6SMatthew G. Knepley   }
2256113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
2257113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2258113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2259113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
2260113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2261113c68e6SMatthew G. Knepley }
2262113c68e6SMatthew G. Knepley 
2263113c68e6SMatthew G. Knepley /*@C
2264113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2265113c68e6SMatthew G. Knepley 
2266113c68e6SMatthew G. Knepley   Not collective
2267113c68e6SMatthew G. Knepley 
2268113c68e6SMatthew G. Knepley   Input Argument:
2269113c68e6SMatthew G. Knepley . dm - the DM
2270113c68e6SMatthew G. Knepley 
2271113c68e6SMatthew G. Knepley   Output Argument:
2272113c68e6SMatthew G. Knepley . minradius - the minium cell radius
2273113c68e6SMatthew G. Knepley 
2274113c68e6SMatthew G. Knepley   Level: developer
2275113c68e6SMatthew G. Knepley 
2276113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
2277113c68e6SMatthew G. Knepley @*/
2278113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2279113c68e6SMatthew G. Knepley {
2280113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2281113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2282113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
2283113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
2284113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2285113c68e6SMatthew G. Knepley }
2286113c68e6SMatthew G. Knepley 
2287113c68e6SMatthew G. Knepley /*@C
2288113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2289113c68e6SMatthew G. Knepley 
2290113c68e6SMatthew G. Knepley   Logically collective
2291113c68e6SMatthew G. Knepley 
2292113c68e6SMatthew G. Knepley   Input Arguments:
2293113c68e6SMatthew G. Knepley + dm - the DM
2294113c68e6SMatthew G. Knepley - minradius - the minium cell radius
2295113c68e6SMatthew G. Knepley 
2296113c68e6SMatthew G. Knepley   Level: developer
2297113c68e6SMatthew G. Knepley 
2298113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
2299113c68e6SMatthew G. Knepley @*/
2300113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2301113c68e6SMatthew G. Knepley {
2302113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2303113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2304113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
2305113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2306113c68e6SMatthew G. Knepley }
2307856ac710SMatthew G. Knepley 
2308856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2309856ac710SMatthew G. Knepley {
2310856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
2311856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
2312856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2313856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2314856ac710SMatthew G. Knepley 
2315856ac710SMatthew G. Knepley   PetscFunctionBegin;
2316856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2317856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2318485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2319856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
2320856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2321c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2322856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2323856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2324856ac710SMatthew G. Knepley     const PetscInt        *faces;
2325856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
2326640bce14SSatish Balay     PetscFVCellGeom        *cg;
2327856ac710SMatthew G. Knepley     PetscBool              boundary;
2328856ac710SMatthew G. Knepley     PetscInt               ghost;
2329856ac710SMatthew G. Knepley 
2330856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2331856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
2332856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
2333856ac710SMatthew 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);
2334856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2335640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2336856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
2337856ac710SMatthew G. Knepley       const PetscInt        *fcells;
2338856ac710SMatthew G. Knepley       PetscInt               ncell, side;
2339856ac710SMatthew G. Knepley 
2340856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2341a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2342856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2343856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
2344856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2345856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
2346856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
2347856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2348856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
2349856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
2350856ac710SMatthew G. Knepley     }
2351856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
2352856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
2353856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2354856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2355a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2356856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2357856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
2358856ac710SMatthew G. Knepley       ++usedFaces;
2359856ac710SMatthew G. Knepley     }
2360856ac710SMatthew G. Knepley   }
2361856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
2362856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2363856ac710SMatthew G. Knepley }
2364856ac710SMatthew G. Knepley 
2365b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2366b81db932SToby Isaac {
2367b81db932SToby Isaac   DMLabel        ghostLabel;
2368b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
2369b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2370b81db932SToby Isaac   PetscSection   neighSec;
2371b81db932SToby Isaac   PetscInt     (*neighbors)[2];
2372b81db932SToby Isaac   PetscInt      *counter;
2373b81db932SToby Isaac   PetscErrorCode ierr;
2374b81db932SToby Isaac 
2375b81db932SToby Isaac   PetscFunctionBegin;
2376b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2377b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2378485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2379485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2380b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
2381b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
2382b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2383c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2384b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2385b81db932SToby Isaac     const PetscInt        *fcells;
2386b81db932SToby Isaac     PetscBool              boundary;
23875bc680faSToby Isaac     PetscInt               ghost = -1;
2388b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2389b81db932SToby Isaac 
239006348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2391a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2392b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2393b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2394b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
239506348e87SToby Isaac     if (numCells == 2) {
2396b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2397b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2398b81db932SToby Isaac         PetscInt cell = fcells[c];
2399b81db932SToby Isaac 
2400e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2401b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
2402b81db932SToby Isaac         }
2403b81db932SToby Isaac       }
2404b81db932SToby Isaac     }
240506348e87SToby Isaac   }
2406b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
2407b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
2408b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2409b81db932SToby Isaac   nStart = 0;
2410b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2411b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2412b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2413b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2414b81db932SToby Isaac     const PetscInt        *fcells;
2415b81db932SToby Isaac     PetscBool              boundary;
24165bc680faSToby Isaac     PetscInt               ghost = -1;
2417b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2418b81db932SToby Isaac 
241906348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2420a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2421b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2422b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2423b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
242406348e87SToby Isaac     if (numCells == 2) {
2425b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2426b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2427b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2428b81db932SToby Isaac 
2429e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2430b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2431b81db932SToby Isaac           off += counter[cell - cStart]++;
2432b81db932SToby Isaac           neighbors[off][0] = f;
2433b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2434b81db932SToby Isaac         }
2435b81db932SToby Isaac       }
2436b81db932SToby Isaac     }
243706348e87SToby Isaac   }
2438b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2439b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2440b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2441317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2442640bce14SSatish Balay     PetscFVCellGeom        *cg;
2443b81db932SToby Isaac 
2444b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2445b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2446b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2447317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2448317218b9SToby 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);
2449b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2450640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2451b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2452b81db932SToby Isaac       const PetscInt        *fcells;
2453b81db932SToby Isaac       PetscInt               ncell, side, nface;
2454b81db932SToby Isaac 
2455b81db932SToby Isaac       nface = neighbors[off + f][0];
2456b81db932SToby Isaac       ncell = neighbors[off + f][1];
2457b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2458b81db932SToby Isaac       side  = (c != fcells[0]);
2459b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2460b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2461b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2462b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2463b81db932SToby Isaac     }
2464b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2465b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2466b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2467b81db932SToby Isaac     }
2468b81db932SToby Isaac   }
2469b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
24705fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2471b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2472b81db932SToby Isaac   PetscFunctionReturn(0);
2473b81db932SToby Isaac }
2474b81db932SToby Isaac 
2475856ac710SMatthew G. Knepley /*@
2476856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2477856ac710SMatthew G. Knepley 
2478d083f849SBarry Smith   Collective on dm
2479856ac710SMatthew G. Knepley 
2480856ac710SMatthew G. Knepley   Input Arguments:
2481856ac710SMatthew G. Knepley + dm  - The DM
2482856ac710SMatthew G. Knepley . fvm - The PetscFV
24838f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
24848f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2485856ac710SMatthew G. Knepley 
2486856ac710SMatthew G. Knepley   Output Parameters:
2487856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2488856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2489856ac710SMatthew G. Knepley 
2490856ac710SMatthew G. Knepley   Level: developer
2491856ac710SMatthew G. Knepley 
2492856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2493856ac710SMatthew G. Knepley @*/
2494856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2495856ac710SMatthew G. Knepley {
2496856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2497856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2498b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2499856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2500856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2501856ac710SMatthew G. Knepley 
2502856ac710SMatthew G. Knepley   PetscFunctionBegin;
2503856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2504856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2505856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2506485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2507856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2508856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2509856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2510856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2511856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2512b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2513b81db932SToby Isaac   if (!parentSection) {
2514856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2515b5a3613cSMatthew G. Knepley   } else {
2516b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2517b81db932SToby Isaac   }
2518856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2519856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2520856ac710SMatthew G. Knepley   /* Create storage for gradients */
2521856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2522856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2523856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2524856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2525856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
252692fd8e1eSJed Brown   ierr = DMSetLocalSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2527856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2528856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2529856ac710SMatthew G. Knepley }
2530b27d5b9eSToby Isaac 
2531c501906fSMatthew G. Knepley /*@
2532c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2533c501906fSMatthew G. Knepley 
2534d083f849SBarry Smith   Collective on dm
2535c501906fSMatthew G. Knepley 
2536c501906fSMatthew G. Knepley   Input Arguments:
2537c501906fSMatthew G. Knepley + dm  - The DM
2538c501906fSMatthew G. Knepley - fvm - The PetscFV
2539c501906fSMatthew G. Knepley 
2540c501906fSMatthew G. Knepley   Output Parameters:
2541c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2542c501906fSMatthew G. Knepley . faceGeometry - The face geometry
2543c501906fSMatthew G. Knepley - dmGrad       - The gradient matrices
2544c501906fSMatthew G. Knepley 
2545c501906fSMatthew G. Knepley   Level: developer
2546c501906fSMatthew G. Knepley 
2547c501906fSMatthew G. Knepley .seealso: DMPlexComputeGeometryFVM()
2548c501906fSMatthew G. Knepley @*/
2549b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2550b27d5b9eSToby Isaac {
2551b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2552b27d5b9eSToby Isaac   PetscErrorCode ierr;
2553b27d5b9eSToby Isaac 
2554b27d5b9eSToby Isaac   PetscFunctionBegin;
2555b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2556b27d5b9eSToby Isaac   if (!cellgeomobj) {
2557b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2558b27d5b9eSToby Isaac 
2559b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2560b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2561b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2562b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2563b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2564b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2565b27d5b9eSToby Isaac   }
2566b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2567b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2568b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2569b27d5b9eSToby Isaac   if (gradDM) {
2570b27d5b9eSToby Isaac     PetscObject gradobj;
2571b27d5b9eSToby Isaac     PetscBool   computeGradients;
2572b27d5b9eSToby Isaac 
2573b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2574b27d5b9eSToby Isaac     if (!computeGradients) {
2575b27d5b9eSToby Isaac       *gradDM = NULL;
2576b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2577b27d5b9eSToby Isaac     }
2578b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2579b27d5b9eSToby Isaac     if (!gradobj) {
2580b27d5b9eSToby Isaac       DM dmGradInt;
2581b27d5b9eSToby Isaac 
2582b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2583b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2584b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2585b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2586b27d5b9eSToby Isaac     }
2587b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2588b27d5b9eSToby Isaac   }
2589b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2590b27d5b9eSToby Isaac }
2591d6143a4eSToby Isaac 
25929d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
25939d150b73SToby Isaac {
25949d150b73SToby Isaac   PetscInt l, m;
25959d150b73SToby Isaac 
2596cd345991SToby Isaac   PetscFunctionBeginHot;
25979d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
25989d150b73SToby Isaac     /* invert Jacobian, multiply */
25999d150b73SToby Isaac     PetscScalar det, idet;
26009d150b73SToby Isaac 
26019d150b73SToby Isaac     switch (dimR) {
26029d150b73SToby Isaac     case 1:
26039d150b73SToby Isaac       invJ[0] = 1./ J[0];
26049d150b73SToby Isaac       break;
26059d150b73SToby Isaac     case 2:
26069d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
26079d150b73SToby Isaac       idet = 1./det;
26089d150b73SToby Isaac       invJ[0] =  J[3] * idet;
26099d150b73SToby Isaac       invJ[1] = -J[1] * idet;
26109d150b73SToby Isaac       invJ[2] = -J[2] * idet;
26119d150b73SToby Isaac       invJ[3] =  J[0] * idet;
26129d150b73SToby Isaac       break;
26139d150b73SToby Isaac     case 3:
26149d150b73SToby Isaac       {
26159d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
26169d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
26179d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
26189d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
26199d150b73SToby Isaac         idet = 1./det;
26209d150b73SToby Isaac         invJ[0] *= idet;
26219d150b73SToby Isaac         invJ[1] *= idet;
26229d150b73SToby Isaac         invJ[2] *= idet;
26239d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
26249d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
26259d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
26269d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
26279d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
26289d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
26299d150b73SToby Isaac       }
26309d150b73SToby Isaac       break;
26319d150b73SToby Isaac     }
26329d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
26339d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2634c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
26359d150b73SToby Isaac       }
26369d150b73SToby Isaac     }
26379d150b73SToby Isaac   } else {
26389d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
26399d150b73SToby Isaac     char transpose = 'C';
26409d150b73SToby Isaac #else
26419d150b73SToby Isaac     char transpose = 'T';
26429d150b73SToby Isaac #endif
26439d150b73SToby Isaac     PetscBLASInt m = dimR;
26449d150b73SToby Isaac     PetscBLASInt n = dimC;
26459d150b73SToby Isaac     PetscBLASInt one = 1;
26469d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
26479d150b73SToby Isaac 
26489d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
26499d150b73SToby Isaac 
26509d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
26519d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
26529d150b73SToby Isaac 
2653c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
26549d150b73SToby Isaac   }
26559d150b73SToby Isaac   PetscFunctionReturn(0);
26569d150b73SToby Isaac }
26579d150b73SToby Isaac 
26589d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
26599d150b73SToby Isaac {
2660c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
26619d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
26629d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
26639d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
26649d150b73SToby Isaac   PetscErrorCode ierr;
26659d150b73SToby Isaac 
26669d150b73SToby Isaac   PetscFunctionBegin;
26679d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26689d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
266913903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
267069291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
267169291d52SBarry Smith   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
26729d150b73SToby Isaac   cellCoords = &cellData[0];
26739d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
26749d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
26759d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
26769d150b73SToby Isaac   invJ       = &J[dimR * dimC];
26779d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
26789d150b73SToby Isaac   if (dimR == 2) {
26799d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
26809d150b73SToby Isaac 
26819d150b73SToby Isaac     for (i = 0; i < 4; i++) {
26829d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
26839d150b73SToby Isaac 
26849d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26859d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26869d150b73SToby Isaac       }
26879d150b73SToby Isaac     }
26889d150b73SToby Isaac   } else if (dimR == 3) {
26899d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
26909d150b73SToby Isaac 
26919d150b73SToby Isaac     for (i = 0; i < 8; i++) {
26929d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
26939d150b73SToby Isaac 
26949d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26959d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26969d150b73SToby Isaac       }
26979d150b73SToby Isaac     }
26989d150b73SToby Isaac   } else {
26999d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
27009d150b73SToby Isaac   }
27019d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
27029d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
27039d150b73SToby Isaac     PetscReal *swap;
27049d150b73SToby Isaac 
27059d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
27069d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
27079d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
27089d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
27099d150b73SToby Isaac       }
27109d150b73SToby Isaac     }
27119d150b73SToby Isaac 
27129d150b73SToby Isaac     if (i < dimR - 1) {
27139d150b73SToby Isaac       swap = cellCoeffs;
27149d150b73SToby Isaac       cellCoeffs = cellCoords;
27159d150b73SToby Isaac       cellCoords = swap;
27169d150b73SToby Isaac     }
27179d150b73SToby Isaac   }
2718580bdb30SBarry Smith   ierr = PetscArrayzero(refCoords,numPoints * dimR);CHKERRQ(ierr);
27199d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
27209d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
27219d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
27229d150b73SToby Isaac 
27239d150b73SToby Isaac       /* compute -residual and Jacobian */
27249d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
27259d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
27269d150b73SToby Isaac       for (k = 0; k < numV; k++) {
27279d150b73SToby Isaac         PetscReal extCoord = 1.;
27289d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
27299d150b73SToby Isaac           PetscReal coord = guess[l];
27309d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
27319d150b73SToby Isaac 
27329d150b73SToby Isaac           extCoord *= dep * coord + !dep;
27339d150b73SToby Isaac           extJ[l] = dep;
27349d150b73SToby Isaac 
27359d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
27369d150b73SToby Isaac             PetscReal coord = guess[m];
27379d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
27389d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
27399d150b73SToby Isaac 
27409d150b73SToby Isaac             extJ[l] *= mult;
27419d150b73SToby Isaac           }
27429d150b73SToby Isaac         }
27439d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
27449d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
27459d150b73SToby Isaac 
27469d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
27479d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
27489d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
27499d150b73SToby Isaac           }
27509d150b73SToby Isaac         }
27519d150b73SToby Isaac       }
275276bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
27530611203eSToby Isaac         PetscReal maxAbs = 0.;
27540611203eSToby Isaac 
27550611203eSToby Isaac         for (l = 0; l < dimC; l++) {
27560611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
27570611203eSToby Isaac         }
2758087ef6b2SMatthew G. Knepley         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
27590611203eSToby Isaac       }
27609d150b73SToby Isaac 
27619d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
27629d150b73SToby Isaac     }
27639d150b73SToby Isaac   }
276469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
276569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
27669d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
27679d150b73SToby Isaac   PetscFunctionReturn(0);
27689d150b73SToby Isaac }
27699d150b73SToby Isaac 
27709d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
27719d150b73SToby Isaac {
27729d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
27739d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
27749d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
27759d150b73SToby Isaac   PetscErrorCode ierr;
27769d150b73SToby Isaac 
27779d150b73SToby Isaac   PetscFunctionBegin;
27789d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27799d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
278013903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
278169291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
27829d150b73SToby Isaac   cellCoords = &cellData[0];
27839d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
27849d150b73SToby Isaac   if (dimR == 2) {
27859d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
27869d150b73SToby Isaac 
27879d150b73SToby Isaac     for (i = 0; i < 4; i++) {
27889d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
27899d150b73SToby Isaac 
27909d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
27919d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
27929d150b73SToby Isaac       }
27939d150b73SToby Isaac     }
27949d150b73SToby Isaac   } else if (dimR == 3) {
27959d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
27969d150b73SToby Isaac 
27979d150b73SToby Isaac     for (i = 0; i < 8; i++) {
27989d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
27999d150b73SToby Isaac 
28009d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
28019d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
28029d150b73SToby Isaac       }
28039d150b73SToby Isaac     }
28049d150b73SToby Isaac   } else {
28059d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
28069d150b73SToby Isaac   }
28079d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
28089d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
28099d150b73SToby Isaac     PetscReal *swap;
28109d150b73SToby Isaac 
28119d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
28129d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
28139d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
28149d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
28159d150b73SToby Isaac       }
28169d150b73SToby Isaac     }
28179d150b73SToby Isaac 
28189d150b73SToby Isaac     if (i < dimR - 1) {
28199d150b73SToby Isaac       swap = cellCoeffs;
28209d150b73SToby Isaac       cellCoeffs = cellCoords;
28219d150b73SToby Isaac       cellCoords = swap;
28229d150b73SToby Isaac     }
28239d150b73SToby Isaac   }
2824580bdb30SBarry Smith   ierr = PetscArrayzero(realCoords,numPoints * dimC);CHKERRQ(ierr);
28259d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28269d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
28279d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
28289d150b73SToby Isaac 
28299d150b73SToby Isaac     for (k = 0; k < numV; k++) {
28309d150b73SToby Isaac       PetscReal extCoord = 1.;
28319d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
28329d150b73SToby Isaac         PetscReal coord = guess[l];
28339d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
28349d150b73SToby Isaac 
28359d150b73SToby Isaac         extCoord *= dep * coord + !dep;
28369d150b73SToby Isaac       }
28379d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
28389d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
28399d150b73SToby Isaac 
28409d150b73SToby Isaac         mapped[l] += coeff * extCoord;
28419d150b73SToby Isaac       }
28429d150b73SToby Isaac     }
28439d150b73SToby Isaac   }
284469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
28459d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
28469d150b73SToby Isaac   PetscFunctionReturn(0);
28479d150b73SToby Isaac }
28489d150b73SToby Isaac 
28499c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
28509c3cf19fSMatthew 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)
28519d150b73SToby Isaac {
28529c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
2853c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2854c6e120d1SToby Isaac   PetscReal      *invV, *modes;
2855c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
2856c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
28579d150b73SToby Isaac   PetscErrorCode ierr;
28589d150b73SToby Isaac 
28599d150b73SToby Isaac   PetscFunctionBegin;
28609c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
28619d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
28629c3cf19fSMatthew 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);
28639d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
28649d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
286569291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
28669d150b73SToby Isaac   invV = fe->invV;
2867012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2868012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2869012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2870012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
28719d150b73SToby Isaac     }
28729d150b73SToby Isaac   }
287369291d52SBarry Smith   ierr   = DMGetWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
28749c3cf19fSMatthew G. Knepley   D      = &B[pdim*Nc];
28759c3cf19fSMatthew G. Knepley   resNeg = &D[pdim*Nc * dimR];
287669291d52SBarry Smith   ierr = DMGetWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
28779c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
28789c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
28799d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
28809d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28819b1f03cbSToby 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 */
28829d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
28839d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
28849c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc; k++) {resNeg[k] = realCoords[j * Nc + k];}
28859c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc * dimR; k++) {J[k] = 0.;}
28869c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
28879c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
2888012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
28899d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
2890012b7cc6SMatthew G. Knepley             J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
28919d150b73SToby Isaac           }
28929d150b73SToby Isaac         }
28939d150b73SToby Isaac       }
289476bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
28950611203eSToby Isaac         PetscReal maxAbs = 0.;
28960611203eSToby Isaac 
28979c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
28980611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
28990611203eSToby Isaac         }
2900087ef6b2SMatthew G. Knepley         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
29010611203eSToby Isaac       }
29029c3cf19fSMatthew G. Knepley       ierr = DMPlexCoordinatesToReference_NewtonUpdate(Nc,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
29039d150b73SToby Isaac     }
29049d150b73SToby Isaac   }
290569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
290669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
290769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
29089d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
29099d150b73SToby Isaac   PetscFunctionReturn(0);
29109d150b73SToby Isaac }
29119d150b73SToby Isaac 
29129c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
29139c3cf19fSMatthew 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)
29149d150b73SToby Isaac {
29159c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, coordSize;
2916c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2917c6e120d1SToby Isaac   PetscReal      *invV, *modes;
29189d150b73SToby Isaac   PetscReal      *B;
29199d150b73SToby Isaac   PetscErrorCode ierr;
29209d150b73SToby Isaac 
29219d150b73SToby Isaac   PetscFunctionBegin;
29229c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
29239d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
29249c3cf19fSMatthew 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);
29259d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
29269d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
292769291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
29289d150b73SToby Isaac   invV = fe->invV;
2929012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2930012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2931012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2932012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
29339d150b73SToby Isaac     }
29349d150b73SToby Isaac   }
293569291d52SBarry Smith   ierr = DMGetWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
2936012b7cc6SMatthew G. Knepley   ierr = PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL);CHKERRQ(ierr);
29379c3cf19fSMatthew G. Knepley   for (i = 0; i < numPoints * Nc; i++) {realCoords[i] = 0.;}
29389d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
29399c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
29409d150b73SToby Isaac 
29419c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
29429c3cf19fSMatthew G. Knepley       for (l = 0; l < Nc; l++) {
294340cf36b3SToby Isaac         mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
29449d150b73SToby Isaac       }
29459d150b73SToby Isaac     }
29469d150b73SToby Isaac   }
294769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
294869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
29499d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
29509d150b73SToby Isaac   PetscFunctionReturn(0);
29519d150b73SToby Isaac }
29529d150b73SToby Isaac 
2953d6143a4eSToby Isaac /*@
2954d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
2955d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
2956d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
2957d6143a4eSToby Isaac 
2958d6143a4eSToby Isaac   Not collective
2959d6143a4eSToby Isaac 
2960d6143a4eSToby Isaac   Input Parameters:
2961d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2962d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2963d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
2964d6143a4eSToby Isaac . cell       - the cell whose map is used.
2965d6143a4eSToby Isaac . numPoints  - the number of points to locate
29661b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2967d6143a4eSToby Isaac 
2968d6143a4eSToby Isaac   Output Parameters:
2969d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
29701b266c99SBarry Smith 
29711b266c99SBarry Smith   Level: intermediate
297273c9229bSMatthew Knepley 
297373c9229bSMatthew Knepley .seealso: DMPlexReferenceToCoordinates()
2974d6143a4eSToby Isaac @*/
2975d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
2976d6143a4eSToby Isaac {
2977485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
29789d150b73SToby Isaac   DM             coordDM = NULL;
29799d150b73SToby Isaac   Vec            coords;
29809d150b73SToby Isaac   PetscFE        fe = NULL;
29819d150b73SToby Isaac   PetscErrorCode ierr;
29829d150b73SToby Isaac 
2983d6143a4eSToby Isaac   PetscFunctionBegin;
29849d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29859d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
29869d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
29879d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
29889d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
29899d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
29909d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
29919d150b73SToby Isaac   if (coordDM) {
29929d150b73SToby Isaac     PetscInt coordFields;
29939d150b73SToby Isaac 
29949d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
29959d150b73SToby Isaac     if (coordFields) {
29969d150b73SToby Isaac       PetscClassId id;
29979d150b73SToby Isaac       PetscObject  disc;
29989d150b73SToby Isaac 
299944a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
30009d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
30019d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
30029d150b73SToby Isaac         fe = (PetscFE) disc;
30039d150b73SToby Isaac       }
30049d150b73SToby Isaac     }
30059d150b73SToby Isaac   }
3006412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
300713903a91SSatish 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);
30089d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
30099d150b73SToby Isaac     PetscInt  coneSize;
30109d150b73SToby Isaac     PetscBool isSimplex, isTensor;
30119d150b73SToby Isaac 
30129d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
30139d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
30149d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
30159d150b73SToby Isaac     if (isSimplex) {
30169d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
30179d150b73SToby Isaac 
301869291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
30199d150b73SToby Isaac       J    = &v0[dimC];
30209d150b73SToby Isaac       invJ = &J[dimC * dimC];
30219d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
30229d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3023c330f8ffSToby Isaac         const PetscReal x0[3] = {-1.,-1.,-1.};
3024c330f8ffSToby Isaac 
3025c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
30269d150b73SToby Isaac       }
302769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
30289d150b73SToby Isaac     } else if (isTensor) {
30299d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
30309d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
30319d150b73SToby Isaac   } else {
30329d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
30339d150b73SToby Isaac   }
30349d150b73SToby Isaac   PetscFunctionReturn(0);
30359d150b73SToby Isaac }
30369d150b73SToby Isaac 
30379d150b73SToby Isaac /*@
30389d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
30399d150b73SToby Isaac 
30409d150b73SToby Isaac   Not collective
30419d150b73SToby Isaac 
30429d150b73SToby Isaac   Input Parameters:
30439d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
30449d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
30459d150b73SToby Isaac                as a multilinear map for tensor-product elements
30469d150b73SToby Isaac . cell       - the cell whose map is used.
30479d150b73SToby Isaac . numPoints  - the number of points to locate
3048a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
30499d150b73SToby Isaac 
30509d150b73SToby Isaac   Output Parameters:
30519d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
30521b266c99SBarry Smith 
30531b266c99SBarry Smith    Level: intermediate
305473c9229bSMatthew Knepley 
305573c9229bSMatthew Knepley .seealso: DMPlexCoordinatesToReference()
30569d150b73SToby Isaac @*/
30579d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
30589d150b73SToby Isaac {
3059485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
30609d150b73SToby Isaac   DM             coordDM = NULL;
30619d150b73SToby Isaac   Vec            coords;
30629d150b73SToby Isaac   PetscFE        fe = NULL;
30639d150b73SToby Isaac   PetscErrorCode ierr;
30649d150b73SToby Isaac 
30659d150b73SToby Isaac   PetscFunctionBegin;
30669d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30679d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
30689d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
30699d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
30709d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
30719d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
30729d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
30739d150b73SToby Isaac   if (coordDM) {
30749d150b73SToby Isaac     PetscInt coordFields;
30759d150b73SToby Isaac 
30769d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
30779d150b73SToby Isaac     if (coordFields) {
30789d150b73SToby Isaac       PetscClassId id;
30799d150b73SToby Isaac       PetscObject  disc;
30809d150b73SToby Isaac 
308144a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
30829d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
30839d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
30849d150b73SToby Isaac         fe = (PetscFE) disc;
30859d150b73SToby Isaac       }
30869d150b73SToby Isaac     }
30879d150b73SToby Isaac   }
3088412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
308913903a91SSatish 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);
30909d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
30919d150b73SToby Isaac     PetscInt  coneSize;
30929d150b73SToby Isaac     PetscBool isSimplex, isTensor;
30939d150b73SToby Isaac 
30949d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
30959d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
30969d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
30979d150b73SToby Isaac     if (isSimplex) {
30989d150b73SToby Isaac       PetscReal detJ, *v0, *J;
30999d150b73SToby Isaac 
310069291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
31019d150b73SToby Isaac       J    = &v0[dimC];
31029d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
3103c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3104c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1.,-1.,-1.};
3105c330f8ffSToby Isaac 
3106c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
31079d150b73SToby Isaac       }
310869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
31099d150b73SToby Isaac     } else if (isTensor) {
31109d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
31119d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
31129d150b73SToby Isaac   } else {
31139d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
31149d150b73SToby Isaac   }
3115d6143a4eSToby Isaac   PetscFunctionReturn(0);
3116d6143a4eSToby Isaac }
31170139fca9SMatthew G. Knepley 
31180139fca9SMatthew G. Knepley /*@C
31190139fca9SMatthew G. Knepley   DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates.
31200139fca9SMatthew G. Knepley 
31210139fca9SMatthew G. Knepley   Not collective
31220139fca9SMatthew G. Knepley 
31230139fca9SMatthew G. Knepley   Input Parameters:
31240139fca9SMatthew G. Knepley + dm      - The DM
31250139fca9SMatthew G. Knepley . time    - The time
31260139fca9SMatthew G. Knepley - func    - The function transforming current coordinates to new coordaintes
31270139fca9SMatthew G. Knepley 
31280139fca9SMatthew G. Knepley    Calling sequence of func:
31290139fca9SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
31300139fca9SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
31310139fca9SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
31320139fca9SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
31330139fca9SMatthew G. Knepley 
31340139fca9SMatthew G. Knepley +  dim          - The spatial dimension
31350139fca9SMatthew G. Knepley .  Nf           - The number of input fields (here 1)
31360139fca9SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
31370139fca9SMatthew G. Knepley .  uOff         - The offset of the coordinates in u[] (here 0)
31380139fca9SMatthew G. Knepley .  uOff_x       - The offset of the coordinates in u_x[] (here 0)
31390139fca9SMatthew G. Knepley .  u            - The coordinate values at this point in space
31400139fca9SMatthew G. Knepley .  u_t          - The coordinate time derivative at this point in space (here NULL)
31410139fca9SMatthew G. Knepley .  u_x          - The coordinate derivatives at this point in space
31420139fca9SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
31430139fca9SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
31440139fca9SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
31450139fca9SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
31460139fca9SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
31470139fca9SMatthew G. Knepley .  t            - The current time
31480139fca9SMatthew G. Knepley .  x            - The coordinates of this point (here not used)
31490139fca9SMatthew G. Knepley .  numConstants - The number of constants
31500139fca9SMatthew G. Knepley .  constants    - The value of each constant
31510139fca9SMatthew G. Knepley -  f            - The new coordinates at this point in space
31520139fca9SMatthew G. Knepley 
31530139fca9SMatthew G. Knepley   Level: intermediate
31540139fca9SMatthew G. Knepley 
31550139fca9SMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMProjectFieldLocal(), DMProjectFieldLabelLocal()
31560139fca9SMatthew G. Knepley @*/
31570139fca9SMatthew G. Knepley PetscErrorCode DMPlexRemapGeometry(DM dm, PetscReal time,
31580139fca9SMatthew G. Knepley                                    void (*func)(PetscInt, PetscInt, PetscInt,
31590139fca9SMatthew G. Knepley                                                 const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
31600139fca9SMatthew G. Knepley                                                 const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
31610139fca9SMatthew G. Knepley                                                 PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]))
31620139fca9SMatthew G. Knepley {
31630139fca9SMatthew G. Knepley   DM             cdm;
31648bf1a49fSMatthew G. Knepley   DMField        cf;
31650139fca9SMatthew G. Knepley   Vec            lCoords, tmpCoords;
31660139fca9SMatthew G. Knepley   PetscErrorCode ierr;
31670139fca9SMatthew G. Knepley 
31680139fca9SMatthew G. Knepley   PetscFunctionBegin;
31690139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
31700139fca9SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &lCoords);CHKERRQ(ierr);
31710139fca9SMatthew G. Knepley   ierr = DMGetLocalVector(cdm, &tmpCoords);CHKERRQ(ierr);
31720139fca9SMatthew G. Knepley   ierr = VecCopy(lCoords, tmpCoords);CHKERRQ(ierr);
31738bf1a49fSMatthew G. Knepley   /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */
31748bf1a49fSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &cf);CHKERRQ(ierr);
31758bf1a49fSMatthew G. Knepley   cdm->coordinateField = cf;
31760139fca9SMatthew G. Knepley   ierr = DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords);CHKERRQ(ierr);
31778bf1a49fSMatthew G. Knepley   cdm->coordinateField = NULL;
31780139fca9SMatthew G. Knepley   ierr = DMRestoreLocalVector(cdm, &tmpCoords);CHKERRQ(ierr);
31790139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
31800139fca9SMatthew G. Knepley }
31810139fca9SMatthew G. Knepley 
31820139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z,
31830139fca9SMatthew G. Knepley   / 1  0  m_0 \
31840139fca9SMatthew G. Knepley   | 0  1  m_1 |
31850139fca9SMatthew G. Knepley   \ 0  0   1  /
31860139fca9SMatthew G. Knepley */
31870139fca9SMatthew G. Knepley static void f0_shear(PetscInt dim, PetscInt Nf, PetscInt NfAux,
31880139fca9SMatthew G. Knepley                      const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
31890139fca9SMatthew G. Knepley                      const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
31900139fca9SMatthew G. Knepley                      PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar coords[])
31910139fca9SMatthew G. Knepley {
31920139fca9SMatthew G. Knepley   const PetscInt Nc = uOff[1]-uOff[0];
3193c1f1bd54SMatthew G. Knepley   const PetscInt ax = (PetscInt) PetscRealPart(constants[0]);
31940139fca9SMatthew G. Knepley   PetscInt       c;
31950139fca9SMatthew G. Knepley 
31960139fca9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
31970139fca9SMatthew G. Knepley     coords[c] = u[c] + constants[c+1]*u[ax];
31980139fca9SMatthew G. Knepley   }
31990139fca9SMatthew G. Knepley }
32000139fca9SMatthew G. Knepley 
32010139fca9SMatthew G. Knepley /*@C
32020139fca9SMatthew G. Knepley   DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates.
32030139fca9SMatthew G. Knepley 
32040139fca9SMatthew G. Knepley   Not collective
32050139fca9SMatthew G. Knepley 
32060139fca9SMatthew G. Knepley   Input Parameters:
32070139fca9SMatthew G. Knepley + dm          - The DM
32083ee9839eSMatthew G. Knepley . direction   - The shear coordinate direction, e.g. 0 is the x-axis
32090139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction
32100139fca9SMatthew G. Knepley 
32110139fca9SMatthew G. Knepley   Level: intermediate
32120139fca9SMatthew G. Knepley 
32130139fca9SMatthew G. Knepley .seealso: DMPlexRemapGeometry()
32140139fca9SMatthew G. Knepley @*/
32153ee9839eSMatthew G. Knepley PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[])
32160139fca9SMatthew G. Knepley {
32170139fca9SMatthew G. Knepley   DM             cdm;
32180139fca9SMatthew G. Knepley   PetscDS        cds;
32190139fca9SMatthew G. Knepley   PetscObject    obj;
32200139fca9SMatthew G. Knepley   PetscClassId   id;
32210139fca9SMatthew G. Knepley   PetscScalar   *moduli;
32223ee9839eSMatthew G. Knepley   const PetscInt dir = (PetscInt) direction;
32230139fca9SMatthew G. Knepley   PetscInt       dE, d, e;
32240139fca9SMatthew G. Knepley   PetscErrorCode ierr;
32250139fca9SMatthew G. Knepley 
32260139fca9SMatthew G. Knepley   PetscFunctionBegin;
32270139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
32280139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
32290139fca9SMatthew G. Knepley   ierr = PetscMalloc1(dE+1, &moduli);CHKERRQ(ierr);
32300139fca9SMatthew G. Knepley   moduli[0] = dir;
32310139fca9SMatthew G. Knepley   for (d = 0, e = 0; d < dE; ++d) moduli[d] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0);
32320139fca9SMatthew G. Knepley   ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
32330139fca9SMatthew G. Knepley   ierr = PetscDSGetDiscretization(cds, 0, &obj);CHKERRQ(ierr);
32340139fca9SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
32350139fca9SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
32360139fca9SMatthew G. Knepley     Vec           lCoords;
32370139fca9SMatthew G. Knepley     PetscSection  cSection;
32380139fca9SMatthew G. Knepley     PetscScalar  *coords;
32390139fca9SMatthew G. Knepley     PetscInt      vStart, vEnd, v;
32400139fca9SMatthew G. Knepley 
32410139fca9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
32420139fca9SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &cSection);CHKERRQ(ierr);
32430139fca9SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &lCoords);CHKERRQ(ierr);
32440139fca9SMatthew G. Knepley     ierr = VecGetArray(lCoords, &coords);CHKERRQ(ierr);
32450139fca9SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
32460139fca9SMatthew G. Knepley       PetscReal ds;
32470139fca9SMatthew G. Knepley       PetscInt  off, c;
32480139fca9SMatthew G. Knepley 
32490139fca9SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, v, &off);CHKERRQ(ierr);
32500139fca9SMatthew G. Knepley       ds   = PetscRealPart(coords[off+dir]);
32510139fca9SMatthew G. Knepley       for (c = 0; c < dE; ++c) coords[off+c] += moduli[c]*ds;
32520139fca9SMatthew G. Knepley     }
32530139fca9SMatthew G. Knepley     ierr = VecRestoreArray(lCoords, &coords);CHKERRQ(ierr);
32540139fca9SMatthew G. Knepley   } else {
32550139fca9SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, dE+1, moduli);CHKERRQ(ierr);
32560139fca9SMatthew G. Knepley     ierr = DMPlexRemapGeometry(dm, 0.0, f0_shear);CHKERRQ(ierr);
32570139fca9SMatthew G. Knepley   }
32580139fca9SMatthew G. Knepley   ierr = PetscFree(moduli);CHKERRQ(ierr);
32590139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
32600139fca9SMatthew G. Knepley }
3261