xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 2d1fa6ca7393507dfa4caee8ec145a256de62743)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2ccd2543fSMatthew G Knepley 
3ccd2543fSMatthew G Knepley #undef __FUNCT__
4fea14342SMatthew G. Knepley #define __FUNCT__ "DMPlexGetLineIntersection_2D_Internal"
5fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
6fea14342SMatthew G. Knepley {
7fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*2+0];
8fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*2+1];
9fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*2+0];
10fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*2+1];
11fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0*2+0];
12fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0*2+1];
13fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1*2+0];
14fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1*2+1];
15fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
16fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
17fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
18fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
19fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
20fea14342SMatthew G. Knepley 
21fea14342SMatthew G. Knepley   PetscFunctionBegin;
22fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
23fea14342SMatthew G. Knepley   /* Non-parallel lines */
24fea14342SMatthew G. Knepley   if (denom != 0.0) {
25fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
26fea14342SMatthew G. Knepley     const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
27fea14342SMatthew G. Knepley 
28fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
29fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
30fea14342SMatthew G. Knepley       if (intersection) {
31fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
32fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
33fea14342SMatthew G. Knepley       }
34fea14342SMatthew G. Knepley     }
35fea14342SMatthew G. Knepley   }
36fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
37fea14342SMatthew G. Knepley }
38fea14342SMatthew G. Knepley 
39fea14342SMatthew G. Knepley #undef __FUNCT__
40ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal"
41ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
42ccd2543fSMatthew G Knepley {
43ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
44f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
45ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
46ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
47ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
48ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
49ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
50ccd2543fSMatthew G Knepley 
51ccd2543fSMatthew G Knepley   PetscFunctionBegin;
528e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
53ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
54ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
55ccd2543fSMatthew G Knepley 
56f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
57c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
58ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
59ccd2543fSMatthew G Knepley }
60ccd2543fSMatthew G Knepley 
61ccd2543fSMatthew G Knepley #undef __FUNCT__
6262a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Simplex_2D_Internal"
6362a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
6462a38674SMatthew G. Knepley {
6562a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
6662a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
6762a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
6862a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
6962a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
7062a38674SMatthew G. Knepley   PetscErrorCode  ierr;
7162a38674SMatthew G. Knepley 
7262a38674SMatthew G. Knepley   PetscFunctionBegin;
7362a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
7462a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
7562a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
7662a38674SMatthew G. Knepley 
7762a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
7862a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
7962a38674SMatthew G. Knepley   r   = (xi + eta)/2.0;
8062a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
8162a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
8262a38674SMatthew G. Knepley     xi  /= r;
8362a38674SMatthew G. Knepley     eta /= r;
8462a38674SMatthew G. Knepley   }
8562a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
8662a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
8762a38674SMatthew G. Knepley   PetscFunctionReturn(0);
8862a38674SMatthew G. Knepley }
8962a38674SMatthew G. Knepley 
9062a38674SMatthew G. Knepley #undef __FUNCT__
91ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal"
92ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
93ccd2543fSMatthew G Knepley {
94ccd2543fSMatthew G Knepley   PetscSection       coordSection;
95ccd2543fSMatthew G Knepley   Vec             coordsLocal;
96a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
97ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
98ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
99ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
100ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
101ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
102ccd2543fSMatthew G Knepley 
103ccd2543fSMatthew G Knepley   PetscFunctionBegin;
104ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
10569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
106ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
107ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
108ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
109ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
110ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
111ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
112ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
113ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
114ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
115ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
116ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
117ccd2543fSMatthew G Knepley   }
118ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
119c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
120ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
121ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
122ccd2543fSMatthew G Knepley }
123ccd2543fSMatthew G Knepley 
124ccd2543fSMatthew G Knepley #undef __FUNCT__
125ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal"
126ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
127ccd2543fSMatthew G Knepley {
128ccd2543fSMatthew G Knepley   const PetscInt embedDim = 3;
129ccd2543fSMatthew G Knepley   PetscReal      v0[3], J[9], invJ[9], detJ;
130ccd2543fSMatthew G Knepley   PetscReal      x = PetscRealPart(point[0]);
131ccd2543fSMatthew G Knepley   PetscReal      y = PetscRealPart(point[1]);
132ccd2543fSMatthew G Knepley   PetscReal      z = PetscRealPart(point[2]);
133ccd2543fSMatthew G Knepley   PetscReal      xi, eta, zeta;
134ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
135ccd2543fSMatthew G Knepley 
136ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1378e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
138ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
139ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
140ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
141ccd2543fSMatthew G Knepley 
142ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c;
143c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
144ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
145ccd2543fSMatthew G Knepley }
146ccd2543fSMatthew G Knepley 
147ccd2543fSMatthew G Knepley #undef __FUNCT__
148ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal"
149ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
150ccd2543fSMatthew G Knepley {
151ccd2543fSMatthew G Knepley   PetscSection   coordSection;
152ccd2543fSMatthew G Knepley   Vec            coordsLocal;
1537c1f9639SMatthew G Knepley   PetscScalar   *coords;
154fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
155fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
156ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
157ccd2543fSMatthew G Knepley   PetscInt       f;
158ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
159ccd2543fSMatthew G Knepley 
160ccd2543fSMatthew G Knepley   PetscFunctionBegin;
161ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
16269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
163ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
164ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
165ccd2543fSMatthew G Knepley     /* Check the point is under plane */
166ccd2543fSMatthew G Knepley     /*   Get face normal */
167ccd2543fSMatthew G Knepley     PetscReal v_i[3];
168ccd2543fSMatthew G Knepley     PetscReal v_j[3];
169ccd2543fSMatthew G Knepley     PetscReal normal[3];
170ccd2543fSMatthew G Knepley     PetscReal pp[3];
171ccd2543fSMatthew G Knepley     PetscReal dot;
172ccd2543fSMatthew G Knepley 
173ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
174ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
175ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
176ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
177ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
178ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
179ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
180ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
181ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
182ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
183ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
184ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
185ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
186ccd2543fSMatthew G Knepley 
187ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
188ccd2543fSMatthew G Knepley     if (dot < 0.0) {
189ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
190ccd2543fSMatthew G Knepley       break;
191ccd2543fSMatthew G Knepley     }
192ccd2543fSMatthew G Knepley   }
193ccd2543fSMatthew G Knepley   if (found) *cell = c;
194c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
195ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
196ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
197ccd2543fSMatthew G Knepley }
198ccd2543fSMatthew G Knepley 
199ccd2543fSMatthew G Knepley #undef __FUNCT__
200c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal"
201c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
202c4eade1cSMatthew G. Knepley {
203c4eade1cSMatthew G. Knepley   PetscInt d;
204c4eade1cSMatthew G. Knepley 
205c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
206c4eade1cSMatthew G. Knepley   box->dim = dim;
207c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
208c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
209c4eade1cSMatthew G. Knepley }
210c4eade1cSMatthew G. Knepley 
211c4eade1cSMatthew G. Knepley #undef __FUNCT__
212c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate"
213c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
214c4eade1cSMatthew G. Knepley {
215c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
216c4eade1cSMatthew G. Knepley 
217c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
218c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
219c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
220c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
221c4eade1cSMatthew G. Knepley }
222c4eade1cSMatthew G. Knepley 
223c4eade1cSMatthew G. Knepley #undef __FUNCT__
224c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge"
225c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
226c4eade1cSMatthew G. Knepley {
227c4eade1cSMatthew G. Knepley   PetscInt d;
228c4eade1cSMatthew G. Knepley 
229c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
230c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
231c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
232c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
233c4eade1cSMatthew G. Knepley   }
234c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
235c4eade1cSMatthew G. Knepley }
236c4eade1cSMatthew G. Knepley 
237c4eade1cSMatthew G. Knepley #undef __FUNCT__
238c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid"
23962a38674SMatthew G. Knepley /*
24062a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
24162a38674SMatthew G. Knepley 
24262a38674SMatthew G. Knepley   Not collective
24362a38674SMatthew G. Knepley 
24462a38674SMatthew G. Knepley   Input Parameters:
24562a38674SMatthew G. Knepley + box - The grid hash object
24662a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
24762a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
24862a38674SMatthew G. Knepley 
24962a38674SMatthew G. Knepley   Level: developer
25062a38674SMatthew G. Knepley 
25162a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
25262a38674SMatthew G. Knepley */
253c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
254c4eade1cSMatthew G. Knepley {
255c4eade1cSMatthew G. Knepley   PetscInt d;
256c4eade1cSMatthew G. Knepley 
257c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
258c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
259c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
260c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
261c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
262c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
263c4eade1cSMatthew G. Knepley     } else {
264c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
265c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
266c4eade1cSMatthew G. Knepley     }
267c4eade1cSMatthew G. Knepley   }
268c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
269c4eade1cSMatthew G. Knepley }
270c4eade1cSMatthew G. Knepley 
271c4eade1cSMatthew G. Knepley #undef __FUNCT__
272c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox"
27362a38674SMatthew G. Knepley /*
27462a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
27562a38674SMatthew G. Knepley 
27662a38674SMatthew G. Knepley   Not collective
27762a38674SMatthew G. Knepley 
27862a38674SMatthew G. Knepley   Input Parameters:
27962a38674SMatthew G. Knepley + box       - The grid hash object
28062a38674SMatthew G. Knepley . numPoints - The number of input points
28162a38674SMatthew G. Knepley - points    - The input point coordinates
28262a38674SMatthew G. Knepley 
28362a38674SMatthew G. Knepley   Output Parameters:
28462a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
28562a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
28662a38674SMatthew G. Knepley 
28762a38674SMatthew G. Knepley   Level: developer
28862a38674SMatthew G. Knepley 
28962a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
29062a38674SMatthew G. Knepley */
2911c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
292c4eade1cSMatthew G. Knepley {
293c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
294c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
295c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
296c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
297c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
298c4eade1cSMatthew G. Knepley   PetscInt         d, p;
299c4eade1cSMatthew G. Knepley 
300c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
301c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
302c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
3031c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
304c4eade1cSMatthew G. Knepley 
3051c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
306c4eade1cSMatthew 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",
3071c6dfc3eSMatthew 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);
308c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
309c4eade1cSMatthew G. Knepley     }
310c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
311c4eade1cSMatthew G. Knepley   }
312c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
313c4eade1cSMatthew G. Knepley }
314c4eade1cSMatthew G. Knepley 
315c4eade1cSMatthew G. Knepley #undef __FUNCT__
316c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy"
317c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
318c4eade1cSMatthew G. Knepley {
319c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
320c4eade1cSMatthew G. Knepley 
321c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
322c4eade1cSMatthew G. Knepley   if (*box) {
323c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
324c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
325c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
326c4eade1cSMatthew G. Knepley   }
327c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
328c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
329c4eade1cSMatthew G. Knepley }
330c4eade1cSMatthew G. Knepley 
331cafe43deSMatthew G. Knepley #undef __FUNCT__
332cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexLocatePoint_Internal"
333cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
334cafe43deSMatthew G. Knepley {
335cafe43deSMatthew G. Knepley   PetscInt       coneSize;
336cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
337cafe43deSMatthew G. Knepley 
338cafe43deSMatthew G. Knepley   PetscFunctionBegin;
339cafe43deSMatthew G. Knepley   switch (dim) {
340cafe43deSMatthew G. Knepley   case 2:
341cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
342cafe43deSMatthew G. Knepley     switch (coneSize) {
343cafe43deSMatthew G. Knepley     case 3:
344cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
345cafe43deSMatthew G. Knepley       break;
346cafe43deSMatthew G. Knepley     case 4:
347cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
348cafe43deSMatthew G. Knepley       break;
349cafe43deSMatthew G. Knepley     default:
350cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
351cafe43deSMatthew G. Knepley     }
352cafe43deSMatthew G. Knepley     break;
353cafe43deSMatthew G. Knepley   case 3:
354cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
355cafe43deSMatthew G. Knepley     switch (coneSize) {
356cafe43deSMatthew G. Knepley     case 4:
357cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
358cafe43deSMatthew G. Knepley       break;
359cafe43deSMatthew G. Knepley     case 6:
360cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
361cafe43deSMatthew G. Knepley       break;
362cafe43deSMatthew G. Knepley     default:
363cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
364cafe43deSMatthew G. Knepley     }
365cafe43deSMatthew G. Knepley     break;
366cafe43deSMatthew G. Knepley   default:
367cafe43deSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
368cafe43deSMatthew G. Knepley   }
369cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
370cafe43deSMatthew G. Knepley }
371cafe43deSMatthew G. Knepley 
372cafe43deSMatthew G. Knepley #undef __FUNCT__
37362a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Internal"
37462a38674SMatthew G. Knepley /*
37562a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
37662a38674SMatthew G. Knepley */
37762a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
37862a38674SMatthew G. Knepley {
37962a38674SMatthew G. Knepley   PetscInt       coneSize;
38062a38674SMatthew G. Knepley   PetscErrorCode ierr;
38162a38674SMatthew G. Knepley 
38262a38674SMatthew G. Knepley   PetscFunctionBegin;
38362a38674SMatthew G. Knepley   switch (dim) {
38462a38674SMatthew G. Knepley   case 2:
38562a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
38662a38674SMatthew G. Knepley     switch (coneSize) {
38762a38674SMatthew G. Knepley     case 3:
38862a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
38962a38674SMatthew G. Knepley       break;
39062a38674SMatthew G. Knepley #if 0
39162a38674SMatthew G. Knepley     case 4:
39262a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
39362a38674SMatthew G. Knepley       break;
39462a38674SMatthew G. Knepley #endif
39562a38674SMatthew G. Knepley     default:
39662a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
39762a38674SMatthew G. Knepley     }
39862a38674SMatthew G. Knepley     break;
39962a38674SMatthew G. Knepley #if 0
40062a38674SMatthew G. Knepley   case 3:
40162a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
40262a38674SMatthew G. Knepley     switch (coneSize) {
40362a38674SMatthew G. Knepley     case 4:
40462a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
40562a38674SMatthew G. Knepley       break;
40662a38674SMatthew G. Knepley     case 6:
40762a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
40862a38674SMatthew G. Knepley       break;
40962a38674SMatthew G. Knepley     default:
41062a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
41162a38674SMatthew G. Knepley     }
41262a38674SMatthew G. Knepley     break;
41362a38674SMatthew G. Knepley #endif
41462a38674SMatthew G. Knepley   default:
41562a38674SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for mesh dimension %D", dim);
41662a38674SMatthew G. Knepley   }
41762a38674SMatthew G. Knepley   PetscFunctionReturn(0);
41862a38674SMatthew G. Knepley }
41962a38674SMatthew G. Knepley 
42062a38674SMatthew G. Knepley #undef __FUNCT__
421cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGridHash_Internal"
42262a38674SMatthew G. Knepley /*
42362a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
42462a38674SMatthew G. Knepley 
42562a38674SMatthew G. Knepley   Collective on DM
42662a38674SMatthew G. Knepley 
42762a38674SMatthew G. Knepley   Input Parameter:
42862a38674SMatthew G. Knepley . dm - The Plex
42962a38674SMatthew G. Knepley 
43062a38674SMatthew G. Knepley   Output Parameter:
43162a38674SMatthew G. Knepley . localBox - The grid hash object
43262a38674SMatthew G. Knepley 
43362a38674SMatthew G. Knepley   Level: developer
43462a38674SMatthew G. Knepley 
43562a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
43662a38674SMatthew G. Knepley */
437cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
438cafe43deSMatthew G. Knepley {
439cafe43deSMatthew G. Knepley   MPI_Comm           comm;
440cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
441cafe43deSMatthew G. Knepley   Vec                coordinates;
442cafe43deSMatthew G. Knepley   PetscSection       coordSection;
443cafe43deSMatthew G. Knepley   Vec                coordsLocal;
444cafe43deSMatthew G. Knepley   const PetscScalar *coords;
445722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
446cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
4471d0c6c94SMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, cMax, c, i;
448cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
449cafe43deSMatthew G. Knepley 
450cafe43deSMatthew G. Knepley   PetscFunctionBegin;
451cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
452cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
453cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
4545b3353d8SMatthew G. Knepley   if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D");
455cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
456cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
457cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
458cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
459cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
460cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
461cafe43deSMatthew G. Knepley #if 0
462cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
463b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
464b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
465cafe43deSMatthew G. Knepley #endif
466cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
467cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
468cafe43deSMatthew G. Knepley   /* Create label */
469cafe43deSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4701d0c6c94SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
4711d0c6c94SMatthew G. Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
472cafe43deSMatthew G. Knepley   ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr);
473cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
474722d0f5cSMatthew G. Knepley   /* Compute boxes which overlap each cell: http://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
475cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
476cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
47738353de4SMatthew G. Knepley   ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr);
478cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
479cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
480cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
48138353de4SMatthew G. Knepley     PetscInt         csize   = 0;
482cafe43deSMatthew G. Knepley     PetscScalar      point[3];
483cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
484cafe43deSMatthew G. Knepley 
485cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
48638353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
48738353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
488722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
48938353de4SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);}
490cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
491cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
4922291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
493cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
494cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
495cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
496cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
497cafe43deSMatthew G. Knepley       }
498cafe43deSMatthew G. Knepley     }
499fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
500cafe43deSMatthew 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]) {
501cafe43deSMatthew 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]) {
502cafe43deSMatthew 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]) {
503cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
504cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
505fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
506cafe43deSMatthew G. Knepley 
507fea14342SMatthew G. Knepley           /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
508cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
509cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
510cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
511cafe43deSMatthew G. Knepley 
512cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
513cafe43deSMatthew G. Knepley                 if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
514cafe43deSMatthew G. Knepley               }
515cafe43deSMatthew G. Knepley             }
516cafe43deSMatthew G. Knepley           }
517fea14342SMatthew G. Knepley           /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */
518fea14342SMatthew G. Knepley           for (edge = 0; edge < dim+1; ++edge) {
519fea14342SMatthew G. Knepley             PetscReal segA[6], segB[6];
520fea14342SMatthew G. Knepley 
521fea14342SMatthew 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]);}
522fea14342SMatthew G. Knepley             for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) {
5239a128ed2SMatthew G. Knepley               if (dim > 2) {segB[2]     = PetscRealPart(point[2]);
5249a128ed2SMatthew G. Knepley                             segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];}
525fea14342SMatthew G. Knepley               for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) {
5269a128ed2SMatthew G. Knepley                 if (dim > 1) {segB[1]     = PetscRealPart(point[1]);
5279a128ed2SMatthew G. Knepley                               segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];}
528fea14342SMatthew G. Knepley                 for (ii = 0; ii < 2; ++ii) {
529fea14342SMatthew G. Knepley                   PetscBool intersects;
530fea14342SMatthew G. Knepley 
5319a128ed2SMatthew G. Knepley                   segB[0]     = PetscRealPart(point[0]);
5329a128ed2SMatthew G. Knepley                   segB[dim+0] = PetscRealPart(point[0]) + ii*h[0];
533fea14342SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
534fea14342SMatthew G. Knepley                   if (intersects) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;}
535cafe43deSMatthew G. Knepley                 }
536cafe43deSMatthew G. Knepley               }
537cafe43deSMatthew G. Knepley             }
538cafe43deSMatthew G. Knepley           }
539fea14342SMatthew G. Knepley         }
540fea14342SMatthew G. Knepley       }
541fea14342SMatthew G. Knepley     }
542fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
543fea14342SMatthew G. Knepley   }
544722d0f5cSMatthew G. Knepley   ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr);
545cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
546cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
547cafe43deSMatthew G. Knepley   *localBox = lbox;
548cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
549cafe43deSMatthew G. Knepley }
550cafe43deSMatthew G. Knepley 
551cafe43deSMatthew G. Knepley #undef __FUNCT__
552ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex"
55362a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
554ccd2543fSMatthew G Knepley {
555cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
556953fc75cSMatthew G. Knepley   PetscBool       hash = mesh->useHashLocation;
5573a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
5581318edbeSMatthew G. Knepley   PetscInt        dim, cStart, cEnd, cMax, numCells, c;
559cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
5603a93e3b7SToby Isaac   PetscSFNode    *cells;
561ccd2543fSMatthew G Knepley   PetscScalar    *a;
5623a93e3b7SToby Isaac   PetscMPIInt     result;
563ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
564ccd2543fSMatthew G Knepley 
565ccd2543fSMatthew G Knepley   PetscFunctionBegin;
56662a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && !hash) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Nearest point location only supported with grid hashing.");
567cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
568cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
5693a93e3b7SToby Isaac   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr);
5703a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
571cafe43deSMatthew 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);
572ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
573ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
574ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
575ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
576ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
577ccd2543fSMatthew G Knepley   numPoints /= bs;
578785e854fSJed Brown   ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
579953fc75cSMatthew G. Knepley   if (hash) {
580ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
581cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
582cafe43deSMatthew G. Knepley     /* Send points to correct process */
583cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
584cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
585cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
586953fc75cSMatthew G. Knepley   }
5873a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
588ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
589953fc75cSMatthew G. Knepley     PetscInt           dbin[3], bin, cell = -1, cellOffset;
590ccd2543fSMatthew G Knepley 
5913a93e3b7SToby Isaac     cells[p].rank  = -1;
5923a93e3b7SToby Isaac     cells[p].index = -1;
593953fc75cSMatthew G. Knepley     if (hash) {
594cafe43deSMatthew G. Knepley       ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
595cafe43deSMatthew G. Knepley       /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
596cafe43deSMatthew G. Knepley       ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
597cafe43deSMatthew G. Knepley       ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
598cafe43deSMatthew G. Knepley       for (c = cellOffset; c < cellOffset + numCells; ++c) {
599cafe43deSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
6003a93e3b7SToby Isaac         if (cell >= 0) {
6013a93e3b7SToby Isaac           cells[p].rank = 0;
6023a93e3b7SToby Isaac           cells[p].index = cell;
6033a93e3b7SToby Isaac           numFound++;
6043a93e3b7SToby Isaac           break;
605ccd2543fSMatthew G Knepley         }
6063a93e3b7SToby Isaac       }
607953fc75cSMatthew G. Knepley     } else {
608953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
609953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
6103a93e3b7SToby Isaac         if (cell >= 0) {
6113a93e3b7SToby Isaac           cells[p].rank = 0;
6123a93e3b7SToby Isaac           cells[p].index = cell;
6133a93e3b7SToby Isaac           numFound++;
6143a93e3b7SToby Isaac           break;
615953fc75cSMatthew G. Knepley         }
616953fc75cSMatthew G. Knepley       }
6173a93e3b7SToby Isaac     }
618ccd2543fSMatthew G Knepley   }
619953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
62062a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
62162a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
62262a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
62362a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
624b716b415SMatthew G. Knepley       PetscInt           dbin[3], bin, cellOffset, d;
62562a38674SMatthew G. Knepley 
62662a38674SMatthew G. Knepley       if (cells[p].rank < 0) {
62762a38674SMatthew G. Knepley         ++numFound;
62862a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
62962a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
63062a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
63162a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
63262a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
633b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
63462a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
63562a38674SMatthew G. Knepley           if (dist < distMax) {
63662a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
63762a38674SMatthew G. Knepley             cells[p].rank  = 0;
63862a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
63962a38674SMatthew G. Knepley             distMax = dist;
64062a38674SMatthew G. Knepley             break;
64162a38674SMatthew G. Knepley           }
64262a38674SMatthew G. Knepley         }
64362a38674SMatthew G. Knepley       }
64462a38674SMatthew G. Knepley     }
64562a38674SMatthew G. Knepley   }
64662a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
647cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
648*2d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
6493a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
6503a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
6513a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
6523a93e3b7SToby Isaac         if (numFound < p) {
6533a93e3b7SToby Isaac           cells[numFound] = cells[p];
6543a93e3b7SToby Isaac         }
6553a93e3b7SToby Isaac         found[numFound++] = p;
6563a93e3b7SToby Isaac       }
6573a93e3b7SToby Isaac     }
6583a93e3b7SToby Isaac   }
65962a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
6603a93e3b7SToby Isaac   ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
661ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
662ccd2543fSMatthew G Knepley }
663ccd2543fSMatthew G Knepley 
664ccd2543fSMatthew G Knepley #undef __FUNCT__
66517fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D_Internal"
66617fe8556SMatthew G. Knepley /*
66717fe8556SMatthew G. Knepley   DMPlexComputeProjection2Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 2D
66817fe8556SMatthew G. Knepley */
6693beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D_Internal(PetscScalar coords[], PetscReal R[])
67017fe8556SMatthew G. Knepley {
67117fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
67217fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
6738b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
67417fe8556SMatthew G. Knepley 
67517fe8556SMatthew G. Knepley   PetscFunctionBegin;
6761c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
6771c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
67817fe8556SMatthew G. Knepley   coords[0] = 0.0;
6797f07f362SMatthew G. Knepley   coords[1] = r;
68017fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
68117fe8556SMatthew G. Knepley }
68217fe8556SMatthew G. Knepley 
68317fe8556SMatthew G. Knepley #undef __FUNCT__
68428dbe442SToby Isaac #define __FUNCT__ "DMPlexComputeProjection3Dto1D_Internal"
68528dbe442SToby Isaac /*
68628dbe442SToby Isaac   DMPlexComputeProjection3Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 3D
68728dbe442SToby Isaac 
68828dbe442SToby Isaac   This uses the basis completion described by Frisvad,
68928dbe442SToby Isaac 
69028dbe442SToby Isaac   http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html
69128dbe442SToby Isaac   DOI:10.1080/2165347X.2012.689606
69228dbe442SToby Isaac */
6933beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D_Internal(PetscScalar coords[], PetscReal R[])
69428dbe442SToby Isaac {
69528dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
69628dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
69728dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
69828dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
69928dbe442SToby Isaac   PetscReal      rinv = 1. / r;
70028dbe442SToby Isaac   PetscFunctionBegin;
70128dbe442SToby Isaac 
70228dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
70328dbe442SToby Isaac   if (x > 0.) {
70428dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
70528dbe442SToby Isaac 
70628dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
70728dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
70828dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
70928dbe442SToby Isaac   }
71028dbe442SToby Isaac   else {
71128dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
71228dbe442SToby Isaac 
71328dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
71428dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
71528dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
71628dbe442SToby Isaac   }
71728dbe442SToby Isaac   coords[0] = 0.0;
71828dbe442SToby Isaac   coords[1] = r;
71928dbe442SToby Isaac   PetscFunctionReturn(0);
72028dbe442SToby Isaac }
72128dbe442SToby Isaac 
72228dbe442SToby Isaac #undef __FUNCT__
723ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D_Internal"
724ccd2543fSMatthew G Knepley /*
725ccd2543fSMatthew G Knepley   DMPlexComputeProjection3Dto2D_Internal - Rewrite coordinates to be the 2D projection of the 3D
726ccd2543fSMatthew G Knepley */
7273beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D_Internal(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
728ccd2543fSMatthew G Knepley {
7291ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
73099dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
7314a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
732ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
73399dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
734ccd2543fSMatthew G Knepley 
735ccd2543fSMatthew G Knepley   PetscFunctionBegin;
736ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
737ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
7381ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
7391ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
740ccd2543fSMatthew G Knepley   }
741ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
742ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
743ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
7448b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
745ccd2543fSMatthew G Knepley   n[0] /= norm;
746ccd2543fSMatthew G Knepley   n[1] /= norm;
747ccd2543fSMatthew G Knepley   n[2] /= norm;
748ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
749ccd2543fSMatthew G Knepley 
750ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
751ccd2543fSMatthew G Knepley 
752ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
753ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
754ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
755ccd2543fSMatthew G Knepley 
756ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
757ccd2543fSMatthew G Knepley   */
7588b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
75973868372SMatthew G. Knepley   /* Check for n = z */
76073868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
7617df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
7627df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
76399dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
76499dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
7657df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
76673868372SMatthew G. Knepley     }
76799dec3a6SMatthew G. Knepley     coords[0] = 0.0;
76899dec3a6SMatthew G. Knepley     coords[1] = 0.0;
7697df32b8bSSanderA     coords[2] = x1[0];
7707df32b8bSSanderA     coords[3] = x1[1] * s;
7717df32b8bSSanderA     coords[4] = x2[0];
7727df32b8bSSanderA     coords[5] = x2[1] * s;
7737df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
7747df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
7757df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
77673868372SMatthew G. Knepley     PetscFunctionReturn(0);
77773868372SMatthew G. Knepley   }
778da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
779ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
780ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
781ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
782ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
783ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
784ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
785ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
786ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
787ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
788ccd2543fSMatthew G Knepley     }
789ccd2543fSMatthew G Knepley   }
7908763be8eSMatthew G. Knepley   if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
7918763be8eSMatthew G. Knepley   if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
792ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
79399dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
79499dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
79599dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
79699dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
79799dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
79899dec3a6SMatthew G. Knepley       }
79999dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
80099dec3a6SMatthew G. Knepley     }
80199dec3a6SMatthew G. Knepley   }
802ccd2543fSMatthew G Knepley   coords[0] = 0.0;
803ccd2543fSMatthew G Knepley   coords[1] = 0.0;
804ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
805ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
806ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
807ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
8087f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
8097f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
8107f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
8117f07f362SMatthew G. Knepley       PetscReal tmp;
8127f07f362SMatthew G. Knepley 
8137f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
8147f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
8157f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
8167f07f362SMatthew G. Knepley     }
8177f07f362SMatthew G. Knepley   }
818ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
819ccd2543fSMatthew G Knepley }
820ccd2543fSMatthew G Knepley 
821ccd2543fSMatthew G Knepley #undef __FUNCT__
822834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal"
8236322fe33SJed Brown PETSC_UNUSED
824834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
825834e62ceSMatthew G. Knepley {
826834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
827834e62ceSMatthew G. Knepley 
828834e62ceSMatthew G. Knepley    |  1  1  1 |
829834e62ceSMatthew G. Knepley    | x0 x1 x2 |
830834e62ceSMatthew G. Knepley    | y0 y1 y2 |
831834e62ceSMatthew G. Knepley 
832834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
833834e62ceSMatthew G. Knepley 
834834e62ceSMatthew G. Knepley    | x1 x2 |
835834e62ceSMatthew G. Knepley    | y1 y2 |
836834e62ceSMatthew G. Knepley   */
837834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
838834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
839834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
840834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
84186623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
842923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
843834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
8443bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
845834e62ceSMatthew G. Knepley }
846834e62ceSMatthew G. Knepley 
847834e62ceSMatthew G. Knepley #undef __FUNCT__
848834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal"
849834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
850834e62ceSMatthew G. Knepley {
851923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
852834e62ceSMatthew G. Knepley   *vol *= 0.5;
853834e62ceSMatthew G. Knepley }
854834e62ceSMatthew G. Knepley 
855834e62ceSMatthew G. Knepley #undef __FUNCT__
856834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal"
8576322fe33SJed Brown PETSC_UNUSED
858834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
859834e62ceSMatthew G. Knepley {
860834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
861834e62ceSMatthew G. Knepley 
862834e62ceSMatthew G. Knepley    |  1  1  1  1 |
863834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
864834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
865834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
866834e62ceSMatthew G. Knepley 
867834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
868834e62ceSMatthew G. Knepley 
869834e62ceSMatthew G. Knepley    | x1 x2 x3 |
870834e62ceSMatthew G. Knepley    | y1 y2 y3 |
871834e62ceSMatthew G. Knepley    | z1 z2 z3 |
872834e62ceSMatthew G. Knepley   */
873834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
874834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
875834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
876834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
877834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
878834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
879834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
880923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
881b7ad821dSMatthew G. Knepley   *vol = -0.16666666666666666666666*detM;
8823bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
883834e62ceSMatthew G. Knepley }
884834e62ceSMatthew G. Knepley 
885834e62ceSMatthew G. Knepley #undef __FUNCT__
8860ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal"
8870ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
8880ec8681fSMatthew G. Knepley {
889923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
890b7ad821dSMatthew G. Knepley   *vol *= -0.16666666666666666666666;
8910ec8681fSMatthew G. Knepley }
8920ec8681fSMatthew G. Knepley 
8930ec8681fSMatthew G. Knepley #undef __FUNCT__
89417fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal"
89517fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
89617fe8556SMatthew G. Knepley {
89717fe8556SMatthew G. Knepley   PetscSection   coordSection;
89817fe8556SMatthew G. Knepley   Vec            coordinates;
899a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9008bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
90117fe8556SMatthew G. Knepley   PetscErrorCode ierr;
90217fe8556SMatthew G. Knepley 
90317fe8556SMatthew G. Knepley   PetscFunctionBegin;
90417fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
90569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
9068bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
9078bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
90817fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9098bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
910adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
9117f07f362SMatthew G. Knepley   *detJ = 0.0;
91228dbe442SToby Isaac   if (numCoords == 6) {
91328dbe442SToby Isaac     const PetscInt dim = 3;
91428dbe442SToby Isaac     PetscReal      R[9], J0;
91528dbe442SToby Isaac 
91628dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
91728dbe442SToby Isaac     ierr = DMPlexComputeProjection3Dto1D_Internal(coords, R);CHKERRQ(ierr);
91828dbe442SToby Isaac     if (J)    {
91928dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
92028dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
92128dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
92228dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
92328dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
92428dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
925adac9986SMatthew G. Knepley     }
92628dbe442SToby Isaac   } else if (numCoords == 4) {
9277f07f362SMatthew G. Knepley     const PetscInt dim = 2;
9287f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
9297f07f362SMatthew G. Knepley 
9307f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
9317f07f362SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D_Internal(coords, R);CHKERRQ(ierr);
93217fe8556SMatthew G. Knepley     if (J)    {
9337f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
9347f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
9357f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
936923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
937923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
938adac9986SMatthew G. Knepley     }
9397f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
9407f07f362SMatthew G. Knepley     const PetscInt dim = 1;
9417f07f362SMatthew G. Knepley 
9427f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
9437f07f362SMatthew G. Knepley     if (J)    {
9447f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
94517fe8556SMatthew G. Knepley       *detJ = J[0];
9463bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
9473bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
948adac9986SMatthew G. Knepley     }
949796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
95017fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
95117fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
95217fe8556SMatthew G. Knepley }
95317fe8556SMatthew G. Knepley 
95417fe8556SMatthew G. Knepley #undef __FUNCT__
955ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal"
956ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
957ccd2543fSMatthew G Knepley {
958ccd2543fSMatthew G Knepley   PetscSection   coordSection;
959ccd2543fSMatthew G Knepley   Vec            coordinates;
960a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9617f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
962ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
963ccd2543fSMatthew G Knepley 
964ccd2543fSMatthew G Knepley   PetscFunctionBegin;
965ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
96669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
967ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9687f07f362SMatthew G. Knepley   *detJ = 0.0;
969ccd2543fSMatthew G Knepley   if (numCoords == 9) {
9707f07f362SMatthew G. Knepley     const PetscInt dim = 3;
9717f07f362SMatthew 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};
9727f07f362SMatthew G. Knepley 
9737f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
97499dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
9757f07f362SMatthew G. Knepley     if (J)    {
976b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
977b7ad821dSMatthew G. Knepley 
978b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
979b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
980b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
981ccd2543fSMatthew G Knepley         }
9827f07f362SMatthew G. Knepley       }
9833bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
984923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
9857f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
9867f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
9877f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
9887f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
9897f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
9907f07f362SMatthew G. Knepley           }
9917f07f362SMatthew G. Knepley         }
9927f07f362SMatthew G. Knepley       }
9933bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
9947f07f362SMatthew G. Knepley     }
995923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
9967f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
9977f07f362SMatthew G. Knepley     const PetscInt dim = 2;
9987f07f362SMatthew G. Knepley 
9997f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1000ccd2543fSMatthew G Knepley     if (J)    {
1001ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1002ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1003ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1004ccd2543fSMatthew G Knepley         }
1005ccd2543fSMatthew G Knepley       }
10063bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1007923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1008ccd2543fSMatthew G Knepley     }
1009923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1010796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1011ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1012ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1013ccd2543fSMatthew G Knepley }
1014ccd2543fSMatthew G Knepley 
1015ccd2543fSMatthew G Knepley #undef __FUNCT__
1016ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal"
1017ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1018ccd2543fSMatthew G Knepley {
1019ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1020ccd2543fSMatthew G Knepley   Vec            coordinates;
1021a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10220d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1023ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1024ccd2543fSMatthew G Knepley 
1025ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1026ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
102769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10280d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10290d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
103099dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
103171f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
10327f07f362SMatthew G. Knepley   *detJ = 0.0;
103399dec3a6SMatthew G. Knepley   if (numCoords == 12) {
103499dec3a6SMatthew G. Knepley     const PetscInt dim = 3;
103599dec3a6SMatthew 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};
103699dec3a6SMatthew G. Knepley 
103799dec3a6SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
103899dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
103999dec3a6SMatthew G. Knepley     if (J)    {
104099dec3a6SMatthew G. Knepley       const PetscInt pdim = 2;
104199dec3a6SMatthew G. Knepley 
104299dec3a6SMatthew G. Knepley       for (d = 0; d < pdim; d++) {
104399dec3a6SMatthew G. Knepley         J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
104499dec3a6SMatthew G. Knepley         J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
104599dec3a6SMatthew G. Knepley       }
10463bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1047923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
104899dec3a6SMatthew G. Knepley       for (d = 0; d < dim; d++) {
104999dec3a6SMatthew G. Knepley         for (f = 0; f < dim; f++) {
105099dec3a6SMatthew G. Knepley           J[d*dim+f] = 0.0;
105199dec3a6SMatthew G. Knepley           for (g = 0; g < dim; g++) {
105299dec3a6SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
105399dec3a6SMatthew G. Knepley           }
105499dec3a6SMatthew G. Knepley         }
105599dec3a6SMatthew G. Knepley       }
10563bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
105799dec3a6SMatthew G. Knepley     }
1058923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
105971f58de1SToby Isaac   } else if (numCoords == 8) {
106099dec3a6SMatthew G. Knepley     const PetscInt dim = 2;
106199dec3a6SMatthew G. Knepley 
10627f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1063ccd2543fSMatthew G Knepley     if (J)    {
1064ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
106599dec3a6SMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
106699dec3a6SMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1067ccd2543fSMatthew G Knepley       }
10683bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1069923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1070ccd2543fSMatthew G Knepley     }
1071923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1072796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
107399dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1074ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1075ccd2543fSMatthew G Knepley }
1076ccd2543fSMatthew G Knepley 
1077ccd2543fSMatthew G Knepley #undef __FUNCT__
1078ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal"
1079ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1080ccd2543fSMatthew G Knepley {
1081ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1082ccd2543fSMatthew G Knepley   Vec            coordinates;
1083a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1084ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
108599dec3a6SMatthew G. Knepley   PetscInt       d;
1086ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1087ccd2543fSMatthew G Knepley 
1088ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1089ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
109069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1091ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
10927f07f362SMatthew G. Knepley   *detJ = 0.0;
10937f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1094ccd2543fSMatthew G Knepley   if (J)    {
1095ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1096f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1097f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1098f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1099f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1100ccd2543fSMatthew G Knepley     }
11013bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1102923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1103ccd2543fSMatthew G Knepley   }
1104923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1105ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1106ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1107ccd2543fSMatthew G Knepley }
1108ccd2543fSMatthew G Knepley 
1109ccd2543fSMatthew G Knepley #undef __FUNCT__
1110ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal"
1111ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1112ccd2543fSMatthew G Knepley {
1113ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1114ccd2543fSMatthew G Knepley   Vec            coordinates;
1115a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1116ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1117ccd2543fSMatthew G Knepley   PetscInt       d;
1118ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1119ccd2543fSMatthew G Knepley 
1120ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1121ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
112269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1123ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11247f07f362SMatthew G. Knepley   *detJ = 0.0;
11257f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1126ccd2543fSMatthew G Knepley   if (J)    {
1127ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1128f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1129f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1130f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1131ccd2543fSMatthew G Knepley     }
11323bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1133923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1134ccd2543fSMatthew G Knepley   }
1135923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1136ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1137ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1138ccd2543fSMatthew G Knepley }
1139ccd2543fSMatthew G Knepley 
1140ccd2543fSMatthew G Knepley #undef __FUNCT__
11418e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM"
1142ccd2543fSMatthew G Knepley /*@C
11438e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1144ccd2543fSMatthew G Knepley 
1145ccd2543fSMatthew G Knepley   Collective on DM
1146ccd2543fSMatthew G Knepley 
1147ccd2543fSMatthew G Knepley   Input Arguments:
1148ccd2543fSMatthew G Knepley + dm   - the DM
1149ccd2543fSMatthew G Knepley - cell - the cell
1150ccd2543fSMatthew G Knepley 
1151ccd2543fSMatthew G Knepley   Output Arguments:
1152ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1153ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1154ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1155ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1156ccd2543fSMatthew G Knepley 
1157ccd2543fSMatthew G Knepley   Level: advanced
1158ccd2543fSMatthew G Knepley 
1159ccd2543fSMatthew G Knepley   Fortran Notes:
1160ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1161ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1162ccd2543fSMatthew G Knepley 
11638e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
1164ccd2543fSMatthew G Knepley @*/
11658e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1166ccd2543fSMatthew G Knepley {
116749dc4407SMatthew G. Knepley   PetscInt       depth, dim, coneSize;
1168ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1169ccd2543fSMatthew G Knepley 
1170ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1171139a35ccSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1172ccd2543fSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
117349dc4407SMatthew G. Knepley   if (depth == 1) {
11748e0841e0SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
11758e0841e0SMatthew G. Knepley   } else {
11768e0841e0SMatthew G. Knepley     DMLabel depth;
11778e0841e0SMatthew G. Knepley 
11788e0841e0SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
11798e0841e0SMatthew G. Knepley     ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr);
11808e0841e0SMatthew G. Knepley   }
1181ccd2543fSMatthew G Knepley   switch (dim) {
118217fe8556SMatthew G. Knepley   case 1:
118317fe8556SMatthew G. Knepley     ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
118417fe8556SMatthew G. Knepley     break;
1185ccd2543fSMatthew G Knepley   case 2:
1186ccd2543fSMatthew G Knepley     switch (coneSize) {
1187ccd2543fSMatthew G Knepley     case 3:
1188ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1189ccd2543fSMatthew G Knepley       break;
1190ccd2543fSMatthew G Knepley     case 4:
1191ccd2543fSMatthew G Knepley       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1192ccd2543fSMatthew G Knepley       break;
1193ccd2543fSMatthew G Knepley     default:
11948e0841e0SMatthew G. Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1195ccd2543fSMatthew G Knepley     }
1196ccd2543fSMatthew G Knepley     break;
1197ccd2543fSMatthew G Knepley   case 3:
1198ccd2543fSMatthew G Knepley     switch (coneSize) {
1199ccd2543fSMatthew G Knepley     case 4:
1200ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1201ccd2543fSMatthew G Knepley       break;
12028e0841e0SMatthew G. Knepley     case 6: /* Faces */
12038e0841e0SMatthew G. Knepley     case 8: /* Vertices */
1204ccd2543fSMatthew G Knepley       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1205ccd2543fSMatthew G Knepley       break;
1206ccd2543fSMatthew G Knepley     default:
12078e0841e0SMatthew G. Knepley         SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1208ccd2543fSMatthew G Knepley     }
1209ccd2543fSMatthew G Knepley       break;
1210ccd2543fSMatthew G Knepley   default:
1211ccd2543fSMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1212ccd2543fSMatthew G Knepley   }
12138e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
12148e0841e0SMatthew G. Knepley }
12158e0841e0SMatthew G. Knepley 
12168e0841e0SMatthew G. Knepley #undef __FUNCT__
12178e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal"
12188e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
12198e0841e0SMatthew G. Knepley {
12208e0841e0SMatthew G. Knepley   PetscQuadrature  quad;
12218e0841e0SMatthew G. Knepley   PetscSection     coordSection;
12228e0841e0SMatthew G. Knepley   Vec              coordinates;
12238e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
12248e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
12258e0841e0SMatthew G. Knepley   PetscReal       *basisDer;
12268e0841e0SMatthew G. Knepley   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, d, q;
12278e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
12288e0841e0SMatthew G. Knepley 
12298e0841e0SMatthew G. Knepley   PetscFunctionBegin;
12308e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
12318e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
12328e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
12338e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12348e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
12358e0841e0SMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1236954b1791SMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
12378e0841e0SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
12388e0841e0SMatthew G. Knepley   ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
12398e0841e0SMatthew G. Knepley   *detJ = 0.0;
12408e0841e0SMatthew G. Knepley   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
12418e0841e0SMatthew 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);
12428e0841e0SMatthew G. Knepley   if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);}
12438e0841e0SMatthew G. Knepley   if (J) {
12440790e268SMatthew G. Knepley     ierr = PetscMemzero(J, Nq*cdim*dim*sizeof(PetscReal));CHKERRQ(ierr);
12458e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
12468e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
12478e0841e0SMatthew G. Knepley 
12488e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
12498e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
12508e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
12518e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
125271d6e60fSMatthew G. Knepley             J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
12533bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
12548e0841e0SMatthew G. Knepley       if (cdim > dim) {
12558e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
12568e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
12578e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
12588e0841e0SMatthew G. Knepley       }
12598e0841e0SMatthew G. Knepley       switch (cdim) {
12608e0841e0SMatthew G. Knepley       case 3:
12618e0841e0SMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J);
12628e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
126317fe8556SMatthew G. Knepley         break;
126449dc4407SMatthew G. Knepley       case 2:
12658e0841e0SMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
12668e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
126749dc4407SMatthew G. Knepley         break;
12688e0841e0SMatthew G. Knepley       case 1:
12698e0841e0SMatthew G. Knepley         *detJ = J[0];
12708e0841e0SMatthew G. Knepley         if (invJ) invJ[0] = 1.0/J[0];
127149dc4407SMatthew G. Knepley       }
127249dc4407SMatthew G. Knepley     }
12738e0841e0SMatthew G. Knepley   }
12748e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
12758e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
12768e0841e0SMatthew G. Knepley }
12778e0841e0SMatthew G. Knepley 
12788e0841e0SMatthew G. Knepley #undef __FUNCT__
12798e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
12808e0841e0SMatthew G. Knepley /*@C
12818e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
12828e0841e0SMatthew G. Knepley 
12838e0841e0SMatthew G. Knepley   Collective on DM
12848e0841e0SMatthew G. Knepley 
12858e0841e0SMatthew G. Knepley   Input Arguments:
12868e0841e0SMatthew G. Knepley + dm   - the DM
12878e0841e0SMatthew G. Knepley . cell - the cell
12888e0841e0SMatthew G. Knepley - fe   - the finite element containing the quadrature
12898e0841e0SMatthew G. Knepley 
12908e0841e0SMatthew G. Knepley   Output Arguments:
12918e0841e0SMatthew G. Knepley + v0   - the translation part of this transform
12928e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
12938e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
12948e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
12958e0841e0SMatthew G. Knepley 
12968e0841e0SMatthew G. Knepley   Level: advanced
12978e0841e0SMatthew G. Knepley 
12988e0841e0SMatthew G. Knepley   Fortran Notes:
12998e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
13008e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
13018e0841e0SMatthew G. Knepley 
13028e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
13038e0841e0SMatthew G. Knepley @*/
13048e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
13058e0841e0SMatthew G. Knepley {
13068e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
13078e0841e0SMatthew G. Knepley 
13088e0841e0SMatthew G. Knepley   PetscFunctionBegin;
13098e0841e0SMatthew G. Knepley   if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
13108e0841e0SMatthew G. Knepley   else     {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
1311ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1312ccd2543fSMatthew G Knepley }
1313834e62ceSMatthew G. Knepley 
1314834e62ceSMatthew G. Knepley #undef __FUNCT__
1315cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
1316011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1317cc08537eSMatthew G. Knepley {
1318cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1319cc08537eSMatthew G. Knepley   Vec            coordinates;
1320a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
132106e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1322cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1323cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1324cc08537eSMatthew G. Knepley 
1325cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1326cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
132769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1328cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1329011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
13302e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1331cc08537eSMatthew G. Knepley   if (centroid) {
133206e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
133306e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1334cc08537eSMatthew G. Knepley   }
1335cc08537eSMatthew G. Knepley   if (normal) {
1336a60a936bSMatthew G. Knepley     PetscReal norm;
1337a60a936bSMatthew G. Knepley 
133806e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
133906e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1340a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1341a60a936bSMatthew G. Knepley     normal[0] /= norm;
1342a60a936bSMatthew G. Knepley     normal[1] /= norm;
1343cc08537eSMatthew G. Knepley   }
1344cc08537eSMatthew G. Knepley   if (vol) {
134506e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1346cc08537eSMatthew G. Knepley   }
1347cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1348cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1349cc08537eSMatthew G. Knepley }
1350cc08537eSMatthew G. Knepley 
1351cc08537eSMatthew G. Knepley #undef __FUNCT__
1352cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1353cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1354011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1355cc08537eSMatthew G. Knepley {
1356cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1357cc08537eSMatthew G. Knepley   Vec            coordinates;
1358cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
13590a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
13600a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1361cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1362cc08537eSMatthew G. Knepley 
1363cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1364cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
13650a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
136669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1367cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
13680bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1369ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1370ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1371ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1372ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1373ceee4971SMatthew G. Knepley   }
1374011ea5d8SMatthew G. Knepley   if (normal) {
1375011ea5d8SMatthew G. Knepley     if (dim > 2) {
13761ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
13771ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
13781ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
13790a1d6728SMatthew G. Knepley       PetscReal       norm;
13800a1d6728SMatthew G. Knepley 
13810a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
13820a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
13830a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
13848b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
13850a1d6728SMatthew G. Knepley       normal[0] /= norm;
13860a1d6728SMatthew G. Knepley       normal[1] /= norm;
13870a1d6728SMatthew G. Knepley       normal[2] /= norm;
1388011ea5d8SMatthew G. Knepley     } else {
1389011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1390011ea5d8SMatthew G. Knepley     }
1391011ea5d8SMatthew G. Knepley   }
139299dec3a6SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D_Internal(coordSize, coords, R);CHKERRQ(ierr);}
13930a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
13940a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
13950a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
13961ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
13971ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
13980a1d6728SMatthew G. Knepley     }
13990a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
14000a1d6728SMatthew G. Knepley     vsum += vtmp;
14010a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14020a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
14030a1d6728SMatthew G. Knepley     }
14040a1d6728SMatthew G. Knepley   }
14050a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
14060a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
14070a1d6728SMatthew G. Knepley   }
14080a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1409ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
14100a1d6728SMatthew G. Knepley   if (centroid) {
14110a1d6728SMatthew G. Knepley     if (dim > 2) {
14120a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14130a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
14140a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
14150a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
14160a1d6728SMatthew G. Knepley         }
14170a1d6728SMatthew G. Knepley       }
14180a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
14190a1d6728SMatthew G. Knepley   }
1420cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1421cc08537eSMatthew G. Knepley }
1422cc08537eSMatthew G. Knepley 
1423cc08537eSMatthew G. Knepley #undef __FUNCT__
14240ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
14250ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1426011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
14270ec8681fSMatthew G. Knepley {
14280ec8681fSMatthew G. Knepley   PetscSection    coordSection;
14290ec8681fSMatthew G. Knepley   Vec             coordinates;
14300ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
143186623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1432a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
14330ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
14340ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
14350ec8681fSMatthew G. Knepley 
14360ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1437f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
14380ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
143969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
14400ec8681fSMatthew G. Knepley 
1441d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
14420ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
14430ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1444a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
14450ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1446011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
14470ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
14480ec8681fSMatthew G. Knepley     switch (numCorners) {
14490ec8681fSMatthew G. Knepley     case 3:
14500ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14511ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
14521ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
14531ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
14540ec8681fSMatthew G. Knepley       }
14550ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1456a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
14570ec8681fSMatthew G. Knepley       vsum += vtmp;
14584f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
14590ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
14601ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
14610ec8681fSMatthew G. Knepley         }
14620ec8681fSMatthew G. Knepley       }
14630ec8681fSMatthew G. Knepley       break;
14640ec8681fSMatthew G. Knepley     case 4:
14650ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
14660ec8681fSMatthew G. Knepley       /* First tet */
14670ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14681ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
14691ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
14701ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
14710ec8681fSMatthew G. Knepley       }
14720ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1473a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
14740ec8681fSMatthew G. Knepley       vsum += vtmp;
14750ec8681fSMatthew G. Knepley       if (centroid) {
14760ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
14770ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
14780ec8681fSMatthew G. Knepley         }
14790ec8681fSMatthew G. Knepley       }
14800ec8681fSMatthew G. Knepley       /* Second tet */
14810ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14821ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
14831ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
14841ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
14850ec8681fSMatthew G. Knepley       }
14860ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1487a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
14880ec8681fSMatthew G. Knepley       vsum += vtmp;
14890ec8681fSMatthew G. Knepley       if (centroid) {
14900ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
14910ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
14920ec8681fSMatthew G. Knepley         }
14930ec8681fSMatthew G. Knepley       }
14940ec8681fSMatthew G. Knepley       break;
14950ec8681fSMatthew G. Knepley     default:
1496796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
14970ec8681fSMatthew G. Knepley     }
14984f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
14990ec8681fSMatthew G. Knepley   }
15008763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
15010ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1502d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
15030ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
15040ec8681fSMatthew G. Knepley }
15050ec8681fSMatthew G. Knepley 
15060ec8681fSMatthew G. Knepley #undef __FUNCT__
1507834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1508834e62ceSMatthew G. Knepley /*@C
1509834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1510834e62ceSMatthew G. Knepley 
1511834e62ceSMatthew G. Knepley   Collective on DM
1512834e62ceSMatthew G. Knepley 
1513834e62ceSMatthew G. Knepley   Input Arguments:
1514834e62ceSMatthew G. Knepley + dm   - the DM
1515834e62ceSMatthew G. Knepley - cell - the cell
1516834e62ceSMatthew G. Knepley 
1517834e62ceSMatthew G. Knepley   Output Arguments:
1518834e62ceSMatthew G. Knepley + volume   - the cell volume
1519cc08537eSMatthew G. Knepley . centroid - the cell centroid
1520cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1521834e62ceSMatthew G. Knepley 
1522834e62ceSMatthew G. Knepley   Level: advanced
1523834e62ceSMatthew G. Knepley 
1524834e62ceSMatthew G. Knepley   Fortran Notes:
1525834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1526834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1527834e62ceSMatthew G. Knepley 
152869d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1529834e62ceSMatthew G. Knepley @*/
1530cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1531834e62ceSMatthew G. Knepley {
15320ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1533834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1534834e62ceSMatthew G. Knepley 
1535834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1536834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1537c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1538834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1539834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1540c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1541834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1542011ea5d8SMatthew G. Knepley   switch (depth) {
1543cc08537eSMatthew G. Knepley   case 1:
1544011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1545cc08537eSMatthew G. Knepley     break;
1546834e62ceSMatthew G. Knepley   case 2:
1547011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1548834e62ceSMatthew G. Knepley     break;
1549834e62ceSMatthew G. Knepley   case 3:
1550011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1551834e62ceSMatthew G. Knepley     break;
1552834e62ceSMatthew G. Knepley   default:
1553834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1554834e62ceSMatthew G. Knepley   }
1555834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1556834e62ceSMatthew G. Knepley }
1557113c68e6SMatthew G. Knepley 
1558113c68e6SMatthew G. Knepley #undef __FUNCT__
1559c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1560c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1561c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1562c0d900a5SMatthew G. Knepley {
1563c0d900a5SMatthew G. Knepley   DM             dmCell;
1564c0d900a5SMatthew G. Knepley   Vec            coordinates;
1565c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1566c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1567c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1568c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1569c0d900a5SMatthew G. Knepley 
1570c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1571c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1572c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1573c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1574c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1575c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1576c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1577c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1578c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1579c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1580c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1581c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
15829e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1583c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1584c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1585c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1586c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1587c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1588c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1589c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1590c0d900a5SMatthew G. Knepley 
1591c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1592c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1593c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1594c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1595c0d900a5SMatthew G. Knepley   }
1596c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1597c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1598c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1599c0d900a5SMatthew G. Knepley }
1600c0d900a5SMatthew G. Knepley 
1601c0d900a5SMatthew G. Knepley #undef __FUNCT__
1602113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1603891a9168SMatthew G. Knepley /*@
1604891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1605891a9168SMatthew G. Knepley 
1606891a9168SMatthew G. Knepley   Input Parameter:
1607891a9168SMatthew G. Knepley . dm - The DM
1608891a9168SMatthew G. Knepley 
1609891a9168SMatthew G. Knepley   Output Parameters:
1610891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1611891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1612891a9168SMatthew G. Knepley 
1613891a9168SMatthew G. Knepley   Level: developer
1614891a9168SMatthew G. Knepley 
1615891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1616891a9168SMatthew G. Knepley @*/
1617113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1618113c68e6SMatthew G. Knepley {
1619113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1620113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1621113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1622113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1623113c68e6SMatthew G. Knepley   Vec            coordinates;
1624113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1625113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1626113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1627113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1628113c68e6SMatthew G. Knepley 
1629113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1630113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1631113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1632113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1633113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1634113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1635113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1636113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1637113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1638113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1639113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1640113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
16419e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1642113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1643113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1644113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1645113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
164606348e87SToby Isaac   if (cEndInterior < 0) {
164706348e87SToby Isaac     cEndInterior = cEnd;
164806348e87SToby Isaac   }
1649113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1650113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1651113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1652113c68e6SMatthew G. Knepley 
1653113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1654113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1655113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1656113c68e6SMatthew G. Knepley   }
1657113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1658113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1659113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1660113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1661113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
16629e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1663113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1664113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1665113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1666113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1667113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1668c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1669113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1670113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1671113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1672113c68e6SMatthew G. Knepley     PetscReal        area;
167350d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
1674113c68e6SMatthew G. Knepley 
16759ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
167650d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
167750d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
1678113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1679113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1680113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1681113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1682113c68e6SMatthew G. Knepley     {
1683113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
168406348e87SToby Isaac       PetscInt         ncells;
1685113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1686113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
16870453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1688113c68e6SMatthew G. Knepley 
1689113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
169006348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
1691113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1692113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
169306348e87SToby Isaac       if (ncells > 1) {
169406348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1695113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
169606348e87SToby Isaac       }
169706348e87SToby Isaac       else {
169806348e87SToby Isaac         rcentroid = fg->centroid;
169906348e87SToby Isaac       }
17002e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
17012e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
17020453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1703113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1704113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1705113c68e6SMatthew G. Knepley       }
1706113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1707113c68e6SMatthew 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]);
1708113c68e6SMatthew 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]);
1709113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1710113c68e6SMatthew G. Knepley       }
1711113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1712113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1713113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1714113c68e6SMatthew G. Knepley       }
171506348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
1716113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
1717113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1718113c68e6SMatthew G. Knepley       }
1719113c68e6SMatthew G. Knepley     }
1720113c68e6SMatthew G. Knepley   }
1721b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1722113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
1723113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
1724113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
1725113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1726113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
1727113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
1728113c68e6SMatthew G. Knepley 
1729113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
1730113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
1731113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
1732113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
173350d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
1734113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
1735113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
1736113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
1737113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
1738113c68e6SMatthew G. Knepley       if (support[s] == c) {
1739640bce14SSatish Balay         PetscFVCellGeom       *ci;
1740113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
1741113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
1742113c68e6SMatthew G. Knepley 
1743113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
1744113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
1745113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
1746113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
1747113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
1748113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
1749113c68e6SMatthew G. Knepley       }
1750113c68e6SMatthew G. Knepley     }
1751113c68e6SMatthew G. Knepley   }
1752113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
1753113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1754113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1755113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
1756113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1757113c68e6SMatthew G. Knepley }
1758113c68e6SMatthew G. Knepley 
1759113c68e6SMatthew G. Knepley #undef __FUNCT__
1760113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
1761113c68e6SMatthew G. Knepley /*@C
1762113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
1763113c68e6SMatthew G. Knepley 
1764113c68e6SMatthew G. Knepley   Not collective
1765113c68e6SMatthew G. Knepley 
1766113c68e6SMatthew G. Knepley   Input Argument:
1767113c68e6SMatthew G. Knepley . dm - the DM
1768113c68e6SMatthew G. Knepley 
1769113c68e6SMatthew G. Knepley   Output Argument:
1770113c68e6SMatthew G. Knepley . minradius - the minium cell radius
1771113c68e6SMatthew G. Knepley 
1772113c68e6SMatthew G. Knepley   Level: developer
1773113c68e6SMatthew G. Knepley 
1774113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
1775113c68e6SMatthew G. Knepley @*/
1776113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
1777113c68e6SMatthew G. Knepley {
1778113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1779113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1780113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
1781113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
1782113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1783113c68e6SMatthew G. Knepley }
1784113c68e6SMatthew G. Knepley 
1785113c68e6SMatthew G. Knepley #undef __FUNCT__
1786113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
1787113c68e6SMatthew G. Knepley /*@C
1788113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
1789113c68e6SMatthew G. Knepley 
1790113c68e6SMatthew G. Knepley   Logically collective
1791113c68e6SMatthew G. Knepley 
1792113c68e6SMatthew G. Knepley   Input Arguments:
1793113c68e6SMatthew G. Knepley + dm - the DM
1794113c68e6SMatthew G. Knepley - minradius - the minium cell radius
1795113c68e6SMatthew G. Knepley 
1796113c68e6SMatthew G. Knepley   Level: developer
1797113c68e6SMatthew G. Knepley 
1798113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
1799113c68e6SMatthew G. Knepley @*/
1800113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
1801113c68e6SMatthew G. Knepley {
1802113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1803113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1804113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
1805113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1806113c68e6SMatthew G. Knepley }
1807856ac710SMatthew G. Knepley 
1808856ac710SMatthew G. Knepley #undef __FUNCT__
1809856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
1810856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1811856ac710SMatthew G. Knepley {
1812856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
1813856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
1814856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
1815856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1816856ac710SMatthew G. Knepley 
1817856ac710SMatthew G. Knepley   PetscFunctionBegin;
1818856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1819856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1820856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1821856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
1822856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1823c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1824856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1825856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
1826856ac710SMatthew G. Knepley     const PetscInt        *faces;
1827856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
1828640bce14SSatish Balay     PetscFVCellGeom        *cg;
1829856ac710SMatthew G. Knepley     PetscBool              boundary;
1830856ac710SMatthew G. Knepley     PetscInt               ghost;
1831856ac710SMatthew G. Knepley 
1832856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1833856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
1834856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
1835856ac710SMatthew 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);
1836856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1837640bce14SSatish Balay       PetscFVCellGeom       *cg1;
1838856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
1839856ac710SMatthew G. Knepley       const PetscInt        *fcells;
1840856ac710SMatthew G. Knepley       PetscInt               ncell, side;
1841856ac710SMatthew G. Knepley 
1842856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1843a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1844856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1845856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
1846856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
1847856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
1848856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
1849856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1850856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
1851856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
1852856ac710SMatthew G. Knepley     }
1853856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
1854856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
1855856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1856856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1857a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1858856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1859856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
1860856ac710SMatthew G. Knepley       ++usedFaces;
1861856ac710SMatthew G. Knepley     }
1862856ac710SMatthew G. Knepley   }
1863856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
1864856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1865856ac710SMatthew G. Knepley }
1866856ac710SMatthew G. Knepley 
1867856ac710SMatthew G. Knepley #undef __FUNCT__
1868b81db932SToby Isaac #define __FUNCT__ "BuildGradientReconstruction_Internal_Tree"
1869b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1870b81db932SToby Isaac {
1871b81db932SToby Isaac   DMLabel        ghostLabel;
1872b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
1873b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
1874b81db932SToby Isaac   PetscSection   neighSec;
1875b81db932SToby Isaac   PetscInt     (*neighbors)[2];
1876b81db932SToby Isaac   PetscInt      *counter;
1877b81db932SToby Isaac   PetscErrorCode ierr;
1878b81db932SToby Isaac 
1879b81db932SToby Isaac   PetscFunctionBegin;
1880b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1881b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1882b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
18835bc680faSToby Isaac   if (cEndInterior < 0) {
18845bc680faSToby Isaac     cEndInterior = cEnd;
18855bc680faSToby Isaac   }
1886b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
1887b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
1888b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1889c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1890b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1891b81db932SToby Isaac     const PetscInt        *fcells;
1892b81db932SToby Isaac     PetscBool              boundary;
18935bc680faSToby Isaac     PetscInt               ghost = -1;
1894b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1895b81db932SToby Isaac 
189606348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1897a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1898b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1899b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1900b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
190106348e87SToby Isaac     if (numCells == 2) {
1902b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
1903b81db932SToby Isaac       for (c = 0; c < 2; c++) {
1904b81db932SToby Isaac         PetscInt cell = fcells[c];
1905b81db932SToby Isaac 
1906e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
1907b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
1908b81db932SToby Isaac         }
1909b81db932SToby Isaac       }
1910b81db932SToby Isaac     }
191106348e87SToby Isaac   }
1912b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
1913b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
1914b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1915b81db932SToby Isaac   nStart = 0;
1916b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
1917b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
1918b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
1919b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1920b81db932SToby Isaac     const PetscInt        *fcells;
1921b81db932SToby Isaac     PetscBool              boundary;
19225bc680faSToby Isaac     PetscInt               ghost = -1;
1923b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1924b81db932SToby Isaac 
192506348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1926a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1927b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1928b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1929b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
193006348e87SToby Isaac     if (numCells == 2) {
1931b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
1932b81db932SToby Isaac       for (c = 0; c < 2; c++) {
1933b81db932SToby Isaac         PetscInt cell = fcells[c], off;
1934b81db932SToby Isaac 
1935e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
1936b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
1937b81db932SToby Isaac           off += counter[cell - cStart]++;
1938b81db932SToby Isaac           neighbors[off][0] = f;
1939b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
1940b81db932SToby Isaac         }
1941b81db932SToby Isaac       }
1942b81db932SToby Isaac     }
194306348e87SToby Isaac   }
1944b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
1945b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1946b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
1947317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
1948640bce14SSatish Balay     PetscFVCellGeom        *cg;
1949b81db932SToby Isaac 
1950b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1951b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
1952b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
1953317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
1954317218b9SToby 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);
1955b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
1956640bce14SSatish Balay       PetscFVCellGeom       *cg1;
1957b81db932SToby Isaac       PetscFVFaceGeom       *fg;
1958b81db932SToby Isaac       const PetscInt        *fcells;
1959b81db932SToby Isaac       PetscInt               ncell, side, nface;
1960b81db932SToby Isaac 
1961b81db932SToby Isaac       nface = neighbors[off + f][0];
1962b81db932SToby Isaac       ncell = neighbors[off + f][1];
1963b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
1964b81db932SToby Isaac       side  = (c != fcells[0]);
1965b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
1966b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1967b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
1968b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
1969b81db932SToby Isaac     }
1970b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
1971b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
1972b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
1973b81db932SToby Isaac     }
1974b81db932SToby Isaac   }
1975b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
19765fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
1977b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
1978b81db932SToby Isaac   PetscFunctionReturn(0);
1979b81db932SToby Isaac }
1980b81db932SToby Isaac 
1981b81db932SToby Isaac #undef __FUNCT__
1982856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
1983856ac710SMatthew G. Knepley /*@
1984856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
1985856ac710SMatthew G. Knepley 
1986856ac710SMatthew G. Knepley   Collective on DM
1987856ac710SMatthew G. Knepley 
1988856ac710SMatthew G. Knepley   Input Arguments:
1989856ac710SMatthew G. Knepley + dm  - The DM
1990856ac710SMatthew G. Knepley . fvm - The PetscFV
19918f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
19928f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
1993856ac710SMatthew G. Knepley 
1994856ac710SMatthew G. Knepley   Output Parameters:
1995856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
1996856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
1997856ac710SMatthew G. Knepley 
1998856ac710SMatthew G. Knepley   Level: developer
1999856ac710SMatthew G. Knepley 
2000856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2001856ac710SMatthew G. Knepley @*/
2002856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2003856ac710SMatthew G. Knepley {
2004856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2005856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2006b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2007856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2008856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2009856ac710SMatthew G. Knepley 
2010856ac710SMatthew G. Knepley   PetscFunctionBegin;
2011856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2012856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2013856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2014856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2015856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2016856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2017856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2018856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2019856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2020b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2021b81db932SToby Isaac   if (!parentSection) {
2022856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2023b5a3613cSMatthew G. Knepley   } else {
2024b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2025b81db932SToby Isaac   }
2026856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2027856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2028856ac710SMatthew G. Knepley   /* Create storage for gradients */
2029856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2030856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2031856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2032856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2033856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2034856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2035856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2036856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2037856ac710SMatthew G. Knepley }
2038