xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision c6e120d1ab0bdcb659d7587b8fdef27a052dabad)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
29d150b73SToby Isaac #include <petsc/private/petscfeimpl.h>  /*I      "petscfe.h"       I*/
39d150b73SToby Isaac #include <petscblaslapack.h>
4ccd2543fSMatthew G Knepley 
5ccd2543fSMatthew G Knepley #undef __FUNCT__
6fea14342SMatthew G. Knepley #define __FUNCT__ "DMPlexGetLineIntersection_2D_Internal"
7fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
8fea14342SMatthew G. Knepley {
9fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*2+0];
10fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*2+1];
11fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*2+0];
12fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*2+1];
13fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0*2+0];
14fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0*2+1];
15fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1*2+0];
16fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1*2+1];
17fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
18fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
19fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
20fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
21fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
22fea14342SMatthew G. Knepley 
23fea14342SMatthew G. Knepley   PetscFunctionBegin;
24fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
25fea14342SMatthew G. Knepley   /* Non-parallel lines */
26fea14342SMatthew G. Knepley   if (denom != 0.0) {
27fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
28fea14342SMatthew G. Knepley     const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
29fea14342SMatthew G. Knepley 
30fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
31fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
32fea14342SMatthew G. Knepley       if (intersection) {
33fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
34fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
35fea14342SMatthew G. Knepley       }
36fea14342SMatthew G. Knepley     }
37fea14342SMatthew G. Knepley   }
38fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
39fea14342SMatthew G. Knepley }
40fea14342SMatthew G. Knepley 
41fea14342SMatthew G. Knepley #undef __FUNCT__
42ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal"
43ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
44ccd2543fSMatthew G Knepley {
45ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
46f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
47ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
48ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
49ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
50ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
51ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
52ccd2543fSMatthew G Knepley 
53ccd2543fSMatthew G Knepley   PetscFunctionBegin;
548e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
55ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
56ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
57ccd2543fSMatthew G Knepley 
58f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
59c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
60ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
61ccd2543fSMatthew G Knepley }
62ccd2543fSMatthew G Knepley 
63ccd2543fSMatthew G Knepley #undef __FUNCT__
6462a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Simplex_2D_Internal"
6562a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
6662a38674SMatthew G. Knepley {
6762a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
6862a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
6962a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
7062a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
7162a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
7262a38674SMatthew G. Knepley   PetscErrorCode  ierr;
7362a38674SMatthew G. Knepley 
7462a38674SMatthew G. Knepley   PetscFunctionBegin;
7562a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
7662a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
7762a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
7862a38674SMatthew G. Knepley 
7962a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
8062a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
8162a38674SMatthew G. Knepley   r   = (xi + eta)/2.0;
8262a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
8362a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
8462a38674SMatthew G. Knepley     xi  /= r;
8562a38674SMatthew G. Knepley     eta /= r;
8662a38674SMatthew G. Knepley   }
8762a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
8862a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
8962a38674SMatthew G. Knepley   PetscFunctionReturn(0);
9062a38674SMatthew G. Knepley }
9162a38674SMatthew G. Knepley 
9262a38674SMatthew G. Knepley #undef __FUNCT__
93ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal"
94ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
95ccd2543fSMatthew G Knepley {
96ccd2543fSMatthew G Knepley   PetscSection       coordSection;
97ccd2543fSMatthew G Knepley   Vec             coordsLocal;
98a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
99ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
100ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
101ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
102ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
103ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
104ccd2543fSMatthew G Knepley 
105ccd2543fSMatthew G Knepley   PetscFunctionBegin;
106ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
10769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
108ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
109ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
110ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
111ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
112ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
113ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
114ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
115ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
116ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
117ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
118ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
119ccd2543fSMatthew G Knepley   }
120ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
121c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
122ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
123ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
124ccd2543fSMatthew G Knepley }
125ccd2543fSMatthew G Knepley 
126ccd2543fSMatthew G Knepley #undef __FUNCT__
127ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal"
128ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
129ccd2543fSMatthew G Knepley {
130ccd2543fSMatthew G Knepley   const PetscInt embedDim = 3;
131ccd2543fSMatthew G Knepley   PetscReal      v0[3], J[9], invJ[9], detJ;
132ccd2543fSMatthew G Knepley   PetscReal      x = PetscRealPart(point[0]);
133ccd2543fSMatthew G Knepley   PetscReal      y = PetscRealPart(point[1]);
134ccd2543fSMatthew G Knepley   PetscReal      z = PetscRealPart(point[2]);
135ccd2543fSMatthew G Knepley   PetscReal      xi, eta, zeta;
136ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
137ccd2543fSMatthew G Knepley 
138ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1398e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
140ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
141ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
142ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
143ccd2543fSMatthew G Knepley 
144ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c;
145c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
146ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
147ccd2543fSMatthew G Knepley }
148ccd2543fSMatthew G Knepley 
149ccd2543fSMatthew G Knepley #undef __FUNCT__
150ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal"
151ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
152ccd2543fSMatthew G Knepley {
153ccd2543fSMatthew G Knepley   PetscSection   coordSection;
154ccd2543fSMatthew G Knepley   Vec            coordsLocal;
1557c1f9639SMatthew G Knepley   PetscScalar   *coords;
156fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
157fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
158ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
159ccd2543fSMatthew G Knepley   PetscInt       f;
160ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
161ccd2543fSMatthew G Knepley 
162ccd2543fSMatthew G Knepley   PetscFunctionBegin;
163ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
16469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
165ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
166ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
167ccd2543fSMatthew G Knepley     /* Check the point is under plane */
168ccd2543fSMatthew G Knepley     /*   Get face normal */
169ccd2543fSMatthew G Knepley     PetscReal v_i[3];
170ccd2543fSMatthew G Knepley     PetscReal v_j[3];
171ccd2543fSMatthew G Knepley     PetscReal normal[3];
172ccd2543fSMatthew G Knepley     PetscReal pp[3];
173ccd2543fSMatthew G Knepley     PetscReal dot;
174ccd2543fSMatthew G Knepley 
175ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
176ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
177ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
178ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
179ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
180ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
181ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
182ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
183ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
184ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
185ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
186ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
187ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
188ccd2543fSMatthew G Knepley 
189ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
190ccd2543fSMatthew G Knepley     if (dot < 0.0) {
191ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
192ccd2543fSMatthew G Knepley       break;
193ccd2543fSMatthew G Knepley     }
194ccd2543fSMatthew G Knepley   }
195ccd2543fSMatthew G Knepley   if (found) *cell = c;
196c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
197ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
198ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
199ccd2543fSMatthew G Knepley }
200ccd2543fSMatthew G Knepley 
201ccd2543fSMatthew G Knepley #undef __FUNCT__
202c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal"
203c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
204c4eade1cSMatthew G. Knepley {
205c4eade1cSMatthew G. Knepley   PetscInt d;
206c4eade1cSMatthew G. Knepley 
207c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
208c4eade1cSMatthew G. Knepley   box->dim = dim;
209c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
210c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
211c4eade1cSMatthew G. Knepley }
212c4eade1cSMatthew G. Knepley 
213c4eade1cSMatthew G. Knepley #undef __FUNCT__
214c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate"
215c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
216c4eade1cSMatthew G. Knepley {
217c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
218c4eade1cSMatthew G. Knepley 
219c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
220c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
221c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
222c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
223c4eade1cSMatthew G. Knepley }
224c4eade1cSMatthew G. Knepley 
225c4eade1cSMatthew G. Knepley #undef __FUNCT__
226c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge"
227c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
228c4eade1cSMatthew G. Knepley {
229c4eade1cSMatthew G. Knepley   PetscInt d;
230c4eade1cSMatthew G. Knepley 
231c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
232c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
233c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
234c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
235c4eade1cSMatthew G. Knepley   }
236c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
237c4eade1cSMatthew G. Knepley }
238c4eade1cSMatthew G. Knepley 
239c4eade1cSMatthew G. Knepley #undef __FUNCT__
240c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid"
24162a38674SMatthew G. Knepley /*
24262a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
24362a38674SMatthew G. Knepley 
24462a38674SMatthew G. Knepley   Not collective
24562a38674SMatthew G. Knepley 
24662a38674SMatthew G. Knepley   Input Parameters:
24762a38674SMatthew G. Knepley + box - The grid hash object
24862a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
24962a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
25062a38674SMatthew G. Knepley 
25162a38674SMatthew G. Knepley   Level: developer
25262a38674SMatthew G. Knepley 
25362a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
25462a38674SMatthew G. Knepley */
255c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
256c4eade1cSMatthew G. Knepley {
257c4eade1cSMatthew G. Knepley   PetscInt d;
258c4eade1cSMatthew G. Knepley 
259c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
260c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
261c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
262c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
263c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
264c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
265c4eade1cSMatthew G. Knepley     } else {
266c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
267c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
268c4eade1cSMatthew G. Knepley     }
269c4eade1cSMatthew G. Knepley   }
270c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
271c4eade1cSMatthew G. Knepley }
272c4eade1cSMatthew G. Knepley 
273c4eade1cSMatthew G. Knepley #undef __FUNCT__
274c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox"
27562a38674SMatthew G. Knepley /*
27662a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
27762a38674SMatthew G. Knepley 
27862a38674SMatthew G. Knepley   Not collective
27962a38674SMatthew G. Knepley 
28062a38674SMatthew G. Knepley   Input Parameters:
28162a38674SMatthew G. Knepley + box       - The grid hash object
28262a38674SMatthew G. Knepley . numPoints - The number of input points
28362a38674SMatthew G. Knepley - points    - The input point coordinates
28462a38674SMatthew G. Knepley 
28562a38674SMatthew G. Knepley   Output Parameters:
28662a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
28762a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
28862a38674SMatthew G. Knepley 
28962a38674SMatthew G. Knepley   Level: developer
29062a38674SMatthew G. Knepley 
29162a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
29262a38674SMatthew G. Knepley */
2931c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
294c4eade1cSMatthew G. Knepley {
295c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
296c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
297c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
298c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
299c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
300c4eade1cSMatthew G. Knepley   PetscInt         d, p;
301c4eade1cSMatthew G. Knepley 
302c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
303c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
304c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
3051c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
306c4eade1cSMatthew G. Knepley 
3071c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
308c4eade1cSMatthew G. Knepley       if (dbox < 0 || dbox >= n[d]) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %d (%g, %g, %g) is outside of our bounding box",
3091c6dfc3eSMatthew G. Knepley                                              p, PetscRealPart(points[p*dim+0]), dim > 1 ? PetscRealPart(points[p*dim+1]) : 0.0, dim > 2 ? PetscRealPart(points[p*dim+2]) : 0.0);
310c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
311c4eade1cSMatthew G. Knepley     }
312c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
313c4eade1cSMatthew G. Knepley   }
314c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
315c4eade1cSMatthew G. Knepley }
316c4eade1cSMatthew G. Knepley 
317c4eade1cSMatthew G. Knepley #undef __FUNCT__
318c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy"
319c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
320c4eade1cSMatthew G. Knepley {
321c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
322c4eade1cSMatthew G. Knepley 
323c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
324c4eade1cSMatthew G. Knepley   if (*box) {
325c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
326c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
327c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
328c4eade1cSMatthew G. Knepley   }
329c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
330c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
331c4eade1cSMatthew G. Knepley }
332c4eade1cSMatthew G. Knepley 
333cafe43deSMatthew G. Knepley #undef __FUNCT__
334cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexLocatePoint_Internal"
335cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
336cafe43deSMatthew G. Knepley {
337cafe43deSMatthew G. Knepley   PetscInt       coneSize;
338cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
339cafe43deSMatthew G. Knepley 
340cafe43deSMatthew G. Knepley   PetscFunctionBegin;
341cafe43deSMatthew G. Knepley   switch (dim) {
342cafe43deSMatthew G. Knepley   case 2:
343cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
344cafe43deSMatthew G. Knepley     switch (coneSize) {
345cafe43deSMatthew G. Knepley     case 3:
346cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
347cafe43deSMatthew G. Knepley       break;
348cafe43deSMatthew G. Knepley     case 4:
349cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
350cafe43deSMatthew G. Knepley       break;
351cafe43deSMatthew G. Knepley     default:
352cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
353cafe43deSMatthew G. Knepley     }
354cafe43deSMatthew G. Knepley     break;
355cafe43deSMatthew G. Knepley   case 3:
356cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
357cafe43deSMatthew G. Knepley     switch (coneSize) {
358cafe43deSMatthew G. Knepley     case 4:
359cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
360cafe43deSMatthew G. Knepley       break;
361cafe43deSMatthew G. Knepley     case 6:
362cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
363cafe43deSMatthew G. Knepley       break;
364cafe43deSMatthew G. Knepley     default:
365cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
366cafe43deSMatthew G. Knepley     }
367cafe43deSMatthew G. Knepley     break;
368cafe43deSMatthew G. Knepley   default:
369cafe43deSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
370cafe43deSMatthew G. Knepley   }
371cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
372cafe43deSMatthew G. Knepley }
373cafe43deSMatthew G. Knepley 
374cafe43deSMatthew G. Knepley #undef __FUNCT__
37562a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Internal"
37662a38674SMatthew G. Knepley /*
37762a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
37862a38674SMatthew G. Knepley */
37962a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
38062a38674SMatthew G. Knepley {
38162a38674SMatthew G. Knepley   PetscInt       coneSize;
38262a38674SMatthew G. Knepley   PetscErrorCode ierr;
38362a38674SMatthew G. Knepley 
38462a38674SMatthew G. Knepley   PetscFunctionBegin;
38562a38674SMatthew G. Knepley   switch (dim) {
38662a38674SMatthew G. Knepley   case 2:
38762a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
38862a38674SMatthew G. Knepley     switch (coneSize) {
38962a38674SMatthew G. Knepley     case 3:
39062a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
39162a38674SMatthew G. Knepley       break;
39262a38674SMatthew G. Knepley #if 0
39362a38674SMatthew G. Knepley     case 4:
39462a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
39562a38674SMatthew G. Knepley       break;
39662a38674SMatthew G. Knepley #endif
39762a38674SMatthew G. Knepley     default:
39862a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
39962a38674SMatthew G. Knepley     }
40062a38674SMatthew G. Knepley     break;
40162a38674SMatthew G. Knepley #if 0
40262a38674SMatthew G. Knepley   case 3:
40362a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
40462a38674SMatthew G. Knepley     switch (coneSize) {
40562a38674SMatthew G. Knepley     case 4:
40662a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
40762a38674SMatthew G. Knepley       break;
40862a38674SMatthew G. Knepley     case 6:
40962a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
41062a38674SMatthew G. Knepley       break;
41162a38674SMatthew G. Knepley     default:
41262a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
41362a38674SMatthew G. Knepley     }
41462a38674SMatthew G. Knepley     break;
41562a38674SMatthew G. Knepley #endif
41662a38674SMatthew G. Knepley   default:
41762a38674SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for mesh dimension %D", dim);
41862a38674SMatthew G. Knepley   }
41962a38674SMatthew G. Knepley   PetscFunctionReturn(0);
42062a38674SMatthew G. Knepley }
42162a38674SMatthew G. Knepley 
42262a38674SMatthew G. Knepley #undef __FUNCT__
423cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGridHash_Internal"
42462a38674SMatthew G. Knepley /*
42562a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
42662a38674SMatthew G. Knepley 
42762a38674SMatthew G. Knepley   Collective on DM
42862a38674SMatthew G. Knepley 
42962a38674SMatthew G. Knepley   Input Parameter:
43062a38674SMatthew G. Knepley . dm - The Plex
43162a38674SMatthew G. Knepley 
43262a38674SMatthew G. Knepley   Output Parameter:
43362a38674SMatthew G. Knepley . localBox - The grid hash object
43462a38674SMatthew G. Knepley 
43562a38674SMatthew G. Knepley   Level: developer
43662a38674SMatthew G. Knepley 
43762a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
43862a38674SMatthew G. Knepley */
439cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
440cafe43deSMatthew G. Knepley {
441cafe43deSMatthew G. Knepley   MPI_Comm           comm;
442cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
443cafe43deSMatthew G. Knepley   Vec                coordinates;
444cafe43deSMatthew G. Knepley   PetscSection       coordSection;
445cafe43deSMatthew G. Knepley   Vec                coordsLocal;
446cafe43deSMatthew G. Knepley   const PetscScalar *coords;
447722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
448cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
4491d0c6c94SMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, cMax, c, i;
450cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
451cafe43deSMatthew G. Knepley 
452cafe43deSMatthew G. Knepley   PetscFunctionBegin;
453cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
454cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
455cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
4565b3353d8SMatthew G. Knepley   if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D");
457cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
458cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
459cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
460cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
461cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
462cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
463cafe43deSMatthew G. Knepley #if 0
464cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
465b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
466b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
467cafe43deSMatthew G. Knepley #endif
468cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
469cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
470cafe43deSMatthew G. Knepley   /* Create label */
471cafe43deSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4721d0c6c94SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
4731d0c6c94SMatthew G. Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
474cafe43deSMatthew G. Knepley   ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr);
475cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
476722d0f5cSMatthew G. Knepley   /* Compute boxes which overlap each cell: http://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
477cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
478cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
47938353de4SMatthew G. Knepley   ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr);
480cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
481cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
482cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
48338353de4SMatthew G. Knepley     PetscInt         csize   = 0;
484cafe43deSMatthew G. Knepley     PetscScalar      point[3];
485cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
486cafe43deSMatthew G. Knepley 
487cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
48838353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
48938353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
490722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
49138353de4SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);}
492cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
493cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
4942291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
495cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
496cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
497cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
498cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
499cafe43deSMatthew G. Knepley       }
500cafe43deSMatthew G. Knepley     }
501fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
502cafe43deSMatthew G. Knepley     for (k = dlim[2*2+0], point[2] = lbox->lower[2] + k*h[2]; k <= dlim[2*2+1]; ++k, point[2] += h[2]) {
503cafe43deSMatthew G. Knepley       for (j = dlim[1*2+0], point[1] = lbox->lower[1] + j*h[1]; j <= dlim[1*2+1]; ++j, point[1] += h[1]) {
504cafe43deSMatthew G. Knepley         for (i = dlim[0*2+0], point[0] = lbox->lower[0] + i*h[0]; i <= dlim[0*2+1]; ++i, point[0] += h[0]) {
505cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
506cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
507fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
508cafe43deSMatthew G. Knepley 
509fea14342SMatthew G. Knepley           /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
510cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
511cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
512cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
513cafe43deSMatthew G. Knepley 
514cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
515cafe43deSMatthew G. Knepley                 if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
516cafe43deSMatthew G. Knepley               }
517cafe43deSMatthew G. Knepley             }
518cafe43deSMatthew G. Knepley           }
519fea14342SMatthew G. Knepley           /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */
520fea14342SMatthew G. Knepley           for (edge = 0; edge < dim+1; ++edge) {
521fea14342SMatthew G. Knepley             PetscReal segA[6], segB[6];
522fea14342SMatthew G. Knepley 
523fea14342SMatthew G. Knepley             for (d = 0; d < dim; ++d) {segA[d] = PetscRealPart(ccoords[edge*dim+d]); segA[dim+d] = PetscRealPart(ccoords[((edge+1)%(dim+1))*dim+d]);}
524fea14342SMatthew G. Knepley             for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) {
5259a128ed2SMatthew G. Knepley               if (dim > 2) {segB[2]     = PetscRealPart(point[2]);
5269a128ed2SMatthew G. Knepley                             segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];}
527fea14342SMatthew G. Knepley               for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) {
5289a128ed2SMatthew G. Knepley                 if (dim > 1) {segB[1]     = PetscRealPart(point[1]);
5299a128ed2SMatthew G. Knepley                               segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];}
530fea14342SMatthew G. Knepley                 for (ii = 0; ii < 2; ++ii) {
531fea14342SMatthew G. Knepley                   PetscBool intersects;
532fea14342SMatthew G. Knepley 
5339a128ed2SMatthew G. Knepley                   segB[0]     = PetscRealPart(point[0]);
5349a128ed2SMatthew G. Knepley                   segB[dim+0] = PetscRealPart(point[0]) + ii*h[0];
535fea14342SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
536fea14342SMatthew G. Knepley                   if (intersects) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;}
537cafe43deSMatthew G. Knepley                 }
538cafe43deSMatthew G. Knepley               }
539cafe43deSMatthew G. Knepley             }
540cafe43deSMatthew G. Knepley           }
541fea14342SMatthew G. Knepley         }
542fea14342SMatthew G. Knepley       }
543fea14342SMatthew G. Knepley     }
544fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
545fea14342SMatthew G. Knepley   }
546722d0f5cSMatthew G. Knepley   ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr);
547cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
548cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
549cafe43deSMatthew G. Knepley   *localBox = lbox;
550cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
551cafe43deSMatthew G. Knepley }
552cafe43deSMatthew G. Knepley 
553cafe43deSMatthew G. Knepley #undef __FUNCT__
554ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex"
55562a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
556ccd2543fSMatthew G Knepley {
557cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
558953fc75cSMatthew G. Knepley   PetscBool       hash = mesh->useHashLocation;
5593a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
5601318edbeSMatthew G. Knepley   PetscInt        dim, cStart, cEnd, cMax, numCells, c;
561cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
5623a93e3b7SToby Isaac   PetscSFNode    *cells;
563ccd2543fSMatthew G Knepley   PetscScalar    *a;
5643a93e3b7SToby Isaac   PetscMPIInt     result;
565ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
566ccd2543fSMatthew G Knepley 
567ccd2543fSMatthew G Knepley   PetscFunctionBegin;
568080342d1SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && !hash) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Nearest point location only supported with grid hashing. Use -dm_plex_hash_location to enable it.");
569cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
570cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
5713a93e3b7SToby Isaac   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr);
5723a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
573cafe43deSMatthew G. Knepley   if (bs != dim) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Block size for point vector %D must be the mesh coordinate dimension %D", bs, dim);
574ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
575ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
576ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
577ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
578ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
579ccd2543fSMatthew G Knepley   numPoints /= bs;
580785e854fSJed Brown   ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
581953fc75cSMatthew G. Knepley   if (hash) {
582ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
583cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
584cafe43deSMatthew G. Knepley     /* Send points to correct process */
585cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
586cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
587cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
588953fc75cSMatthew G. Knepley   }
5893a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
590ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
591953fc75cSMatthew G. Knepley     PetscInt           dbin[3], bin, cell = -1, cellOffset;
592ccd2543fSMatthew G Knepley 
593e9b685f5SMatthew G. Knepley     cells[p].rank  = 0;
594e9b685f5SMatthew G. Knepley     cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
595953fc75cSMatthew G. Knepley     if (hash) {
596cafe43deSMatthew G. Knepley       ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
597cafe43deSMatthew G. Knepley       /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
598cafe43deSMatthew G. Knepley       ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
599cafe43deSMatthew G. Knepley       ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
600cafe43deSMatthew G. Knepley       for (c = cellOffset; c < cellOffset + numCells; ++c) {
601cafe43deSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
6023a93e3b7SToby Isaac         if (cell >= 0) {
6033a93e3b7SToby Isaac           cells[p].rank = 0;
6043a93e3b7SToby Isaac           cells[p].index = cell;
6053a93e3b7SToby Isaac           numFound++;
6063a93e3b7SToby Isaac           break;
607ccd2543fSMatthew G Knepley         }
6083a93e3b7SToby Isaac       }
609953fc75cSMatthew G. Knepley     } else {
610953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
611953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
6123a93e3b7SToby Isaac         if (cell >= 0) {
6133a93e3b7SToby Isaac           cells[p].rank = 0;
6143a93e3b7SToby Isaac           cells[p].index = cell;
6153a93e3b7SToby Isaac           numFound++;
6163a93e3b7SToby Isaac           break;
617953fc75cSMatthew G. Knepley         }
618953fc75cSMatthew G. Knepley       }
6193a93e3b7SToby Isaac     }
620ccd2543fSMatthew G Knepley   }
621953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
62262a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
62362a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
62462a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
62562a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
626b716b415SMatthew G. Knepley       PetscInt           dbin[3], bin, cellOffset, d;
62762a38674SMatthew G. Knepley 
628e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
62962a38674SMatthew G. Knepley         ++numFound;
63062a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
63162a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
63262a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
63362a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
63462a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
635b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
63662a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
63762a38674SMatthew G. Knepley           if (dist < distMax) {
63862a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
63962a38674SMatthew G. Knepley             cells[p].rank  = 0;
64062a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
64162a38674SMatthew G. Knepley             distMax = dist;
64262a38674SMatthew G. Knepley             break;
64362a38674SMatthew G. Knepley           }
64462a38674SMatthew G. Knepley         }
64562a38674SMatthew G. Knepley       }
64662a38674SMatthew G. Knepley     }
64762a38674SMatthew G. Knepley   }
64862a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
649cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
6502d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
6513a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
6523a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
6533a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
6543a93e3b7SToby Isaac         if (numFound < p) {
6553a93e3b7SToby Isaac           cells[numFound] = cells[p];
6563a93e3b7SToby Isaac         }
6573a93e3b7SToby Isaac         found[numFound++] = p;
6583a93e3b7SToby Isaac       }
6593a93e3b7SToby Isaac     }
6603a93e3b7SToby Isaac   }
66162a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
6623a93e3b7SToby Isaac   ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
663ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
664ccd2543fSMatthew G Knepley }
665ccd2543fSMatthew G Knepley 
666ccd2543fSMatthew G Knepley #undef __FUNCT__
667741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D"
668741bfc07SMatthew G. Knepley /*@C
669741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
670741bfc07SMatthew G. Knepley 
671741bfc07SMatthew G. Knepley   Not collective
672741bfc07SMatthew G. Knepley 
673741bfc07SMatthew G. Knepley   Input Parameter:
674741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
675741bfc07SMatthew G. Knepley 
676741bfc07SMatthew G. Knepley   Output Parameters:
677741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x
678741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
679741bfc07SMatthew G. Knepley 
680741bfc07SMatthew G. Knepley   Level: developer
681741bfc07SMatthew G. Knepley 
682741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
683741bfc07SMatthew G. Knepley @*/
684741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
68517fe8556SMatthew G. Knepley {
68617fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
68717fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
6888b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
68917fe8556SMatthew G. Knepley 
69017fe8556SMatthew G. Knepley   PetscFunctionBegin;
6911c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
6921c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
69317fe8556SMatthew G. Knepley   coords[0] = 0.0;
6947f07f362SMatthew G. Knepley   coords[1] = r;
69517fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
69617fe8556SMatthew G. Knepley }
69717fe8556SMatthew G. Knepley 
69817fe8556SMatthew G. Knepley #undef __FUNCT__
699741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto1D"
700741bfc07SMatthew G. Knepley /*@C
701741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
70228dbe442SToby Isaac 
703741bfc07SMatthew G. Knepley   Not collective
70428dbe442SToby Isaac 
705741bfc07SMatthew G. Knepley   Input Parameter:
706741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
707741bfc07SMatthew G. Knepley 
708741bfc07SMatthew G. Knepley   Output Parameters:
709741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x and z
710741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
711741bfc07SMatthew G. Knepley 
712741bfc07SMatthew G. Knepley   Note: This uses the basis completion described by Frisvad in http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html, DOI:10.1080/2165347X.2012.689606
713741bfc07SMatthew G. Knepley 
714741bfc07SMatthew G. Knepley   Level: developer
715741bfc07SMatthew G. Knepley 
716741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
717741bfc07SMatthew G. Knepley @*/
718741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
71928dbe442SToby Isaac {
72028dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
72128dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
72228dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
72328dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
72428dbe442SToby Isaac   PetscReal      rinv = 1. / r;
72528dbe442SToby Isaac   PetscFunctionBegin;
72628dbe442SToby Isaac 
72728dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
72828dbe442SToby Isaac   if (x > 0.) {
72928dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
73028dbe442SToby Isaac 
73128dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
73228dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
73328dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
73428dbe442SToby Isaac   }
73528dbe442SToby Isaac   else {
73628dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
73728dbe442SToby Isaac 
73828dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
73928dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
74028dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
74128dbe442SToby Isaac   }
74228dbe442SToby Isaac   coords[0] = 0.0;
74328dbe442SToby Isaac   coords[1] = r;
74428dbe442SToby Isaac   PetscFunctionReturn(0);
74528dbe442SToby Isaac }
74628dbe442SToby Isaac 
74728dbe442SToby Isaac #undef __FUNCT__
748741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D"
749741bfc07SMatthew G. Knepley /*@
750741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto2D - Rewrite coordinates to be the 2D projection of the 3D coordinates
751741bfc07SMatthew G. Knepley 
752741bfc07SMatthew G. Knepley   Not collective
753741bfc07SMatthew G. Knepley 
754741bfc07SMatthew G. Knepley   Input Parameter:
755741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
756741bfc07SMatthew G. Knepley 
757741bfc07SMatthew G. Knepley   Output Parameters:
758741bfc07SMatthew G. Knepley + coords - The new y- and z-coordinates, and 0 for x
759741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
760741bfc07SMatthew G. Knepley 
761741bfc07SMatthew G. Knepley   Level: developer
762741bfc07SMatthew G. Knepley 
763741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
764741bfc07SMatthew G. Knepley @*/
765741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
766ccd2543fSMatthew G Knepley {
7671ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
76899dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
7694a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
770ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
77199dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
772ccd2543fSMatthew G Knepley 
773ccd2543fSMatthew G Knepley   PetscFunctionBegin;
774ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
775ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
7761ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
7771ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
778ccd2543fSMatthew G Knepley   }
779ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
780ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
781ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
7828b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
783ccd2543fSMatthew G Knepley   n[0] /= norm;
784ccd2543fSMatthew G Knepley   n[1] /= norm;
785ccd2543fSMatthew G Knepley   n[2] /= norm;
786ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
787ccd2543fSMatthew G Knepley 
788ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
789ccd2543fSMatthew G Knepley 
790ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
791ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
792ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
793ccd2543fSMatthew G Knepley 
794ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
795ccd2543fSMatthew G Knepley   */
7968b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
79773868372SMatthew G. Knepley   /* Check for n = z */
79873868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
7997df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
8007df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
80199dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
80299dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
8037df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
80473868372SMatthew G. Knepley     }
80599dec3a6SMatthew G. Knepley     coords[0] = 0.0;
80699dec3a6SMatthew G. Knepley     coords[1] = 0.0;
8077df32b8bSSanderA     coords[2] = x1[0];
8087df32b8bSSanderA     coords[3] = x1[1] * s;
8097df32b8bSSanderA     coords[4] = x2[0];
8107df32b8bSSanderA     coords[5] = x2[1] * s;
8117df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
8127df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
8137df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
81473868372SMatthew G. Knepley     PetscFunctionReturn(0);
81573868372SMatthew G. Knepley   }
816da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
817ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
818ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
819ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
820ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
821ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
822ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
823ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
824ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
825ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
826ccd2543fSMatthew G Knepley     }
827ccd2543fSMatthew G Knepley   }
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;
1295f960e424SToby Isaac   PetscReal       *basisDer, *basis, detJt;
1296f960e424SToby Isaac   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, 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);
1308f960e424SToby Isaac   ierr = PetscFEGetDefaultTabulation(fe, &basis, &basisDer, NULL);CHKERRQ(ierr);
13098e0841e0SMatthew G. Knepley   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
13108e0841e0SMatthew 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);
1311f960e424SToby Isaac   if (v0) {
1312f960e424SToby Isaac     ierr = PetscMemzero(v0, Nq*cdim*sizeof(PetscReal));CHKERRQ(ierr);
1313f960e424SToby Isaac     for (q = 0; q < Nq; ++q) {
1314f960e424SToby Isaac       PetscInt i, k;
1315f960e424SToby Isaac 
1316f960e424SToby Isaac       for (k = 0; k < pdim; ++k)
1317f960e424SToby Isaac         for (i = 0; i < cdim; ++i)
1318f960e424SToby Isaac           v0[q*cdim + i] += basis[q*pdim + k] * PetscRealPart(coords[k*cdim + i]);
1319f960e424SToby Isaac       ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1320f960e424SToby Isaac     }
1321f960e424SToby Isaac   }
13228e0841e0SMatthew G. Knepley   if (J) {
13230790e268SMatthew G. Knepley     ierr = PetscMemzero(J, Nq*cdim*dim*sizeof(PetscReal));CHKERRQ(ierr);
13248e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
13258e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
13268e0841e0SMatthew G. Knepley 
13278e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
13288e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
13298e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
13308e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
133171d6e60fSMatthew G. Knepley             J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
13323bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
13338e0841e0SMatthew G. Knepley       if (cdim > dim) {
13348e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
13358e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
13368e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
13378e0841e0SMatthew G. Knepley       }
1338f960e424SToby Isaac       if (!detJ && !invJ) continue;
1339a63b72c6SToby Isaac       detJt = 0.;
13408e0841e0SMatthew G. Knepley       switch (cdim) {
13418e0841e0SMatthew G. Knepley       case 3:
1342037dc194SToby Isaac         DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]);
1343037dc194SToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
134417fe8556SMatthew G. Knepley         break;
134549dc4407SMatthew G. Knepley       case 2:
13469f328543SToby Isaac         DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]);
1347037dc194SToby Isaac         if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
134849dc4407SMatthew G. Knepley         break;
13498e0841e0SMatthew G. Knepley       case 1:
1350037dc194SToby Isaac         detJt = J[q*cdim*dim];
1351037dc194SToby Isaac         if (invJ) invJ[q*cdim*dim] = 1.0/detJt;
135249dc4407SMatthew G. Knepley       }
1353f960e424SToby Isaac       if (detJ) detJ[q] = detJt;
135449dc4407SMatthew G. Knepley     }
135549dc4407SMatthew G. Knepley   }
1356037dc194SToby Isaac   else if (detJ || invJ) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
13578e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
13588e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
13598e0841e0SMatthew G. Knepley }
13608e0841e0SMatthew G. Knepley 
13618e0841e0SMatthew G. Knepley #undef __FUNCT__
13628e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
13638e0841e0SMatthew G. Knepley /*@C
13648e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
13658e0841e0SMatthew G. Knepley 
13668e0841e0SMatthew G. Knepley   Collective on DM
13678e0841e0SMatthew G. Knepley 
13688e0841e0SMatthew G. Knepley   Input Arguments:
13698e0841e0SMatthew G. Knepley + dm   - the DM
13708e0841e0SMatthew G. Knepley . cell - the cell
13718e0841e0SMatthew G. Knepley - fe   - the finite element containing the quadrature
13728e0841e0SMatthew G. Knepley 
13738e0841e0SMatthew G. Knepley   Output Arguments:
1374f960e424SToby Isaac + v0   - if fe != NULL, the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
13758e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
13768e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
13778e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
13788e0841e0SMatthew G. Knepley 
13798e0841e0SMatthew G. Knepley   Level: advanced
13808e0841e0SMatthew G. Knepley 
13818e0841e0SMatthew G. Knepley   Fortran Notes:
13828e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
13838e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
13848e0841e0SMatthew G. Knepley 
13858e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
13868e0841e0SMatthew G. Knepley @*/
13878e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
13888e0841e0SMatthew G. Knepley {
13898e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
13908e0841e0SMatthew G. Knepley 
13918e0841e0SMatthew G. Knepley   PetscFunctionBegin;
13928e0841e0SMatthew G. Knepley   if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
13938e0841e0SMatthew G. Knepley   else     {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
1394ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1395ccd2543fSMatthew G Knepley }
1396834e62ceSMatthew G. Knepley 
1397834e62ceSMatthew G. Knepley #undef __FUNCT__
1398cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
1399011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1400cc08537eSMatthew G. Knepley {
1401cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1402cc08537eSMatthew G. Knepley   Vec            coordinates;
1403a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
140406e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1405cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1406cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1407cc08537eSMatthew G. Knepley 
1408cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1409cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
141069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1411cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1412011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
14132e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1414cc08537eSMatthew G. Knepley   if (centroid) {
141506e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
141606e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1417cc08537eSMatthew G. Knepley   }
1418cc08537eSMatthew G. Knepley   if (normal) {
1419a60a936bSMatthew G. Knepley     PetscReal norm;
1420a60a936bSMatthew G. Knepley 
142106e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
142206e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1423a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1424a60a936bSMatthew G. Knepley     normal[0] /= norm;
1425a60a936bSMatthew G. Knepley     normal[1] /= norm;
1426cc08537eSMatthew G. Knepley   }
1427cc08537eSMatthew G. Knepley   if (vol) {
142806e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1429cc08537eSMatthew G. Knepley   }
1430cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1431cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1432cc08537eSMatthew G. Knepley }
1433cc08537eSMatthew G. Knepley 
1434cc08537eSMatthew G. Knepley #undef __FUNCT__
1435cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1436cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1437011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1438cc08537eSMatthew G. Knepley {
1439cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1440cc08537eSMatthew G. Knepley   Vec            coordinates;
1441cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
14420a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
14430a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1444cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1445cc08537eSMatthew G. Knepley 
1446cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1447cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
14480a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
144969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1450cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
14510bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1452ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1453ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1454ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1455ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1456ceee4971SMatthew G. Knepley   }
1457011ea5d8SMatthew G. Knepley   if (normal) {
1458011ea5d8SMatthew G. Knepley     if (dim > 2) {
14591ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
14601ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
14611ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
14620a1d6728SMatthew G. Knepley       PetscReal       norm;
14630a1d6728SMatthew G. Knepley 
14640a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
14650a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
14660a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
14678b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
14680a1d6728SMatthew G. Knepley       normal[0] /= norm;
14690a1d6728SMatthew G. Knepley       normal[1] /= norm;
14700a1d6728SMatthew G. Knepley       normal[2] /= norm;
1471011ea5d8SMatthew G. Knepley     } else {
1472011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1473011ea5d8SMatthew G. Knepley     }
1474011ea5d8SMatthew G. Knepley   }
1475741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
14760a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
14770a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
14780a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14791ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
14801ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
14810a1d6728SMatthew G. Knepley     }
14820a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
14830a1d6728SMatthew G. Knepley     vsum += vtmp;
14840a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14850a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
14860a1d6728SMatthew G. Knepley     }
14870a1d6728SMatthew G. Knepley   }
14880a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
14890a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
14900a1d6728SMatthew G. Knepley   }
14910a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1492ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
14930a1d6728SMatthew G. Knepley   if (centroid) {
14940a1d6728SMatthew G. Knepley     if (dim > 2) {
14950a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14960a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
14970a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
14980a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
14990a1d6728SMatthew G. Knepley         }
15000a1d6728SMatthew G. Knepley       }
15010a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
15020a1d6728SMatthew G. Knepley   }
1503cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1504cc08537eSMatthew G. Knepley }
1505cc08537eSMatthew G. Knepley 
1506cc08537eSMatthew G. Knepley #undef __FUNCT__
15070ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
15080ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1509011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
15100ec8681fSMatthew G. Knepley {
15110ec8681fSMatthew G. Knepley   PetscSection    coordSection;
15120ec8681fSMatthew G. Knepley   Vec             coordinates;
15130ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
151486623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1515a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
15160ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
15170ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
15180ec8681fSMatthew G. Knepley 
15190ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1520f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
15210ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
152269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
15230ec8681fSMatthew G. Knepley 
1524d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
15250ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
15260ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1527a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
15280ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1529011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
15300ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
15310ec8681fSMatthew G. Knepley     switch (numCorners) {
15320ec8681fSMatthew G. Knepley     case 3:
15330ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15341ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
15351ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
15361ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
15370ec8681fSMatthew G. Knepley       }
15380ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1539a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15400ec8681fSMatthew G. Knepley       vsum += vtmp;
15414f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
15420ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15431ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15440ec8681fSMatthew G. Knepley         }
15450ec8681fSMatthew G. Knepley       }
15460ec8681fSMatthew G. Knepley       break;
15470ec8681fSMatthew G. Knepley     case 4:
15480ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
15490ec8681fSMatthew G. Knepley       /* First tet */
15500ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15511ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
15521ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
15531ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
15540ec8681fSMatthew G. Knepley       }
15550ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1556a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15570ec8681fSMatthew G. Knepley       vsum += vtmp;
15580ec8681fSMatthew G. Knepley       if (centroid) {
15590ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15600ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15610ec8681fSMatthew G. Knepley         }
15620ec8681fSMatthew G. Knepley       }
15630ec8681fSMatthew G. Knepley       /* Second tet */
15640ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15651ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
15661ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
15671ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
15680ec8681fSMatthew G. Knepley       }
15690ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1570a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15710ec8681fSMatthew G. Knepley       vsum += vtmp;
15720ec8681fSMatthew G. Knepley       if (centroid) {
15730ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15740ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15750ec8681fSMatthew G. Knepley         }
15760ec8681fSMatthew G. Knepley       }
15770ec8681fSMatthew G. Knepley       break;
15780ec8681fSMatthew G. Knepley     default:
1579796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
15800ec8681fSMatthew G. Knepley     }
15814f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
15820ec8681fSMatthew G. Knepley   }
15838763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
15840ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1585d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
15860ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
15870ec8681fSMatthew G. Knepley }
15880ec8681fSMatthew G. Knepley 
15890ec8681fSMatthew G. Knepley #undef __FUNCT__
1590834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1591834e62ceSMatthew G. Knepley /*@C
1592834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1593834e62ceSMatthew G. Knepley 
1594834e62ceSMatthew G. Knepley   Collective on DM
1595834e62ceSMatthew G. Knepley 
1596834e62ceSMatthew G. Knepley   Input Arguments:
1597834e62ceSMatthew G. Knepley + dm   - the DM
1598834e62ceSMatthew G. Knepley - cell - the cell
1599834e62ceSMatthew G. Knepley 
1600834e62ceSMatthew G. Knepley   Output Arguments:
1601834e62ceSMatthew G. Knepley + volume   - the cell volume
1602cc08537eSMatthew G. Knepley . centroid - the cell centroid
1603cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1604834e62ceSMatthew G. Knepley 
1605834e62ceSMatthew G. Knepley   Level: advanced
1606834e62ceSMatthew G. Knepley 
1607834e62ceSMatthew G. Knepley   Fortran Notes:
1608834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1609834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1610834e62ceSMatthew G. Knepley 
161169d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1612834e62ceSMatthew G. Knepley @*/
1613cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1614834e62ceSMatthew G. Knepley {
16150ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1616834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1617834e62ceSMatthew G. Knepley 
1618834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1619834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1620c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1621834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1622834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1623c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1624834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1625011ea5d8SMatthew G. Knepley   switch (depth) {
1626cc08537eSMatthew G. Knepley   case 1:
1627011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1628cc08537eSMatthew G. Knepley     break;
1629834e62ceSMatthew G. Knepley   case 2:
1630011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1631834e62ceSMatthew G. Knepley     break;
1632834e62ceSMatthew G. Knepley   case 3:
1633011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1634834e62ceSMatthew G. Knepley     break;
1635834e62ceSMatthew G. Knepley   default:
1636834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1637834e62ceSMatthew G. Knepley   }
1638834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1639834e62ceSMatthew G. Knepley }
1640113c68e6SMatthew G. Knepley 
1641113c68e6SMatthew G. Knepley #undef __FUNCT__
1642c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1643c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1644c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1645c0d900a5SMatthew G. Knepley {
1646c0d900a5SMatthew G. Knepley   DM             dmCell;
1647c0d900a5SMatthew G. Knepley   Vec            coordinates;
1648c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1649c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1650c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1651c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1652c0d900a5SMatthew G. Knepley 
1653c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1654c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1655c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1656c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1657c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1658c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1659c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1660c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1661c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1662c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1663c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1664c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
16659e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1666c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1667c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1668c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1669c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1670c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1671c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1672c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1673c0d900a5SMatthew G. Knepley 
1674c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1675c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1676c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1677c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1678c0d900a5SMatthew G. Knepley   }
1679c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1680c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1681c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1682c0d900a5SMatthew G. Knepley }
1683c0d900a5SMatthew G. Knepley 
1684c0d900a5SMatthew G. Knepley #undef __FUNCT__
1685113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1686891a9168SMatthew G. Knepley /*@
1687891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1688891a9168SMatthew G. Knepley 
1689891a9168SMatthew G. Knepley   Input Parameter:
1690891a9168SMatthew G. Knepley . dm - The DM
1691891a9168SMatthew G. Knepley 
1692891a9168SMatthew G. Knepley   Output Parameters:
1693891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1694891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1695891a9168SMatthew G. Knepley 
1696891a9168SMatthew G. Knepley   Level: developer
1697891a9168SMatthew G. Knepley 
1698891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1699891a9168SMatthew G. Knepley @*/
1700113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1701113c68e6SMatthew G. Knepley {
1702113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1703113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1704113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1705113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1706113c68e6SMatthew G. Knepley   Vec            coordinates;
1707113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1708113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1709113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1710113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1711113c68e6SMatthew G. Knepley 
1712113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1713113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1714113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1715113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1716113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1717113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1718113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1719113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1720113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1721113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1722113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1723113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
17249e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1725113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1726113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1727113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1728113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
172906348e87SToby Isaac   if (cEndInterior < 0) {
173006348e87SToby Isaac     cEndInterior = cEnd;
173106348e87SToby Isaac   }
1732113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1733113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1734113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1735113c68e6SMatthew G. Knepley 
1736113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1737113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1738113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1739113c68e6SMatthew G. Knepley   }
1740113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1741113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1742113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1743113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1744113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
17459e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1746113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1747113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1748113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1749113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1750113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1751c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1752113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1753113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1754113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1755113c68e6SMatthew G. Knepley     PetscReal        area;
175650d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
1757113c68e6SMatthew G. Knepley 
17589ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
175950d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
176050d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
1761113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1762113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1763113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1764113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1765113c68e6SMatthew G. Knepley     {
1766113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
176706348e87SToby Isaac       PetscInt         ncells;
1768113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1769113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
17700453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1771113c68e6SMatthew G. Knepley 
1772113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
177306348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
1774113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1775113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
177606348e87SToby Isaac       if (ncells > 1) {
177706348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1778113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
177906348e87SToby Isaac       }
178006348e87SToby Isaac       else {
178106348e87SToby Isaac         rcentroid = fg->centroid;
178206348e87SToby Isaac       }
17832e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
17842e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
17850453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1786113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1787113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1788113c68e6SMatthew G. Knepley       }
1789113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1790113c68e6SMatthew 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]);
1791113c68e6SMatthew 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]);
1792113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1793113c68e6SMatthew G. Knepley       }
1794113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1795113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1796113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1797113c68e6SMatthew G. Knepley       }
179806348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
1799113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
1800113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1801113c68e6SMatthew G. Knepley       }
1802113c68e6SMatthew G. Knepley     }
1803113c68e6SMatthew G. Knepley   }
1804b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1805113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
1806113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
1807113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
1808113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1809113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
1810113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
1811113c68e6SMatthew G. Knepley 
1812113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
1813113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
1814113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
1815113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
181650d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
1817113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
1818113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
1819113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
1820113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
1821113c68e6SMatthew G. Knepley       if (support[s] == c) {
1822640bce14SSatish Balay         PetscFVCellGeom       *ci;
1823113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
1824113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
1825113c68e6SMatthew G. Knepley 
1826113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
1827113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
1828113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
1829113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
1830113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
1831113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
1832113c68e6SMatthew G. Knepley       }
1833113c68e6SMatthew G. Knepley     }
1834113c68e6SMatthew G. Knepley   }
1835113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
1836113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1837113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1838113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
1839113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1840113c68e6SMatthew G. Knepley }
1841113c68e6SMatthew G. Knepley 
1842113c68e6SMatthew G. Knepley #undef __FUNCT__
1843113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
1844113c68e6SMatthew G. Knepley /*@C
1845113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
1846113c68e6SMatthew G. Knepley 
1847113c68e6SMatthew G. Knepley   Not collective
1848113c68e6SMatthew G. Knepley 
1849113c68e6SMatthew G. Knepley   Input Argument:
1850113c68e6SMatthew G. Knepley . dm - the DM
1851113c68e6SMatthew G. Knepley 
1852113c68e6SMatthew G. Knepley   Output Argument:
1853113c68e6SMatthew G. Knepley . minradius - the minium cell radius
1854113c68e6SMatthew G. Knepley 
1855113c68e6SMatthew G. Knepley   Level: developer
1856113c68e6SMatthew G. Knepley 
1857113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
1858113c68e6SMatthew G. Knepley @*/
1859113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
1860113c68e6SMatthew G. Knepley {
1861113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1862113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1863113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
1864113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
1865113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1866113c68e6SMatthew G. Knepley }
1867113c68e6SMatthew G. Knepley 
1868113c68e6SMatthew G. Knepley #undef __FUNCT__
1869113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
1870113c68e6SMatthew G. Knepley /*@C
1871113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
1872113c68e6SMatthew G. Knepley 
1873113c68e6SMatthew G. Knepley   Logically collective
1874113c68e6SMatthew G. Knepley 
1875113c68e6SMatthew G. Knepley   Input Arguments:
1876113c68e6SMatthew G. Knepley + dm - the DM
1877113c68e6SMatthew G. Knepley - minradius - the minium cell radius
1878113c68e6SMatthew G. Knepley 
1879113c68e6SMatthew G. Knepley   Level: developer
1880113c68e6SMatthew G. Knepley 
1881113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
1882113c68e6SMatthew G. Knepley @*/
1883113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
1884113c68e6SMatthew G. Knepley {
1885113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1886113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1887113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
1888113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1889113c68e6SMatthew G. Knepley }
1890856ac710SMatthew G. Knepley 
1891856ac710SMatthew G. Knepley #undef __FUNCT__
1892856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
1893856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1894856ac710SMatthew G. Knepley {
1895856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
1896856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
1897856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
1898856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1899856ac710SMatthew G. Knepley 
1900856ac710SMatthew G. Knepley   PetscFunctionBegin;
1901856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1902856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1903856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1904856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
1905856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1906c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1907856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1908856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
1909856ac710SMatthew G. Knepley     const PetscInt        *faces;
1910856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
1911640bce14SSatish Balay     PetscFVCellGeom        *cg;
1912856ac710SMatthew G. Knepley     PetscBool              boundary;
1913856ac710SMatthew G. Knepley     PetscInt               ghost;
1914856ac710SMatthew G. Knepley 
1915856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1916856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
1917856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
1918856ac710SMatthew 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);
1919856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1920640bce14SSatish Balay       PetscFVCellGeom       *cg1;
1921856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
1922856ac710SMatthew G. Knepley       const PetscInt        *fcells;
1923856ac710SMatthew G. Knepley       PetscInt               ncell, side;
1924856ac710SMatthew G. Knepley 
1925856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1926a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1927856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1928856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
1929856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
1930856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
1931856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
1932856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1933856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
1934856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
1935856ac710SMatthew G. Knepley     }
1936856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
1937856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
1938856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1939856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1940a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1941856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1942856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
1943856ac710SMatthew G. Knepley       ++usedFaces;
1944856ac710SMatthew G. Knepley     }
1945856ac710SMatthew G. Knepley   }
1946856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
1947856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1948856ac710SMatthew G. Knepley }
1949856ac710SMatthew G. Knepley 
1950856ac710SMatthew G. Knepley #undef __FUNCT__
1951b81db932SToby Isaac #define __FUNCT__ "BuildGradientReconstruction_Internal_Tree"
1952b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1953b81db932SToby Isaac {
1954b81db932SToby Isaac   DMLabel        ghostLabel;
1955b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
1956b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
1957b81db932SToby Isaac   PetscSection   neighSec;
1958b81db932SToby Isaac   PetscInt     (*neighbors)[2];
1959b81db932SToby Isaac   PetscInt      *counter;
1960b81db932SToby Isaac   PetscErrorCode ierr;
1961b81db932SToby Isaac 
1962b81db932SToby Isaac   PetscFunctionBegin;
1963b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1964b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1965b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
19665bc680faSToby Isaac   if (cEndInterior < 0) {
19675bc680faSToby Isaac     cEndInterior = cEnd;
19685bc680faSToby Isaac   }
1969b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
1970b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
1971b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1972c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1973b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1974b81db932SToby Isaac     const PetscInt        *fcells;
1975b81db932SToby Isaac     PetscBool              boundary;
19765bc680faSToby Isaac     PetscInt               ghost = -1;
1977b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1978b81db932SToby Isaac 
197906348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1980a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1981b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1982b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1983b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
198406348e87SToby Isaac     if (numCells == 2) {
1985b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
1986b81db932SToby Isaac       for (c = 0; c < 2; c++) {
1987b81db932SToby Isaac         PetscInt cell = fcells[c];
1988b81db932SToby Isaac 
1989e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
1990b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
1991b81db932SToby Isaac         }
1992b81db932SToby Isaac       }
1993b81db932SToby Isaac     }
199406348e87SToby Isaac   }
1995b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
1996b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
1997b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1998b81db932SToby Isaac   nStart = 0;
1999b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2000b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2001b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2002b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2003b81db932SToby Isaac     const PetscInt        *fcells;
2004b81db932SToby Isaac     PetscBool              boundary;
20055bc680faSToby Isaac     PetscInt               ghost = -1;
2006b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2007b81db932SToby Isaac 
200806348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2009a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2010b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2011b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2012b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
201306348e87SToby Isaac     if (numCells == 2) {
2014b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2015b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2016b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2017b81db932SToby Isaac 
2018e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2019b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2020b81db932SToby Isaac           off += counter[cell - cStart]++;
2021b81db932SToby Isaac           neighbors[off][0] = f;
2022b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2023b81db932SToby Isaac         }
2024b81db932SToby Isaac       }
2025b81db932SToby Isaac     }
202606348e87SToby Isaac   }
2027b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2028b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2029b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2030317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2031640bce14SSatish Balay     PetscFVCellGeom        *cg;
2032b81db932SToby Isaac 
2033b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2034b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2035b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2036317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2037317218b9SToby 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);
2038b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2039640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2040b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2041b81db932SToby Isaac       const PetscInt        *fcells;
2042b81db932SToby Isaac       PetscInt               ncell, side, nface;
2043b81db932SToby Isaac 
2044b81db932SToby Isaac       nface = neighbors[off + f][0];
2045b81db932SToby Isaac       ncell = neighbors[off + f][1];
2046b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2047b81db932SToby Isaac       side  = (c != fcells[0]);
2048b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2049b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2050b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2051b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2052b81db932SToby Isaac     }
2053b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2054b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2055b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2056b81db932SToby Isaac     }
2057b81db932SToby Isaac   }
2058b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
20595fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2060b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2061b81db932SToby Isaac   PetscFunctionReturn(0);
2062b81db932SToby Isaac }
2063b81db932SToby Isaac 
2064b81db932SToby Isaac #undef __FUNCT__
2065856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
2066856ac710SMatthew G. Knepley /*@
2067856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2068856ac710SMatthew G. Knepley 
2069856ac710SMatthew G. Knepley   Collective on DM
2070856ac710SMatthew G. Knepley 
2071856ac710SMatthew G. Knepley   Input Arguments:
2072856ac710SMatthew G. Knepley + dm  - The DM
2073856ac710SMatthew G. Knepley . fvm - The PetscFV
20748f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
20758f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2076856ac710SMatthew G. Knepley 
2077856ac710SMatthew G. Knepley   Output Parameters:
2078856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2079856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2080856ac710SMatthew G. Knepley 
2081856ac710SMatthew G. Knepley   Level: developer
2082856ac710SMatthew G. Knepley 
2083856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2084856ac710SMatthew G. Knepley @*/
2085856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2086856ac710SMatthew G. Knepley {
2087856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2088856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2089b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2090856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2091856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2092856ac710SMatthew G. Knepley 
2093856ac710SMatthew G. Knepley   PetscFunctionBegin;
2094856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2095856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2096856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2097856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2098856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2099856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2100856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2101856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2102856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2103b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2104b81db932SToby Isaac   if (!parentSection) {
2105856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2106b5a3613cSMatthew G. Knepley   } else {
2107b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2108b81db932SToby Isaac   }
2109856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2110856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2111856ac710SMatthew G. Knepley   /* Create storage for gradients */
2112856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2113856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2114856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2115856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2116856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2117856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2118856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2119856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2120856ac710SMatthew G. Knepley }
2121b27d5b9eSToby Isaac 
2122b27d5b9eSToby Isaac #undef __FUNCT__
2123b27d5b9eSToby Isaac #define __FUNCT__ "DMPlexGetDataFVM"
2124b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2125b27d5b9eSToby Isaac {
2126b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2127b27d5b9eSToby Isaac   PetscErrorCode ierr;
2128b27d5b9eSToby Isaac 
2129b27d5b9eSToby Isaac   PetscFunctionBegin;
2130b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2131b27d5b9eSToby Isaac   if (!cellgeomobj) {
2132b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2133b27d5b9eSToby Isaac 
2134b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2135b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2136b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2137b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2138b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2139b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2140b27d5b9eSToby Isaac   }
2141b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2142b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2143b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2144b27d5b9eSToby Isaac   if (gradDM) {
2145b27d5b9eSToby Isaac     PetscObject gradobj;
2146b27d5b9eSToby Isaac     PetscBool   computeGradients;
2147b27d5b9eSToby Isaac 
2148b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2149b27d5b9eSToby Isaac     if (!computeGradients) {
2150b27d5b9eSToby Isaac       *gradDM = NULL;
2151b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2152b27d5b9eSToby Isaac     }
2153b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2154b27d5b9eSToby Isaac     if (!gradobj) {
2155b27d5b9eSToby Isaac       DM dmGradInt;
2156b27d5b9eSToby Isaac 
2157b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2158b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2159b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2160b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2161b27d5b9eSToby Isaac     }
2162b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2163b27d5b9eSToby Isaac   }
2164b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2165b27d5b9eSToby Isaac }
2166d6143a4eSToby Isaac 
2167d6143a4eSToby Isaac #undef __FUNCT__
21689d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_NewtonUpdate"
21699d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
21709d150b73SToby Isaac {
21719d150b73SToby Isaac   PetscFunctionBeginHot;
21729d150b73SToby Isaac 
21739d150b73SToby Isaac   PetscInt l, m;
21749d150b73SToby Isaac 
21759d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
21769d150b73SToby Isaac     /* invert Jacobian, multiply */
21779d150b73SToby Isaac     PetscScalar det, idet;
21789d150b73SToby Isaac 
21799d150b73SToby Isaac     switch (dimR) {
21809d150b73SToby Isaac     case 1:
21819d150b73SToby Isaac       invJ[0] = 1./ J[0];
21829d150b73SToby Isaac       break;
21839d150b73SToby Isaac     case 2:
21849d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
21859d150b73SToby Isaac       idet = 1./det;
21869d150b73SToby Isaac       invJ[0] =  J[3] * idet;
21879d150b73SToby Isaac       invJ[1] = -J[1] * idet;
21889d150b73SToby Isaac       invJ[2] = -J[2] * idet;
21899d150b73SToby Isaac       invJ[3] =  J[0] * idet;
21909d150b73SToby Isaac       break;
21919d150b73SToby Isaac     case 3:
21929d150b73SToby Isaac       {
21939d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
21949d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
21959d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
21969d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
21979d150b73SToby Isaac         idet = 1./det;
21989d150b73SToby Isaac         invJ[0] *= idet;
21999d150b73SToby Isaac         invJ[1] *= idet;
22009d150b73SToby Isaac         invJ[2] *= idet;
22019d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
22029d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
22039d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
22049d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
22059d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
22069d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
22079d150b73SToby Isaac       }
22089d150b73SToby Isaac       break;
22099d150b73SToby Isaac     }
22109d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
22119d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2212*c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
22139d150b73SToby Isaac       }
22149d150b73SToby Isaac     }
22159d150b73SToby Isaac   } else {
22169d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
22179d150b73SToby Isaac     char transpose = 'C';
22189d150b73SToby Isaac #else
22199d150b73SToby Isaac     char transpose = 'T';
22209d150b73SToby Isaac #endif
22219d150b73SToby Isaac     PetscBLASInt m = dimR;
22229d150b73SToby Isaac     PetscBLASInt n = dimC;
22239d150b73SToby Isaac     PetscBLASInt one = 1;
22249d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
22259d150b73SToby Isaac 
22269d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
22279d150b73SToby Isaac 
22289d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
22299d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
22309d150b73SToby Isaac 
2231*c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
22329d150b73SToby Isaac   }
22339d150b73SToby Isaac   PetscFunctionReturn(0);
22349d150b73SToby Isaac }
22359d150b73SToby Isaac 
22369d150b73SToby Isaac #undef __FUNCT__
22379d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_Tensor"
22389d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
22399d150b73SToby Isaac {
2240c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
22419d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
22429d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
22439d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
22449d150b73SToby Isaac   PetscErrorCode ierr;
22459d150b73SToby Isaac 
22469d150b73SToby Isaac   PetscFunctionBegin;
22479d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22489d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
22499d150b73SToby 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);
22509d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
22519d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
22529d150b73SToby Isaac   cellCoords = &cellData[0];
22539d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
22549d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
22559d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
22569d150b73SToby Isaac   invJ       = &J[dimR * dimC];
22579d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
22589d150b73SToby Isaac   if (dimR == 2) {
22599d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
22609d150b73SToby Isaac 
22619d150b73SToby Isaac     for (i = 0; i < 4; i++) {
22629d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
22639d150b73SToby Isaac 
22649d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
22659d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
22669d150b73SToby Isaac       }
22679d150b73SToby Isaac     }
22689d150b73SToby Isaac   } else if (dimR == 3) {
22699d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
22709d150b73SToby Isaac 
22719d150b73SToby Isaac     for (i = 0; i < 8; i++) {
22729d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
22739d150b73SToby Isaac 
22749d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
22759d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
22769d150b73SToby Isaac       }
22779d150b73SToby Isaac     }
22789d150b73SToby Isaac   } else {
22799d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
22809d150b73SToby Isaac   }
22819d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
22829d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
22839d150b73SToby Isaac     PetscReal *swap;
22849d150b73SToby Isaac 
22859d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
22869d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
22879d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
22889d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
22899d150b73SToby Isaac       }
22909d150b73SToby Isaac     }
22919d150b73SToby Isaac 
22929d150b73SToby Isaac     if (i < dimR - 1) {
22939d150b73SToby Isaac       swap = cellCoeffs;
22949d150b73SToby Isaac       cellCoeffs = cellCoords;
22959d150b73SToby Isaac       cellCoords = swap;
22969d150b73SToby Isaac     }
22979d150b73SToby Isaac   }
22989d150b73SToby Isaac   ierr = PetscMemzero(refCoords,numPoints * dimR * sizeof (PetscReal));CHKERRQ(ierr);
22999d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
23009d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
23019d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
23029d150b73SToby Isaac 
23039d150b73SToby Isaac       /* compute -residual and Jacobian */
23049d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
23059d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
23069d150b73SToby Isaac       for (k = 0; k < numV; k++) {
23079d150b73SToby Isaac         PetscReal extCoord = 1.;
23089d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
23099d150b73SToby Isaac           PetscReal coord = guess[l];
23109d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
23119d150b73SToby Isaac 
23129d150b73SToby Isaac           extCoord *= dep * coord + !dep;
23139d150b73SToby Isaac           extJ[l] = dep;
23149d150b73SToby Isaac 
23159d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
23169d150b73SToby Isaac             PetscReal coord = guess[m];
23179d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
23189d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
23199d150b73SToby Isaac 
23209d150b73SToby Isaac             extJ[l] *= mult;
23219d150b73SToby Isaac           }
23229d150b73SToby Isaac         }
23239d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
23249d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
23259d150b73SToby Isaac 
23269d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
23279d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
23289d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
23299d150b73SToby Isaac           }
23309d150b73SToby Isaac         }
23319d150b73SToby Isaac       }
23320611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
23330611203eSToby Isaac       {
23340611203eSToby Isaac         PetscReal maxAbs = 0.;
23350611203eSToby Isaac 
23360611203eSToby Isaac         for (l = 0; l < dimC; l++) {
23370611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
23380611203eSToby Isaac         }
23390611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
23400611203eSToby Isaac       }
23410611203eSToby Isaac #endif
23429d150b73SToby Isaac 
23439d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
23449d150b73SToby Isaac     }
23459d150b73SToby Isaac   }
23469d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
23479d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
23489d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
23499d150b73SToby Isaac   PetscFunctionReturn(0);
23509d150b73SToby Isaac }
23519d150b73SToby Isaac 
23529d150b73SToby Isaac #undef __FUNCT__
23539d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates_Tensor"
23549d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
23559d150b73SToby Isaac {
23569d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
23579d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
23589d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
23599d150b73SToby Isaac   PetscErrorCode ierr;
23609d150b73SToby Isaac 
23619d150b73SToby Isaac   PetscFunctionBegin;
23629d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23639d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
23649d150b73SToby 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);
23659d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
23669d150b73SToby Isaac   cellCoords = &cellData[0];
23679d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
23689d150b73SToby Isaac   if (dimR == 2) {
23699d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
23709d150b73SToby Isaac 
23719d150b73SToby Isaac     for (i = 0; i < 4; i++) {
23729d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
23739d150b73SToby Isaac 
23749d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
23759d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
23769d150b73SToby Isaac       }
23779d150b73SToby Isaac     }
23789d150b73SToby Isaac   } else if (dimR == 3) {
23799d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
23809d150b73SToby Isaac 
23819d150b73SToby Isaac     for (i = 0; i < 8; i++) {
23829d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
23839d150b73SToby Isaac 
23849d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
23859d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
23869d150b73SToby Isaac       }
23879d150b73SToby Isaac     }
23889d150b73SToby Isaac   } else {
23899d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
23909d150b73SToby Isaac   }
23919d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
23929d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
23939d150b73SToby Isaac     PetscReal *swap;
23949d150b73SToby Isaac 
23959d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
23969d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
23979d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
23989d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
23999d150b73SToby Isaac       }
24009d150b73SToby Isaac     }
24019d150b73SToby Isaac 
24029d150b73SToby Isaac     if (i < dimR - 1) {
24039d150b73SToby Isaac       swap = cellCoeffs;
24049d150b73SToby Isaac       cellCoeffs = cellCoords;
24059d150b73SToby Isaac       cellCoords = swap;
24069d150b73SToby Isaac     }
24079d150b73SToby Isaac   }
24089d150b73SToby Isaac   ierr = PetscMemzero(realCoords,numPoints * dimC * sizeof (PetscReal));CHKERRQ(ierr);
24099d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
24109d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
24119d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
24129d150b73SToby Isaac 
24139d150b73SToby Isaac     for (k = 0; k < numV; k++) {
24149d150b73SToby Isaac       PetscReal extCoord = 1.;
24159d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
24169d150b73SToby Isaac         PetscReal coord = guess[l];
24179d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
24189d150b73SToby Isaac 
24199d150b73SToby Isaac         extCoord *= dep * coord + !dep;
24209d150b73SToby Isaac       }
24219d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
24229d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
24239d150b73SToby Isaac 
24249d150b73SToby Isaac         mapped[l] += coeff * extCoord;
24259d150b73SToby Isaac       }
24269d150b73SToby Isaac     }
24279d150b73SToby Isaac   }
24289d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
24299d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
24309d150b73SToby Isaac   PetscFunctionReturn(0);
24319d150b73SToby Isaac }
24329d150b73SToby Isaac 
24339d150b73SToby Isaac #undef __FUNCT__
24349d150b73SToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference_FE"
24359d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
24369d150b73SToby Isaac {
2437c0cbe899SToby Isaac   PetscInt       numComp, numDof, i, j, k, l, m, maxIter = 7, coordSize;
2438*c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2439*c6e120d1SToby Isaac   PetscReal      *invV, *modes;
2440*c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
2441*c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
24429d150b73SToby Isaac   PetscErrorCode ierr;
24439d150b73SToby Isaac 
24449d150b73SToby Isaac   PetscFunctionBegin;
24459d150b73SToby Isaac   ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr);
24469d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
24479d150b73SToby 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);
24489d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
24499d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2450*c6e120d1SToby Isaac   ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
24519d150b73SToby Isaac   invV = fe->invV;
24529d150b73SToby Isaac   for (i = 0; i < numDof; i++) {
24539d150b73SToby Isaac     for (j = 0; j < dimC; j++) {
24549d150b73SToby Isaac       modes[i * dimC + j] = 0.;
24559d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
2456*c6e120d1SToby Isaac         modes[i * dimC + j] += invV[i * numDof + k] * PetscRealPart(nodes[k * dimC + j]);
24579d150b73SToby Isaac       }
24589d150b73SToby Isaac     }
24599d150b73SToby Isaac   }
2460*c6e120d1SToby Isaac   ierr   = DMGetWorkArray(dm,numDof + numDof * dimR + dimC,PETSC_REAL,&B);CHKERRQ(ierr);
2461*c6e120d1SToby Isaac   D      = &B[numDof];
2462*c6e120d1SToby Isaac   resNeg = &D[numDof * dimR];
2463*c6e120d1SToby Isaac   ierr = DMGetWorkArray(dm,3 * dimC * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr);
24649d150b73SToby Isaac   invJ = &J[dimC * dimR];
24659d150b73SToby Isaac   work = &invJ[dimC * dimR];
24669d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
24679d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
24689b1f03cbSToby 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 */
24699d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
24709d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
24719d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[j * dimC + k];}
24729d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
24739d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
24749d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
24759d150b73SToby Isaac           resNeg[l] -= modes[k * dimC + l] * B[k];
24769d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
24779d150b73SToby Isaac             J[l * dimR + m] += modes[k * dimC + l] * D[k * dimR + m];
24789d150b73SToby Isaac           }
24799d150b73SToby Isaac         }
24809d150b73SToby Isaac       }
24810611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
24820611203eSToby Isaac       {
24830611203eSToby Isaac         PetscReal maxAbs = 0.;
24840611203eSToby Isaac 
24850611203eSToby Isaac         for (l = 0; l < dimC; l++) {
24860611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
24870611203eSToby Isaac         }
24880611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
24890611203eSToby Isaac       }
24900611203eSToby Isaac #endif
24919d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
24929d150b73SToby Isaac     }
24939d150b73SToby Isaac   }
2494*c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,3 * dimC * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr);
2495*c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,numDof + numDof * dimR + dimC,PETSC_REAL,&B);CHKERRQ(ierr);
2496*c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
24979d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
24989d150b73SToby Isaac   PetscFunctionReturn(0);
24999d150b73SToby Isaac }
25009d150b73SToby Isaac 
25019d150b73SToby Isaac #undef __FUNCT__
25029d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates_FE"
25039d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
25049d150b73SToby Isaac {
25059d150b73SToby Isaac   PetscInt       numComp, numDof, i, j, k, l, coordSize;
2506*c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2507*c6e120d1SToby Isaac   PetscReal      *invV, *modes;
25089d150b73SToby Isaac   PetscReal      *B;
25099d150b73SToby Isaac   PetscErrorCode ierr;
25109d150b73SToby Isaac 
25119d150b73SToby Isaac   PetscFunctionBegin;
25129d150b73SToby Isaac   ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr);
25139d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
25149d150b73SToby 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);
25159d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
25169d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2517*c6e120d1SToby Isaac   ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
25189d150b73SToby Isaac   invV = fe->invV;
25199d150b73SToby Isaac   for (i = 0; i < numDof; i++) {
25209d150b73SToby Isaac     for (j = 0; j < dimC; j++) {
25219d150b73SToby Isaac       modes[i * dimC + j] = 0.;
25229d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
2523*c6e120d1SToby Isaac         modes[i * dimC + j] += invV[i * numDof + k] * PetscRealPart(nodes[k * dimC + j]);
25249d150b73SToby Isaac       }
25259d150b73SToby Isaac     }
25269d150b73SToby Isaac   }
25279b1f03cbSToby Isaac   ierr = DMGetWorkArray(dm,numDof,PETSC_REAL,&B);CHKERRQ(ierr);
25289d150b73SToby Isaac   for (i = 0; i < numPoints * dimC; i++) {realCoords[i] = 0.;}
25299d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
25309d150b73SToby Isaac     const PetscReal *guess  = &refCoords[j * dimR];
25319d150b73SToby Isaac     PetscReal       *mapped = &realCoords[j * dimC];
25329d150b73SToby Isaac 
25339d150b73SToby Isaac     ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, NULL, NULL);CHKERRQ(ierr);
25349d150b73SToby Isaac     for (k = 0; k < numDof; k++) {
25359d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
25369d150b73SToby Isaac         mapped[l] += modes[k * dimC + l] * B[k];
25379d150b73SToby Isaac       }
25389d150b73SToby Isaac     }
25399d150b73SToby Isaac   }
25409b1f03cbSToby Isaac   ierr = DMRestoreWorkArray(dm,numDof,PETSC_REAL,&B);CHKERRQ(ierr);
2541*c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
25429d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
25439d150b73SToby Isaac   PetscFunctionReturn(0);
25449d150b73SToby Isaac }
25459d150b73SToby Isaac 
25469d150b73SToby Isaac #undef __FUNCT__
2547d6143a4eSToby Isaac #define __FUNCT__ "DMPlexCoordinatesToReference"
2548d6143a4eSToby Isaac /*@
2549d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
2550d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
2551d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
2552d6143a4eSToby Isaac 
2553d6143a4eSToby Isaac   Not collective
2554d6143a4eSToby Isaac 
2555d6143a4eSToby Isaac   Input Parameters:
2556d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2557d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2558d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
2559d6143a4eSToby Isaac . cell       - the cell whose map is used.
2560d6143a4eSToby Isaac . numPoints  - the number of points to locate
2561d6143a4eSToby Isaac + realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2562d6143a4eSToby Isaac 
2563d6143a4eSToby Isaac   Output Parameters:
2564d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
2565d6143a4eSToby Isaac @*/
2566d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
2567d6143a4eSToby Isaac {
25689d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
25699d150b73SToby Isaac   DM             coordDM = NULL;
25709d150b73SToby Isaac   Vec            coords;
25719d150b73SToby Isaac   PetscFE        fe = NULL;
25729d150b73SToby Isaac   PetscErrorCode ierr;
25739d150b73SToby Isaac 
2574d6143a4eSToby Isaac   PetscFunctionBegin;
25759d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25769d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
25779d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
25789d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
25799d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
25809d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
25819d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
25829d150b73SToby Isaac   if (coordDM) {
25839d150b73SToby Isaac     PetscInt coordFields;
25849d150b73SToby Isaac 
25859d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
25869d150b73SToby Isaac     if (coordFields) {
25879d150b73SToby Isaac       PetscClassId id;
25889d150b73SToby Isaac       PetscObject  disc;
25899d150b73SToby Isaac 
25909d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
25919d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
25929d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
25939d150b73SToby Isaac         fe = (PetscFE) disc;
25949d150b73SToby Isaac       }
25959d150b73SToby Isaac     }
25969d150b73SToby Isaac   }
25979d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
25989d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
25999d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
26009d150b73SToby 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);
26019d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
26029d150b73SToby Isaac     PetscInt  coneSize;
26039d150b73SToby Isaac     PetscBool isSimplex, isTensor;
26049d150b73SToby Isaac 
26059d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
26069d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
26079d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
26089d150b73SToby Isaac     if (isSimplex) {
26099d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
26109d150b73SToby Isaac 
26119d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
26129d150b73SToby Isaac       J    = &v0[dimC];
26139d150b73SToby Isaac       invJ = &J[dimC * dimC];
26149d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
26159d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
26169d150b73SToby Isaac         CoordinatesRealToRef(dimC, dimR, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
26179d150b73SToby Isaac       }
26189d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
26199d150b73SToby Isaac     } else if (isTensor) {
26209d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
26219d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
26229d150b73SToby Isaac   } else {
26239d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
26249d150b73SToby Isaac   }
26259d150b73SToby Isaac   PetscFunctionReturn(0);
26269d150b73SToby Isaac }
26279d150b73SToby Isaac 
26289d150b73SToby Isaac #undef __FUNCT__
26299d150b73SToby Isaac #define __FUNCT__ "DMPlexReferenceToCoordinates"
26309d150b73SToby Isaac /*@
26319d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
26329d150b73SToby Isaac 
26339d150b73SToby Isaac   Not collective
26349d150b73SToby Isaac 
26359d150b73SToby Isaac   Input Parameters:
26369d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
26379d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
26389d150b73SToby Isaac                as a multilinear map for tensor-product elements
26399d150b73SToby Isaac . cell       - the cell whose map is used.
26409d150b73SToby Isaac . numPoints  - the number of points to locate
26419d150b73SToby Isaac + refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
26429d150b73SToby Isaac 
26439d150b73SToby Isaac   Output Parameters:
26449d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
26459d150b73SToby Isaac @*/
26469d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
26479d150b73SToby Isaac {
26489d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
26499d150b73SToby Isaac   DM             coordDM = NULL;
26509d150b73SToby Isaac   Vec            coords;
26519d150b73SToby Isaac   PetscFE        fe = NULL;
26529d150b73SToby Isaac   PetscErrorCode ierr;
26539d150b73SToby Isaac 
26549d150b73SToby Isaac   PetscFunctionBegin;
26559d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26569d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
26579d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
26589d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
26599d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
26609d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
26619d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
26629d150b73SToby Isaac   if (coordDM) {
26639d150b73SToby Isaac     PetscInt coordFields;
26649d150b73SToby Isaac 
26659d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
26669d150b73SToby Isaac     if (coordFields) {
26679d150b73SToby Isaac       PetscClassId id;
26689d150b73SToby Isaac       PetscObject  disc;
26699d150b73SToby Isaac 
26709d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
26719d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
26729d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
26739d150b73SToby Isaac         fe = (PetscFE) disc;
26749d150b73SToby Isaac       }
26759d150b73SToby Isaac     }
26769d150b73SToby Isaac   }
26779d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
26789d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
26799d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
26809d150b73SToby 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);
26819d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
26829d150b73SToby Isaac     PetscInt  coneSize;
26839d150b73SToby Isaac     PetscBool isSimplex, isTensor;
26849d150b73SToby Isaac 
26859d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
26869d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
26879d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
26889d150b73SToby Isaac     if (isSimplex) {
26899d150b73SToby Isaac       PetscReal detJ, *v0, *J;
26909d150b73SToby Isaac 
26919d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
26929d150b73SToby Isaac       J    = &v0[dimC];
26939d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
26949d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
26959d150b73SToby Isaac         CoordinatesRefToReal(dimC, dimR, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
26969d150b73SToby Isaac       }
26979d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
26989d150b73SToby Isaac     } else if (isTensor) {
26999d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
27009d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
27019d150b73SToby Isaac   } else {
27029d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
27039d150b73SToby Isaac   }
2704d6143a4eSToby Isaac   PetscFunctionReturn(0);
2705d6143a4eSToby Isaac }
2706