xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 4165533ce9fbcd1440639b2ad505faa054a49d0c)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
29d150b73SToby Isaac #include <petsc/private/petscfeimpl.h>  /*I      "petscfe.h"       I*/
39d150b73SToby Isaac #include <petscblaslapack.h>
4af74b616SDave May #include <petsctime.h>
5ccd2543fSMatthew G Knepley 
63985bb02SVaclav Hapla /*@
73985bb02SVaclav Hapla   DMPlexFindVertices - Try to find DAG points based on their coordinates.
83985bb02SVaclav Hapla 
93985bb02SVaclav Hapla   Not Collective (provided DMGetCoordinatesLocalSetUp() has been called already)
103985bb02SVaclav Hapla 
113985bb02SVaclav Hapla   Input Parameters:
123985bb02SVaclav Hapla + dm - The DMPlex object
133985bb02SVaclav Hapla . npoints - The number of sought points
143985bb02SVaclav Hapla . coords - The array of coordinates of the sought points
153985bb02SVaclav Hapla - eps - The tolerance or PETSC_DEFAULT
163985bb02SVaclav Hapla 
173985bb02SVaclav Hapla   Output Parameters:
183985bb02SVaclav Hapla . dagPoints - The array of found DAG points, or -1 if not found
193985bb02SVaclav Hapla 
203985bb02SVaclav Hapla   Level: intermediate
213985bb02SVaclav Hapla 
223985bb02SVaclav Hapla   Notes:
233985bb02SVaclav Hapla   The length of the array coords must be npoints * dim where dim is the spatial dimension returned by DMGetDimension().
243985bb02SVaclav Hapla 
253985bb02SVaclav Hapla   The output array dagPoints is NOT newly allocated; the user must pass an array of length npoints.
263985bb02SVaclav Hapla 
273985bb02SVaclav Hapla   Each rank does the search independently; a nonnegative value is returned only if this rank's local DMPlex portion contains the point.
283985bb02SVaclav Hapla 
293985bb02SVaclav Hapla   The tolerance is interpreted as the maximum Euclidean (L2) distance of the sought point from the specified coordinates.
303985bb02SVaclav Hapla 
31335ef845SVaclav Hapla   Complexity of this function is currently O(mn) with m number of vertices to find and n number of vertices in the local mesh. This could probably be improved.
32335ef845SVaclav Hapla 
3337900f7dSMatthew G. Knepley .seealso: DMPlexCreate(), DMGetCoordinatesLocal()
343985bb02SVaclav Hapla @*/
353985bb02SVaclav Hapla PetscErrorCode DMPlexFindVertices(DM dm, PetscInt npoints, const PetscReal coord[], PetscReal eps, PetscInt dagPoints[])
363985bb02SVaclav Hapla {
3737900f7dSMatthew G. Knepley   PetscInt          c, cdim, i, j, o, p, vStart, vEnd;
383985bb02SVaclav Hapla   Vec               allCoordsVec;
393985bb02SVaclav Hapla   const PetscScalar *allCoords;
403985bb02SVaclav Hapla   PetscReal         norm;
413985bb02SVaclav Hapla   PetscErrorCode    ierr;
423985bb02SVaclav Hapla 
433985bb02SVaclav Hapla   PetscFunctionBegin;
443985bb02SVaclav Hapla   if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON;
4537900f7dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
463985bb02SVaclav Hapla   ierr = DMGetCoordinatesLocal(dm, &allCoordsVec);CHKERRQ(ierr);
473985bb02SVaclav Hapla   ierr = VecGetArrayRead(allCoordsVec, &allCoords);CHKERRQ(ierr);
483985bb02SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
4976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
50335ef845SVaclav Hapla     /* check coordinate section is consistent with DM dimension */
51335ef845SVaclav Hapla     PetscSection      cs;
52335ef845SVaclav Hapla     PetscInt          ndof;
53335ef845SVaclav Hapla 
54335ef845SVaclav Hapla     ierr = DMGetCoordinateSection(dm, &cs);CHKERRQ(ierr);
553985bb02SVaclav Hapla     for (p = vStart; p < vEnd; p++) {
563985bb02SVaclav Hapla       ierr = PetscSectionGetDof(cs, p, &ndof);CHKERRQ(ierr);
5737900f7dSMatthew G. Knepley       if (PetscUnlikely(ndof != cdim)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %D: ndof = %D != %D = cdim", p, ndof, cdim);
58335ef845SVaclav Hapla     }
59335ef845SVaclav Hapla   }
60eca9f518SVaclav Hapla   if (eps == 0.0) {
6137900f7dSMatthew G. Knepley     for (i=0,j=0; i < npoints; i++,j+=cdim) {
62eca9f518SVaclav Hapla       dagPoints[i] = -1;
6337900f7dSMatthew G. Knepley       for (p = vStart,o=0; p < vEnd; p++,o+=cdim) {
6437900f7dSMatthew G. Knepley         for (c = 0; c < cdim; c++) {
65eca9f518SVaclav Hapla           if (coord[j+c] != PetscRealPart(allCoords[o+c])) break;
66eca9f518SVaclav Hapla         }
6737900f7dSMatthew G. Knepley         if (c == cdim) {
68eca9f518SVaclav Hapla           dagPoints[i] = p;
69eca9f518SVaclav Hapla           break;
70eca9f518SVaclav Hapla         }
71eca9f518SVaclav Hapla       }
72eca9f518SVaclav Hapla     }
73eca9f518SVaclav Hapla     ierr = VecRestoreArrayRead(allCoordsVec, &allCoords);CHKERRQ(ierr);
74eca9f518SVaclav Hapla     PetscFunctionReturn(0);
75eca9f518SVaclav Hapla   }
7637900f7dSMatthew G. Knepley   for (i=0,j=0; i < npoints; i++,j+=cdim) {
77335ef845SVaclav Hapla     dagPoints[i] = -1;
7837900f7dSMatthew G. Knepley     for (p = vStart,o=0; p < vEnd; p++,o+=cdim) {
793985bb02SVaclav Hapla       norm = 0.0;
8037900f7dSMatthew G. Knepley       for (c = 0; c < cdim; c++) {
813985bb02SVaclav Hapla         norm += PetscSqr(coord[j+c] - PetscRealPart(allCoords[o+c]));
823985bb02SVaclav Hapla       }
833985bb02SVaclav Hapla       norm = PetscSqrtReal(norm);
843985bb02SVaclav Hapla       if (norm <= eps) {
853985bb02SVaclav Hapla         dagPoints[i] = p;
863985bb02SVaclav Hapla         break;
873985bb02SVaclav Hapla       }
883985bb02SVaclav Hapla     }
893985bb02SVaclav Hapla   }
903985bb02SVaclav Hapla   ierr = VecRestoreArrayRead(allCoordsVec, &allCoords);CHKERRQ(ierr);
913985bb02SVaclav Hapla   PetscFunctionReturn(0);
923985bb02SVaclav Hapla }
933985bb02SVaclav Hapla 
94fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
95fea14342SMatthew G. Knepley {
96fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*2+0];
97fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*2+1];
98fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*2+0];
99fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*2+1];
100fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0*2+0];
101fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0*2+1];
102fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1*2+0];
103fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1*2+1];
104fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
105fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
106fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
107fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
108fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
109fea14342SMatthew G. Knepley 
110fea14342SMatthew G. Knepley   PetscFunctionBegin;
111fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
112fea14342SMatthew G. Knepley   /* Non-parallel lines */
113fea14342SMatthew G. Knepley   if (denom != 0.0) {
114fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
115fea14342SMatthew G. Knepley     const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
116fea14342SMatthew G. Knepley 
117fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
118fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
119fea14342SMatthew G. Knepley       if (intersection) {
120fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
121fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
122fea14342SMatthew G. Knepley       }
123fea14342SMatthew G. Knepley     }
124fea14342SMatthew G. Knepley   }
125fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
126fea14342SMatthew G. Knepley }
127fea14342SMatthew G. Knepley 
12814bbb9f0SLawrence Mitchell static PetscErrorCode DMPlexLocatePoint_Simplex_1D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
12914bbb9f0SLawrence Mitchell {
13014bbb9f0SLawrence Mitchell   const PetscReal eps = PETSC_SQRT_MACHINE_EPSILON;
13114bbb9f0SLawrence Mitchell   const PetscReal x   = PetscRealPart(point[0]);
13214bbb9f0SLawrence Mitchell   PetscReal       v0, J, invJ, detJ;
13314bbb9f0SLawrence Mitchell   PetscReal       xi;
13414bbb9f0SLawrence Mitchell   PetscErrorCode  ierr;
13514bbb9f0SLawrence Mitchell 
13614bbb9f0SLawrence Mitchell   PetscFunctionBegin;
13714bbb9f0SLawrence Mitchell   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0, &J, &invJ, &detJ);CHKERRQ(ierr);
13814bbb9f0SLawrence Mitchell   xi   = invJ*(x - v0);
13914bbb9f0SLawrence Mitchell 
14014bbb9f0SLawrence Mitchell   if ((xi >= -eps) && (xi <= 2.+eps)) *cell = c;
14114bbb9f0SLawrence Mitchell   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
14214bbb9f0SLawrence Mitchell   PetscFunctionReturn(0);
14314bbb9f0SLawrence Mitchell }
14414bbb9f0SLawrence Mitchell 
145ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
146ccd2543fSMatthew G Knepley {
147ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
148f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
149ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
150ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
151ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
152ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
153ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
154ccd2543fSMatthew G Knepley 
155ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1568e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
157ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
158ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
159ccd2543fSMatthew G Knepley 
160f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
161c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
162ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
163ccd2543fSMatthew G Knepley }
164ccd2543fSMatthew G Knepley 
16562a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
16662a38674SMatthew G. Knepley {
16762a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
16862a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
16962a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
17062a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
17162a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
17262a38674SMatthew G. Knepley   PetscErrorCode  ierr;
17362a38674SMatthew G. Knepley 
17462a38674SMatthew G. Knepley   PetscFunctionBegin;
17562a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
17662a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
17762a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
17862a38674SMatthew G. Knepley 
17962a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
18062a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
18162a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
18262a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
18362a38674SMatthew G. Knepley     xi  /= r;
18462a38674SMatthew G. Knepley     eta /= r;
18562a38674SMatthew G. Knepley   }
18662a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
18762a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
18862a38674SMatthew G. Knepley   PetscFunctionReturn(0);
18962a38674SMatthew G. Knepley }
19062a38674SMatthew G. Knepley 
191ba2698f1SMatthew G. Knepley static PetscErrorCode DMPlexLocatePoint_Quad_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
192ccd2543fSMatthew G Knepley {
193ccd2543fSMatthew G Knepley   PetscSection       coordSection;
194ccd2543fSMatthew G Knepley   Vec             coordsLocal;
195a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
196ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
197ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
198ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
199ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
200ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
201ccd2543fSMatthew G Knepley 
202ccd2543fSMatthew G Knepley   PetscFunctionBegin;
203ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
20469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
205ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
206ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
207ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
208ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
209ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
210ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
211ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
212ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
213ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
214ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
215ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
216ccd2543fSMatthew G Knepley   }
217ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
218c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
219ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
220ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
221ccd2543fSMatthew G Knepley }
222ccd2543fSMatthew G Knepley 
223ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
224ccd2543fSMatthew G Knepley {
225ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 3;
22637900f7dSMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
227ccd2543fSMatthew G Knepley   PetscReal       v0[3], J[9], invJ[9], detJ;
228ccd2543fSMatthew G Knepley   PetscReal       x = PetscRealPart(point[0]);
229ccd2543fSMatthew G Knepley   PetscReal       y = PetscRealPart(point[1]);
230ccd2543fSMatthew G Knepley   PetscReal       z = PetscRealPart(point[2]);
231ccd2543fSMatthew G Knepley   PetscReal       xi, eta, zeta;
232ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
233ccd2543fSMatthew G Knepley 
234ccd2543fSMatthew G Knepley   PetscFunctionBegin;
2358e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
236ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
237ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
238ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
239ccd2543fSMatthew G Knepley 
24037900f7dSMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (zeta >= -eps) && (xi + eta + zeta <= 2.0+eps)) *cell = c;
241c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
242ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
243ccd2543fSMatthew G Knepley }
244ccd2543fSMatthew G Knepley 
245ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
246ccd2543fSMatthew G Knepley {
247ccd2543fSMatthew G Knepley   PetscSection   coordSection;
248ccd2543fSMatthew G Knepley   Vec            coordsLocal;
249872a9804SMatthew G. Knepley   PetscScalar   *coords = NULL;
250fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
251fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
252ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
253ccd2543fSMatthew G Knepley   PetscInt       f;
254ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
255ccd2543fSMatthew G Knepley 
256ccd2543fSMatthew G Knepley   PetscFunctionBegin;
257ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
25869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
259ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
260ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
261ccd2543fSMatthew G Knepley     /* Check the point is under plane */
262ccd2543fSMatthew G Knepley     /*   Get face normal */
263ccd2543fSMatthew G Knepley     PetscReal v_i[3];
264ccd2543fSMatthew G Knepley     PetscReal v_j[3];
265ccd2543fSMatthew G Knepley     PetscReal normal[3];
266ccd2543fSMatthew G Knepley     PetscReal pp[3];
267ccd2543fSMatthew G Knepley     PetscReal dot;
268ccd2543fSMatthew G Knepley 
269ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
270ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
271ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
272ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
273ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
274ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
275ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
276ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
277ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
278ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
279ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
280ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
281ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
282ccd2543fSMatthew G Knepley 
283ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
284ccd2543fSMatthew G Knepley     if (dot < 0.0) {
285ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
286ccd2543fSMatthew G Knepley       break;
287ccd2543fSMatthew G Knepley     }
288ccd2543fSMatthew G Knepley   }
289ccd2543fSMatthew G Knepley   if (found) *cell = c;
290c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
291ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
292ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
293ccd2543fSMatthew G Knepley }
294ccd2543fSMatthew G Knepley 
295c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
296c4eade1cSMatthew G. Knepley {
297c4eade1cSMatthew G. Knepley   PetscInt d;
298c4eade1cSMatthew G. Knepley 
299c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
300c4eade1cSMatthew G. Knepley   box->dim = dim;
301c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
302c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
303c4eade1cSMatthew G. Knepley }
304c4eade1cSMatthew G. Knepley 
305c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
306c4eade1cSMatthew G. Knepley {
307c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
308c4eade1cSMatthew G. Knepley 
309c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
310c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
311c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
312c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
313c4eade1cSMatthew G. Knepley }
314c4eade1cSMatthew G. Knepley 
315c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
316c4eade1cSMatthew G. Knepley {
317c4eade1cSMatthew G. Knepley   PetscInt d;
318c4eade1cSMatthew G. Knepley 
319c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
320c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
321c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
322c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
323c4eade1cSMatthew G. Knepley   }
324c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
325c4eade1cSMatthew G. Knepley }
326c4eade1cSMatthew G. Knepley 
32762a38674SMatthew G. Knepley /*
32862a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
32962a38674SMatthew G. Knepley 
33062a38674SMatthew G. Knepley   Not collective
33162a38674SMatthew G. Knepley 
33262a38674SMatthew G. Knepley   Input Parameters:
33362a38674SMatthew G. Knepley + box - The grid hash object
33462a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
33562a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
33662a38674SMatthew G. Knepley 
33762a38674SMatthew G. Knepley   Level: developer
33862a38674SMatthew G. Knepley 
33962a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
34062a38674SMatthew G. Knepley */
341c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
342c4eade1cSMatthew G. Knepley {
343c4eade1cSMatthew G. Knepley   PetscInt d;
344c4eade1cSMatthew G. Knepley 
345c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
346c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
347c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
348c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
349c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
350c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
351c4eade1cSMatthew G. Knepley     } else {
352c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
353c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
354c4eade1cSMatthew G. Knepley     }
355c4eade1cSMatthew G. Knepley   }
356c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
357c4eade1cSMatthew G. Knepley }
358c4eade1cSMatthew G. Knepley 
35962a38674SMatthew G. Knepley /*
36062a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
36162a38674SMatthew G. Knepley 
36262a38674SMatthew G. Knepley   Not collective
36362a38674SMatthew G. Knepley 
36462a38674SMatthew G. Knepley   Input Parameters:
36562a38674SMatthew G. Knepley + box       - The grid hash object
36662a38674SMatthew G. Knepley . numPoints - The number of input points
36762a38674SMatthew G. Knepley - points    - The input point coordinates
36862a38674SMatthew G. Knepley 
36962a38674SMatthew G. Knepley   Output Parameters:
37062a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
37162a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
37262a38674SMatthew G. Knepley 
37362a38674SMatthew G. Knepley   Level: developer
37462a38674SMatthew G. Knepley 
37562a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
37662a38674SMatthew G. Knepley */
3771c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
378c4eade1cSMatthew G. Knepley {
379c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
380c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
381c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
382c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
383c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
384c4eade1cSMatthew G. Knepley   PetscInt         d, p;
385c4eade1cSMatthew G. Knepley 
386c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
387c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
388c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
3891c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
390c4eade1cSMatthew G. Knepley 
3911c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
3922a705cacSMatthew G. Knepley       if (dbox == -1   && PetscAbsReal(PetscRealPart(points[p*dim+d]) - lower[d]) < 1.0e-9) dbox = 0;
393c4eade1cSMatthew 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",
394087ef6b2SMatthew 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);
395c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
396c4eade1cSMatthew G. Knepley     }
397c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
398c4eade1cSMatthew G. Knepley   }
399c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
400c4eade1cSMatthew G. Knepley }
401c4eade1cSMatthew G. Knepley 
402af74b616SDave May /*
403af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
404af74b616SDave May 
405af74b616SDave May  Not collective
406af74b616SDave May 
407af74b616SDave May   Input Parameters:
408af74b616SDave May + box       - The grid hash object
409af74b616SDave May . numPoints - The number of input points
410af74b616SDave May - points    - The input point coordinates
411af74b616SDave May 
412af74b616SDave May   Output Parameters:
413af74b616SDave May + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
414af74b616SDave May . boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
415af74b616SDave May - found     - Flag indicating if point was located within a box
416af74b616SDave May 
417af74b616SDave May   Level: developer
418af74b616SDave May 
419af74b616SDave May .seealso: PetscGridHashGetEnclosingBox()
420af74b616SDave May */
421af74b616SDave May PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[],PetscBool *found)
422af74b616SDave May {
423af74b616SDave May   const PetscReal *lower = box->lower;
424af74b616SDave May   const PetscReal *upper = box->upper;
425af74b616SDave May   const PetscReal *h     = box->h;
426af74b616SDave May   const PetscInt  *n     = box->n;
427af74b616SDave May   const PetscInt   dim   = box->dim;
428af74b616SDave May   PetscInt         d, p;
429af74b616SDave May 
430af74b616SDave May   PetscFunctionBegin;
431af74b616SDave May   *found = PETSC_FALSE;
432af74b616SDave May   for (p = 0; p < numPoints; ++p) {
433af74b616SDave May     for (d = 0; d < dim; ++d) {
434af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
435af74b616SDave May 
436af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
437af74b616SDave May       if (dbox < 0 || dbox >= n[d]) {
438af74b616SDave May         PetscFunctionReturn(0);
439af74b616SDave May       }
440af74b616SDave May       dboxes[p*dim+d] = dbox;
441af74b616SDave May     }
442af74b616SDave May     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
443af74b616SDave May   }
444af74b616SDave May   *found = PETSC_TRUE;
445af74b616SDave May   PetscFunctionReturn(0);
446af74b616SDave May }
447af74b616SDave May 
448c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
449c4eade1cSMatthew G. Knepley {
450c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
451c4eade1cSMatthew G. Knepley 
452c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
453c4eade1cSMatthew G. Knepley   if (*box) {
454c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
455c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
456c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
457c4eade1cSMatthew G. Knepley   }
458c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
459c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
460c4eade1cSMatthew G. Knepley }
461c4eade1cSMatthew G. Knepley 
462cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
463cafe43deSMatthew G. Knepley {
464ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
465cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
466cafe43deSMatthew G. Knepley 
467cafe43deSMatthew G. Knepley   PetscFunctionBegin;
468ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cellStart, &ct);CHKERRQ(ierr);
469ba2698f1SMatthew G. Knepley   switch (ct) {
47014bbb9f0SLawrence Mitchell     case DM_POLYTOPE_SEGMENT:
47114bbb9f0SLawrence Mitchell     ierr = DMPlexLocatePoint_Simplex_1D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
472ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
473ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
474ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
475ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Quad_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
476ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
477ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
478ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
479ba2698f1SMatthew G. Knepley     ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);break;
480ba2698f1SMatthew G. Knepley     default: SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell %D with type %s", cellStart, DMPolytopeTypes[ct]);
481cafe43deSMatthew G. Knepley   }
482cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
483cafe43deSMatthew G. Knepley }
484cafe43deSMatthew G. Knepley 
48562a38674SMatthew G. Knepley /*
48662a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
48762a38674SMatthew G. Knepley */
48862a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
48962a38674SMatthew G. Knepley {
490ba2698f1SMatthew G. Knepley   DMPolytopeType ct;
49162a38674SMatthew G. Knepley   PetscErrorCode ierr;
49262a38674SMatthew G. Knepley 
49362a38674SMatthew G. Knepley   PetscFunctionBegin;
494ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
495ba2698f1SMatthew G. Knepley   switch (ct) {
496ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
497ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
49862a38674SMatthew G. Knepley #if 0
499ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
500ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
501ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
502ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
503ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
504ba2698f1SMatthew G. Knepley     ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);break;
50562a38674SMatthew G. Knepley #endif
506ba2698f1SMatthew G. Knepley     default: SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell %D with type %s", cell, DMPolytopeTypes[ct]);
50762a38674SMatthew G. Knepley   }
50862a38674SMatthew G. Knepley   PetscFunctionReturn(0);
50962a38674SMatthew G. Knepley }
51062a38674SMatthew G. Knepley 
51162a38674SMatthew G. Knepley /*
51262a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
51362a38674SMatthew G. Knepley 
514d083f849SBarry Smith   Collective on dm
51562a38674SMatthew G. Knepley 
51662a38674SMatthew G. Knepley   Input Parameter:
51762a38674SMatthew G. Knepley . dm - The Plex
51862a38674SMatthew G. Knepley 
51962a38674SMatthew G. Knepley   Output Parameter:
52062a38674SMatthew G. Knepley . localBox - The grid hash object
52162a38674SMatthew G. Knepley 
52262a38674SMatthew G. Knepley   Level: developer
52362a38674SMatthew G. Knepley 
52462a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
52562a38674SMatthew G. Knepley */
526cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
527cafe43deSMatthew G. Knepley {
528cafe43deSMatthew G. Knepley   MPI_Comm           comm;
529cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
530cafe43deSMatthew G. Knepley   Vec                coordinates;
531cafe43deSMatthew G. Knepley   PetscSection       coordSection;
532cafe43deSMatthew G. Knepley   Vec                coordsLocal;
533cafe43deSMatthew G. Knepley   const PetscScalar *coords;
534722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
535cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
536412e9a14SMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, c, i;
537cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
538cafe43deSMatthew G. Knepley 
539cafe43deSMatthew G. Knepley   PetscFunctionBegin;
540cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
541cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
542cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
5435b3353d8SMatthew G. Knepley   if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D");
544cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
545cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
546cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
547cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
548cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
5499cb35068SDave May   ierr = PetscOptionsGetInt(NULL,NULL,"-dm_plex_hash_box_nijk",&n[0],NULL);CHKERRQ(ierr);
5509cb35068SDave May   n[1] = n[0];
5519cb35068SDave May   n[2] = n[0];
552cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
553cafe43deSMatthew G. Knepley #if 0
554cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
555820f2d46SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRMPI(ierr);
556820f2d46SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRMPI(ierr);
557cafe43deSMatthew G. Knepley #endif
558cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
559cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
560cafe43deSMatthew G. Knepley   /* Create label */
561412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
562d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse);CHKERRQ(ierr);
563cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
564a8d69d7bSBarry Smith   /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
565cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
566cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
56738353de4SMatthew G. Knepley   ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr);
568cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
569cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
570cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
57138353de4SMatthew G. Knepley     PetscInt         csize   = 0;
572cafe43deSMatthew G. Knepley     PetscScalar      point[3];
573cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
574cafe43deSMatthew G. Knepley 
575cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
57638353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
57738353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
578722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
57938353de4SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);}
580cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
581cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
5822291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
583cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
584cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
585cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
586cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
587cafe43deSMatthew G. Knepley       }
588cafe43deSMatthew G. Knepley     }
589fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
590cafe43deSMatthew 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]) {
591cafe43deSMatthew 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]) {
592cafe43deSMatthew 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]) {
593cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
594cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
595fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
596cafe43deSMatthew G. Knepley 
597fea14342SMatthew G. Knepley           /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
598cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
599cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
600cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
601cafe43deSMatthew G. Knepley 
602cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
603367003a6SStefano Zampini                 if (cell >= 0) { ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
604cafe43deSMatthew G. Knepley               }
605cafe43deSMatthew G. Knepley             }
606cafe43deSMatthew G. Knepley           }
607fea14342SMatthew G. Knepley           /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */
608fea14342SMatthew G. Knepley           for (edge = 0; edge < dim+1; ++edge) {
609fea14342SMatthew G. Knepley             PetscReal segA[6], segB[6];
610fea14342SMatthew G. Knepley 
611aab5bcd8SJed Brown             if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected dim %d > 3",dim);
612fea14342SMatthew 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]);}
613fea14342SMatthew G. Knepley             for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) {
6149a128ed2SMatthew G. Knepley               if (dim > 2) {segB[2]     = PetscRealPart(point[2]);
6159a128ed2SMatthew G. Knepley                             segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];}
616fea14342SMatthew G. Knepley               for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) {
6179a128ed2SMatthew G. Knepley                 if (dim > 1) {segB[1]     = PetscRealPart(point[1]);
6189a128ed2SMatthew G. Knepley                               segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];}
619fea14342SMatthew G. Knepley                 for (ii = 0; ii < 2; ++ii) {
620fea14342SMatthew G. Knepley                   PetscBool intersects;
621fea14342SMatthew G. Knepley 
6229a128ed2SMatthew G. Knepley                   segB[0]     = PetscRealPart(point[0]);
6239a128ed2SMatthew G. Knepley                   segB[dim+0] = PetscRealPart(point[0]) + ii*h[0];
624fea14342SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
625367003a6SStefano Zampini                   if (intersects) { ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;}
626cafe43deSMatthew G. Knepley                 }
627cafe43deSMatthew G. Knepley               }
628cafe43deSMatthew G. Knepley             }
629cafe43deSMatthew G. Knepley           }
630fea14342SMatthew G. Knepley         }
631fea14342SMatthew G. Knepley       }
632fea14342SMatthew G. Knepley     }
633fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
634fea14342SMatthew G. Knepley   }
635722d0f5cSMatthew G. Knepley   ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr);
636cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
637cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
638cafe43deSMatthew G. Knepley   *localBox = lbox;
639cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
640cafe43deSMatthew G. Knepley }
641cafe43deSMatthew G. Knepley 
64262a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
643ccd2543fSMatthew G Knepley {
644cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
645af74b616SDave May   PetscBool       hash = mesh->useHashLocation, reuse = PETSC_FALSE;
6463a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
647412e9a14SMatthew G. Knepley   PetscInt        dim, cStart, cEnd, numCells, c, d;
648cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
6493a93e3b7SToby Isaac   PetscSFNode    *cells;
650ccd2543fSMatthew G Knepley   PetscScalar    *a;
6513a93e3b7SToby Isaac   PetscMPIInt     result;
652af74b616SDave May   PetscLogDouble  t0,t1;
6539cb35068SDave May   PetscReal       gmin[3],gmax[3];
6549cb35068SDave May   PetscInt        terminating_query_type[] = { 0, 0, 0 };
655ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
656ccd2543fSMatthew G Knepley 
657ccd2543fSMatthew G Knepley   PetscFunctionBegin;
658cadf77a0SMark Adams   ierr = PetscLogEventBegin(DMPLEX_LocatePoints,0,0,0,0);CHKERRQ(ierr);
659af74b616SDave May   ierr = PetscTime(&t0);CHKERRQ(ierr);
660080342d1SMatthew 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.");
661cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
662cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
663ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRMPI(ierr);
6643a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
665cafe43deSMatthew 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);
666412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
667ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
668ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
669ccd2543fSMatthew G Knepley   numPoints /= bs;
670af74b616SDave May   {
671af74b616SDave May     const PetscSFNode *sf_cells;
672af74b616SDave May 
673af74b616SDave May     ierr = PetscSFGetGraph(cellSF,NULL,NULL,NULL,&sf_cells);CHKERRQ(ierr);
674af74b616SDave May     if (sf_cells) {
675af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Re-using existing StarForest node list\n");CHKERRQ(ierr);
676af74b616SDave May       cells = (PetscSFNode*)sf_cells;
677af74b616SDave May       reuse = PETSC_TRUE;
678af74b616SDave May     } else {
679af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n");CHKERRQ(ierr);
680785e854fSJed Brown       ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
681af74b616SDave May       /* initialize cells if created */
682af74b616SDave May       for (p=0; p<numPoints; p++) {
683af74b616SDave May         cells[p].rank  = 0;
684af74b616SDave May         cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
685af74b616SDave May       }
686af74b616SDave May     }
687af74b616SDave May   }
6889cb35068SDave May   /* define domain bounding box */
6899cb35068SDave May   {
6909cb35068SDave May     Vec coorglobal;
6919cb35068SDave May 
6929cb35068SDave May     ierr = DMGetCoordinates(dm,&coorglobal);CHKERRQ(ierr);
6939cb35068SDave May     ierr = VecStrideMaxAll(coorglobal,NULL,gmax);CHKERRQ(ierr);
6949cb35068SDave May     ierr = VecStrideMinAll(coorglobal,NULL,gmin);CHKERRQ(ierr);
6959cb35068SDave May   }
696953fc75cSMatthew G. Knepley   if (hash) {
697ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
698cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
699cafe43deSMatthew G. Knepley     /* Send points to correct process */
700cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
701cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
702cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
703953fc75cSMatthew G. Knepley   }
7043a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
705ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
706e56f9228SJed Brown     PetscInt           dbin[3] = {-1,-1,-1}, bin, cell = -1, cellOffset;
7079cb35068SDave May     PetscBool          point_outside_domain = PETSC_FALSE;
708ccd2543fSMatthew G Knepley 
7099cb35068SDave May     /* check bounding box of domain */
7109cb35068SDave May     for (d=0; d<dim; d++) {
711a5f152d1SDave May       if (PetscRealPart(point[d]) < gmin[d]) { point_outside_domain = PETSC_TRUE; break; }
712a5f152d1SDave May       if (PetscRealPart(point[d]) > gmax[d]) { point_outside_domain = PETSC_TRUE; break; }
7139cb35068SDave May     }
7149cb35068SDave May     if (point_outside_domain) {
715e9b685f5SMatthew G. Knepley       cells[p].rank = 0;
716e9b685f5SMatthew G. Knepley       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
7179cb35068SDave May       terminating_query_type[0]++;
7189cb35068SDave May       continue;
7199cb35068SDave May     }
720ccd2543fSMatthew G Knepley 
721af74b616SDave May     /* check initial values in cells[].index - abort early if found */
722af74b616SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
723af74b616SDave May       c = cells[p].index;
7243a93e3b7SToby Isaac       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
725af74b616SDave May       ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
726af74b616SDave May       if (cell >= 0) {
727af74b616SDave May         cells[p].rank = 0;
728af74b616SDave May         cells[p].index = cell;
729af74b616SDave May         numFound++;
730af74b616SDave May       }
731af74b616SDave May     }
7329cb35068SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
7339cb35068SDave May       terminating_query_type[1]++;
7349cb35068SDave May       continue;
7359cb35068SDave May     }
736af74b616SDave May 
737953fc75cSMatthew G. Knepley     if (hash) {
738af74b616SDave May       PetscBool found_box;
739af74b616SDave May 
740af74b616SDave May       /* allow for case that point is outside box - abort early */
741af74b616SDave May       ierr = PetscGridHashGetEnclosingBoxQuery(mesh->lbox, 1, point, dbin, &bin,&found_box);CHKERRQ(ierr);
742af74b616SDave May       if (found_box) {
743cafe43deSMatthew G. Knepley         /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
744cafe43deSMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
745cafe43deSMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
746cafe43deSMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
747cafe43deSMatthew G. Knepley           ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
7483a93e3b7SToby Isaac           if (cell >= 0) {
7493a93e3b7SToby Isaac             cells[p].rank = 0;
7503a93e3b7SToby Isaac             cells[p].index = cell;
7513a93e3b7SToby Isaac             numFound++;
7529cb35068SDave May             terminating_query_type[2]++;
7533a93e3b7SToby Isaac             break;
754ccd2543fSMatthew G Knepley           }
7553a93e3b7SToby Isaac         }
756af74b616SDave May       }
757953fc75cSMatthew G. Knepley     } else {
758953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
759953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
7603a93e3b7SToby Isaac         if (cell >= 0) {
7613a93e3b7SToby Isaac           cells[p].rank = 0;
7623a93e3b7SToby Isaac           cells[p].index = cell;
7633a93e3b7SToby Isaac           numFound++;
7649cb35068SDave May           terminating_query_type[2]++;
7653a93e3b7SToby Isaac           break;
766953fc75cSMatthew G. Knepley         }
767953fc75cSMatthew G. Knepley       }
7683a93e3b7SToby Isaac     }
769ccd2543fSMatthew G Knepley   }
770953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
77162a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
77262a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
77362a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
77462a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
775e56f9228SJed Brown       PetscInt           dbin[3] = {-1,-1,-1}, bin, cellOffset, d;
77662a38674SMatthew G. Knepley 
777e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
77862a38674SMatthew G. Knepley         ++numFound;
77962a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
78062a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
78162a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
78262a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
78362a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
784b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
78562a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
78662a38674SMatthew G. Knepley           if (dist < distMax) {
78762a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
78862a38674SMatthew G. Knepley             cells[p].rank  = 0;
78962a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
79062a38674SMatthew G. Knepley             distMax = dist;
79162a38674SMatthew G. Knepley             break;
79262a38674SMatthew G. Knepley           }
79362a38674SMatthew G. Knepley         }
79462a38674SMatthew G. Knepley       }
79562a38674SMatthew G. Knepley     }
79662a38674SMatthew G. Knepley   }
79762a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
798cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
7992d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
8003a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
8013a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
8023a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
8033a93e3b7SToby Isaac         if (numFound < p) {
8043a93e3b7SToby Isaac           cells[numFound] = cells[p];
8053a93e3b7SToby Isaac         }
8063a93e3b7SToby Isaac         found[numFound++] = p;
8073a93e3b7SToby Isaac       }
8083a93e3b7SToby Isaac     }
8093a93e3b7SToby Isaac   }
81062a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
811af74b616SDave May   if (!reuse) {
8123a93e3b7SToby Isaac     ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
813af74b616SDave May   }
814af74b616SDave May   ierr = PetscTime(&t1);CHKERRQ(ierr);
8159cb35068SDave May   if (hash) {
8162d4ee042Sprj-     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);
8179cb35068SDave May   } else {
8182d4ee042Sprj-     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);
8199cb35068SDave May   }
820af74b616SDave 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);
821cadf77a0SMark Adams   ierr = PetscLogEventEnd(DMPLEX_LocatePoints,0,0,0,0);CHKERRQ(ierr);
822ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
823ccd2543fSMatthew G Knepley }
824ccd2543fSMatthew G Knepley 
825741bfc07SMatthew G. Knepley /*@C
826741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
827741bfc07SMatthew G. Knepley 
828741bfc07SMatthew G. Knepley   Not collective
829741bfc07SMatthew G. Knepley 
8306b867d5aSJose E. Roman   Input/Output Parameter:
8316b867d5aSJose E. Roman . coords - The coordinates of a segment, on output the new y-coordinate, and 0 for x
832741bfc07SMatthew G. Knepley 
8336b867d5aSJose E. Roman   Output Parameter:
8346b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
835741bfc07SMatthew G. Knepley 
836741bfc07SMatthew G. Knepley   Level: developer
837741bfc07SMatthew G. Knepley 
838741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
839741bfc07SMatthew G. Knepley @*/
840741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
84117fe8556SMatthew G. Knepley {
84217fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
84317fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
8448b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
84517fe8556SMatthew G. Knepley 
84617fe8556SMatthew G. Knepley   PetscFunctionBegin;
8471c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
8481c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
84917fe8556SMatthew G. Knepley   coords[0] = 0.0;
8507f07f362SMatthew G. Knepley   coords[1] = r;
85117fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
85217fe8556SMatthew G. Knepley }
85317fe8556SMatthew G. Knepley 
854741bfc07SMatthew G. Knepley /*@C
855741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
85628dbe442SToby Isaac 
857741bfc07SMatthew G. Knepley   Not collective
85828dbe442SToby Isaac 
8596b867d5aSJose E. Roman   Input/Output Parameter:
8606b867d5aSJose E. Roman . coords - The coordinates of a segment; on output, the new y-coordinate, and 0 for x and z
861741bfc07SMatthew G. Knepley 
8626b867d5aSJose E. Roman   Output Parameter:
8636b867d5aSJose E. Roman . R - The rotation which accomplishes the projection
864741bfc07SMatthew G. Knepley 
865741bfc07SMatthew 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
866741bfc07SMatthew G. Knepley 
867741bfc07SMatthew G. Knepley   Level: developer
868741bfc07SMatthew G. Knepley 
869741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
870741bfc07SMatthew G. Knepley @*/
871741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
87228dbe442SToby Isaac {
87328dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
87428dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
87528dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
87628dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
87728dbe442SToby Isaac   PetscReal      rinv = 1. / r;
87828dbe442SToby Isaac   PetscFunctionBegin;
87928dbe442SToby Isaac 
88028dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
88128dbe442SToby Isaac   if (x > 0.) {
88228dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
88328dbe442SToby Isaac 
88428dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
88528dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
88628dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
88728dbe442SToby Isaac   }
88828dbe442SToby Isaac   else {
88928dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
89028dbe442SToby Isaac 
89128dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
89228dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
89328dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
89428dbe442SToby Isaac   }
89528dbe442SToby Isaac   coords[0] = 0.0;
89628dbe442SToby Isaac   coords[1] = r;
89728dbe442SToby Isaac   PetscFunctionReturn(0);
89828dbe442SToby Isaac }
89928dbe442SToby Isaac 
900741bfc07SMatthew G. Knepley /*@
901c871b86eSJed Brown   DMPlexComputeProjection3Dto2D - Rewrite coordinates of 3 or more coplanar 3D points to a common 2D basis for the
902c871b86eSJed Brown     plane.  The normal is defined by positive orientation of the first 3 points.
903741bfc07SMatthew G. Knepley 
904741bfc07SMatthew G. Knepley   Not collective
905741bfc07SMatthew G. Knepley 
9066b867d5aSJose E. Roman   Input Parameter:
9076b867d5aSJose E. Roman . coordSize - Length of coordinate array (3x number of points); must be at least 9 (3 points)
908741bfc07SMatthew G. Knepley 
9096b867d5aSJose E. Roman   Input/Output Parameter:
9106b867d5aSJose E. Roman . coords - The interlaced coordinates of each coplanar 3D point; on output the first
9116b867d5aSJose E. Roman            2*coordSize/3 entries contain interlaced 2D points, with the rest undefined
9126b867d5aSJose E. Roman 
9136b867d5aSJose E. Roman   Output Parameter:
9146b867d5aSJose E. Roman . R - 3x3 row-major rotation matrix whose columns are the tangent basis [t1, t2, n].  Multiplying by R^T transforms from original frame to tangent frame.
915741bfc07SMatthew G. Knepley 
916741bfc07SMatthew G. Knepley   Level: developer
917741bfc07SMatthew G. Knepley 
918741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
919741bfc07SMatthew G. Knepley @*/
920741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
921ccd2543fSMatthew G Knepley {
922c871b86eSJed Brown   PetscReal x1[3], x2[3], n[3], c[3], norm;
923ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
924c871b86eSJed Brown   PetscInt       d, p;
925ccd2543fSMatthew G Knepley 
926ccd2543fSMatthew G Knepley   PetscFunctionBegin;
927ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
928ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
9291ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
9301ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
931ccd2543fSMatthew G Knepley   }
932c871b86eSJed Brown   // n = x1 \otimes x2
933ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
934ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
935ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
9368b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
937c871b86eSJed Brown   for (d = 0; d < dim; d++) n[d] /= norm;
938c871b86eSJed Brown   norm = PetscSqrtReal(x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]);
939c871b86eSJed Brown   for (d = 0; d < dim; d++) x1[d] /= norm;
940c871b86eSJed Brown   // x2 = n \otimes x1
941c871b86eSJed Brown   x2[0] = n[1] * x1[2] - n[2] * x1[1];
942c871b86eSJed Brown   x2[1] = n[2] * x1[0] - n[0] * x1[2];
943c871b86eSJed Brown   x2[2] = n[0] * x1[1] - n[1] * x1[0];
944c871b86eSJed Brown   for (d=0; d<dim; d++) {
945c871b86eSJed Brown     R[d * dim + 0] = x1[d];
946c871b86eSJed Brown     R[d * dim + 1] = x2[d];
947c871b86eSJed Brown     R[d * dim + 2] = n[d];
948c871b86eSJed Brown     c[d] = PetscRealPart(coords[0*dim + d]);
94973868372SMatthew G. Knepley   }
950c871b86eSJed Brown   for (p=0; p<coordSize/dim; p++) {
951c871b86eSJed Brown     PetscReal y[3];
952c871b86eSJed Brown     for (d=0; d<dim; d++) y[d] = PetscRealPart(coords[p*dim + d]) - c[d];
953c871b86eSJed Brown     for (d=0; d<2; d++) coords[p*2+d] = R[0*dim + d] * y[0] + R[1*dim + d] * y[1] + R[2*dim + d] * y[2];
9547f07f362SMatthew G. Knepley   }
955ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
956ccd2543fSMatthew G Knepley }
957ccd2543fSMatthew G Knepley 
9586322fe33SJed Brown PETSC_UNUSED
959834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
960834e62ceSMatthew G. Knepley {
961834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
962834e62ceSMatthew G. Knepley 
963834e62ceSMatthew G. Knepley    |  1  1  1 |
964834e62ceSMatthew G. Knepley    | x0 x1 x2 |
965834e62ceSMatthew G. Knepley    | y0 y1 y2 |
966834e62ceSMatthew G. Knepley 
967834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
968834e62ceSMatthew G. Knepley 
969834e62ceSMatthew G. Knepley    | x1 x2 |
970834e62ceSMatthew G. Knepley    | y1 y2 |
971834e62ceSMatthew G. Knepley   */
972834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
973834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
974834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
975834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
97686623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
977923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
978834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
9793bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
980834e62ceSMatthew G. Knepley }
981834e62ceSMatthew G. Knepley 
982834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
983834e62ceSMatthew G. Knepley {
984923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
985834e62ceSMatthew G. Knepley   *vol *= 0.5;
986834e62ceSMatthew G. Knepley }
987834e62ceSMatthew G. Knepley 
9886322fe33SJed Brown PETSC_UNUSED
989834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
990834e62ceSMatthew G. Knepley {
991834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
992834e62ceSMatthew G. Knepley 
993834e62ceSMatthew G. Knepley    |  1  1  1  1 |
994834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
995834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
996834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
997834e62ceSMatthew G. Knepley 
998834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
999834e62ceSMatthew G. Knepley 
1000834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1001834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1002834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1003834e62ceSMatthew G. Knepley   */
1004834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
1005834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
1006834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
10070a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1008834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
1009834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
1010834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
1011834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
1012923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
10130a3da2c2SToby Isaac   *vol = -onesixth*detM;
10143bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1015834e62ceSMatthew G. Knepley }
1016834e62ceSMatthew G. Knepley 
10170ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
10180ec8681fSMatthew G. Knepley {
10190a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1020923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
10210a3da2c2SToby Isaac   *vol *= -onesixth;
10220ec8681fSMatthew G. Knepley }
10230ec8681fSMatthew G. Knepley 
1024cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1025cb92db44SToby Isaac {
1026cb92db44SToby Isaac   PetscSection   coordSection;
1027cb92db44SToby Isaac   Vec            coordinates;
1028cb92db44SToby Isaac   const PetscScalar *coords;
1029cb92db44SToby Isaac   PetscInt       dim, d, off;
1030cb92db44SToby Isaac   PetscErrorCode ierr;
1031cb92db44SToby Isaac 
1032cb92db44SToby Isaac   PetscFunctionBegin;
1033cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1034cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1035cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
1036cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
1037cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
1038cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
1039cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
1040cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
1041cb92db44SToby Isaac   *detJ = 1.;
1042cb92db44SToby Isaac   if (J) {
1043cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1044cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1045cb92db44SToby Isaac     if (invJ) {
1046cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1047cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1048cb92db44SToby Isaac     }
1049cb92db44SToby Isaac   }
1050cb92db44SToby Isaac   PetscFunctionReturn(0);
1051cb92db44SToby Isaac }
1052cb92db44SToby Isaac 
105317fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
105417fe8556SMatthew G. Knepley {
105517fe8556SMatthew G. Knepley   PetscSection   coordSection;
105617fe8556SMatthew G. Knepley   Vec            coordinates;
1057a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10588bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
105917fe8556SMatthew G. Knepley   PetscErrorCode ierr;
106017fe8556SMatthew G. Knepley 
106117fe8556SMatthew G. Knepley   PetscFunctionBegin;
106217fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
106369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10648bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10658bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
106617fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
10678bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1068adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
10697f07f362SMatthew G. Knepley   *detJ = 0.0;
107028dbe442SToby Isaac   if (numCoords == 6) {
107128dbe442SToby Isaac     const PetscInt dim = 3;
107228dbe442SToby Isaac     PetscReal      R[9], J0;
107328dbe442SToby Isaac 
107428dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1075741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
107628dbe442SToby Isaac     if (J)    {
107728dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
107828dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
107928dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
108028dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
108128dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
108228dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1083adac9986SMatthew G. Knepley     }
108428dbe442SToby Isaac   } else if (numCoords == 4) {
10857f07f362SMatthew G. Knepley     const PetscInt dim = 2;
10867f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
10877f07f362SMatthew G. Knepley 
10887f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1089741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
109017fe8556SMatthew G. Knepley     if (J)    {
10917f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
10927f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
10937f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
1094923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1095923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1096adac9986SMatthew G. Knepley     }
10977f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
10987f07f362SMatthew G. Knepley     const PetscInt dim = 1;
10997f07f362SMatthew G. Knepley 
11007f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
11017f07f362SMatthew G. Knepley     if (J)    {
11027f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
110317fe8556SMatthew G. Knepley       *detJ = J[0];
11043bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
11053bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
1106adac9986SMatthew G. Knepley     }
1107796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
110817fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
110917fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
111017fe8556SMatthew G. Knepley }
111117fe8556SMatthew G. Knepley 
1112ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1113ccd2543fSMatthew G Knepley {
1114ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1115ccd2543fSMatthew G Knepley   Vec            coordinates;
1116a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
111703d7ed2eSStefano Zampini   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1118ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1119ccd2543fSMatthew G Knepley 
1120ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1121ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
112269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
112303d7ed2eSStefano Zampini   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
112403d7ed2eSStefano Zampini   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
1125ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
112603d7ed2eSStefano Zampini   numCoords = numSelfCoords ? numSelfCoords : numCoords;
11277f07f362SMatthew G. Knepley   *detJ = 0.0;
1128ccd2543fSMatthew G Knepley   if (numCoords == 9) {
11297f07f362SMatthew G. Knepley     const PetscInt dim = 3;
11307f07f362SMatthew 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};
11317f07f362SMatthew G. Knepley 
11327f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1133741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
11347f07f362SMatthew G. Knepley     if (J)    {
1135b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1136b7ad821dSMatthew G. Knepley 
1137b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1138b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1139b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1140ccd2543fSMatthew G Knepley         }
11417f07f362SMatthew G. Knepley       }
11423bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1143923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
11447f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
11457f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
11467f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
11477f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
11487f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
11497f07f362SMatthew G. Knepley           }
11507f07f362SMatthew G. Knepley         }
11517f07f362SMatthew G. Knepley       }
11523bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
11537f07f362SMatthew G. Knepley     }
1154923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
11557f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
11567f07f362SMatthew G. Knepley     const PetscInt dim = 2;
11577f07f362SMatthew G. Knepley 
11587f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1159ccd2543fSMatthew G Knepley     if (J)    {
1160ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1161ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1162ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1163ccd2543fSMatthew G Knepley         }
1164ccd2543fSMatthew G Knepley       }
11653bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1166923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1167ccd2543fSMatthew G Knepley     }
1168923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1169796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1170ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1171ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1172ccd2543fSMatthew G Knepley }
1173ccd2543fSMatthew G Knepley 
1174412e9a14SMatthew 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)
1175ccd2543fSMatthew G Knepley {
1176ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1177ccd2543fSMatthew G Knepley   Vec            coordinates;
1178a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
11790d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1180ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1181ccd2543fSMatthew G Knepley 
1182ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1183ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
118469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
11850d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
11860d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
118799dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
118871f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1189dfccc68fSToby Isaac   if (!Nq) {
1190412e9a14SMatthew G. Knepley     PetscInt vorder[4] = {0, 1, 2, 3};
1191412e9a14SMatthew G. Knepley 
1192412e9a14SMatthew G. Knepley     if (isTensor) {vorder[2] = 3; vorder[3] = 2;}
11937f07f362SMatthew G. Knepley     *detJ = 0.0;
119499dec3a6SMatthew G. Knepley     if (numCoords == 12) {
119599dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
119699dec3a6SMatthew 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};
119799dec3a6SMatthew G. Knepley 
1198dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1199741bfc07SMatthew G. Knepley       ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
120099dec3a6SMatthew G. Knepley       if (J)    {
120199dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
120299dec3a6SMatthew G. Knepley 
120399dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
1204412e9a14SMatthew G. Knepley           J0[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*pdim+d]) - PetscRealPart(coords[vorder[0]*pdim+d]));
1205412e9a14SMatthew G. Knepley           J0[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[2]*pdim+d]) - PetscRealPart(coords[vorder[1]*pdim+d]));
120699dec3a6SMatthew G. Knepley         }
12073bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1208923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
120999dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
121099dec3a6SMatthew G. Knepley           for (f = 0; f < dim; f++) {
121199dec3a6SMatthew G. Knepley             J[d*dim+f] = 0.0;
121299dec3a6SMatthew G. Knepley             for (g = 0; g < dim; g++) {
121399dec3a6SMatthew G. Knepley               J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
121499dec3a6SMatthew G. Knepley             }
121599dec3a6SMatthew G. Knepley           }
121699dec3a6SMatthew G. Knepley         }
12173bc0b13bSBarry Smith         ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
121899dec3a6SMatthew G. Knepley       }
1219923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
122071f58de1SToby Isaac     } else if (numCoords == 8) {
122199dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
122299dec3a6SMatthew G. Knepley 
1223dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1224ccd2543fSMatthew G Knepley       if (J)    {
1225ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
1226412e9a14SMatthew G. Knepley           J[d*dim+0] = 0.5*(PetscRealPart(coords[vorder[1]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d]));
1227412e9a14SMatthew G. Knepley           J[d*dim+1] = 0.5*(PetscRealPart(coords[vorder[3]*dim+d]) - PetscRealPart(coords[vorder[0]*dim+d]));
1228ccd2543fSMatthew G Knepley         }
12293bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1230923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1231ccd2543fSMatthew G Knepley       }
1232923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1233796f034aSJed Brown     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1234dfccc68fSToby Isaac   } else {
1235dfccc68fSToby Isaac     const PetscInt Nv = 4;
1236dfccc68fSToby Isaac     const PetscInt dimR = 2;
1237412e9a14SMatthew G. Knepley     PetscInt  zToPlex[4] = {0, 1, 3, 2};
1238dfccc68fSToby Isaac     PetscReal zOrder[12];
1239dfccc68fSToby Isaac     PetscReal zCoeff[12];
1240dfccc68fSToby Isaac     PetscInt  i, j, k, l, dim;
1241dfccc68fSToby Isaac 
1242412e9a14SMatthew G. Knepley     if (isTensor) {zToPlex[2] = 2; zToPlex[3] = 3;}
1243dfccc68fSToby Isaac     if (numCoords == 12) {
1244dfccc68fSToby Isaac       dim = 3;
1245dfccc68fSToby Isaac     } else if (numCoords == 8) {
1246dfccc68fSToby Isaac       dim = 2;
1247dfccc68fSToby Isaac     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1248dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1249dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1250dfccc68fSToby Isaac 
1251dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1252dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1253dfccc68fSToby Isaac       }
1254dfccc68fSToby Isaac     }
1255dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1256dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1257dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1258dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1259dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1260dfccc68fSToby Isaac     }
1261dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1262dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1263dfccc68fSToby Isaac 
1264dfccc68fSToby Isaac       if (v) {
1265dfccc68fSToby Isaac         PetscReal extPoint[4];
1266dfccc68fSToby Isaac 
1267dfccc68fSToby Isaac         extPoint[0] = 1.;
1268dfccc68fSToby Isaac         extPoint[1] = xi;
1269dfccc68fSToby Isaac         extPoint[2] = eta;
1270dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1271dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1272dfccc68fSToby Isaac           PetscReal val = 0.;
1273dfccc68fSToby Isaac 
1274dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1275dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1276dfccc68fSToby Isaac           }
1277dfccc68fSToby Isaac           v[i * dim + j] = val;
1278dfccc68fSToby Isaac         }
1279dfccc68fSToby Isaac       }
1280dfccc68fSToby Isaac       if (J) {
1281dfccc68fSToby Isaac         PetscReal extJ[8];
1282dfccc68fSToby Isaac 
1283dfccc68fSToby Isaac         extJ[0] = 0.;
1284dfccc68fSToby Isaac         extJ[1] = 0.;
1285dfccc68fSToby Isaac         extJ[2] = 1.;
1286dfccc68fSToby Isaac         extJ[3] = 0.;
1287dfccc68fSToby Isaac         extJ[4] = 0.;
1288dfccc68fSToby Isaac         extJ[5] = 1.;
1289dfccc68fSToby Isaac         extJ[6] = eta;
1290dfccc68fSToby Isaac         extJ[7] = xi;
1291dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1292dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1293dfccc68fSToby Isaac             PetscReal val = 0.;
1294dfccc68fSToby Isaac 
1295dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1296dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1297dfccc68fSToby Isaac             }
1298dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1299dfccc68fSToby Isaac           }
1300dfccc68fSToby Isaac         }
1301dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1302dfccc68fSToby Isaac           PetscReal x, y, z;
1303dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1304dfccc68fSToby Isaac           PetscReal norm;
1305dfccc68fSToby Isaac 
1306dfccc68fSToby Isaac           x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1307dfccc68fSToby Isaac           y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1308dfccc68fSToby Isaac           z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1309dfccc68fSToby Isaac           norm = PetscSqrtReal(x * x + y * y + z * z);
1310dfccc68fSToby Isaac           iJ[2] = x / norm;
1311dfccc68fSToby Isaac           iJ[5] = y / norm;
1312dfccc68fSToby Isaac           iJ[8] = z / norm;
1313dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1314dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1315dfccc68fSToby Isaac         } else {
1316dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1317dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1318dfccc68fSToby Isaac         }
1319dfccc68fSToby Isaac       }
1320dfccc68fSToby Isaac     }
1321dfccc68fSToby Isaac   }
132299dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1323ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1324ccd2543fSMatthew G Knepley }
1325ccd2543fSMatthew G Knepley 
1326ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1327ccd2543fSMatthew G Knepley {
1328ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1329ccd2543fSMatthew G Knepley   Vec            coordinates;
1330a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1331ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
133299dec3a6SMatthew G. Knepley   PetscInt       d;
1333ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1334ccd2543fSMatthew G Knepley 
1335ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1336ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
133769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1338ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
13397f07f362SMatthew G. Knepley   *detJ = 0.0;
13407f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1341ccd2543fSMatthew G Knepley   if (J)    {
1342ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1343f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1344f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1345f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1346f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1347ccd2543fSMatthew G Knepley     }
13483bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1349923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1350ccd2543fSMatthew G Knepley   }
1351923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1352ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1353ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1354ccd2543fSMatthew G Knepley }
1355ccd2543fSMatthew G Knepley 
1356dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1357ccd2543fSMatthew G Knepley {
1358ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1359ccd2543fSMatthew G Knepley   Vec            coordinates;
1360a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1361ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1362ccd2543fSMatthew G Knepley   PetscInt       d;
1363ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1364ccd2543fSMatthew G Knepley 
1365ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1366ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
136769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1368ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1369dfccc68fSToby Isaac   if (!Nq) {
13707f07f362SMatthew G. Knepley     *detJ = 0.0;
1371dfccc68fSToby Isaac     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1372ccd2543fSMatthew G Knepley     if (J)    {
1373ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1374f0df753eSMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1375f0df753eSMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1376f0df753eSMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1377ccd2543fSMatthew G Knepley       }
13783bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1379923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1380ccd2543fSMatthew G Knepley     }
1381923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1382dfccc68fSToby Isaac   } else {
1383dfccc68fSToby Isaac     const PetscInt Nv = 8;
1384dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1385dfccc68fSToby Isaac     const PetscInt dim = 3;
1386dfccc68fSToby Isaac     const PetscInt dimR = 3;
1387dfccc68fSToby Isaac     PetscReal zOrder[24];
1388dfccc68fSToby Isaac     PetscReal zCoeff[24];
1389dfccc68fSToby Isaac     PetscInt  i, j, k, l;
1390dfccc68fSToby Isaac 
1391dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1392dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1393dfccc68fSToby Isaac 
1394dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1395dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1396dfccc68fSToby Isaac       }
1397dfccc68fSToby Isaac     }
1398dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1399dfccc68fSToby 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]);
1400dfccc68fSToby 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]);
1401dfccc68fSToby 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]);
1402dfccc68fSToby 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]);
1403dfccc68fSToby 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]);
1404dfccc68fSToby 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]);
1405dfccc68fSToby 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]);
1406dfccc68fSToby 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]);
1407dfccc68fSToby Isaac     }
1408dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1409dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1410dfccc68fSToby Isaac 
1411dfccc68fSToby Isaac       if (v) {
141291d2b7ceSToby Isaac         PetscReal extPoint[8];
1413dfccc68fSToby Isaac 
1414dfccc68fSToby Isaac         extPoint[0] = 1.;
1415dfccc68fSToby Isaac         extPoint[1] = xi;
1416dfccc68fSToby Isaac         extPoint[2] = eta;
1417dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1418dfccc68fSToby Isaac         extPoint[4] = theta;
1419dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1420dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1421dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1422dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1423dfccc68fSToby Isaac           PetscReal val = 0.;
1424dfccc68fSToby Isaac 
1425dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1426dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1427dfccc68fSToby Isaac           }
1428dfccc68fSToby Isaac           v[i * dim + j] = val;
1429dfccc68fSToby Isaac         }
1430dfccc68fSToby Isaac       }
1431dfccc68fSToby Isaac       if (J) {
1432dfccc68fSToby Isaac         PetscReal extJ[24];
1433dfccc68fSToby Isaac 
1434dfccc68fSToby Isaac         extJ[0]  = 0.         ; extJ[1]  = 0.        ; extJ[2]  = 0.      ;
1435dfccc68fSToby Isaac         extJ[3]  = 1.         ; extJ[4]  = 0.        ; extJ[5]  = 0.      ;
1436dfccc68fSToby Isaac         extJ[6]  = 0.         ; extJ[7]  = 1.        ; extJ[8]  = 0.      ;
1437dfccc68fSToby Isaac         extJ[9]  = eta        ; extJ[10] = xi        ; extJ[11] = 0.      ;
1438dfccc68fSToby Isaac         extJ[12] = 0.         ; extJ[13] = 0.        ; extJ[14] = 1.      ;
1439dfccc68fSToby Isaac         extJ[15] = theta      ; extJ[16] = 0.        ; extJ[17] = xi      ;
1440dfccc68fSToby Isaac         extJ[18] = 0.         ; extJ[19] = theta     ; extJ[20] = eta     ;
1441dfccc68fSToby Isaac         extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi;
1442dfccc68fSToby Isaac 
1443dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1444dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1445dfccc68fSToby Isaac             PetscReal val = 0.;
1446dfccc68fSToby Isaac 
1447dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1448dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1449dfccc68fSToby Isaac             }
1450dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1451dfccc68fSToby Isaac           }
1452dfccc68fSToby Isaac         }
1453dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1454dfccc68fSToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1455dfccc68fSToby Isaac       }
1456dfccc68fSToby Isaac     }
1457dfccc68fSToby Isaac   }
1458ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1459ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1460ccd2543fSMatthew G Knepley }
1461ccd2543fSMatthew G Knepley 
1462dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1463dfccc68fSToby Isaac {
1464ba2698f1SMatthew G. Knepley   DMPolytopeType  ct;
1465dfccc68fSToby Isaac   PetscInt        depth, dim, coordDim, coneSize, i;
1466dfccc68fSToby Isaac   PetscInt        Nq = 0;
1467dfccc68fSToby Isaac   const PetscReal *points = NULL;
1468dfccc68fSToby Isaac   DMLabel         depthLabel;
1469c330f8ffSToby Isaac   PetscReal       xi0[3] = {-1.,-1.,-1.}, v0[3], J0[9], detJ0;
1470dfccc68fSToby Isaac   PetscBool       isAffine = PETSC_TRUE;
1471dfccc68fSToby Isaac   PetscErrorCode  ierr;
1472dfccc68fSToby Isaac 
1473dfccc68fSToby Isaac   PetscFunctionBegin;
1474dfccc68fSToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1475dfccc68fSToby Isaac   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1476dfccc68fSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1477dfccc68fSToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1478dfccc68fSToby Isaac   if (depth == 1 && dim == 1) {
1479dfccc68fSToby Isaac     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1480dfccc68fSToby Isaac   }
1481dfccc68fSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1482dfccc68fSToby Isaac   if (coordDim > 3) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim);
14839c3cf19fSMatthew G. Knepley   if (quad) {ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL);CHKERRQ(ierr);}
1484ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1485ba2698f1SMatthew G. Knepley   switch (ct) {
1486ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_POINT:
1487dfccc68fSToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1488dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1489dfccc68fSToby Isaac     break;
1490ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
1491412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
1492ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1493ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1494dfccc68fSToby Isaac     break;
1495ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
1496ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1497ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1498dfccc68fSToby Isaac     break;
1499ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
1500412e9a14SMatthew G. Knepley     ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_FALSE, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1501412e9a14SMatthew G. Knepley     isAffine = PETSC_FALSE;
1502412e9a14SMatthew G. Knepley     break;
1503412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1504412e9a14SMatthew G. Knepley     ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, PETSC_TRUE, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1505dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1506dfccc68fSToby Isaac     break;
1507ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
1508ba2698f1SMatthew G. Knepley     if (Nq) {ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);}
1509ba2698f1SMatthew G. Knepley     else    {ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v,  J,  invJ,  detJ);CHKERRQ(ierr);}
1510dfccc68fSToby Isaac     break;
1511ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
1512dfccc68fSToby Isaac     ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1513dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1514dfccc68fSToby Isaac     break;
1515ba2698f1SMatthew G. Knepley     default: SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No element geometry for cell %D with type %s", cell, DMPolytopeTypes[ct]);
1516dfccc68fSToby Isaac   }
15177318780aSToby Isaac   if (isAffine && Nq) {
1518dfccc68fSToby Isaac     if (v) {
1519dfccc68fSToby Isaac       for (i = 0; i < Nq; i++) {
1520c330f8ffSToby Isaac         CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]);
1521dfccc68fSToby Isaac       }
1522dfccc68fSToby Isaac     }
15237318780aSToby Isaac     if (detJ) {
15247318780aSToby Isaac       for (i = 0; i < Nq; i++) {
15257318780aSToby Isaac         detJ[i] = detJ0;
1526dfccc68fSToby Isaac       }
15277318780aSToby Isaac     }
15287318780aSToby Isaac     if (J) {
15297318780aSToby Isaac       PetscInt k;
15307318780aSToby Isaac 
15317318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1532dfccc68fSToby Isaac         PetscInt j;
1533dfccc68fSToby Isaac 
15347318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
15357318780aSToby Isaac           J[k] = J0[j];
15367318780aSToby Isaac         }
15377318780aSToby Isaac       }
15387318780aSToby Isaac     }
15397318780aSToby Isaac     if (invJ) {
15407318780aSToby Isaac       PetscInt k;
15417318780aSToby Isaac       switch (coordDim) {
15427318780aSToby Isaac       case 0:
15437318780aSToby Isaac         break;
15447318780aSToby Isaac       case 1:
15457318780aSToby Isaac         invJ[0] = 1./J0[0];
15467318780aSToby Isaac         break;
15477318780aSToby Isaac       case 2:
15487318780aSToby Isaac         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
15497318780aSToby Isaac         break;
15507318780aSToby Isaac       case 3:
15517318780aSToby Isaac         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
15527318780aSToby Isaac         break;
15537318780aSToby Isaac       }
15547318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
15557318780aSToby Isaac         PetscInt j;
15567318780aSToby Isaac 
15577318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
15587318780aSToby Isaac           invJ[k] = invJ[j];
15597318780aSToby Isaac         }
1560dfccc68fSToby Isaac       }
1561dfccc68fSToby Isaac     }
1562dfccc68fSToby Isaac   }
1563dfccc68fSToby Isaac   PetscFunctionReturn(0);
1564dfccc68fSToby Isaac }
1565dfccc68fSToby Isaac 
1566ccd2543fSMatthew G Knepley /*@C
15678e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1568ccd2543fSMatthew G Knepley 
1569d083f849SBarry Smith   Collective on dm
1570ccd2543fSMatthew G Knepley 
1571*4165533cSJose E. Roman   Input Parameters:
1572ccd2543fSMatthew G Knepley + dm   - the DM
1573ccd2543fSMatthew G Knepley - cell - the cell
1574ccd2543fSMatthew G Knepley 
1575*4165533cSJose E. Roman   Output Parameters:
1576ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1577ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1578ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1579ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1580ccd2543fSMatthew G Knepley 
1581ccd2543fSMatthew G Knepley   Level: advanced
1582ccd2543fSMatthew G Knepley 
1583ccd2543fSMatthew G Knepley   Fortran Notes:
1584ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1585ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1586ccd2543fSMatthew G Knepley 
1587e8964c0aSStefano Zampini .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinates()
1588ccd2543fSMatthew G Knepley @*/
15898e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1590ccd2543fSMatthew G Knepley {
1591ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1592ccd2543fSMatthew G Knepley 
1593ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1594dfccc68fSToby Isaac   ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr);
15958e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
15968e0841e0SMatthew G. Knepley }
15978e0841e0SMatthew G. Knepley 
1598dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
15998e0841e0SMatthew G. Knepley {
1600dfccc68fSToby Isaac   PetscQuadrature   feQuad;
16018e0841e0SMatthew G. Knepley   PetscSection      coordSection;
16028e0841e0SMatthew G. Knepley   Vec               coordinates;
16038e0841e0SMatthew G. Knepley   PetscScalar      *coords = NULL;
16048e0841e0SMatthew G. Knepley   const PetscReal  *quadPoints;
1605ef0bb6c7SMatthew G. Knepley   PetscTabulation T;
1606f960e424SToby Isaac   PetscInt          dim, cdim, pdim, qdim, Nq, numCoords, q;
16078e0841e0SMatthew G. Knepley   PetscErrorCode    ierr;
16088e0841e0SMatthew G. Knepley 
16098e0841e0SMatthew G. Knepley   PetscFunctionBegin;
16108e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
16118e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
16128e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
16138e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16148e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1615dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
1616dfccc68fSToby Isaac     PetscDualSpace dsp;
1617dfccc68fSToby Isaac 
1618dfccc68fSToby Isaac     ierr = PetscFEGetDualSpace(fe, &dsp);CHKERRQ(ierr);
1619dfccc68fSToby Isaac     ierr = PetscDualSpaceGetFunctional(dsp, 0, &quad);CHKERRQ(ierr);
16209c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1621dfccc68fSToby Isaac     Nq = 1;
1622dfccc68fSToby Isaac   } else {
16239c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1624dfccc68fSToby Isaac   }
162591d2b7ceSToby Isaac   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
1626dfccc68fSToby Isaac   ierr = PetscFEGetQuadrature(fe, &feQuad);CHKERRQ(ierr);
1627dfccc68fSToby Isaac   if (feQuad == quad) {
1628f9244615SMatthew G. Knepley     ierr = PetscFEGetCellTabulation(fe, J ? 1 : 0, &T);CHKERRQ(ierr);
16298e0841e0SMatthew 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);
1630dfccc68fSToby Isaac   } else {
1631ef0bb6c7SMatthew G. Knepley     ierr = PetscFECreateTabulation(fe, 1, Nq, quadPoints, J ? 1 : 0, &T);CHKERRQ(ierr);
1632dfccc68fSToby Isaac   }
1633dfccc68fSToby Isaac   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
1634ef0bb6c7SMatthew G. Knepley   {
1635ef0bb6c7SMatthew G. Knepley     const PetscReal *basis    = T->T[0];
1636ef0bb6c7SMatthew G. Knepley     const PetscReal *basisDer = T->T[1];
1637ef0bb6c7SMatthew G. Knepley     PetscReal        detJt;
1638ef0bb6c7SMatthew G. Knepley 
1639a2a9e04cSMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
1640a2a9e04cSMatthew G. Knepley     if (Nq != T->Np)     SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Np %D != %D", Nq, T->Np);
1641a2a9e04cSMatthew G. Knepley     if (pdim != T->Nb)   SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nb %D != %D", pdim, T->Nb);
1642a2a9e04cSMatthew G. Knepley     if (dim != T->Nc)    SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nc %D != %D", dim, T->Nc);
1643a2a9e04cSMatthew G. Knepley     if (cdim != T->cdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "cdim %D != %D", cdim, T->cdim);
1644a2a9e04cSMatthew G. Knepley #endif
1645dfccc68fSToby Isaac     if (v) {
1646580bdb30SBarry Smith       ierr = PetscArrayzero(v, Nq*cdim);CHKERRQ(ierr);
1647f960e424SToby Isaac       for (q = 0; q < Nq; ++q) {
1648f960e424SToby Isaac         PetscInt i, k;
1649f960e424SToby Isaac 
1650301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
1651301b184aSMatthew G. Knepley           const PetscInt vertex = k/cdim;
1652301b184aSMatthew G. Knepley           for (i = 0; i < cdim; ++i) {
1653301b184aSMatthew G. Knepley             v[q*cdim + i] += basis[(q*pdim + k)*cdim + i] * PetscRealPart(coords[vertex*cdim + i]);
1654301b184aSMatthew G. Knepley           }
1655301b184aSMatthew G. Knepley         }
1656f960e424SToby Isaac         ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1657f960e424SToby Isaac       }
1658f960e424SToby Isaac     }
16598e0841e0SMatthew G. Knepley     if (J) {
1660580bdb30SBarry Smith       ierr = PetscArrayzero(J, Nq*cdim*cdim);CHKERRQ(ierr);
16618e0841e0SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
16628e0841e0SMatthew G. Knepley         PetscInt i, j, k, c, r;
16638e0841e0SMatthew G. Knepley 
16648e0841e0SMatthew G. Knepley         /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
1665301b184aSMatthew G. Knepley         for (k = 0; k < pdim; ++k) {
1666301b184aSMatthew G. Knepley           const PetscInt vertex = k/cdim;
1667301b184aSMatthew G. Knepley           for (j = 0; j < dim; ++j) {
1668301b184aSMatthew G. Knepley             for (i = 0; i < cdim; ++i) {
1669301b184aSMatthew G. Knepley               J[(q*cdim + i)*cdim + j] += basisDer[((q*pdim + k)*cdim + i)*dim + j] * PetscRealPart(coords[vertex*cdim + i]);
1670301b184aSMatthew G. Knepley             }
1671301b184aSMatthew G. Knepley           }
1672301b184aSMatthew G. Knepley         }
16733bc0b13bSBarry Smith         ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
16748e0841e0SMatthew G. Knepley         if (cdim > dim) {
16758e0841e0SMatthew G. Knepley           for (c = dim; c < cdim; ++c)
16768e0841e0SMatthew G. Knepley             for (r = 0; r < cdim; ++r)
16778e0841e0SMatthew G. Knepley               J[r*cdim+c] = r == c ? 1.0 : 0.0;
16788e0841e0SMatthew G. Knepley         }
1679f960e424SToby Isaac         if (!detJ && !invJ) continue;
1680a63b72c6SToby Isaac         detJt = 0.;
16818e0841e0SMatthew G. Knepley         switch (cdim) {
16828e0841e0SMatthew G. Knepley         case 3:
1683037dc194SToby Isaac           DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]);
1684037dc194SToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
168517fe8556SMatthew G. Knepley           break;
168649dc4407SMatthew G. Knepley         case 2:
16879f328543SToby Isaac           DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]);
1688037dc194SToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
168949dc4407SMatthew G. Knepley           break;
16908e0841e0SMatthew G. Knepley         case 1:
1691037dc194SToby Isaac           detJt = J[q*cdim*dim];
1692037dc194SToby Isaac           if (invJ) invJ[q*cdim*dim] = 1.0/detJt;
169349dc4407SMatthew G. Knepley         }
1694f960e424SToby Isaac         if (detJ) detJ[q] = detJt;
169549dc4407SMatthew G. Knepley       }
1696ef0bb6c7SMatthew G. Knepley     } else if (detJ || invJ) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
169749dc4407SMatthew G. Knepley   }
1698ef0bb6c7SMatthew G. Knepley   if (feQuad != quad) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
16998e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
17008e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
17018e0841e0SMatthew G. Knepley }
17028e0841e0SMatthew G. Knepley 
17038e0841e0SMatthew G. Knepley /*@C
17048e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
17058e0841e0SMatthew G. Knepley 
1706d083f849SBarry Smith   Collective on dm
17078e0841e0SMatthew G. Knepley 
1708*4165533cSJose E. Roman   Input Parameters:
17098e0841e0SMatthew G. Knepley + dm   - the DM
17108e0841e0SMatthew G. Knepley . cell - the cell
1711dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
1712dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
17138e0841e0SMatthew G. Knepley 
1714*4165533cSJose E. Roman   Output Parameters:
1715dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
17168e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
17178e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
17188e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
17198e0841e0SMatthew G. Knepley 
17208e0841e0SMatthew G. Knepley   Level: advanced
17218e0841e0SMatthew G. Knepley 
17228e0841e0SMatthew G. Knepley   Fortran Notes:
17238e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
17248e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
17258e0841e0SMatthew G. Knepley 
1726e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
17278e0841e0SMatthew G. Knepley @*/
1728dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
17298e0841e0SMatthew G. Knepley {
1730bb4a5db5SMatthew G. Knepley   DM             cdm;
1731dfccc68fSToby Isaac   PetscFE        fe = NULL;
17328e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
17338e0841e0SMatthew G. Knepley 
17348e0841e0SMatthew G. Knepley   PetscFunctionBegin;
17352d89661fSMatthew G. Knepley   PetscValidPointer(detJ, 7);
1736bb4a5db5SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1737bb4a5db5SMatthew G. Knepley   if (cdm) {
1738dfccc68fSToby Isaac     PetscClassId id;
1739dfccc68fSToby Isaac     PetscInt     numFields;
1740e5e52638SMatthew G. Knepley     PetscDS      prob;
1741dfccc68fSToby Isaac     PetscObject  disc;
1742dfccc68fSToby Isaac 
1743bb4a5db5SMatthew G. Knepley     ierr = DMGetNumFields(cdm, &numFields);CHKERRQ(ierr);
1744dfccc68fSToby Isaac     if (numFields) {
1745bb4a5db5SMatthew G. Knepley       ierr = DMGetDS(cdm, &prob);CHKERRQ(ierr);
1746dfccc68fSToby Isaac       ierr = PetscDSGetDiscretization(prob,0,&disc);CHKERRQ(ierr);
1747dfccc68fSToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1748dfccc68fSToby Isaac       if (id == PETSCFE_CLASSID) {
1749dfccc68fSToby Isaac         fe = (PetscFE) disc;
1750dfccc68fSToby Isaac       }
1751dfccc68fSToby Isaac     }
1752dfccc68fSToby Isaac   }
1753dfccc68fSToby Isaac   if (!fe) {ierr = DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1754dfccc68fSToby Isaac   else     {ierr = DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1755ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1756ccd2543fSMatthew G Knepley }
1757834e62ceSMatthew G. Knepley 
1758011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1759cc08537eSMatthew G. Knepley {
1760cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1761cc08537eSMatthew G. Knepley   Vec            coordinates;
1762a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
176306e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1764714b99b6SMatthew G. Knepley   PetscInt       coordSize, d;
1765cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1766cc08537eSMatthew G. Knepley 
1767cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1768cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
176969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1770cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
17712e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1772cc08537eSMatthew G. Knepley   if (centroid) {
1773714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) centroid[d] = 0.5*PetscRealPart(coords[d] + tmp[d]);
1774cc08537eSMatthew G. Knepley   }
1775cc08537eSMatthew G. Knepley   if (normal) {
1776a60a936bSMatthew G. Knepley     PetscReal norm;
1777a60a936bSMatthew G. Knepley 
1778714b99b6SMatthew G. Knepley     if (dim != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "We only support 2D edges right now");
177906e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
178006e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1781714b99b6SMatthew G. Knepley     norm       = DMPlex_NormD_Internal(dim, normal);
1782714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) normal[d] /= norm;
1783cc08537eSMatthew G. Knepley   }
1784cc08537eSMatthew G. Knepley   if (vol) {
1785714b99b6SMatthew G. Knepley     *vol = 0.0;
1786714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - tmp[d]));
1787714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
1788cc08537eSMatthew G. Knepley   }
1789cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1790cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1791cc08537eSMatthew G. Knepley }
1792cc08537eSMatthew G. Knepley 
1793cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i) / A */
1794011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1795cc08537eSMatthew G. Knepley {
1796412e9a14SMatthew G. Knepley   DMPolytopeType ct;
1797cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1798cc08537eSMatthew G. Knepley   Vec            coordinates;
1799cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
18000a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
1801793a2a13SMatthew G. Knepley   PetscBool      isHybrid = PETSC_FALSE;
1802793a2a13SMatthew G. Knepley   PetscInt       fv[4] = {0, 1, 2, 3};
1803412e9a14SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1804cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1805cc08537eSMatthew G. Knepley 
1806cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1807793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
1808412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1809412e9a14SMatthew G. Knepley   switch (ct) {
1810412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
1811412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1812412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
1813412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
1814412e9a14SMatthew G. Knepley       isHybrid = PETSC_TRUE;
1815412e9a14SMatthew G. Knepley     default: break;
1816412e9a14SMatthew G. Knepley   }
1817cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
18180a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
181969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1820cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
18210bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1822793a2a13SMatthew G. Knepley   /* Side faces for hybrid cells are are stored as tensor products */
1823793a2a13SMatthew G. Knepley   if (isHybrid && numCorners == 4) {fv[2] = 3; fv[3] = 2;}
1824793a2a13SMatthew G. Knepley 
1825ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1826ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1827ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1828ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1829ceee4971SMatthew G. Knepley   }
1830011ea5d8SMatthew G. Knepley   if (normal) {
1831011ea5d8SMatthew G. Knepley     if (dim > 2) {
1832793a2a13SMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim*fv[1]+0] - coords[0]), x1 = PetscRealPart(coords[dim*fv[2]+0] - coords[0]);
1833793a2a13SMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim*fv[1]+1] - coords[1]), y1 = PetscRealPart(coords[dim*fv[2]+1] - coords[1]);
1834793a2a13SMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim*fv[1]+2] - coords[2]), z1 = PetscRealPart(coords[dim*fv[2]+2] - coords[2]);
18350a1d6728SMatthew G. Knepley       PetscReal       norm;
18360a1d6728SMatthew G. Knepley 
18370a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
18380a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
18390a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
18408b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
18410a1d6728SMatthew G. Knepley       normal[0] /= norm;
18420a1d6728SMatthew G. Knepley       normal[1] /= norm;
18430a1d6728SMatthew G. Knepley       normal[2] /= norm;
1844011ea5d8SMatthew G. Knepley     } else {
1845011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1846011ea5d8SMatthew G. Knepley     }
1847011ea5d8SMatthew G. Knepley   }
1848741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
18490a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
1850793a2a13SMatthew G. Knepley     const PetscInt pi  = p < 4 ? fv[p] : p;
1851793a2a13SMatthew G. Knepley     const PetscInt pin = p < 3 ? fv[(p+1)%numCorners] : (p+1)%numCorners;
18520a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
18530a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
1854793a2a13SMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[pi*tdim+d]);
1855793a2a13SMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[pin*tdim+d]);
18560a1d6728SMatthew G. Knepley     }
18570a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
18580a1d6728SMatthew G. Knepley     vsum += vtmp;
18590a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
18600a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
18610a1d6728SMatthew G. Knepley     }
18620a1d6728SMatthew G. Knepley   }
18630a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
18640a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
18650a1d6728SMatthew G. Knepley   }
18660a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1867ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
18680a1d6728SMatthew G. Knepley   if (centroid) {
18690a1d6728SMatthew G. Knepley     if (dim > 2) {
18700a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
18710a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
18720a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
18730a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
18740a1d6728SMatthew G. Knepley         }
18750a1d6728SMatthew G. Knepley       }
18760a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
18770a1d6728SMatthew G. Knepley   }
1878cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1879cc08537eSMatthew G. Knepley }
1880cc08537eSMatthew G. Knepley 
18810ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i) / V */
1882011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
18830ec8681fSMatthew G. Knepley {
1884412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
18850ec8681fSMatthew G. Knepley   PetscSection    coordSection;
18860ec8681fSMatthew G. Knepley   Vec             coordinates;
18870ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
188886623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1889a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
1890793a2a13SMatthew G. Knepley   PetscBool       isHybrid = PETSC_FALSE;
1891412e9a14SMatthew G. Knepley   PetscInt        numFaces, f, coordSize, p, d;
18920ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
18930ec8681fSMatthew G. Knepley 
18940ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1895f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
1896793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
1897412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1898412e9a14SMatthew G. Knepley   switch (ct) {
1899412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
1900412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1901412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
1902412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
1903412e9a14SMatthew G. Knepley       isHybrid = PETSC_TRUE;
1904412e9a14SMatthew G. Knepley     default: break;
1905412e9a14SMatthew G. Knepley   }
1906793a2a13SMatthew G. Knepley 
19070ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
190869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
19090ec8681fSMatthew G. Knepley 
1910d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
19110ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
19120ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1913a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
19140ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1915793a2a13SMatthew G. Knepley     PetscBool      flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
1916ba2698f1SMatthew G. Knepley     DMPolytopeType ct;
1917793a2a13SMatthew G. Knepley 
1918011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
1919ba2698f1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, faces[f], &ct);CHKERRQ(ierr);
1920ba2698f1SMatthew G. Knepley     switch (ct) {
1921ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
19220ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
19231ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
19241ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
19251ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
19260ec8681fSMatthew G. Knepley       }
19270ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1928793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
19290ec8681fSMatthew G. Knepley       vsum += vtmp;
19304f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
19310ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19321ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
19330ec8681fSMatthew G. Knepley         }
19340ec8681fSMatthew G. Knepley       }
19350ec8681fSMatthew G. Knepley       break;
1936ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
1937412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
1938793a2a13SMatthew G. Knepley     {
1939793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
1940793a2a13SMatthew G. Knepley 
1941793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
1942793a2a13SMatthew G. Knepley       if (isHybrid && f > 1) {fv[2] = 3; fv[3] = 2;}
19430ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
19440ec8681fSMatthew G. Knepley       /* First tet */
19450ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
1946793a2a13SMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[0]*dim+d]);
1947793a2a13SMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[1]*dim+d]);
1948793a2a13SMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]);
19490ec8681fSMatthew G. Knepley       }
19500ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1951793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
19520ec8681fSMatthew G. Knepley       vsum += vtmp;
19530ec8681fSMatthew G. Knepley       if (centroid) {
19540ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19550ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
19560ec8681fSMatthew G. Knepley         }
19570ec8681fSMatthew G. Knepley       }
19580ec8681fSMatthew G. Knepley       /* Second tet */
19590ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
1960793a2a13SMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[1]*dim+d]);
1961793a2a13SMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[2]*dim+d]);
1962793a2a13SMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]);
19630ec8681fSMatthew G. Knepley       }
19640ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1965793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
19660ec8681fSMatthew G. Knepley       vsum += vtmp;
19670ec8681fSMatthew G. Knepley       if (centroid) {
19680ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19690ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
19700ec8681fSMatthew G. Knepley         }
19710ec8681fSMatthew G. Knepley       }
19720ec8681fSMatthew G. Knepley       break;
1973793a2a13SMatthew G. Knepley     }
19740ec8681fSMatthew G. Knepley     default:
1975ba2698f1SMatthew G. Knepley       SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle face %D of type %s", faces[f], DMPolytopeTypes[ct]);
19760ec8681fSMatthew G. Knepley     }
19774f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
19780ec8681fSMatthew G. Knepley   }
19798763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
19800ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1981d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
19820ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
19830ec8681fSMatthew G. Knepley }
19840ec8681fSMatthew G. Knepley 
1985834e62ceSMatthew G. Knepley /*@C
1986834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1987834e62ceSMatthew G. Knepley 
1988d083f849SBarry Smith   Collective on dm
1989834e62ceSMatthew G. Knepley 
1990*4165533cSJose E. Roman   Input Parameters:
1991834e62ceSMatthew G. Knepley + dm   - the DM
1992834e62ceSMatthew G. Knepley - cell - the cell
1993834e62ceSMatthew G. Knepley 
1994*4165533cSJose E. Roman   Output Parameters:
1995834e62ceSMatthew G. Knepley + volume   - the cell volume
1996cc08537eSMatthew G. Knepley . centroid - the cell centroid
1997cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1998834e62ceSMatthew G. Knepley 
1999834e62ceSMatthew G. Knepley   Level: advanced
2000834e62ceSMatthew G. Knepley 
2001834e62ceSMatthew G. Knepley   Fortran Notes:
2002834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2003834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
2004834e62ceSMatthew G. Knepley 
2005e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
2006834e62ceSMatthew G. Knepley @*/
2007cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2008834e62ceSMatthew G. Knepley {
20090ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
2010834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
2011834e62ceSMatthew G. Knepley 
2012834e62ceSMatthew G. Knepley   PetscFunctionBegin;
2013834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2014c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2015834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
2016ba2698f1SMatthew G. Knepley   ierr = DMPlexGetPointDepth(dm, cell, &depth);CHKERRQ(ierr);
2017011ea5d8SMatthew G. Knepley   switch (depth) {
2018cc08537eSMatthew G. Knepley   case 1:
2019011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2020cc08537eSMatthew G. Knepley     break;
2021834e62ceSMatthew G. Knepley   case 2:
2022011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2023834e62ceSMatthew G. Knepley     break;
2024834e62ceSMatthew G. Knepley   case 3:
2025011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2026834e62ceSMatthew G. Knepley     break;
2027834e62ceSMatthew G. Knepley   default:
2028b81cf158SMatthew G. Knepley     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D (depth %D) for element geometry computation", dim, depth);
2029834e62ceSMatthew G. Knepley   }
2030834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
2031834e62ceSMatthew G. Knepley }
2032113c68e6SMatthew G. Knepley 
2033c501906fSMatthew G. Knepley /*@
2034c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
2035c501906fSMatthew G. Knepley 
2036c501906fSMatthew G. Knepley   Collective on dm
2037c501906fSMatthew G. Knepley 
2038c501906fSMatthew G. Knepley   Input Parameter:
2039c501906fSMatthew G. Knepley . dm - The DMPlex
2040c501906fSMatthew G. Knepley 
2041c501906fSMatthew G. Knepley   Output Parameter:
2042c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
2043c501906fSMatthew G. Knepley 
2044c501906fSMatthew G. Knepley   Level: beginner
2045c501906fSMatthew G. Knepley 
2046c501906fSMatthew G. Knepley @*/
2047c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
2048c0d900a5SMatthew G. Knepley {
2049c0d900a5SMatthew G. Knepley   DM             dmCell;
2050c0d900a5SMatthew G. Knepley   Vec            coordinates;
2051c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
2052c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
2053412e9a14SMatthew G. Knepley   PetscInt       cStart, cEnd, c;
2054c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
2055c0d900a5SMatthew G. Knepley 
2056c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
2057c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2058c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2059c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2060c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2061c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2062c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2063412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2064c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
2065c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
2066cf0b7c11SKarl Rupp   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFEGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2067c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
206892fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2069c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2070c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2071c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2072c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2073cf0b7c11SKarl Rupp     PetscFEGeom *cg;
2074c0d900a5SMatthew G. Knepley 
2075c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2076580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2077cf0b7c11SKarl Rupp     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ);CHKERRQ(ierr);
2078087ef6b2SMatthew 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);
2079c0d900a5SMatthew G. Knepley   }
2080c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
2081c0d900a5SMatthew G. Knepley }
2082c0d900a5SMatthew G. Knepley 
2083891a9168SMatthew G. Knepley /*@
2084891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2085891a9168SMatthew G. Knepley 
2086891a9168SMatthew G. Knepley   Input Parameter:
2087891a9168SMatthew G. Knepley . dm - The DM
2088891a9168SMatthew G. Knepley 
2089891a9168SMatthew G. Knepley   Output Parameters:
2090891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2091a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data
2092891a9168SMatthew G. Knepley 
2093891a9168SMatthew G. Knepley   Level: developer
2094891a9168SMatthew G. Knepley 
2095891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
2096891a9168SMatthew G. Knepley @*/
2097113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2098113c68e6SMatthew G. Knepley {
2099113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
2100113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
2101113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
2102113c68e6SMatthew G. Knepley   PetscSection   coordSection;
2103113c68e6SMatthew G. Knepley   Vec            coordinates;
2104113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2105113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
2106113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2107113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
2108113c68e6SMatthew G. Knepley 
2109113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2110113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2111113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2112113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2113113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
2114113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2115113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2116113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2117113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2118113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2119485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2120113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
21219e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2122113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
212392fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2124113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2125113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2126485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2127113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2128113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2129113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2130113c68e6SMatthew G. Knepley 
2131113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2132580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2133113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
2134113c68e6SMatthew G. Knepley   }
2135113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
2136113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
2137113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
2138113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2139113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
21409e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2141113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
214292fd8e1eSJed Brown   ierr = DMSetLocalSection(dmFace, sectionFace);CHKERRQ(ierr);
2143113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
2144113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
2145113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
2146c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2147113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2148113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2149113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2150113c68e6SMatthew G. Knepley     PetscReal        area;
2151412e9a14SMatthew G. Knepley     const PetscInt  *cells;
2152412e9a14SMatthew G. Knepley     PetscInt         ncells, ghost = -1, d, numChildren;
2153113c68e6SMatthew G. Knepley 
21549ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
215550d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
2156412e9a14SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
2157412e9a14SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
2158412e9a14SMatthew G. Knepley     /* It is possible to get a face with no support when using partition overlap */
2159412e9a14SMatthew G. Knepley     if (!ncells || ghost >= 0 || numChildren) continue;
2160113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
2161113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
2162113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2163113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2164113c68e6SMatthew G. Knepley     {
2165113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
2166113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
21670453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2168113c68e6SMatthew G. Knepley 
2169113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
2170113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
217106348e87SToby Isaac       if (ncells > 1) {
217206348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
2173113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
217406348e87SToby Isaac       }
217506348e87SToby Isaac       else {
217606348e87SToby Isaac         rcentroid = fg->centroid;
217706348e87SToby Isaac       }
21782e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
21792e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
21800453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2181113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2182113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2183113c68e6SMatthew G. Knepley       }
2184113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
2185113c68e6SMatthew 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]);
2186113c68e6SMatthew 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]);
2187113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
2188113c68e6SMatthew G. Knepley       }
2189113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2190113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2191113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2192113c68e6SMatthew G. Knepley       }
219306348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2194113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2195113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2196113c68e6SMatthew G. Knepley       }
2197113c68e6SMatthew G. Knepley     }
2198113c68e6SMatthew G. Knepley   }
2199820f2d46SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
2200113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
2201113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2202113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2203113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2204113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
2205113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2206113c68e6SMatthew G. Knepley 
2207113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
2208113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
2209113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
2210113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
221150d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
2212113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
2213113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
2214113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2215113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2216113c68e6SMatthew G. Knepley       if (support[s] == c) {
2217640bce14SSatish Balay         PetscFVCellGeom       *ci;
2218113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
2219113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
2220113c68e6SMatthew G. Knepley 
2221113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
2222113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2223113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
2224113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
2225113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
2226113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2227113c68e6SMatthew G. Knepley       }
2228113c68e6SMatthew G. Knepley     }
2229113c68e6SMatthew G. Knepley   }
2230113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
2231113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2232113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2233113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
2234113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2235113c68e6SMatthew G. Knepley }
2236113c68e6SMatthew G. Knepley 
2237113c68e6SMatthew G. Knepley /*@C
2238113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2239113c68e6SMatthew G. Knepley 
2240113c68e6SMatthew G. Knepley   Not collective
2241113c68e6SMatthew G. Knepley 
2242*4165533cSJose E. Roman   Input Parameter:
2243113c68e6SMatthew G. Knepley . dm - the DM
2244113c68e6SMatthew G. Knepley 
2245*4165533cSJose E. Roman   Output Parameter:
2246a5b23f4aSJose E. Roman . minradius - the minimum cell radius
2247113c68e6SMatthew G. Knepley 
2248113c68e6SMatthew G. Knepley   Level: developer
2249113c68e6SMatthew G. Knepley 
2250113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
2251113c68e6SMatthew G. Knepley @*/
2252113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2253113c68e6SMatthew G. Knepley {
2254113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2255113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2256113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
2257113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
2258113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2259113c68e6SMatthew G. Knepley }
2260113c68e6SMatthew G. Knepley 
2261113c68e6SMatthew G. Knepley /*@C
2262113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2263113c68e6SMatthew G. Knepley 
2264113c68e6SMatthew G. Knepley   Logically collective
2265113c68e6SMatthew G. Knepley 
2266*4165533cSJose E. Roman   Input Parameters:
2267113c68e6SMatthew G. Knepley + dm - the DM
2268a5b23f4aSJose E. Roman - minradius - the minimum cell radius
2269113c68e6SMatthew G. Knepley 
2270113c68e6SMatthew G. Knepley   Level: developer
2271113c68e6SMatthew G. Knepley 
2272113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
2273113c68e6SMatthew G. Knepley @*/
2274113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2275113c68e6SMatthew G. Knepley {
2276113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2277113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2278113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
2279113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2280113c68e6SMatthew G. Knepley }
2281856ac710SMatthew G. Knepley 
2282856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2283856ac710SMatthew G. Knepley {
2284856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
2285856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
2286856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2287856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2288856ac710SMatthew G. Knepley 
2289856ac710SMatthew G. Knepley   PetscFunctionBegin;
2290856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2291856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2292485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2293089217ebSMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
2294856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
2295856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2296c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2297856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2298856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2299856ac710SMatthew G. Knepley     const PetscInt        *faces;
2300856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
2301640bce14SSatish Balay     PetscFVCellGeom        *cg;
2302856ac710SMatthew G. Knepley     PetscBool              boundary;
2303856ac710SMatthew G. Knepley     PetscInt               ghost;
2304856ac710SMatthew G. Knepley 
2305856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2306856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
2307856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
2308856ac710SMatthew 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);
2309856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2310640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2311856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
2312856ac710SMatthew G. Knepley       const PetscInt        *fcells;
2313856ac710SMatthew G. Knepley       PetscInt               ncell, side;
2314856ac710SMatthew G. Knepley 
2315856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2316a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2317856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2318856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
2319856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2320856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
2321856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
2322856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2323856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
2324856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
2325856ac710SMatthew G. Knepley     }
2326856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
2327856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
2328856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2329856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2330a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2331856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2332856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
2333856ac710SMatthew G. Knepley       ++usedFaces;
2334856ac710SMatthew G. Knepley     }
2335856ac710SMatthew G. Knepley   }
2336856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
2337856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2338856ac710SMatthew G. Knepley }
2339856ac710SMatthew G. Knepley 
2340b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2341b81db932SToby Isaac {
2342b81db932SToby Isaac   DMLabel        ghostLabel;
2343b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
2344b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2345b81db932SToby Isaac   PetscSection   neighSec;
2346b81db932SToby Isaac   PetscInt     (*neighbors)[2];
2347b81db932SToby Isaac   PetscInt      *counter;
2348b81db932SToby Isaac   PetscErrorCode ierr;
2349b81db932SToby Isaac 
2350b81db932SToby Isaac   PetscFunctionBegin;
2351b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2352b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2353485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2354485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2355b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
2356b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
2357b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2358c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2359b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2360b81db932SToby Isaac     const PetscInt        *fcells;
2361b81db932SToby Isaac     PetscBool              boundary;
23625bc680faSToby Isaac     PetscInt               ghost = -1;
2363b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2364b81db932SToby Isaac 
236506348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2366a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2367b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2368b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2369b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
237006348e87SToby Isaac     if (numCells == 2) {
2371b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2372b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2373b81db932SToby Isaac         PetscInt cell = fcells[c];
2374b81db932SToby Isaac 
2375e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2376b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
2377b81db932SToby Isaac         }
2378b81db932SToby Isaac       }
2379b81db932SToby Isaac     }
238006348e87SToby Isaac   }
2381b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
2382b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
2383b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2384b81db932SToby Isaac   nStart = 0;
2385b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2386b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2387b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2388b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2389b81db932SToby Isaac     const PetscInt        *fcells;
2390b81db932SToby Isaac     PetscBool              boundary;
23915bc680faSToby Isaac     PetscInt               ghost = -1;
2392b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2393b81db932SToby Isaac 
239406348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2395a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2396b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2397b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2398b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
239906348e87SToby Isaac     if (numCells == 2) {
2400b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2401b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2402b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2403b81db932SToby Isaac 
2404e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2405b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2406b81db932SToby Isaac           off += counter[cell - cStart]++;
2407b81db932SToby Isaac           neighbors[off][0] = f;
2408b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2409b81db932SToby Isaac         }
2410b81db932SToby Isaac       }
2411b81db932SToby Isaac     }
241206348e87SToby Isaac   }
2413b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2414b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2415b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2416317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2417640bce14SSatish Balay     PetscFVCellGeom        *cg;
2418b81db932SToby Isaac 
2419b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2420b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2421b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2422317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2423317218b9SToby 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);
2424b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2425640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2426b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2427b81db932SToby Isaac       const PetscInt        *fcells;
2428b81db932SToby Isaac       PetscInt               ncell, side, nface;
2429b81db932SToby Isaac 
2430b81db932SToby Isaac       nface = neighbors[off + f][0];
2431b81db932SToby Isaac       ncell = neighbors[off + f][1];
2432b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2433b81db932SToby Isaac       side  = (c != fcells[0]);
2434b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2435b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2436b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2437b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2438b81db932SToby Isaac     }
2439b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2440b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2441b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2442b81db932SToby Isaac     }
2443b81db932SToby Isaac   }
2444b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
24455fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2446b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2447b81db932SToby Isaac   PetscFunctionReturn(0);
2448b81db932SToby Isaac }
2449b81db932SToby Isaac 
2450856ac710SMatthew G. Knepley /*@
2451856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2452856ac710SMatthew G. Knepley 
2453d083f849SBarry Smith   Collective on dm
2454856ac710SMatthew G. Knepley 
2455*4165533cSJose E. Roman   Input Parameters:
2456856ac710SMatthew G. Knepley + dm  - The DM
2457856ac710SMatthew G. Knepley . fvm - The PetscFV
24588f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2459856ac710SMatthew G. Knepley 
24606b867d5aSJose E. Roman   Input/Output Parameter:
24616b867d5aSJose E. Roman . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM(); on output
24626b867d5aSJose E. Roman                  the geometric factors for gradient calculation are inserted
24636b867d5aSJose E. Roman 
24646b867d5aSJose E. Roman   Output Parameter:
24656b867d5aSJose E. Roman . dmGrad - The DM describing the layout of gradient data
2466856ac710SMatthew G. Knepley 
2467856ac710SMatthew G. Knepley   Level: developer
2468856ac710SMatthew G. Knepley 
2469856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2470856ac710SMatthew G. Knepley @*/
2471856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2472856ac710SMatthew G. Knepley {
2473856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2474856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2475b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2476856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2477856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2478856ac710SMatthew G. Knepley 
2479856ac710SMatthew G. Knepley   PetscFunctionBegin;
2480856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2481856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2482856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2483485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2484856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2485856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2486856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2487856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2488856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2489b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2490b81db932SToby Isaac   if (!parentSection) {
2491856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2492b5a3613cSMatthew G. Knepley   } else {
2493b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2494b81db932SToby Isaac   }
2495856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2496856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2497856ac710SMatthew G. Knepley   /* Create storage for gradients */
2498856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2499856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2500856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2501856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2502856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
250392fd8e1eSJed Brown   ierr = DMSetLocalSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2504856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2505856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2506856ac710SMatthew G. Knepley }
2507b27d5b9eSToby Isaac 
2508c501906fSMatthew G. Knepley /*@
2509c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2510c501906fSMatthew G. Knepley 
2511d083f849SBarry Smith   Collective on dm
2512c501906fSMatthew G. Knepley 
2513*4165533cSJose E. Roman   Input Parameters:
2514c501906fSMatthew G. Knepley + dm  - The DM
25156b867d5aSJose E. Roman - fv  - The PetscFV
2516c501906fSMatthew G. Knepley 
2517c501906fSMatthew G. Knepley   Output Parameters:
2518c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2519c501906fSMatthew G. Knepley . faceGeometry - The face geometry
25206b867d5aSJose E. Roman - gradDM       - The gradient matrices
2521c501906fSMatthew G. Knepley 
2522c501906fSMatthew G. Knepley   Level: developer
2523c501906fSMatthew G. Knepley 
2524c501906fSMatthew G. Knepley .seealso: DMPlexComputeGeometryFVM()
2525c501906fSMatthew G. Knepley @*/
2526b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2527b27d5b9eSToby Isaac {
2528b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2529b27d5b9eSToby Isaac   PetscErrorCode ierr;
2530b27d5b9eSToby Isaac 
2531b27d5b9eSToby Isaac   PetscFunctionBegin;
2532b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2533b27d5b9eSToby Isaac   if (!cellgeomobj) {
2534b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2535b27d5b9eSToby Isaac 
2536b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2537b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2538b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2539b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2540b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2541b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2542b27d5b9eSToby Isaac   }
2543b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2544b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2545b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2546b27d5b9eSToby Isaac   if (gradDM) {
2547b27d5b9eSToby Isaac     PetscObject gradobj;
2548b27d5b9eSToby Isaac     PetscBool   computeGradients;
2549b27d5b9eSToby Isaac 
2550b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2551b27d5b9eSToby Isaac     if (!computeGradients) {
2552b27d5b9eSToby Isaac       *gradDM = NULL;
2553b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2554b27d5b9eSToby Isaac     }
2555b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2556b27d5b9eSToby Isaac     if (!gradobj) {
2557b27d5b9eSToby Isaac       DM dmGradInt;
2558b27d5b9eSToby Isaac 
2559b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2560b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2561b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2562b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2563b27d5b9eSToby Isaac     }
2564b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2565b27d5b9eSToby Isaac   }
2566b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2567b27d5b9eSToby Isaac }
2568d6143a4eSToby Isaac 
25699d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
25709d150b73SToby Isaac {
25719d150b73SToby Isaac   PetscInt l, m;
25729d150b73SToby Isaac 
2573cd345991SToby Isaac   PetscFunctionBeginHot;
25749d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
25759d150b73SToby Isaac     /* invert Jacobian, multiply */
25769d150b73SToby Isaac     PetscScalar det, idet;
25779d150b73SToby Isaac 
25789d150b73SToby Isaac     switch (dimR) {
25799d150b73SToby Isaac     case 1:
25809d150b73SToby Isaac       invJ[0] = 1./ J[0];
25819d150b73SToby Isaac       break;
25829d150b73SToby Isaac     case 2:
25839d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
25849d150b73SToby Isaac       idet = 1./det;
25859d150b73SToby Isaac       invJ[0] =  J[3] * idet;
25869d150b73SToby Isaac       invJ[1] = -J[1] * idet;
25879d150b73SToby Isaac       invJ[2] = -J[2] * idet;
25889d150b73SToby Isaac       invJ[3] =  J[0] * idet;
25899d150b73SToby Isaac       break;
25909d150b73SToby Isaac     case 3:
25919d150b73SToby Isaac       {
25929d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
25939d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
25949d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
25959d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
25969d150b73SToby Isaac         idet = 1./det;
25979d150b73SToby Isaac         invJ[0] *= idet;
25989d150b73SToby Isaac         invJ[1] *= idet;
25999d150b73SToby Isaac         invJ[2] *= idet;
26009d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
26019d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
26029d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
26039d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
26049d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
26059d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
26069d150b73SToby Isaac       }
26079d150b73SToby Isaac       break;
26089d150b73SToby Isaac     }
26099d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
26109d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2611c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
26129d150b73SToby Isaac       }
26139d150b73SToby Isaac     }
26149d150b73SToby Isaac   } else {
26159d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
26169d150b73SToby Isaac     char transpose = 'C';
26179d150b73SToby Isaac #else
26189d150b73SToby Isaac     char transpose = 'T';
26199d150b73SToby Isaac #endif
26209d150b73SToby Isaac     PetscBLASInt m = dimR;
26219d150b73SToby Isaac     PetscBLASInt n = dimC;
26229d150b73SToby Isaac     PetscBLASInt one = 1;
26239d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
26249d150b73SToby Isaac 
26259d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
26269d150b73SToby Isaac 
26279d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
26289d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
26299d150b73SToby Isaac 
2630c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
26319d150b73SToby Isaac   }
26329d150b73SToby Isaac   PetscFunctionReturn(0);
26339d150b73SToby Isaac }
26349d150b73SToby Isaac 
26359d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
26369d150b73SToby Isaac {
2637c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
26389d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
26399d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
26409d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
26419d150b73SToby Isaac   PetscErrorCode ierr;
26429d150b73SToby Isaac 
26439d150b73SToby Isaac   PetscFunctionBegin;
26449d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26459d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
264613903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
264769291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
264869291d52SBarry Smith   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
26499d150b73SToby Isaac   cellCoords = &cellData[0];
26509d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
26519d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
26529d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
26539d150b73SToby Isaac   invJ       = &J[dimR * dimC];
26549d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
26559d150b73SToby Isaac   if (dimR == 2) {
26569d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
26579d150b73SToby Isaac 
26589d150b73SToby Isaac     for (i = 0; i < 4; i++) {
26599d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
26609d150b73SToby Isaac 
26619d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26629d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26639d150b73SToby Isaac       }
26649d150b73SToby Isaac     }
26659d150b73SToby Isaac   } else if (dimR == 3) {
26669d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
26679d150b73SToby Isaac 
26689d150b73SToby Isaac     for (i = 0; i < 8; i++) {
26699d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
26709d150b73SToby Isaac 
26719d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26729d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26739d150b73SToby Isaac       }
26749d150b73SToby Isaac     }
26759d150b73SToby Isaac   } else {
26769d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
26779d150b73SToby Isaac   }
26789d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
26799d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
26809d150b73SToby Isaac     PetscReal *swap;
26819d150b73SToby Isaac 
26829d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
26839d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
26849d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
26859d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
26869d150b73SToby Isaac       }
26879d150b73SToby Isaac     }
26889d150b73SToby Isaac 
26899d150b73SToby Isaac     if (i < dimR - 1) {
26909d150b73SToby Isaac       swap = cellCoeffs;
26919d150b73SToby Isaac       cellCoeffs = cellCoords;
26929d150b73SToby Isaac       cellCoords = swap;
26939d150b73SToby Isaac     }
26949d150b73SToby Isaac   }
2695580bdb30SBarry Smith   ierr = PetscArrayzero(refCoords,numPoints * dimR);CHKERRQ(ierr);
26969d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
26979d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
26989d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
26999d150b73SToby Isaac 
27009d150b73SToby Isaac       /* compute -residual and Jacobian */
27019d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
27029d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
27039d150b73SToby Isaac       for (k = 0; k < numV; k++) {
27049d150b73SToby Isaac         PetscReal extCoord = 1.;
27059d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
27069d150b73SToby Isaac           PetscReal coord = guess[l];
27079d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
27089d150b73SToby Isaac 
27099d150b73SToby Isaac           extCoord *= dep * coord + !dep;
27109d150b73SToby Isaac           extJ[l] = dep;
27119d150b73SToby Isaac 
27129d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
27139d150b73SToby Isaac             PetscReal coord = guess[m];
27149d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
27159d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
27169d150b73SToby Isaac 
27179d150b73SToby Isaac             extJ[l] *= mult;
27189d150b73SToby Isaac           }
27199d150b73SToby Isaac         }
27209d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
27219d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
27229d150b73SToby Isaac 
27239d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
27249d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
27259d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
27269d150b73SToby Isaac           }
27279d150b73SToby Isaac         }
27289d150b73SToby Isaac       }
272976bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
27300611203eSToby Isaac         PetscReal maxAbs = 0.;
27310611203eSToby Isaac 
27320611203eSToby Isaac         for (l = 0; l < dimC; l++) {
27330611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
27340611203eSToby Isaac         }
2735087ef6b2SMatthew G. Knepley         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
27360611203eSToby Isaac       }
27379d150b73SToby Isaac 
27389d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
27399d150b73SToby Isaac     }
27409d150b73SToby Isaac   }
274169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
274269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
27439d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
27449d150b73SToby Isaac   PetscFunctionReturn(0);
27459d150b73SToby Isaac }
27469d150b73SToby Isaac 
27479d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
27489d150b73SToby Isaac {
27499d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
27509d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
27519d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
27529d150b73SToby Isaac   PetscErrorCode ierr;
27539d150b73SToby Isaac 
27549d150b73SToby Isaac   PetscFunctionBegin;
27559d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27569d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
275713903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
275869291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
27599d150b73SToby Isaac   cellCoords = &cellData[0];
27609d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
27619d150b73SToby Isaac   if (dimR == 2) {
27629d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
27639d150b73SToby Isaac 
27649d150b73SToby Isaac     for (i = 0; i < 4; i++) {
27659d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
27669d150b73SToby Isaac 
27679d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
27689d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
27699d150b73SToby Isaac       }
27709d150b73SToby Isaac     }
27719d150b73SToby Isaac   } else if (dimR == 3) {
27729d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
27739d150b73SToby Isaac 
27749d150b73SToby Isaac     for (i = 0; i < 8; i++) {
27759d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
27769d150b73SToby Isaac 
27779d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
27789d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
27799d150b73SToby Isaac       }
27809d150b73SToby Isaac     }
27819d150b73SToby Isaac   } else {
27829d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
27839d150b73SToby Isaac   }
27849d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
27859d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
27869d150b73SToby Isaac     PetscReal *swap;
27879d150b73SToby Isaac 
27889d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
27899d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
27909d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
27919d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
27929d150b73SToby Isaac       }
27939d150b73SToby Isaac     }
27949d150b73SToby Isaac 
27959d150b73SToby Isaac     if (i < dimR - 1) {
27969d150b73SToby Isaac       swap = cellCoeffs;
27979d150b73SToby Isaac       cellCoeffs = cellCoords;
27989d150b73SToby Isaac       cellCoords = swap;
27999d150b73SToby Isaac     }
28009d150b73SToby Isaac   }
2801580bdb30SBarry Smith   ierr = PetscArrayzero(realCoords,numPoints * dimC);CHKERRQ(ierr);
28029d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28039d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
28049d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
28059d150b73SToby Isaac 
28069d150b73SToby Isaac     for (k = 0; k < numV; k++) {
28079d150b73SToby Isaac       PetscReal extCoord = 1.;
28089d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
28099d150b73SToby Isaac         PetscReal coord = guess[l];
28109d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
28119d150b73SToby Isaac 
28129d150b73SToby Isaac         extCoord *= dep * coord + !dep;
28139d150b73SToby Isaac       }
28149d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
28159d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
28169d150b73SToby Isaac 
28179d150b73SToby Isaac         mapped[l] += coeff * extCoord;
28189d150b73SToby Isaac       }
28199d150b73SToby Isaac     }
28209d150b73SToby Isaac   }
282169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
28229d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
28239d150b73SToby Isaac   PetscFunctionReturn(0);
28249d150b73SToby Isaac }
28259d150b73SToby Isaac 
28269c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
28279c3cf19fSMatthew 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)
28289d150b73SToby Isaac {
28299c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
2830c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2831c6e120d1SToby Isaac   PetscReal      *invV, *modes;
2832c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
2833c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
28349d150b73SToby Isaac   PetscErrorCode ierr;
28359d150b73SToby Isaac 
28369d150b73SToby Isaac   PetscFunctionBegin;
28379c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
28389d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
28399c3cf19fSMatthew 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);
28409d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
28419d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
284269291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
28439d150b73SToby Isaac   invV = fe->invV;
2844012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2845012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2846012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2847012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
28489d150b73SToby Isaac     }
28499d150b73SToby Isaac   }
285069291d52SBarry Smith   ierr   = DMGetWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
28519c3cf19fSMatthew G. Knepley   D      = &B[pdim*Nc];
28529c3cf19fSMatthew G. Knepley   resNeg = &D[pdim*Nc * dimR];
285369291d52SBarry Smith   ierr = DMGetWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
28549c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
28559c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
28569d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
28579d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28589b1f03cbSToby 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 */
28599d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
28609d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
28619c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc; k++) {resNeg[k] = realCoords[j * Nc + k];}
28629c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc * dimR; k++) {J[k] = 0.;}
28639c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
28649c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
2865012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
28669d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
2867012b7cc6SMatthew G. Knepley             J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
28689d150b73SToby Isaac           }
28699d150b73SToby Isaac         }
28709d150b73SToby Isaac       }
287176bd3646SJed Brown       if (0 && PetscDefined(USE_DEBUG)) {
28720611203eSToby Isaac         PetscReal maxAbs = 0.;
28730611203eSToby Isaac 
28749c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
28750611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
28760611203eSToby Isaac         }
2877087ef6b2SMatthew G. Knepley         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
28780611203eSToby Isaac       }
28799c3cf19fSMatthew G. Knepley       ierr = DMPlexCoordinatesToReference_NewtonUpdate(Nc,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
28809d150b73SToby Isaac     }
28819d150b73SToby Isaac   }
288269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
288369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
288469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
28859d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
28869d150b73SToby Isaac   PetscFunctionReturn(0);
28879d150b73SToby Isaac }
28889d150b73SToby Isaac 
28899c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
28909c3cf19fSMatthew 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)
28919d150b73SToby Isaac {
28929c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, coordSize;
2893c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2894c6e120d1SToby Isaac   PetscReal      *invV, *modes;
28959d150b73SToby Isaac   PetscReal      *B;
28969d150b73SToby Isaac   PetscErrorCode ierr;
28979d150b73SToby Isaac 
28989d150b73SToby Isaac   PetscFunctionBegin;
28999c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
29009d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
29019c3cf19fSMatthew 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);
29029d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
29039d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
290469291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
29059d150b73SToby Isaac   invV = fe->invV;
2906012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2907012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2908012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2909012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
29109d150b73SToby Isaac     }
29119d150b73SToby Isaac   }
291269291d52SBarry Smith   ierr = DMGetWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
2913012b7cc6SMatthew G. Knepley   ierr = PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL);CHKERRQ(ierr);
29149c3cf19fSMatthew G. Knepley   for (i = 0; i < numPoints * Nc; i++) {realCoords[i] = 0.;}
29159d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
29169c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
29179d150b73SToby Isaac 
29189c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
29199c3cf19fSMatthew G. Knepley       for (l = 0; l < Nc; l++) {
292040cf36b3SToby Isaac         mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
29219d150b73SToby Isaac       }
29229d150b73SToby Isaac     }
29239d150b73SToby Isaac   }
292469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
292569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
29269d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
29279d150b73SToby Isaac   PetscFunctionReturn(0);
29289d150b73SToby Isaac }
29299d150b73SToby Isaac 
2930d6143a4eSToby Isaac /*@
2931d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
2932d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
2933d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
2934d6143a4eSToby Isaac 
2935d6143a4eSToby Isaac   Not collective
2936d6143a4eSToby Isaac 
2937d6143a4eSToby Isaac   Input Parameters:
2938d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2939d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2940d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
2941d6143a4eSToby Isaac . cell       - the cell whose map is used.
2942d6143a4eSToby Isaac . numPoints  - the number of points to locate
29431b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2944d6143a4eSToby Isaac 
2945d6143a4eSToby Isaac   Output Parameters:
2946d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
29471b266c99SBarry Smith 
29481b266c99SBarry Smith   Level: intermediate
294973c9229bSMatthew Knepley 
295073c9229bSMatthew Knepley .seealso: DMPlexReferenceToCoordinates()
2951d6143a4eSToby Isaac @*/
2952d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
2953d6143a4eSToby Isaac {
2954485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
29559d150b73SToby Isaac   DM             coordDM = NULL;
29569d150b73SToby Isaac   Vec            coords;
29579d150b73SToby Isaac   PetscFE        fe = NULL;
29589d150b73SToby Isaac   PetscErrorCode ierr;
29599d150b73SToby Isaac 
2960d6143a4eSToby Isaac   PetscFunctionBegin;
29619d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29629d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
29639d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
29649d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
29659d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
29669d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
29679d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
29689d150b73SToby Isaac   if (coordDM) {
29699d150b73SToby Isaac     PetscInt coordFields;
29709d150b73SToby Isaac 
29719d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
29729d150b73SToby Isaac     if (coordFields) {
29739d150b73SToby Isaac       PetscClassId id;
29749d150b73SToby Isaac       PetscObject  disc;
29759d150b73SToby Isaac 
297644a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
29779d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
29789d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
29799d150b73SToby Isaac         fe = (PetscFE) disc;
29809d150b73SToby Isaac       }
29819d150b73SToby Isaac     }
29829d150b73SToby Isaac   }
2983412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
298413903a91SSatish 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);
29859d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
29869d150b73SToby Isaac     PetscInt  coneSize;
29879d150b73SToby Isaac     PetscBool isSimplex, isTensor;
29889d150b73SToby Isaac 
29899d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
29909d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
29919d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
29929d150b73SToby Isaac     if (isSimplex) {
29939d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
29949d150b73SToby Isaac 
299569291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
29969d150b73SToby Isaac       J    = &v0[dimC];
29979d150b73SToby Isaac       invJ = &J[dimC * dimC];
29989d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
29999d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3000c330f8ffSToby Isaac         const PetscReal x0[3] = {-1.,-1.,-1.};
3001c330f8ffSToby Isaac 
3002c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
30039d150b73SToby Isaac       }
300469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
30059d150b73SToby Isaac     } else if (isTensor) {
30069d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
30079d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
30089d150b73SToby Isaac   } else {
30099d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
30109d150b73SToby Isaac   }
30119d150b73SToby Isaac   PetscFunctionReturn(0);
30129d150b73SToby Isaac }
30139d150b73SToby Isaac 
30149d150b73SToby Isaac /*@
30159d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
30169d150b73SToby Isaac 
30179d150b73SToby Isaac   Not collective
30189d150b73SToby Isaac 
30199d150b73SToby Isaac   Input Parameters:
30209d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
30219d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
30229d150b73SToby Isaac                as a multilinear map for tensor-product elements
30239d150b73SToby Isaac . cell       - the cell whose map is used.
30249d150b73SToby Isaac . numPoints  - the number of points to locate
3025a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
30269d150b73SToby Isaac 
30279d150b73SToby Isaac   Output Parameters:
30289d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
30291b266c99SBarry Smith 
30301b266c99SBarry Smith    Level: intermediate
303173c9229bSMatthew Knepley 
303273c9229bSMatthew Knepley .seealso: DMPlexCoordinatesToReference()
30339d150b73SToby Isaac @*/
30349d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
30359d150b73SToby Isaac {
3036485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
30379d150b73SToby Isaac   DM             coordDM = NULL;
30389d150b73SToby Isaac   Vec            coords;
30399d150b73SToby Isaac   PetscFE        fe = NULL;
30409d150b73SToby Isaac   PetscErrorCode ierr;
30419d150b73SToby Isaac 
30429d150b73SToby Isaac   PetscFunctionBegin;
30439d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30449d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
30459d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
30469d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
30479d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
30489d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
30499d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
30509d150b73SToby Isaac   if (coordDM) {
30519d150b73SToby Isaac     PetscInt coordFields;
30529d150b73SToby Isaac 
30539d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
30549d150b73SToby Isaac     if (coordFields) {
30559d150b73SToby Isaac       PetscClassId id;
30569d150b73SToby Isaac       PetscObject  disc;
30579d150b73SToby Isaac 
305844a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
30599d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
30609d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
30619d150b73SToby Isaac         fe = (PetscFE) disc;
30629d150b73SToby Isaac       }
30639d150b73SToby Isaac     }
30649d150b73SToby Isaac   }
3065412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
306613903a91SSatish 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);
30679d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
30689d150b73SToby Isaac     PetscInt  coneSize;
30699d150b73SToby Isaac     PetscBool isSimplex, isTensor;
30709d150b73SToby Isaac 
30719d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
30729d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
30739d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
30749d150b73SToby Isaac     if (isSimplex) {
30759d150b73SToby Isaac       PetscReal detJ, *v0, *J;
30769d150b73SToby Isaac 
307769291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
30789d150b73SToby Isaac       J    = &v0[dimC];
30799d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
3080c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3081c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1.,-1.,-1.};
3082c330f8ffSToby Isaac 
3083c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
30849d150b73SToby Isaac       }
308569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
30869d150b73SToby Isaac     } else if (isTensor) {
30879d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
30889d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
30899d150b73SToby Isaac   } else {
30909d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
30919d150b73SToby Isaac   }
3092d6143a4eSToby Isaac   PetscFunctionReturn(0);
3093d6143a4eSToby Isaac }
30940139fca9SMatthew G. Knepley 
30950139fca9SMatthew G. Knepley /*@C
30960139fca9SMatthew G. Knepley   DMPlexRemapGeometry - This function maps the original DM coordinates to new coordinates.
30970139fca9SMatthew G. Knepley 
30980139fca9SMatthew G. Knepley   Not collective
30990139fca9SMatthew G. Knepley 
31000139fca9SMatthew G. Knepley   Input Parameters:
31010139fca9SMatthew G. Knepley + dm      - The DM
31020139fca9SMatthew G. Knepley . time    - The time
31030139fca9SMatthew G. Knepley - func    - The function transforming current coordinates to new coordaintes
31040139fca9SMatthew G. Knepley 
31050139fca9SMatthew G. Knepley    Calling sequence of func:
31060139fca9SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
31070139fca9SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
31080139fca9SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
31090139fca9SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
31100139fca9SMatthew G. Knepley 
31110139fca9SMatthew G. Knepley +  dim          - The spatial dimension
31120139fca9SMatthew G. Knepley .  Nf           - The number of input fields (here 1)
31130139fca9SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
31140139fca9SMatthew G. Knepley .  uOff         - The offset of the coordinates in u[] (here 0)
31150139fca9SMatthew G. Knepley .  uOff_x       - The offset of the coordinates in u_x[] (here 0)
31160139fca9SMatthew G. Knepley .  u            - The coordinate values at this point in space
31170139fca9SMatthew G. Knepley .  u_t          - The coordinate time derivative at this point in space (here NULL)
31180139fca9SMatthew G. Knepley .  u_x          - The coordinate derivatives at this point in space
31190139fca9SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
31200139fca9SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
31210139fca9SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
31220139fca9SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
31230139fca9SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
31240139fca9SMatthew G. Knepley .  t            - The current time
31250139fca9SMatthew G. Knepley .  x            - The coordinates of this point (here not used)
31260139fca9SMatthew G. Knepley .  numConstants - The number of constants
31270139fca9SMatthew G. Knepley .  constants    - The value of each constant
31280139fca9SMatthew G. Knepley -  f            - The new coordinates at this point in space
31290139fca9SMatthew G. Knepley 
31300139fca9SMatthew G. Knepley   Level: intermediate
31310139fca9SMatthew G. Knepley 
31320139fca9SMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMProjectFieldLocal(), DMProjectFieldLabelLocal()
31330139fca9SMatthew G. Knepley @*/
31340139fca9SMatthew G. Knepley PetscErrorCode DMPlexRemapGeometry(DM dm, PetscReal time,
31350139fca9SMatthew G. Knepley                                    void (*func)(PetscInt, PetscInt, PetscInt,
31360139fca9SMatthew G. Knepley                                                 const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
31370139fca9SMatthew G. Knepley                                                 const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
31380139fca9SMatthew G. Knepley                                                 PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]))
31390139fca9SMatthew G. Knepley {
31400139fca9SMatthew G. Knepley   DM             cdm;
31418bf1a49fSMatthew G. Knepley   DMField        cf;
31420139fca9SMatthew G. Knepley   Vec            lCoords, tmpCoords;
31430139fca9SMatthew G. Knepley   PetscErrorCode ierr;
31440139fca9SMatthew G. Knepley 
31450139fca9SMatthew G. Knepley   PetscFunctionBegin;
31460139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
31470139fca9SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &lCoords);CHKERRQ(ierr);
31480139fca9SMatthew G. Knepley   ierr = DMGetLocalVector(cdm, &tmpCoords);CHKERRQ(ierr);
31490139fca9SMatthew G. Knepley   ierr = VecCopy(lCoords, tmpCoords);CHKERRQ(ierr);
31508bf1a49fSMatthew G. Knepley   /* We have to do the coordinate field manually right now since the coordinate DM will not have its own */
31518bf1a49fSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &cf);CHKERRQ(ierr);
31528bf1a49fSMatthew G. Knepley   cdm->coordinateField = cf;
31530139fca9SMatthew G. Knepley   ierr = DMProjectFieldLocal(cdm, time, tmpCoords, &func, INSERT_VALUES, lCoords);CHKERRQ(ierr);
31548bf1a49fSMatthew G. Knepley   cdm->coordinateField = NULL;
31550139fca9SMatthew G. Knepley   ierr = DMRestoreLocalVector(cdm, &tmpCoords);CHKERRQ(ierr);
3156cdaaecf7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, lCoords);CHKERRQ(ierr);
31570139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
31580139fca9SMatthew G. Knepley }
31590139fca9SMatthew G. Knepley 
31600139fca9SMatthew G. Knepley /* Shear applies the transformation, assuming we fix z,
31610139fca9SMatthew G. Knepley   / 1  0  m_0 \
31620139fca9SMatthew G. Knepley   | 0  1  m_1 |
31630139fca9SMatthew G. Knepley   \ 0  0   1  /
31640139fca9SMatthew G. Knepley */
31650139fca9SMatthew G. Knepley static void f0_shear(PetscInt dim, PetscInt Nf, PetscInt NfAux,
31660139fca9SMatthew G. Knepley                      const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
31670139fca9SMatthew G. Knepley                      const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
31680139fca9SMatthew G. Knepley                      PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar coords[])
31690139fca9SMatthew G. Knepley {
31700139fca9SMatthew G. Knepley   const PetscInt Nc = uOff[1]-uOff[0];
3171c1f1bd54SMatthew G. Knepley   const PetscInt ax = (PetscInt) PetscRealPart(constants[0]);
31720139fca9SMatthew G. Knepley   PetscInt       c;
31730139fca9SMatthew G. Knepley 
31740139fca9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
31750139fca9SMatthew G. Knepley     coords[c] = u[c] + constants[c+1]*u[ax];
31760139fca9SMatthew G. Knepley   }
31770139fca9SMatthew G. Knepley }
31780139fca9SMatthew G. Knepley 
31790139fca9SMatthew G. Knepley /*@C
31800139fca9SMatthew G. Knepley   DMPlexShearGeometry - This shears the domain, meaning adds a multiple of the shear coordinate to all other coordinates.
31810139fca9SMatthew G. Knepley 
31820139fca9SMatthew G. Knepley   Not collective
31830139fca9SMatthew G. Knepley 
31840139fca9SMatthew G. Knepley   Input Parameters:
31850139fca9SMatthew G. Knepley + dm          - The DM
31863ee9839eSMatthew G. Knepley . direction   - The shear coordinate direction, e.g. 0 is the x-axis
31870139fca9SMatthew G. Knepley - multipliers - The multiplier m for each direction which is not the shear direction
31880139fca9SMatthew G. Knepley 
31890139fca9SMatthew G. Knepley   Level: intermediate
31900139fca9SMatthew G. Knepley 
31910139fca9SMatthew G. Knepley .seealso: DMPlexRemapGeometry()
31920139fca9SMatthew G. Knepley @*/
31933ee9839eSMatthew G. Knepley PetscErrorCode DMPlexShearGeometry(DM dm, DMDirection direction, PetscReal multipliers[])
31940139fca9SMatthew G. Knepley {
31950139fca9SMatthew G. Knepley   DM             cdm;
31960139fca9SMatthew G. Knepley   PetscDS        cds;
31970139fca9SMatthew G. Knepley   PetscObject    obj;
31980139fca9SMatthew G. Knepley   PetscClassId   id;
31990139fca9SMatthew G. Knepley   PetscScalar   *moduli;
32003ee9839eSMatthew G. Knepley   const PetscInt dir = (PetscInt) direction;
32010139fca9SMatthew G. Knepley   PetscInt       dE, d, e;
32020139fca9SMatthew G. Knepley   PetscErrorCode ierr;
32030139fca9SMatthew G. Knepley 
32040139fca9SMatthew G. Knepley   PetscFunctionBegin;
32050139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
32060139fca9SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
32070139fca9SMatthew G. Knepley   ierr = PetscMalloc1(dE+1, &moduli);CHKERRQ(ierr);
32080139fca9SMatthew G. Knepley   moduli[0] = dir;
3209cdaaecf7SMatthew G. Knepley   for (d = 0, e = 0; d < dE; ++d) moduli[d+1] = d == dir ? 0.0 : (multipliers ? multipliers[e++] : 1.0);
32100139fca9SMatthew G. Knepley   ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
32110139fca9SMatthew G. Knepley   ierr = PetscDSGetDiscretization(cds, 0, &obj);CHKERRQ(ierr);
32120139fca9SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
32130139fca9SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
32140139fca9SMatthew G. Knepley     Vec           lCoords;
32150139fca9SMatthew G. Knepley     PetscSection  cSection;
32160139fca9SMatthew G. Knepley     PetscScalar  *coords;
32170139fca9SMatthew G. Knepley     PetscInt      vStart, vEnd, v;
32180139fca9SMatthew G. Knepley 
32190139fca9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
32200139fca9SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &cSection);CHKERRQ(ierr);
32210139fca9SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &lCoords);CHKERRQ(ierr);
32220139fca9SMatthew G. Knepley     ierr = VecGetArray(lCoords, &coords);CHKERRQ(ierr);
32230139fca9SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
32240139fca9SMatthew G. Knepley       PetscReal ds;
32250139fca9SMatthew G. Knepley       PetscInt  off, c;
32260139fca9SMatthew G. Knepley 
32270139fca9SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, v, &off);CHKERRQ(ierr);
32280139fca9SMatthew G. Knepley       ds   = PetscRealPart(coords[off+dir]);
32290139fca9SMatthew G. Knepley       for (c = 0; c < dE; ++c) coords[off+c] += moduli[c]*ds;
32300139fca9SMatthew G. Knepley     }
32310139fca9SMatthew G. Knepley     ierr = VecRestoreArray(lCoords, &coords);CHKERRQ(ierr);
32320139fca9SMatthew G. Knepley   } else {
32330139fca9SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, dE+1, moduli);CHKERRQ(ierr);
32340139fca9SMatthew G. Knepley     ierr = DMPlexRemapGeometry(dm, 0.0, f0_shear);CHKERRQ(ierr);
32350139fca9SMatthew G. Knepley   }
32360139fca9SMatthew G. Knepley   ierr = PetscFree(moduli);CHKERRQ(ierr);
32370139fca9SMatthew G. Knepley   PetscFunctionReturn(0);
32380139fca9SMatthew G. Knepley }
3239