xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 7318780a40fab654c7cda3dab6256a7bd3c996fd)
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>
4ccd2543fSMatthew G Knepley 
5ccd2543fSMatthew G Knepley #undef __FUNCT__
6fea14342SMatthew G. Knepley #define __FUNCT__ "DMPlexGetLineIntersection_2D_Internal"
7fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
8fea14342SMatthew G. Knepley {
9fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*2+0];
10fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*2+1];
11fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*2+0];
12fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*2+1];
13fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0*2+0];
14fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0*2+1];
15fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1*2+0];
16fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1*2+1];
17fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
18fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
19fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
20fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
21fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
22fea14342SMatthew G. Knepley 
23fea14342SMatthew G. Knepley   PetscFunctionBegin;
24fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
25fea14342SMatthew G. Knepley   /* Non-parallel lines */
26fea14342SMatthew G. Knepley   if (denom != 0.0) {
27fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
28fea14342SMatthew G. Knepley     const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
29fea14342SMatthew G. Knepley 
30fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
31fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
32fea14342SMatthew G. Knepley       if (intersection) {
33fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
34fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
35fea14342SMatthew G. Knepley       }
36fea14342SMatthew G. Knepley     }
37fea14342SMatthew G. Knepley   }
38fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
39fea14342SMatthew G. Knepley }
40fea14342SMatthew G. Knepley 
41fea14342SMatthew G. Knepley #undef __FUNCT__
42ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal"
43ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
44ccd2543fSMatthew G Knepley {
45ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
46f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
47ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
48ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
49ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
50ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
51ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
52ccd2543fSMatthew G Knepley 
53ccd2543fSMatthew G Knepley   PetscFunctionBegin;
548e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
55ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
56ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
57ccd2543fSMatthew G Knepley 
58f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
59c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
60ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
61ccd2543fSMatthew G Knepley }
62ccd2543fSMatthew G Knepley 
63ccd2543fSMatthew G Knepley #undef __FUNCT__
6462a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Simplex_2D_Internal"
6562a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
6662a38674SMatthew G. Knepley {
6762a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
6862a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
6962a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
7062a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
7162a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
7262a38674SMatthew G. Knepley   PetscErrorCode  ierr;
7362a38674SMatthew G. Knepley 
7462a38674SMatthew G. Knepley   PetscFunctionBegin;
7562a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
7662a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
7762a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
7862a38674SMatthew G. Knepley 
7962a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
8062a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
8162a38674SMatthew G. Knepley   r   = (xi + eta)/2.0;
8262a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
8362a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
8462a38674SMatthew G. Knepley     xi  /= r;
8562a38674SMatthew G. Knepley     eta /= r;
8662a38674SMatthew G. Knepley   }
8762a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
8862a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
8962a38674SMatthew G. Knepley   PetscFunctionReturn(0);
9062a38674SMatthew G. Knepley }
9162a38674SMatthew G. Knepley 
9262a38674SMatthew G. Knepley #undef __FUNCT__
93ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal"
94ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
95ccd2543fSMatthew G Knepley {
96ccd2543fSMatthew G Knepley   PetscSection       coordSection;
97ccd2543fSMatthew G Knepley   Vec             coordsLocal;
98a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
99ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
100ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
101ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
102ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
103ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
104ccd2543fSMatthew G Knepley 
105ccd2543fSMatthew G Knepley   PetscFunctionBegin;
106ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
10769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
108ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
109ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
110ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
111ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
112ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
113ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
114ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
115ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
116ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
117ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
118ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
119ccd2543fSMatthew G Knepley   }
120ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
121c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
122ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
123ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
124ccd2543fSMatthew G Knepley }
125ccd2543fSMatthew G Knepley 
126ccd2543fSMatthew G Knepley #undef __FUNCT__
127ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal"
128ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
129ccd2543fSMatthew G Knepley {
130ccd2543fSMatthew G Knepley   const PetscInt embedDim = 3;
131ccd2543fSMatthew G Knepley   PetscReal      v0[3], J[9], invJ[9], detJ;
132ccd2543fSMatthew G Knepley   PetscReal      x = PetscRealPart(point[0]);
133ccd2543fSMatthew G Knepley   PetscReal      y = PetscRealPart(point[1]);
134ccd2543fSMatthew G Knepley   PetscReal      z = PetscRealPart(point[2]);
135ccd2543fSMatthew G Knepley   PetscReal      xi, eta, zeta;
136ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
137ccd2543fSMatthew G Knepley 
138ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1398e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
140ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
141ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
142ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
143ccd2543fSMatthew G Knepley 
144ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c;
145c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
146ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
147ccd2543fSMatthew G Knepley }
148ccd2543fSMatthew G Knepley 
149ccd2543fSMatthew G Knepley #undef __FUNCT__
150ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal"
151ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
152ccd2543fSMatthew G Knepley {
153ccd2543fSMatthew G Knepley   PetscSection   coordSection;
154ccd2543fSMatthew G Knepley   Vec            coordsLocal;
1557c1f9639SMatthew G Knepley   PetscScalar   *coords;
156fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
157fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
158ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
159ccd2543fSMatthew G Knepley   PetscInt       f;
160ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
161ccd2543fSMatthew G Knepley 
162ccd2543fSMatthew G Knepley   PetscFunctionBegin;
163ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
16469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
165ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
166ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
167ccd2543fSMatthew G Knepley     /* Check the point is under plane */
168ccd2543fSMatthew G Knepley     /*   Get face normal */
169ccd2543fSMatthew G Knepley     PetscReal v_i[3];
170ccd2543fSMatthew G Knepley     PetscReal v_j[3];
171ccd2543fSMatthew G Knepley     PetscReal normal[3];
172ccd2543fSMatthew G Knepley     PetscReal pp[3];
173ccd2543fSMatthew G Knepley     PetscReal dot;
174ccd2543fSMatthew G Knepley 
175ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
176ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
177ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
178ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
179ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
180ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
181ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
182ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
183ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
184ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
185ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
186ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
187ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
188ccd2543fSMatthew G Knepley 
189ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
190ccd2543fSMatthew G Knepley     if (dot < 0.0) {
191ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
192ccd2543fSMatthew G Knepley       break;
193ccd2543fSMatthew G Knepley     }
194ccd2543fSMatthew G Knepley   }
195ccd2543fSMatthew G Knepley   if (found) *cell = c;
196c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
197ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
198ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
199ccd2543fSMatthew G Knepley }
200ccd2543fSMatthew G Knepley 
201ccd2543fSMatthew G Knepley #undef __FUNCT__
202c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal"
203c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
204c4eade1cSMatthew G. Knepley {
205c4eade1cSMatthew G. Knepley   PetscInt d;
206c4eade1cSMatthew G. Knepley 
207c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
208c4eade1cSMatthew G. Knepley   box->dim = dim;
209c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
210c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
211c4eade1cSMatthew G. Knepley }
212c4eade1cSMatthew G. Knepley 
213c4eade1cSMatthew G. Knepley #undef __FUNCT__
214c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate"
215c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
216c4eade1cSMatthew G. Knepley {
217c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
218c4eade1cSMatthew G. Knepley 
219c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
220c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
221c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
222c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
223c4eade1cSMatthew G. Knepley }
224c4eade1cSMatthew G. Knepley 
225c4eade1cSMatthew G. Knepley #undef __FUNCT__
226c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge"
227c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
228c4eade1cSMatthew G. Knepley {
229c4eade1cSMatthew G. Knepley   PetscInt d;
230c4eade1cSMatthew G. Knepley 
231c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
232c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
233c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
234c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
235c4eade1cSMatthew G. Knepley   }
236c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
237c4eade1cSMatthew G. Knepley }
238c4eade1cSMatthew G. Knepley 
239c4eade1cSMatthew G. Knepley #undef __FUNCT__
240c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid"
24162a38674SMatthew G. Knepley /*
24262a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
24362a38674SMatthew G. Knepley 
24462a38674SMatthew G. Knepley   Not collective
24562a38674SMatthew G. Knepley 
24662a38674SMatthew G. Knepley   Input Parameters:
24762a38674SMatthew G. Knepley + box - The grid hash object
24862a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
24962a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
25062a38674SMatthew G. Knepley 
25162a38674SMatthew G. Knepley   Level: developer
25262a38674SMatthew G. Knepley 
25362a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
25462a38674SMatthew G. Knepley */
255c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
256c4eade1cSMatthew G. Knepley {
257c4eade1cSMatthew G. Knepley   PetscInt d;
258c4eade1cSMatthew G. Knepley 
259c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
260c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
261c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
262c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
263c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
264c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
265c4eade1cSMatthew G. Knepley     } else {
266c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
267c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
268c4eade1cSMatthew G. Knepley     }
269c4eade1cSMatthew G. Knepley   }
270c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
271c4eade1cSMatthew G. Knepley }
272c4eade1cSMatthew G. Knepley 
273c4eade1cSMatthew G. Knepley #undef __FUNCT__
274c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox"
27562a38674SMatthew G. Knepley /*
27662a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
27762a38674SMatthew G. Knepley 
27862a38674SMatthew G. Knepley   Not collective
27962a38674SMatthew G. Knepley 
28062a38674SMatthew G. Knepley   Input Parameters:
28162a38674SMatthew G. Knepley + box       - The grid hash object
28262a38674SMatthew G. Knepley . numPoints - The number of input points
28362a38674SMatthew G. Knepley - points    - The input point coordinates
28462a38674SMatthew G. Knepley 
28562a38674SMatthew G. Knepley   Output Parameters:
28662a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
28762a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
28862a38674SMatthew G. Knepley 
28962a38674SMatthew G. Knepley   Level: developer
29062a38674SMatthew G. Knepley 
29162a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
29262a38674SMatthew G. Knepley */
2931c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
294c4eade1cSMatthew G. Knepley {
295c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
296c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
297c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
298c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
299c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
300c4eade1cSMatthew G. Knepley   PetscInt         d, p;
301c4eade1cSMatthew G. Knepley 
302c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
303c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
304c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
3051c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
306c4eade1cSMatthew G. Knepley 
3071c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
308c4eade1cSMatthew 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",
3091c6dfc3eSMatthew G. Knepley                                              p, PetscRealPart(points[p*dim+0]), dim > 1 ? PetscRealPart(points[p*dim+1]) : 0.0, dim > 2 ? PetscRealPart(points[p*dim+2]) : 0.0);
310c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
311c4eade1cSMatthew G. Knepley     }
312c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
313c4eade1cSMatthew G. Knepley   }
314c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
315c4eade1cSMatthew G. Knepley }
316c4eade1cSMatthew G. Knepley 
317c4eade1cSMatthew G. Knepley #undef __FUNCT__
318c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy"
319c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
320c4eade1cSMatthew G. Knepley {
321c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
322c4eade1cSMatthew G. Knepley 
323c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
324c4eade1cSMatthew G. Knepley   if (*box) {
325c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
326c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
327c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
328c4eade1cSMatthew G. Knepley   }
329c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
330c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
331c4eade1cSMatthew G. Knepley }
332c4eade1cSMatthew G. Knepley 
333cafe43deSMatthew G. Knepley #undef __FUNCT__
334cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexLocatePoint_Internal"
335cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
336cafe43deSMatthew G. Knepley {
337cafe43deSMatthew G. Knepley   PetscInt       coneSize;
338cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
339cafe43deSMatthew G. Knepley 
340cafe43deSMatthew G. Knepley   PetscFunctionBegin;
341cafe43deSMatthew G. Knepley   switch (dim) {
342cafe43deSMatthew G. Knepley   case 2:
343cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
344cafe43deSMatthew G. Knepley     switch (coneSize) {
345cafe43deSMatthew G. Knepley     case 3:
346cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
347cafe43deSMatthew G. Knepley       break;
348cafe43deSMatthew G. Knepley     case 4:
349cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
350cafe43deSMatthew G. Knepley       break;
351cafe43deSMatthew G. Knepley     default:
352cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
353cafe43deSMatthew G. Knepley     }
354cafe43deSMatthew G. Knepley     break;
355cafe43deSMatthew G. Knepley   case 3:
356cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
357cafe43deSMatthew G. Knepley     switch (coneSize) {
358cafe43deSMatthew G. Knepley     case 4:
359cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
360cafe43deSMatthew G. Knepley       break;
361cafe43deSMatthew G. Knepley     case 6:
362cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
363cafe43deSMatthew G. Knepley       break;
364cafe43deSMatthew G. Knepley     default:
365cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
366cafe43deSMatthew G. Knepley     }
367cafe43deSMatthew G. Knepley     break;
368cafe43deSMatthew G. Knepley   default:
369cafe43deSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
370cafe43deSMatthew G. Knepley   }
371cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
372cafe43deSMatthew G. Knepley }
373cafe43deSMatthew G. Knepley 
374cafe43deSMatthew G. Knepley #undef __FUNCT__
37562a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Internal"
37662a38674SMatthew G. Knepley /*
37762a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
37862a38674SMatthew G. Knepley */
37962a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
38062a38674SMatthew G. Knepley {
38162a38674SMatthew G. Knepley   PetscInt       coneSize;
38262a38674SMatthew G. Knepley   PetscErrorCode ierr;
38362a38674SMatthew G. Knepley 
38462a38674SMatthew G. Knepley   PetscFunctionBegin;
38562a38674SMatthew G. Knepley   switch (dim) {
38662a38674SMatthew G. Knepley   case 2:
38762a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
38862a38674SMatthew G. Knepley     switch (coneSize) {
38962a38674SMatthew G. Knepley     case 3:
39062a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
39162a38674SMatthew G. Knepley       break;
39262a38674SMatthew G. Knepley #if 0
39362a38674SMatthew G. Knepley     case 4:
39462a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
39562a38674SMatthew G. Knepley       break;
39662a38674SMatthew G. Knepley #endif
39762a38674SMatthew G. Knepley     default:
39862a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
39962a38674SMatthew G. Knepley     }
40062a38674SMatthew G. Knepley     break;
40162a38674SMatthew G. Knepley #if 0
40262a38674SMatthew G. Knepley   case 3:
40362a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
40462a38674SMatthew G. Knepley     switch (coneSize) {
40562a38674SMatthew G. Knepley     case 4:
40662a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
40762a38674SMatthew G. Knepley       break;
40862a38674SMatthew G. Knepley     case 6:
40962a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
41062a38674SMatthew G. Knepley       break;
41162a38674SMatthew G. Knepley     default:
41262a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
41362a38674SMatthew G. Knepley     }
41462a38674SMatthew G. Knepley     break;
41562a38674SMatthew G. Knepley #endif
41662a38674SMatthew G. Knepley   default:
41762a38674SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for mesh dimension %D", dim);
41862a38674SMatthew G. Knepley   }
41962a38674SMatthew G. Knepley   PetscFunctionReturn(0);
42062a38674SMatthew G. Knepley }
42162a38674SMatthew G. Knepley 
42262a38674SMatthew G. Knepley #undef __FUNCT__
423cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGridHash_Internal"
42462a38674SMatthew G. Knepley /*
42562a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
42662a38674SMatthew G. Knepley 
42762a38674SMatthew G. Knepley   Collective on DM
42862a38674SMatthew G. Knepley 
42962a38674SMatthew G. Knepley   Input Parameter:
43062a38674SMatthew G. Knepley . dm - The Plex
43162a38674SMatthew G. Knepley 
43262a38674SMatthew G. Knepley   Output Parameter:
43362a38674SMatthew G. Knepley . localBox - The grid hash object
43462a38674SMatthew G. Knepley 
43562a38674SMatthew G. Knepley   Level: developer
43662a38674SMatthew G. Knepley 
43762a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
43862a38674SMatthew G. Knepley */
439cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
440cafe43deSMatthew G. Knepley {
441cafe43deSMatthew G. Knepley   MPI_Comm           comm;
442cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
443cafe43deSMatthew G. Knepley   Vec                coordinates;
444cafe43deSMatthew G. Knepley   PetscSection       coordSection;
445cafe43deSMatthew G. Knepley   Vec                coordsLocal;
446cafe43deSMatthew G. Knepley   const PetscScalar *coords;
447722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
448cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
4491d0c6c94SMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, cMax, c, i;
450cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
451cafe43deSMatthew G. Knepley 
452cafe43deSMatthew G. Knepley   PetscFunctionBegin;
453cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
454cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
455cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
4565b3353d8SMatthew G. Knepley   if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D");
457cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
458cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
459cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
460cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
461cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
462cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
463cafe43deSMatthew G. Knepley #if 0
464cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
465b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
466b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
467cafe43deSMatthew G. Knepley #endif
468cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
469cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
470cafe43deSMatthew G. Knepley   /* Create label */
471cafe43deSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4721d0c6c94SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
4731d0c6c94SMatthew G. Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
474cafe43deSMatthew G. Knepley   ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr);
475cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
476722d0f5cSMatthew G. Knepley   /* Compute boxes which overlap each cell: http://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
477cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
478cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
47938353de4SMatthew G. Knepley   ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr);
480cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
481cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
482cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
48338353de4SMatthew G. Knepley     PetscInt         csize   = 0;
484cafe43deSMatthew G. Knepley     PetscScalar      point[3];
485cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
486cafe43deSMatthew G. Knepley 
487cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
48838353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
48938353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
490722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
49138353de4SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);}
492cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
493cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
4942291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
495cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
496cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
497cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
498cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
499cafe43deSMatthew G. Knepley       }
500cafe43deSMatthew G. Knepley     }
501fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
502cafe43deSMatthew 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]) {
503cafe43deSMatthew 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]) {
504cafe43deSMatthew 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]) {
505cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
506cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
507fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
508cafe43deSMatthew G. Knepley 
509fea14342SMatthew G. Knepley           /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
510cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
511cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
512cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
513cafe43deSMatthew G. Knepley 
514cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
515cafe43deSMatthew G. Knepley                 if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
516cafe43deSMatthew G. Knepley               }
517cafe43deSMatthew G. Knepley             }
518cafe43deSMatthew G. Knepley           }
519fea14342SMatthew G. Knepley           /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */
520fea14342SMatthew G. Knepley           for (edge = 0; edge < dim+1; ++edge) {
521fea14342SMatthew G. Knepley             PetscReal segA[6], segB[6];
522fea14342SMatthew G. Knepley 
523fea14342SMatthew 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]);}
524fea14342SMatthew G. Knepley             for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) {
5259a128ed2SMatthew G. Knepley               if (dim > 2) {segB[2]     = PetscRealPart(point[2]);
5269a128ed2SMatthew G. Knepley                             segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];}
527fea14342SMatthew G. Knepley               for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) {
5289a128ed2SMatthew G. Knepley                 if (dim > 1) {segB[1]     = PetscRealPart(point[1]);
5299a128ed2SMatthew G. Knepley                               segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];}
530fea14342SMatthew G. Knepley                 for (ii = 0; ii < 2; ++ii) {
531fea14342SMatthew G. Knepley                   PetscBool intersects;
532fea14342SMatthew G. Knepley 
5339a128ed2SMatthew G. Knepley                   segB[0]     = PetscRealPart(point[0]);
5349a128ed2SMatthew G. Knepley                   segB[dim+0] = PetscRealPart(point[0]) + ii*h[0];
535fea14342SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
536fea14342SMatthew G. Knepley                   if (intersects) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;}
537cafe43deSMatthew G. Knepley                 }
538cafe43deSMatthew G. Knepley               }
539cafe43deSMatthew G. Knepley             }
540cafe43deSMatthew G. Knepley           }
541fea14342SMatthew G. Knepley         }
542fea14342SMatthew G. Knepley       }
543fea14342SMatthew G. Knepley     }
544fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
545fea14342SMatthew G. Knepley   }
546722d0f5cSMatthew G. Knepley   ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr);
547cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
548cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
549cafe43deSMatthew G. Knepley   *localBox = lbox;
550cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
551cafe43deSMatthew G. Knepley }
552cafe43deSMatthew G. Knepley 
553cafe43deSMatthew G. Knepley #undef __FUNCT__
554ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex"
55562a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
556ccd2543fSMatthew G Knepley {
557cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
558953fc75cSMatthew G. Knepley   PetscBool       hash = mesh->useHashLocation;
5593a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
5601318edbeSMatthew G. Knepley   PetscInt        dim, cStart, cEnd, cMax, numCells, c;
561cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
5623a93e3b7SToby Isaac   PetscSFNode    *cells;
563ccd2543fSMatthew G Knepley   PetscScalar    *a;
5643a93e3b7SToby Isaac   PetscMPIInt     result;
565ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
566ccd2543fSMatthew G Knepley 
567ccd2543fSMatthew G Knepley   PetscFunctionBegin;
568080342d1SMatthew 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.");
569cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
570cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
5713a93e3b7SToby Isaac   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr);
5723a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
573cafe43deSMatthew 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);
574ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
575ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
576ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
577ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
578ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
579ccd2543fSMatthew G Knepley   numPoints /= bs;
580785e854fSJed Brown   ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
581953fc75cSMatthew G. Knepley   if (hash) {
582ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
583cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
584cafe43deSMatthew G. Knepley     /* Send points to correct process */
585cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
586cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
587cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
588953fc75cSMatthew G. Knepley   }
5893a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
590ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
591953fc75cSMatthew G. Knepley     PetscInt           dbin[3], bin, cell = -1, cellOffset;
592ccd2543fSMatthew G Knepley 
593e9b685f5SMatthew G. Knepley     cells[p].rank  = 0;
594e9b685f5SMatthew G. Knepley     cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
595953fc75cSMatthew G. Knepley     if (hash) {
596cafe43deSMatthew G. Knepley       ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
597cafe43deSMatthew G. Knepley       /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
598cafe43deSMatthew G. Knepley       ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
599cafe43deSMatthew G. Knepley       ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
600cafe43deSMatthew G. Knepley       for (c = cellOffset; c < cellOffset + numCells; ++c) {
601cafe43deSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
6023a93e3b7SToby Isaac         if (cell >= 0) {
6033a93e3b7SToby Isaac           cells[p].rank = 0;
6043a93e3b7SToby Isaac           cells[p].index = cell;
6053a93e3b7SToby Isaac           numFound++;
6063a93e3b7SToby Isaac           break;
607ccd2543fSMatthew G Knepley         }
6083a93e3b7SToby Isaac       }
609953fc75cSMatthew G. Knepley     } else {
610953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
611953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
6123a93e3b7SToby Isaac         if (cell >= 0) {
6133a93e3b7SToby Isaac           cells[p].rank = 0;
6143a93e3b7SToby Isaac           cells[p].index = cell;
6153a93e3b7SToby Isaac           numFound++;
6163a93e3b7SToby Isaac           break;
617953fc75cSMatthew G. Knepley         }
618953fc75cSMatthew G. Knepley       }
6193a93e3b7SToby Isaac     }
620ccd2543fSMatthew G Knepley   }
621953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
62262a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
62362a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
62462a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
62562a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
626b716b415SMatthew G. Knepley       PetscInt           dbin[3], bin, cellOffset, d;
62762a38674SMatthew G. Knepley 
628e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
62962a38674SMatthew G. Knepley         ++numFound;
63062a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
63162a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
63262a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
63362a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
63462a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
635b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
63662a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
63762a38674SMatthew G. Knepley           if (dist < distMax) {
63862a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
63962a38674SMatthew G. Knepley             cells[p].rank  = 0;
64062a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
64162a38674SMatthew G. Knepley             distMax = dist;
64262a38674SMatthew G. Knepley             break;
64362a38674SMatthew G. Knepley           }
64462a38674SMatthew G. Knepley         }
64562a38674SMatthew G. Knepley       }
64662a38674SMatthew G. Knepley     }
64762a38674SMatthew G. Knepley   }
64862a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
649cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
6502d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
6513a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
6523a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
6533a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
6543a93e3b7SToby Isaac         if (numFound < p) {
6553a93e3b7SToby Isaac           cells[numFound] = cells[p];
6563a93e3b7SToby Isaac         }
6573a93e3b7SToby Isaac         found[numFound++] = p;
6583a93e3b7SToby Isaac       }
6593a93e3b7SToby Isaac     }
6603a93e3b7SToby Isaac   }
66162a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
6623a93e3b7SToby Isaac   ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
663ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
664ccd2543fSMatthew G Knepley }
665ccd2543fSMatthew G Knepley 
666ccd2543fSMatthew G Knepley #undef __FUNCT__
667741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D"
668741bfc07SMatthew G. Knepley /*@C
669741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
670741bfc07SMatthew G. Knepley 
671741bfc07SMatthew G. Knepley   Not collective
672741bfc07SMatthew G. Knepley 
673741bfc07SMatthew G. Knepley   Input Parameter:
674741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
675741bfc07SMatthew G. Knepley 
676741bfc07SMatthew G. Knepley   Output Parameters:
677741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x
678741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
679741bfc07SMatthew G. Knepley 
680741bfc07SMatthew G. Knepley   Level: developer
681741bfc07SMatthew G. Knepley 
682741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
683741bfc07SMatthew G. Knepley @*/
684741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
68517fe8556SMatthew G. Knepley {
68617fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
68717fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
6888b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
68917fe8556SMatthew G. Knepley 
69017fe8556SMatthew G. Knepley   PetscFunctionBegin;
6911c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
6921c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
69317fe8556SMatthew G. Knepley   coords[0] = 0.0;
6947f07f362SMatthew G. Knepley   coords[1] = r;
69517fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
69617fe8556SMatthew G. Knepley }
69717fe8556SMatthew G. Knepley 
69817fe8556SMatthew G. Knepley #undef __FUNCT__
699741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto1D"
700741bfc07SMatthew G. Knepley /*@C
701741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
70228dbe442SToby Isaac 
703741bfc07SMatthew G. Knepley   Not collective
70428dbe442SToby Isaac 
705741bfc07SMatthew G. Knepley   Input Parameter:
706741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
707741bfc07SMatthew G. Knepley 
708741bfc07SMatthew G. Knepley   Output Parameters:
709741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x and z
710741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
711741bfc07SMatthew G. Knepley 
712741bfc07SMatthew 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
713741bfc07SMatthew G. Knepley 
714741bfc07SMatthew G. Knepley   Level: developer
715741bfc07SMatthew G. Knepley 
716741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
717741bfc07SMatthew G. Knepley @*/
718741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
71928dbe442SToby Isaac {
72028dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
72128dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
72228dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
72328dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
72428dbe442SToby Isaac   PetscReal      rinv = 1. / r;
72528dbe442SToby Isaac   PetscFunctionBegin;
72628dbe442SToby Isaac 
72728dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
72828dbe442SToby Isaac   if (x > 0.) {
72928dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
73028dbe442SToby Isaac 
73128dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
73228dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
73328dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
73428dbe442SToby Isaac   }
73528dbe442SToby Isaac   else {
73628dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
73728dbe442SToby Isaac 
73828dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
73928dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
74028dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
74128dbe442SToby Isaac   }
74228dbe442SToby Isaac   coords[0] = 0.0;
74328dbe442SToby Isaac   coords[1] = r;
74428dbe442SToby Isaac   PetscFunctionReturn(0);
74528dbe442SToby Isaac }
74628dbe442SToby Isaac 
74728dbe442SToby Isaac #undef __FUNCT__
748741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D"
749741bfc07SMatthew G. Knepley /*@
750741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto2D - Rewrite coordinates to be the 2D projection of the 3D coordinates
751741bfc07SMatthew G. Knepley 
752741bfc07SMatthew G. Knepley   Not collective
753741bfc07SMatthew G. Knepley 
754741bfc07SMatthew G. Knepley   Input Parameter:
755741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
756741bfc07SMatthew G. Knepley 
757741bfc07SMatthew G. Knepley   Output Parameters:
758741bfc07SMatthew G. Knepley + coords - The new y- and z-coordinates, and 0 for x
759741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
760741bfc07SMatthew G. Knepley 
761741bfc07SMatthew G. Knepley   Level: developer
762741bfc07SMatthew G. Knepley 
763741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
764741bfc07SMatthew G. Knepley @*/
765741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
766ccd2543fSMatthew G Knepley {
7671ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
76899dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
7694a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
770ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
77199dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
772ccd2543fSMatthew G Knepley 
773ccd2543fSMatthew G Knepley   PetscFunctionBegin;
774ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
775ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
7761ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
7771ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
778ccd2543fSMatthew G Knepley   }
779ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
780ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
781ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
7828b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
783ccd2543fSMatthew G Knepley   n[0] /= norm;
784ccd2543fSMatthew G Knepley   n[1] /= norm;
785ccd2543fSMatthew G Knepley   n[2] /= norm;
786ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
787ccd2543fSMatthew G Knepley 
788ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
789ccd2543fSMatthew G Knepley 
790ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
791ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
792ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
793ccd2543fSMatthew G Knepley 
794ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
795ccd2543fSMatthew G Knepley   */
7968b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
79773868372SMatthew G. Knepley   /* Check for n = z */
79873868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
7997df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
8007df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
80199dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
80299dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
8037df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
80473868372SMatthew G. Knepley     }
80599dec3a6SMatthew G. Knepley     coords[0] = 0.0;
80699dec3a6SMatthew G. Knepley     coords[1] = 0.0;
8077df32b8bSSanderA     coords[2] = x1[0];
8087df32b8bSSanderA     coords[3] = x1[1] * s;
8097df32b8bSSanderA     coords[4] = x2[0];
8107df32b8bSSanderA     coords[5] = x2[1] * s;
8117df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
8127df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
8137df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
81473868372SMatthew G. Knepley     PetscFunctionReturn(0);
81573868372SMatthew G. Knepley   }
816da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
817ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
818ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
819ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
820ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
821ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
822ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
823ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
824ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
825ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
826ccd2543fSMatthew G Knepley     }
827ccd2543fSMatthew G Knepley   }
82848120919SToby Isaac   if (PetscAbsReal(x1p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
82948120919SToby Isaac   if (PetscAbsReal(x2p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
830ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
83199dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
83299dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
83399dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
83499dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
83599dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
83699dec3a6SMatthew G. Knepley       }
83799dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
83899dec3a6SMatthew G. Knepley     }
83999dec3a6SMatthew G. Knepley   }
840ccd2543fSMatthew G Knepley   coords[0] = 0.0;
841ccd2543fSMatthew G Knepley   coords[1] = 0.0;
842ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
843ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
844ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
845ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
8467f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
8477f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
8487f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
8497f07f362SMatthew G. Knepley       PetscReal tmp;
8507f07f362SMatthew G. Knepley 
8517f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
8527f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
8537f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
8547f07f362SMatthew G. Knepley     }
8557f07f362SMatthew G. Knepley   }
856ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
857ccd2543fSMatthew G Knepley }
858ccd2543fSMatthew G Knepley 
859ccd2543fSMatthew G Knepley #undef __FUNCT__
860834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal"
8616322fe33SJed Brown PETSC_UNUSED
862834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
863834e62ceSMatthew G. Knepley {
864834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
865834e62ceSMatthew G. Knepley 
866834e62ceSMatthew G. Knepley    |  1  1  1 |
867834e62ceSMatthew G. Knepley    | x0 x1 x2 |
868834e62ceSMatthew G. Knepley    | y0 y1 y2 |
869834e62ceSMatthew G. Knepley 
870834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
871834e62ceSMatthew G. Knepley 
872834e62ceSMatthew G. Knepley    | x1 x2 |
873834e62ceSMatthew G. Knepley    | y1 y2 |
874834e62ceSMatthew G. Knepley   */
875834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
876834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
877834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
878834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
87986623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
880923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
881834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
8823bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
883834e62ceSMatthew G. Knepley }
884834e62ceSMatthew G. Knepley 
885834e62ceSMatthew G. Knepley #undef __FUNCT__
886834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal"
887834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
888834e62ceSMatthew G. Knepley {
889923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
890834e62ceSMatthew G. Knepley   *vol *= 0.5;
891834e62ceSMatthew G. Knepley }
892834e62ceSMatthew G. Knepley 
893834e62ceSMatthew G. Knepley #undef __FUNCT__
894834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal"
8956322fe33SJed Brown PETSC_UNUSED
896834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
897834e62ceSMatthew G. Knepley {
898834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
899834e62ceSMatthew G. Knepley 
900834e62ceSMatthew G. Knepley    |  1  1  1  1 |
901834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
902834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
903834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
904834e62ceSMatthew G. Knepley 
905834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
906834e62ceSMatthew G. Knepley 
907834e62ceSMatthew G. Knepley    | x1 x2 x3 |
908834e62ceSMatthew G. Knepley    | y1 y2 y3 |
909834e62ceSMatthew G. Knepley    | z1 z2 z3 |
910834e62ceSMatthew G. Knepley   */
911834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
912834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
913834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
914834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
915834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
916834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
917834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
918923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
919b7ad821dSMatthew G. Knepley   *vol = -0.16666666666666666666666*detM;
9203bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
921834e62ceSMatthew G. Knepley }
922834e62ceSMatthew G. Knepley 
923834e62ceSMatthew G. Knepley #undef __FUNCT__
9240ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal"
9250ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
9260ec8681fSMatthew G. Knepley {
927923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
928b7ad821dSMatthew G. Knepley   *vol *= -0.16666666666666666666666;
9290ec8681fSMatthew G. Knepley }
9300ec8681fSMatthew G. Knepley 
9310ec8681fSMatthew G. Knepley #undef __FUNCT__
932cb92db44SToby Isaac #define __FUNCT__ "DMPlexComputePointGeometry_Internal"
933cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
934cb92db44SToby Isaac {
935cb92db44SToby Isaac   PetscSection   coordSection;
936cb92db44SToby Isaac   Vec            coordinates;
937cb92db44SToby Isaac   const PetscScalar *coords;
938cb92db44SToby Isaac   PetscInt       dim, d, off;
939cb92db44SToby Isaac   PetscErrorCode ierr;
940cb92db44SToby Isaac 
941cb92db44SToby Isaac   PetscFunctionBegin;
942cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
943cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
944cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
945cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
946cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
947cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
948cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
949cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
950cb92db44SToby Isaac   *detJ = 1.;
951cb92db44SToby Isaac   if (J) {
952cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
953cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
954cb92db44SToby Isaac     if (invJ) {
955cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
956cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
957cb92db44SToby Isaac     }
958cb92db44SToby Isaac   }
959cb92db44SToby Isaac   PetscFunctionReturn(0);
960cb92db44SToby Isaac }
961cb92db44SToby Isaac 
962cb92db44SToby Isaac #undef __FUNCT__
96317fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal"
96417fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
96517fe8556SMatthew G. Knepley {
96617fe8556SMatthew G. Knepley   PetscSection   coordSection;
96717fe8556SMatthew G. Knepley   Vec            coordinates;
968a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9698bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
97017fe8556SMatthew G. Knepley   PetscErrorCode ierr;
97117fe8556SMatthew G. Knepley 
97217fe8556SMatthew G. Knepley   PetscFunctionBegin;
97317fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
97469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
9758bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
9768bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
97717fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9788bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
979adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
9807f07f362SMatthew G. Knepley   *detJ = 0.0;
98128dbe442SToby Isaac   if (numCoords == 6) {
98228dbe442SToby Isaac     const PetscInt dim = 3;
98328dbe442SToby Isaac     PetscReal      R[9], J0;
98428dbe442SToby Isaac 
98528dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
986741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
98728dbe442SToby Isaac     if (J)    {
98828dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
98928dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
99028dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
99128dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
99228dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
99328dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
994adac9986SMatthew G. Knepley     }
99528dbe442SToby Isaac   } else if (numCoords == 4) {
9967f07f362SMatthew G. Knepley     const PetscInt dim = 2;
9977f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
9987f07f362SMatthew G. Knepley 
9997f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1000741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
100117fe8556SMatthew G. Knepley     if (J)    {
10027f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
10037f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
10047f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
1005923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1006923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1007adac9986SMatthew G. Knepley     }
10087f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
10097f07f362SMatthew G. Knepley     const PetscInt dim = 1;
10107f07f362SMatthew G. Knepley 
10117f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
10127f07f362SMatthew G. Knepley     if (J)    {
10137f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
101417fe8556SMatthew G. Knepley       *detJ = J[0];
10153bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
10163bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
1017adac9986SMatthew G. Knepley     }
1018796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
101917fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
102017fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
102117fe8556SMatthew G. Knepley }
102217fe8556SMatthew G. Knepley 
102317fe8556SMatthew G. Knepley #undef __FUNCT__
1024ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal"
1025ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1026ccd2543fSMatthew G Knepley {
1027ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1028ccd2543fSMatthew G Knepley   Vec            coordinates;
1029a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10307f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
1031ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1032ccd2543fSMatthew G Knepley 
1033ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1034ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
103569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1036ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
10377f07f362SMatthew G. Knepley   *detJ = 0.0;
1038ccd2543fSMatthew G Knepley   if (numCoords == 9) {
10397f07f362SMatthew G. Knepley     const PetscInt dim = 3;
10407f07f362SMatthew 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};
10417f07f362SMatthew G. Knepley 
10427f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1043741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
10447f07f362SMatthew G. Knepley     if (J)    {
1045b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1046b7ad821dSMatthew G. Knepley 
1047b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1048b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1049b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1050ccd2543fSMatthew G Knepley         }
10517f07f362SMatthew G. Knepley       }
10523bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1053923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
10547f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
10557f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
10567f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
10577f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
10587f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
10597f07f362SMatthew G. Knepley           }
10607f07f362SMatthew G. Knepley         }
10617f07f362SMatthew G. Knepley       }
10623bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
10637f07f362SMatthew G. Knepley     }
1064923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
10657f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
10667f07f362SMatthew G. Knepley     const PetscInt dim = 2;
10677f07f362SMatthew G. Knepley 
10687f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1069ccd2543fSMatthew G Knepley     if (J)    {
1070ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1071ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1072ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1073ccd2543fSMatthew G Knepley         }
1074ccd2543fSMatthew G Knepley       }
10753bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1076923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1077ccd2543fSMatthew G Knepley     }
1078923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1079796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1080ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1081ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1082ccd2543fSMatthew G Knepley }
1083ccd2543fSMatthew G Knepley 
1084ccd2543fSMatthew G Knepley #undef __FUNCT__
1085ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal"
1086dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1087ccd2543fSMatthew G Knepley {
1088ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1089ccd2543fSMatthew G Knepley   Vec            coordinates;
1090a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10910d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1092ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1093ccd2543fSMatthew G Knepley 
1094ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1095ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
109669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10970d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10980d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
109999dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
110071f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1101dfccc68fSToby Isaac   if (!Nq) {
11027f07f362SMatthew G. Knepley     *detJ = 0.0;
110399dec3a6SMatthew G. Knepley     if (numCoords == 12) {
110499dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
110599dec3a6SMatthew 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};
110699dec3a6SMatthew G. Knepley 
1107dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1108741bfc07SMatthew G. Knepley       ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
110999dec3a6SMatthew G. Knepley       if (J)    {
111099dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
111199dec3a6SMatthew G. Knepley 
111299dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
111399dec3a6SMatthew G. Knepley           J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
111499dec3a6SMatthew G. Knepley           J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
111599dec3a6SMatthew G. Knepley         }
11163bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1117923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
111899dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
111999dec3a6SMatthew G. Knepley           for (f = 0; f < dim; f++) {
112099dec3a6SMatthew G. Knepley             J[d*dim+f] = 0.0;
112199dec3a6SMatthew G. Knepley             for (g = 0; g < dim; g++) {
112299dec3a6SMatthew G. Knepley               J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
112399dec3a6SMatthew G. Knepley             }
112499dec3a6SMatthew G. Knepley           }
112599dec3a6SMatthew G. Knepley         }
11263bc0b13bSBarry Smith         ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
112799dec3a6SMatthew G. Knepley       }
1128923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
112971f58de1SToby Isaac     } else if (numCoords == 8) {
113099dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
113199dec3a6SMatthew G. Knepley 
1132dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1133ccd2543fSMatthew G Knepley       if (J)    {
1134ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
113599dec3a6SMatthew G. Knepley           J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
113699dec3a6SMatthew G. Knepley           J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1137ccd2543fSMatthew G Knepley         }
11383bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1139923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1140ccd2543fSMatthew G Knepley       }
1141923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1142796f034aSJed Brown     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1143dfccc68fSToby Isaac   } else {
1144dfccc68fSToby Isaac     const PetscInt Nv = 4;
1145dfccc68fSToby Isaac     const PetscInt dimR = 2;
1146dfccc68fSToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
1147dfccc68fSToby Isaac     PetscReal zOrder[12];
1148dfccc68fSToby Isaac     PetscReal zCoeff[12];
1149dfccc68fSToby Isaac     PetscInt  i, j, k, l, dim;
1150dfccc68fSToby Isaac 
1151dfccc68fSToby Isaac     if (numCoords == 12) {
1152dfccc68fSToby Isaac       dim = 3;
1153dfccc68fSToby Isaac     } else if (numCoords == 8) {
1154dfccc68fSToby Isaac       dim = 2;
1155dfccc68fSToby Isaac     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1156dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1157dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1158dfccc68fSToby Isaac 
1159dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1160dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1161dfccc68fSToby Isaac       }
1162dfccc68fSToby Isaac     }
1163dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1164dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1165dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1166dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1167dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1168dfccc68fSToby Isaac     }
1169dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1170dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1171dfccc68fSToby Isaac 
1172dfccc68fSToby Isaac       if (v) {
1173dfccc68fSToby Isaac         PetscReal extPoint[4];
1174dfccc68fSToby Isaac 
1175dfccc68fSToby Isaac         extPoint[0] = 1.;
1176dfccc68fSToby Isaac         extPoint[1] = xi;
1177dfccc68fSToby Isaac         extPoint[2] = eta;
1178dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1179dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1180dfccc68fSToby Isaac           PetscReal val = 0.;
1181dfccc68fSToby Isaac 
1182dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1183dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1184dfccc68fSToby Isaac           }
1185dfccc68fSToby Isaac           v[i * dim + j] = val;
1186dfccc68fSToby Isaac         }
1187dfccc68fSToby Isaac       }
1188dfccc68fSToby Isaac       if (J) {
1189dfccc68fSToby Isaac         PetscReal extJ[8];
1190dfccc68fSToby Isaac 
1191dfccc68fSToby Isaac         extJ[0] = 0.;
1192dfccc68fSToby Isaac         extJ[1] = 0.;
1193dfccc68fSToby Isaac         extJ[2] = 1.;
1194dfccc68fSToby Isaac         extJ[3] = 0.;
1195dfccc68fSToby Isaac         extJ[4] = 0.;
1196dfccc68fSToby Isaac         extJ[5] = 1.;
1197dfccc68fSToby Isaac         extJ[6] = eta;
1198dfccc68fSToby Isaac         extJ[7] = xi;
1199dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1200dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1201dfccc68fSToby Isaac             PetscReal val = 0.;
1202dfccc68fSToby Isaac 
1203dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1204dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1205dfccc68fSToby Isaac             }
1206dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1207dfccc68fSToby Isaac           }
1208dfccc68fSToby Isaac         }
1209dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1210dfccc68fSToby Isaac           PetscReal x, y, z;
1211dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1212dfccc68fSToby Isaac           PetscReal norm;
1213dfccc68fSToby Isaac 
1214dfccc68fSToby Isaac           x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1215dfccc68fSToby Isaac           y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1216dfccc68fSToby Isaac           z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1217dfccc68fSToby Isaac           norm = PetscSqrtReal(x * x + y * y + z * z);
1218dfccc68fSToby Isaac           iJ[2] = x / norm;
1219dfccc68fSToby Isaac           iJ[5] = y / norm;
1220dfccc68fSToby Isaac           iJ[8] = z / norm;
1221dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1222dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1223dfccc68fSToby Isaac         } else {
1224dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1225dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1226dfccc68fSToby Isaac         }
1227dfccc68fSToby Isaac       }
1228dfccc68fSToby Isaac     }
1229dfccc68fSToby Isaac   }
123099dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1231ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1232ccd2543fSMatthew G Knepley }
1233ccd2543fSMatthew G Knepley 
1234ccd2543fSMatthew G Knepley #undef __FUNCT__
1235ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal"
1236ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1237ccd2543fSMatthew G Knepley {
1238ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1239ccd2543fSMatthew G Knepley   Vec            coordinates;
1240a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1241ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
124299dec3a6SMatthew G. Knepley   PetscInt       d;
1243ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1244ccd2543fSMatthew G Knepley 
1245ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1246ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
124769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1248ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
12497f07f362SMatthew G. Knepley   *detJ = 0.0;
12507f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1251ccd2543fSMatthew G Knepley   if (J)    {
1252ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1253f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1254f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1255f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1256f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1257ccd2543fSMatthew G Knepley     }
12583bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1259923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1260ccd2543fSMatthew G Knepley   }
1261923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1262ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1263ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1264ccd2543fSMatthew G Knepley }
1265ccd2543fSMatthew G Knepley 
1266ccd2543fSMatthew G Knepley #undef __FUNCT__
1267ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal"
1268dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1269ccd2543fSMatthew G Knepley {
1270ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1271ccd2543fSMatthew G Knepley   Vec            coordinates;
1272a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1273ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1274ccd2543fSMatthew G Knepley   PetscInt       d;
1275ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1276ccd2543fSMatthew G Knepley 
1277ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1278ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
127969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1280ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1281dfccc68fSToby Isaac   if (!Nq) {
12827f07f362SMatthew G. Knepley     *detJ = 0.0;
1283dfccc68fSToby Isaac     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1284ccd2543fSMatthew G Knepley     if (J)    {
1285ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1286f0df753eSMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1287f0df753eSMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1288f0df753eSMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1289ccd2543fSMatthew G Knepley       }
12903bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1291923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1292ccd2543fSMatthew G Knepley     }
1293923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1294dfccc68fSToby Isaac   } else {
1295dfccc68fSToby Isaac     const PetscInt Nv = 8;
1296dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1297dfccc68fSToby Isaac     const PetscInt dim = 3;
1298dfccc68fSToby Isaac     const PetscInt dimR = 3;
1299dfccc68fSToby Isaac     PetscReal zOrder[24];
1300dfccc68fSToby Isaac     PetscReal zCoeff[24];
1301dfccc68fSToby Isaac     PetscInt  i, j, k, l;
1302dfccc68fSToby Isaac 
1303dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1304dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1305dfccc68fSToby Isaac 
1306dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1307dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1308dfccc68fSToby Isaac       }
1309dfccc68fSToby Isaac     }
1310dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1311dfccc68fSToby 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]);
1312dfccc68fSToby 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]);
1313dfccc68fSToby 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]);
1314dfccc68fSToby 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]);
1315dfccc68fSToby 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]);
1316dfccc68fSToby 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]);
1317dfccc68fSToby 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]);
1318dfccc68fSToby 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]);
1319dfccc68fSToby Isaac     }
1320dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1321dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1322dfccc68fSToby Isaac 
1323dfccc68fSToby Isaac       if (v) {
132491d2b7ceSToby Isaac         PetscReal extPoint[8];
1325dfccc68fSToby Isaac 
1326dfccc68fSToby Isaac         extPoint[0] = 1.;
1327dfccc68fSToby Isaac         extPoint[1] = xi;
1328dfccc68fSToby Isaac         extPoint[2] = eta;
1329dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1330dfccc68fSToby Isaac         extPoint[4] = theta;
1331dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1332dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1333dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1334dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1335dfccc68fSToby Isaac           PetscReal val = 0.;
1336dfccc68fSToby Isaac 
1337dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1338dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1339dfccc68fSToby Isaac           }
1340dfccc68fSToby Isaac           v[i * dim + j] = val;
1341dfccc68fSToby Isaac         }
1342dfccc68fSToby Isaac       }
1343dfccc68fSToby Isaac       if (J) {
1344dfccc68fSToby Isaac         PetscReal extJ[24];
1345dfccc68fSToby Isaac 
1346dfccc68fSToby Isaac         extJ[0]  = 0.         ; extJ[1]  = 0.        ; extJ[2]  = 0.      ;
1347dfccc68fSToby Isaac         extJ[3]  = 1.         ; extJ[4]  = 0.        ; extJ[5]  = 0.      ;
1348dfccc68fSToby Isaac         extJ[6]  = 0.         ; extJ[7]  = 1.        ; extJ[8]  = 0.      ;
1349dfccc68fSToby Isaac         extJ[9]  = eta        ; extJ[10] = xi        ; extJ[11] = 0.      ;
1350dfccc68fSToby Isaac         extJ[12] = 0.         ; extJ[13] = 0.        ; extJ[14] = 1.      ;
1351dfccc68fSToby Isaac         extJ[15] = theta      ; extJ[16] = 0.        ; extJ[17] = xi      ;
1352dfccc68fSToby Isaac         extJ[18] = 0.         ; extJ[19] = theta     ; extJ[20] = eta     ;
1353dfccc68fSToby Isaac         extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi;
1354dfccc68fSToby Isaac 
1355dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1356dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1357dfccc68fSToby Isaac             PetscReal val = 0.;
1358dfccc68fSToby Isaac 
1359dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1360dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1361dfccc68fSToby Isaac             }
1362dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1363dfccc68fSToby Isaac           }
1364dfccc68fSToby Isaac         }
1365dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1366dfccc68fSToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1367dfccc68fSToby Isaac       }
1368dfccc68fSToby Isaac     }
1369dfccc68fSToby Isaac   }
1370ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1371ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1372ccd2543fSMatthew G Knepley }
1373ccd2543fSMatthew G Knepley 
1374ccd2543fSMatthew G Knepley #undef __FUNCT__
1375dfccc68fSToby Isaac #define __FUNCT__ "DMPlexComputeCellGeometryFEM_Implicit"
1376dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1377dfccc68fSToby Isaac {
1378dfccc68fSToby Isaac   PetscInt        depth, dim, coordDim, coneSize, i;
1379dfccc68fSToby Isaac   PetscInt        Nq = 0;
1380dfccc68fSToby Isaac   const PetscReal *points = NULL;
1381dfccc68fSToby Isaac   DMLabel         depthLabel;
1382*7318780aSToby Isaac   PetscReal       v0[3] = {-1.}, J0[9] = {-1.}, detJ0 = -1.;
1383dfccc68fSToby Isaac   PetscBool       isAffine = PETSC_TRUE;
1384dfccc68fSToby Isaac   PetscErrorCode  ierr;
1385dfccc68fSToby Isaac 
1386dfccc68fSToby Isaac   PetscFunctionBegin;
1387dfccc68fSToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1388dfccc68fSToby Isaac   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1389dfccc68fSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1390dfccc68fSToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1391dfccc68fSToby Isaac   if (depth == 1 && dim == 1) {
1392dfccc68fSToby Isaac     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1393dfccc68fSToby Isaac   }
1394dfccc68fSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1395dfccc68fSToby Isaac   if (coordDim > 3) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim);
1396dfccc68fSToby Isaac   if (quad) {ierr = PetscQuadratureGetData(quad, NULL, &Nq, &points, NULL);CHKERRQ(ierr);}
1397dfccc68fSToby Isaac   switch (dim) {
1398dfccc68fSToby Isaac   case 0:
1399dfccc68fSToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1400dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1401dfccc68fSToby Isaac     break;
1402dfccc68fSToby Isaac   case 1:
1403*7318780aSToby Isaac     if (Nq) {
1404*7318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
1405*7318780aSToby Isaac     } else {
1406*7318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1407*7318780aSToby Isaac     }
1408dfccc68fSToby Isaac     break;
1409dfccc68fSToby Isaac   case 2:
1410dfccc68fSToby Isaac     switch (coneSize) {
1411dfccc68fSToby Isaac     case 3:
1412*7318780aSToby Isaac       if (Nq) {
1413*7318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
1414*7318780aSToby Isaac       } else {
1415*7318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1416*7318780aSToby Isaac       }
1417dfccc68fSToby Isaac       break;
1418dfccc68fSToby Isaac     case 4:
1419dfccc68fSToby Isaac       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1420dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1421dfccc68fSToby Isaac       break;
1422dfccc68fSToby Isaac     default:
1423dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1424dfccc68fSToby Isaac     }
1425dfccc68fSToby Isaac     break;
1426dfccc68fSToby Isaac   case 3:
1427dfccc68fSToby Isaac     switch (coneSize) {
1428dfccc68fSToby Isaac     case 4:
1429*7318780aSToby Isaac       if (Nq) {
1430*7318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
1431*7318780aSToby Isaac       } else {
1432*7318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1433*7318780aSToby Isaac       }
1434dfccc68fSToby Isaac       break;
1435dfccc68fSToby Isaac     case 6: /* Faces */
1436dfccc68fSToby Isaac     case 8: /* Vertices */
1437dfccc68fSToby Isaac       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1438dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1439dfccc68fSToby Isaac       break;
1440dfccc68fSToby Isaac     default:
1441dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1442dfccc68fSToby Isaac     }
1443dfccc68fSToby Isaac     break;
1444dfccc68fSToby Isaac   default:
1445dfccc68fSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1446dfccc68fSToby Isaac   }
1447*7318780aSToby Isaac   if (isAffine && Nq) {
1448dfccc68fSToby Isaac     if (v) {
1449dfccc68fSToby Isaac       for (i = 0; i < Nq; i++) {
1450*7318780aSToby Isaac         CoordinatesRefToReal(coordDim,dim,v0, J0, &points[dim * i], &v[coordDim * i]);
1451dfccc68fSToby Isaac       }
1452dfccc68fSToby Isaac     }
1453*7318780aSToby Isaac     if (detJ) {
1454*7318780aSToby Isaac       for (i = 0; i < Nq; i++) {
1455*7318780aSToby Isaac         detJ[i] = detJ0;
1456dfccc68fSToby Isaac       }
1457*7318780aSToby Isaac     }
1458*7318780aSToby Isaac     if (J) {
1459*7318780aSToby Isaac       PetscInt k;
1460*7318780aSToby Isaac 
1461*7318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1462dfccc68fSToby Isaac         PetscInt j;
1463dfccc68fSToby Isaac 
1464*7318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
1465*7318780aSToby Isaac           J[k] = J0[j];
1466*7318780aSToby Isaac         }
1467*7318780aSToby Isaac       }
1468*7318780aSToby Isaac     }
1469*7318780aSToby Isaac     if (invJ) {
1470*7318780aSToby Isaac       PetscInt k;
1471*7318780aSToby Isaac       switch (coordDim) {
1472*7318780aSToby Isaac       case 0:
1473*7318780aSToby Isaac         break;
1474*7318780aSToby Isaac       case 1:
1475*7318780aSToby Isaac         invJ[0] = 1./J0[0];
1476*7318780aSToby Isaac         break;
1477*7318780aSToby Isaac       case 2:
1478*7318780aSToby Isaac         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
1479*7318780aSToby Isaac         break;
1480*7318780aSToby Isaac       case 3:
1481*7318780aSToby Isaac         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
1482*7318780aSToby Isaac         break;
1483*7318780aSToby Isaac       }
1484*7318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
1485*7318780aSToby Isaac         PetscInt j;
1486*7318780aSToby Isaac 
1487*7318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
1488*7318780aSToby Isaac           invJ[k] = invJ[j];
1489*7318780aSToby Isaac         }
1490dfccc68fSToby Isaac       }
1491dfccc68fSToby Isaac     }
1492dfccc68fSToby Isaac   }
1493dfccc68fSToby Isaac   PetscFunctionReturn(0);
1494dfccc68fSToby Isaac }
1495dfccc68fSToby Isaac 
1496dfccc68fSToby Isaac #undef __FUNCT__
14978e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM"
1498ccd2543fSMatthew G Knepley /*@C
14998e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1500ccd2543fSMatthew G Knepley 
1501ccd2543fSMatthew G Knepley   Collective on DM
1502ccd2543fSMatthew G Knepley 
1503ccd2543fSMatthew G Knepley   Input Arguments:
1504ccd2543fSMatthew G Knepley + dm   - the DM
1505ccd2543fSMatthew G Knepley - cell - the cell
1506ccd2543fSMatthew G Knepley 
1507ccd2543fSMatthew G Knepley   Output Arguments:
1508ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1509ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1510ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1511ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1512ccd2543fSMatthew G Knepley 
1513ccd2543fSMatthew G Knepley   Level: advanced
1514ccd2543fSMatthew G Knepley 
1515ccd2543fSMatthew G Knepley   Fortran Notes:
1516ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1517ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1518ccd2543fSMatthew G Knepley 
15198e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
1520ccd2543fSMatthew G Knepley @*/
15218e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1522ccd2543fSMatthew G Knepley {
1523ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1524ccd2543fSMatthew G Knepley 
1525ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1526dfccc68fSToby Isaac   ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr);
15278e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
15288e0841e0SMatthew G. Knepley }
15298e0841e0SMatthew G. Knepley 
15308e0841e0SMatthew G. Knepley #undef __FUNCT__
1531dfccc68fSToby Isaac #define __FUNCT__ "DMPlexComputeCellGeometryFEM_FE"
1532dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
15338e0841e0SMatthew G. Knepley {
1534dfccc68fSToby Isaac   PetscQuadrature  feQuad;
15358e0841e0SMatthew G. Knepley   PetscSection     coordSection;
15368e0841e0SMatthew G. Knepley   Vec              coordinates;
15378e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
15388e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
1539f960e424SToby Isaac   PetscReal       *basisDer, *basis, detJt;
1540f960e424SToby Isaac   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, q;
15418e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
15428e0841e0SMatthew G. Knepley 
15438e0841e0SMatthew G. Knepley   PetscFunctionBegin;
15448e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
15458e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
15468e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
15478e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
15488e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1549dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
1550dfccc68fSToby Isaac     PetscDualSpace dsp;
1551dfccc68fSToby Isaac 
1552dfccc68fSToby Isaac     ierr = PetscFEGetDualSpace(fe, &dsp);CHKERRQ(ierr);
1553dfccc68fSToby Isaac     ierr = PetscDualSpaceGetFunctional(dsp, 0, &quad);CHKERRQ(ierr);
15548e0841e0SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1555dfccc68fSToby Isaac     Nq = 1;
1556dfccc68fSToby Isaac   } else {
1557dfccc68fSToby Isaac     ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1558dfccc68fSToby Isaac   }
155991d2b7ceSToby Isaac   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
1560dfccc68fSToby Isaac   ierr = PetscFEGetQuadrature(fe, &feQuad);CHKERRQ(ierr);
1561dfccc68fSToby Isaac   if (feQuad == quad) {
1562f960e424SToby Isaac     ierr = PetscFEGetDefaultTabulation(fe, &basis, &basisDer, NULL);CHKERRQ(ierr);
15638e0841e0SMatthew 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);
1564dfccc68fSToby Isaac   } else {
1565dfccc68fSToby Isaac     ierr = PetscFEGetTabulation(fe, Nq, quadPoints, &basis, &basisDer, NULL);CHKERRQ(ierr);
1566dfccc68fSToby Isaac   }
1567dfccc68fSToby Isaac   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
1568dfccc68fSToby Isaac   if (v) {
1569dfccc68fSToby Isaac     ierr = PetscMemzero(v, Nq*cdim*sizeof(PetscReal));CHKERRQ(ierr);
1570f960e424SToby Isaac     for (q = 0; q < Nq; ++q) {
1571f960e424SToby Isaac       PetscInt i, k;
1572f960e424SToby Isaac 
1573f960e424SToby Isaac       for (k = 0; k < pdim; ++k)
1574f960e424SToby Isaac         for (i = 0; i < cdim; ++i)
1575dfccc68fSToby Isaac           v[q*cdim + i] += basis[q*pdim + k] * PetscRealPart(coords[k*cdim + i]);
1576f960e424SToby Isaac       ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1577f960e424SToby Isaac     }
1578f960e424SToby Isaac   }
15798e0841e0SMatthew G. Knepley   if (J) {
1580dfccc68fSToby Isaac     ierr = PetscMemzero(J, Nq*cdim*cdim*sizeof(PetscReal));CHKERRQ(ierr);
15818e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
15828e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
15838e0841e0SMatthew G. Knepley 
15848e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
15858e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
15868e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
15878e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
1588dfccc68fSToby Isaac             J[(q*cdim + i)*cdim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
15893bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
15908e0841e0SMatthew G. Knepley       if (cdim > dim) {
15918e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
15928e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
15938e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
15948e0841e0SMatthew G. Knepley       }
1595f960e424SToby Isaac       if (!detJ && !invJ) continue;
1596a63b72c6SToby Isaac       detJt = 0.;
15978e0841e0SMatthew G. Knepley       switch (cdim) {
15988e0841e0SMatthew G. Knepley       case 3:
1599037dc194SToby Isaac         DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]);
1600037dc194SToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
160117fe8556SMatthew G. Knepley         break;
160249dc4407SMatthew G. Knepley       case 2:
16039f328543SToby Isaac         DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]);
1604037dc194SToby Isaac         if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
160549dc4407SMatthew G. Knepley         break;
16068e0841e0SMatthew G. Knepley       case 1:
1607037dc194SToby Isaac         detJt = J[q*cdim*dim];
1608037dc194SToby Isaac         if (invJ) invJ[q*cdim*dim] = 1.0/detJt;
160949dc4407SMatthew G. Knepley       }
1610f960e424SToby Isaac       if (detJ) detJ[q] = detJt;
161149dc4407SMatthew G. Knepley     }
161249dc4407SMatthew G. Knepley   }
1613037dc194SToby Isaac   else if (detJ || invJ) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
1614dfccc68fSToby Isaac   if (feQuad != quad) {
1615dfccc68fSToby Isaac     ierr = PetscFERestoreTabulation(fe, Nq, quadPoints, &basis, &basisDer, NULL);CHKERRQ(ierr);
1616dfccc68fSToby Isaac   }
16178e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
16188e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
16198e0841e0SMatthew G. Knepley }
16208e0841e0SMatthew G. Knepley 
16218e0841e0SMatthew G. Knepley #undef __FUNCT__
16228e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
16238e0841e0SMatthew G. Knepley /*@C
16248e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
16258e0841e0SMatthew G. Knepley 
16268e0841e0SMatthew G. Knepley   Collective on DM
16278e0841e0SMatthew G. Knepley 
16288e0841e0SMatthew G. Knepley   Input Arguments:
16298e0841e0SMatthew G. Knepley + dm   - the DM
16308e0841e0SMatthew G. Knepley . cell - the cell
1631dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
1632dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
16338e0841e0SMatthew G. Knepley 
16348e0841e0SMatthew G. Knepley   Output Arguments:
1635dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
16368e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
16378e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
16388e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
16398e0841e0SMatthew G. Knepley 
16408e0841e0SMatthew G. Knepley   Level: advanced
16418e0841e0SMatthew G. Knepley 
16428e0841e0SMatthew G. Knepley   Fortran Notes:
16438e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
16448e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
16458e0841e0SMatthew G. Knepley 
16468e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
16478e0841e0SMatthew G. Knepley @*/
1648dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
16498e0841e0SMatthew G. Knepley {
1650dfccc68fSToby Isaac   PetscFE        fe = NULL;
16518e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
16528e0841e0SMatthew G. Knepley 
16538e0841e0SMatthew G. Knepley   PetscFunctionBegin;
1654dfccc68fSToby Isaac   if (dm->coordinateDM) {
1655dfccc68fSToby Isaac     PetscClassId id;
1656dfccc68fSToby Isaac     PetscInt     numFields;
1657dfccc68fSToby Isaac     PetscDS      prob = dm->coordinateDM->prob;
1658dfccc68fSToby Isaac     PetscObject  disc;
1659dfccc68fSToby Isaac 
1660dfccc68fSToby Isaac     ierr = PetscDSGetNumFields(prob, &numFields);CHKERRQ(ierr);
1661dfccc68fSToby Isaac     if (numFields) {
1662dfccc68fSToby Isaac       ierr = PetscDSGetDiscretization(prob,0,&disc);CHKERRQ(ierr);
1663dfccc68fSToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1664dfccc68fSToby Isaac       if (id == PETSCFE_CLASSID) {
1665dfccc68fSToby Isaac         fe = (PetscFE) disc;
1666dfccc68fSToby Isaac       }
1667dfccc68fSToby Isaac     }
1668dfccc68fSToby Isaac   }
1669dfccc68fSToby Isaac   if (!fe) {ierr = DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1670dfccc68fSToby Isaac   else     {ierr = DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1671ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1672ccd2543fSMatthew G Knepley }
1673834e62ceSMatthew G. Knepley 
1674834e62ceSMatthew G. Knepley #undef __FUNCT__
1675cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
1676011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1677cc08537eSMatthew G. Knepley {
1678cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1679cc08537eSMatthew G. Knepley   Vec            coordinates;
1680a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
168106e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1682cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1683cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1684cc08537eSMatthew G. Knepley 
1685cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1686cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
168769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1688cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1689011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
16902e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1691cc08537eSMatthew G. Knepley   if (centroid) {
169206e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
169306e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1694cc08537eSMatthew G. Knepley   }
1695cc08537eSMatthew G. Knepley   if (normal) {
1696a60a936bSMatthew G. Knepley     PetscReal norm;
1697a60a936bSMatthew G. Knepley 
169806e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
169906e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1700a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1701a60a936bSMatthew G. Knepley     normal[0] /= norm;
1702a60a936bSMatthew G. Knepley     normal[1] /= norm;
1703cc08537eSMatthew G. Knepley   }
1704cc08537eSMatthew G. Knepley   if (vol) {
170506e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1706cc08537eSMatthew G. Knepley   }
1707cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1708cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1709cc08537eSMatthew G. Knepley }
1710cc08537eSMatthew G. Knepley 
1711cc08537eSMatthew G. Knepley #undef __FUNCT__
1712cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1713cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1714011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1715cc08537eSMatthew G. Knepley {
1716cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1717cc08537eSMatthew G. Knepley   Vec            coordinates;
1718cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
17190a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
17200a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1721cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1722cc08537eSMatthew G. Knepley 
1723cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1724cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
17250a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
172669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1727cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
17280bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1729ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1730ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1731ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1732ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1733ceee4971SMatthew G. Knepley   }
1734011ea5d8SMatthew G. Knepley   if (normal) {
1735011ea5d8SMatthew G. Knepley     if (dim > 2) {
17361ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
17371ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
17381ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
17390a1d6728SMatthew G. Knepley       PetscReal       norm;
17400a1d6728SMatthew G. Knepley 
17410a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
17420a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
17430a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
17448b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
17450a1d6728SMatthew G. Knepley       normal[0] /= norm;
17460a1d6728SMatthew G. Knepley       normal[1] /= norm;
17470a1d6728SMatthew G. Knepley       normal[2] /= norm;
1748011ea5d8SMatthew G. Knepley     } else {
1749011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1750011ea5d8SMatthew G. Knepley     }
1751011ea5d8SMatthew G. Knepley   }
1752741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
17530a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
17540a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
17550a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
17561ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
17571ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
17580a1d6728SMatthew G. Knepley     }
17590a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
17600a1d6728SMatthew G. Knepley     vsum += vtmp;
17610a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
17620a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
17630a1d6728SMatthew G. Knepley     }
17640a1d6728SMatthew G. Knepley   }
17650a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
17660a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
17670a1d6728SMatthew G. Knepley   }
17680a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1769ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
17700a1d6728SMatthew G. Knepley   if (centroid) {
17710a1d6728SMatthew G. Knepley     if (dim > 2) {
17720a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
17730a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
17740a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
17750a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
17760a1d6728SMatthew G. Knepley         }
17770a1d6728SMatthew G. Knepley       }
17780a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
17790a1d6728SMatthew G. Knepley   }
1780cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1781cc08537eSMatthew G. Knepley }
1782cc08537eSMatthew G. Knepley 
1783cc08537eSMatthew G. Knepley #undef __FUNCT__
17840ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
17850ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1786011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
17870ec8681fSMatthew G. Knepley {
17880ec8681fSMatthew G. Knepley   PetscSection    coordSection;
17890ec8681fSMatthew G. Knepley   Vec             coordinates;
17900ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
179186623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1792a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
17930ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
17940ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
17950ec8681fSMatthew G. Knepley 
17960ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1797f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
17980ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
179969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
18000ec8681fSMatthew G. Knepley 
1801d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
18020ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
18030ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1804a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
18050ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1806011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
18070ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
18080ec8681fSMatthew G. Knepley     switch (numCorners) {
18090ec8681fSMatthew G. Knepley     case 3:
18100ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
18111ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
18121ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
18131ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
18140ec8681fSMatthew G. Knepley       }
18150ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1816a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
18170ec8681fSMatthew G. Knepley       vsum += vtmp;
18184f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
18190ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
18201ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
18210ec8681fSMatthew G. Knepley         }
18220ec8681fSMatthew G. Knepley       }
18230ec8681fSMatthew G. Knepley       break;
18240ec8681fSMatthew G. Knepley     case 4:
18250ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
18260ec8681fSMatthew G. Knepley       /* First tet */
18270ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
18281ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
18291ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
18301ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
18310ec8681fSMatthew G. Knepley       }
18320ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1833a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
18340ec8681fSMatthew G. Knepley       vsum += vtmp;
18350ec8681fSMatthew G. Knepley       if (centroid) {
18360ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
18370ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
18380ec8681fSMatthew G. Knepley         }
18390ec8681fSMatthew G. Knepley       }
18400ec8681fSMatthew G. Knepley       /* Second tet */
18410ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
18421ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
18431ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
18441ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
18450ec8681fSMatthew G. Knepley       }
18460ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1847a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
18480ec8681fSMatthew G. Knepley       vsum += vtmp;
18490ec8681fSMatthew G. Knepley       if (centroid) {
18500ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
18510ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
18520ec8681fSMatthew G. Knepley         }
18530ec8681fSMatthew G. Knepley       }
18540ec8681fSMatthew G. Knepley       break;
18550ec8681fSMatthew G. Knepley     default:
1856796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
18570ec8681fSMatthew G. Knepley     }
18584f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
18590ec8681fSMatthew G. Knepley   }
18608763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
18610ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1862d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
18630ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
18640ec8681fSMatthew G. Knepley }
18650ec8681fSMatthew G. Knepley 
18660ec8681fSMatthew G. Knepley #undef __FUNCT__
1867834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1868834e62ceSMatthew G. Knepley /*@C
1869834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1870834e62ceSMatthew G. Knepley 
1871834e62ceSMatthew G. Knepley   Collective on DM
1872834e62ceSMatthew G. Knepley 
1873834e62ceSMatthew G. Knepley   Input Arguments:
1874834e62ceSMatthew G. Knepley + dm   - the DM
1875834e62ceSMatthew G. Knepley - cell - the cell
1876834e62ceSMatthew G. Knepley 
1877834e62ceSMatthew G. Knepley   Output Arguments:
1878834e62ceSMatthew G. Knepley + volume   - the cell volume
1879cc08537eSMatthew G. Knepley . centroid - the cell centroid
1880cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1881834e62ceSMatthew G. Knepley 
1882834e62ceSMatthew G. Knepley   Level: advanced
1883834e62ceSMatthew G. Knepley 
1884834e62ceSMatthew G. Knepley   Fortran Notes:
1885834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1886834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1887834e62ceSMatthew G. Knepley 
188869d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1889834e62ceSMatthew G. Knepley @*/
1890cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1891834e62ceSMatthew G. Knepley {
18920ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1893834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1894834e62ceSMatthew G. Knepley 
1895834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1896834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1897c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1898834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1899834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1900c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1901834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1902011ea5d8SMatthew G. Knepley   switch (depth) {
1903cc08537eSMatthew G. Knepley   case 1:
1904011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1905cc08537eSMatthew G. Knepley     break;
1906834e62ceSMatthew G. Knepley   case 2:
1907011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1908834e62ceSMatthew G. Knepley     break;
1909834e62ceSMatthew G. Knepley   case 3:
1910011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1911834e62ceSMatthew G. Knepley     break;
1912834e62ceSMatthew G. Knepley   default:
1913834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1914834e62ceSMatthew G. Knepley   }
1915834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1916834e62ceSMatthew G. Knepley }
1917113c68e6SMatthew G. Knepley 
1918113c68e6SMatthew G. Knepley #undef __FUNCT__
1919c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1920c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1921c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1922c0d900a5SMatthew G. Knepley {
1923c0d900a5SMatthew G. Knepley   DM             dmCell;
1924c0d900a5SMatthew G. Knepley   Vec            coordinates;
1925c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1926c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1927c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1928c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1929c0d900a5SMatthew G. Knepley 
1930c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1931c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1932c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1933c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1934c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1935c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1936c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1937c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1938c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1939c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1940c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1941c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
19429e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1943c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1944c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1945c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1946c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1947c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1948c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1949c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1950c0d900a5SMatthew G. Knepley 
1951c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1952c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1953c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1954c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1955c0d900a5SMatthew G. Knepley   }
1956c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1957c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1958c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1959c0d900a5SMatthew G. Knepley }
1960c0d900a5SMatthew G. Knepley 
1961c0d900a5SMatthew G. Knepley #undef __FUNCT__
1962113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1963891a9168SMatthew G. Knepley /*@
1964891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1965891a9168SMatthew G. Knepley 
1966891a9168SMatthew G. Knepley   Input Parameter:
1967891a9168SMatthew G. Knepley . dm - The DM
1968891a9168SMatthew G. Knepley 
1969891a9168SMatthew G. Knepley   Output Parameters:
1970891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1971891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1972891a9168SMatthew G. Knepley 
1973891a9168SMatthew G. Knepley   Level: developer
1974891a9168SMatthew G. Knepley 
1975891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1976891a9168SMatthew G. Knepley @*/
1977113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1978113c68e6SMatthew G. Knepley {
1979113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1980113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1981113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1982113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1983113c68e6SMatthew G. Knepley   Vec            coordinates;
1984113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1985113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1986113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1987113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1988113c68e6SMatthew G. Knepley 
1989113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1990113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1991113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1992113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1993113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1994113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1995113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1996113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1997113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1998113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1999113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2000113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
20019e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2002113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
2003113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
2004113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2005113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
200606348e87SToby Isaac   if (cEndInterior < 0) {
200706348e87SToby Isaac     cEndInterior = cEnd;
200806348e87SToby Isaac   }
2009113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2010113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2011113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2012113c68e6SMatthew G. Knepley 
2013113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2014113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
2015113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
2016113c68e6SMatthew G. Knepley   }
2017113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
2018113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
2019113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
2020113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2021113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
20229e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2023113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
2024113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
2025113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
2026113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
2027113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
2028c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2029113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2030113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2031113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2032113c68e6SMatthew G. Knepley     PetscReal        area;
203350d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
2034113c68e6SMatthew G. Knepley 
20359ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
203650d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
203750d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
2038113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
2039113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
2040113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2041113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2042113c68e6SMatthew G. Knepley     {
2043113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
204406348e87SToby Isaac       PetscInt         ncells;
2045113c68e6SMatthew G. Knepley       const PetscInt  *cells;
2046113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
20470453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2048113c68e6SMatthew G. Knepley 
2049113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
205006348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
2051113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
2052113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
205306348e87SToby Isaac       if (ncells > 1) {
205406348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
2055113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
205606348e87SToby Isaac       }
205706348e87SToby Isaac       else {
205806348e87SToby Isaac         rcentroid = fg->centroid;
205906348e87SToby Isaac       }
20602e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
20612e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
20620453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2063113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2064113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2065113c68e6SMatthew G. Knepley       }
2066113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
2067113c68e6SMatthew 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]);
2068113c68e6SMatthew 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]);
2069113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
2070113c68e6SMatthew G. Knepley       }
2071113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2072113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2073113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2074113c68e6SMatthew G. Knepley       }
207506348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2076113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2077113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2078113c68e6SMatthew G. Knepley       }
2079113c68e6SMatthew G. Knepley     }
2080113c68e6SMatthew G. Knepley   }
2081b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2082113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
2083113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2084113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2085113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2086113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
2087113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2088113c68e6SMatthew G. Knepley 
2089113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
2090113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
2091113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
2092113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
209350d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
2094113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
2095113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
2096113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2097113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2098113c68e6SMatthew G. Knepley       if (support[s] == c) {
2099640bce14SSatish Balay         PetscFVCellGeom       *ci;
2100113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
2101113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
2102113c68e6SMatthew G. Knepley 
2103113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
2104113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2105113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
2106113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
2107113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
2108113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2109113c68e6SMatthew G. Knepley       }
2110113c68e6SMatthew G. Knepley     }
2111113c68e6SMatthew G. Knepley   }
2112113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
2113113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2114113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2115113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
2116113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2117113c68e6SMatthew G. Knepley }
2118113c68e6SMatthew G. Knepley 
2119113c68e6SMatthew G. Knepley #undef __FUNCT__
2120113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
2121113c68e6SMatthew G. Knepley /*@C
2122113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2123113c68e6SMatthew G. Knepley 
2124113c68e6SMatthew G. Knepley   Not collective
2125113c68e6SMatthew G. Knepley 
2126113c68e6SMatthew G. Knepley   Input Argument:
2127113c68e6SMatthew G. Knepley . dm - the DM
2128113c68e6SMatthew G. Knepley 
2129113c68e6SMatthew G. Knepley   Output Argument:
2130113c68e6SMatthew G. Knepley . minradius - the minium cell radius
2131113c68e6SMatthew G. Knepley 
2132113c68e6SMatthew G. Knepley   Level: developer
2133113c68e6SMatthew G. Knepley 
2134113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
2135113c68e6SMatthew G. Knepley @*/
2136113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2137113c68e6SMatthew G. Knepley {
2138113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2139113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2140113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
2141113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
2142113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2143113c68e6SMatthew G. Knepley }
2144113c68e6SMatthew G. Knepley 
2145113c68e6SMatthew G. Knepley #undef __FUNCT__
2146113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
2147113c68e6SMatthew G. Knepley /*@C
2148113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2149113c68e6SMatthew G. Knepley 
2150113c68e6SMatthew G. Knepley   Logically collective
2151113c68e6SMatthew G. Knepley 
2152113c68e6SMatthew G. Knepley   Input Arguments:
2153113c68e6SMatthew G. Knepley + dm - the DM
2154113c68e6SMatthew G. Knepley - minradius - the minium cell radius
2155113c68e6SMatthew G. Knepley 
2156113c68e6SMatthew G. Knepley   Level: developer
2157113c68e6SMatthew G. Knepley 
2158113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
2159113c68e6SMatthew G. Knepley @*/
2160113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2161113c68e6SMatthew G. Knepley {
2162113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2163113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2164113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
2165113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2166113c68e6SMatthew G. Knepley }
2167856ac710SMatthew G. Knepley 
2168856ac710SMatthew G. Knepley #undef __FUNCT__
2169856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
2170856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2171856ac710SMatthew G. Knepley {
2172856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
2173856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
2174856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2175856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2176856ac710SMatthew G. Knepley 
2177856ac710SMatthew G. Knepley   PetscFunctionBegin;
2178856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2179856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2180856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2181856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
2182856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2183c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2184856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2185856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2186856ac710SMatthew G. Knepley     const PetscInt        *faces;
2187856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
2188640bce14SSatish Balay     PetscFVCellGeom        *cg;
2189856ac710SMatthew G. Knepley     PetscBool              boundary;
2190856ac710SMatthew G. Knepley     PetscInt               ghost;
2191856ac710SMatthew G. Knepley 
2192856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2193856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
2194856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
2195856ac710SMatthew 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);
2196856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2197640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2198856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
2199856ac710SMatthew G. Knepley       const PetscInt        *fcells;
2200856ac710SMatthew G. Knepley       PetscInt               ncell, side;
2201856ac710SMatthew G. Knepley 
2202856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2203a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2204856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2205856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
2206856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2207856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
2208856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
2209856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2210856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
2211856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
2212856ac710SMatthew G. Knepley     }
2213856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
2214856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
2215856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2216856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2217a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2218856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2219856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
2220856ac710SMatthew G. Knepley       ++usedFaces;
2221856ac710SMatthew G. Knepley     }
2222856ac710SMatthew G. Knepley   }
2223856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
2224856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2225856ac710SMatthew G. Knepley }
2226856ac710SMatthew G. Knepley 
2227856ac710SMatthew G. Knepley #undef __FUNCT__
2228b81db932SToby Isaac #define __FUNCT__ "BuildGradientReconstruction_Internal_Tree"
2229b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2230b81db932SToby Isaac {
2231b81db932SToby Isaac   DMLabel        ghostLabel;
2232b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
2233b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2234b81db932SToby Isaac   PetscSection   neighSec;
2235b81db932SToby Isaac   PetscInt     (*neighbors)[2];
2236b81db932SToby Isaac   PetscInt      *counter;
2237b81db932SToby Isaac   PetscErrorCode ierr;
2238b81db932SToby Isaac 
2239b81db932SToby Isaac   PetscFunctionBegin;
2240b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2241b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2242b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
22435bc680faSToby Isaac   if (cEndInterior < 0) {
22445bc680faSToby Isaac     cEndInterior = cEnd;
22455bc680faSToby Isaac   }
2246b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
2247b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
2248b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2249c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2250b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2251b81db932SToby Isaac     const PetscInt        *fcells;
2252b81db932SToby Isaac     PetscBool              boundary;
22535bc680faSToby Isaac     PetscInt               ghost = -1;
2254b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2255b81db932SToby Isaac 
225606348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2257a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2258b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2259b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2260b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
226106348e87SToby Isaac     if (numCells == 2) {
2262b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2263b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2264b81db932SToby Isaac         PetscInt cell = fcells[c];
2265b81db932SToby Isaac 
2266e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2267b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
2268b81db932SToby Isaac         }
2269b81db932SToby Isaac       }
2270b81db932SToby Isaac     }
227106348e87SToby Isaac   }
2272b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
2273b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
2274b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2275b81db932SToby Isaac   nStart = 0;
2276b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2277b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2278b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2279b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2280b81db932SToby Isaac     const PetscInt        *fcells;
2281b81db932SToby Isaac     PetscBool              boundary;
22825bc680faSToby Isaac     PetscInt               ghost = -1;
2283b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2284b81db932SToby Isaac 
228506348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2286a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2287b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2288b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2289b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
229006348e87SToby Isaac     if (numCells == 2) {
2291b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2292b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2293b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2294b81db932SToby Isaac 
2295e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2296b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2297b81db932SToby Isaac           off += counter[cell - cStart]++;
2298b81db932SToby Isaac           neighbors[off][0] = f;
2299b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2300b81db932SToby Isaac         }
2301b81db932SToby Isaac       }
2302b81db932SToby Isaac     }
230306348e87SToby Isaac   }
2304b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2305b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2306b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2307317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2308640bce14SSatish Balay     PetscFVCellGeom        *cg;
2309b81db932SToby Isaac 
2310b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2311b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2312b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2313317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2314317218b9SToby 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);
2315b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2316640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2317b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2318b81db932SToby Isaac       const PetscInt        *fcells;
2319b81db932SToby Isaac       PetscInt               ncell, side, nface;
2320b81db932SToby Isaac 
2321b81db932SToby Isaac       nface = neighbors[off + f][0];
2322b81db932SToby Isaac       ncell = neighbors[off + f][1];
2323b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2324b81db932SToby Isaac       side  = (c != fcells[0]);
2325b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2326b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2327b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2328b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2329b81db932SToby Isaac     }
2330b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2331b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2332b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2333b81db932SToby Isaac     }
2334b81db932SToby Isaac   }
2335b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
23365fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2337b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2338b81db932SToby Isaac   PetscFunctionReturn(0);
2339b81db932SToby Isaac }
2340b81db932SToby Isaac 
2341b81db932SToby Isaac #undef __FUNCT__
2342856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
2343856ac710SMatthew G. Knepley /*@
2344856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2345856ac710SMatthew G. Knepley 
2346856ac710SMatthew G. Knepley   Collective on DM
2347856ac710SMatthew G. Knepley 
2348856ac710SMatthew G. Knepley   Input Arguments:
2349856ac710SMatthew G. Knepley + dm  - The DM
2350856ac710SMatthew G. Knepley . fvm - The PetscFV
23518f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
23528f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2353856ac710SMatthew G. Knepley 
2354856ac710SMatthew G. Knepley   Output Parameters:
2355856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2356856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2357856ac710SMatthew G. Knepley 
2358856ac710SMatthew G. Knepley   Level: developer
2359856ac710SMatthew G. Knepley 
2360856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2361856ac710SMatthew G. Knepley @*/
2362856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2363856ac710SMatthew G. Knepley {
2364856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2365856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2366b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2367856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2368856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2369856ac710SMatthew G. Knepley 
2370856ac710SMatthew G. Knepley   PetscFunctionBegin;
2371856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2372856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2373856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2374856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2375856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2376856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2377856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2378856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2379856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2380b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2381b81db932SToby Isaac   if (!parentSection) {
2382856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2383b5a3613cSMatthew G. Knepley   } else {
2384b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2385b81db932SToby Isaac   }
2386856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2387856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2388856ac710SMatthew G. Knepley   /* Create storage for gradients */
2389856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2390856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2391856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2392856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2393856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2394856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2395856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2396856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2397856ac710SMatthew G. Knepley }
2398b27d5b9eSToby Isaac 
2399b27d5b9eSToby Isaac #undef __FUNCT__
2400b27d5b9eSToby Isaac #define __FUNCT__ "DMPlexGetDataFVM"
2401b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2402b27d5b9eSToby Isaac {
2403b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2404b27d5b9eSToby Isaac   PetscErrorCode ierr;
2405b27d5b9eSToby Isaac 
2406b27d5b9eSToby Isaac   PetscFunctionBegin;
2407b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2408b27d5b9eSToby Isaac   if (!cellgeomobj) {
2409b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2410b27d5b9eSToby Isaac 
2411b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2412b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2413b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2414b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2415b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2416b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2417b27d5b9eSToby Isaac   }
2418b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2419b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2420b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2421b27d5b9eSToby Isaac   if (gradDM) {
2422b27d5b9eSToby Isaac     PetscObject gradobj;
2423b27d5b9eSToby Isaac     PetscBool   computeGradients;
2424b27d5b9eSToby Isaac 
2425b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2426b27d5b9eSToby Isaac     if (!computeGradients) {
2427b27d5b9eSToby Isaac       *gradDM = NULL;
2428b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2429b27d5b9eSToby Isaac     }
2430b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2431b27d5b9eSToby Isaac     if (!gradobj) {
2432b27d5b9eSToby Isaac       DM dmGradInt;
2433b27d5b9eSToby Isaac 
2434b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2435b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2436b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2437b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2438b27d5b9eSToby Isaac     }
2439b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2440b27d5b9eSToby Isaac   }
2441b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2442b27d5b9eSToby Isaac }
2443d6143a4eSToby Isaac 
2444d6143a4eSToby Isaac #undef __FUNCT__
24459d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_NewtonUpdate"
24469d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
24479d150b73SToby Isaac {
24489d150b73SToby Isaac   PetscInt l, m;
24499d150b73SToby Isaac 
2450cd345991SToby Isaac   PetscFunctionBeginHot;
24519d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
24529d150b73SToby Isaac     /* invert Jacobian, multiply */
24539d150b73SToby Isaac     PetscScalar det, idet;
24549d150b73SToby Isaac 
24559d150b73SToby Isaac     switch (dimR) {
24569d150b73SToby Isaac     case 1:
24579d150b73SToby Isaac       invJ[0] = 1./ J[0];
24589d150b73SToby Isaac       break;
24599d150b73SToby Isaac     case 2:
24609d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
24619d150b73SToby Isaac       idet = 1./det;
24629d150b73SToby Isaac       invJ[0] =  J[3] * idet;
24639d150b73SToby Isaac       invJ[1] = -J[1] * idet;
24649d150b73SToby Isaac       invJ[2] = -J[2] * idet;
24659d150b73SToby Isaac       invJ[3] =  J[0] * idet;
24669d150b73SToby Isaac       break;
24679d150b73SToby Isaac     case 3:
24689d150b73SToby Isaac       {
24699d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
24709d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
24719d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
24729d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
24739d150b73SToby Isaac         idet = 1./det;
24749d150b73SToby Isaac         invJ[0] *= idet;
24759d150b73SToby Isaac         invJ[1] *= idet;
24769d150b73SToby Isaac         invJ[2] *= idet;
24779d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
24789d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
24799d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
24809d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
24819d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
24829d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
24839d150b73SToby Isaac       }
24849d150b73SToby Isaac       break;
24859d150b73SToby Isaac     }
24869d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
24879d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2488c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
24899d150b73SToby Isaac       }
24909d150b73SToby Isaac     }
24919d150b73SToby Isaac   } else {
24929d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
24939d150b73SToby Isaac     char transpose = 'C';
24949d150b73SToby Isaac #else
24959d150b73SToby Isaac     char transpose = 'T';
24969d150b73SToby Isaac #endif
24979d150b73SToby Isaac     PetscBLASInt m = dimR;
24989d150b73SToby Isaac     PetscBLASInt n = dimC;
24999d150b73SToby Isaac     PetscBLASInt one = 1;
25009d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
25019d150b73SToby Isaac 
25029d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
25039d150b73SToby Isaac 
25049d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
25059d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
25069d150b73SToby Isaac 
2507c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
25089d150b73SToby Isaac   }
25099d150b73SToby Isaac   PetscFunctionReturn(0);
25109d150b73SToby Isaac }
25119d150b73SToby Isaac 
25129d150b73SToby Isaac #undef __FUNCT__
25139d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_Tensor"
25149d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
25159d150b73SToby Isaac {
2516c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
25179d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
25189d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
25199d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
25209d150b73SToby Isaac   PetscErrorCode ierr;
25219d150b73SToby Isaac 
25229d150b73SToby Isaac   PetscFunctionBegin;
25239d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25249d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
25259d150b73SToby Isaac   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);CHKERRQ(ierr);
25269d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
25279d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
25289d150b73SToby Isaac   cellCoords = &cellData[0];
25299d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
25309d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
25319d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
25329d150b73SToby Isaac   invJ       = &J[dimR * dimC];
25339d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
25349d150b73SToby Isaac   if (dimR == 2) {
25359d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
25369d150b73SToby Isaac 
25379d150b73SToby Isaac     for (i = 0; i < 4; i++) {
25389d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
25399d150b73SToby Isaac 
25409d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
25419d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
25429d150b73SToby Isaac       }
25439d150b73SToby Isaac     }
25449d150b73SToby Isaac   } else if (dimR == 3) {
25459d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
25469d150b73SToby Isaac 
25479d150b73SToby Isaac     for (i = 0; i < 8; i++) {
25489d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
25499d150b73SToby Isaac 
25509d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
25519d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
25529d150b73SToby Isaac       }
25539d150b73SToby Isaac     }
25549d150b73SToby Isaac   } else {
25559d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
25569d150b73SToby Isaac   }
25579d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
25589d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
25599d150b73SToby Isaac     PetscReal *swap;
25609d150b73SToby Isaac 
25619d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
25629d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
25639d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
25649d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
25659d150b73SToby Isaac       }
25669d150b73SToby Isaac     }
25679d150b73SToby Isaac 
25689d150b73SToby Isaac     if (i < dimR - 1) {
25699d150b73SToby Isaac       swap = cellCoeffs;
25709d150b73SToby Isaac       cellCoeffs = cellCoords;
25719d150b73SToby Isaac       cellCoords = swap;
25729d150b73SToby Isaac     }
25739d150b73SToby Isaac   }
25749d150b73SToby Isaac   ierr = PetscMemzero(refCoords,numPoints * dimR * sizeof (PetscReal));CHKERRQ(ierr);
25759d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
25769d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
25779d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
25789d150b73SToby Isaac 
25799d150b73SToby Isaac       /* compute -residual and Jacobian */
25809d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
25819d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
25829d150b73SToby Isaac       for (k = 0; k < numV; k++) {
25839d150b73SToby Isaac         PetscReal extCoord = 1.;
25849d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
25859d150b73SToby Isaac           PetscReal coord = guess[l];
25869d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
25879d150b73SToby Isaac 
25889d150b73SToby Isaac           extCoord *= dep * coord + !dep;
25899d150b73SToby Isaac           extJ[l] = dep;
25909d150b73SToby Isaac 
25919d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
25929d150b73SToby Isaac             PetscReal coord = guess[m];
25939d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
25949d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
25959d150b73SToby Isaac 
25969d150b73SToby Isaac             extJ[l] *= mult;
25979d150b73SToby Isaac           }
25989d150b73SToby Isaac         }
25999d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
26009d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
26019d150b73SToby Isaac 
26029d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
26039d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
26049d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
26059d150b73SToby Isaac           }
26069d150b73SToby Isaac         }
26079d150b73SToby Isaac       }
26080611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
26090611203eSToby Isaac       {
26100611203eSToby Isaac         PetscReal maxAbs = 0.;
26110611203eSToby Isaac 
26120611203eSToby Isaac         for (l = 0; l < dimC; l++) {
26130611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
26140611203eSToby Isaac         }
26150611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
26160611203eSToby Isaac       }
26170611203eSToby Isaac #endif
26189d150b73SToby Isaac 
26199d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
26209d150b73SToby Isaac     }
26219d150b73SToby Isaac   }
26229d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
26239d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
26249d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
26259d150b73SToby Isaac   PetscFunctionReturn(0);
26269d150b73SToby Isaac }
26279d150b73SToby Isaac 
26289d150b73SToby Isaac #undef __FUNCT__
26299d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates_Tensor"
26309d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
26319d150b73SToby Isaac {
26329d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
26339d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
26349d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
26359d150b73SToby Isaac   PetscErrorCode ierr;
26369d150b73SToby Isaac 
26379d150b73SToby Isaac   PetscFunctionBegin;
26389d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26399d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
26409d150b73SToby Isaac   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);CHKERRQ(ierr);
26419d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
26429d150b73SToby Isaac   cellCoords = &cellData[0];
26439d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
26449d150b73SToby Isaac   if (dimR == 2) {
26459d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
26469d150b73SToby Isaac 
26479d150b73SToby Isaac     for (i = 0; i < 4; i++) {
26489d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
26499d150b73SToby Isaac 
26509d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26519d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26529d150b73SToby Isaac       }
26539d150b73SToby Isaac     }
26549d150b73SToby Isaac   } else if (dimR == 3) {
26559d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
26569d150b73SToby Isaac 
26579d150b73SToby Isaac     for (i = 0; i < 8; i++) {
26589d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
26599d150b73SToby Isaac 
26609d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26619d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26629d150b73SToby Isaac       }
26639d150b73SToby Isaac     }
26649d150b73SToby Isaac   } else {
26659d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
26669d150b73SToby Isaac   }
26679d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
26689d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
26699d150b73SToby Isaac     PetscReal *swap;
26709d150b73SToby Isaac 
26719d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
26729d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
26739d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
26749d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
26759d150b73SToby Isaac       }
26769d150b73SToby Isaac     }
26779d150b73SToby Isaac 
26789d150b73SToby Isaac     if (i < dimR - 1) {
26799d150b73SToby Isaac       swap = cellCoeffs;
26809d150b73SToby Isaac       cellCoeffs = cellCoords;
26819d150b73SToby Isaac       cellCoords = swap;
26829d150b73SToby Isaac     }
26839d150b73SToby Isaac   }
26849d150b73SToby Isaac   ierr = PetscMemzero(realCoords,numPoints * dimC * sizeof (PetscReal));CHKERRQ(ierr);
26859d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
26869d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
26879d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
26889d150b73SToby Isaac 
26899d150b73SToby Isaac     for (k = 0; k < numV; k++) {
26909d150b73SToby Isaac       PetscReal extCoord = 1.;
26919d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
26929d150b73SToby Isaac         PetscReal coord = guess[l];
26939d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
26949d150b73SToby Isaac 
26959d150b73SToby Isaac         extCoord *= dep * coord + !dep;
26969d150b73SToby Isaac       }
26979d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
26989d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
26999d150b73SToby Isaac 
27009d150b73SToby Isaac         mapped[l] += coeff * extCoord;
27019d150b73SToby Isaac       }
27029d150b73SToby Isaac     }
27039d150b73SToby Isaac   }
27049d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
27059d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
27069d150b73SToby Isaac   PetscFunctionReturn(0);
27079d150b73SToby Isaac }
27089d150b73SToby Isaac 
27099d150b73SToby Isaac #undef __FUNCT__
27109d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_FE"
27119d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
27129d150b73SToby Isaac {
2713c0cbe899SToby Isaac   PetscInt       numComp, numDof, i, j, k, l, m, maxIter = 7, coordSize;
2714c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2715c6e120d1SToby Isaac   PetscReal      *invV, *modes;
2716c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
2717c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
27189d150b73SToby Isaac   PetscErrorCode ierr;
27199d150b73SToby Isaac 
27209d150b73SToby Isaac   PetscFunctionBegin;
27219d150b73SToby Isaac   ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr);
27229d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
27239d150b73SToby Isaac   if (numComp != dimC) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,dimC);
27249d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
27259d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2726c6e120d1SToby Isaac   ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
27279d150b73SToby Isaac   invV = fe->invV;
27289d150b73SToby Isaac   for (i = 0; i < numDof; i++) {
27299d150b73SToby Isaac     for (j = 0; j < dimC; j++) {
27309d150b73SToby Isaac       modes[i * dimC + j] = 0.;
27319d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
2732c6e120d1SToby Isaac         modes[i * dimC + j] += invV[i * numDof + k] * PetscRealPart(nodes[k * dimC + j]);
27339d150b73SToby Isaac       }
27349d150b73SToby Isaac     }
27359d150b73SToby Isaac   }
2736c6e120d1SToby Isaac   ierr   = DMGetWorkArray(dm,numDof + numDof * dimR + dimC,PETSC_REAL,&B);CHKERRQ(ierr);
2737c6e120d1SToby Isaac   D      = &B[numDof];
2738c6e120d1SToby Isaac   resNeg = &D[numDof * dimR];
2739c6e120d1SToby Isaac   ierr = DMGetWorkArray(dm,3 * dimC * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr);
27409d150b73SToby Isaac   invJ = &J[dimC * dimR];
27419d150b73SToby Isaac   work = &invJ[dimC * dimR];
27429d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
27439d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
27449b1f03cbSToby 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 */
27459d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
27469d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
27479d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[j * dimC + k];}
27489d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
27499d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
27509d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
27519d150b73SToby Isaac           resNeg[l] -= modes[k * dimC + l] * B[k];
27529d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
27539d150b73SToby Isaac             J[l * dimR + m] += modes[k * dimC + l] * D[k * dimR + m];
27549d150b73SToby Isaac           }
27559d150b73SToby Isaac         }
27569d150b73SToby Isaac       }
27570611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
27580611203eSToby Isaac       {
27590611203eSToby Isaac         PetscReal maxAbs = 0.;
27600611203eSToby Isaac 
27610611203eSToby Isaac         for (l = 0; l < dimC; l++) {
27620611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
27630611203eSToby Isaac         }
27640611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
27650611203eSToby Isaac       }
27660611203eSToby Isaac #endif
27679d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
27689d150b73SToby Isaac     }
27699d150b73SToby Isaac   }
2770c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,3 * dimC * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr);
2771c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,numDof + numDof * dimR + dimC,PETSC_REAL,&B);CHKERRQ(ierr);
2772c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
27739d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
27749d150b73SToby Isaac   PetscFunctionReturn(0);
27759d150b73SToby Isaac }
27769d150b73SToby Isaac 
27779d150b73SToby Isaac #undef __FUNCT__
27789d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates_FE"
27799d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
27809d150b73SToby Isaac {
27819d150b73SToby Isaac   PetscInt       numComp, numDof, i, j, k, l, coordSize;
2782c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2783c6e120d1SToby Isaac   PetscReal      *invV, *modes;
27849d150b73SToby Isaac   PetscReal      *B;
27859d150b73SToby Isaac   PetscErrorCode ierr;
27869d150b73SToby Isaac 
27879d150b73SToby Isaac   PetscFunctionBegin;
27889d150b73SToby Isaac   ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr);
27899d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
27909d150b73SToby Isaac   if (numComp != dimC) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,dimC);
27919d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
27929d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2793c6e120d1SToby Isaac   ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
27949d150b73SToby Isaac   invV = fe->invV;
27959d150b73SToby Isaac   for (i = 0; i < numDof; i++) {
27969d150b73SToby Isaac     for (j = 0; j < dimC; j++) {
27979d150b73SToby Isaac       modes[i * dimC + j] = 0.;
27989d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
2799c6e120d1SToby Isaac         modes[i * dimC + j] += invV[i * numDof + k] * PetscRealPart(nodes[k * dimC + j]);
28009d150b73SToby Isaac       }
28019d150b73SToby Isaac     }
28029d150b73SToby Isaac   }
28039b1f03cbSToby Isaac   ierr = DMGetWorkArray(dm,numDof,PETSC_REAL,&B);CHKERRQ(ierr);
28049d150b73SToby Isaac   for (i = 0; i < numPoints * dimC; i++) {realCoords[i] = 0.;}
28059d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28069d150b73SToby Isaac     const PetscReal *guess  = &refCoords[j * dimR];
28079d150b73SToby Isaac     PetscReal       *mapped = &realCoords[j * dimC];
28089d150b73SToby Isaac 
28099d150b73SToby Isaac     ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, NULL, NULL);CHKERRQ(ierr);
28109d150b73SToby Isaac     for (k = 0; k < numDof; k++) {
28119d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
28129d150b73SToby Isaac         mapped[l] += modes[k * dimC + l] * B[k];
28139d150b73SToby Isaac       }
28149d150b73SToby Isaac     }
28159d150b73SToby Isaac   }
28169b1f03cbSToby Isaac   ierr = DMRestoreWorkArray(dm,numDof,PETSC_REAL,&B);CHKERRQ(ierr);
2817c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
28189d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
28199d150b73SToby Isaac   PetscFunctionReturn(0);
28209d150b73SToby Isaac }
28219d150b73SToby Isaac 
28229d150b73SToby Isaac #undef __FUNCT__
2823d6143a4eSToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference"
2824d6143a4eSToby Isaac /*@
2825d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
2826d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
2827d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
2828d6143a4eSToby Isaac 
2829d6143a4eSToby Isaac   Not collective
2830d6143a4eSToby Isaac 
2831d6143a4eSToby Isaac   Input Parameters:
2832d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2833d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2834d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
2835d6143a4eSToby Isaac . cell       - the cell whose map is used.
2836d6143a4eSToby Isaac . numPoints  - the number of points to locate
2837d6143a4eSToby Isaac + realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2838d6143a4eSToby Isaac 
2839d6143a4eSToby Isaac   Output Parameters:
2840d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
2841d6143a4eSToby Isaac @*/
2842d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
2843d6143a4eSToby Isaac {
28449d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
28459d150b73SToby Isaac   DM             coordDM = NULL;
28469d150b73SToby Isaac   Vec            coords;
28479d150b73SToby Isaac   PetscFE        fe = NULL;
28489d150b73SToby Isaac   PetscErrorCode ierr;
28499d150b73SToby Isaac 
2850d6143a4eSToby Isaac   PetscFunctionBegin;
28519d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28529d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
28539d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
28549d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
28559d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
28569d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
28579d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
28589d150b73SToby Isaac   if (coordDM) {
28599d150b73SToby Isaac     PetscInt coordFields;
28609d150b73SToby Isaac 
28619d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
28629d150b73SToby Isaac     if (coordFields) {
28639d150b73SToby Isaac       PetscClassId id;
28649d150b73SToby Isaac       PetscObject  disc;
28659d150b73SToby Isaac 
28669d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
28679d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
28689d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
28699d150b73SToby Isaac         fe = (PetscFE) disc;
28709d150b73SToby Isaac       }
28719d150b73SToby Isaac     }
28729d150b73SToby Isaac   }
28739d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
28749d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
28759d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
28769d150b73SToby Isaac   if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);CHKERRQ(ierr);
28779d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
28789d150b73SToby Isaac     PetscInt  coneSize;
28799d150b73SToby Isaac     PetscBool isSimplex, isTensor;
28809d150b73SToby Isaac 
28819d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
28829d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
28839d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
28849d150b73SToby Isaac     if (isSimplex) {
28859d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
28869d150b73SToby Isaac 
28879d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
28889d150b73SToby Isaac       J    = &v0[dimC];
28899d150b73SToby Isaac       invJ = &J[dimC * dimC];
28909d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
28919d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
28929d150b73SToby Isaac         CoordinatesRealToRef(dimC, dimR, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
28939d150b73SToby Isaac       }
28949d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
28959d150b73SToby Isaac     } else if (isTensor) {
28969d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
28979d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
28989d150b73SToby Isaac   } else {
28999d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
29009d150b73SToby Isaac   }
29019d150b73SToby Isaac   PetscFunctionReturn(0);
29029d150b73SToby Isaac }
29039d150b73SToby Isaac 
29049d150b73SToby Isaac #undef __FUNCT__
29059d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates"
29069d150b73SToby Isaac /*@
29079d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
29089d150b73SToby Isaac 
29099d150b73SToby Isaac   Not collective
29109d150b73SToby Isaac 
29119d150b73SToby Isaac   Input Parameters:
29129d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
29139d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
29149d150b73SToby Isaac                as a multilinear map for tensor-product elements
29159d150b73SToby Isaac . cell       - the cell whose map is used.
29169d150b73SToby Isaac . numPoints  - the number of points to locate
29179d150b73SToby Isaac + refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
29189d150b73SToby Isaac 
29199d150b73SToby Isaac   Output Parameters:
29209d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
29219d150b73SToby Isaac @*/
29229d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
29239d150b73SToby Isaac {
29249d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
29259d150b73SToby Isaac   DM             coordDM = NULL;
29269d150b73SToby Isaac   Vec            coords;
29279d150b73SToby Isaac   PetscFE        fe = NULL;
29289d150b73SToby Isaac   PetscErrorCode ierr;
29299d150b73SToby Isaac 
29309d150b73SToby Isaac   PetscFunctionBegin;
29319d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29329d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
29339d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
29349d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
29359d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
29369d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
29379d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
29389d150b73SToby Isaac   if (coordDM) {
29399d150b73SToby Isaac     PetscInt coordFields;
29409d150b73SToby Isaac 
29419d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
29429d150b73SToby Isaac     if (coordFields) {
29439d150b73SToby Isaac       PetscClassId id;
29449d150b73SToby Isaac       PetscObject  disc;
29459d150b73SToby Isaac 
29469d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
29479d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
29489d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
29499d150b73SToby Isaac         fe = (PetscFE) disc;
29509d150b73SToby Isaac       }
29519d150b73SToby Isaac     }
29529d150b73SToby Isaac   }
29539d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
29549d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
29559d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
29569d150b73SToby Isaac   if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);CHKERRQ(ierr);
29579d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
29589d150b73SToby Isaac     PetscInt  coneSize;
29599d150b73SToby Isaac     PetscBool isSimplex, isTensor;
29609d150b73SToby Isaac 
29619d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
29629d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
29639d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
29649d150b73SToby Isaac     if (isSimplex) {
29659d150b73SToby Isaac       PetscReal detJ, *v0, *J;
29669d150b73SToby Isaac 
29679d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
29689d150b73SToby Isaac       J    = &v0[dimC];
29699d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
29709d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
29719d150b73SToby Isaac         CoordinatesRefToReal(dimC, dimR, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
29729d150b73SToby Isaac       }
29739d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
29749d150b73SToby Isaac     } else if (isTensor) {
29759d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
29769d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
29779d150b73SToby Isaac   } else {
29789d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
29799d150b73SToby Isaac   }
2980d6143a4eSToby Isaac   PetscFunctionReturn(0);
2981d6143a4eSToby Isaac }
2982