xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision b716b415066c2a62f54dc730fcba103c0da5e0e9)
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;
57ccd2543fSMatthew G Knepley   else *cell = -1;
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;
119ccd2543fSMatthew G Knepley   else *cell = -1;
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;
143ccd2543fSMatthew G Knepley   else *cell = -1;
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;
194ccd2543fSMatthew G Knepley   else *cell = -1;
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;
624*b716b415SMatthew 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);
633*b716b415SMatthew 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?) */
6483a93e3b7SToby Isaac   if (numFound < numPoints) {
64962a38674SMatthew G. Knepley     if (ltype == DM_POINTLOCATION_NEAREST) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Nearest point location does not support parallel point location.");
6503a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
6513a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
6523a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
6533a93e3b7SToby Isaac         if (numFound < p) {
6543a93e3b7SToby Isaac           cells[numFound] = cells[p];
6553a93e3b7SToby Isaac         }
6563a93e3b7SToby Isaac         found[numFound++] = p;
6573a93e3b7SToby Isaac       }
6583a93e3b7SToby Isaac     }
6593a93e3b7SToby Isaac   }
66062a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
6613a93e3b7SToby Isaac   ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
662ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
663ccd2543fSMatthew G Knepley }
664ccd2543fSMatthew G Knepley 
665ccd2543fSMatthew G Knepley #undef __FUNCT__
66617fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D_Internal"
66717fe8556SMatthew G. Knepley /*
66817fe8556SMatthew G. Knepley   DMPlexComputeProjection2Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 2D
66917fe8556SMatthew G. Knepley */
6703beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D_Internal(PetscScalar coords[], PetscReal R[])
67117fe8556SMatthew G. Knepley {
67217fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
67317fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
6748b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
67517fe8556SMatthew G. Knepley 
67617fe8556SMatthew G. Knepley   PetscFunctionBegin;
6771c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
6781c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
67917fe8556SMatthew G. Knepley   coords[0] = 0.0;
6807f07f362SMatthew G. Knepley   coords[1] = r;
68117fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
68217fe8556SMatthew G. Knepley }
68317fe8556SMatthew G. Knepley 
68417fe8556SMatthew G. Knepley #undef __FUNCT__
68528dbe442SToby Isaac #define __FUNCT__ "DMPlexComputeProjection3Dto1D_Internal"
68628dbe442SToby Isaac /*
68728dbe442SToby Isaac   DMPlexComputeProjection3Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 3D
68828dbe442SToby Isaac 
68928dbe442SToby Isaac   This uses the basis completion described by Frisvad,
69028dbe442SToby Isaac 
69128dbe442SToby Isaac   http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html
69228dbe442SToby Isaac   DOI:10.1080/2165347X.2012.689606
69328dbe442SToby Isaac */
6943beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D_Internal(PetscScalar coords[], PetscReal R[])
69528dbe442SToby Isaac {
69628dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
69728dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
69828dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
69928dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
70028dbe442SToby Isaac   PetscReal      rinv = 1. / r;
70128dbe442SToby Isaac   PetscFunctionBegin;
70228dbe442SToby Isaac 
70328dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
70428dbe442SToby Isaac   if (x > 0.) {
70528dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
70628dbe442SToby Isaac 
70728dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
70828dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
70928dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
71028dbe442SToby Isaac   }
71128dbe442SToby Isaac   else {
71228dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
71328dbe442SToby Isaac 
71428dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
71528dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
71628dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
71728dbe442SToby Isaac   }
71828dbe442SToby Isaac   coords[0] = 0.0;
71928dbe442SToby Isaac   coords[1] = r;
72028dbe442SToby Isaac   PetscFunctionReturn(0);
72128dbe442SToby Isaac }
72228dbe442SToby Isaac 
72328dbe442SToby Isaac #undef __FUNCT__
724ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D_Internal"
725ccd2543fSMatthew G Knepley /*
726ccd2543fSMatthew G Knepley   DMPlexComputeProjection3Dto2D_Internal - Rewrite coordinates to be the 2D projection of the 3D
727ccd2543fSMatthew G Knepley */
7283beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D_Internal(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
729ccd2543fSMatthew G Knepley {
7301ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
73199dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
7324a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
733ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
73499dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
735ccd2543fSMatthew G Knepley 
736ccd2543fSMatthew G Knepley   PetscFunctionBegin;
737ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
738ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
7391ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
7401ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
741ccd2543fSMatthew G Knepley   }
742ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
743ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
744ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
7458b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
746ccd2543fSMatthew G Knepley   n[0] /= norm;
747ccd2543fSMatthew G Knepley   n[1] /= norm;
748ccd2543fSMatthew G Knepley   n[2] /= norm;
749ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
750ccd2543fSMatthew G Knepley 
751ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
752ccd2543fSMatthew G Knepley 
753ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
754ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
755ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
756ccd2543fSMatthew G Knepley 
757ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
758ccd2543fSMatthew G Knepley   */
7598b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
76073868372SMatthew G. Knepley   /* Check for n = z */
76173868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
7627df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
7637df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
76499dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
76599dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
7667df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
76773868372SMatthew G. Knepley     }
76899dec3a6SMatthew G. Knepley     coords[0] = 0.0;
76999dec3a6SMatthew G. Knepley     coords[1] = 0.0;
7707df32b8bSSanderA     coords[2] = x1[0];
7717df32b8bSSanderA     coords[3] = x1[1] * s;
7727df32b8bSSanderA     coords[4] = x2[0];
7737df32b8bSSanderA     coords[5] = x2[1] * s;
7747df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
7757df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
7767df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
77773868372SMatthew G. Knepley     PetscFunctionReturn(0);
77873868372SMatthew G. Knepley   }
779da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
780ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
781ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
782ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
783ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
784ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
785ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
786ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
787ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
788ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
789ccd2543fSMatthew G Knepley     }
790ccd2543fSMatthew G Knepley   }
7918763be8eSMatthew G. Knepley   if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
7928763be8eSMatthew G. Knepley   if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
793ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
79499dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
79599dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
79699dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
79799dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
79899dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
79999dec3a6SMatthew G. Knepley       }
80099dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
80199dec3a6SMatthew G. Knepley     }
80299dec3a6SMatthew G. Knepley   }
803ccd2543fSMatthew G Knepley   coords[0] = 0.0;
804ccd2543fSMatthew G Knepley   coords[1] = 0.0;
805ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
806ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
807ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
808ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
8097f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
8107f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
8117f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
8127f07f362SMatthew G. Knepley       PetscReal tmp;
8137f07f362SMatthew G. Knepley 
8147f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
8157f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
8167f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
8177f07f362SMatthew G. Knepley     }
8187f07f362SMatthew G. Knepley   }
819ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
820ccd2543fSMatthew G Knepley }
821ccd2543fSMatthew G Knepley 
822ccd2543fSMatthew G Knepley #undef __FUNCT__
823834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal"
8246322fe33SJed Brown PETSC_UNUSED
825834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
826834e62ceSMatthew G. Knepley {
827834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
828834e62ceSMatthew G. Knepley 
829834e62ceSMatthew G. Knepley    |  1  1  1 |
830834e62ceSMatthew G. Knepley    | x0 x1 x2 |
831834e62ceSMatthew G. Knepley    | y0 y1 y2 |
832834e62ceSMatthew G. Knepley 
833834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
834834e62ceSMatthew G. Knepley 
835834e62ceSMatthew G. Knepley    | x1 x2 |
836834e62ceSMatthew G. Knepley    | y1 y2 |
837834e62ceSMatthew G. Knepley   */
838834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
839834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
840834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
841834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
84286623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
843923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
844834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
8453bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
846834e62ceSMatthew G. Knepley }
847834e62ceSMatthew G. Knepley 
848834e62ceSMatthew G. Knepley #undef __FUNCT__
849834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal"
850834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
851834e62ceSMatthew G. Knepley {
852923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
853834e62ceSMatthew G. Knepley   *vol *= 0.5;
854834e62ceSMatthew G. Knepley }
855834e62ceSMatthew G. Knepley 
856834e62ceSMatthew G. Knepley #undef __FUNCT__
857834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal"
8586322fe33SJed Brown PETSC_UNUSED
859834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
860834e62ceSMatthew G. Knepley {
861834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
862834e62ceSMatthew G. Knepley 
863834e62ceSMatthew G. Knepley    |  1  1  1  1 |
864834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
865834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
866834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
867834e62ceSMatthew G. Knepley 
868834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
869834e62ceSMatthew G. Knepley 
870834e62ceSMatthew G. Knepley    | x1 x2 x3 |
871834e62ceSMatthew G. Knepley    | y1 y2 y3 |
872834e62ceSMatthew G. Knepley    | z1 z2 z3 |
873834e62ceSMatthew G. Knepley   */
874834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
875834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
876834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
877834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
878834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
879834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
880834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
881923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
882b7ad821dSMatthew G. Knepley   *vol = -0.16666666666666666666666*detM;
8833bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
884834e62ceSMatthew G. Knepley }
885834e62ceSMatthew G. Knepley 
886834e62ceSMatthew G. Knepley #undef __FUNCT__
8870ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal"
8880ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
8890ec8681fSMatthew G. Knepley {
890923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
891b7ad821dSMatthew G. Knepley   *vol *= -0.16666666666666666666666;
8920ec8681fSMatthew G. Knepley }
8930ec8681fSMatthew G. Knepley 
8940ec8681fSMatthew G. Knepley #undef __FUNCT__
89517fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal"
89617fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
89717fe8556SMatthew G. Knepley {
89817fe8556SMatthew G. Knepley   PetscSection   coordSection;
89917fe8556SMatthew G. Knepley   Vec            coordinates;
900a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9018bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
90217fe8556SMatthew G. Knepley   PetscErrorCode ierr;
90317fe8556SMatthew G. Knepley 
90417fe8556SMatthew G. Knepley   PetscFunctionBegin;
90517fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
90669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
9078bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
9088bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
90917fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9108bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
911adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
9127f07f362SMatthew G. Knepley   *detJ = 0.0;
91328dbe442SToby Isaac   if (numCoords == 6) {
91428dbe442SToby Isaac     const PetscInt dim = 3;
91528dbe442SToby Isaac     PetscReal      R[9], J0;
91628dbe442SToby Isaac 
91728dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
91828dbe442SToby Isaac     ierr = DMPlexComputeProjection3Dto1D_Internal(coords, R);CHKERRQ(ierr);
91928dbe442SToby Isaac     if (J)    {
92028dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
92128dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
92228dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
92328dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
92428dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
92528dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
926adac9986SMatthew G. Knepley     }
92728dbe442SToby Isaac   } else if (numCoords == 4) {
9287f07f362SMatthew G. Knepley     const PetscInt dim = 2;
9297f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
9307f07f362SMatthew G. Knepley 
9317f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
9327f07f362SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D_Internal(coords, R);CHKERRQ(ierr);
93317fe8556SMatthew G. Knepley     if (J)    {
9347f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
9357f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
9367f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
937923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
938923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
939adac9986SMatthew G. Knepley     }
9407f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
9417f07f362SMatthew G. Knepley     const PetscInt dim = 1;
9427f07f362SMatthew G. Knepley 
9437f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
9447f07f362SMatthew G. Knepley     if (J)    {
9457f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
94617fe8556SMatthew G. Knepley       *detJ = J[0];
9473bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
9483bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
949adac9986SMatthew G. Knepley     }
950796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
95117fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
95217fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
95317fe8556SMatthew G. Knepley }
95417fe8556SMatthew G. Knepley 
95517fe8556SMatthew G. Knepley #undef __FUNCT__
956ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal"
957ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
958ccd2543fSMatthew G Knepley {
959ccd2543fSMatthew G Knepley   PetscSection   coordSection;
960ccd2543fSMatthew G Knepley   Vec            coordinates;
961a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9627f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
963ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
964ccd2543fSMatthew G Knepley 
965ccd2543fSMatthew G Knepley   PetscFunctionBegin;
966ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
96769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
968ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9697f07f362SMatthew G. Knepley   *detJ = 0.0;
970ccd2543fSMatthew G Knepley   if (numCoords == 9) {
9717f07f362SMatthew G. Knepley     const PetscInt dim = 3;
9727f07f362SMatthew 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};
9737f07f362SMatthew G. Knepley 
9747f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
97599dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
9767f07f362SMatthew G. Knepley     if (J)    {
977b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
978b7ad821dSMatthew G. Knepley 
979b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
980b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
981b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
982ccd2543fSMatthew G Knepley         }
9837f07f362SMatthew G. Knepley       }
9843bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
985923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
9867f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
9877f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
9887f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
9897f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
9907f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
9917f07f362SMatthew G. Knepley           }
9927f07f362SMatthew G. Knepley         }
9937f07f362SMatthew G. Knepley       }
9943bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
9957f07f362SMatthew G. Knepley     }
996923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
9977f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
9987f07f362SMatthew G. Knepley     const PetscInt dim = 2;
9997f07f362SMatthew G. Knepley 
10007f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1001ccd2543fSMatthew G Knepley     if (J)    {
1002ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1003ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1004ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1005ccd2543fSMatthew G Knepley         }
1006ccd2543fSMatthew G Knepley       }
10073bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1008923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1009ccd2543fSMatthew G Knepley     }
1010923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1011796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1012ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1013ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1014ccd2543fSMatthew G Knepley }
1015ccd2543fSMatthew G Knepley 
1016ccd2543fSMatthew G Knepley #undef __FUNCT__
1017ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal"
1018ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1019ccd2543fSMatthew G Knepley {
1020ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1021ccd2543fSMatthew G Knepley   Vec            coordinates;
1022a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10230d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1024ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1025ccd2543fSMatthew G Knepley 
1026ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1027ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
102869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10290d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10300d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
103199dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
103271f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
10337f07f362SMatthew G. Knepley   *detJ = 0.0;
103499dec3a6SMatthew G. Knepley   if (numCoords == 12) {
103599dec3a6SMatthew G. Knepley     const PetscInt dim = 3;
103699dec3a6SMatthew 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};
103799dec3a6SMatthew G. Knepley 
103899dec3a6SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
103999dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
104099dec3a6SMatthew G. Knepley     if (J)    {
104199dec3a6SMatthew G. Knepley       const PetscInt pdim = 2;
104299dec3a6SMatthew G. Knepley 
104399dec3a6SMatthew G. Knepley       for (d = 0; d < pdim; d++) {
104499dec3a6SMatthew G. Knepley         J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
104599dec3a6SMatthew G. Knepley         J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
104699dec3a6SMatthew G. Knepley       }
10473bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1048923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
104999dec3a6SMatthew G. Knepley       for (d = 0; d < dim; d++) {
105099dec3a6SMatthew G. Knepley         for (f = 0; f < dim; f++) {
105199dec3a6SMatthew G. Knepley           J[d*dim+f] = 0.0;
105299dec3a6SMatthew G. Knepley           for (g = 0; g < dim; g++) {
105399dec3a6SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
105499dec3a6SMatthew G. Knepley           }
105599dec3a6SMatthew G. Knepley         }
105699dec3a6SMatthew G. Knepley       }
10573bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
105899dec3a6SMatthew G. Knepley     }
1059923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
106071f58de1SToby Isaac   } else if (numCoords == 8) {
106199dec3a6SMatthew G. Knepley     const PetscInt dim = 2;
106299dec3a6SMatthew G. Knepley 
10637f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1064ccd2543fSMatthew G Knepley     if (J)    {
1065ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
106699dec3a6SMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
106799dec3a6SMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1068ccd2543fSMatthew G Knepley       }
10693bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1070923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1071ccd2543fSMatthew G Knepley     }
1072923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1073796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
107499dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1075ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1076ccd2543fSMatthew G Knepley }
1077ccd2543fSMatthew G Knepley 
1078ccd2543fSMatthew G Knepley #undef __FUNCT__
1079ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal"
1080ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1081ccd2543fSMatthew G Knepley {
1082ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1083ccd2543fSMatthew G Knepley   Vec            coordinates;
1084a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1085ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
108699dec3a6SMatthew G. Knepley   PetscInt       d;
1087ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1088ccd2543fSMatthew G Knepley 
1089ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1090ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
109169d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1092ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
10937f07f362SMatthew G. Knepley   *detJ = 0.0;
10947f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1095ccd2543fSMatthew G Knepley   if (J)    {
1096ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1097f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1098f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1099f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1100f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1101ccd2543fSMatthew G Knepley     }
11023bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1103923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1104ccd2543fSMatthew G Knepley   }
1105923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1106ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1107ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1108ccd2543fSMatthew G Knepley }
1109ccd2543fSMatthew G Knepley 
1110ccd2543fSMatthew G Knepley #undef __FUNCT__
1111ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal"
1112ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1113ccd2543fSMatthew G Knepley {
1114ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1115ccd2543fSMatthew G Knepley   Vec            coordinates;
1116a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1117ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1118ccd2543fSMatthew G Knepley   PetscInt       d;
1119ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1120ccd2543fSMatthew G Knepley 
1121ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1122ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
112369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1124ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11257f07f362SMatthew G. Knepley   *detJ = 0.0;
11267f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1127ccd2543fSMatthew G Knepley   if (J)    {
1128ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1129f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1130f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1131f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1132ccd2543fSMatthew G Knepley     }
11333bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1134923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1135ccd2543fSMatthew G Knepley   }
1136923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1137ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1138ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1139ccd2543fSMatthew G Knepley }
1140ccd2543fSMatthew G Knepley 
1141ccd2543fSMatthew G Knepley #undef __FUNCT__
11428e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM"
1143ccd2543fSMatthew G Knepley /*@C
11448e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1145ccd2543fSMatthew G Knepley 
1146ccd2543fSMatthew G Knepley   Collective on DM
1147ccd2543fSMatthew G Knepley 
1148ccd2543fSMatthew G Knepley   Input Arguments:
1149ccd2543fSMatthew G Knepley + dm   - the DM
1150ccd2543fSMatthew G Knepley - cell - the cell
1151ccd2543fSMatthew G Knepley 
1152ccd2543fSMatthew G Knepley   Output Arguments:
1153ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1154ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1155ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1156ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1157ccd2543fSMatthew G Knepley 
1158ccd2543fSMatthew G Knepley   Level: advanced
1159ccd2543fSMatthew G Knepley 
1160ccd2543fSMatthew G Knepley   Fortran Notes:
1161ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1162ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1163ccd2543fSMatthew G Knepley 
11648e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
1165ccd2543fSMatthew G Knepley @*/
11668e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1167ccd2543fSMatthew G Knepley {
116849dc4407SMatthew G. Knepley   PetscInt       depth, dim, coneSize;
1169ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1170ccd2543fSMatthew G Knepley 
1171ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1172139a35ccSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1173ccd2543fSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
117449dc4407SMatthew G. Knepley   if (depth == 1) {
11758e0841e0SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
11768e0841e0SMatthew G. Knepley   } else {
11778e0841e0SMatthew G. Knepley     DMLabel depth;
11788e0841e0SMatthew G. Knepley 
11798e0841e0SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
11808e0841e0SMatthew G. Knepley     ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr);
11818e0841e0SMatthew G. Knepley   }
1182ccd2543fSMatthew G Knepley   switch (dim) {
118317fe8556SMatthew G. Knepley   case 1:
118417fe8556SMatthew G. Knepley     ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
118517fe8556SMatthew G. Knepley     break;
1186ccd2543fSMatthew G Knepley   case 2:
1187ccd2543fSMatthew G Knepley     switch (coneSize) {
1188ccd2543fSMatthew G Knepley     case 3:
1189ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1190ccd2543fSMatthew G Knepley       break;
1191ccd2543fSMatthew G Knepley     case 4:
1192ccd2543fSMatthew G Knepley       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1193ccd2543fSMatthew G Knepley       break;
1194ccd2543fSMatthew G Knepley     default:
11958e0841e0SMatthew G. Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1196ccd2543fSMatthew G Knepley     }
1197ccd2543fSMatthew G Knepley     break;
1198ccd2543fSMatthew G Knepley   case 3:
1199ccd2543fSMatthew G Knepley     switch (coneSize) {
1200ccd2543fSMatthew G Knepley     case 4:
1201ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1202ccd2543fSMatthew G Knepley       break;
12038e0841e0SMatthew G. Knepley     case 6: /* Faces */
12048e0841e0SMatthew G. Knepley     case 8: /* Vertices */
1205ccd2543fSMatthew G Knepley       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1206ccd2543fSMatthew G Knepley       break;
1207ccd2543fSMatthew G Knepley     default:
12088e0841e0SMatthew G. Knepley         SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1209ccd2543fSMatthew G Knepley     }
1210ccd2543fSMatthew G Knepley       break;
1211ccd2543fSMatthew G Knepley   default:
1212ccd2543fSMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1213ccd2543fSMatthew G Knepley   }
12148e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
12158e0841e0SMatthew G. Knepley }
12168e0841e0SMatthew G. Knepley 
12178e0841e0SMatthew G. Knepley #undef __FUNCT__
12188e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal"
12198e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
12208e0841e0SMatthew G. Knepley {
12218e0841e0SMatthew G. Knepley   PetscQuadrature  quad;
12228e0841e0SMatthew G. Knepley   PetscSection     coordSection;
12238e0841e0SMatthew G. Knepley   Vec              coordinates;
12248e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
12258e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
12268e0841e0SMatthew G. Knepley   PetscReal       *basisDer;
12278e0841e0SMatthew G. Knepley   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, d, q;
12288e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
12298e0841e0SMatthew G. Knepley 
12308e0841e0SMatthew G. Knepley   PetscFunctionBegin;
12318e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
12328e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
12338e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
12348e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12358e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
12368e0841e0SMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1237954b1791SMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
12388e0841e0SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
12398e0841e0SMatthew G. Knepley   ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
12408e0841e0SMatthew G. Knepley   *detJ = 0.0;
12418e0841e0SMatthew G. Knepley   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
12428e0841e0SMatthew 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);
12438e0841e0SMatthew G. Knepley   if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);}
12448e0841e0SMatthew G. Knepley   if (J) {
12450790e268SMatthew G. Knepley     ierr = PetscMemzero(J, Nq*cdim*dim*sizeof(PetscReal));CHKERRQ(ierr);
12468e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
12478e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
12488e0841e0SMatthew G. Knepley 
12498e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
12508e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
12518e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
12528e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
125371d6e60fSMatthew G. Knepley             J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
12543bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
12558e0841e0SMatthew G. Knepley       if (cdim > dim) {
12568e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
12578e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
12588e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
12598e0841e0SMatthew G. Knepley       }
12608e0841e0SMatthew G. Knepley       switch (cdim) {
12618e0841e0SMatthew G. Knepley       case 3:
12628e0841e0SMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J);
12638e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
126417fe8556SMatthew G. Knepley         break;
126549dc4407SMatthew G. Knepley       case 2:
12668e0841e0SMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
12678e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
126849dc4407SMatthew G. Knepley         break;
12698e0841e0SMatthew G. Knepley       case 1:
12708e0841e0SMatthew G. Knepley         *detJ = J[0];
12718e0841e0SMatthew G. Knepley         if (invJ) invJ[0] = 1.0/J[0];
127249dc4407SMatthew G. Knepley       }
127349dc4407SMatthew G. Knepley     }
12748e0841e0SMatthew G. Knepley   }
12758e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
12768e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
12778e0841e0SMatthew G. Knepley }
12788e0841e0SMatthew G. Knepley 
12798e0841e0SMatthew G. Knepley #undef __FUNCT__
12808e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
12818e0841e0SMatthew G. Knepley /*@C
12828e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
12838e0841e0SMatthew G. Knepley 
12848e0841e0SMatthew G. Knepley   Collective on DM
12858e0841e0SMatthew G. Knepley 
12868e0841e0SMatthew G. Knepley   Input Arguments:
12878e0841e0SMatthew G. Knepley + dm   - the DM
12888e0841e0SMatthew G. Knepley . cell - the cell
12898e0841e0SMatthew G. Knepley - fe   - the finite element containing the quadrature
12908e0841e0SMatthew G. Knepley 
12918e0841e0SMatthew G. Knepley   Output Arguments:
12928e0841e0SMatthew G. Knepley + v0   - the translation part of this transform
12938e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
12948e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
12958e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
12968e0841e0SMatthew G. Knepley 
12978e0841e0SMatthew G. Knepley   Level: advanced
12988e0841e0SMatthew G. Knepley 
12998e0841e0SMatthew G. Knepley   Fortran Notes:
13008e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
13018e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
13028e0841e0SMatthew G. Knepley 
13038e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
13048e0841e0SMatthew G. Knepley @*/
13058e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
13068e0841e0SMatthew G. Knepley {
13078e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
13088e0841e0SMatthew G. Knepley 
13098e0841e0SMatthew G. Knepley   PetscFunctionBegin;
13108e0841e0SMatthew G. Knepley   if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
13118e0841e0SMatthew G. Knepley   else     {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
1312ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1313ccd2543fSMatthew G Knepley }
1314834e62ceSMatthew G. Knepley 
1315834e62ceSMatthew G. Knepley #undef __FUNCT__
1316cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
1317011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1318cc08537eSMatthew G. Knepley {
1319cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1320cc08537eSMatthew G. Knepley   Vec            coordinates;
1321a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
132206e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1323cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1324cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1325cc08537eSMatthew G. Knepley 
1326cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1327cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
132869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1329cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1330011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
13312e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1332cc08537eSMatthew G. Knepley   if (centroid) {
133306e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
133406e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1335cc08537eSMatthew G. Knepley   }
1336cc08537eSMatthew G. Knepley   if (normal) {
1337a60a936bSMatthew G. Knepley     PetscReal norm;
1338a60a936bSMatthew G. Knepley 
133906e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
134006e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1341a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1342a60a936bSMatthew G. Knepley     normal[0] /= norm;
1343a60a936bSMatthew G. Knepley     normal[1] /= norm;
1344cc08537eSMatthew G. Knepley   }
1345cc08537eSMatthew G. Knepley   if (vol) {
134606e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1347cc08537eSMatthew G. Knepley   }
1348cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1349cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1350cc08537eSMatthew G. Knepley }
1351cc08537eSMatthew G. Knepley 
1352cc08537eSMatthew G. Knepley #undef __FUNCT__
1353cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1354cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1355011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1356cc08537eSMatthew G. Knepley {
1357cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1358cc08537eSMatthew G. Knepley   Vec            coordinates;
1359cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
13600a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
13610a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1362cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1363cc08537eSMatthew G. Knepley 
1364cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1365cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
13660a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
136769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1368cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
13690bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1370ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1371ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1372ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1373ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1374ceee4971SMatthew G. Knepley   }
1375011ea5d8SMatthew G. Knepley   if (normal) {
1376011ea5d8SMatthew G. Knepley     if (dim > 2) {
13771ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
13781ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
13791ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
13800a1d6728SMatthew G. Knepley       PetscReal       norm;
13810a1d6728SMatthew G. Knepley 
13820a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
13830a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
13840a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
13858b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
13860a1d6728SMatthew G. Knepley       normal[0] /= norm;
13870a1d6728SMatthew G. Knepley       normal[1] /= norm;
13880a1d6728SMatthew G. Knepley       normal[2] /= norm;
1389011ea5d8SMatthew G. Knepley     } else {
1390011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1391011ea5d8SMatthew G. Knepley     }
1392011ea5d8SMatthew G. Knepley   }
139399dec3a6SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D_Internal(coordSize, coords, R);CHKERRQ(ierr);}
13940a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
13950a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
13960a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
13971ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
13981ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
13990a1d6728SMatthew G. Knepley     }
14000a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
14010a1d6728SMatthew G. Knepley     vsum += vtmp;
14020a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14030a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
14040a1d6728SMatthew G. Knepley     }
14050a1d6728SMatthew G. Knepley   }
14060a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
14070a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
14080a1d6728SMatthew G. Knepley   }
14090a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1410ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
14110a1d6728SMatthew G. Knepley   if (centroid) {
14120a1d6728SMatthew G. Knepley     if (dim > 2) {
14130a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14140a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
14150a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
14160a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
14170a1d6728SMatthew G. Knepley         }
14180a1d6728SMatthew G. Knepley       }
14190a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
14200a1d6728SMatthew G. Knepley   }
1421cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1422cc08537eSMatthew G. Knepley }
1423cc08537eSMatthew G. Knepley 
1424cc08537eSMatthew G. Knepley #undef __FUNCT__
14250ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
14260ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1427011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
14280ec8681fSMatthew G. Knepley {
14290ec8681fSMatthew G. Knepley   PetscSection    coordSection;
14300ec8681fSMatthew G. Knepley   Vec             coordinates;
14310ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
143286623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1433a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
14340ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
14350ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
14360ec8681fSMatthew G. Knepley 
14370ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1438f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
14390ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
144069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
14410ec8681fSMatthew G. Knepley 
1442d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
14430ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
14440ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1445a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
14460ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1447011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
14480ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
14490ec8681fSMatthew G. Knepley     switch (numCorners) {
14500ec8681fSMatthew G. Knepley     case 3:
14510ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14521ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
14531ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
14541ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
14550ec8681fSMatthew G. Knepley       }
14560ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1457a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
14580ec8681fSMatthew G. Knepley       vsum += vtmp;
14594f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
14600ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
14611ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
14620ec8681fSMatthew G. Knepley         }
14630ec8681fSMatthew G. Knepley       }
14640ec8681fSMatthew G. Knepley       break;
14650ec8681fSMatthew G. Knepley     case 4:
14660ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
14670ec8681fSMatthew G. Knepley       /* First tet */
14680ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14691ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
14701ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
14711ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
14720ec8681fSMatthew G. Knepley       }
14730ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1474a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
14750ec8681fSMatthew G. Knepley       vsum += vtmp;
14760ec8681fSMatthew G. Knepley       if (centroid) {
14770ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
14780ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
14790ec8681fSMatthew G. Knepley         }
14800ec8681fSMatthew G. Knepley       }
14810ec8681fSMatthew G. Knepley       /* Second tet */
14820ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14831ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
14841ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
14851ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
14860ec8681fSMatthew G. Knepley       }
14870ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1488a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
14890ec8681fSMatthew G. Knepley       vsum += vtmp;
14900ec8681fSMatthew G. Knepley       if (centroid) {
14910ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
14920ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
14930ec8681fSMatthew G. Knepley         }
14940ec8681fSMatthew G. Knepley       }
14950ec8681fSMatthew G. Knepley       break;
14960ec8681fSMatthew G. Knepley     default:
1497796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
14980ec8681fSMatthew G. Knepley     }
14994f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
15000ec8681fSMatthew G. Knepley   }
15018763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
15020ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1503d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
15040ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
15050ec8681fSMatthew G. Knepley }
15060ec8681fSMatthew G. Knepley 
15070ec8681fSMatthew G. Knepley #undef __FUNCT__
1508834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1509834e62ceSMatthew G. Knepley /*@C
1510834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1511834e62ceSMatthew G. Knepley 
1512834e62ceSMatthew G. Knepley   Collective on DM
1513834e62ceSMatthew G. Knepley 
1514834e62ceSMatthew G. Knepley   Input Arguments:
1515834e62ceSMatthew G. Knepley + dm   - the DM
1516834e62ceSMatthew G. Knepley - cell - the cell
1517834e62ceSMatthew G. Knepley 
1518834e62ceSMatthew G. Knepley   Output Arguments:
1519834e62ceSMatthew G. Knepley + volume   - the cell volume
1520cc08537eSMatthew G. Knepley . centroid - the cell centroid
1521cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1522834e62ceSMatthew G. Knepley 
1523834e62ceSMatthew G. Knepley   Level: advanced
1524834e62ceSMatthew G. Knepley 
1525834e62ceSMatthew G. Knepley   Fortran Notes:
1526834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1527834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1528834e62ceSMatthew G. Knepley 
152969d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1530834e62ceSMatthew G. Knepley @*/
1531cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1532834e62ceSMatthew G. Knepley {
15330ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1534834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1535834e62ceSMatthew G. Knepley 
1536834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1537834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1538c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1539834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1540834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1541c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1542834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1543011ea5d8SMatthew G. Knepley   switch (depth) {
1544cc08537eSMatthew G. Knepley   case 1:
1545011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1546cc08537eSMatthew G. Knepley     break;
1547834e62ceSMatthew G. Knepley   case 2:
1548011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1549834e62ceSMatthew G. Knepley     break;
1550834e62ceSMatthew G. Knepley   case 3:
1551011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1552834e62ceSMatthew G. Knepley     break;
1553834e62ceSMatthew G. Knepley   default:
1554834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1555834e62ceSMatthew G. Knepley   }
1556834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1557834e62ceSMatthew G. Knepley }
1558113c68e6SMatthew G. Knepley 
1559113c68e6SMatthew G. Knepley #undef __FUNCT__
1560c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1561c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1562c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1563c0d900a5SMatthew G. Knepley {
1564c0d900a5SMatthew G. Knepley   DM             dmCell;
1565c0d900a5SMatthew G. Knepley   Vec            coordinates;
1566c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1567c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1568c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1569c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1570c0d900a5SMatthew G. Knepley 
1571c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1572c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1573c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1574c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1575c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1576c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1577c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1578c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1579c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1580c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1581c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1582c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
15839e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1584c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1585c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1586c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1587c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1588c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1589c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1590c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1591c0d900a5SMatthew G. Knepley 
1592c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1593c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1594c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1595c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1596c0d900a5SMatthew G. Knepley   }
1597c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1598c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1599c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1600c0d900a5SMatthew G. Knepley }
1601c0d900a5SMatthew G. Knepley 
1602c0d900a5SMatthew G. Knepley #undef __FUNCT__
1603113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1604891a9168SMatthew G. Knepley /*@
1605891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1606891a9168SMatthew G. Knepley 
1607891a9168SMatthew G. Knepley   Input Parameter:
1608891a9168SMatthew G. Knepley . dm - The DM
1609891a9168SMatthew G. Knepley 
1610891a9168SMatthew G. Knepley   Output Parameters:
1611891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1612891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1613891a9168SMatthew G. Knepley 
1614891a9168SMatthew G. Knepley   Level: developer
1615891a9168SMatthew G. Knepley 
1616891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1617891a9168SMatthew G. Knepley @*/
1618113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1619113c68e6SMatthew G. Knepley {
1620113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1621113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1622113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1623113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1624113c68e6SMatthew G. Knepley   Vec            coordinates;
1625113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1626113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1627113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1628113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1629113c68e6SMatthew G. Knepley 
1630113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1631113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1632113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1633113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1634113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1635113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1636113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1637113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1638113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1639113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1640113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1641113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
16429e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1643113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1644113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1645113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1646113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
164706348e87SToby Isaac   if (cEndInterior < 0) {
164806348e87SToby Isaac     cEndInterior = cEnd;
164906348e87SToby Isaac   }
1650113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1651113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1652113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1653113c68e6SMatthew G. Knepley 
1654113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1655113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1656113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1657113c68e6SMatthew G. Knepley   }
1658113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1659113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1660113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1661113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1662113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
16639e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1664113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1665113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1666113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1667113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1668113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1669c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1670113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1671113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1672113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1673113c68e6SMatthew G. Knepley     PetscReal        area;
167450d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
1675113c68e6SMatthew G. Knepley 
16769ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
167750d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
167850d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
1679113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1680113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1681113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1682113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1683113c68e6SMatthew G. Knepley     {
1684113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
168506348e87SToby Isaac       PetscInt         ncells;
1686113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1687113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
16880453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1689113c68e6SMatthew G. Knepley 
1690113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
169106348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
1692113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1693113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
169406348e87SToby Isaac       if (ncells > 1) {
169506348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1696113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
169706348e87SToby Isaac       }
169806348e87SToby Isaac       else {
169906348e87SToby Isaac         rcentroid = fg->centroid;
170006348e87SToby Isaac       }
17012e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
17022e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
17030453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1704113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1705113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1706113c68e6SMatthew G. Knepley       }
1707113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1708113c68e6SMatthew 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]);
1709113c68e6SMatthew 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]);
1710113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1711113c68e6SMatthew G. Knepley       }
1712113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1713113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1714113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1715113c68e6SMatthew G. Knepley       }
171606348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
1717113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
1718113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1719113c68e6SMatthew G. Knepley       }
1720113c68e6SMatthew G. Knepley     }
1721113c68e6SMatthew G. Knepley   }
1722b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1723113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
1724113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
1725113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
1726113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1727113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
1728113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
1729113c68e6SMatthew G. Knepley 
1730113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
1731113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
1732113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
1733113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
173450d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
1735113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
1736113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
1737113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
1738113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
1739113c68e6SMatthew G. Knepley       if (support[s] == c) {
1740640bce14SSatish Balay         PetscFVCellGeom       *ci;
1741113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
1742113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
1743113c68e6SMatthew G. Knepley 
1744113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
1745113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
1746113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
1747113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
1748113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
1749113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
1750113c68e6SMatthew G. Knepley       }
1751113c68e6SMatthew G. Knepley     }
1752113c68e6SMatthew G. Knepley   }
1753113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
1754113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1755113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1756113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
1757113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1758113c68e6SMatthew G. Knepley }
1759113c68e6SMatthew G. Knepley 
1760113c68e6SMatthew G. Knepley #undef __FUNCT__
1761113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
1762113c68e6SMatthew G. Knepley /*@C
1763113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
1764113c68e6SMatthew G. Knepley 
1765113c68e6SMatthew G. Knepley   Not collective
1766113c68e6SMatthew G. Knepley 
1767113c68e6SMatthew G. Knepley   Input Argument:
1768113c68e6SMatthew G. Knepley . dm - the DM
1769113c68e6SMatthew G. Knepley 
1770113c68e6SMatthew G. Knepley   Output Argument:
1771113c68e6SMatthew G. Knepley . minradius - the minium cell radius
1772113c68e6SMatthew G. Knepley 
1773113c68e6SMatthew G. Knepley   Level: developer
1774113c68e6SMatthew G. Knepley 
1775113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
1776113c68e6SMatthew G. Knepley @*/
1777113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
1778113c68e6SMatthew G. Knepley {
1779113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1780113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1781113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
1782113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
1783113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1784113c68e6SMatthew G. Knepley }
1785113c68e6SMatthew G. Knepley 
1786113c68e6SMatthew G. Knepley #undef __FUNCT__
1787113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
1788113c68e6SMatthew G. Knepley /*@C
1789113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
1790113c68e6SMatthew G. Knepley 
1791113c68e6SMatthew G. Knepley   Logically collective
1792113c68e6SMatthew G. Knepley 
1793113c68e6SMatthew G. Knepley   Input Arguments:
1794113c68e6SMatthew G. Knepley + dm - the DM
1795113c68e6SMatthew G. Knepley - minradius - the minium cell radius
1796113c68e6SMatthew G. Knepley 
1797113c68e6SMatthew G. Knepley   Level: developer
1798113c68e6SMatthew G. Knepley 
1799113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
1800113c68e6SMatthew G. Knepley @*/
1801113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
1802113c68e6SMatthew G. Knepley {
1803113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1804113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1805113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
1806113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1807113c68e6SMatthew G. Knepley }
1808856ac710SMatthew G. Knepley 
1809856ac710SMatthew G. Knepley #undef __FUNCT__
1810856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
1811856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1812856ac710SMatthew G. Knepley {
1813856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
1814856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
1815856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
1816856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1817856ac710SMatthew G. Knepley 
1818856ac710SMatthew G. Knepley   PetscFunctionBegin;
1819856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1820856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1821856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1822856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
1823856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1824c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1825856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1826856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
1827856ac710SMatthew G. Knepley     const PetscInt        *faces;
1828856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
1829640bce14SSatish Balay     PetscFVCellGeom        *cg;
1830856ac710SMatthew G. Knepley     PetscBool              boundary;
1831856ac710SMatthew G. Knepley     PetscInt               ghost;
1832856ac710SMatthew G. Knepley 
1833856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1834856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
1835856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
1836856ac710SMatthew 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);
1837856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1838640bce14SSatish Balay       PetscFVCellGeom       *cg1;
1839856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
1840856ac710SMatthew G. Knepley       const PetscInt        *fcells;
1841856ac710SMatthew G. Knepley       PetscInt               ncell, side;
1842856ac710SMatthew G. Knepley 
1843856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1844a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1845856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1846856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
1847856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
1848856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
1849856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
1850856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1851856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
1852856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
1853856ac710SMatthew G. Knepley     }
1854856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
1855856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
1856856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1857856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1858a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1859856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1860856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
1861856ac710SMatthew G. Knepley       ++usedFaces;
1862856ac710SMatthew G. Knepley     }
1863856ac710SMatthew G. Knepley   }
1864856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
1865856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1866856ac710SMatthew G. Knepley }
1867856ac710SMatthew G. Knepley 
1868856ac710SMatthew G. Knepley #undef __FUNCT__
1869b81db932SToby Isaac #define __FUNCT__ "BuildGradientReconstruction_Internal_Tree"
1870b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1871b81db932SToby Isaac {
1872b81db932SToby Isaac   DMLabel        ghostLabel;
1873b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
1874b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
1875b81db932SToby Isaac   PetscSection   neighSec;
1876b81db932SToby Isaac   PetscInt     (*neighbors)[2];
1877b81db932SToby Isaac   PetscInt      *counter;
1878b81db932SToby Isaac   PetscErrorCode ierr;
1879b81db932SToby Isaac 
1880b81db932SToby Isaac   PetscFunctionBegin;
1881b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1882b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1883b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
18845bc680faSToby Isaac   if (cEndInterior < 0) {
18855bc680faSToby Isaac     cEndInterior = cEnd;
18865bc680faSToby Isaac   }
1887b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
1888b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
1889b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1890c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1891b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1892b81db932SToby Isaac     const PetscInt        *fcells;
1893b81db932SToby Isaac     PetscBool              boundary;
18945bc680faSToby Isaac     PetscInt               ghost = -1;
1895b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1896b81db932SToby Isaac 
189706348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1898a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1899b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1900b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1901b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
190206348e87SToby Isaac     if (numCells == 2) {
1903b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
1904b81db932SToby Isaac       for (c = 0; c < 2; c++) {
1905b81db932SToby Isaac         PetscInt cell = fcells[c];
1906b81db932SToby Isaac 
1907e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
1908b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
1909b81db932SToby Isaac         }
1910b81db932SToby Isaac       }
1911b81db932SToby Isaac     }
191206348e87SToby Isaac   }
1913b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
1914b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
1915b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1916b81db932SToby Isaac   nStart = 0;
1917b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
1918b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
1919b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
1920b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1921b81db932SToby Isaac     const PetscInt        *fcells;
1922b81db932SToby Isaac     PetscBool              boundary;
19235bc680faSToby Isaac     PetscInt               ghost = -1;
1924b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1925b81db932SToby Isaac 
192606348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1927a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1928b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1929b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1930b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
193106348e87SToby Isaac     if (numCells == 2) {
1932b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
1933b81db932SToby Isaac       for (c = 0; c < 2; c++) {
1934b81db932SToby Isaac         PetscInt cell = fcells[c], off;
1935b81db932SToby Isaac 
1936e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
1937b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
1938b81db932SToby Isaac           off += counter[cell - cStart]++;
1939b81db932SToby Isaac           neighbors[off][0] = f;
1940b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
1941b81db932SToby Isaac         }
1942b81db932SToby Isaac       }
1943b81db932SToby Isaac     }
194406348e87SToby Isaac   }
1945b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
1946b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1947b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
1948317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
1949640bce14SSatish Balay     PetscFVCellGeom        *cg;
1950b81db932SToby Isaac 
1951b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1952b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
1953b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
1954317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
1955317218b9SToby 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);
1956b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
1957640bce14SSatish Balay       PetscFVCellGeom       *cg1;
1958b81db932SToby Isaac       PetscFVFaceGeom       *fg;
1959b81db932SToby Isaac       const PetscInt        *fcells;
1960b81db932SToby Isaac       PetscInt               ncell, side, nface;
1961b81db932SToby Isaac 
1962b81db932SToby Isaac       nface = neighbors[off + f][0];
1963b81db932SToby Isaac       ncell = neighbors[off + f][1];
1964b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
1965b81db932SToby Isaac       side  = (c != fcells[0]);
1966b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
1967b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1968b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
1969b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
1970b81db932SToby Isaac     }
1971b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
1972b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
1973b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
1974b81db932SToby Isaac     }
1975b81db932SToby Isaac   }
1976b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
19775fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
1978b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
1979b81db932SToby Isaac   PetscFunctionReturn(0);
1980b81db932SToby Isaac }
1981b81db932SToby Isaac 
1982b81db932SToby Isaac #undef __FUNCT__
1983856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
1984856ac710SMatthew G. Knepley /*@
1985856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
1986856ac710SMatthew G. Knepley 
1987856ac710SMatthew G. Knepley   Collective on DM
1988856ac710SMatthew G. Knepley 
1989856ac710SMatthew G. Knepley   Input Arguments:
1990856ac710SMatthew G. Knepley + dm  - The DM
1991856ac710SMatthew G. Knepley . fvm - The PetscFV
1992856ac710SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexGetFaceGeometryFVM()
1993856ac710SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexGetCellGeometryFVM()
1994856ac710SMatthew G. Knepley 
1995856ac710SMatthew G. Knepley   Output Parameters:
1996856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
1997856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
1998856ac710SMatthew G. Knepley 
1999856ac710SMatthew G. Knepley   Level: developer
2000856ac710SMatthew G. Knepley 
2001856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2002856ac710SMatthew G. Knepley @*/
2003856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2004856ac710SMatthew G. Knepley {
2005856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2006856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2007b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2008856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2009856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2010856ac710SMatthew G. Knepley 
2011856ac710SMatthew G. Knepley   PetscFunctionBegin;
2012856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2013856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2014856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2015856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2016856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2017856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2018856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2019856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2020856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2021b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2022b81db932SToby Isaac   if (!parentSection) {
2023856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2024b5a3613cSMatthew G. Knepley   } else {
2025b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2026b81db932SToby Isaac   }
2027856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2028856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2029856ac710SMatthew G. Knepley   /* Create storage for gradients */
2030856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2031856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2032856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2033856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2034856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2035856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2036856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2037856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2038856ac710SMatthew G. Knepley }
2039