xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 080342d120e9cea7bf7533c1a39f7396dff9dfed)
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;
566*080342d1SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && !hash) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Nearest point location only supported with grid hashing. Use -dm_plex_hash_location to enable it.");
567cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
568cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
5693a93e3b7SToby Isaac   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr);
5703a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
571cafe43deSMatthew G. Knepley   if (bs != dim) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Block size for point vector %D must be the mesh coordinate dimension %D", bs, dim);
572ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
573ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
574ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
575ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
576ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
577ccd2543fSMatthew G Knepley   numPoints /= bs;
578785e854fSJed Brown   ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
579953fc75cSMatthew G. Knepley   if (hash) {
580ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
581cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
582cafe43deSMatthew G. Knepley     /* Send points to correct process */
583cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
584cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
585cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
586953fc75cSMatthew G. Knepley   }
5873a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
588ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
589953fc75cSMatthew G. Knepley     PetscInt           dbin[3], bin, cell = -1, cellOffset;
590ccd2543fSMatthew G Knepley 
5913a93e3b7SToby Isaac     cells[p].rank  = -1;
5923a93e3b7SToby Isaac     cells[p].index = -1;
593953fc75cSMatthew G. Knepley     if (hash) {
594cafe43deSMatthew G. Knepley       ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
595cafe43deSMatthew G. Knepley       /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
596cafe43deSMatthew G. Knepley       ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
597cafe43deSMatthew G. Knepley       ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
598cafe43deSMatthew G. Knepley       for (c = cellOffset; c < cellOffset + numCells; ++c) {
599cafe43deSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
6003a93e3b7SToby Isaac         if (cell >= 0) {
6013a93e3b7SToby Isaac           cells[p].rank = 0;
6023a93e3b7SToby Isaac           cells[p].index = cell;
6033a93e3b7SToby Isaac           numFound++;
6043a93e3b7SToby Isaac           break;
605ccd2543fSMatthew G Knepley         }
6063a93e3b7SToby Isaac       }
607953fc75cSMatthew G. Knepley     } else {
608953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
609953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
6103a93e3b7SToby Isaac         if (cell >= 0) {
6113a93e3b7SToby Isaac           cells[p].rank = 0;
6123a93e3b7SToby Isaac           cells[p].index = cell;
6133a93e3b7SToby Isaac           numFound++;
6143a93e3b7SToby Isaac           break;
615953fc75cSMatthew G. Knepley         }
616953fc75cSMatthew G. Knepley       }
6173a93e3b7SToby Isaac     }
618ccd2543fSMatthew G Knepley   }
619953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
62062a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
62162a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
62262a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
62362a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
624b716b415SMatthew G. Knepley       PetscInt           dbin[3], bin, cellOffset, d;
62562a38674SMatthew G. Knepley 
62662a38674SMatthew G. Knepley       if (cells[p].rank < 0) {
62762a38674SMatthew G. Knepley         ++numFound;
62862a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
62962a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
63062a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
63162a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
63262a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
633b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
63462a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
63562a38674SMatthew G. Knepley           if (dist < distMax) {
63662a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
63762a38674SMatthew G. Knepley             cells[p].rank  = 0;
63862a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
63962a38674SMatthew G. Knepley             distMax = dist;
64062a38674SMatthew G. Knepley             break;
64162a38674SMatthew G. Knepley           }
64262a38674SMatthew G. Knepley         }
64362a38674SMatthew G. Knepley       }
64462a38674SMatthew G. Knepley     }
64562a38674SMatthew G. Knepley   }
64662a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
647cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
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__
666741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D"
667741bfc07SMatthew G. Knepley /*@C
668741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
669741bfc07SMatthew G. Knepley 
670741bfc07SMatthew G. Knepley   Not collective
671741bfc07SMatthew G. Knepley 
672741bfc07SMatthew G. Knepley   Input Parameter:
673741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
674741bfc07SMatthew G. Knepley 
675741bfc07SMatthew G. Knepley   Output Parameters:
676741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x
677741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
678741bfc07SMatthew G. Knepley 
679741bfc07SMatthew G. Knepley   Level: developer
680741bfc07SMatthew G. Knepley 
681741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
682741bfc07SMatthew G. Knepley @*/
683741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
68417fe8556SMatthew G. Knepley {
68517fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
68617fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
6878b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
68817fe8556SMatthew G. Knepley 
68917fe8556SMatthew G. Knepley   PetscFunctionBegin;
6901c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
6911c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
69217fe8556SMatthew G. Knepley   coords[0] = 0.0;
6937f07f362SMatthew G. Knepley   coords[1] = r;
69417fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
69517fe8556SMatthew G. Knepley }
69617fe8556SMatthew G. Knepley 
69717fe8556SMatthew G. Knepley #undef __FUNCT__
698741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto1D"
699741bfc07SMatthew G. Knepley /*@C
700741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
70128dbe442SToby Isaac 
702741bfc07SMatthew G. Knepley   Not collective
70328dbe442SToby Isaac 
704741bfc07SMatthew G. Knepley   Input Parameter:
705741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
706741bfc07SMatthew G. Knepley 
707741bfc07SMatthew G. Knepley   Output Parameters:
708741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x and z
709741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
710741bfc07SMatthew G. Knepley 
711741bfc07SMatthew G. Knepley   Note: This uses the basis completion described by Frisvad in http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html, DOI:10.1080/2165347X.2012.689606
712741bfc07SMatthew G. Knepley 
713741bfc07SMatthew G. Knepley   Level: developer
714741bfc07SMatthew G. Knepley 
715741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
716741bfc07SMatthew G. Knepley @*/
717741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
71828dbe442SToby Isaac {
71928dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
72028dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
72128dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
72228dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
72328dbe442SToby Isaac   PetscReal      rinv = 1. / r;
72428dbe442SToby Isaac   PetscFunctionBegin;
72528dbe442SToby Isaac 
72628dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
72728dbe442SToby Isaac   if (x > 0.) {
72828dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
72928dbe442SToby Isaac 
73028dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
73128dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
73228dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
73328dbe442SToby Isaac   }
73428dbe442SToby Isaac   else {
73528dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
73628dbe442SToby Isaac 
73728dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
73828dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
73928dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
74028dbe442SToby Isaac   }
74128dbe442SToby Isaac   coords[0] = 0.0;
74228dbe442SToby Isaac   coords[1] = r;
74328dbe442SToby Isaac   PetscFunctionReturn(0);
74428dbe442SToby Isaac }
74528dbe442SToby Isaac 
74628dbe442SToby Isaac #undef __FUNCT__
747741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D"
748741bfc07SMatthew G. Knepley /*@
749741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto2D - Rewrite coordinates to be the 2D projection of the 3D coordinates
750741bfc07SMatthew G. Knepley 
751741bfc07SMatthew G. Knepley   Not collective
752741bfc07SMatthew G. Knepley 
753741bfc07SMatthew G. Knepley   Input Parameter:
754741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
755741bfc07SMatthew G. Knepley 
756741bfc07SMatthew G. Knepley   Output Parameters:
757741bfc07SMatthew G. Knepley + coords - The new y- and z-coordinates, and 0 for x
758741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
759741bfc07SMatthew G. Knepley 
760741bfc07SMatthew G. Knepley   Level: developer
761741bfc07SMatthew G. Knepley 
762741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
763741bfc07SMatthew G. Knepley @*/
764741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
765ccd2543fSMatthew G Knepley {
7661ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
76799dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
7684a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
769ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
77099dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
771ccd2543fSMatthew G Knepley 
772ccd2543fSMatthew G Knepley   PetscFunctionBegin;
773ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
774ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
7751ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
7761ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
777ccd2543fSMatthew G Knepley   }
778ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
779ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
780ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
7818b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
782ccd2543fSMatthew G Knepley   n[0] /= norm;
783ccd2543fSMatthew G Knepley   n[1] /= norm;
784ccd2543fSMatthew G Knepley   n[2] /= norm;
785ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
786ccd2543fSMatthew G Knepley 
787ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
788ccd2543fSMatthew G Knepley 
789ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
790ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
791ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
792ccd2543fSMatthew G Knepley 
793ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
794ccd2543fSMatthew G Knepley   */
7958b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
79673868372SMatthew G. Knepley   /* Check for n = z */
79773868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
7987df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
7997df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
80099dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
80199dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
8027df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
80373868372SMatthew G. Knepley     }
80499dec3a6SMatthew G. Knepley     coords[0] = 0.0;
80599dec3a6SMatthew G. Knepley     coords[1] = 0.0;
8067df32b8bSSanderA     coords[2] = x1[0];
8077df32b8bSSanderA     coords[3] = x1[1] * s;
8087df32b8bSSanderA     coords[4] = x2[0];
8097df32b8bSSanderA     coords[5] = x2[1] * s;
8107df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
8117df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
8127df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
81373868372SMatthew G. Knepley     PetscFunctionReturn(0);
81473868372SMatthew G. Knepley   }
815da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
816ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
817ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
818ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
819ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
820ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
821ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
822ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
823ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
824ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
825ccd2543fSMatthew G Knepley     }
826ccd2543fSMatthew G Knepley   }
8278763be8eSMatthew G. Knepley   if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
8288763be8eSMatthew G. Knepley   if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
829ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
83099dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
83199dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
83299dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
83399dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
83499dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
83599dec3a6SMatthew G. Knepley       }
83699dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
83799dec3a6SMatthew G. Knepley     }
83899dec3a6SMatthew G. Knepley   }
839ccd2543fSMatthew G Knepley   coords[0] = 0.0;
840ccd2543fSMatthew G Knepley   coords[1] = 0.0;
841ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
842ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
843ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
844ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
8457f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
8467f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
8477f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
8487f07f362SMatthew G. Knepley       PetscReal tmp;
8497f07f362SMatthew G. Knepley 
8507f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
8517f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
8527f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
8537f07f362SMatthew G. Knepley     }
8547f07f362SMatthew G. Knepley   }
855ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
856ccd2543fSMatthew G Knepley }
857ccd2543fSMatthew G Knepley 
858ccd2543fSMatthew G Knepley #undef __FUNCT__
859834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal"
8606322fe33SJed Brown PETSC_UNUSED
861834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
862834e62ceSMatthew G. Knepley {
863834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
864834e62ceSMatthew G. Knepley 
865834e62ceSMatthew G. Knepley    |  1  1  1 |
866834e62ceSMatthew G. Knepley    | x0 x1 x2 |
867834e62ceSMatthew G. Knepley    | y0 y1 y2 |
868834e62ceSMatthew G. Knepley 
869834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
870834e62ceSMatthew G. Knepley 
871834e62ceSMatthew G. Knepley    | x1 x2 |
872834e62ceSMatthew G. Knepley    | y1 y2 |
873834e62ceSMatthew G. Knepley   */
874834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
875834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
876834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
877834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
87886623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
879923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
880834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
8813bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
882834e62ceSMatthew G. Knepley }
883834e62ceSMatthew G. Knepley 
884834e62ceSMatthew G. Knepley #undef __FUNCT__
885834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal"
886834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
887834e62ceSMatthew G. Knepley {
888923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
889834e62ceSMatthew G. Knepley   *vol *= 0.5;
890834e62ceSMatthew G. Knepley }
891834e62ceSMatthew G. Knepley 
892834e62ceSMatthew G. Knepley #undef __FUNCT__
893834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal"
8946322fe33SJed Brown PETSC_UNUSED
895834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
896834e62ceSMatthew G. Knepley {
897834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
898834e62ceSMatthew G. Knepley 
899834e62ceSMatthew G. Knepley    |  1  1  1  1 |
900834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
901834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
902834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
903834e62ceSMatthew G. Knepley 
904834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
905834e62ceSMatthew G. Knepley 
906834e62ceSMatthew G. Knepley    | x1 x2 x3 |
907834e62ceSMatthew G. Knepley    | y1 y2 y3 |
908834e62ceSMatthew G. Knepley    | z1 z2 z3 |
909834e62ceSMatthew G. Knepley   */
910834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
911834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
912834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
913834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
914834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
915834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
916834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
917923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
918b7ad821dSMatthew G. Knepley   *vol = -0.16666666666666666666666*detM;
9193bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
920834e62ceSMatthew G. Knepley }
921834e62ceSMatthew G. Knepley 
922834e62ceSMatthew G. Knepley #undef __FUNCT__
9230ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal"
9240ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
9250ec8681fSMatthew G. Knepley {
926923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
927b7ad821dSMatthew G. Knepley   *vol *= -0.16666666666666666666666;
9280ec8681fSMatthew G. Knepley }
9290ec8681fSMatthew G. Knepley 
9300ec8681fSMatthew G. Knepley #undef __FUNCT__
93117fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal"
93217fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
93317fe8556SMatthew G. Knepley {
93417fe8556SMatthew G. Knepley   PetscSection   coordSection;
93517fe8556SMatthew G. Knepley   Vec            coordinates;
936a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9378bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
93817fe8556SMatthew G. Knepley   PetscErrorCode ierr;
93917fe8556SMatthew G. Knepley 
94017fe8556SMatthew G. Knepley   PetscFunctionBegin;
94117fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
94269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
9438bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
9448bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
94517fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9468bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
947adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
9487f07f362SMatthew G. Knepley   *detJ = 0.0;
94928dbe442SToby Isaac   if (numCoords == 6) {
95028dbe442SToby Isaac     const PetscInt dim = 3;
95128dbe442SToby Isaac     PetscReal      R[9], J0;
95228dbe442SToby Isaac 
95328dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
954741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
95528dbe442SToby Isaac     if (J)    {
95628dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
95728dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
95828dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
95928dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
96028dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
96128dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
962adac9986SMatthew G. Knepley     }
96328dbe442SToby Isaac   } else if (numCoords == 4) {
9647f07f362SMatthew G. Knepley     const PetscInt dim = 2;
9657f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
9667f07f362SMatthew G. Knepley 
9677f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
968741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
96917fe8556SMatthew G. Knepley     if (J)    {
9707f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
9717f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
9727f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
973923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
974923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
975adac9986SMatthew G. Knepley     }
9767f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
9777f07f362SMatthew G. Knepley     const PetscInt dim = 1;
9787f07f362SMatthew G. Knepley 
9797f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
9807f07f362SMatthew G. Knepley     if (J)    {
9817f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
98217fe8556SMatthew G. Knepley       *detJ = J[0];
9833bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
9843bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
985adac9986SMatthew G. Knepley     }
986796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
98717fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
98817fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
98917fe8556SMatthew G. Knepley }
99017fe8556SMatthew G. Knepley 
99117fe8556SMatthew G. Knepley #undef __FUNCT__
992ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal"
993ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
994ccd2543fSMatthew G Knepley {
995ccd2543fSMatthew G Knepley   PetscSection   coordSection;
996ccd2543fSMatthew G Knepley   Vec            coordinates;
997a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9987f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
999ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1000ccd2543fSMatthew G Knepley 
1001ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1002ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
100369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1004ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
10057f07f362SMatthew G. Knepley   *detJ = 0.0;
1006ccd2543fSMatthew G Knepley   if (numCoords == 9) {
10077f07f362SMatthew G. Knepley     const PetscInt dim = 3;
10087f07f362SMatthew 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};
10097f07f362SMatthew G. Knepley 
10107f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1011741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
10127f07f362SMatthew G. Knepley     if (J)    {
1013b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1014b7ad821dSMatthew G. Knepley 
1015b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1016b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1017b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1018ccd2543fSMatthew G Knepley         }
10197f07f362SMatthew G. Knepley       }
10203bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1021923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
10227f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
10237f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
10247f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
10257f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
10267f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
10277f07f362SMatthew G. Knepley           }
10287f07f362SMatthew G. Knepley         }
10297f07f362SMatthew G. Knepley       }
10303bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
10317f07f362SMatthew G. Knepley     }
1032923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
10337f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
10347f07f362SMatthew G. Knepley     const PetscInt dim = 2;
10357f07f362SMatthew G. Knepley 
10367f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1037ccd2543fSMatthew G Knepley     if (J)    {
1038ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1039ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1040ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1041ccd2543fSMatthew G Knepley         }
1042ccd2543fSMatthew G Knepley       }
10433bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1044923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1045ccd2543fSMatthew G Knepley     }
1046923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1047796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1048ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1049ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1050ccd2543fSMatthew G Knepley }
1051ccd2543fSMatthew G Knepley 
1052ccd2543fSMatthew G Knepley #undef __FUNCT__
1053ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal"
1054ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1055ccd2543fSMatthew G Knepley {
1056ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1057ccd2543fSMatthew G Knepley   Vec            coordinates;
1058a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10590d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1060ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1061ccd2543fSMatthew G Knepley 
1062ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1063ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
106469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10650d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10660d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
106799dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
106871f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
10697f07f362SMatthew G. Knepley   *detJ = 0.0;
107099dec3a6SMatthew G. Knepley   if (numCoords == 12) {
107199dec3a6SMatthew G. Knepley     const PetscInt dim = 3;
107299dec3a6SMatthew 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};
107399dec3a6SMatthew G. Knepley 
107499dec3a6SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1075741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
107699dec3a6SMatthew G. Knepley     if (J)    {
107799dec3a6SMatthew G. Knepley       const PetscInt pdim = 2;
107899dec3a6SMatthew G. Knepley 
107999dec3a6SMatthew G. Knepley       for (d = 0; d < pdim; d++) {
108099dec3a6SMatthew G. Knepley         J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
108199dec3a6SMatthew G. Knepley         J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
108299dec3a6SMatthew G. Knepley       }
10833bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1084923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
108599dec3a6SMatthew G. Knepley       for (d = 0; d < dim; d++) {
108699dec3a6SMatthew G. Knepley         for (f = 0; f < dim; f++) {
108799dec3a6SMatthew G. Knepley           J[d*dim+f] = 0.0;
108899dec3a6SMatthew G. Knepley           for (g = 0; g < dim; g++) {
108999dec3a6SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
109099dec3a6SMatthew G. Knepley           }
109199dec3a6SMatthew G. Knepley         }
109299dec3a6SMatthew G. Knepley       }
10933bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
109499dec3a6SMatthew G. Knepley     }
1095923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
109671f58de1SToby Isaac   } else if (numCoords == 8) {
109799dec3a6SMatthew G. Knepley     const PetscInt dim = 2;
109899dec3a6SMatthew G. Knepley 
10997f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1100ccd2543fSMatthew G Knepley     if (J)    {
1101ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
110299dec3a6SMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
110399dec3a6SMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1104ccd2543fSMatthew G Knepley       }
11053bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1106923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1107ccd2543fSMatthew G Knepley     }
1108923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1109796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
111099dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1111ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1112ccd2543fSMatthew G Knepley }
1113ccd2543fSMatthew G Knepley 
1114ccd2543fSMatthew G Knepley #undef __FUNCT__
1115ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal"
1116ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1117ccd2543fSMatthew G Knepley {
1118ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1119ccd2543fSMatthew G Knepley   Vec            coordinates;
1120a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1121ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
112299dec3a6SMatthew G. Knepley   PetscInt       d;
1123ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1124ccd2543fSMatthew G Knepley 
1125ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1126ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
112769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1128ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11297f07f362SMatthew G. Knepley   *detJ = 0.0;
11307f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1131ccd2543fSMatthew G Knepley   if (J)    {
1132ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1133f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1134f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1135f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1136f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1137ccd2543fSMatthew G Knepley     }
11383bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1139923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1140ccd2543fSMatthew G Knepley   }
1141923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1142ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1143ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1144ccd2543fSMatthew G Knepley }
1145ccd2543fSMatthew G Knepley 
1146ccd2543fSMatthew G Knepley #undef __FUNCT__
1147ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal"
1148ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1149ccd2543fSMatthew G Knepley {
1150ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1151ccd2543fSMatthew G Knepley   Vec            coordinates;
1152a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1153ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1154ccd2543fSMatthew G Knepley   PetscInt       d;
1155ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1156ccd2543fSMatthew G Knepley 
1157ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1158ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
115969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1160ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11617f07f362SMatthew G. Knepley   *detJ = 0.0;
11627f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1163ccd2543fSMatthew G Knepley   if (J)    {
1164ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1165f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1166f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1167f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1168ccd2543fSMatthew G Knepley     }
11693bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1170923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1171ccd2543fSMatthew G Knepley   }
1172923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1173ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1174ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1175ccd2543fSMatthew G Knepley }
1176ccd2543fSMatthew G Knepley 
1177ccd2543fSMatthew G Knepley #undef __FUNCT__
11788e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM"
1179ccd2543fSMatthew G Knepley /*@C
11808e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1181ccd2543fSMatthew G Knepley 
1182ccd2543fSMatthew G Knepley   Collective on DM
1183ccd2543fSMatthew G Knepley 
1184ccd2543fSMatthew G Knepley   Input Arguments:
1185ccd2543fSMatthew G Knepley + dm   - the DM
1186ccd2543fSMatthew G Knepley - cell - the cell
1187ccd2543fSMatthew G Knepley 
1188ccd2543fSMatthew G Knepley   Output Arguments:
1189ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1190ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1191ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1192ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1193ccd2543fSMatthew G Knepley 
1194ccd2543fSMatthew G Knepley   Level: advanced
1195ccd2543fSMatthew G Knepley 
1196ccd2543fSMatthew G Knepley   Fortran Notes:
1197ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1198ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1199ccd2543fSMatthew G Knepley 
12008e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
1201ccd2543fSMatthew G Knepley @*/
12028e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1203ccd2543fSMatthew G Knepley {
120449dc4407SMatthew G. Knepley   PetscInt       depth, dim, coneSize;
1205ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1206ccd2543fSMatthew G Knepley 
1207ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1208139a35ccSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1209ccd2543fSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
121049dc4407SMatthew G. Knepley   if (depth == 1) {
12118e0841e0SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12128e0841e0SMatthew G. Knepley   } else {
12138e0841e0SMatthew G. Knepley     DMLabel depth;
12148e0841e0SMatthew G. Knepley 
12158e0841e0SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
12168e0841e0SMatthew G. Knepley     ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr);
12178e0841e0SMatthew G. Knepley   }
1218ccd2543fSMatthew G Knepley   switch (dim) {
121917fe8556SMatthew G. Knepley   case 1:
122017fe8556SMatthew G. Knepley     ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
122117fe8556SMatthew G. Knepley     break;
1222ccd2543fSMatthew G Knepley   case 2:
1223ccd2543fSMatthew G Knepley     switch (coneSize) {
1224ccd2543fSMatthew G Knepley     case 3:
1225ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1226ccd2543fSMatthew G Knepley       break;
1227ccd2543fSMatthew G Knepley     case 4:
1228ccd2543fSMatthew G Knepley       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1229ccd2543fSMatthew G Knepley       break;
1230ccd2543fSMatthew G Knepley     default:
12318e0841e0SMatthew G. Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1232ccd2543fSMatthew G Knepley     }
1233ccd2543fSMatthew G Knepley     break;
1234ccd2543fSMatthew G Knepley   case 3:
1235ccd2543fSMatthew G Knepley     switch (coneSize) {
1236ccd2543fSMatthew G Knepley     case 4:
1237ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1238ccd2543fSMatthew G Knepley       break;
12398e0841e0SMatthew G. Knepley     case 6: /* Faces */
12408e0841e0SMatthew G. Knepley     case 8: /* Vertices */
1241ccd2543fSMatthew G Knepley       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1242ccd2543fSMatthew G Knepley       break;
1243ccd2543fSMatthew G Knepley     default:
12448e0841e0SMatthew G. Knepley         SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1245ccd2543fSMatthew G Knepley     }
1246ccd2543fSMatthew G Knepley       break;
1247ccd2543fSMatthew G Knepley   default:
1248ccd2543fSMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1249ccd2543fSMatthew G Knepley   }
12508e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
12518e0841e0SMatthew G. Knepley }
12528e0841e0SMatthew G. Knepley 
12538e0841e0SMatthew G. Knepley #undef __FUNCT__
12548e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal"
12558e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
12568e0841e0SMatthew G. Knepley {
12578e0841e0SMatthew G. Knepley   PetscQuadrature  quad;
12588e0841e0SMatthew G. Knepley   PetscSection     coordSection;
12598e0841e0SMatthew G. Knepley   Vec              coordinates;
12608e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
12618e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
12628e0841e0SMatthew G. Knepley   PetscReal       *basisDer;
12638e0841e0SMatthew G. Knepley   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, d, q;
12648e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
12658e0841e0SMatthew G. Knepley 
12668e0841e0SMatthew G. Knepley   PetscFunctionBegin;
12678e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
12688e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
12698e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
12708e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12718e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
12728e0841e0SMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1273954b1791SMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
12748e0841e0SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
12758e0841e0SMatthew G. Knepley   ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
12768e0841e0SMatthew G. Knepley   *detJ = 0.0;
12778e0841e0SMatthew G. Knepley   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
12788e0841e0SMatthew 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);
12798e0841e0SMatthew G. Knepley   if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);}
12808e0841e0SMatthew G. Knepley   if (J) {
12810790e268SMatthew G. Knepley     ierr = PetscMemzero(J, Nq*cdim*dim*sizeof(PetscReal));CHKERRQ(ierr);
12828e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
12838e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
12848e0841e0SMatthew G. Knepley 
12858e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
12868e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
12878e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
12888e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
128971d6e60fSMatthew G. Knepley             J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
12903bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
12918e0841e0SMatthew G. Knepley       if (cdim > dim) {
12928e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
12938e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
12948e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
12958e0841e0SMatthew G. Knepley       }
12968e0841e0SMatthew G. Knepley       switch (cdim) {
12978e0841e0SMatthew G. Knepley       case 3:
12988e0841e0SMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J);
12998e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
130017fe8556SMatthew G. Knepley         break;
130149dc4407SMatthew G. Knepley       case 2:
13028e0841e0SMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
13038e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
130449dc4407SMatthew G. Knepley         break;
13058e0841e0SMatthew G. Knepley       case 1:
13068e0841e0SMatthew G. Knepley         *detJ = J[0];
13078e0841e0SMatthew G. Knepley         if (invJ) invJ[0] = 1.0/J[0];
130849dc4407SMatthew G. Knepley       }
130949dc4407SMatthew G. Knepley     }
13108e0841e0SMatthew G. Knepley   }
13118e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
13128e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
13138e0841e0SMatthew G. Knepley }
13148e0841e0SMatthew G. Knepley 
13158e0841e0SMatthew G. Knepley #undef __FUNCT__
13168e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
13178e0841e0SMatthew G. Knepley /*@C
13188e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
13198e0841e0SMatthew G. Knepley 
13208e0841e0SMatthew G. Knepley   Collective on DM
13218e0841e0SMatthew G. Knepley 
13228e0841e0SMatthew G. Knepley   Input Arguments:
13238e0841e0SMatthew G. Knepley + dm   - the DM
13248e0841e0SMatthew G. Knepley . cell - the cell
13258e0841e0SMatthew G. Knepley - fe   - the finite element containing the quadrature
13268e0841e0SMatthew G. Knepley 
13278e0841e0SMatthew G. Knepley   Output Arguments:
13288e0841e0SMatthew G. Knepley + v0   - the translation part of this transform
13298e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
13308e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
13318e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
13328e0841e0SMatthew G. Knepley 
13338e0841e0SMatthew G. Knepley   Level: advanced
13348e0841e0SMatthew G. Knepley 
13358e0841e0SMatthew G. Knepley   Fortran Notes:
13368e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
13378e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
13388e0841e0SMatthew G. Knepley 
13398e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
13408e0841e0SMatthew G. Knepley @*/
13418e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
13428e0841e0SMatthew G. Knepley {
13438e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
13448e0841e0SMatthew G. Knepley 
13458e0841e0SMatthew G. Knepley   PetscFunctionBegin;
13468e0841e0SMatthew G. Knepley   if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
13478e0841e0SMatthew G. Knepley   else     {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
1348ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1349ccd2543fSMatthew G Knepley }
1350834e62ceSMatthew G. Knepley 
1351834e62ceSMatthew G. Knepley #undef __FUNCT__
1352cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
1353011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1354cc08537eSMatthew G. Knepley {
1355cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1356cc08537eSMatthew G. Knepley   Vec            coordinates;
1357a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
135806e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1359cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1360cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1361cc08537eSMatthew G. Knepley 
1362cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1363cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
136469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1365cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1366011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
13672e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1368cc08537eSMatthew G. Knepley   if (centroid) {
136906e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
137006e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1371cc08537eSMatthew G. Knepley   }
1372cc08537eSMatthew G. Knepley   if (normal) {
1373a60a936bSMatthew G. Knepley     PetscReal norm;
1374a60a936bSMatthew G. Knepley 
137506e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
137606e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1377a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1378a60a936bSMatthew G. Knepley     normal[0] /= norm;
1379a60a936bSMatthew G. Knepley     normal[1] /= norm;
1380cc08537eSMatthew G. Knepley   }
1381cc08537eSMatthew G. Knepley   if (vol) {
138206e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1383cc08537eSMatthew G. Knepley   }
1384cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1385cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1386cc08537eSMatthew G. Knepley }
1387cc08537eSMatthew G. Knepley 
1388cc08537eSMatthew G. Knepley #undef __FUNCT__
1389cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1390cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1391011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1392cc08537eSMatthew G. Knepley {
1393cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1394cc08537eSMatthew G. Knepley   Vec            coordinates;
1395cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
13960a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
13970a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1398cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1399cc08537eSMatthew G. Knepley 
1400cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1401cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
14020a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
140369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1404cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
14050bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1406ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1407ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1408ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1409ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1410ceee4971SMatthew G. Knepley   }
1411011ea5d8SMatthew G. Knepley   if (normal) {
1412011ea5d8SMatthew G. Knepley     if (dim > 2) {
14131ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
14141ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
14151ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
14160a1d6728SMatthew G. Knepley       PetscReal       norm;
14170a1d6728SMatthew G. Knepley 
14180a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
14190a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
14200a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
14218b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
14220a1d6728SMatthew G. Knepley       normal[0] /= norm;
14230a1d6728SMatthew G. Knepley       normal[1] /= norm;
14240a1d6728SMatthew G. Knepley       normal[2] /= norm;
1425011ea5d8SMatthew G. Knepley     } else {
1426011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1427011ea5d8SMatthew G. Knepley     }
1428011ea5d8SMatthew G. Knepley   }
1429741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
14300a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
14310a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
14320a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14331ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
14341ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
14350a1d6728SMatthew G. Knepley     }
14360a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
14370a1d6728SMatthew G. Knepley     vsum += vtmp;
14380a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14390a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
14400a1d6728SMatthew G. Knepley     }
14410a1d6728SMatthew G. Knepley   }
14420a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
14430a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
14440a1d6728SMatthew G. Knepley   }
14450a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1446ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
14470a1d6728SMatthew G. Knepley   if (centroid) {
14480a1d6728SMatthew G. Knepley     if (dim > 2) {
14490a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14500a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
14510a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
14520a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
14530a1d6728SMatthew G. Knepley         }
14540a1d6728SMatthew G. Knepley       }
14550a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
14560a1d6728SMatthew G. Knepley   }
1457cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1458cc08537eSMatthew G. Knepley }
1459cc08537eSMatthew G. Knepley 
1460cc08537eSMatthew G. Knepley #undef __FUNCT__
14610ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
14620ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1463011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
14640ec8681fSMatthew G. Knepley {
14650ec8681fSMatthew G. Knepley   PetscSection    coordSection;
14660ec8681fSMatthew G. Knepley   Vec             coordinates;
14670ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
146886623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1469a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
14700ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
14710ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
14720ec8681fSMatthew G. Knepley 
14730ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1474f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
14750ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
147669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
14770ec8681fSMatthew G. Knepley 
1478d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
14790ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
14800ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1481a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
14820ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1483011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
14840ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
14850ec8681fSMatthew G. Knepley     switch (numCorners) {
14860ec8681fSMatthew G. Knepley     case 3:
14870ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14881ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
14891ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
14901ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
14910ec8681fSMatthew G. Knepley       }
14920ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1493a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
14940ec8681fSMatthew G. Knepley       vsum += vtmp;
14954f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
14960ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
14971ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
14980ec8681fSMatthew G. Knepley         }
14990ec8681fSMatthew G. Knepley       }
15000ec8681fSMatthew G. Knepley       break;
15010ec8681fSMatthew G. Knepley     case 4:
15020ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
15030ec8681fSMatthew G. Knepley       /* First tet */
15040ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15051ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
15061ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
15071ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
15080ec8681fSMatthew G. Knepley       }
15090ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1510a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15110ec8681fSMatthew G. Knepley       vsum += vtmp;
15120ec8681fSMatthew G. Knepley       if (centroid) {
15130ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15140ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15150ec8681fSMatthew G. Knepley         }
15160ec8681fSMatthew G. Knepley       }
15170ec8681fSMatthew G. Knepley       /* Second tet */
15180ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15191ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
15201ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
15211ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
15220ec8681fSMatthew G. Knepley       }
15230ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1524a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15250ec8681fSMatthew G. Knepley       vsum += vtmp;
15260ec8681fSMatthew G. Knepley       if (centroid) {
15270ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15280ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15290ec8681fSMatthew G. Knepley         }
15300ec8681fSMatthew G. Knepley       }
15310ec8681fSMatthew G. Knepley       break;
15320ec8681fSMatthew G. Knepley     default:
1533796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
15340ec8681fSMatthew G. Knepley     }
15354f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
15360ec8681fSMatthew G. Knepley   }
15378763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
15380ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1539d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
15400ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
15410ec8681fSMatthew G. Knepley }
15420ec8681fSMatthew G. Knepley 
15430ec8681fSMatthew G. Knepley #undef __FUNCT__
1544834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1545834e62ceSMatthew G. Knepley /*@C
1546834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1547834e62ceSMatthew G. Knepley 
1548834e62ceSMatthew G. Knepley   Collective on DM
1549834e62ceSMatthew G. Knepley 
1550834e62ceSMatthew G. Knepley   Input Arguments:
1551834e62ceSMatthew G. Knepley + dm   - the DM
1552834e62ceSMatthew G. Knepley - cell - the cell
1553834e62ceSMatthew G. Knepley 
1554834e62ceSMatthew G. Knepley   Output Arguments:
1555834e62ceSMatthew G. Knepley + volume   - the cell volume
1556cc08537eSMatthew G. Knepley . centroid - the cell centroid
1557cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1558834e62ceSMatthew G. Knepley 
1559834e62ceSMatthew G. Knepley   Level: advanced
1560834e62ceSMatthew G. Knepley 
1561834e62ceSMatthew G. Knepley   Fortran Notes:
1562834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1563834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1564834e62ceSMatthew G. Knepley 
156569d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1566834e62ceSMatthew G. Knepley @*/
1567cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1568834e62ceSMatthew G. Knepley {
15690ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1570834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1571834e62ceSMatthew G. Knepley 
1572834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1573834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1574c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1575834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1576834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1577c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1578834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1579011ea5d8SMatthew G. Knepley   switch (depth) {
1580cc08537eSMatthew G. Knepley   case 1:
1581011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1582cc08537eSMatthew G. Knepley     break;
1583834e62ceSMatthew G. Knepley   case 2:
1584011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1585834e62ceSMatthew G. Knepley     break;
1586834e62ceSMatthew G. Knepley   case 3:
1587011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1588834e62ceSMatthew G. Knepley     break;
1589834e62ceSMatthew G. Knepley   default:
1590834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1591834e62ceSMatthew G. Knepley   }
1592834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1593834e62ceSMatthew G. Knepley }
1594113c68e6SMatthew G. Knepley 
1595113c68e6SMatthew G. Knepley #undef __FUNCT__
1596c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1597c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1598c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1599c0d900a5SMatthew G. Knepley {
1600c0d900a5SMatthew G. Knepley   DM             dmCell;
1601c0d900a5SMatthew G. Knepley   Vec            coordinates;
1602c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1603c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1604c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1605c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1606c0d900a5SMatthew G. Knepley 
1607c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1608c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1609c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1610c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1611c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1612c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1613c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1614c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1615c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1616c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1617c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1618c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
16199e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1620c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1621c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1622c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1623c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1624c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1625c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1626c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1627c0d900a5SMatthew G. Knepley 
1628c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1629c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1630c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1631c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1632c0d900a5SMatthew G. Knepley   }
1633c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1634c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1635c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1636c0d900a5SMatthew G. Knepley }
1637c0d900a5SMatthew G. Knepley 
1638c0d900a5SMatthew G. Knepley #undef __FUNCT__
1639113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1640891a9168SMatthew G. Knepley /*@
1641891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1642891a9168SMatthew G. Knepley 
1643891a9168SMatthew G. Knepley   Input Parameter:
1644891a9168SMatthew G. Knepley . dm - The DM
1645891a9168SMatthew G. Knepley 
1646891a9168SMatthew G. Knepley   Output Parameters:
1647891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1648891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1649891a9168SMatthew G. Knepley 
1650891a9168SMatthew G. Knepley   Level: developer
1651891a9168SMatthew G. Knepley 
1652891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1653891a9168SMatthew G. Knepley @*/
1654113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1655113c68e6SMatthew G. Knepley {
1656113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1657113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1658113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1659113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1660113c68e6SMatthew G. Knepley   Vec            coordinates;
1661113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1662113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1663113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1664113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1665113c68e6SMatthew G. Knepley 
1666113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1667113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1668113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1669113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1670113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1671113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1672113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1673113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1674113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1675113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1676113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1677113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
16789e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1679113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1680113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1681113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1682113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
168306348e87SToby Isaac   if (cEndInterior < 0) {
168406348e87SToby Isaac     cEndInterior = cEnd;
168506348e87SToby Isaac   }
1686113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1687113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1688113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1689113c68e6SMatthew G. Knepley 
1690113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1691113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1692113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1693113c68e6SMatthew G. Knepley   }
1694113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1695113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1696113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1697113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1698113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
16999e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1700113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1701113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1702113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1703113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1704113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1705c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1706113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1707113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1708113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1709113c68e6SMatthew G. Knepley     PetscReal        area;
171050d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
1711113c68e6SMatthew G. Knepley 
17129ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
171350d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
171450d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
1715113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1716113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1717113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1718113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1719113c68e6SMatthew G. Knepley     {
1720113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
172106348e87SToby Isaac       PetscInt         ncells;
1722113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1723113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
17240453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1725113c68e6SMatthew G. Knepley 
1726113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
172706348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
1728113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1729113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
173006348e87SToby Isaac       if (ncells > 1) {
173106348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1732113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
173306348e87SToby Isaac       }
173406348e87SToby Isaac       else {
173506348e87SToby Isaac         rcentroid = fg->centroid;
173606348e87SToby Isaac       }
17372e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
17382e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
17390453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1740113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1741113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1742113c68e6SMatthew G. Knepley       }
1743113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1744113c68e6SMatthew 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]);
1745113c68e6SMatthew 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]);
1746113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1747113c68e6SMatthew G. Knepley       }
1748113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1749113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1750113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1751113c68e6SMatthew G. Knepley       }
175206348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
1753113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
1754113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1755113c68e6SMatthew G. Knepley       }
1756113c68e6SMatthew G. Knepley     }
1757113c68e6SMatthew G. Knepley   }
1758b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1759113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
1760113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
1761113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
1762113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1763113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
1764113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
1765113c68e6SMatthew G. Knepley 
1766113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
1767113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
1768113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
1769113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
177050d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
1771113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
1772113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
1773113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
1774113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
1775113c68e6SMatthew G. Knepley       if (support[s] == c) {
1776640bce14SSatish Balay         PetscFVCellGeom       *ci;
1777113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
1778113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
1779113c68e6SMatthew G. Knepley 
1780113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
1781113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
1782113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
1783113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
1784113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
1785113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
1786113c68e6SMatthew G. Knepley       }
1787113c68e6SMatthew G. Knepley     }
1788113c68e6SMatthew G. Knepley   }
1789113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
1790113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1791113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1792113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
1793113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1794113c68e6SMatthew G. Knepley }
1795113c68e6SMatthew G. Knepley 
1796113c68e6SMatthew G. Knepley #undef __FUNCT__
1797113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
1798113c68e6SMatthew G. Knepley /*@C
1799113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
1800113c68e6SMatthew G. Knepley 
1801113c68e6SMatthew G. Knepley   Not collective
1802113c68e6SMatthew G. Knepley 
1803113c68e6SMatthew G. Knepley   Input Argument:
1804113c68e6SMatthew G. Knepley . dm - the DM
1805113c68e6SMatthew G. Knepley 
1806113c68e6SMatthew G. Knepley   Output Argument:
1807113c68e6SMatthew G. Knepley . minradius - the minium cell radius
1808113c68e6SMatthew G. Knepley 
1809113c68e6SMatthew G. Knepley   Level: developer
1810113c68e6SMatthew G. Knepley 
1811113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
1812113c68e6SMatthew G. Knepley @*/
1813113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
1814113c68e6SMatthew G. Knepley {
1815113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1816113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1817113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
1818113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
1819113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1820113c68e6SMatthew G. Knepley }
1821113c68e6SMatthew G. Knepley 
1822113c68e6SMatthew G. Knepley #undef __FUNCT__
1823113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
1824113c68e6SMatthew G. Knepley /*@C
1825113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
1826113c68e6SMatthew G. Knepley 
1827113c68e6SMatthew G. Knepley   Logically collective
1828113c68e6SMatthew G. Knepley 
1829113c68e6SMatthew G. Knepley   Input Arguments:
1830113c68e6SMatthew G. Knepley + dm - the DM
1831113c68e6SMatthew G. Knepley - minradius - the minium cell radius
1832113c68e6SMatthew G. Knepley 
1833113c68e6SMatthew G. Knepley   Level: developer
1834113c68e6SMatthew G. Knepley 
1835113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
1836113c68e6SMatthew G. Knepley @*/
1837113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
1838113c68e6SMatthew G. Knepley {
1839113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1840113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1841113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
1842113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1843113c68e6SMatthew G. Knepley }
1844856ac710SMatthew G. Knepley 
1845856ac710SMatthew G. Knepley #undef __FUNCT__
1846856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
1847856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1848856ac710SMatthew G. Knepley {
1849856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
1850856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
1851856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
1852856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1853856ac710SMatthew G. Knepley 
1854856ac710SMatthew G. Knepley   PetscFunctionBegin;
1855856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1856856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1857856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1858856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
1859856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1860c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1861856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1862856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
1863856ac710SMatthew G. Knepley     const PetscInt        *faces;
1864856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
1865640bce14SSatish Balay     PetscFVCellGeom        *cg;
1866856ac710SMatthew G. Knepley     PetscBool              boundary;
1867856ac710SMatthew G. Knepley     PetscInt               ghost;
1868856ac710SMatthew G. Knepley 
1869856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1870856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
1871856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
1872856ac710SMatthew 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);
1873856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1874640bce14SSatish Balay       PetscFVCellGeom       *cg1;
1875856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
1876856ac710SMatthew G. Knepley       const PetscInt        *fcells;
1877856ac710SMatthew G. Knepley       PetscInt               ncell, side;
1878856ac710SMatthew G. Knepley 
1879856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1880a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1881856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1882856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
1883856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
1884856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
1885856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
1886856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1887856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
1888856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
1889856ac710SMatthew G. Knepley     }
1890856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
1891856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
1892856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1893856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1894a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1895856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1896856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
1897856ac710SMatthew G. Knepley       ++usedFaces;
1898856ac710SMatthew G. Knepley     }
1899856ac710SMatthew G. Knepley   }
1900856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
1901856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1902856ac710SMatthew G. Knepley }
1903856ac710SMatthew G. Knepley 
1904856ac710SMatthew G. Knepley #undef __FUNCT__
1905b81db932SToby Isaac #define __FUNCT__ "BuildGradientReconstruction_Internal_Tree"
1906b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1907b81db932SToby Isaac {
1908b81db932SToby Isaac   DMLabel        ghostLabel;
1909b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
1910b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
1911b81db932SToby Isaac   PetscSection   neighSec;
1912b81db932SToby Isaac   PetscInt     (*neighbors)[2];
1913b81db932SToby Isaac   PetscInt      *counter;
1914b81db932SToby Isaac   PetscErrorCode ierr;
1915b81db932SToby Isaac 
1916b81db932SToby Isaac   PetscFunctionBegin;
1917b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1918b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1919b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
19205bc680faSToby Isaac   if (cEndInterior < 0) {
19215bc680faSToby Isaac     cEndInterior = cEnd;
19225bc680faSToby Isaac   }
1923b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
1924b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
1925b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1926c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1927b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1928b81db932SToby Isaac     const PetscInt        *fcells;
1929b81db932SToby Isaac     PetscBool              boundary;
19305bc680faSToby Isaac     PetscInt               ghost = -1;
1931b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1932b81db932SToby Isaac 
193306348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1934a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1935b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1936b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1937b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
193806348e87SToby Isaac     if (numCells == 2) {
1939b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
1940b81db932SToby Isaac       for (c = 0; c < 2; c++) {
1941b81db932SToby Isaac         PetscInt cell = fcells[c];
1942b81db932SToby Isaac 
1943e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
1944b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
1945b81db932SToby Isaac         }
1946b81db932SToby Isaac       }
1947b81db932SToby Isaac     }
194806348e87SToby Isaac   }
1949b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
1950b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
1951b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1952b81db932SToby Isaac   nStart = 0;
1953b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
1954b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
1955b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
1956b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1957b81db932SToby Isaac     const PetscInt        *fcells;
1958b81db932SToby Isaac     PetscBool              boundary;
19595bc680faSToby Isaac     PetscInt               ghost = -1;
1960b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1961b81db932SToby Isaac 
196206348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1963a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1964b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1965b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1966b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
196706348e87SToby Isaac     if (numCells == 2) {
1968b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
1969b81db932SToby Isaac       for (c = 0; c < 2; c++) {
1970b81db932SToby Isaac         PetscInt cell = fcells[c], off;
1971b81db932SToby Isaac 
1972e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
1973b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
1974b81db932SToby Isaac           off += counter[cell - cStart]++;
1975b81db932SToby Isaac           neighbors[off][0] = f;
1976b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
1977b81db932SToby Isaac         }
1978b81db932SToby Isaac       }
1979b81db932SToby Isaac     }
198006348e87SToby Isaac   }
1981b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
1982b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1983b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
1984317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
1985640bce14SSatish Balay     PetscFVCellGeom        *cg;
1986b81db932SToby Isaac 
1987b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1988b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
1989b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
1990317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
1991317218b9SToby 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);
1992b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
1993640bce14SSatish Balay       PetscFVCellGeom       *cg1;
1994b81db932SToby Isaac       PetscFVFaceGeom       *fg;
1995b81db932SToby Isaac       const PetscInt        *fcells;
1996b81db932SToby Isaac       PetscInt               ncell, side, nface;
1997b81db932SToby Isaac 
1998b81db932SToby Isaac       nface = neighbors[off + f][0];
1999b81db932SToby Isaac       ncell = neighbors[off + f][1];
2000b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2001b81db932SToby Isaac       side  = (c != fcells[0]);
2002b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2003b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2004b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2005b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2006b81db932SToby Isaac     }
2007b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2008b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2009b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2010b81db932SToby Isaac     }
2011b81db932SToby Isaac   }
2012b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
20135fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2014b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2015b81db932SToby Isaac   PetscFunctionReturn(0);
2016b81db932SToby Isaac }
2017b81db932SToby Isaac 
2018b81db932SToby Isaac #undef __FUNCT__
2019856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
2020856ac710SMatthew G. Knepley /*@
2021856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2022856ac710SMatthew G. Knepley 
2023856ac710SMatthew G. Knepley   Collective on DM
2024856ac710SMatthew G. Knepley 
2025856ac710SMatthew G. Knepley   Input Arguments:
2026856ac710SMatthew G. Knepley + dm  - The DM
2027856ac710SMatthew G. Knepley . fvm - The PetscFV
2028856ac710SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexGetFaceGeometryFVM()
2029856ac710SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexGetCellGeometryFVM()
2030856ac710SMatthew G. Knepley 
2031856ac710SMatthew G. Knepley   Output Parameters:
2032856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2033856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2034856ac710SMatthew G. Knepley 
2035856ac710SMatthew G. Knepley   Level: developer
2036856ac710SMatthew G. Knepley 
2037856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2038856ac710SMatthew G. Knepley @*/
2039856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2040856ac710SMatthew G. Knepley {
2041856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2042856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2043b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2044856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2045856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2046856ac710SMatthew G. Knepley 
2047856ac710SMatthew G. Knepley   PetscFunctionBegin;
2048856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2049856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2050856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2051856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2052856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2053856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2054856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2055856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2056856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2057b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2058b81db932SToby Isaac   if (!parentSection) {
2059856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2060b5a3613cSMatthew G. Knepley   } else {
2061b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2062b81db932SToby Isaac   }
2063856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2064856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2065856ac710SMatthew G. Knepley   /* Create storage for gradients */
2066856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2067856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2068856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2069856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2070856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2071856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2072856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2073856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2074856ac710SMatthew G. Knepley }
2075b27d5b9eSToby Isaac 
2076b27d5b9eSToby Isaac #undef __FUNCT__
2077b27d5b9eSToby Isaac #define __FUNCT__ "DMPlexGetDataFVM"
2078b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2079b27d5b9eSToby Isaac {
2080b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2081b27d5b9eSToby Isaac   PetscErrorCode ierr;
2082b27d5b9eSToby Isaac 
2083b27d5b9eSToby Isaac   PetscFunctionBegin;
2084b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2085b27d5b9eSToby Isaac   if (!cellgeomobj) {
2086b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2087b27d5b9eSToby Isaac 
2088b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2089b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2090b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2091b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2092b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2093b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2094b27d5b9eSToby Isaac   }
2095b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2096b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2097b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2098b27d5b9eSToby Isaac   if (gradDM) {
2099b27d5b9eSToby Isaac     PetscObject gradobj;
2100b27d5b9eSToby Isaac     PetscBool   computeGradients;
2101b27d5b9eSToby Isaac 
2102b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2103b27d5b9eSToby Isaac     if (!computeGradients) {
2104b27d5b9eSToby Isaac       *gradDM = NULL;
2105b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2106b27d5b9eSToby Isaac     }
2107b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2108b27d5b9eSToby Isaac     if (!gradobj) {
2109b27d5b9eSToby Isaac       DM dmGradInt;
2110b27d5b9eSToby Isaac 
2111b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2112b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2113b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2114b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2115b27d5b9eSToby Isaac     }
2116b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2117b27d5b9eSToby Isaac   }
2118b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2119b27d5b9eSToby Isaac }
2120