xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 9d150b735cbdd09c12e89f7ca202dc1d86043513)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2*9d150b73SToby Isaac #include <petsc/private/petscfeimpl.h>  /*I      "petscfe.h"       I*/
3*9d150b73SToby 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   }
8288763be8eSMatthew G. Knepley   if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
8298763be8eSMatthew G. Knepley   if (PetscAbsReal(x2p[2]) > 1.0e-9) 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"
1086ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], 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;
11017f07f362SMatthew G. Knepley   *detJ = 0.0;
110299dec3a6SMatthew G. Knepley   if (numCoords == 12) {
110399dec3a6SMatthew G. Knepley     const PetscInt dim = 3;
110499dec3a6SMatthew 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};
110599dec3a6SMatthew G. Knepley 
110699dec3a6SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1107741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
110899dec3a6SMatthew G. Knepley     if (J)    {
110999dec3a6SMatthew G. Knepley       const PetscInt pdim = 2;
111099dec3a6SMatthew G. Knepley 
111199dec3a6SMatthew G. Knepley       for (d = 0; d < pdim; d++) {
111299dec3a6SMatthew G. Knepley         J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
111399dec3a6SMatthew G. Knepley         J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
111499dec3a6SMatthew G. Knepley       }
11153bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1116923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
111799dec3a6SMatthew G. Knepley       for (d = 0; d < dim; d++) {
111899dec3a6SMatthew G. Knepley         for (f = 0; f < dim; f++) {
111999dec3a6SMatthew G. Knepley           J[d*dim+f] = 0.0;
112099dec3a6SMatthew G. Knepley           for (g = 0; g < dim; g++) {
112199dec3a6SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
112299dec3a6SMatthew G. Knepley           }
112399dec3a6SMatthew G. Knepley         }
112499dec3a6SMatthew G. Knepley       }
11253bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
112699dec3a6SMatthew G. Knepley     }
1127923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
112871f58de1SToby Isaac   } else if (numCoords == 8) {
112999dec3a6SMatthew G. Knepley     const PetscInt dim = 2;
113099dec3a6SMatthew G. Knepley 
11317f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1132ccd2543fSMatthew G Knepley     if (J)    {
1133ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
113499dec3a6SMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
113599dec3a6SMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1136ccd2543fSMatthew G Knepley       }
11373bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1138923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1139ccd2543fSMatthew G Knepley     }
1140923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1141796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
114299dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1143ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1144ccd2543fSMatthew G Knepley }
1145ccd2543fSMatthew G Knepley 
1146ccd2543fSMatthew G Knepley #undef __FUNCT__
1147ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal"
1148ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1149ccd2543fSMatthew G Knepley {
1150ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1151ccd2543fSMatthew G Knepley   Vec            coordinates;
1152a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1153ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
115499dec3a6SMatthew G. Knepley   PetscInt       d;
1155ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1156ccd2543fSMatthew G Knepley 
1157ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1158ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
115969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1160ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11617f07f362SMatthew G. Knepley   *detJ = 0.0;
11627f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1163ccd2543fSMatthew G Knepley   if (J)    {
1164ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1165f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1166f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1167f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1168f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1169ccd2543fSMatthew G Knepley     }
11703bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1171923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1172ccd2543fSMatthew G Knepley   }
1173923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1174ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1175ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1176ccd2543fSMatthew G Knepley }
1177ccd2543fSMatthew G Knepley 
1178ccd2543fSMatthew G Knepley #undef __FUNCT__
1179ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal"
1180ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1181ccd2543fSMatthew G Knepley {
1182ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1183ccd2543fSMatthew G Knepley   Vec            coordinates;
1184a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1185ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1186ccd2543fSMatthew G Knepley   PetscInt       d;
1187ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1188ccd2543fSMatthew G Knepley 
1189ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1190ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
119169d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1192ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11937f07f362SMatthew G. Knepley   *detJ = 0.0;
11947f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1195ccd2543fSMatthew G Knepley   if (J)    {
1196ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1197f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1198f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1199f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1200ccd2543fSMatthew G Knepley     }
12013bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1202923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1203ccd2543fSMatthew G Knepley   }
1204923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1205ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1206ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1207ccd2543fSMatthew G Knepley }
1208ccd2543fSMatthew G Knepley 
1209ccd2543fSMatthew G Knepley #undef __FUNCT__
12108e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM"
1211ccd2543fSMatthew G Knepley /*@C
12128e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1213ccd2543fSMatthew G Knepley 
1214ccd2543fSMatthew G Knepley   Collective on DM
1215ccd2543fSMatthew G Knepley 
1216ccd2543fSMatthew G Knepley   Input Arguments:
1217ccd2543fSMatthew G Knepley + dm   - the DM
1218ccd2543fSMatthew G Knepley - cell - the cell
1219ccd2543fSMatthew G Knepley 
1220ccd2543fSMatthew G Knepley   Output Arguments:
1221ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1222ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1223ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1224ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1225ccd2543fSMatthew G Knepley 
1226ccd2543fSMatthew G Knepley   Level: advanced
1227ccd2543fSMatthew G Knepley 
1228ccd2543fSMatthew G Knepley   Fortran Notes:
1229ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1230ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1231ccd2543fSMatthew G Knepley 
12328e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
1233ccd2543fSMatthew G Knepley @*/
12348e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1235ccd2543fSMatthew G Knepley {
123649dc4407SMatthew G. Knepley   PetscInt       depth, dim, coneSize;
1237cb92db44SToby Isaac   DMLabel        depthLabel;
1238ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1239ccd2543fSMatthew G Knepley 
1240ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1241139a35ccSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1242ccd2543fSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1243cb92db44SToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1244cb92db44SToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1245cb92db44SToby Isaac   if (depth == 1 && dim == 1) {
12468e0841e0SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12478e0841e0SMatthew G. Knepley   }
1248ccd2543fSMatthew G Knepley   switch (dim) {
1249cb92db44SToby Isaac   case 0:
1250cb92db44SToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1251cb92db44SToby Isaac     break;
125217fe8556SMatthew G. Knepley   case 1:
125317fe8556SMatthew G. Knepley     ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
125417fe8556SMatthew G. Knepley     break;
1255ccd2543fSMatthew G Knepley   case 2:
1256ccd2543fSMatthew G Knepley     switch (coneSize) {
1257ccd2543fSMatthew G Knepley     case 3:
1258ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1259ccd2543fSMatthew G Knepley       break;
1260ccd2543fSMatthew G Knepley     case 4:
1261ccd2543fSMatthew G Knepley       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1262ccd2543fSMatthew G Knepley       break;
1263ccd2543fSMatthew G Knepley     default:
12648e0841e0SMatthew G. Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1265ccd2543fSMatthew G Knepley     }
1266ccd2543fSMatthew G Knepley     break;
1267ccd2543fSMatthew G Knepley   case 3:
1268ccd2543fSMatthew G Knepley     switch (coneSize) {
1269ccd2543fSMatthew G Knepley     case 4:
1270ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1271ccd2543fSMatthew G Knepley       break;
12728e0841e0SMatthew G. Knepley     case 6: /* Faces */
12738e0841e0SMatthew G. Knepley     case 8: /* Vertices */
1274ccd2543fSMatthew G Knepley       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1275ccd2543fSMatthew G Knepley       break;
1276ccd2543fSMatthew G Knepley     default:
12778e0841e0SMatthew G. Knepley         SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1278ccd2543fSMatthew G Knepley     }
1279ccd2543fSMatthew G Knepley       break;
1280ccd2543fSMatthew G Knepley   default:
1281ccd2543fSMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1282ccd2543fSMatthew G Knepley   }
12838e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
12848e0841e0SMatthew G. Knepley }
12858e0841e0SMatthew G. Knepley 
12868e0841e0SMatthew G. Knepley #undef __FUNCT__
12878e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal"
12888e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
12898e0841e0SMatthew G. Knepley {
12908e0841e0SMatthew G. Knepley   PetscQuadrature  quad;
12918e0841e0SMatthew G. Knepley   PetscSection     coordSection;
12928e0841e0SMatthew G. Knepley   Vec              coordinates;
12938e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
12948e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
12958e0841e0SMatthew G. Knepley   PetscReal       *basisDer;
12968e0841e0SMatthew G. Knepley   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, d, q;
12978e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
12988e0841e0SMatthew G. Knepley 
12998e0841e0SMatthew G. Knepley   PetscFunctionBegin;
13008e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
13018e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
13028e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
13038e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
13048e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
13058e0841e0SMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1306954b1791SMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
13078e0841e0SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
13088e0841e0SMatthew G. Knepley   ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
13098e0841e0SMatthew G. Knepley   *detJ = 0.0;
13108e0841e0SMatthew G. Knepley   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
13118e0841e0SMatthew 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);
13128e0841e0SMatthew G. Knepley   if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);}
13138e0841e0SMatthew G. Knepley   if (J) {
13140790e268SMatthew G. Knepley     ierr = PetscMemzero(J, Nq*cdim*dim*sizeof(PetscReal));CHKERRQ(ierr);
13158e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
13168e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
13178e0841e0SMatthew G. Knepley 
13188e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
13198e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
13208e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
13218e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
132271d6e60fSMatthew G. Knepley             J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
13233bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
13248e0841e0SMatthew G. Knepley       if (cdim > dim) {
13258e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
13268e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
13278e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
13288e0841e0SMatthew G. Knepley       }
13298e0841e0SMatthew G. Knepley       switch (cdim) {
13308e0841e0SMatthew G. Knepley       case 3:
13318e0841e0SMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J);
13328e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
133317fe8556SMatthew G. Knepley         break;
133449dc4407SMatthew G. Knepley       case 2:
13358e0841e0SMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
13368e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
133749dc4407SMatthew G. Knepley         break;
13388e0841e0SMatthew G. Knepley       case 1:
13398e0841e0SMatthew G. Knepley         *detJ = J[0];
13408e0841e0SMatthew G. Knepley         if (invJ) invJ[0] = 1.0/J[0];
134149dc4407SMatthew G. Knepley       }
134249dc4407SMatthew G. Knepley     }
13438e0841e0SMatthew G. Knepley   }
13448e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
13458e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
13468e0841e0SMatthew G. Knepley }
13478e0841e0SMatthew G. Knepley 
13488e0841e0SMatthew G. Knepley #undef __FUNCT__
13498e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
13508e0841e0SMatthew G. Knepley /*@C
13518e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
13528e0841e0SMatthew G. Knepley 
13538e0841e0SMatthew G. Knepley   Collective on DM
13548e0841e0SMatthew G. Knepley 
13558e0841e0SMatthew G. Knepley   Input Arguments:
13568e0841e0SMatthew G. Knepley + dm   - the DM
13578e0841e0SMatthew G. Knepley . cell - the cell
13588e0841e0SMatthew G. Knepley - fe   - the finite element containing the quadrature
13598e0841e0SMatthew G. Knepley 
13608e0841e0SMatthew G. Knepley   Output Arguments:
13618e0841e0SMatthew G. Knepley + v0   - the translation part of this transform
13628e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
13638e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
13648e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
13658e0841e0SMatthew G. Knepley 
13668e0841e0SMatthew G. Knepley   Level: advanced
13678e0841e0SMatthew G. Knepley 
13688e0841e0SMatthew G. Knepley   Fortran Notes:
13698e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
13708e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
13718e0841e0SMatthew G. Knepley 
13728e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
13738e0841e0SMatthew G. Knepley @*/
13748e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
13758e0841e0SMatthew G. Knepley {
13768e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
13778e0841e0SMatthew G. Knepley 
13788e0841e0SMatthew G. Knepley   PetscFunctionBegin;
13798e0841e0SMatthew G. Knepley   if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
13808e0841e0SMatthew G. Knepley   else     {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
1381ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1382ccd2543fSMatthew G Knepley }
1383834e62ceSMatthew G. Knepley 
1384834e62ceSMatthew G. Knepley #undef __FUNCT__
1385cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
1386011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1387cc08537eSMatthew G. Knepley {
1388cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1389cc08537eSMatthew G. Knepley   Vec            coordinates;
1390a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
139106e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1392cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1393cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1394cc08537eSMatthew G. Knepley 
1395cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1396cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
139769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1398cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1399011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
14002e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1401cc08537eSMatthew G. Knepley   if (centroid) {
140206e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
140306e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1404cc08537eSMatthew G. Knepley   }
1405cc08537eSMatthew G. Knepley   if (normal) {
1406a60a936bSMatthew G. Knepley     PetscReal norm;
1407a60a936bSMatthew G. Knepley 
140806e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
140906e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1410a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1411a60a936bSMatthew G. Knepley     normal[0] /= norm;
1412a60a936bSMatthew G. Knepley     normal[1] /= norm;
1413cc08537eSMatthew G. Knepley   }
1414cc08537eSMatthew G. Knepley   if (vol) {
141506e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1416cc08537eSMatthew G. Knepley   }
1417cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1418cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1419cc08537eSMatthew G. Knepley }
1420cc08537eSMatthew G. Knepley 
1421cc08537eSMatthew G. Knepley #undef __FUNCT__
1422cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1423cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1424011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1425cc08537eSMatthew G. Knepley {
1426cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1427cc08537eSMatthew G. Knepley   Vec            coordinates;
1428cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
14290a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
14300a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1431cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1432cc08537eSMatthew G. Knepley 
1433cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1434cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
14350a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
143669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1437cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
14380bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1439ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1440ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1441ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1442ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1443ceee4971SMatthew G. Knepley   }
1444011ea5d8SMatthew G. Knepley   if (normal) {
1445011ea5d8SMatthew G. Knepley     if (dim > 2) {
14461ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
14471ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
14481ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
14490a1d6728SMatthew G. Knepley       PetscReal       norm;
14500a1d6728SMatthew G. Knepley 
14510a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
14520a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
14530a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
14548b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
14550a1d6728SMatthew G. Knepley       normal[0] /= norm;
14560a1d6728SMatthew G. Knepley       normal[1] /= norm;
14570a1d6728SMatthew G. Knepley       normal[2] /= norm;
1458011ea5d8SMatthew G. Knepley     } else {
1459011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1460011ea5d8SMatthew G. Knepley     }
1461011ea5d8SMatthew G. Knepley   }
1462741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
14630a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
14640a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
14650a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14661ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
14671ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
14680a1d6728SMatthew G. Knepley     }
14690a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
14700a1d6728SMatthew G. Knepley     vsum += vtmp;
14710a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14720a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
14730a1d6728SMatthew G. Knepley     }
14740a1d6728SMatthew G. Knepley   }
14750a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
14760a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
14770a1d6728SMatthew G. Knepley   }
14780a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1479ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
14800a1d6728SMatthew G. Knepley   if (centroid) {
14810a1d6728SMatthew G. Knepley     if (dim > 2) {
14820a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14830a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
14840a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
14850a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
14860a1d6728SMatthew G. Knepley         }
14870a1d6728SMatthew G. Knepley       }
14880a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
14890a1d6728SMatthew G. Knepley   }
1490cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1491cc08537eSMatthew G. Knepley }
1492cc08537eSMatthew G. Knepley 
1493cc08537eSMatthew G. Knepley #undef __FUNCT__
14940ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
14950ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1496011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
14970ec8681fSMatthew G. Knepley {
14980ec8681fSMatthew G. Knepley   PetscSection    coordSection;
14990ec8681fSMatthew G. Knepley   Vec             coordinates;
15000ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
150186623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1502a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
15030ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
15040ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
15050ec8681fSMatthew G. Knepley 
15060ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1507f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
15080ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
150969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
15100ec8681fSMatthew G. Knepley 
1511d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
15120ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
15130ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1514a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
15150ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1516011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
15170ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
15180ec8681fSMatthew G. Knepley     switch (numCorners) {
15190ec8681fSMatthew G. Knepley     case 3:
15200ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15211ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
15221ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
15231ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
15240ec8681fSMatthew G. Knepley       }
15250ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1526a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15270ec8681fSMatthew G. Knepley       vsum += vtmp;
15284f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
15290ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15301ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15310ec8681fSMatthew G. Knepley         }
15320ec8681fSMatthew G. Knepley       }
15330ec8681fSMatthew G. Knepley       break;
15340ec8681fSMatthew G. Knepley     case 4:
15350ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
15360ec8681fSMatthew G. Knepley       /* First tet */
15370ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15381ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
15391ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
15401ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
15410ec8681fSMatthew G. Knepley       }
15420ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1543a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15440ec8681fSMatthew G. Knepley       vsum += vtmp;
15450ec8681fSMatthew G. Knepley       if (centroid) {
15460ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15470ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15480ec8681fSMatthew G. Knepley         }
15490ec8681fSMatthew G. Knepley       }
15500ec8681fSMatthew G. Knepley       /* Second tet */
15510ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15521ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
15531ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
15541ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
15550ec8681fSMatthew G. Knepley       }
15560ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1557a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15580ec8681fSMatthew G. Knepley       vsum += vtmp;
15590ec8681fSMatthew G. Knepley       if (centroid) {
15600ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15610ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15620ec8681fSMatthew G. Knepley         }
15630ec8681fSMatthew G. Knepley       }
15640ec8681fSMatthew G. Knepley       break;
15650ec8681fSMatthew G. Knepley     default:
1566796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
15670ec8681fSMatthew G. Knepley     }
15684f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
15690ec8681fSMatthew G. Knepley   }
15708763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
15710ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1572d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
15730ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
15740ec8681fSMatthew G. Knepley }
15750ec8681fSMatthew G. Knepley 
15760ec8681fSMatthew G. Knepley #undef __FUNCT__
1577834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1578834e62ceSMatthew G. Knepley /*@C
1579834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1580834e62ceSMatthew G. Knepley 
1581834e62ceSMatthew G. Knepley   Collective on DM
1582834e62ceSMatthew G. Knepley 
1583834e62ceSMatthew G. Knepley   Input Arguments:
1584834e62ceSMatthew G. Knepley + dm   - the DM
1585834e62ceSMatthew G. Knepley - cell - the cell
1586834e62ceSMatthew G. Knepley 
1587834e62ceSMatthew G. Knepley   Output Arguments:
1588834e62ceSMatthew G. Knepley + volume   - the cell volume
1589cc08537eSMatthew G. Knepley . centroid - the cell centroid
1590cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1591834e62ceSMatthew G. Knepley 
1592834e62ceSMatthew G. Knepley   Level: advanced
1593834e62ceSMatthew G. Knepley 
1594834e62ceSMatthew G. Knepley   Fortran Notes:
1595834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1596834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1597834e62ceSMatthew G. Knepley 
159869d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1599834e62ceSMatthew G. Knepley @*/
1600cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1601834e62ceSMatthew G. Knepley {
16020ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1603834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1604834e62ceSMatthew G. Knepley 
1605834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1606834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1607c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1608834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1609834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1610c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1611834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1612011ea5d8SMatthew G. Knepley   switch (depth) {
1613cc08537eSMatthew G. Knepley   case 1:
1614011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1615cc08537eSMatthew G. Knepley     break;
1616834e62ceSMatthew G. Knepley   case 2:
1617011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1618834e62ceSMatthew G. Knepley     break;
1619834e62ceSMatthew G. Knepley   case 3:
1620011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1621834e62ceSMatthew G. Knepley     break;
1622834e62ceSMatthew G. Knepley   default:
1623834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1624834e62ceSMatthew G. Knepley   }
1625834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1626834e62ceSMatthew G. Knepley }
1627113c68e6SMatthew G. Knepley 
1628113c68e6SMatthew G. Knepley #undef __FUNCT__
1629c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1630c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1631c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1632c0d900a5SMatthew G. Knepley {
1633c0d900a5SMatthew G. Knepley   DM             dmCell;
1634c0d900a5SMatthew G. Knepley   Vec            coordinates;
1635c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1636c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1637c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1638c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1639c0d900a5SMatthew G. Knepley 
1640c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1641c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1642c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1643c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1644c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1645c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1646c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1647c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1648c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1649c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1650c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1651c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
16529e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1653c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1654c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1655c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1656c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1657c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1658c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1659c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1660c0d900a5SMatthew G. Knepley 
1661c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1662c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1663c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1664c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1665c0d900a5SMatthew G. Knepley   }
1666c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1667c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1668c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1669c0d900a5SMatthew G. Knepley }
1670c0d900a5SMatthew G. Knepley 
1671c0d900a5SMatthew G. Knepley #undef __FUNCT__
1672113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1673891a9168SMatthew G. Knepley /*@
1674891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1675891a9168SMatthew G. Knepley 
1676891a9168SMatthew G. Knepley   Input Parameter:
1677891a9168SMatthew G. Knepley . dm - The DM
1678891a9168SMatthew G. Knepley 
1679891a9168SMatthew G. Knepley   Output Parameters:
1680891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1681891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1682891a9168SMatthew G. Knepley 
1683891a9168SMatthew G. Knepley   Level: developer
1684891a9168SMatthew G. Knepley 
1685891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1686891a9168SMatthew G. Knepley @*/
1687113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1688113c68e6SMatthew G. Knepley {
1689113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1690113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1691113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1692113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1693113c68e6SMatthew G. Knepley   Vec            coordinates;
1694113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1695113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1696113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1697113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1698113c68e6SMatthew G. Knepley 
1699113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1700113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1701113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1702113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1703113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1704113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1705113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1706113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1707113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1708113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1709113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1710113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
17119e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1712113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1713113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1714113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1715113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
171606348e87SToby Isaac   if (cEndInterior < 0) {
171706348e87SToby Isaac     cEndInterior = cEnd;
171806348e87SToby Isaac   }
1719113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1720113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1721113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1722113c68e6SMatthew G. Knepley 
1723113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1724113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1725113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1726113c68e6SMatthew G. Knepley   }
1727113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1728113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1729113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1730113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1731113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
17329e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1733113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1734113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1735113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1736113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1737113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1738c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1739113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1740113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1741113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1742113c68e6SMatthew G. Knepley     PetscReal        area;
174350d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
1744113c68e6SMatthew G. Knepley 
17459ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
174650d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
174750d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
1748113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1749113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1750113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1751113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1752113c68e6SMatthew G. Knepley     {
1753113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
175406348e87SToby Isaac       PetscInt         ncells;
1755113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1756113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
17570453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1758113c68e6SMatthew G. Knepley 
1759113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
176006348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
1761113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1762113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
176306348e87SToby Isaac       if (ncells > 1) {
176406348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1765113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
176606348e87SToby Isaac       }
176706348e87SToby Isaac       else {
176806348e87SToby Isaac         rcentroid = fg->centroid;
176906348e87SToby Isaac       }
17702e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
17712e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
17720453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1773113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1774113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1775113c68e6SMatthew G. Knepley       }
1776113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1777113c68e6SMatthew 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]);
1778113c68e6SMatthew 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]);
1779113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1780113c68e6SMatthew G. Knepley       }
1781113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1782113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1783113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1784113c68e6SMatthew G. Knepley       }
178506348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
1786113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
1787113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1788113c68e6SMatthew G. Knepley       }
1789113c68e6SMatthew G. Knepley     }
1790113c68e6SMatthew G. Knepley   }
1791b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1792113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
1793113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
1794113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
1795113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1796113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
1797113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
1798113c68e6SMatthew G. Knepley 
1799113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
1800113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
1801113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
1802113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
180350d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
1804113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
1805113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
1806113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
1807113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
1808113c68e6SMatthew G. Knepley       if (support[s] == c) {
1809640bce14SSatish Balay         PetscFVCellGeom       *ci;
1810113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
1811113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
1812113c68e6SMatthew G. Knepley 
1813113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
1814113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
1815113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
1816113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
1817113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
1818113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
1819113c68e6SMatthew G. Knepley       }
1820113c68e6SMatthew G. Knepley     }
1821113c68e6SMatthew G. Knepley   }
1822113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
1823113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1824113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1825113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
1826113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1827113c68e6SMatthew G. Knepley }
1828113c68e6SMatthew G. Knepley 
1829113c68e6SMatthew G. Knepley #undef __FUNCT__
1830113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
1831113c68e6SMatthew G. Knepley /*@C
1832113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
1833113c68e6SMatthew G. Knepley 
1834113c68e6SMatthew G. Knepley   Not collective
1835113c68e6SMatthew G. Knepley 
1836113c68e6SMatthew G. Knepley   Input Argument:
1837113c68e6SMatthew G. Knepley . dm - the DM
1838113c68e6SMatthew G. Knepley 
1839113c68e6SMatthew G. Knepley   Output Argument:
1840113c68e6SMatthew G. Knepley . minradius - the minium cell radius
1841113c68e6SMatthew G. Knepley 
1842113c68e6SMatthew G. Knepley   Level: developer
1843113c68e6SMatthew G. Knepley 
1844113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
1845113c68e6SMatthew G. Knepley @*/
1846113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
1847113c68e6SMatthew G. Knepley {
1848113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1849113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1850113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
1851113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
1852113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1853113c68e6SMatthew G. Knepley }
1854113c68e6SMatthew G. Knepley 
1855113c68e6SMatthew G. Knepley #undef __FUNCT__
1856113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
1857113c68e6SMatthew G. Knepley /*@C
1858113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
1859113c68e6SMatthew G. Knepley 
1860113c68e6SMatthew G. Knepley   Logically collective
1861113c68e6SMatthew G. Knepley 
1862113c68e6SMatthew G. Knepley   Input Arguments:
1863113c68e6SMatthew G. Knepley + dm - the DM
1864113c68e6SMatthew G. Knepley - minradius - the minium cell radius
1865113c68e6SMatthew G. Knepley 
1866113c68e6SMatthew G. Knepley   Level: developer
1867113c68e6SMatthew G. Knepley 
1868113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
1869113c68e6SMatthew G. Knepley @*/
1870113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
1871113c68e6SMatthew G. Knepley {
1872113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1873113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1874113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
1875113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1876113c68e6SMatthew G. Knepley }
1877856ac710SMatthew G. Knepley 
1878856ac710SMatthew G. Knepley #undef __FUNCT__
1879856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
1880856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1881856ac710SMatthew G. Knepley {
1882856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
1883856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
1884856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
1885856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1886856ac710SMatthew G. Knepley 
1887856ac710SMatthew G. Knepley   PetscFunctionBegin;
1888856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1889856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1890856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1891856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
1892856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1893c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1894856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1895856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
1896856ac710SMatthew G. Knepley     const PetscInt        *faces;
1897856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
1898640bce14SSatish Balay     PetscFVCellGeom        *cg;
1899856ac710SMatthew G. Knepley     PetscBool              boundary;
1900856ac710SMatthew G. Knepley     PetscInt               ghost;
1901856ac710SMatthew G. Knepley 
1902856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1903856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
1904856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
1905856ac710SMatthew 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);
1906856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1907640bce14SSatish Balay       PetscFVCellGeom       *cg1;
1908856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
1909856ac710SMatthew G. Knepley       const PetscInt        *fcells;
1910856ac710SMatthew G. Knepley       PetscInt               ncell, side;
1911856ac710SMatthew G. Knepley 
1912856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1913a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1914856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1915856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
1916856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
1917856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
1918856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
1919856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1920856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
1921856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
1922856ac710SMatthew G. Knepley     }
1923856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
1924856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
1925856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1926856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1927a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1928856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1929856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
1930856ac710SMatthew G. Knepley       ++usedFaces;
1931856ac710SMatthew G. Knepley     }
1932856ac710SMatthew G. Knepley   }
1933856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
1934856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1935856ac710SMatthew G. Knepley }
1936856ac710SMatthew G. Knepley 
1937856ac710SMatthew G. Knepley #undef __FUNCT__
1938b81db932SToby Isaac #define __FUNCT__ "BuildGradientReconstruction_Internal_Tree"
1939b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1940b81db932SToby Isaac {
1941b81db932SToby Isaac   DMLabel        ghostLabel;
1942b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
1943b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
1944b81db932SToby Isaac   PetscSection   neighSec;
1945b81db932SToby Isaac   PetscInt     (*neighbors)[2];
1946b81db932SToby Isaac   PetscInt      *counter;
1947b81db932SToby Isaac   PetscErrorCode ierr;
1948b81db932SToby Isaac 
1949b81db932SToby Isaac   PetscFunctionBegin;
1950b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1951b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1952b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
19535bc680faSToby Isaac   if (cEndInterior < 0) {
19545bc680faSToby Isaac     cEndInterior = cEnd;
19555bc680faSToby Isaac   }
1956b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
1957b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
1958b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1959c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1960b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1961b81db932SToby Isaac     const PetscInt        *fcells;
1962b81db932SToby Isaac     PetscBool              boundary;
19635bc680faSToby Isaac     PetscInt               ghost = -1;
1964b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1965b81db932SToby Isaac 
196606348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1967a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1968b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1969b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1970b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
197106348e87SToby Isaac     if (numCells == 2) {
1972b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
1973b81db932SToby Isaac       for (c = 0; c < 2; c++) {
1974b81db932SToby Isaac         PetscInt cell = fcells[c];
1975b81db932SToby Isaac 
1976e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
1977b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
1978b81db932SToby Isaac         }
1979b81db932SToby Isaac       }
1980b81db932SToby Isaac     }
198106348e87SToby Isaac   }
1982b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
1983b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
1984b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1985b81db932SToby Isaac   nStart = 0;
1986b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
1987b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
1988b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
1989b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1990b81db932SToby Isaac     const PetscInt        *fcells;
1991b81db932SToby Isaac     PetscBool              boundary;
19925bc680faSToby Isaac     PetscInt               ghost = -1;
1993b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1994b81db932SToby Isaac 
199506348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1996a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1997b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1998b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1999b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
200006348e87SToby Isaac     if (numCells == 2) {
2001b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2002b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2003b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2004b81db932SToby Isaac 
2005e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2006b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2007b81db932SToby Isaac           off += counter[cell - cStart]++;
2008b81db932SToby Isaac           neighbors[off][0] = f;
2009b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2010b81db932SToby Isaac         }
2011b81db932SToby Isaac       }
2012b81db932SToby Isaac     }
201306348e87SToby Isaac   }
2014b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2015b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2016b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2017317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2018640bce14SSatish Balay     PetscFVCellGeom        *cg;
2019b81db932SToby Isaac 
2020b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2021b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2022b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2023317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2024317218b9SToby 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);
2025b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2026640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2027b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2028b81db932SToby Isaac       const PetscInt        *fcells;
2029b81db932SToby Isaac       PetscInt               ncell, side, nface;
2030b81db932SToby Isaac 
2031b81db932SToby Isaac       nface = neighbors[off + f][0];
2032b81db932SToby Isaac       ncell = neighbors[off + f][1];
2033b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2034b81db932SToby Isaac       side  = (c != fcells[0]);
2035b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2036b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2037b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2038b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2039b81db932SToby Isaac     }
2040b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2041b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2042b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2043b81db932SToby Isaac     }
2044b81db932SToby Isaac   }
2045b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
20465fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2047b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2048b81db932SToby Isaac   PetscFunctionReturn(0);
2049b81db932SToby Isaac }
2050b81db932SToby Isaac 
2051b81db932SToby Isaac #undef __FUNCT__
2052856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
2053856ac710SMatthew G. Knepley /*@
2054856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2055856ac710SMatthew G. Knepley 
2056856ac710SMatthew G. Knepley   Collective on DM
2057856ac710SMatthew G. Knepley 
2058856ac710SMatthew G. Knepley   Input Arguments:
2059856ac710SMatthew G. Knepley + dm  - The DM
2060856ac710SMatthew G. Knepley . fvm - The PetscFV
20618f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
20628f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2063856ac710SMatthew G. Knepley 
2064856ac710SMatthew G. Knepley   Output Parameters:
2065856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2066856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2067856ac710SMatthew G. Knepley 
2068856ac710SMatthew G. Knepley   Level: developer
2069856ac710SMatthew G. Knepley 
2070856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2071856ac710SMatthew G. Knepley @*/
2072856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2073856ac710SMatthew G. Knepley {
2074856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2075856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2076b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2077856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2078856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2079856ac710SMatthew G. Knepley 
2080856ac710SMatthew G. Knepley   PetscFunctionBegin;
2081856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2082856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2083856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2084856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2085856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2086856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2087856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2088856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2089856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2090b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2091b81db932SToby Isaac   if (!parentSection) {
2092856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2093b5a3613cSMatthew G. Knepley   } else {
2094b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2095b81db932SToby Isaac   }
2096856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2097856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2098856ac710SMatthew G. Knepley   /* Create storage for gradients */
2099856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2100856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2101856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2102856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2103856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2104856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2105856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2106856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2107856ac710SMatthew G. Knepley }
2108b27d5b9eSToby Isaac 
2109b27d5b9eSToby Isaac #undef __FUNCT__
2110b27d5b9eSToby Isaac #define __FUNCT__ "DMPlexGetDataFVM"
2111b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2112b27d5b9eSToby Isaac {
2113b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2114b27d5b9eSToby Isaac   PetscErrorCode ierr;
2115b27d5b9eSToby Isaac 
2116b27d5b9eSToby Isaac   PetscFunctionBegin;
2117b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2118b27d5b9eSToby Isaac   if (!cellgeomobj) {
2119b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2120b27d5b9eSToby Isaac 
2121b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2122b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2123b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2124b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2125b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2126b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2127b27d5b9eSToby Isaac   }
2128b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2129b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2130b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2131b27d5b9eSToby Isaac   if (gradDM) {
2132b27d5b9eSToby Isaac     PetscObject gradobj;
2133b27d5b9eSToby Isaac     PetscBool   computeGradients;
2134b27d5b9eSToby Isaac 
2135b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2136b27d5b9eSToby Isaac     if (!computeGradients) {
2137b27d5b9eSToby Isaac       *gradDM = NULL;
2138b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2139b27d5b9eSToby Isaac     }
2140b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2141b27d5b9eSToby Isaac     if (!gradobj) {
2142b27d5b9eSToby Isaac       DM dmGradInt;
2143b27d5b9eSToby Isaac 
2144b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2145b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2146b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2147b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2148b27d5b9eSToby Isaac     }
2149b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2150b27d5b9eSToby Isaac   }
2151b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2152b27d5b9eSToby Isaac }
2153d6143a4eSToby Isaac 
2154d6143a4eSToby Isaac #undef __FUNCT__
2155*9d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_NewtonUpdate"
2156*9d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
2157*9d150b73SToby Isaac {
2158*9d150b73SToby Isaac   PetscFunctionBeginHot;
2159*9d150b73SToby Isaac 
2160*9d150b73SToby Isaac   PetscInt l, m;
2161*9d150b73SToby Isaac 
2162*9d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
2163*9d150b73SToby Isaac     /* invert Jacobian, multiply */
2164*9d150b73SToby Isaac     PetscScalar det, idet;
2165*9d150b73SToby Isaac 
2166*9d150b73SToby Isaac     switch (dimR) {
2167*9d150b73SToby Isaac     case 1:
2168*9d150b73SToby Isaac       invJ[0] = 1./ J[0];
2169*9d150b73SToby Isaac       break;
2170*9d150b73SToby Isaac     case 2:
2171*9d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
2172*9d150b73SToby Isaac       idet = 1./det;
2173*9d150b73SToby Isaac       invJ[0] =  J[3] * idet;
2174*9d150b73SToby Isaac       invJ[1] = -J[1] * idet;
2175*9d150b73SToby Isaac       invJ[2] = -J[2] * idet;
2176*9d150b73SToby Isaac       invJ[3] =  J[0] * idet;
2177*9d150b73SToby Isaac       break;
2178*9d150b73SToby Isaac     case 3:
2179*9d150b73SToby Isaac       {
2180*9d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
2181*9d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
2182*9d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
2183*9d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
2184*9d150b73SToby Isaac         idet = 1./det;
2185*9d150b73SToby Isaac         invJ[0] *= idet;
2186*9d150b73SToby Isaac         invJ[1] *= idet;
2187*9d150b73SToby Isaac         invJ[2] *= idet;
2188*9d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
2189*9d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
2190*9d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
2191*9d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
2192*9d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
2193*9d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
2194*9d150b73SToby Isaac       }
2195*9d150b73SToby Isaac       break;
2196*9d150b73SToby Isaac     }
2197*9d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
2198*9d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2199*9d150b73SToby Isaac         guess[m] += invJ[l * dimC + m] * resNeg[m];
2200*9d150b73SToby Isaac       }
2201*9d150b73SToby Isaac     }
2202*9d150b73SToby Isaac   } else {
2203*9d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
2204*9d150b73SToby Isaac     char transpose = 'C';
2205*9d150b73SToby Isaac #else
2206*9d150b73SToby Isaac     char transpose = 'T';
2207*9d150b73SToby Isaac #endif
2208*9d150b73SToby Isaac     PetscBLASInt m = dimR;
2209*9d150b73SToby Isaac     PetscBLASInt n = dimC;
2210*9d150b73SToby Isaac     PetscBLASInt one = 1;
2211*9d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
2212*9d150b73SToby Isaac 
2213*9d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
2214*9d150b73SToby Isaac 
2215*9d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
2216*9d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
2217*9d150b73SToby Isaac 
2218*9d150b73SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += invJ[l];}
2219*9d150b73SToby Isaac   }
2220*9d150b73SToby Isaac   PetscFunctionReturn(0);
2221*9d150b73SToby Isaac }
2222*9d150b73SToby Isaac 
2223*9d150b73SToby Isaac #undef __FUNCT__
2224*9d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_Tensor"
2225*9d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
2226*9d150b73SToby Isaac {
2227*9d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 10, numV = (1 << dimR);
2228*9d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
2229*9d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
2230*9d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
2231*9d150b73SToby Isaac   PetscErrorCode ierr;
2232*9d150b73SToby Isaac 
2233*9d150b73SToby Isaac   PetscFunctionBegin;
2234*9d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2235*9d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
2236*9d150b73SToby 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);
2237*9d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
2238*9d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
2239*9d150b73SToby Isaac   cellCoords = &cellData[0];
2240*9d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
2241*9d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
2242*9d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
2243*9d150b73SToby Isaac   invJ       = &J[dimR * dimC];
2244*9d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
2245*9d150b73SToby Isaac   if (dimR == 2) {
2246*9d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
2247*9d150b73SToby Isaac 
2248*9d150b73SToby Isaac     for (i = 0; i < 4; i++) {
2249*9d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
2250*9d150b73SToby Isaac 
2251*9d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
2252*9d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
2253*9d150b73SToby Isaac       }
2254*9d150b73SToby Isaac     }
2255*9d150b73SToby Isaac   } else if (dimR == 3) {
2256*9d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
2257*9d150b73SToby Isaac 
2258*9d150b73SToby Isaac     for (i = 0; i < 8; i++) {
2259*9d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
2260*9d150b73SToby Isaac 
2261*9d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
2262*9d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
2263*9d150b73SToby Isaac       }
2264*9d150b73SToby Isaac     }
2265*9d150b73SToby Isaac   } else {
2266*9d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
2267*9d150b73SToby Isaac   }
2268*9d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
2269*9d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
2270*9d150b73SToby Isaac     PetscReal *swap;
2271*9d150b73SToby Isaac 
2272*9d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
2273*9d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
2274*9d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
2275*9d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
2276*9d150b73SToby Isaac       }
2277*9d150b73SToby Isaac     }
2278*9d150b73SToby Isaac 
2279*9d150b73SToby Isaac     if (i < dimR - 1) {
2280*9d150b73SToby Isaac       swap = cellCoeffs;
2281*9d150b73SToby Isaac       cellCoeffs = cellCoords;
2282*9d150b73SToby Isaac       cellCoords = swap;
2283*9d150b73SToby Isaac     }
2284*9d150b73SToby Isaac   }
2285*9d150b73SToby Isaac   ierr = PetscMemzero(refCoords,numPoints * dimR * sizeof (PetscReal));CHKERRQ(ierr);
2286*9d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
2287*9d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
2288*9d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
2289*9d150b73SToby Isaac 
2290*9d150b73SToby Isaac       /* compute -residual and Jacobian */
2291*9d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
2292*9d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
2293*9d150b73SToby Isaac       for (k = 0; k < numV; k++) {
2294*9d150b73SToby Isaac         PetscReal extCoord = 1.;
2295*9d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
2296*9d150b73SToby Isaac           PetscReal coord = guess[l];
2297*9d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
2298*9d150b73SToby Isaac 
2299*9d150b73SToby Isaac           extCoord *= dep * coord + !dep;
2300*9d150b73SToby Isaac           extJ[l] = dep;
2301*9d150b73SToby Isaac 
2302*9d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
2303*9d150b73SToby Isaac             PetscReal coord = guess[m];
2304*9d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
2305*9d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
2306*9d150b73SToby Isaac 
2307*9d150b73SToby Isaac             extJ[l] *= mult;
2308*9d150b73SToby Isaac           }
2309*9d150b73SToby Isaac         }
2310*9d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
2311*9d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
2312*9d150b73SToby Isaac 
2313*9d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
2314*9d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
2315*9d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
2316*9d150b73SToby Isaac           }
2317*9d150b73SToby Isaac         }
2318*9d150b73SToby Isaac       }
2319*9d150b73SToby Isaac 
2320*9d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
2321*9d150b73SToby Isaac     }
2322*9d150b73SToby Isaac   }
2323*9d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
2324*9d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
2325*9d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
2326*9d150b73SToby Isaac   PetscFunctionReturn(0);
2327*9d150b73SToby Isaac }
2328*9d150b73SToby Isaac 
2329*9d150b73SToby Isaac #undef __FUNCT__
2330*9d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates_Tensor"
2331*9d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
2332*9d150b73SToby Isaac {
2333*9d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
2334*9d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
2335*9d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
2336*9d150b73SToby Isaac   PetscErrorCode ierr;
2337*9d150b73SToby Isaac 
2338*9d150b73SToby Isaac   PetscFunctionBegin;
2339*9d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2340*9d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
2341*9d150b73SToby 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);
2342*9d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
2343*9d150b73SToby Isaac   cellCoords = &cellData[0];
2344*9d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
2345*9d150b73SToby Isaac   if (dimR == 2) {
2346*9d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
2347*9d150b73SToby Isaac 
2348*9d150b73SToby Isaac     for (i = 0; i < 4; i++) {
2349*9d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
2350*9d150b73SToby Isaac 
2351*9d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
2352*9d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
2353*9d150b73SToby Isaac       }
2354*9d150b73SToby Isaac     }
2355*9d150b73SToby Isaac   } else if (dimR == 3) {
2356*9d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
2357*9d150b73SToby Isaac 
2358*9d150b73SToby Isaac     for (i = 0; i < 8; i++) {
2359*9d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
2360*9d150b73SToby Isaac 
2361*9d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
2362*9d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
2363*9d150b73SToby Isaac       }
2364*9d150b73SToby Isaac     }
2365*9d150b73SToby Isaac   } else {
2366*9d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
2367*9d150b73SToby Isaac   }
2368*9d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
2369*9d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
2370*9d150b73SToby Isaac     PetscReal *swap;
2371*9d150b73SToby Isaac 
2372*9d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
2373*9d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
2374*9d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
2375*9d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
2376*9d150b73SToby Isaac       }
2377*9d150b73SToby Isaac     }
2378*9d150b73SToby Isaac 
2379*9d150b73SToby Isaac     if (i < dimR - 1) {
2380*9d150b73SToby Isaac       swap = cellCoeffs;
2381*9d150b73SToby Isaac       cellCoeffs = cellCoords;
2382*9d150b73SToby Isaac       cellCoords = swap;
2383*9d150b73SToby Isaac     }
2384*9d150b73SToby Isaac   }
2385*9d150b73SToby Isaac   ierr = PetscMemzero(realCoords,numPoints * dimC * sizeof (PetscReal));CHKERRQ(ierr);
2386*9d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
2387*9d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
2388*9d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
2389*9d150b73SToby Isaac 
2390*9d150b73SToby Isaac     for (k = 0; k < numV; k++) {
2391*9d150b73SToby Isaac       PetscReal extCoord = 1.;
2392*9d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
2393*9d150b73SToby Isaac         PetscReal coord = guess[l];
2394*9d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
2395*9d150b73SToby Isaac 
2396*9d150b73SToby Isaac         extCoord *= dep * coord + !dep;
2397*9d150b73SToby Isaac       }
2398*9d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
2399*9d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
2400*9d150b73SToby Isaac 
2401*9d150b73SToby Isaac         mapped[l] += coeff * extCoord;
2402*9d150b73SToby Isaac       }
2403*9d150b73SToby Isaac     }
2404*9d150b73SToby Isaac   }
2405*9d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
2406*9d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
2407*9d150b73SToby Isaac   PetscFunctionReturn(0);
2408*9d150b73SToby Isaac }
2409*9d150b73SToby Isaac 
2410*9d150b73SToby Isaac #undef __FUNCT__
2411*9d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_FE"
2412*9d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
2413*9d150b73SToby Isaac {
2414*9d150b73SToby Isaac   PetscInt       numComp, numDof, i, j, k, l, m, maxIter = 10, coordSize;
2415*9d150b73SToby Isaac   PetscScalar    *nodes, *modes, *invV;
2416*9d150b73SToby Isaac   PetscReal      *B;
2417*9d150b73SToby Isaac   PetscReal      *D;
2418*9d150b73SToby Isaac   PetscScalar    *resNeg, *J, *invJ, *work;
2419*9d150b73SToby Isaac   PetscErrorCode ierr;
2420*9d150b73SToby Isaac 
2421*9d150b73SToby Isaac   PetscFunctionBegin;
2422*9d150b73SToby Isaac   ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr);
2423*9d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
2424*9d150b73SToby 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);
2425*9d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
2426*9d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2427*9d150b73SToby Isaac   ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_SCALAR,&modes);CHKERRQ(ierr);
2428*9d150b73SToby Isaac   invV = fe->invV;
2429*9d150b73SToby Isaac   for (i = 0; i < numDof; i++) {
2430*9d150b73SToby Isaac     for (j = 0; j < dimC; j++) {
2431*9d150b73SToby Isaac       modes[i * dimC + j] = 0.;
2432*9d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
2433*9d150b73SToby Isaac         modes[i * dimC + j] += invV[i * numDof + k] * nodes[k * dimC + j];
2434*9d150b73SToby Isaac       }
2435*9d150b73SToby Isaac     }
2436*9d150b73SToby Isaac   }
2437*9d150b73SToby Isaac   ierr = DMGetWorkArray(dm,numDof * numDof,PETSC_REAL,&B);CHKERRQ(ierr);
2438*9d150b73SToby Isaac   ierr = DMGetWorkArray(dm,numDof * dimR,PETSC_REAL,&D);CHKERRQ(ierr);
2439*9d150b73SToby Isaac   ierr = DMGetWorkArray(dm,dimC + 3 * dimC * dimR,PETSC_SCALAR,&resNeg);CHKERRQ(ierr);
2440*9d150b73SToby Isaac   J = &resNeg[dimC];
2441*9d150b73SToby Isaac   invJ = &J[dimC * dimR];
2442*9d150b73SToby Isaac   work = &invJ[dimC * dimR];
2443*9d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
2444*9d150b73SToby 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 */
2445*9d150b73SToby Isaac     for (j = 0; j < numPoints; j++) {
2446*9d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
2447*9d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
2448*9d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[j * dimC + k];}
2449*9d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
2450*9d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
2451*9d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
2452*9d150b73SToby Isaac           resNeg[l] -= modes[k * dimC + l] * B[k];
2453*9d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
2454*9d150b73SToby Isaac             J[l * dimR + m] += modes[k * dimC + l] * D[k * dimR + m];
2455*9d150b73SToby Isaac           }
2456*9d150b73SToby Isaac         }
2457*9d150b73SToby Isaac       }
2458*9d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
2459*9d150b73SToby Isaac     }
2460*9d150b73SToby Isaac   }
2461*9d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC + dimR,PETSC_SCALAR,&resNeg);CHKERRQ(ierr);
2462*9d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm,numDof * numDof * dimR,PETSC_REAL,&D);CHKERRQ(ierr);
2463*9d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm,numDof * numDof,PETSC_REAL,&B);CHKERRQ(ierr);
2464*9d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_SCALAR,&modes);CHKERRQ(ierr);
2465*9d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
2466*9d150b73SToby Isaac   PetscFunctionReturn(0);
2467*9d150b73SToby Isaac }
2468*9d150b73SToby Isaac 
2469*9d150b73SToby Isaac #undef __FUNCT__
2470*9d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates_FE"
2471*9d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
2472*9d150b73SToby Isaac {
2473*9d150b73SToby Isaac   PetscInt       numComp, numDof, i, j, k, l, coordSize;
2474*9d150b73SToby Isaac   PetscScalar    *nodes, *modes, *invV;
2475*9d150b73SToby Isaac   PetscReal      *B;
2476*9d150b73SToby Isaac   PetscErrorCode ierr;
2477*9d150b73SToby Isaac 
2478*9d150b73SToby Isaac   PetscFunctionBegin;
2479*9d150b73SToby Isaac   ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr);
2480*9d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
2481*9d150b73SToby 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);
2482*9d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
2483*9d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2484*9d150b73SToby Isaac   ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_SCALAR,&modes);CHKERRQ(ierr);
2485*9d150b73SToby Isaac   invV = fe->invV;
2486*9d150b73SToby Isaac   for (i = 0; i < numDof; i++) {
2487*9d150b73SToby Isaac     for (j = 0; j < dimC; j++) {
2488*9d150b73SToby Isaac       modes[i * dimC + j] = 0.;
2489*9d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
2490*9d150b73SToby Isaac         modes[i * dimC + j] += invV[i * numDof + k] * nodes[k * dimC + j];
2491*9d150b73SToby Isaac       }
2492*9d150b73SToby Isaac     }
2493*9d150b73SToby Isaac   }
2494*9d150b73SToby Isaac   ierr = DMGetWorkArray(dm,numDof * numDof,PETSC_REAL,&B);CHKERRQ(ierr);
2495*9d150b73SToby Isaac   for (i = 0; i < numPoints * dimC; i++) {realCoords[i] = 0.;}
2496*9d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
2497*9d150b73SToby Isaac     const PetscReal *guess  = &refCoords[j * dimR];
2498*9d150b73SToby Isaac     PetscReal       *mapped = &realCoords[j * dimC];
2499*9d150b73SToby Isaac 
2500*9d150b73SToby Isaac     ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, NULL, NULL);CHKERRQ(ierr);
2501*9d150b73SToby Isaac     for (k = 0; k < numDof; k++) {
2502*9d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
2503*9d150b73SToby Isaac         mapped[l] += modes[k * dimC + l] * B[k];
2504*9d150b73SToby Isaac       }
2505*9d150b73SToby Isaac     }
2506*9d150b73SToby Isaac   }
2507*9d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm,numDof * numDof,PETSC_REAL,&B);CHKERRQ(ierr);
2508*9d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_SCALAR,&modes);CHKERRQ(ierr);
2509*9d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
2510*9d150b73SToby Isaac   PetscFunctionReturn(0);
2511*9d150b73SToby Isaac }
2512*9d150b73SToby Isaac 
2513*9d150b73SToby Isaac #undef __FUNCT__
2514d6143a4eSToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference"
2515d6143a4eSToby Isaac /*@
2516d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
2517d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
2518d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
2519d6143a4eSToby Isaac 
2520d6143a4eSToby Isaac   Not collective
2521d6143a4eSToby Isaac 
2522d6143a4eSToby Isaac   Input Parameters:
2523d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2524d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2525d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
2526d6143a4eSToby Isaac . cell       - the cell whose map is used.
2527d6143a4eSToby Isaac . numPoints  - the number of points to locate
2528d6143a4eSToby Isaac + realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2529d6143a4eSToby Isaac 
2530d6143a4eSToby Isaac   Output Parameters:
2531d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
2532d6143a4eSToby Isaac @*/
2533d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
2534d6143a4eSToby Isaac {
2535*9d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
2536*9d150b73SToby Isaac   DM             coordDM = NULL;
2537*9d150b73SToby Isaac   Vec            coords;
2538*9d150b73SToby Isaac   PetscFE        fe = NULL;
2539*9d150b73SToby Isaac   PetscErrorCode ierr;
2540*9d150b73SToby Isaac 
2541d6143a4eSToby Isaac   PetscFunctionBegin;
2542*9d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2543*9d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
2544*9d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
2545*9d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
2546*9d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
2547*9d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
2548*9d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
2549*9d150b73SToby Isaac   if (coordDM) {
2550*9d150b73SToby Isaac     PetscInt coordFields;
2551*9d150b73SToby Isaac 
2552*9d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
2553*9d150b73SToby Isaac     if (coordFields) {
2554*9d150b73SToby Isaac       PetscClassId id;
2555*9d150b73SToby Isaac       PetscObject  disc;
2556*9d150b73SToby Isaac 
2557*9d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
2558*9d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
2559*9d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
2560*9d150b73SToby Isaac         fe = (PetscFE) disc;
2561*9d150b73SToby Isaac       }
2562*9d150b73SToby Isaac     }
2563*9d150b73SToby Isaac   }
2564*9d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
2565*9d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
2566*9d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
2567*9d150b73SToby 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);
2568*9d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
2569*9d150b73SToby Isaac     PetscInt  coneSize;
2570*9d150b73SToby Isaac     PetscBool isSimplex, isTensor;
2571*9d150b73SToby Isaac 
2572*9d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
2573*9d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
2574*9d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
2575*9d150b73SToby Isaac     if (isSimplex) {
2576*9d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
2577*9d150b73SToby Isaac 
2578*9d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
2579*9d150b73SToby Isaac       J    = &v0[dimC];
2580*9d150b73SToby Isaac       invJ = &J[dimC * dimC];
2581*9d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
2582*9d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
2583*9d150b73SToby Isaac         CoordinatesRealToRef(dimC, dimR, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
2584*9d150b73SToby Isaac       }
2585*9d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
2586*9d150b73SToby Isaac     } else if (isTensor) {
2587*9d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
2588*9d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
2589*9d150b73SToby Isaac   } else {
2590*9d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
2591*9d150b73SToby Isaac   }
2592*9d150b73SToby Isaac   PetscFunctionReturn(0);
2593*9d150b73SToby Isaac }
2594*9d150b73SToby Isaac 
2595*9d150b73SToby Isaac #undef __FUNCT__
2596*9d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates"
2597*9d150b73SToby Isaac /*@
2598*9d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
2599*9d150b73SToby Isaac 
2600*9d150b73SToby Isaac   Not collective
2601*9d150b73SToby Isaac 
2602*9d150b73SToby Isaac   Input Parameters:
2603*9d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2604*9d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2605*9d150b73SToby Isaac                as a multilinear map for tensor-product elements
2606*9d150b73SToby Isaac . cell       - the cell whose map is used.
2607*9d150b73SToby Isaac . numPoints  - the number of points to locate
2608*9d150b73SToby Isaac + refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
2609*9d150b73SToby Isaac 
2610*9d150b73SToby Isaac   Output Parameters:
2611*9d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2612*9d150b73SToby Isaac @*/
2613*9d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
2614*9d150b73SToby Isaac {
2615*9d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
2616*9d150b73SToby Isaac   DM             coordDM = NULL;
2617*9d150b73SToby Isaac   Vec            coords;
2618*9d150b73SToby Isaac   PetscFE        fe = NULL;
2619*9d150b73SToby Isaac   PetscErrorCode ierr;
2620*9d150b73SToby Isaac 
2621*9d150b73SToby Isaac   PetscFunctionBegin;
2622*9d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2623*9d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
2624*9d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
2625*9d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
2626*9d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
2627*9d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
2628*9d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
2629*9d150b73SToby Isaac   if (coordDM) {
2630*9d150b73SToby Isaac     PetscInt coordFields;
2631*9d150b73SToby Isaac 
2632*9d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
2633*9d150b73SToby Isaac     if (coordFields) {
2634*9d150b73SToby Isaac       PetscClassId id;
2635*9d150b73SToby Isaac       PetscObject  disc;
2636*9d150b73SToby Isaac 
2637*9d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
2638*9d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
2639*9d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
2640*9d150b73SToby Isaac         fe = (PetscFE) disc;
2641*9d150b73SToby Isaac       }
2642*9d150b73SToby Isaac     }
2643*9d150b73SToby Isaac   }
2644*9d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
2645*9d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
2646*9d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
2647*9d150b73SToby 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);
2648*9d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
2649*9d150b73SToby Isaac     PetscInt  coneSize;
2650*9d150b73SToby Isaac     PetscBool isSimplex, isTensor;
2651*9d150b73SToby Isaac 
2652*9d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
2653*9d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
2654*9d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
2655*9d150b73SToby Isaac     if (isSimplex) {
2656*9d150b73SToby Isaac       PetscReal detJ, *v0, *J;
2657*9d150b73SToby Isaac 
2658*9d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
2659*9d150b73SToby Isaac       J    = &v0[dimC];
2660*9d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
2661*9d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
2662*9d150b73SToby Isaac         CoordinatesRefToReal(dimC, dimR, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
2663*9d150b73SToby Isaac       }
2664*9d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
2665*9d150b73SToby Isaac     } else if (isTensor) {
2666*9d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
2667*9d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
2668*9d150b73SToby Isaac   } else {
2669*9d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
2670*9d150b73SToby Isaac   }
2671d6143a4eSToby Isaac   PetscFunctionReturn(0);
2672d6143a4eSToby Isaac }
2673