xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision d80ece959af10c1aa984cf3c9a69f33acf9f641b)
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 
313985bb02SVaclav Hapla .seealso: DMPlexCreate(), DMGetCoordinates()
323985bb02SVaclav Hapla @*/
333985bb02SVaclav Hapla PetscErrorCode DMPlexFindVertices(DM dm, PetscInt npoints, const PetscReal coord[], PetscReal eps, PetscInt dagPoints[])
343985bb02SVaclav Hapla {
353985bb02SVaclav Hapla   PetscInt          c, dim, i, j, ndof, o, p, vStart, vEnd;
363985bb02SVaclav Hapla   PetscSection      cs;
373985bb02SVaclav Hapla   Vec               allCoordsVec;
383985bb02SVaclav Hapla   const PetscScalar *allCoords;
393985bb02SVaclav Hapla   PetscReal         norm;
403985bb02SVaclav Hapla   PetscErrorCode    ierr;
413985bb02SVaclav Hapla 
423985bb02SVaclav Hapla   PetscFunctionBegin;
433985bb02SVaclav Hapla   if (eps < 0) eps = PETSC_SQRT_MACHINE_EPSILON;
443985bb02SVaclav Hapla   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
453985bb02SVaclav Hapla   ierr = DMGetCoordinateSection(dm, &cs);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);
493985bb02SVaclav Hapla   for (i=0,j=0; i < npoints; i++,j+=dim) {
503985bb02SVaclav Hapla     dagPoints[i] = -1;
513985bb02SVaclav Hapla     for (p = vStart; p < vEnd; p++) {
523985bb02SVaclav Hapla       ierr = PetscSectionGetOffset(cs, p, &o);CHKERRQ(ierr);
533985bb02SVaclav Hapla       ierr = PetscSectionGetDof(cs, p, &ndof);CHKERRQ(ierr);
543985bb02SVaclav Hapla       if (PetscUnlikely(ndof != dim)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point %D: ndof = %D != %D = dim", p, ndof, dim);
553985bb02SVaclav Hapla       norm = 0.0;
563985bb02SVaclav Hapla       for (c = 0; c < dim; c++) {
573985bb02SVaclav Hapla         norm += PetscSqr(coord[j+c] - PetscRealPart(allCoords[o+c]));
583985bb02SVaclav Hapla       }
593985bb02SVaclav Hapla       norm = PetscSqrtReal(norm);
603985bb02SVaclav Hapla       if (norm <= eps) {
613985bb02SVaclav Hapla         dagPoints[i] = p;
623985bb02SVaclav Hapla         break;
633985bb02SVaclav Hapla       }
643985bb02SVaclav Hapla     }
653985bb02SVaclav Hapla   }
663985bb02SVaclav Hapla   ierr = VecRestoreArrayRead(allCoordsVec, &allCoords);CHKERRQ(ierr);
673985bb02SVaclav Hapla   PetscFunctionReturn(0);
683985bb02SVaclav Hapla }
693985bb02SVaclav Hapla 
70fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
71fea14342SMatthew G. Knepley {
72fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*2+0];
73fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*2+1];
74fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*2+0];
75fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*2+1];
76fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0*2+0];
77fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0*2+1];
78fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1*2+0];
79fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1*2+1];
80fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
81fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
82fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
83fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
84fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
85fea14342SMatthew G. Knepley 
86fea14342SMatthew G. Knepley   PetscFunctionBegin;
87fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
88fea14342SMatthew G. Knepley   /* Non-parallel lines */
89fea14342SMatthew G. Knepley   if (denom != 0.0) {
90fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
91fea14342SMatthew G. Knepley     const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
92fea14342SMatthew G. Knepley 
93fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
94fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
95fea14342SMatthew G. Knepley       if (intersection) {
96fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
97fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
98fea14342SMatthew G. Knepley       }
99fea14342SMatthew G. Knepley     }
100fea14342SMatthew G. Knepley   }
101fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
102fea14342SMatthew G. Knepley }
103fea14342SMatthew G. Knepley 
104ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
105ccd2543fSMatthew G Knepley {
106ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
107f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
108ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
109ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
110ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
111ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
112ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
113ccd2543fSMatthew G Knepley 
114ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1158e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
116ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
117ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
118ccd2543fSMatthew G Knepley 
119f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
120c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
121ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
122ccd2543fSMatthew G Knepley }
123ccd2543fSMatthew G Knepley 
12462a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
12562a38674SMatthew G. Knepley {
12662a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
12762a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
12862a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
12962a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
13062a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
13162a38674SMatthew G. Knepley   PetscErrorCode  ierr;
13262a38674SMatthew G. Knepley 
13362a38674SMatthew G. Knepley   PetscFunctionBegin;
13462a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
13562a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
13662a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
13762a38674SMatthew G. Knepley 
13862a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
13962a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
14062a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
14162a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
14262a38674SMatthew G. Knepley     xi  /= r;
14362a38674SMatthew G. Knepley     eta /= r;
14462a38674SMatthew G. Knepley   }
14562a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
14662a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
14762a38674SMatthew G. Knepley   PetscFunctionReturn(0);
14862a38674SMatthew G. Knepley }
14962a38674SMatthew G. Knepley 
150ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
151ccd2543fSMatthew G Knepley {
152ccd2543fSMatthew G Knepley   PetscSection       coordSection;
153ccd2543fSMatthew G Knepley   Vec             coordsLocal;
154a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
155ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
156ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
157ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
158ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
159ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
160ccd2543fSMatthew G Knepley 
161ccd2543fSMatthew G Knepley   PetscFunctionBegin;
162ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
16369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
164ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
165ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
166ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
167ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
168ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
169ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
170ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
171ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
172ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
173ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
174ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
175ccd2543fSMatthew G Knepley   }
176ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
177c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
178ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
179ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
180ccd2543fSMatthew G Knepley }
181ccd2543fSMatthew G Knepley 
182ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
183ccd2543fSMatthew G Knepley {
184ccd2543fSMatthew G Knepley   const PetscInt embedDim = 3;
185ccd2543fSMatthew G Knepley   PetscReal      v0[3], J[9], invJ[9], detJ;
186ccd2543fSMatthew G Knepley   PetscReal      x = PetscRealPart(point[0]);
187ccd2543fSMatthew G Knepley   PetscReal      y = PetscRealPart(point[1]);
188ccd2543fSMatthew G Knepley   PetscReal      z = PetscRealPart(point[2]);
189ccd2543fSMatthew G Knepley   PetscReal      xi, eta, zeta;
190ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
191ccd2543fSMatthew G Knepley 
192ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1938e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
194ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
195ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
196ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
197ccd2543fSMatthew G Knepley 
198ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c;
199c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
200ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
201ccd2543fSMatthew G Knepley }
202ccd2543fSMatthew G Knepley 
203ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
204ccd2543fSMatthew G Knepley {
205ccd2543fSMatthew G Knepley   PetscSection   coordSection;
206ccd2543fSMatthew G Knepley   Vec            coordsLocal;
207872a9804SMatthew G. Knepley   PetscScalar   *coords = NULL;
208fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
209fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
210ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
211ccd2543fSMatthew G Knepley   PetscInt       f;
212ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
213ccd2543fSMatthew G Knepley 
214ccd2543fSMatthew G Knepley   PetscFunctionBegin;
215ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
21669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
217ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
218ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
219ccd2543fSMatthew G Knepley     /* Check the point is under plane */
220ccd2543fSMatthew G Knepley     /*   Get face normal */
221ccd2543fSMatthew G Knepley     PetscReal v_i[3];
222ccd2543fSMatthew G Knepley     PetscReal v_j[3];
223ccd2543fSMatthew G Knepley     PetscReal normal[3];
224ccd2543fSMatthew G Knepley     PetscReal pp[3];
225ccd2543fSMatthew G Knepley     PetscReal dot;
226ccd2543fSMatthew G Knepley 
227ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
228ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
229ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
230ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
231ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
232ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
233ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
234ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
235ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
236ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
237ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
238ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
239ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
240ccd2543fSMatthew G Knepley 
241ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
242ccd2543fSMatthew G Knepley     if (dot < 0.0) {
243ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
244ccd2543fSMatthew G Knepley       break;
245ccd2543fSMatthew G Knepley     }
246ccd2543fSMatthew G Knepley   }
247ccd2543fSMatthew G Knepley   if (found) *cell = c;
248c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
249ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
250ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
251ccd2543fSMatthew G Knepley }
252ccd2543fSMatthew G Knepley 
253c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
254c4eade1cSMatthew G. Knepley {
255c4eade1cSMatthew G. Knepley   PetscInt d;
256c4eade1cSMatthew G. Knepley 
257c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
258c4eade1cSMatthew G. Knepley   box->dim = dim;
259c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
260c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
261c4eade1cSMatthew G. Knepley }
262c4eade1cSMatthew G. Knepley 
263c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
264c4eade1cSMatthew G. Knepley {
265c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
266c4eade1cSMatthew G. Knepley 
267c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
268c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
269c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
270c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
271c4eade1cSMatthew G. Knepley }
272c4eade1cSMatthew G. Knepley 
273c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
274c4eade1cSMatthew G. Knepley {
275c4eade1cSMatthew G. Knepley   PetscInt d;
276c4eade1cSMatthew G. Knepley 
277c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
278c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
279c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
280c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
281c4eade1cSMatthew G. Knepley   }
282c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
283c4eade1cSMatthew G. Knepley }
284c4eade1cSMatthew G. Knepley 
28562a38674SMatthew G. Knepley /*
28662a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
28762a38674SMatthew G. Knepley 
28862a38674SMatthew G. Knepley   Not collective
28962a38674SMatthew G. Knepley 
29062a38674SMatthew G. Knepley   Input Parameters:
29162a38674SMatthew G. Knepley + box - The grid hash object
29262a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
29362a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
29462a38674SMatthew G. Knepley 
29562a38674SMatthew G. Knepley   Level: developer
29662a38674SMatthew G. Knepley 
29762a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
29862a38674SMatthew G. Knepley */
299c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
300c4eade1cSMatthew G. Knepley {
301c4eade1cSMatthew G. Knepley   PetscInt d;
302c4eade1cSMatthew G. Knepley 
303c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
304c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
305c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
306c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
307c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
308c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
309c4eade1cSMatthew G. Knepley     } else {
310c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
311c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
312c4eade1cSMatthew G. Knepley     }
313c4eade1cSMatthew G. Knepley   }
314c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
315c4eade1cSMatthew G. Knepley }
316c4eade1cSMatthew G. Knepley 
31762a38674SMatthew G. Knepley /*
31862a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
31962a38674SMatthew G. Knepley 
32062a38674SMatthew G. Knepley   Not collective
32162a38674SMatthew G. Knepley 
32262a38674SMatthew G. Knepley   Input Parameters:
32362a38674SMatthew G. Knepley + box       - The grid hash object
32462a38674SMatthew G. Knepley . numPoints - The number of input points
32562a38674SMatthew G. Knepley - points    - The input point coordinates
32662a38674SMatthew G. Knepley 
32762a38674SMatthew G. Knepley   Output Parameters:
32862a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
32962a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
33062a38674SMatthew G. Knepley 
33162a38674SMatthew G. Knepley   Level: developer
33262a38674SMatthew G. Knepley 
33362a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
33462a38674SMatthew G. Knepley */
3351c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
336c4eade1cSMatthew G. Knepley {
337c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
338c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
339c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
340c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
341c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
342c4eade1cSMatthew G. Knepley   PetscInt         d, p;
343c4eade1cSMatthew G. Knepley 
344c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
345c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
346c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
3471c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
348c4eade1cSMatthew G. Knepley 
3491c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
3502a705cacSMatthew G. Knepley       if (dbox == -1   && PetscAbsReal(PetscRealPart(points[p*dim+d]) - lower[d]) < 1.0e-9) dbox = 0;
351c4eade1cSMatthew 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",
352087ef6b2SMatthew 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);
353c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
354c4eade1cSMatthew G. Knepley     }
355c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
356c4eade1cSMatthew G. Knepley   }
357c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
358c4eade1cSMatthew G. Knepley }
359c4eade1cSMatthew G. Knepley 
360af74b616SDave May /*
361af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
362af74b616SDave May 
363af74b616SDave May  Not collective
364af74b616SDave May 
365af74b616SDave May   Input Parameters:
366af74b616SDave May + box       - The grid hash object
367af74b616SDave May . numPoints - The number of input points
368af74b616SDave May - points    - The input point coordinates
369af74b616SDave May 
370af74b616SDave May   Output Parameters:
371af74b616SDave May + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
372af74b616SDave May . boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
373af74b616SDave May - found     - Flag indicating if point was located within a box
374af74b616SDave May 
375af74b616SDave May   Level: developer
376af74b616SDave May 
377af74b616SDave May .seealso: PetscGridHashGetEnclosingBox()
378af74b616SDave May */
379af74b616SDave May PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[],PetscBool *found)
380af74b616SDave May {
381af74b616SDave May   const PetscReal *lower = box->lower;
382af74b616SDave May   const PetscReal *upper = box->upper;
383af74b616SDave May   const PetscReal *h     = box->h;
384af74b616SDave May   const PetscInt  *n     = box->n;
385af74b616SDave May   const PetscInt   dim   = box->dim;
386af74b616SDave May   PetscInt         d, p;
387af74b616SDave May 
388af74b616SDave May   PetscFunctionBegin;
389af74b616SDave May   *found = PETSC_FALSE;
390af74b616SDave May   for (p = 0; p < numPoints; ++p) {
391af74b616SDave May     for (d = 0; d < dim; ++d) {
392af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
393af74b616SDave May 
394af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
395af74b616SDave May       if (dbox < 0 || dbox >= n[d]) {
396af74b616SDave May         PetscFunctionReturn(0);
397af74b616SDave May       }
398af74b616SDave May       dboxes[p*dim+d] = dbox;
399af74b616SDave May     }
400af74b616SDave May     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
401af74b616SDave May   }
402af74b616SDave May   *found = PETSC_TRUE;
403af74b616SDave May   PetscFunctionReturn(0);
404af74b616SDave May }
405af74b616SDave May 
406c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
407c4eade1cSMatthew G. Knepley {
408c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
409c4eade1cSMatthew G. Knepley 
410c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
411c4eade1cSMatthew G. Knepley   if (*box) {
412c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
413c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
414c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
415c4eade1cSMatthew G. Knepley   }
416c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
417c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
418c4eade1cSMatthew G. Knepley }
419c4eade1cSMatthew G. Knepley 
420cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
421cafe43deSMatthew G. Knepley {
422cafe43deSMatthew G. Knepley   PetscInt       coneSize;
423cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
424cafe43deSMatthew G. Knepley 
425cafe43deSMatthew G. Knepley   PetscFunctionBegin;
426cafe43deSMatthew G. Knepley   switch (dim) {
427cafe43deSMatthew G. Knepley   case 2:
428cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
429cafe43deSMatthew G. Knepley     switch (coneSize) {
430cafe43deSMatthew G. Knepley     case 3:
431cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
432cafe43deSMatthew G. Knepley       break;
433cafe43deSMatthew G. Knepley     case 4:
434cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
435cafe43deSMatthew G. Knepley       break;
436cafe43deSMatthew G. Knepley     default:
437cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
438cafe43deSMatthew G. Knepley     }
439cafe43deSMatthew G. Knepley     break;
440cafe43deSMatthew G. Knepley   case 3:
441cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
442cafe43deSMatthew G. Knepley     switch (coneSize) {
443cafe43deSMatthew G. Knepley     case 4:
444cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
445cafe43deSMatthew G. Knepley       break;
446cafe43deSMatthew G. Knepley     case 6:
447cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
448cafe43deSMatthew G. Knepley       break;
449cafe43deSMatthew G. Knepley     default:
450cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
451cafe43deSMatthew G. Knepley     }
452cafe43deSMatthew G. Knepley     break;
453cafe43deSMatthew G. Knepley   default:
454cafe43deSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
455cafe43deSMatthew G. Knepley   }
456cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
457cafe43deSMatthew G. Knepley }
458cafe43deSMatthew G. Knepley 
45962a38674SMatthew G. Knepley /*
46062a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
46162a38674SMatthew G. Knepley */
46262a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
46362a38674SMatthew G. Knepley {
46462a38674SMatthew G. Knepley   PetscInt       coneSize;
46562a38674SMatthew G. Knepley   PetscErrorCode ierr;
46662a38674SMatthew G. Knepley 
46762a38674SMatthew G. Knepley   PetscFunctionBegin;
46862a38674SMatthew G. Knepley   switch (dim) {
46962a38674SMatthew G. Knepley   case 2:
47062a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
47162a38674SMatthew G. Knepley     switch (coneSize) {
47262a38674SMatthew G. Knepley     case 3:
47362a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
47462a38674SMatthew G. Knepley       break;
47562a38674SMatthew G. Knepley #if 0
47662a38674SMatthew G. Knepley     case 4:
47762a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
47862a38674SMatthew G. Knepley       break;
47962a38674SMatthew G. Knepley #endif
48062a38674SMatthew G. Knepley     default:
48162a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
48262a38674SMatthew G. Knepley     }
48362a38674SMatthew G. Knepley     break;
48462a38674SMatthew G. Knepley #if 0
48562a38674SMatthew G. Knepley   case 3:
48662a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
48762a38674SMatthew G. Knepley     switch (coneSize) {
48862a38674SMatthew G. Knepley     case 4:
48962a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
49062a38674SMatthew G. Knepley       break;
49162a38674SMatthew G. Knepley     case 6:
49262a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
49362a38674SMatthew G. Knepley       break;
49462a38674SMatthew G. Knepley     default:
49562a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
49662a38674SMatthew G. Knepley     }
49762a38674SMatthew G. Knepley     break;
49862a38674SMatthew G. Knepley #endif
49962a38674SMatthew G. Knepley   default:
50062a38674SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for mesh dimension %D", dim);
50162a38674SMatthew G. Knepley   }
50262a38674SMatthew G. Knepley   PetscFunctionReturn(0);
50362a38674SMatthew G. Knepley }
50462a38674SMatthew G. Knepley 
50562a38674SMatthew G. Knepley /*
50662a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
50762a38674SMatthew G. Knepley 
508d083f849SBarry Smith   Collective on dm
50962a38674SMatthew G. Knepley 
51062a38674SMatthew G. Knepley   Input Parameter:
51162a38674SMatthew G. Knepley . dm - The Plex
51262a38674SMatthew G. Knepley 
51362a38674SMatthew G. Knepley   Output Parameter:
51462a38674SMatthew G. Knepley . localBox - The grid hash object
51562a38674SMatthew G. Knepley 
51662a38674SMatthew G. Knepley   Level: developer
51762a38674SMatthew G. Knepley 
51862a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
51962a38674SMatthew G. Knepley */
520cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
521cafe43deSMatthew G. Knepley {
522cafe43deSMatthew G. Knepley   MPI_Comm           comm;
523cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
524cafe43deSMatthew G. Knepley   Vec                coordinates;
525cafe43deSMatthew G. Knepley   PetscSection       coordSection;
526cafe43deSMatthew G. Knepley   Vec                coordsLocal;
527cafe43deSMatthew G. Knepley   const PetscScalar *coords;
528722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
529cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
5301d0c6c94SMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, cMax, c, i;
531cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
532cafe43deSMatthew G. Knepley 
533cafe43deSMatthew G. Knepley   PetscFunctionBegin;
534cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
535cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
536cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
5375b3353d8SMatthew G. Knepley   if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D");
538cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
539cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
540cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
541cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
542cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
5439cb35068SDave May   ierr = PetscOptionsGetInt(NULL,NULL,"-dm_plex_hash_box_nijk",&n[0],NULL);CHKERRQ(ierr);
5449cb35068SDave May   n[1] = n[0];
5459cb35068SDave May   n[2] = n[0];
546cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
547cafe43deSMatthew G. Knepley #if 0
548cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
549b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
550b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
551cafe43deSMatthew G. Knepley #endif
552cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
553cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
554cafe43deSMatthew G. Knepley   /* Create label */
555cafe43deSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5561d0c6c94SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
5571d0c6c94SMatthew G. Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
558d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "cells", &lbox->cellsSparse);CHKERRQ(ierr);
559cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
560a8d69d7bSBarry Smith   /* Compute boxes which overlap each cell: https://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
561cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
562cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
56338353de4SMatthew G. Knepley   ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr);
564cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
565cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
566cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
56738353de4SMatthew G. Knepley     PetscInt         csize   = 0;
568cafe43deSMatthew G. Knepley     PetscScalar      point[3];
569cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
570cafe43deSMatthew G. Knepley 
571cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
57238353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
57338353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
574722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
57538353de4SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);}
576cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
577cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
5782291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
579cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
580cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
581cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
582cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
583cafe43deSMatthew G. Knepley       }
584cafe43deSMatthew G. Knepley     }
585fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
586cafe43deSMatthew 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]) {
587cafe43deSMatthew 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]) {
588cafe43deSMatthew 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]) {
589cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
590cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
591fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
592cafe43deSMatthew G. Knepley 
593fea14342SMatthew G. Knepley           /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
594cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
595cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
596cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
597cafe43deSMatthew G. Knepley 
598cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
599367003a6SStefano Zampini                 if (cell >= 0) { ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
600cafe43deSMatthew G. Knepley               }
601cafe43deSMatthew G. Knepley             }
602cafe43deSMatthew G. Knepley           }
603fea14342SMatthew G. Knepley           /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */
604fea14342SMatthew G. Knepley           for (edge = 0; edge < dim+1; ++edge) {
605fea14342SMatthew G. Knepley             PetscReal segA[6], segB[6];
606fea14342SMatthew G. Knepley 
607aab5bcd8SJed Brown             if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected dim %d > 3",dim);
608fea14342SMatthew 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]);}
609fea14342SMatthew G. Knepley             for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) {
6109a128ed2SMatthew G. Knepley               if (dim > 2) {segB[2]     = PetscRealPart(point[2]);
6119a128ed2SMatthew G. Knepley                             segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];}
612fea14342SMatthew G. Knepley               for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) {
6139a128ed2SMatthew G. Knepley                 if (dim > 1) {segB[1]     = PetscRealPart(point[1]);
6149a128ed2SMatthew G. Knepley                               segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];}
615fea14342SMatthew G. Knepley                 for (ii = 0; ii < 2; ++ii) {
616fea14342SMatthew G. Knepley                   PetscBool intersects;
617fea14342SMatthew G. Knepley 
6189a128ed2SMatthew G. Knepley                   segB[0]     = PetscRealPart(point[0]);
6199a128ed2SMatthew G. Knepley                   segB[dim+0] = PetscRealPart(point[0]) + ii*h[0];
620fea14342SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
621367003a6SStefano Zampini                   if (intersects) { ierr = DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;}
622cafe43deSMatthew G. Knepley                 }
623cafe43deSMatthew G. Knepley               }
624cafe43deSMatthew G. Knepley             }
625cafe43deSMatthew G. Knepley           }
626fea14342SMatthew G. Knepley         }
627fea14342SMatthew G. Knepley       }
628fea14342SMatthew G. Knepley     }
629fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
630fea14342SMatthew G. Knepley   }
631722d0f5cSMatthew G. Knepley   ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr);
632cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
633cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
634cafe43deSMatthew G. Knepley   *localBox = lbox;
635cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
636cafe43deSMatthew G. Knepley }
637cafe43deSMatthew G. Knepley 
63862a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
639ccd2543fSMatthew G Knepley {
640cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
641af74b616SDave May   PetscBool       hash = mesh->useHashLocation, reuse = PETSC_FALSE;
6423a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
6439cb35068SDave May   PetscInt        dim, cStart, cEnd, cMax, numCells, c, d;
644cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
6453a93e3b7SToby Isaac   PetscSFNode    *cells;
646ccd2543fSMatthew G Knepley   PetscScalar    *a;
6473a93e3b7SToby Isaac   PetscMPIInt     result;
648af74b616SDave May   PetscLogDouble  t0,t1;
6499cb35068SDave May   PetscReal       gmin[3],gmax[3];
6509cb35068SDave May   PetscInt        terminating_query_type[] = { 0, 0, 0 };
651ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
652ccd2543fSMatthew G Knepley 
653ccd2543fSMatthew G Knepley   PetscFunctionBegin;
654af74b616SDave May   ierr = PetscTime(&t0);CHKERRQ(ierr);
655080342d1SMatthew 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.");
656cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
657cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
6583a93e3b7SToby Isaac   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr);
6593a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
660cafe43deSMatthew 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);
661ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
662ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
663ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
664ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
665ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
666ccd2543fSMatthew G Knepley   numPoints /= bs;
667af74b616SDave May   {
668af74b616SDave May     const PetscSFNode *sf_cells;
669af74b616SDave May 
670af74b616SDave May     ierr = PetscSFGetGraph(cellSF,NULL,NULL,NULL,&sf_cells);CHKERRQ(ierr);
671af74b616SDave May     if (sf_cells) {
672af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Re-using existing StarForest node list\n");CHKERRQ(ierr);
673af74b616SDave May       cells = (PetscSFNode*)sf_cells;
674af74b616SDave May       reuse = PETSC_TRUE;
675af74b616SDave May     } else {
676af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n");CHKERRQ(ierr);
677785e854fSJed Brown       ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
678af74b616SDave May       /* initialize cells if created */
679af74b616SDave May       for (p=0; p<numPoints; p++) {
680af74b616SDave May         cells[p].rank  = 0;
681af74b616SDave May         cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
682af74b616SDave May       }
683af74b616SDave May     }
684af74b616SDave May   }
6859cb35068SDave May   /* define domain bounding box */
6869cb35068SDave May   {
6879cb35068SDave May     Vec coorglobal;
6889cb35068SDave May 
6899cb35068SDave May     ierr = DMGetCoordinates(dm,&coorglobal);CHKERRQ(ierr);
6909cb35068SDave May     ierr = VecStrideMaxAll(coorglobal,NULL,gmax);CHKERRQ(ierr);
6919cb35068SDave May     ierr = VecStrideMinAll(coorglobal,NULL,gmin);CHKERRQ(ierr);
6929cb35068SDave May   }
693953fc75cSMatthew G. Knepley   if (hash) {
694ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
695cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
696cafe43deSMatthew G. Knepley     /* Send points to correct process */
697cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
698cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
699cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
700953fc75cSMatthew G. Knepley   }
7013a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
702ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
703e56f9228SJed Brown     PetscInt           dbin[3] = {-1,-1,-1}, bin, cell = -1, cellOffset;
7049cb35068SDave May     PetscBool          point_outside_domain = PETSC_FALSE;
705ccd2543fSMatthew G Knepley 
7069cb35068SDave May     /* check bounding box of domain */
7079cb35068SDave May     for (d=0; d<dim; d++) {
708a5f152d1SDave May       if (PetscRealPart(point[d]) < gmin[d]) { point_outside_domain = PETSC_TRUE; break; }
709a5f152d1SDave May       if (PetscRealPart(point[d]) > gmax[d]) { point_outside_domain = PETSC_TRUE; break; }
7109cb35068SDave May     }
7119cb35068SDave May     if (point_outside_domain) {
712e9b685f5SMatthew G. Knepley       cells[p].rank = 0;
713e9b685f5SMatthew G. Knepley       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
7149cb35068SDave May       terminating_query_type[0]++;
7159cb35068SDave May       continue;
7169cb35068SDave May     }
717ccd2543fSMatthew G Knepley 
718af74b616SDave May     /* check initial values in cells[].index - abort early if found */
719af74b616SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
720af74b616SDave May       c = cells[p].index;
7213a93e3b7SToby Isaac       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
722af74b616SDave May       ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
723af74b616SDave May       if (cell >= 0) {
724af74b616SDave May         cells[p].rank = 0;
725af74b616SDave May         cells[p].index = cell;
726af74b616SDave May         numFound++;
727af74b616SDave May       }
728af74b616SDave May     }
7299cb35068SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
7309cb35068SDave May       terminating_query_type[1]++;
7319cb35068SDave May       continue;
7329cb35068SDave May     }
733af74b616SDave May 
734953fc75cSMatthew G. Knepley     if (hash) {
735af74b616SDave May       PetscBool found_box;
736af74b616SDave May 
737af74b616SDave May       /* allow for case that point is outside box - abort early */
738af74b616SDave May       ierr = PetscGridHashGetEnclosingBoxQuery(mesh->lbox, 1, point, dbin, &bin,&found_box);CHKERRQ(ierr);
739af74b616SDave May       if (found_box) {
740cafe43deSMatthew G. Knepley         /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
741cafe43deSMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
742cafe43deSMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
743cafe43deSMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
744cafe43deSMatthew G. Knepley           ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
7453a93e3b7SToby Isaac           if (cell >= 0) {
7463a93e3b7SToby Isaac             cells[p].rank = 0;
7473a93e3b7SToby Isaac             cells[p].index = cell;
7483a93e3b7SToby Isaac             numFound++;
7499cb35068SDave May             terminating_query_type[2]++;
7503a93e3b7SToby Isaac             break;
751ccd2543fSMatthew G Knepley           }
7523a93e3b7SToby Isaac         }
753af74b616SDave May       }
754953fc75cSMatthew G. Knepley     } else {
755953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
756953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
7573a93e3b7SToby Isaac         if (cell >= 0) {
7583a93e3b7SToby Isaac           cells[p].rank = 0;
7593a93e3b7SToby Isaac           cells[p].index = cell;
7603a93e3b7SToby Isaac           numFound++;
7619cb35068SDave May           terminating_query_type[2]++;
7623a93e3b7SToby Isaac           break;
763953fc75cSMatthew G. Knepley         }
764953fc75cSMatthew G. Knepley       }
7653a93e3b7SToby Isaac     }
766ccd2543fSMatthew G Knepley   }
767953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
76862a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
76962a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
77062a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
77162a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
772e56f9228SJed Brown       PetscInt           dbin[3] = {-1,-1,-1}, bin, cellOffset, d;
77362a38674SMatthew G. Knepley 
774e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
77562a38674SMatthew G. Knepley         ++numFound;
77662a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
77762a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
77862a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
77962a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
78062a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
781b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
78262a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
78362a38674SMatthew G. Knepley           if (dist < distMax) {
78462a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
78562a38674SMatthew G. Knepley             cells[p].rank  = 0;
78662a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
78762a38674SMatthew G. Knepley             distMax = dist;
78862a38674SMatthew G. Knepley             break;
78962a38674SMatthew G. Knepley           }
79062a38674SMatthew G. Knepley         }
79162a38674SMatthew G. Knepley       }
79262a38674SMatthew G. Knepley     }
79362a38674SMatthew G. Knepley   }
79462a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
795cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
7962d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
7973a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
7983a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
7993a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
8003a93e3b7SToby Isaac         if (numFound < p) {
8013a93e3b7SToby Isaac           cells[numFound] = cells[p];
8023a93e3b7SToby Isaac         }
8033a93e3b7SToby Isaac         found[numFound++] = p;
8043a93e3b7SToby Isaac       }
8053a93e3b7SToby Isaac     }
8063a93e3b7SToby Isaac   }
80762a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
808af74b616SDave May   if (!reuse) {
8093a93e3b7SToby Isaac     ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
810af74b616SDave May   }
811af74b616SDave May   ierr = PetscTime(&t1);CHKERRQ(ierr);
8129cb35068SDave May   if (hash) {
813d6e0b8ebSSatish Balay     ierr = PetscInfo3(dm,"[DMLocatePoints_Plex] terminating_query_type : %D [outside domain] : %D [inside intial cell] : %D [hash]\n",terminating_query_type[0],terminating_query_type[1],terminating_query_type[2]);CHKERRQ(ierr);
8149cb35068SDave May   } else {
815d6e0b8ebSSatish Balay     ierr = PetscInfo3(dm,"[DMLocatePoints_Plex] terminating_query_type : %D [outside domain] : %D [inside intial cell] : %D [brute-force]\n",terminating_query_type[0],terminating_query_type[1],terminating_query_type[2]);CHKERRQ(ierr);
8169cb35068SDave May   }
817af74b616SDave 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);
818ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
819ccd2543fSMatthew G Knepley }
820ccd2543fSMatthew G Knepley 
821741bfc07SMatthew G. Knepley /*@C
822741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
823741bfc07SMatthew G. Knepley 
824741bfc07SMatthew G. Knepley   Not collective
825741bfc07SMatthew G. Knepley 
826741bfc07SMatthew G. Knepley   Input Parameter:
827741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
828741bfc07SMatthew G. Knepley 
829741bfc07SMatthew G. Knepley   Output Parameters:
830741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x
831741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
832741bfc07SMatthew G. Knepley 
833741bfc07SMatthew G. Knepley   Level: developer
834741bfc07SMatthew G. Knepley 
835741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
836741bfc07SMatthew G. Knepley @*/
837741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
83817fe8556SMatthew G. Knepley {
83917fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
84017fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
8418b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
84217fe8556SMatthew G. Knepley 
84317fe8556SMatthew G. Knepley   PetscFunctionBegin;
8441c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
8451c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
84617fe8556SMatthew G. Knepley   coords[0] = 0.0;
8477f07f362SMatthew G. Knepley   coords[1] = r;
84817fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
84917fe8556SMatthew G. Knepley }
85017fe8556SMatthew G. Knepley 
851741bfc07SMatthew G. Knepley /*@C
852741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
85328dbe442SToby Isaac 
854741bfc07SMatthew G. Knepley   Not collective
85528dbe442SToby Isaac 
856741bfc07SMatthew G. Knepley   Input Parameter:
857741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
858741bfc07SMatthew G. Knepley 
859741bfc07SMatthew G. Knepley   Output Parameters:
860741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x and z
861741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
862741bfc07SMatthew G. Knepley 
863741bfc07SMatthew 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
864741bfc07SMatthew G. Knepley 
865741bfc07SMatthew G. Knepley   Level: developer
866741bfc07SMatthew G. Knepley 
867741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
868741bfc07SMatthew G. Knepley @*/
869741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
87028dbe442SToby Isaac {
87128dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
87228dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
87328dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
87428dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
87528dbe442SToby Isaac   PetscReal      rinv = 1. / r;
87628dbe442SToby Isaac   PetscFunctionBegin;
87728dbe442SToby Isaac 
87828dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
87928dbe442SToby Isaac   if (x > 0.) {
88028dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
88128dbe442SToby Isaac 
88228dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
88328dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
88428dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
88528dbe442SToby Isaac   }
88628dbe442SToby Isaac   else {
88728dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
88828dbe442SToby Isaac 
88928dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
89028dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
89128dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
89228dbe442SToby Isaac   }
89328dbe442SToby Isaac   coords[0] = 0.0;
89428dbe442SToby Isaac   coords[1] = r;
89528dbe442SToby Isaac   PetscFunctionReturn(0);
89628dbe442SToby Isaac }
89728dbe442SToby Isaac 
898741bfc07SMatthew G. Knepley /*@
899741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto2D - Rewrite coordinates to be the 2D projection of the 3D coordinates
900741bfc07SMatthew G. Knepley 
901741bfc07SMatthew G. Knepley   Not collective
902741bfc07SMatthew G. Knepley 
903741bfc07SMatthew G. Knepley   Input Parameter:
904741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
905741bfc07SMatthew G. Knepley 
906741bfc07SMatthew G. Knepley   Output Parameters:
907741bfc07SMatthew G. Knepley + coords - The new y- and z-coordinates, and 0 for x
908741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
909741bfc07SMatthew G. Knepley 
910741bfc07SMatthew G. Knepley   Level: developer
911741bfc07SMatthew G. Knepley 
912741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
913741bfc07SMatthew G. Knepley @*/
914741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
915ccd2543fSMatthew G Knepley {
9161ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
91799dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
9184a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
919ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
92099dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
921ccd2543fSMatthew G Knepley 
922ccd2543fSMatthew G Knepley   PetscFunctionBegin;
923ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
924ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
9251ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
9261ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
927ccd2543fSMatthew G Knepley   }
928ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
929ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
930ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
9318b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
932ccd2543fSMatthew G Knepley   n[0] /= norm;
933ccd2543fSMatthew G Knepley   n[1] /= norm;
934ccd2543fSMatthew G Knepley   n[2] /= norm;
935ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
936ccd2543fSMatthew G Knepley 
937ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
938ccd2543fSMatthew G Knepley 
939ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
940ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
941ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
942ccd2543fSMatthew G Knepley 
943ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
944ccd2543fSMatthew G Knepley   */
9458b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
94673868372SMatthew G. Knepley   /* Check for n = z */
94773868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
9487df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
9497df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
95099dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
95199dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
9527df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
95373868372SMatthew G. Knepley     }
95499dec3a6SMatthew G. Knepley     coords[0] = 0.0;
95599dec3a6SMatthew G. Knepley     coords[1] = 0.0;
9567df32b8bSSanderA     coords[2] = x1[0];
9577df32b8bSSanderA     coords[3] = x1[1] * s;
9587df32b8bSSanderA     coords[4] = x2[0];
9597df32b8bSSanderA     coords[5] = x2[1] * s;
9607df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
9617df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
9627df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
96373868372SMatthew G. Knepley     PetscFunctionReturn(0);
96473868372SMatthew G. Knepley   }
965da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
966ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
967ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
968ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
969ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
970ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
971ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
972ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
973ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
974ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
975ccd2543fSMatthew G Knepley     }
976ccd2543fSMatthew G Knepley   }
97748120919SToby Isaac   if (PetscAbsReal(x1p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
97848120919SToby Isaac   if (PetscAbsReal(x2p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
979ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
98099dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
98199dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
98299dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
98399dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
98499dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
98599dec3a6SMatthew G. Knepley       }
98699dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
98799dec3a6SMatthew G. Knepley     }
98899dec3a6SMatthew G. Knepley   }
989ccd2543fSMatthew G Knepley   coords[0] = 0.0;
990ccd2543fSMatthew G Knepley   coords[1] = 0.0;
991ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
992ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
993ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
994ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
9957f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
9967f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
9977f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
9987f07f362SMatthew G. Knepley       PetscReal tmp;
9997f07f362SMatthew G. Knepley 
10007f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
10017f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
10027f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
10037f07f362SMatthew G. Knepley     }
10047f07f362SMatthew G. Knepley   }
1005ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1006ccd2543fSMatthew G Knepley }
1007ccd2543fSMatthew G Knepley 
10086322fe33SJed Brown PETSC_UNUSED
1009834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
1010834e62ceSMatthew G. Knepley {
1011834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
1012834e62ceSMatthew G. Knepley 
1013834e62ceSMatthew G. Knepley    |  1  1  1 |
1014834e62ceSMatthew G. Knepley    | x0 x1 x2 |
1015834e62ceSMatthew G. Knepley    | y0 y1 y2 |
1016834e62ceSMatthew G. Knepley 
1017834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
1018834e62ceSMatthew G. Knepley 
1019834e62ceSMatthew G. Knepley    | x1 x2 |
1020834e62ceSMatthew G. Knepley    | y1 y2 |
1021834e62ceSMatthew G. Knepley   */
1022834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
1023834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
1024834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
1025834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
102686623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
1027923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
1028834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
10293bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
1030834e62ceSMatthew G. Knepley }
1031834e62ceSMatthew G. Knepley 
1032834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
1033834e62ceSMatthew G. Knepley {
1034923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
1035834e62ceSMatthew G. Knepley   *vol *= 0.5;
1036834e62ceSMatthew G. Knepley }
1037834e62ceSMatthew G. Knepley 
10386322fe33SJed Brown PETSC_UNUSED
1039834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
1040834e62ceSMatthew G. Knepley {
1041834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
1042834e62ceSMatthew G. Knepley 
1043834e62ceSMatthew G. Knepley    |  1  1  1  1 |
1044834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
1045834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
1046834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
1047834e62ceSMatthew G. Knepley 
1048834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
1049834e62ceSMatthew G. Knepley 
1050834e62ceSMatthew G. Knepley    | x1 x2 x3 |
1051834e62ceSMatthew G. Knepley    | y1 y2 y3 |
1052834e62ceSMatthew G. Knepley    | z1 z2 z3 |
1053834e62ceSMatthew G. Knepley   */
1054834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
1055834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
1056834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
10570a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1058834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
1059834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
1060834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
1061834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
1062923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
10630a3da2c2SToby Isaac   *vol = -onesixth*detM;
10643bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1065834e62ceSMatthew G. Knepley }
1066834e62ceSMatthew G. Knepley 
10670ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
10680ec8681fSMatthew G. Knepley {
10690a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1070923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
10710a3da2c2SToby Isaac   *vol *= -onesixth;
10720ec8681fSMatthew G. Knepley }
10730ec8681fSMatthew G. Knepley 
1074cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1075cb92db44SToby Isaac {
1076cb92db44SToby Isaac   PetscSection   coordSection;
1077cb92db44SToby Isaac   Vec            coordinates;
1078cb92db44SToby Isaac   const PetscScalar *coords;
1079cb92db44SToby Isaac   PetscInt       dim, d, off;
1080cb92db44SToby Isaac   PetscErrorCode ierr;
1081cb92db44SToby Isaac 
1082cb92db44SToby Isaac   PetscFunctionBegin;
1083cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1084cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1085cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
1086cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
1087cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
1088cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
1089cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
1090cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
1091cb92db44SToby Isaac   *detJ = 1.;
1092cb92db44SToby Isaac   if (J) {
1093cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1094cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1095cb92db44SToby Isaac     if (invJ) {
1096cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1097cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1098cb92db44SToby Isaac     }
1099cb92db44SToby Isaac   }
1100cb92db44SToby Isaac   PetscFunctionReturn(0);
1101cb92db44SToby Isaac }
1102cb92db44SToby Isaac 
110317fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
110417fe8556SMatthew G. Knepley {
110517fe8556SMatthew G. Knepley   PetscSection   coordSection;
110617fe8556SMatthew G. Knepley   Vec            coordinates;
1107a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
11088bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
110917fe8556SMatthew G. Knepley   PetscErrorCode ierr;
111017fe8556SMatthew G. Knepley 
111117fe8556SMatthew G. Knepley   PetscFunctionBegin;
111217fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
111369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
11148bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
11158bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
111617fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
11178bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1118adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
11197f07f362SMatthew G. Knepley   *detJ = 0.0;
112028dbe442SToby Isaac   if (numCoords == 6) {
112128dbe442SToby Isaac     const PetscInt dim = 3;
112228dbe442SToby Isaac     PetscReal      R[9], J0;
112328dbe442SToby Isaac 
112428dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1125741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
112628dbe442SToby Isaac     if (J)    {
112728dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
112828dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
112928dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
113028dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
113128dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
113228dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1133adac9986SMatthew G. Knepley     }
113428dbe442SToby Isaac   } else if (numCoords == 4) {
11357f07f362SMatthew G. Knepley     const PetscInt dim = 2;
11367f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
11377f07f362SMatthew G. Knepley 
11387f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1139741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
114017fe8556SMatthew G. Knepley     if (J)    {
11417f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
11427f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
11437f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
1144923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1145923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1146adac9986SMatthew G. Knepley     }
11477f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
11487f07f362SMatthew G. Knepley     const PetscInt dim = 1;
11497f07f362SMatthew G. Knepley 
11507f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
11517f07f362SMatthew G. Knepley     if (J)    {
11527f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
115317fe8556SMatthew G. Knepley       *detJ = J[0];
11543bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
11553bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
1156adac9986SMatthew G. Knepley     }
1157796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
115817fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
115917fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
116017fe8556SMatthew G. Knepley }
116117fe8556SMatthew G. Knepley 
1162ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1163ccd2543fSMatthew G Knepley {
1164ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1165ccd2543fSMatthew G Knepley   Vec            coordinates;
1166a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
116703d7ed2eSStefano Zampini   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1168ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1169ccd2543fSMatthew G Knepley 
1170ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1171ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
117269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
117303d7ed2eSStefano Zampini   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
117403d7ed2eSStefano Zampini   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
1175ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
117603d7ed2eSStefano Zampini   numCoords = numSelfCoords ? numSelfCoords : numCoords;
11777f07f362SMatthew G. Knepley   *detJ = 0.0;
1178ccd2543fSMatthew G Knepley   if (numCoords == 9) {
11797f07f362SMatthew G. Knepley     const PetscInt dim = 3;
11807f07f362SMatthew 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};
11817f07f362SMatthew G. Knepley 
11827f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1183741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
11847f07f362SMatthew G. Knepley     if (J)    {
1185b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1186b7ad821dSMatthew G. Knepley 
1187b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1188b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1189b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1190ccd2543fSMatthew G Knepley         }
11917f07f362SMatthew G. Knepley       }
11923bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1193923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
11947f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
11957f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
11967f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
11977f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
11987f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
11997f07f362SMatthew G. Knepley           }
12007f07f362SMatthew G. Knepley         }
12017f07f362SMatthew G. Knepley       }
12023bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
12037f07f362SMatthew G. Knepley     }
1204923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
12057f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
12067f07f362SMatthew G. Knepley     const PetscInt dim = 2;
12077f07f362SMatthew G. Knepley 
12087f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1209ccd2543fSMatthew G Knepley     if (J)    {
1210ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1211ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1212ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1213ccd2543fSMatthew G Knepley         }
1214ccd2543fSMatthew G Knepley       }
12153bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1216923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1217ccd2543fSMatthew G Knepley     }
1218923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1219796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1220ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1221ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1222ccd2543fSMatthew G Knepley }
1223ccd2543fSMatthew G Knepley 
1224dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1225ccd2543fSMatthew G Knepley {
1226ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1227ccd2543fSMatthew G Knepley   Vec            coordinates;
1228a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
12290d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1230ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1231ccd2543fSMatthew G Knepley 
1232ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1233ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
123469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
12350d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
12360d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
123799dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
123871f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1239dfccc68fSToby Isaac   if (!Nq) {
12407f07f362SMatthew G. Knepley     *detJ = 0.0;
124199dec3a6SMatthew G. Knepley     if (numCoords == 12) {
124299dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
124399dec3a6SMatthew 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};
124499dec3a6SMatthew G. Knepley 
1245dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1246741bfc07SMatthew G. Knepley       ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
124799dec3a6SMatthew G. Knepley       if (J)    {
124899dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
124999dec3a6SMatthew G. Knepley 
125099dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
125199dec3a6SMatthew G. Knepley           J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
12520892ce5fSStefano Zampini           J0[d*dim+1] = 0.5*(PetscRealPart(coords[2*pdim+d]) - PetscRealPart(coords[1*pdim+d]));
125399dec3a6SMatthew G. Knepley         }
12543bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1255923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
125699dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
125799dec3a6SMatthew G. Knepley           for (f = 0; f < dim; f++) {
125899dec3a6SMatthew G. Knepley             J[d*dim+f] = 0.0;
125999dec3a6SMatthew G. Knepley             for (g = 0; g < dim; g++) {
126099dec3a6SMatthew G. Knepley               J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
126199dec3a6SMatthew G. Knepley             }
126299dec3a6SMatthew G. Knepley           }
126399dec3a6SMatthew G. Knepley         }
12643bc0b13bSBarry Smith         ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
126599dec3a6SMatthew G. Knepley       }
1266923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
126771f58de1SToby Isaac     } else if (numCoords == 8) {
126899dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
126999dec3a6SMatthew G. Knepley 
1270dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1271ccd2543fSMatthew G Knepley       if (J)    {
1272ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
127399dec3a6SMatthew G. Knepley           J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
127499dec3a6SMatthew G. Knepley           J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1275ccd2543fSMatthew G Knepley         }
12763bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1277923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1278ccd2543fSMatthew G Knepley       }
1279923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1280796f034aSJed Brown     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1281dfccc68fSToby Isaac   } else {
1282dfccc68fSToby Isaac     const PetscInt Nv = 4;
1283dfccc68fSToby Isaac     const PetscInt dimR = 2;
1284dfccc68fSToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
1285dfccc68fSToby Isaac     PetscReal zOrder[12];
1286dfccc68fSToby Isaac     PetscReal zCoeff[12];
1287dfccc68fSToby Isaac     PetscInt  i, j, k, l, dim;
1288dfccc68fSToby Isaac 
1289dfccc68fSToby Isaac     if (numCoords == 12) {
1290dfccc68fSToby Isaac       dim = 3;
1291dfccc68fSToby Isaac     } else if (numCoords == 8) {
1292dfccc68fSToby Isaac       dim = 2;
1293dfccc68fSToby Isaac     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1294dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1295dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1296dfccc68fSToby Isaac 
1297dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1298dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1299dfccc68fSToby Isaac       }
1300dfccc68fSToby Isaac     }
1301dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1302dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1303dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1304dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1305dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1306dfccc68fSToby Isaac     }
1307dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1308dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1309dfccc68fSToby Isaac 
1310dfccc68fSToby Isaac       if (v) {
1311dfccc68fSToby Isaac         PetscReal extPoint[4];
1312dfccc68fSToby Isaac 
1313dfccc68fSToby Isaac         extPoint[0] = 1.;
1314dfccc68fSToby Isaac         extPoint[1] = xi;
1315dfccc68fSToby Isaac         extPoint[2] = eta;
1316dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1317dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1318dfccc68fSToby Isaac           PetscReal val = 0.;
1319dfccc68fSToby Isaac 
1320dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1321dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1322dfccc68fSToby Isaac           }
1323dfccc68fSToby Isaac           v[i * dim + j] = val;
1324dfccc68fSToby Isaac         }
1325dfccc68fSToby Isaac       }
1326dfccc68fSToby Isaac       if (J) {
1327dfccc68fSToby Isaac         PetscReal extJ[8];
1328dfccc68fSToby Isaac 
1329dfccc68fSToby Isaac         extJ[0] = 0.;
1330dfccc68fSToby Isaac         extJ[1] = 0.;
1331dfccc68fSToby Isaac         extJ[2] = 1.;
1332dfccc68fSToby Isaac         extJ[3] = 0.;
1333dfccc68fSToby Isaac         extJ[4] = 0.;
1334dfccc68fSToby Isaac         extJ[5] = 1.;
1335dfccc68fSToby Isaac         extJ[6] = eta;
1336dfccc68fSToby Isaac         extJ[7] = xi;
1337dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1338dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1339dfccc68fSToby Isaac             PetscReal val = 0.;
1340dfccc68fSToby Isaac 
1341dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1342dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1343dfccc68fSToby Isaac             }
1344dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1345dfccc68fSToby Isaac           }
1346dfccc68fSToby Isaac         }
1347dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1348dfccc68fSToby Isaac           PetscReal x, y, z;
1349dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1350dfccc68fSToby Isaac           PetscReal norm;
1351dfccc68fSToby Isaac 
1352dfccc68fSToby Isaac           x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1353dfccc68fSToby Isaac           y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1354dfccc68fSToby Isaac           z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1355dfccc68fSToby Isaac           norm = PetscSqrtReal(x * x + y * y + z * z);
1356dfccc68fSToby Isaac           iJ[2] = x / norm;
1357dfccc68fSToby Isaac           iJ[5] = y / norm;
1358dfccc68fSToby Isaac           iJ[8] = z / norm;
1359dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1360dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1361dfccc68fSToby Isaac         } else {
1362dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1363dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1364dfccc68fSToby Isaac         }
1365dfccc68fSToby Isaac       }
1366dfccc68fSToby Isaac     }
1367dfccc68fSToby Isaac   }
136899dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1369ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1370ccd2543fSMatthew G Knepley }
1371ccd2543fSMatthew G Knepley 
1372ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1373ccd2543fSMatthew G Knepley {
1374ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1375ccd2543fSMatthew G Knepley   Vec            coordinates;
1376a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1377ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
137899dec3a6SMatthew G. Knepley   PetscInt       d;
1379ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1380ccd2543fSMatthew G Knepley 
1381ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1382ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
138369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1384ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
13857f07f362SMatthew G. Knepley   *detJ = 0.0;
13867f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1387ccd2543fSMatthew G Knepley   if (J)    {
1388ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1389f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1390f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1391f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1392f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1393ccd2543fSMatthew G Knepley     }
13943bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1395923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1396ccd2543fSMatthew G Knepley   }
1397923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1398ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1399ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1400ccd2543fSMatthew G Knepley }
1401ccd2543fSMatthew G Knepley 
1402dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1403ccd2543fSMatthew G Knepley {
1404ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1405ccd2543fSMatthew G Knepley   Vec            coordinates;
1406a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1407ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1408ccd2543fSMatthew G Knepley   PetscInt       d;
1409ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1410ccd2543fSMatthew G Knepley 
1411ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1412ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
141369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1414ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1415dfccc68fSToby Isaac   if (!Nq) {
14167f07f362SMatthew G. Knepley     *detJ = 0.0;
1417dfccc68fSToby Isaac     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1418ccd2543fSMatthew G Knepley     if (J)    {
1419ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1420f0df753eSMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1421f0df753eSMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1422f0df753eSMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1423ccd2543fSMatthew G Knepley       }
14243bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1425923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1426ccd2543fSMatthew G Knepley     }
1427923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1428dfccc68fSToby Isaac   } else {
1429dfccc68fSToby Isaac     const PetscInt Nv = 8;
1430dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1431dfccc68fSToby Isaac     const PetscInt dim = 3;
1432dfccc68fSToby Isaac     const PetscInt dimR = 3;
1433dfccc68fSToby Isaac     PetscReal zOrder[24];
1434dfccc68fSToby Isaac     PetscReal zCoeff[24];
1435dfccc68fSToby Isaac     PetscInt  i, j, k, l;
1436dfccc68fSToby Isaac 
1437dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1438dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1439dfccc68fSToby Isaac 
1440dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1441dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1442dfccc68fSToby Isaac       }
1443dfccc68fSToby Isaac     }
1444dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1445dfccc68fSToby 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]);
1446dfccc68fSToby 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]);
1447dfccc68fSToby 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]);
1448dfccc68fSToby 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]);
1449dfccc68fSToby 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]);
1450dfccc68fSToby 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]);
1451dfccc68fSToby 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]);
1452dfccc68fSToby 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]);
1453dfccc68fSToby Isaac     }
1454dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1455dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1456dfccc68fSToby Isaac 
1457dfccc68fSToby Isaac       if (v) {
145891d2b7ceSToby Isaac         PetscReal extPoint[8];
1459dfccc68fSToby Isaac 
1460dfccc68fSToby Isaac         extPoint[0] = 1.;
1461dfccc68fSToby Isaac         extPoint[1] = xi;
1462dfccc68fSToby Isaac         extPoint[2] = eta;
1463dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1464dfccc68fSToby Isaac         extPoint[4] = theta;
1465dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1466dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1467dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1468dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1469dfccc68fSToby Isaac           PetscReal val = 0.;
1470dfccc68fSToby Isaac 
1471dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1472dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1473dfccc68fSToby Isaac           }
1474dfccc68fSToby Isaac           v[i * dim + j] = val;
1475dfccc68fSToby Isaac         }
1476dfccc68fSToby Isaac       }
1477dfccc68fSToby Isaac       if (J) {
1478dfccc68fSToby Isaac         PetscReal extJ[24];
1479dfccc68fSToby Isaac 
1480dfccc68fSToby Isaac         extJ[0]  = 0.         ; extJ[1]  = 0.        ; extJ[2]  = 0.      ;
1481dfccc68fSToby Isaac         extJ[3]  = 1.         ; extJ[4]  = 0.        ; extJ[5]  = 0.      ;
1482dfccc68fSToby Isaac         extJ[6]  = 0.         ; extJ[7]  = 1.        ; extJ[8]  = 0.      ;
1483dfccc68fSToby Isaac         extJ[9]  = eta        ; extJ[10] = xi        ; extJ[11] = 0.      ;
1484dfccc68fSToby Isaac         extJ[12] = 0.         ; extJ[13] = 0.        ; extJ[14] = 1.      ;
1485dfccc68fSToby Isaac         extJ[15] = theta      ; extJ[16] = 0.        ; extJ[17] = xi      ;
1486dfccc68fSToby Isaac         extJ[18] = 0.         ; extJ[19] = theta     ; extJ[20] = eta     ;
1487dfccc68fSToby Isaac         extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi;
1488dfccc68fSToby Isaac 
1489dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1490dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1491dfccc68fSToby Isaac             PetscReal val = 0.;
1492dfccc68fSToby Isaac 
1493dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1494dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1495dfccc68fSToby Isaac             }
1496dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1497dfccc68fSToby Isaac           }
1498dfccc68fSToby Isaac         }
1499dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1500dfccc68fSToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1501dfccc68fSToby Isaac       }
1502dfccc68fSToby Isaac     }
1503dfccc68fSToby Isaac   }
1504ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1505ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1506ccd2543fSMatthew G Knepley }
1507ccd2543fSMatthew G Knepley 
1508dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1509dfccc68fSToby Isaac {
1510dfccc68fSToby Isaac   PetscInt        depth, dim, coordDim, coneSize, i;
1511dfccc68fSToby Isaac   PetscInt        Nq = 0;
1512dfccc68fSToby Isaac   const PetscReal *points = NULL;
1513dfccc68fSToby Isaac   DMLabel         depthLabel;
1514c330f8ffSToby Isaac   PetscReal       xi0[3] = {-1.,-1.,-1.}, v0[3], J0[9], detJ0;
1515dfccc68fSToby Isaac   PetscBool       isAffine = PETSC_TRUE;
1516dfccc68fSToby Isaac   PetscErrorCode  ierr;
1517dfccc68fSToby Isaac 
1518dfccc68fSToby Isaac   PetscFunctionBegin;
1519dfccc68fSToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1520dfccc68fSToby Isaac   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1521dfccc68fSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1522dfccc68fSToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1523dfccc68fSToby Isaac   if (depth == 1 && dim == 1) {
1524dfccc68fSToby Isaac     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1525dfccc68fSToby Isaac   }
1526dfccc68fSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1527dfccc68fSToby Isaac   if (coordDim > 3) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim);
15289c3cf19fSMatthew G. Knepley   if (quad) {ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL);CHKERRQ(ierr);}
1529dfccc68fSToby Isaac   switch (dim) {
1530dfccc68fSToby Isaac   case 0:
1531dfccc68fSToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1532dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1533dfccc68fSToby Isaac     break;
1534dfccc68fSToby Isaac   case 1:
15357318780aSToby Isaac     if (Nq) {
15367318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
15377318780aSToby Isaac     } else {
15387318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
15397318780aSToby Isaac     }
1540dfccc68fSToby Isaac     break;
1541dfccc68fSToby Isaac   case 2:
1542dfccc68fSToby Isaac     switch (coneSize) {
1543dfccc68fSToby Isaac     case 3:
15447318780aSToby Isaac       if (Nq) {
15457318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
15467318780aSToby Isaac       } else {
15477318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
15487318780aSToby Isaac       }
1549dfccc68fSToby Isaac       break;
1550dfccc68fSToby Isaac     case 4:
1551dfccc68fSToby Isaac       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1552dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1553dfccc68fSToby Isaac       break;
1554dfccc68fSToby Isaac     default:
1555dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1556dfccc68fSToby Isaac     }
1557dfccc68fSToby Isaac     break;
1558dfccc68fSToby Isaac   case 3:
1559dfccc68fSToby Isaac     switch (coneSize) {
1560dfccc68fSToby Isaac     case 4:
15617318780aSToby Isaac       if (Nq) {
15627318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
15637318780aSToby Isaac       } else {
15647318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
15657318780aSToby Isaac       }
1566dfccc68fSToby Isaac       break;
1567dfccc68fSToby Isaac     case 6: /* Faces */
1568dfccc68fSToby Isaac     case 8: /* Vertices */
1569dfccc68fSToby Isaac       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1570dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1571dfccc68fSToby Isaac       break;
1572dfccc68fSToby Isaac     default:
1573dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1574dfccc68fSToby Isaac     }
1575dfccc68fSToby Isaac     break;
1576dfccc68fSToby Isaac   default:
1577dfccc68fSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1578dfccc68fSToby Isaac   }
15797318780aSToby Isaac   if (isAffine && Nq) {
1580dfccc68fSToby Isaac     if (v) {
1581dfccc68fSToby Isaac       for (i = 0; i < Nq; i++) {
1582c330f8ffSToby Isaac         CoordinatesRefToReal(coordDim, dim, xi0, v0, J0, &points[dim * i], &v[coordDim * i]);
1583dfccc68fSToby Isaac       }
1584dfccc68fSToby Isaac     }
15857318780aSToby Isaac     if (detJ) {
15867318780aSToby Isaac       for (i = 0; i < Nq; i++) {
15877318780aSToby Isaac         detJ[i] = detJ0;
1588dfccc68fSToby Isaac       }
15897318780aSToby Isaac     }
15907318780aSToby Isaac     if (J) {
15917318780aSToby Isaac       PetscInt k;
15927318780aSToby Isaac 
15937318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1594dfccc68fSToby Isaac         PetscInt j;
1595dfccc68fSToby Isaac 
15967318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
15977318780aSToby Isaac           J[k] = J0[j];
15987318780aSToby Isaac         }
15997318780aSToby Isaac       }
16007318780aSToby Isaac     }
16017318780aSToby Isaac     if (invJ) {
16027318780aSToby Isaac       PetscInt k;
16037318780aSToby Isaac       switch (coordDim) {
16047318780aSToby Isaac       case 0:
16057318780aSToby Isaac         break;
16067318780aSToby Isaac       case 1:
16077318780aSToby Isaac         invJ[0] = 1./J0[0];
16087318780aSToby Isaac         break;
16097318780aSToby Isaac       case 2:
16107318780aSToby Isaac         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
16117318780aSToby Isaac         break;
16127318780aSToby Isaac       case 3:
16137318780aSToby Isaac         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
16147318780aSToby Isaac         break;
16157318780aSToby Isaac       }
16167318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
16177318780aSToby Isaac         PetscInt j;
16187318780aSToby Isaac 
16197318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
16207318780aSToby Isaac           invJ[k] = invJ[j];
16217318780aSToby Isaac         }
1622dfccc68fSToby Isaac       }
1623dfccc68fSToby Isaac     }
1624dfccc68fSToby Isaac   }
1625dfccc68fSToby Isaac   PetscFunctionReturn(0);
1626dfccc68fSToby Isaac }
1627dfccc68fSToby Isaac 
1628ccd2543fSMatthew G Knepley /*@C
16298e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1630ccd2543fSMatthew G Knepley 
1631d083f849SBarry Smith   Collective on dm
1632ccd2543fSMatthew G Knepley 
1633ccd2543fSMatthew G Knepley   Input Arguments:
1634ccd2543fSMatthew G Knepley + dm   - the DM
1635ccd2543fSMatthew G Knepley - cell - the cell
1636ccd2543fSMatthew G Knepley 
1637ccd2543fSMatthew G Knepley   Output Arguments:
1638ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1639ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1640ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1641ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1642ccd2543fSMatthew G Knepley 
1643ccd2543fSMatthew G Knepley   Level: advanced
1644ccd2543fSMatthew G Knepley 
1645ccd2543fSMatthew G Knepley   Fortran Notes:
1646ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1647ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1648ccd2543fSMatthew G Knepley 
1649e8964c0aSStefano Zampini .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinates()
1650ccd2543fSMatthew G Knepley @*/
16518e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1652ccd2543fSMatthew G Knepley {
1653ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1654ccd2543fSMatthew G Knepley 
1655ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1656dfccc68fSToby Isaac   ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr);
16578e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
16588e0841e0SMatthew G. Knepley }
16598e0841e0SMatthew G. Knepley 
1660dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
16618e0841e0SMatthew G. Knepley {
1662dfccc68fSToby Isaac   PetscQuadrature  feQuad;
16638e0841e0SMatthew G. Knepley   PetscSection     coordSection;
16648e0841e0SMatthew G. Knepley   Vec              coordinates;
16658e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
16668e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
1667f960e424SToby Isaac   PetscReal       *basisDer, *basis, detJt;
1668f960e424SToby Isaac   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, q;
16698e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
16708e0841e0SMatthew G. Knepley 
16718e0841e0SMatthew G. Knepley   PetscFunctionBegin;
16728e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
16738e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
16748e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
16758e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16768e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1677dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
1678dfccc68fSToby Isaac     PetscDualSpace dsp;
1679dfccc68fSToby Isaac 
1680dfccc68fSToby Isaac     ierr = PetscFEGetDualSpace(fe, &dsp);CHKERRQ(ierr);
1681dfccc68fSToby Isaac     ierr = PetscDualSpaceGetFunctional(dsp, 0, &quad);CHKERRQ(ierr);
16829c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1683dfccc68fSToby Isaac     Nq = 1;
1684dfccc68fSToby Isaac   } else {
16859c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1686dfccc68fSToby Isaac   }
168791d2b7ceSToby Isaac   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
1688dfccc68fSToby Isaac   ierr = PetscFEGetQuadrature(fe, &feQuad);CHKERRQ(ierr);
1689dfccc68fSToby Isaac   if (feQuad == quad) {
16908135c375SStefano Zampini     ierr = PetscFEGetDefaultTabulation(fe, &basis, J ? &basisDer : NULL, NULL);CHKERRQ(ierr);
16918e0841e0SMatthew 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);
1692dfccc68fSToby Isaac   } else {
16938135c375SStefano Zampini     ierr = PetscFEGetTabulation(fe, Nq, quadPoints, &basis, J ? &basisDer : NULL, NULL);CHKERRQ(ierr);
1694dfccc68fSToby Isaac   }
1695dfccc68fSToby Isaac   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
1696dfccc68fSToby Isaac   if (v) {
1697580bdb30SBarry Smith     ierr = PetscArrayzero(v, Nq*cdim);CHKERRQ(ierr);
1698f960e424SToby Isaac     for (q = 0; q < Nq; ++q) {
1699f960e424SToby Isaac       PetscInt i, k;
1700f960e424SToby Isaac 
1701f960e424SToby Isaac       for (k = 0; k < pdim; ++k)
1702f960e424SToby Isaac         for (i = 0; i < cdim; ++i)
1703dfccc68fSToby Isaac           v[q*cdim + i] += basis[q*pdim + k] * PetscRealPart(coords[k*cdim + i]);
1704f960e424SToby Isaac       ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1705f960e424SToby Isaac     }
1706f960e424SToby Isaac   }
17078e0841e0SMatthew G. Knepley   if (J) {
1708580bdb30SBarry Smith     ierr = PetscArrayzero(J, Nq*cdim*cdim);CHKERRQ(ierr);
17098e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
17108e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
17118e0841e0SMatthew G. Knepley 
17128e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
17138e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
17148e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
17158e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
1716dfccc68fSToby Isaac             J[(q*cdim + i)*cdim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
17173bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
17188e0841e0SMatthew G. Knepley       if (cdim > dim) {
17198e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
17208e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
17218e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
17228e0841e0SMatthew G. Knepley       }
1723f960e424SToby Isaac       if (!detJ && !invJ) continue;
1724a63b72c6SToby Isaac       detJt = 0.;
17258e0841e0SMatthew G. Knepley       switch (cdim) {
17268e0841e0SMatthew G. Knepley       case 3:
1727037dc194SToby Isaac         DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]);
1728037dc194SToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
172917fe8556SMatthew G. Knepley         break;
173049dc4407SMatthew G. Knepley       case 2:
17319f328543SToby Isaac         DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]);
1732037dc194SToby Isaac         if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
173349dc4407SMatthew G. Knepley         break;
17348e0841e0SMatthew G. Knepley       case 1:
1735037dc194SToby Isaac         detJt = J[q*cdim*dim];
1736037dc194SToby Isaac         if (invJ) invJ[q*cdim*dim] = 1.0/detJt;
173749dc4407SMatthew G. Knepley       }
1738f960e424SToby Isaac       if (detJ) detJ[q] = detJt;
173949dc4407SMatthew G. Knepley     }
174049dc4407SMatthew G. Knepley   }
1741037dc194SToby Isaac   else if (detJ || invJ) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
1742dfccc68fSToby Isaac   if (feQuad != quad) {
17438135c375SStefano Zampini     ierr = PetscFERestoreTabulation(fe, Nq, quadPoints, &basis, J ? &basisDer : NULL, NULL);CHKERRQ(ierr);
1744dfccc68fSToby Isaac   }
17458e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
17468e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
17478e0841e0SMatthew G. Knepley }
17488e0841e0SMatthew G. Knepley 
17498e0841e0SMatthew G. Knepley /*@C
17508e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
17518e0841e0SMatthew G. Knepley 
1752d083f849SBarry Smith   Collective on dm
17538e0841e0SMatthew G. Knepley 
17548e0841e0SMatthew G. Knepley   Input Arguments:
17558e0841e0SMatthew G. Knepley + dm   - the DM
17568e0841e0SMatthew G. Knepley . cell - the cell
1757dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
1758dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
17598e0841e0SMatthew G. Knepley 
17608e0841e0SMatthew G. Knepley   Output Arguments:
1761dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
17628e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
17638e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
17648e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
17658e0841e0SMatthew G. Knepley 
17668e0841e0SMatthew G. Knepley   Level: advanced
17678e0841e0SMatthew G. Knepley 
17688e0841e0SMatthew G. Knepley   Fortran Notes:
17698e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
17708e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
17718e0841e0SMatthew G. Knepley 
1772e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
17738e0841e0SMatthew G. Knepley @*/
1774dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
17758e0841e0SMatthew G. Knepley {
1776bb4a5db5SMatthew G. Knepley   DM             cdm;
1777dfccc68fSToby Isaac   PetscFE        fe = NULL;
17788e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
17798e0841e0SMatthew G. Knepley 
17808e0841e0SMatthew G. Knepley   PetscFunctionBegin;
17812d89661fSMatthew G. Knepley   PetscValidPointer(detJ, 7);
1782bb4a5db5SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1783bb4a5db5SMatthew G. Knepley   if (cdm) {
1784dfccc68fSToby Isaac     PetscClassId id;
1785dfccc68fSToby Isaac     PetscInt     numFields;
1786e5e52638SMatthew G. Knepley     PetscDS      prob;
1787dfccc68fSToby Isaac     PetscObject  disc;
1788dfccc68fSToby Isaac 
1789bb4a5db5SMatthew G. Knepley     ierr = DMGetNumFields(cdm, &numFields);CHKERRQ(ierr);
1790dfccc68fSToby Isaac     if (numFields) {
1791bb4a5db5SMatthew G. Knepley       ierr = DMGetDS(cdm, &prob);CHKERRQ(ierr);
1792dfccc68fSToby Isaac       ierr = PetscDSGetDiscretization(prob,0,&disc);CHKERRQ(ierr);
1793dfccc68fSToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1794dfccc68fSToby Isaac       if (id == PETSCFE_CLASSID) {
1795dfccc68fSToby Isaac         fe = (PetscFE) disc;
1796dfccc68fSToby Isaac       }
1797dfccc68fSToby Isaac     }
1798dfccc68fSToby Isaac   }
1799dfccc68fSToby Isaac   if (!fe) {ierr = DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1800dfccc68fSToby Isaac   else     {ierr = DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1801ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1802ccd2543fSMatthew G Knepley }
1803834e62ceSMatthew G. Knepley 
1804011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1805cc08537eSMatthew G. Knepley {
1806cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1807cc08537eSMatthew G. Knepley   Vec            coordinates;
1808a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
180906e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1810714b99b6SMatthew G. Knepley   PetscInt       coordSize, d;
1811cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1812cc08537eSMatthew G. Knepley 
1813cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1814cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
181569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1816cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
18172e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1818cc08537eSMatthew G. Knepley   if (centroid) {
1819714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) centroid[d] = 0.5*PetscRealPart(coords[d] + tmp[d]);
1820cc08537eSMatthew G. Knepley   }
1821cc08537eSMatthew G. Knepley   if (normal) {
1822a60a936bSMatthew G. Knepley     PetscReal norm;
1823a60a936bSMatthew G. Knepley 
1824714b99b6SMatthew G. Knepley     if (dim != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "We only support 2D edges right now");
182506e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
182606e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1827714b99b6SMatthew G. Knepley     norm       = DMPlex_NormD_Internal(dim, normal);
1828714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) normal[d] /= norm;
1829cc08537eSMatthew G. Knepley   }
1830cc08537eSMatthew G. Knepley   if (vol) {
1831714b99b6SMatthew G. Knepley     *vol = 0.0;
1832714b99b6SMatthew G. Knepley     for (d = 0; d < dim; ++d) *vol += PetscSqr(PetscRealPart(coords[d] - tmp[d]));
1833714b99b6SMatthew G. Knepley     *vol = PetscSqrtReal(*vol);
1834cc08537eSMatthew G. Knepley   }
1835cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1836cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1837cc08537eSMatthew G. Knepley }
1838cc08537eSMatthew G. Knepley 
1839cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1840011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1841cc08537eSMatthew G. Knepley {
1842793a2a13SMatthew G. Knepley   DMLabel        depth;
1843cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1844cc08537eSMatthew G. Knepley   Vec            coordinates;
1845cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
18460a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
1847793a2a13SMatthew G. Knepley   PetscBool      isHybrid = PETSC_FALSE;
1848793a2a13SMatthew G. Knepley   PetscInt       fv[4] = {0, 1, 2, 3};
1849*d80ece95SMatthew G. Knepley   PetscInt       pEndInterior[4], cdepth, tdim = 2, coordSize, numCorners, p, d, e;
1850cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1851cc08537eSMatthew G. Knepley 
1852cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1853793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
1854793a2a13SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
1855793a2a13SMatthew G. Knepley   ierr = DMLabelGetValue(depth, cell, &cdepth);CHKERRQ(ierr);
1856*d80ece95SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pEndInterior[3], &pEndInterior[2], &pEndInterior[1], &pEndInterior[0]);CHKERRQ(ierr);
1857*d80ece95SMatthew G. Knepley   if ((pEndInterior[cdepth]) >=0 && (cell >= pEndInterior[cdepth])) isHybrid = PETSC_TRUE;
1858cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
18590a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
186069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1861cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
18620bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1863793a2a13SMatthew G. Knepley   /* Side faces for hybrid cells are are stored as tensor products */
1864793a2a13SMatthew G. Knepley   if (isHybrid && numCorners == 4) {fv[2] = 3; fv[3] = 2;}
1865793a2a13SMatthew G. Knepley 
1866ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1867ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1868ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1869ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1870ceee4971SMatthew G. Knepley   }
1871011ea5d8SMatthew G. Knepley   if (normal) {
1872011ea5d8SMatthew G. Knepley     if (dim > 2) {
1873793a2a13SMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim*fv[1]+0] - coords[0]), x1 = PetscRealPart(coords[dim*fv[2]+0] - coords[0]);
1874793a2a13SMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim*fv[1]+1] - coords[1]), y1 = PetscRealPart(coords[dim*fv[2]+1] - coords[1]);
1875793a2a13SMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim*fv[1]+2] - coords[2]), z1 = PetscRealPart(coords[dim*fv[2]+2] - coords[2]);
18760a1d6728SMatthew G. Knepley       PetscReal       norm;
18770a1d6728SMatthew G. Knepley 
18780a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
18790a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
18800a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
18818b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
18820a1d6728SMatthew G. Knepley       normal[0] /= norm;
18830a1d6728SMatthew G. Knepley       normal[1] /= norm;
18840a1d6728SMatthew G. Knepley       normal[2] /= norm;
1885011ea5d8SMatthew G. Knepley     } else {
1886011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1887011ea5d8SMatthew G. Knepley     }
1888011ea5d8SMatthew G. Knepley   }
1889741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
18900a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
1891793a2a13SMatthew G. Knepley     const PetscInt pi  = p < 4 ? fv[p] : p;
1892793a2a13SMatthew G. Knepley     const PetscInt pin = p < 3 ? fv[(p+1)%numCorners] : (p+1)%numCorners;
18930a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
18940a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
1895793a2a13SMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[pi*tdim+d]);
1896793a2a13SMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[pin*tdim+d]);
18970a1d6728SMatthew G. Knepley     }
18980a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
18990a1d6728SMatthew G. Knepley     vsum += vtmp;
19000a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
19010a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
19020a1d6728SMatthew G. Knepley     }
19030a1d6728SMatthew G. Knepley   }
19040a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
19050a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
19060a1d6728SMatthew G. Knepley   }
19070a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1908ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
19090a1d6728SMatthew G. Knepley   if (centroid) {
19100a1d6728SMatthew G. Knepley     if (dim > 2) {
19110a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
19120a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
19130a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
19140a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
19150a1d6728SMatthew G. Knepley         }
19160a1d6728SMatthew G. Knepley       }
19170a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
19180a1d6728SMatthew G. Knepley   }
1919cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1920cc08537eSMatthew G. Knepley }
1921cc08537eSMatthew G. Knepley 
19220ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1923011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
19240ec8681fSMatthew G. Knepley {
1925793a2a13SMatthew G. Knepley   DMLabel         depth;
19260ec8681fSMatthew G. Knepley   PetscSection    coordSection;
19270ec8681fSMatthew G. Knepley   Vec             coordinates;
19280ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
192986623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1930a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
1931793a2a13SMatthew G. Knepley   PetscBool       isHybrid = PETSC_FALSE;
1932793a2a13SMatthew G. Knepley   PetscInt        pEndInterior[4], cdepth, numFaces, f, coordSize, numCorners, p, d;
19330ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
19340ec8681fSMatthew G. Knepley 
19350ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1936f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
1937793a2a13SMatthew G. Knepley   /* Must check for hybrid cells because prisms have a different orientation scheme */
1938793a2a13SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
1939793a2a13SMatthew G. Knepley   ierr = DMLabelGetValue(depth, cell, &cdepth);CHKERRQ(ierr);
19402b2dc32bSSatish Balay   ierr = DMPlexGetHybridBounds(dm, &pEndInterior[3], &pEndInterior[2], &pEndInterior[1], &pEndInterior[0]);CHKERRQ(ierr);
1941793a2a13SMatthew G. Knepley   if ((pEndInterior[cdepth]) >=0 && (cell >= pEndInterior[cdepth])) isHybrid = PETSC_TRUE;
1942793a2a13SMatthew G. Knepley 
19430ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
194469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
19450ec8681fSMatthew G. Knepley 
1946d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
19470ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
19480ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1949a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
19500ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1951793a2a13SMatthew G. Knepley     PetscBool flip = isHybrid && f == 0 ? PETSC_TRUE : PETSC_FALSE; /* The first hybrid face is reversed */
1952793a2a13SMatthew G. Knepley 
1953011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
19540ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
19550ec8681fSMatthew G. Knepley     switch (numCorners) {
19560ec8681fSMatthew G. Knepley     case 3:
19570ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
19581ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
19591ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
19601ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
19610ec8681fSMatthew G. Knepley       }
19620ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1963793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
19640ec8681fSMatthew G. Knepley       vsum += vtmp;
19654f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
19660ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19671ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
19680ec8681fSMatthew G. Knepley         }
19690ec8681fSMatthew G. Knepley       }
19700ec8681fSMatthew G. Knepley       break;
19710ec8681fSMatthew G. Knepley     case 4:
1972793a2a13SMatthew G. Knepley     {
1973793a2a13SMatthew G. Knepley       PetscInt fv[4] = {0, 1, 2, 3};
1974793a2a13SMatthew G. Knepley 
1975793a2a13SMatthew G. Knepley       /* Side faces for hybrid cells are are stored as tensor products */
1976793a2a13SMatthew G. Knepley       if (isHybrid && f > 1) {fv[2] = 3; fv[3] = 2;}
19770ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
19780ec8681fSMatthew G. Knepley       /* First tet */
19790ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
1980793a2a13SMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[0]*dim+d]);
1981793a2a13SMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[1]*dim+d]);
1982793a2a13SMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]);
19830ec8681fSMatthew G. Knepley       }
19840ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1985793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
19860ec8681fSMatthew G. Knepley       vsum += vtmp;
19870ec8681fSMatthew G. Knepley       if (centroid) {
19880ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19890ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
19900ec8681fSMatthew G. Knepley         }
19910ec8681fSMatthew G. Knepley       }
19920ec8681fSMatthew G. Knepley       /* Second tet */
19930ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
1994793a2a13SMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[fv[1]*dim+d]);
1995793a2a13SMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[fv[2]*dim+d]);
1996793a2a13SMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[fv[3]*dim+d]);
19970ec8681fSMatthew G. Knepley       }
19980ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1999793a2a13SMatthew G. Knepley       if (facesO[f] < 0 || flip) vtmp = -vtmp;
20000ec8681fSMatthew G. Knepley       vsum += vtmp;
20010ec8681fSMatthew G. Knepley       if (centroid) {
20020ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
20030ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
20040ec8681fSMatthew G. Knepley         }
20050ec8681fSMatthew G. Knepley       }
20060ec8681fSMatthew G. Knepley       break;
2007793a2a13SMatthew G. Knepley     }
20080ec8681fSMatthew G. Knepley     default:
2009796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
20100ec8681fSMatthew G. Knepley     }
20114f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
20120ec8681fSMatthew G. Knepley   }
20138763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
20140ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
2015d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
20160ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
20170ec8681fSMatthew G. Knepley }
20180ec8681fSMatthew G. Knepley 
2019834e62ceSMatthew G. Knepley /*@C
2020834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
2021834e62ceSMatthew G. Knepley 
2022d083f849SBarry Smith   Collective on dm
2023834e62ceSMatthew G. Knepley 
2024834e62ceSMatthew G. Knepley   Input Arguments:
2025834e62ceSMatthew G. Knepley + dm   - the DM
2026834e62ceSMatthew G. Knepley - cell - the cell
2027834e62ceSMatthew G. Knepley 
2028834e62ceSMatthew G. Knepley   Output Arguments:
2029834e62ceSMatthew G. Knepley + volume   - the cell volume
2030cc08537eSMatthew G. Knepley . centroid - the cell centroid
2031cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
2032834e62ceSMatthew G. Knepley 
2033834e62ceSMatthew G. Knepley   Level: advanced
2034834e62ceSMatthew G. Knepley 
2035834e62ceSMatthew G. Knepley   Fortran Notes:
2036834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
2037834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
2038834e62ceSMatthew G. Knepley 
2039e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
2040834e62ceSMatthew G. Knepley @*/
2041cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
2042834e62ceSMatthew G. Knepley {
20430ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
2044834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
2045834e62ceSMatthew G. Knepley 
2046834e62ceSMatthew G. Knepley   PetscFunctionBegin;
2047834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2048c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2049834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
2050834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
2051c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
2052834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
2053011ea5d8SMatthew G. Knepley   switch (depth) {
2054cc08537eSMatthew G. Knepley   case 1:
2055011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2056cc08537eSMatthew G. Knepley     break;
2057834e62ceSMatthew G. Knepley   case 2:
2058011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2059834e62ceSMatthew G. Knepley     break;
2060834e62ceSMatthew G. Knepley   case 3:
2061011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
2062834e62ceSMatthew G. Knepley     break;
2063834e62ceSMatthew G. Knepley   default:
2064b81cf158SMatthew G. Knepley     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D (depth %D) for element geometry computation", dim, depth);
2065834e62ceSMatthew G. Knepley   }
2066834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
2067834e62ceSMatthew G. Knepley }
2068113c68e6SMatthew G. Knepley 
2069c501906fSMatthew G. Knepley /*@
2070c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
2071c501906fSMatthew G. Knepley 
2072c501906fSMatthew G. Knepley   Collective on dm
2073c501906fSMatthew G. Knepley 
2074c501906fSMatthew G. Knepley   Input Parameter:
2075c501906fSMatthew G. Knepley . dm - The DMPlex
2076c501906fSMatthew G. Knepley 
2077c501906fSMatthew G. Knepley   Output Parameter:
2078c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
2079c501906fSMatthew G. Knepley 
2080c501906fSMatthew G. Knepley   Level: beginner
2081c501906fSMatthew G. Knepley 
2082c501906fSMatthew G. Knepley @*/
2083c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
2084c0d900a5SMatthew G. Knepley {
2085c0d900a5SMatthew G. Knepley   DM             dmCell;
2086c0d900a5SMatthew G. Knepley   Vec            coordinates;
2087c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
2088c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
2089c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
2090c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
2091c0d900a5SMatthew G. Knepley 
2092c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
2093c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2094c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2095c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2096c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2097c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2098c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2099c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2100c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2101c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
2102c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
2103c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
2104cf0b7c11SKarl Rupp   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFEGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2105c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
210692fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2107c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2108c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2109c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2110c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2111cf0b7c11SKarl Rupp     PetscFEGeom *cg;
2112c0d900a5SMatthew G. Knepley 
2113c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2114580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2115cf0b7c11SKarl Rupp     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v, cg->J, cg->invJ, cg->detJ);CHKERRQ(ierr);
2116087ef6b2SMatthew 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);
2117c0d900a5SMatthew G. Knepley   }
2118c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
2119c0d900a5SMatthew G. Knepley }
2120c0d900a5SMatthew G. Knepley 
2121891a9168SMatthew G. Knepley /*@
2122891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2123891a9168SMatthew G. Knepley 
2124891a9168SMatthew G. Knepley   Input Parameter:
2125891a9168SMatthew G. Knepley . dm - The DM
2126891a9168SMatthew G. Knepley 
2127891a9168SMatthew G. Knepley   Output Parameters:
2128891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2129a2b725a8SWilliam Gropp - facegeom - A Vec of PetscFVFaceGeom data
2130891a9168SMatthew G. Knepley 
2131891a9168SMatthew G. Knepley   Level: developer
2132891a9168SMatthew G. Knepley 
2133891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
2134891a9168SMatthew G. Knepley @*/
2135113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2136113c68e6SMatthew G. Knepley {
2137113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
2138113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
2139113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
2140113c68e6SMatthew G. Knepley   PetscSection   coordSection;
2141113c68e6SMatthew G. Knepley   Vec            coordinates;
2142113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2143113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
2144113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2145113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
2146113c68e6SMatthew G. Knepley 
2147113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2148113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2149113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2150113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2151113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
2152113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2153113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2154113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2155113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2156113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2157485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2158113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
21599e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2160113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
216192fd8e1eSJed Brown   ierr = DMSetLocalSection(dmCell, sectionCell);CHKERRQ(ierr);
2162113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2163113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2164485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2165113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2166113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2167113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2168113c68e6SMatthew G. Knepley 
2169113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2170580bdb30SBarry Smith     ierr = PetscArrayzero(cg, 1);CHKERRQ(ierr);
2171113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
2172113c68e6SMatthew G. Knepley   }
2173113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
2174113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
2175113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
2176113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2177113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
21789e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2179113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
218092fd8e1eSJed Brown   ierr = DMSetLocalSection(dmFace, sectionFace);CHKERRQ(ierr);
2181113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
2182113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
2183113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
2184c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2185113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2186113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2187113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2188113c68e6SMatthew G. Knepley     PetscReal        area;
218950d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
2190113c68e6SMatthew G. Knepley 
21919ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
219250d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
219350d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
2194113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
2195113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
2196113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2197113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2198113c68e6SMatthew G. Knepley     {
2199113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
220006348e87SToby Isaac       PetscInt         ncells;
2201113c68e6SMatthew G. Knepley       const PetscInt  *cells;
2202113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
22030453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2204113c68e6SMatthew G. Knepley 
2205113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
220606348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
2207113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
2208113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
220906348e87SToby Isaac       if (ncells > 1) {
221006348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
2211113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
221206348e87SToby Isaac       }
221306348e87SToby Isaac       else {
221406348e87SToby Isaac         rcentroid = fg->centroid;
221506348e87SToby Isaac       }
22162e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
22172e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
22180453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2219113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2220113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2221113c68e6SMatthew G. Knepley       }
2222113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
2223113c68e6SMatthew 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]);
2224113c68e6SMatthew 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]);
2225113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
2226113c68e6SMatthew G. Knepley       }
2227113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2228113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2229113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2230113c68e6SMatthew G. Knepley       }
223106348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2232113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2233113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2234113c68e6SMatthew G. Knepley       }
2235113c68e6SMatthew G. Knepley     }
2236113c68e6SMatthew G. Knepley   }
2237b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2238113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
2239113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2240113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2241113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2242113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
2243113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2244113c68e6SMatthew G. Knepley 
2245113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
2246113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
2247113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
2248113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
224950d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
2250113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
2251113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
2252113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2253113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2254113c68e6SMatthew G. Knepley       if (support[s] == c) {
2255640bce14SSatish Balay         PetscFVCellGeom       *ci;
2256113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
2257113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
2258113c68e6SMatthew G. Knepley 
2259113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
2260113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2261113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
2262113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
2263113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
2264113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2265113c68e6SMatthew G. Knepley       }
2266113c68e6SMatthew G. Knepley     }
2267113c68e6SMatthew G. Knepley   }
2268113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
2269113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2270113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2271113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
2272113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2273113c68e6SMatthew G. Knepley }
2274113c68e6SMatthew G. Knepley 
2275113c68e6SMatthew G. Knepley /*@C
2276113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2277113c68e6SMatthew G. Knepley 
2278113c68e6SMatthew G. Knepley   Not collective
2279113c68e6SMatthew G. Knepley 
2280113c68e6SMatthew G. Knepley   Input Argument:
2281113c68e6SMatthew G. Knepley . dm - the DM
2282113c68e6SMatthew G. Knepley 
2283113c68e6SMatthew G. Knepley   Output Argument:
2284113c68e6SMatthew G. Knepley . minradius - the minium cell radius
2285113c68e6SMatthew G. Knepley 
2286113c68e6SMatthew G. Knepley   Level: developer
2287113c68e6SMatthew G. Knepley 
2288113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
2289113c68e6SMatthew G. Knepley @*/
2290113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2291113c68e6SMatthew G. Knepley {
2292113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2293113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2294113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
2295113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
2296113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2297113c68e6SMatthew G. Knepley }
2298113c68e6SMatthew G. Knepley 
2299113c68e6SMatthew G. Knepley /*@C
2300113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2301113c68e6SMatthew G. Knepley 
2302113c68e6SMatthew G. Knepley   Logically collective
2303113c68e6SMatthew G. Knepley 
2304113c68e6SMatthew G. Knepley   Input Arguments:
2305113c68e6SMatthew G. Knepley + dm - the DM
2306113c68e6SMatthew G. Knepley - minradius - the minium cell radius
2307113c68e6SMatthew G. Knepley 
2308113c68e6SMatthew G. Knepley   Level: developer
2309113c68e6SMatthew G. Knepley 
2310113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
2311113c68e6SMatthew G. Knepley @*/
2312113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2313113c68e6SMatthew G. Knepley {
2314113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2315113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2316113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
2317113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2318113c68e6SMatthew G. Knepley }
2319856ac710SMatthew G. Knepley 
2320856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2321856ac710SMatthew G. Knepley {
2322856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
2323856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
2324856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2325856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2326856ac710SMatthew G. Knepley 
2327856ac710SMatthew G. Knepley   PetscFunctionBegin;
2328856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2329856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2330485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2331856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
2332856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2333c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2334856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2335856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2336856ac710SMatthew G. Knepley     const PetscInt        *faces;
2337856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
2338640bce14SSatish Balay     PetscFVCellGeom        *cg;
2339856ac710SMatthew G. Knepley     PetscBool              boundary;
2340856ac710SMatthew G. Knepley     PetscInt               ghost;
2341856ac710SMatthew G. Knepley 
2342856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2343856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
2344856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
2345856ac710SMatthew 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);
2346856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2347640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2348856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
2349856ac710SMatthew G. Knepley       const PetscInt        *fcells;
2350856ac710SMatthew G. Knepley       PetscInt               ncell, side;
2351856ac710SMatthew G. Knepley 
2352856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2353a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2354856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2355856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
2356856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2357856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
2358856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
2359856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2360856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
2361856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
2362856ac710SMatthew G. Knepley     }
2363856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
2364856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
2365856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2366856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2367a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2368856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2369856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
2370856ac710SMatthew G. Knepley       ++usedFaces;
2371856ac710SMatthew G. Knepley     }
2372856ac710SMatthew G. Knepley   }
2373856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
2374856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2375856ac710SMatthew G. Knepley }
2376856ac710SMatthew G. Knepley 
2377b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2378b81db932SToby Isaac {
2379b81db932SToby Isaac   DMLabel        ghostLabel;
2380b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
2381b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2382b81db932SToby Isaac   PetscSection   neighSec;
2383b81db932SToby Isaac   PetscInt     (*neighbors)[2];
2384b81db932SToby Isaac   PetscInt      *counter;
2385b81db932SToby Isaac   PetscErrorCode ierr;
2386b81db932SToby Isaac 
2387b81db932SToby Isaac   PetscFunctionBegin;
2388b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2389b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2390485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2391485ad865SMatthew G. Knepley   if (cEndInterior < 0) cEndInterior = cEnd;
2392b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
2393b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
2394b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2395c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2396b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2397b81db932SToby Isaac     const PetscInt        *fcells;
2398b81db932SToby Isaac     PetscBool              boundary;
23995bc680faSToby Isaac     PetscInt               ghost = -1;
2400b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2401b81db932SToby Isaac 
240206348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2403a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2404b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2405b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2406b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
240706348e87SToby Isaac     if (numCells == 2) {
2408b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2409b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2410b81db932SToby Isaac         PetscInt cell = fcells[c];
2411b81db932SToby Isaac 
2412e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2413b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
2414b81db932SToby Isaac         }
2415b81db932SToby Isaac       }
2416b81db932SToby Isaac     }
241706348e87SToby Isaac   }
2418b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
2419b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
2420b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2421b81db932SToby Isaac   nStart = 0;
2422b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2423b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2424b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2425b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2426b81db932SToby Isaac     const PetscInt        *fcells;
2427b81db932SToby Isaac     PetscBool              boundary;
24285bc680faSToby Isaac     PetscInt               ghost = -1;
2429b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2430b81db932SToby Isaac 
243106348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2432a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2433b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2434b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2435b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
243606348e87SToby Isaac     if (numCells == 2) {
2437b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2438b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2439b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2440b81db932SToby Isaac 
2441e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2442b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2443b81db932SToby Isaac           off += counter[cell - cStart]++;
2444b81db932SToby Isaac           neighbors[off][0] = f;
2445b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2446b81db932SToby Isaac         }
2447b81db932SToby Isaac       }
2448b81db932SToby Isaac     }
244906348e87SToby Isaac   }
2450b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2451b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2452b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2453317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2454640bce14SSatish Balay     PetscFVCellGeom        *cg;
2455b81db932SToby Isaac 
2456b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2457b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2458b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2459317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2460317218b9SToby 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);
2461b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2462640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2463b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2464b81db932SToby Isaac       const PetscInt        *fcells;
2465b81db932SToby Isaac       PetscInt               ncell, side, nface;
2466b81db932SToby Isaac 
2467b81db932SToby Isaac       nface = neighbors[off + f][0];
2468b81db932SToby Isaac       ncell = neighbors[off + f][1];
2469b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2470b81db932SToby Isaac       side  = (c != fcells[0]);
2471b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2472b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2473b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2474b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2475b81db932SToby Isaac     }
2476b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2477b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2478b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2479b81db932SToby Isaac     }
2480b81db932SToby Isaac   }
2481b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
24825fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2483b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2484b81db932SToby Isaac   PetscFunctionReturn(0);
2485b81db932SToby Isaac }
2486b81db932SToby Isaac 
2487856ac710SMatthew G. Knepley /*@
2488856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2489856ac710SMatthew G. Knepley 
2490d083f849SBarry Smith   Collective on dm
2491856ac710SMatthew G. Knepley 
2492856ac710SMatthew G. Knepley   Input Arguments:
2493856ac710SMatthew G. Knepley + dm  - The DM
2494856ac710SMatthew G. Knepley . fvm - The PetscFV
24958f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
24968f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2497856ac710SMatthew G. Knepley 
2498856ac710SMatthew G. Knepley   Output Parameters:
2499856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2500856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2501856ac710SMatthew G. Knepley 
2502856ac710SMatthew G. Knepley   Level: developer
2503856ac710SMatthew G. Knepley 
2504856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2505856ac710SMatthew G. Knepley @*/
2506856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2507856ac710SMatthew G. Knepley {
2508856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2509856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2510b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2511856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2512856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2513856ac710SMatthew G. Knepley 
2514856ac710SMatthew G. Knepley   PetscFunctionBegin;
2515856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2516856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2517856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2518485ad865SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &cEndInterior, NULL);CHKERRQ(ierr);
2519856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2520856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2521856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2522856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2523856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2524b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2525b81db932SToby Isaac   if (!parentSection) {
2526856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2527b5a3613cSMatthew G. Knepley   } else {
2528b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2529b81db932SToby Isaac   }
2530856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2531856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2532856ac710SMatthew G. Knepley   /* Create storage for gradients */
2533856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2534856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2535856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2536856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2537856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
253892fd8e1eSJed Brown   ierr = DMSetLocalSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2539856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2540856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2541856ac710SMatthew G. Knepley }
2542b27d5b9eSToby Isaac 
2543c501906fSMatthew G. Knepley /*@
2544c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2545c501906fSMatthew G. Knepley 
2546d083f849SBarry Smith   Collective on dm
2547c501906fSMatthew G. Knepley 
2548c501906fSMatthew G. Knepley   Input Arguments:
2549c501906fSMatthew G. Knepley + dm  - The DM
2550c501906fSMatthew G. Knepley - fvm - The PetscFV
2551c501906fSMatthew G. Knepley 
2552c501906fSMatthew G. Knepley   Output Parameters:
2553c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2554c501906fSMatthew G. Knepley . faceGeometry - The face geometry
2555c501906fSMatthew G. Knepley - dmGrad       - The gradient matrices
2556c501906fSMatthew G. Knepley 
2557c501906fSMatthew G. Knepley   Level: developer
2558c501906fSMatthew G. Knepley 
2559c501906fSMatthew G. Knepley .seealso: DMPlexComputeGeometryFVM()
2560c501906fSMatthew G. Knepley @*/
2561b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2562b27d5b9eSToby Isaac {
2563b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2564b27d5b9eSToby Isaac   PetscErrorCode ierr;
2565b27d5b9eSToby Isaac 
2566b27d5b9eSToby Isaac   PetscFunctionBegin;
2567b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2568b27d5b9eSToby Isaac   if (!cellgeomobj) {
2569b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2570b27d5b9eSToby Isaac 
2571b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2572b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2573b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2574b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2575b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2576b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2577b27d5b9eSToby Isaac   }
2578b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2579b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2580b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2581b27d5b9eSToby Isaac   if (gradDM) {
2582b27d5b9eSToby Isaac     PetscObject gradobj;
2583b27d5b9eSToby Isaac     PetscBool   computeGradients;
2584b27d5b9eSToby Isaac 
2585b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2586b27d5b9eSToby Isaac     if (!computeGradients) {
2587b27d5b9eSToby Isaac       *gradDM = NULL;
2588b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2589b27d5b9eSToby Isaac     }
2590b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2591b27d5b9eSToby Isaac     if (!gradobj) {
2592b27d5b9eSToby Isaac       DM dmGradInt;
2593b27d5b9eSToby Isaac 
2594b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2595b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2596b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2597b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2598b27d5b9eSToby Isaac     }
2599b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2600b27d5b9eSToby Isaac   }
2601b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2602b27d5b9eSToby Isaac }
2603d6143a4eSToby Isaac 
26049d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
26059d150b73SToby Isaac {
26069d150b73SToby Isaac   PetscInt l, m;
26079d150b73SToby Isaac 
2608cd345991SToby Isaac   PetscFunctionBeginHot;
26099d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
26109d150b73SToby Isaac     /* invert Jacobian, multiply */
26119d150b73SToby Isaac     PetscScalar det, idet;
26129d150b73SToby Isaac 
26139d150b73SToby Isaac     switch (dimR) {
26149d150b73SToby Isaac     case 1:
26159d150b73SToby Isaac       invJ[0] = 1./ J[0];
26169d150b73SToby Isaac       break;
26179d150b73SToby Isaac     case 2:
26189d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
26199d150b73SToby Isaac       idet = 1./det;
26209d150b73SToby Isaac       invJ[0] =  J[3] * idet;
26219d150b73SToby Isaac       invJ[1] = -J[1] * idet;
26229d150b73SToby Isaac       invJ[2] = -J[2] * idet;
26239d150b73SToby Isaac       invJ[3] =  J[0] * idet;
26249d150b73SToby Isaac       break;
26259d150b73SToby Isaac     case 3:
26269d150b73SToby Isaac       {
26279d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
26289d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
26299d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
26309d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
26319d150b73SToby Isaac         idet = 1./det;
26329d150b73SToby Isaac         invJ[0] *= idet;
26339d150b73SToby Isaac         invJ[1] *= idet;
26349d150b73SToby Isaac         invJ[2] *= idet;
26359d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
26369d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
26379d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
26389d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
26399d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
26409d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
26419d150b73SToby Isaac       }
26429d150b73SToby Isaac       break;
26439d150b73SToby Isaac     }
26449d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
26459d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2646c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
26479d150b73SToby Isaac       }
26489d150b73SToby Isaac     }
26499d150b73SToby Isaac   } else {
26509d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
26519d150b73SToby Isaac     char transpose = 'C';
26529d150b73SToby Isaac #else
26539d150b73SToby Isaac     char transpose = 'T';
26549d150b73SToby Isaac #endif
26559d150b73SToby Isaac     PetscBLASInt m = dimR;
26569d150b73SToby Isaac     PetscBLASInt n = dimC;
26579d150b73SToby Isaac     PetscBLASInt one = 1;
26589d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
26599d150b73SToby Isaac 
26609d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
26619d150b73SToby Isaac 
26629d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
26639d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
26649d150b73SToby Isaac 
2665c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
26669d150b73SToby Isaac   }
26679d150b73SToby Isaac   PetscFunctionReturn(0);
26689d150b73SToby Isaac }
26699d150b73SToby Isaac 
26709d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
26719d150b73SToby Isaac {
2672c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
26739d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
26749d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
26759d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
26769d150b73SToby Isaac   PetscErrorCode ierr;
26779d150b73SToby Isaac 
26789d150b73SToby Isaac   PetscFunctionBegin;
26799d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26809d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
268113903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
268269291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
268369291d52SBarry Smith   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
26849d150b73SToby Isaac   cellCoords = &cellData[0];
26859d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
26869d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
26879d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
26889d150b73SToby Isaac   invJ       = &J[dimR * dimC];
26899d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
26909d150b73SToby Isaac   if (dimR == 2) {
26919d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
26929d150b73SToby Isaac 
26939d150b73SToby Isaac     for (i = 0; i < 4; i++) {
26949d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
26959d150b73SToby Isaac 
26969d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26979d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26989d150b73SToby Isaac       }
26999d150b73SToby Isaac     }
27009d150b73SToby Isaac   } else if (dimR == 3) {
27019d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
27029d150b73SToby Isaac 
27039d150b73SToby Isaac     for (i = 0; i < 8; i++) {
27049d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
27059d150b73SToby Isaac 
27069d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
27079d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
27089d150b73SToby Isaac       }
27099d150b73SToby Isaac     }
27109d150b73SToby Isaac   } else {
27119d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
27129d150b73SToby Isaac   }
27139d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
27149d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
27159d150b73SToby Isaac     PetscReal *swap;
27169d150b73SToby Isaac 
27179d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
27189d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
27199d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
27209d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
27219d150b73SToby Isaac       }
27229d150b73SToby Isaac     }
27239d150b73SToby Isaac 
27249d150b73SToby Isaac     if (i < dimR - 1) {
27259d150b73SToby Isaac       swap = cellCoeffs;
27269d150b73SToby Isaac       cellCoeffs = cellCoords;
27279d150b73SToby Isaac       cellCoords = swap;
27289d150b73SToby Isaac     }
27299d150b73SToby Isaac   }
2730580bdb30SBarry Smith   ierr = PetscArrayzero(refCoords,numPoints * dimR);CHKERRQ(ierr);
27319d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
27329d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
27339d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
27349d150b73SToby Isaac 
27359d150b73SToby Isaac       /* compute -residual and Jacobian */
27369d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
27379d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
27389d150b73SToby Isaac       for (k = 0; k < numV; k++) {
27399d150b73SToby Isaac         PetscReal extCoord = 1.;
27409d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
27419d150b73SToby Isaac           PetscReal coord = guess[l];
27429d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
27439d150b73SToby Isaac 
27449d150b73SToby Isaac           extCoord *= dep * coord + !dep;
27459d150b73SToby Isaac           extJ[l] = dep;
27469d150b73SToby Isaac 
27479d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
27489d150b73SToby Isaac             PetscReal coord = guess[m];
27499d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
27509d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
27519d150b73SToby Isaac 
27529d150b73SToby Isaac             extJ[l] *= mult;
27539d150b73SToby Isaac           }
27549d150b73SToby Isaac         }
27559d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
27569d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
27579d150b73SToby Isaac 
27589d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
27599d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
27609d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
27619d150b73SToby Isaac           }
27629d150b73SToby Isaac         }
27639d150b73SToby Isaac       }
27640611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
27650611203eSToby Isaac       {
27660611203eSToby Isaac         PetscReal maxAbs = 0.;
27670611203eSToby Isaac 
27680611203eSToby Isaac         for (l = 0; l < dimC; l++) {
27690611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
27700611203eSToby Isaac         }
2771087ef6b2SMatthew G. Knepley         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
27720611203eSToby Isaac       }
27730611203eSToby Isaac #endif
27749d150b73SToby Isaac 
27759d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
27769d150b73SToby Isaac     }
27779d150b73SToby Isaac   }
277869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
277969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
27809d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
27819d150b73SToby Isaac   PetscFunctionReturn(0);
27829d150b73SToby Isaac }
27839d150b73SToby Isaac 
27849d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
27859d150b73SToby Isaac {
27869d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
27879d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
27889d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
27899d150b73SToby Isaac   PetscErrorCode ierr;
27909d150b73SToby Isaac 
27919d150b73SToby Isaac   PetscFunctionBegin;
27929d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27939d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
279413903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
279569291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
27969d150b73SToby Isaac   cellCoords = &cellData[0];
27979d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
27989d150b73SToby Isaac   if (dimR == 2) {
27999d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
28009d150b73SToby Isaac 
28019d150b73SToby Isaac     for (i = 0; i < 4; i++) {
28029d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
28039d150b73SToby Isaac 
28049d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
28059d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
28069d150b73SToby Isaac       }
28079d150b73SToby Isaac     }
28089d150b73SToby Isaac   } else if (dimR == 3) {
28099d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
28109d150b73SToby Isaac 
28119d150b73SToby Isaac     for (i = 0; i < 8; i++) {
28129d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
28139d150b73SToby Isaac 
28149d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
28159d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
28169d150b73SToby Isaac       }
28179d150b73SToby Isaac     }
28189d150b73SToby Isaac   } else {
28199d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
28209d150b73SToby Isaac   }
28219d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
28229d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
28239d150b73SToby Isaac     PetscReal *swap;
28249d150b73SToby Isaac 
28259d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
28269d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
28279d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
28289d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
28299d150b73SToby Isaac       }
28309d150b73SToby Isaac     }
28319d150b73SToby Isaac 
28329d150b73SToby Isaac     if (i < dimR - 1) {
28339d150b73SToby Isaac       swap = cellCoeffs;
28349d150b73SToby Isaac       cellCoeffs = cellCoords;
28359d150b73SToby Isaac       cellCoords = swap;
28369d150b73SToby Isaac     }
28379d150b73SToby Isaac   }
2838580bdb30SBarry Smith   ierr = PetscArrayzero(realCoords,numPoints * dimC);CHKERRQ(ierr);
28399d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28409d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
28419d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
28429d150b73SToby Isaac 
28439d150b73SToby Isaac     for (k = 0; k < numV; k++) {
28449d150b73SToby Isaac       PetscReal extCoord = 1.;
28459d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
28469d150b73SToby Isaac         PetscReal coord = guess[l];
28479d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
28489d150b73SToby Isaac 
28499d150b73SToby Isaac         extCoord *= dep * coord + !dep;
28509d150b73SToby Isaac       }
28519d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
28529d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
28539d150b73SToby Isaac 
28549d150b73SToby Isaac         mapped[l] += coeff * extCoord;
28559d150b73SToby Isaac       }
28569d150b73SToby Isaac     }
28579d150b73SToby Isaac   }
285869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
28599d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
28609d150b73SToby Isaac   PetscFunctionReturn(0);
28619d150b73SToby Isaac }
28629d150b73SToby Isaac 
28639c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
28649c3cf19fSMatthew 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)
28659d150b73SToby Isaac {
28669c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
2867c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2868c6e120d1SToby Isaac   PetscReal      *invV, *modes;
2869c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
2870c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
28719d150b73SToby Isaac   PetscErrorCode ierr;
28729d150b73SToby Isaac 
28739d150b73SToby Isaac   PetscFunctionBegin;
28749c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
28759d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
28769c3cf19fSMatthew 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);
28779d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
28789d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
287969291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
28809d150b73SToby Isaac   invV = fe->invV;
2881012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2882012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2883012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2884012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
28859d150b73SToby Isaac     }
28869d150b73SToby Isaac   }
288769291d52SBarry Smith   ierr   = DMGetWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
28889c3cf19fSMatthew G. Knepley   D      = &B[pdim*Nc];
28899c3cf19fSMatthew G. Knepley   resNeg = &D[pdim*Nc * dimR];
289069291d52SBarry Smith   ierr = DMGetWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
28919c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
28929c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
28939d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
28949d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28959b1f03cbSToby 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 */
28969d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
28979d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
28989c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc; k++) {resNeg[k] = realCoords[j * Nc + k];}
28999c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc * dimR; k++) {J[k] = 0.;}
29009c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
29019c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
2902012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
29039d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
2904012b7cc6SMatthew G. Knepley             J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
29059d150b73SToby Isaac           }
29069d150b73SToby Isaac         }
29079d150b73SToby Isaac       }
29080611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
29090611203eSToby Isaac       {
29100611203eSToby Isaac         PetscReal maxAbs = 0.;
29110611203eSToby Isaac 
29129c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
29130611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
29140611203eSToby Isaac         }
2915087ef6b2SMatthew G. Knepley         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,(double) maxAbs);CHKERRQ(ierr);
29160611203eSToby Isaac       }
29170611203eSToby Isaac #endif
29189c3cf19fSMatthew G. Knepley       ierr = DMPlexCoordinatesToReference_NewtonUpdate(Nc,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
29199d150b73SToby Isaac     }
29209d150b73SToby Isaac   }
292169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
292269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
292369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
29249d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
29259d150b73SToby Isaac   PetscFunctionReturn(0);
29269d150b73SToby Isaac }
29279d150b73SToby Isaac 
29289c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
29299c3cf19fSMatthew 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)
29309d150b73SToby Isaac {
29319c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, coordSize;
2932c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2933c6e120d1SToby Isaac   PetscReal      *invV, *modes;
29349d150b73SToby Isaac   PetscReal      *B;
29359d150b73SToby Isaac   PetscErrorCode ierr;
29369d150b73SToby Isaac 
29379d150b73SToby Isaac   PetscFunctionBegin;
29389c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
29399d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
29409c3cf19fSMatthew 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);
29419d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
29429d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
294369291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
29449d150b73SToby Isaac   invV = fe->invV;
2945012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2946012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2947012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2948012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
29499d150b73SToby Isaac     }
29509d150b73SToby Isaac   }
295169291d52SBarry Smith   ierr = DMGetWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
2952012b7cc6SMatthew G. Knepley   ierr = PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL);CHKERRQ(ierr);
29539c3cf19fSMatthew G. Knepley   for (i = 0; i < numPoints * Nc; i++) {realCoords[i] = 0.;}
29549d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
29559c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
29569d150b73SToby Isaac 
29579c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
29589c3cf19fSMatthew G. Knepley       for (l = 0; l < Nc; l++) {
295940cf36b3SToby Isaac         mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
29609d150b73SToby Isaac       }
29619d150b73SToby Isaac     }
29629d150b73SToby Isaac   }
296369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
296469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
29659d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
29669d150b73SToby Isaac   PetscFunctionReturn(0);
29679d150b73SToby Isaac }
29689d150b73SToby Isaac 
2969d6143a4eSToby Isaac /*@
2970d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
2971d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
2972d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
2973d6143a4eSToby Isaac 
2974d6143a4eSToby Isaac   Not collective
2975d6143a4eSToby Isaac 
2976d6143a4eSToby Isaac   Input Parameters:
2977d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2978d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2979d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
2980d6143a4eSToby Isaac . cell       - the cell whose map is used.
2981d6143a4eSToby Isaac . numPoints  - the number of points to locate
29821b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2983d6143a4eSToby Isaac 
2984d6143a4eSToby Isaac   Output Parameters:
2985d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
29861b266c99SBarry Smith 
29871b266c99SBarry Smith   Level: intermediate
298873c9229bSMatthew Knepley 
298973c9229bSMatthew Knepley .seealso: DMPlexReferenceToCoordinates()
2990d6143a4eSToby Isaac @*/
2991d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
2992d6143a4eSToby Isaac {
2993485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
29949d150b73SToby Isaac   DM             coordDM = NULL;
29959d150b73SToby Isaac   Vec            coords;
29969d150b73SToby Isaac   PetscFE        fe = NULL;
29979d150b73SToby Isaac   PetscErrorCode ierr;
29989d150b73SToby Isaac 
2999d6143a4eSToby Isaac   PetscFunctionBegin;
30009d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30019d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
30029d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
30039d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
30049d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
30059d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
30069d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
30079d150b73SToby Isaac   if (coordDM) {
30089d150b73SToby Isaac     PetscInt coordFields;
30099d150b73SToby Isaac 
30109d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
30119d150b73SToby Isaac     if (coordFields) {
30129d150b73SToby Isaac       PetscClassId id;
30139d150b73SToby Isaac       PetscObject  disc;
30149d150b73SToby Isaac 
301544a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
30169d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
30179d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
30189d150b73SToby Isaac         fe = (PetscFE) disc;
30199d150b73SToby Isaac       }
30209d150b73SToby Isaac     }
30219d150b73SToby Isaac   }
3022485ad865SMatthew G. Knepley   ierr = DMPlexGetInteriorCellStratum(dm,&cStart,&cEnd);CHKERRQ(ierr);
302313903a91SSatish 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);
30249d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
30259d150b73SToby Isaac     PetscInt  coneSize;
30269d150b73SToby Isaac     PetscBool isSimplex, isTensor;
30279d150b73SToby Isaac 
30289d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
30299d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
30309d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
30319d150b73SToby Isaac     if (isSimplex) {
30329d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
30339d150b73SToby Isaac 
303469291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
30359d150b73SToby Isaac       J    = &v0[dimC];
30369d150b73SToby Isaac       invJ = &J[dimC * dimC];
30379d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
30389d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
3039c330f8ffSToby Isaac         const PetscReal x0[3] = {-1.,-1.,-1.};
3040c330f8ffSToby Isaac 
3041c330f8ffSToby Isaac         CoordinatesRealToRef(dimC, dimR, x0, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
30429d150b73SToby Isaac       }
304369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
30449d150b73SToby Isaac     } else if (isTensor) {
30459d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
30469d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
30479d150b73SToby Isaac   } else {
30489d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
30499d150b73SToby Isaac   }
30509d150b73SToby Isaac   PetscFunctionReturn(0);
30519d150b73SToby Isaac }
30529d150b73SToby Isaac 
30539d150b73SToby Isaac /*@
30549d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
30559d150b73SToby Isaac 
30569d150b73SToby Isaac   Not collective
30579d150b73SToby Isaac 
30589d150b73SToby Isaac   Input Parameters:
30599d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
30609d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
30619d150b73SToby Isaac                as a multilinear map for tensor-product elements
30629d150b73SToby Isaac . cell       - the cell whose map is used.
30639d150b73SToby Isaac . numPoints  - the number of points to locate
3064a2b725a8SWilliam Gropp - refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
30659d150b73SToby Isaac 
30669d150b73SToby Isaac   Output Parameters:
30679d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
30681b266c99SBarry Smith 
30691b266c99SBarry Smith    Level: intermediate
307073c9229bSMatthew Knepley 
307173c9229bSMatthew Knepley .seealso: DMPlexCoordinatesToReference()
30729d150b73SToby Isaac @*/
30739d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
30749d150b73SToby Isaac {
3075485ad865SMatthew G. Knepley   PetscInt       dimC, dimR, depth, cStart, cEnd, i;
30769d150b73SToby Isaac   DM             coordDM = NULL;
30779d150b73SToby Isaac   Vec            coords;
30789d150b73SToby Isaac   PetscFE        fe = NULL;
30799d150b73SToby Isaac   PetscErrorCode ierr;
30809d150b73SToby Isaac 
30819d150b73SToby Isaac   PetscFunctionBegin;
30829d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30839d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
30849d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
30859d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
30869d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
30879d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
30889d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
30899d150b73SToby Isaac   if (coordDM) {
30909d150b73SToby Isaac     PetscInt coordFields;
30919d150b73SToby Isaac 
30929d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
30939d150b73SToby Isaac     if (coordFields) {
30949d150b73SToby Isaac       PetscClassId id;
30959d150b73SToby Isaac       PetscObject  disc;
30969d150b73SToby Isaac 
309744a7f3ddSMatthew G. Knepley       ierr = DMGetField(coordDM,0,NULL,&disc);CHKERRQ(ierr);
30989d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
30999d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
31009d150b73SToby Isaac         fe = (PetscFE) disc;
31019d150b73SToby Isaac       }
31029d150b73SToby Isaac     }
31039d150b73SToby Isaac   }
3104485ad865SMatthew G. Knepley   ierr = DMPlexGetInteriorCellStratum(dm,&cStart,&cEnd);CHKERRQ(ierr);
310513903a91SSatish 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);
31069d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
31079d150b73SToby Isaac     PetscInt  coneSize;
31089d150b73SToby Isaac     PetscBool isSimplex, isTensor;
31099d150b73SToby Isaac 
31109d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
31119d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
31129d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
31139d150b73SToby Isaac     if (isSimplex) {
31149d150b73SToby Isaac       PetscReal detJ, *v0, *J;
31159d150b73SToby Isaac 
311669291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
31179d150b73SToby Isaac       J    = &v0[dimC];
31189d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
3119c330f8ffSToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the affine transformation for each point */
3120c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1.,-1.,-1.};
3121c330f8ffSToby Isaac 
3122c330f8ffSToby Isaac         CoordinatesRefToReal(dimC, dimR, xi0, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
31239d150b73SToby Isaac       }
312469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
31259d150b73SToby Isaac     } else if (isTensor) {
31269d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
31279d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
31289d150b73SToby Isaac   } else {
31299d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
31309d150b73SToby Isaac   }
3131d6143a4eSToby Isaac   PetscFunctionReturn(0);
3132d6143a4eSToby Isaac }
3133