xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision f960e424b81a78d32f06045c8dd8c9a8a4e02a33)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2ccd2543fSMatthew G Knepley 
3ccd2543fSMatthew G Knepley #undef __FUNCT__
4fea14342SMatthew G. Knepley #define __FUNCT__ "DMPlexGetLineIntersection_2D_Internal"
5fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
6fea14342SMatthew G. Knepley {
7fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*2+0];
8fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*2+1];
9fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*2+0];
10fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*2+1];
11fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0*2+0];
12fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0*2+1];
13fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1*2+0];
14fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1*2+1];
15fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
16fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
17fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
18fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
19fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
20fea14342SMatthew G. Knepley 
21fea14342SMatthew G. Knepley   PetscFunctionBegin;
22fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
23fea14342SMatthew G. Knepley   /* Non-parallel lines */
24fea14342SMatthew G. Knepley   if (denom != 0.0) {
25fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
26fea14342SMatthew G. Knepley     const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
27fea14342SMatthew G. Knepley 
28fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
29fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
30fea14342SMatthew G. Knepley       if (intersection) {
31fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
32fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
33fea14342SMatthew G. Knepley       }
34fea14342SMatthew G. Knepley     }
35fea14342SMatthew G. Knepley   }
36fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
37fea14342SMatthew G. Knepley }
38fea14342SMatthew G. Knepley 
39fea14342SMatthew G. Knepley #undef __FUNCT__
40ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal"
41ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
42ccd2543fSMatthew G Knepley {
43ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
44f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
45ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
46ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
47ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
48ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
49ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
50ccd2543fSMatthew G Knepley 
51ccd2543fSMatthew G Knepley   PetscFunctionBegin;
528e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
53ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
54ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
55ccd2543fSMatthew G Knepley 
56f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
57c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
58ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
59ccd2543fSMatthew G Knepley }
60ccd2543fSMatthew G Knepley 
61ccd2543fSMatthew G Knepley #undef __FUNCT__
6262a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Simplex_2D_Internal"
6362a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
6462a38674SMatthew G. Knepley {
6562a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
6662a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
6762a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
6862a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
6962a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
7062a38674SMatthew G. Knepley   PetscErrorCode  ierr;
7162a38674SMatthew G. Knepley 
7262a38674SMatthew G. Knepley   PetscFunctionBegin;
7362a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
7462a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
7562a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
7662a38674SMatthew G. Knepley 
7762a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
7862a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
7962a38674SMatthew G. Knepley   r   = (xi + eta)/2.0;
8062a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
8162a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
8262a38674SMatthew G. Knepley     xi  /= r;
8362a38674SMatthew G. Knepley     eta /= r;
8462a38674SMatthew G. Knepley   }
8562a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
8662a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
8762a38674SMatthew G. Knepley   PetscFunctionReturn(0);
8862a38674SMatthew G. Knepley }
8962a38674SMatthew G. Knepley 
9062a38674SMatthew G. Knepley #undef __FUNCT__
91ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal"
92ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
93ccd2543fSMatthew G Knepley {
94ccd2543fSMatthew G Knepley   PetscSection       coordSection;
95ccd2543fSMatthew G Knepley   Vec             coordsLocal;
96a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
97ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
98ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
99ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
100ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
101ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
102ccd2543fSMatthew G Knepley 
103ccd2543fSMatthew G Knepley   PetscFunctionBegin;
104ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
10569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
106ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
107ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
108ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
109ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
110ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
111ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
112ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
113ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
114ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
115ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
116ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
117ccd2543fSMatthew G Knepley   }
118ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
119c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
120ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
121ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
122ccd2543fSMatthew G Knepley }
123ccd2543fSMatthew G Knepley 
124ccd2543fSMatthew G Knepley #undef __FUNCT__
125ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal"
126ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
127ccd2543fSMatthew G Knepley {
128ccd2543fSMatthew G Knepley   const PetscInt embedDim = 3;
129ccd2543fSMatthew G Knepley   PetscReal      v0[3], J[9], invJ[9], detJ;
130ccd2543fSMatthew G Knepley   PetscReal      x = PetscRealPart(point[0]);
131ccd2543fSMatthew G Knepley   PetscReal      y = PetscRealPart(point[1]);
132ccd2543fSMatthew G Knepley   PetscReal      z = PetscRealPart(point[2]);
133ccd2543fSMatthew G Knepley   PetscReal      xi, eta, zeta;
134ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
135ccd2543fSMatthew G Knepley 
136ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1378e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
138ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
139ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
140ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
141ccd2543fSMatthew G Knepley 
142ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c;
143c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
144ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
145ccd2543fSMatthew G Knepley }
146ccd2543fSMatthew G Knepley 
147ccd2543fSMatthew G Knepley #undef __FUNCT__
148ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal"
149ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
150ccd2543fSMatthew G Knepley {
151ccd2543fSMatthew G Knepley   PetscSection   coordSection;
152ccd2543fSMatthew G Knepley   Vec            coordsLocal;
1537c1f9639SMatthew G Knepley   PetscScalar   *coords;
154fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
155fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
156ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
157ccd2543fSMatthew G Knepley   PetscInt       f;
158ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
159ccd2543fSMatthew G Knepley 
160ccd2543fSMatthew G Knepley   PetscFunctionBegin;
161ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
16269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
163ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
164ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
165ccd2543fSMatthew G Knepley     /* Check the point is under plane */
166ccd2543fSMatthew G Knepley     /*   Get face normal */
167ccd2543fSMatthew G Knepley     PetscReal v_i[3];
168ccd2543fSMatthew G Knepley     PetscReal v_j[3];
169ccd2543fSMatthew G Knepley     PetscReal normal[3];
170ccd2543fSMatthew G Knepley     PetscReal pp[3];
171ccd2543fSMatthew G Knepley     PetscReal dot;
172ccd2543fSMatthew G Knepley 
173ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
174ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
175ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
176ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
177ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
178ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
179ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
180ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
181ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
182ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
183ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
184ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
185ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
186ccd2543fSMatthew G Knepley 
187ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
188ccd2543fSMatthew G Knepley     if (dot < 0.0) {
189ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
190ccd2543fSMatthew G Knepley       break;
191ccd2543fSMatthew G Knepley     }
192ccd2543fSMatthew G Knepley   }
193ccd2543fSMatthew G Knepley   if (found) *cell = c;
194c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
195ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
196ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
197ccd2543fSMatthew G Knepley }
198ccd2543fSMatthew G Knepley 
199ccd2543fSMatthew G Knepley #undef __FUNCT__
200c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal"
201c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
202c4eade1cSMatthew G. Knepley {
203c4eade1cSMatthew G. Knepley   PetscInt d;
204c4eade1cSMatthew G. Knepley 
205c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
206c4eade1cSMatthew G. Knepley   box->dim = dim;
207c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
208c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
209c4eade1cSMatthew G. Knepley }
210c4eade1cSMatthew G. Knepley 
211c4eade1cSMatthew G. Knepley #undef __FUNCT__
212c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate"
213c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
214c4eade1cSMatthew G. Knepley {
215c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
216c4eade1cSMatthew G. Knepley 
217c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
218c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
219c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
220c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
221c4eade1cSMatthew G. Knepley }
222c4eade1cSMatthew G. Knepley 
223c4eade1cSMatthew G. Knepley #undef __FUNCT__
224c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge"
225c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
226c4eade1cSMatthew G. Knepley {
227c4eade1cSMatthew G. Knepley   PetscInt d;
228c4eade1cSMatthew G. Knepley 
229c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
230c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
231c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
232c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
233c4eade1cSMatthew G. Knepley   }
234c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
235c4eade1cSMatthew G. Knepley }
236c4eade1cSMatthew G. Knepley 
237c4eade1cSMatthew G. Knepley #undef __FUNCT__
238c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid"
23962a38674SMatthew G. Knepley /*
24062a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
24162a38674SMatthew G. Knepley 
24262a38674SMatthew G. Knepley   Not collective
24362a38674SMatthew G. Knepley 
24462a38674SMatthew G. Knepley   Input Parameters:
24562a38674SMatthew G. Knepley + box - The grid hash object
24662a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
24762a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
24862a38674SMatthew G. Knepley 
24962a38674SMatthew G. Knepley   Level: developer
25062a38674SMatthew G. Knepley 
25162a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
25262a38674SMatthew G. Knepley */
253c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
254c4eade1cSMatthew G. Knepley {
255c4eade1cSMatthew G. Knepley   PetscInt d;
256c4eade1cSMatthew G. Knepley 
257c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
258c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
259c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
260c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
261c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
262c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
263c4eade1cSMatthew G. Knepley     } else {
264c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
265c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
266c4eade1cSMatthew G. Knepley     }
267c4eade1cSMatthew G. Knepley   }
268c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
269c4eade1cSMatthew G. Knepley }
270c4eade1cSMatthew G. Knepley 
271c4eade1cSMatthew G. Knepley #undef __FUNCT__
272c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox"
27362a38674SMatthew G. Knepley /*
27462a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
27562a38674SMatthew G. Knepley 
27662a38674SMatthew G. Knepley   Not collective
27762a38674SMatthew G. Knepley 
27862a38674SMatthew G. Knepley   Input Parameters:
27962a38674SMatthew G. Knepley + box       - The grid hash object
28062a38674SMatthew G. Knepley . numPoints - The number of input points
28162a38674SMatthew G. Knepley - points    - The input point coordinates
28262a38674SMatthew G. Knepley 
28362a38674SMatthew G. Knepley   Output Parameters:
28462a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
28562a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
28662a38674SMatthew G. Knepley 
28762a38674SMatthew G. Knepley   Level: developer
28862a38674SMatthew G. Knepley 
28962a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
29062a38674SMatthew G. Knepley */
2911c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
292c4eade1cSMatthew G. Knepley {
293c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
294c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
295c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
296c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
297c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
298c4eade1cSMatthew G. Knepley   PetscInt         d, p;
299c4eade1cSMatthew G. Knepley 
300c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
301c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
302c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
3031c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
304c4eade1cSMatthew G. Knepley 
3051c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
306c4eade1cSMatthew G. Knepley       if (dbox < 0 || dbox >= n[d]) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %d (%g, %g, %g) is outside of our bounding box",
3071c6dfc3eSMatthew G. Knepley                                              p, PetscRealPart(points[p*dim+0]), dim > 1 ? PetscRealPart(points[p*dim+1]) : 0.0, dim > 2 ? PetscRealPart(points[p*dim+2]) : 0.0);
308c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
309c4eade1cSMatthew G. Knepley     }
310c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
311c4eade1cSMatthew G. Knepley   }
312c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
313c4eade1cSMatthew G. Knepley }
314c4eade1cSMatthew G. Knepley 
315c4eade1cSMatthew G. Knepley #undef __FUNCT__
316c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy"
317c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
318c4eade1cSMatthew G. Knepley {
319c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
320c4eade1cSMatthew G. Knepley 
321c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
322c4eade1cSMatthew G. Knepley   if (*box) {
323c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
324c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
325c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
326c4eade1cSMatthew G. Knepley   }
327c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
328c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
329c4eade1cSMatthew G. Knepley }
330c4eade1cSMatthew G. Knepley 
331cafe43deSMatthew G. Knepley #undef __FUNCT__
332cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexLocatePoint_Internal"
333cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
334cafe43deSMatthew G. Knepley {
335cafe43deSMatthew G. Knepley   PetscInt       coneSize;
336cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
337cafe43deSMatthew G. Knepley 
338cafe43deSMatthew G. Knepley   PetscFunctionBegin;
339cafe43deSMatthew G. Knepley   switch (dim) {
340cafe43deSMatthew G. Knepley   case 2:
341cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
342cafe43deSMatthew G. Knepley     switch (coneSize) {
343cafe43deSMatthew G. Knepley     case 3:
344cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
345cafe43deSMatthew G. Knepley       break;
346cafe43deSMatthew G. Knepley     case 4:
347cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
348cafe43deSMatthew G. Knepley       break;
349cafe43deSMatthew G. Knepley     default:
350cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
351cafe43deSMatthew G. Knepley     }
352cafe43deSMatthew G. Knepley     break;
353cafe43deSMatthew G. Knepley   case 3:
354cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
355cafe43deSMatthew G. Knepley     switch (coneSize) {
356cafe43deSMatthew G. Knepley     case 4:
357cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
358cafe43deSMatthew G. Knepley       break;
359cafe43deSMatthew G. Knepley     case 6:
360cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
361cafe43deSMatthew G. Knepley       break;
362cafe43deSMatthew G. Knepley     default:
363cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
364cafe43deSMatthew G. Knepley     }
365cafe43deSMatthew G. Knepley     break;
366cafe43deSMatthew G. Knepley   default:
367cafe43deSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
368cafe43deSMatthew G. Knepley   }
369cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
370cafe43deSMatthew G. Knepley }
371cafe43deSMatthew G. Knepley 
372cafe43deSMatthew G. Knepley #undef __FUNCT__
37362a38674SMatthew G. Knepley #define __FUNCT__ "DMPlexClosestPoint_Internal"
37462a38674SMatthew G. Knepley /*
37562a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
37662a38674SMatthew G. Knepley */
37762a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
37862a38674SMatthew G. Knepley {
37962a38674SMatthew G. Knepley   PetscInt       coneSize;
38062a38674SMatthew G. Knepley   PetscErrorCode ierr;
38162a38674SMatthew G. Knepley 
38262a38674SMatthew G. Knepley   PetscFunctionBegin;
38362a38674SMatthew G. Knepley   switch (dim) {
38462a38674SMatthew G. Knepley   case 2:
38562a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
38662a38674SMatthew G. Knepley     switch (coneSize) {
38762a38674SMatthew G. Knepley     case 3:
38862a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
38962a38674SMatthew G. Knepley       break;
39062a38674SMatthew G. Knepley #if 0
39162a38674SMatthew G. Knepley     case 4:
39262a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
39362a38674SMatthew G. Knepley       break;
39462a38674SMatthew G. Knepley #endif
39562a38674SMatthew G. Knepley     default:
39662a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
39762a38674SMatthew G. Knepley     }
39862a38674SMatthew G. Knepley     break;
39962a38674SMatthew G. Knepley #if 0
40062a38674SMatthew G. Knepley   case 3:
40162a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
40262a38674SMatthew G. Knepley     switch (coneSize) {
40362a38674SMatthew G. Knepley     case 4:
40462a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
40562a38674SMatthew G. Knepley       break;
40662a38674SMatthew G. Knepley     case 6:
40762a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
40862a38674SMatthew G. Knepley       break;
40962a38674SMatthew G. Knepley     default:
41062a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
41162a38674SMatthew G. Knepley     }
41262a38674SMatthew G. Knepley     break;
41362a38674SMatthew G. Knepley #endif
41462a38674SMatthew G. Knepley   default:
41562a38674SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for mesh dimension %D", dim);
41662a38674SMatthew G. Knepley   }
41762a38674SMatthew G. Knepley   PetscFunctionReturn(0);
41862a38674SMatthew G. Knepley }
41962a38674SMatthew G. Knepley 
42062a38674SMatthew G. Knepley #undef __FUNCT__
421cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGridHash_Internal"
42262a38674SMatthew G. Knepley /*
42362a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
42462a38674SMatthew G. Knepley 
42562a38674SMatthew G. Knepley   Collective on DM
42662a38674SMatthew G. Knepley 
42762a38674SMatthew G. Knepley   Input Parameter:
42862a38674SMatthew G. Knepley . dm - The Plex
42962a38674SMatthew G. Knepley 
43062a38674SMatthew G. Knepley   Output Parameter:
43162a38674SMatthew G. Knepley . localBox - The grid hash object
43262a38674SMatthew G. Knepley 
43362a38674SMatthew G. Knepley   Level: developer
43462a38674SMatthew G. Knepley 
43562a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
43662a38674SMatthew G. Knepley */
437cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
438cafe43deSMatthew G. Knepley {
439cafe43deSMatthew G. Knepley   MPI_Comm           comm;
440cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
441cafe43deSMatthew G. Knepley   Vec                coordinates;
442cafe43deSMatthew G. Knepley   PetscSection       coordSection;
443cafe43deSMatthew G. Knepley   Vec                coordsLocal;
444cafe43deSMatthew G. Knepley   const PetscScalar *coords;
445722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
446cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
4471d0c6c94SMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, cMax, c, i;
448cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
449cafe43deSMatthew G. Knepley 
450cafe43deSMatthew G. Knepley   PetscFunctionBegin;
451cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
452cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
453cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
4545b3353d8SMatthew G. Knepley   if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D");
455cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
456cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
457cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
458cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
459cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
460cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
461cafe43deSMatthew G. Knepley #if 0
462cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
463b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
464b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
465cafe43deSMatthew G. Knepley #endif
466cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
467cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
468cafe43deSMatthew G. Knepley   /* Create label */
469cafe43deSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4701d0c6c94SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
4711d0c6c94SMatthew G. Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
472cafe43deSMatthew G. Knepley   ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr);
473cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
474722d0f5cSMatthew G. Knepley   /* Compute boxes which overlap each cell: http://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
475cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
476cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
47738353de4SMatthew G. Knepley   ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr);
478cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
479cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
480cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
48138353de4SMatthew G. Knepley     PetscInt         csize   = 0;
482cafe43deSMatthew G. Knepley     PetscScalar      point[3];
483cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
484cafe43deSMatthew G. Knepley 
485cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
48638353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
48738353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
488722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
48938353de4SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);}
490cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
491cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
4922291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
493cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
494cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
495cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
496cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
497cafe43deSMatthew G. Knepley       }
498cafe43deSMatthew G. Knepley     }
499fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
500cafe43deSMatthew G. Knepley     for (k = dlim[2*2+0], point[2] = lbox->lower[2] + k*h[2]; k <= dlim[2*2+1]; ++k, point[2] += h[2]) {
501cafe43deSMatthew G. Knepley       for (j = dlim[1*2+0], point[1] = lbox->lower[1] + j*h[1]; j <= dlim[1*2+1]; ++j, point[1] += h[1]) {
502cafe43deSMatthew G. Knepley         for (i = dlim[0*2+0], point[0] = lbox->lower[0] + i*h[0]; i <= dlim[0*2+1]; ++i, point[0] += h[0]) {
503cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
504cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
505fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
506cafe43deSMatthew G. Knepley 
507fea14342SMatthew G. Knepley           /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
508cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
509cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
510cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
511cafe43deSMatthew G. Knepley 
512cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
513cafe43deSMatthew G. Knepley                 if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
514cafe43deSMatthew G. Knepley               }
515cafe43deSMatthew G. Knepley             }
516cafe43deSMatthew G. Knepley           }
517fea14342SMatthew G. Knepley           /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */
518fea14342SMatthew G. Knepley           for (edge = 0; edge < dim+1; ++edge) {
519fea14342SMatthew G. Knepley             PetscReal segA[6], segB[6];
520fea14342SMatthew G. Knepley 
521fea14342SMatthew G. Knepley             for (d = 0; d < dim; ++d) {segA[d] = PetscRealPart(ccoords[edge*dim+d]); segA[dim+d] = PetscRealPart(ccoords[((edge+1)%(dim+1))*dim+d]);}
522fea14342SMatthew G. Knepley             for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) {
5239a128ed2SMatthew G. Knepley               if (dim > 2) {segB[2]     = PetscRealPart(point[2]);
5249a128ed2SMatthew G. Knepley                             segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];}
525fea14342SMatthew G. Knepley               for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) {
5269a128ed2SMatthew G. Knepley                 if (dim > 1) {segB[1]     = PetscRealPart(point[1]);
5279a128ed2SMatthew G. Knepley                               segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];}
528fea14342SMatthew G. Knepley                 for (ii = 0; ii < 2; ++ii) {
529fea14342SMatthew G. Knepley                   PetscBool intersects;
530fea14342SMatthew G. Knepley 
5319a128ed2SMatthew G. Knepley                   segB[0]     = PetscRealPart(point[0]);
5329a128ed2SMatthew G. Knepley                   segB[dim+0] = PetscRealPart(point[0]) + ii*h[0];
533fea14342SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
534fea14342SMatthew G. Knepley                   if (intersects) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;}
535cafe43deSMatthew G. Knepley                 }
536cafe43deSMatthew G. Knepley               }
537cafe43deSMatthew G. Knepley             }
538cafe43deSMatthew G. Knepley           }
539fea14342SMatthew G. Knepley         }
540fea14342SMatthew G. Knepley       }
541fea14342SMatthew G. Knepley     }
542fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
543fea14342SMatthew G. Knepley   }
544722d0f5cSMatthew G. Knepley   ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr);
545cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
546cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
547cafe43deSMatthew G. Knepley   *localBox = lbox;
548cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
549cafe43deSMatthew G. Knepley }
550cafe43deSMatthew G. Knepley 
551cafe43deSMatthew G. Knepley #undef __FUNCT__
552ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex"
55362a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
554ccd2543fSMatthew G Knepley {
555cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
556953fc75cSMatthew G. Knepley   PetscBool       hash = mesh->useHashLocation;
5573a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
5581318edbeSMatthew G. Knepley   PetscInt        dim, cStart, cEnd, cMax, numCells, c;
559cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
5603a93e3b7SToby Isaac   PetscSFNode    *cells;
561ccd2543fSMatthew G Knepley   PetscScalar    *a;
5623a93e3b7SToby Isaac   PetscMPIInt     result;
563ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
564ccd2543fSMatthew G Knepley 
565ccd2543fSMatthew G Knepley   PetscFunctionBegin;
566080342d1SMatthew 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 
591e9b685f5SMatthew G. Knepley     cells[p].rank  = 0;
592e9b685f5SMatthew G. Knepley     cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
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 
626e9b685f5SMatthew G. Knepley       if (cells[p].index < 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?) */
6482d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
6493a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
6503a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
6513a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
6523a93e3b7SToby Isaac         if (numFound < p) {
6533a93e3b7SToby Isaac           cells[numFound] = cells[p];
6543a93e3b7SToby Isaac         }
6553a93e3b7SToby Isaac         found[numFound++] = p;
6563a93e3b7SToby Isaac       }
6573a93e3b7SToby Isaac     }
6583a93e3b7SToby Isaac   }
65962a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
6603a93e3b7SToby Isaac   ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
661ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
662ccd2543fSMatthew G Knepley }
663ccd2543fSMatthew G Knepley 
664ccd2543fSMatthew G Knepley #undef __FUNCT__
665741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D"
666741bfc07SMatthew G. Knepley /*@C
667741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
668741bfc07SMatthew G. Knepley 
669741bfc07SMatthew G. Knepley   Not collective
670741bfc07SMatthew G. Knepley 
671741bfc07SMatthew G. Knepley   Input Parameter:
672741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
673741bfc07SMatthew G. Knepley 
674741bfc07SMatthew G. Knepley   Output Parameters:
675741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x
676741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
677741bfc07SMatthew G. Knepley 
678741bfc07SMatthew G. Knepley   Level: developer
679741bfc07SMatthew G. Knepley 
680741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
681741bfc07SMatthew G. Knepley @*/
682741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
68317fe8556SMatthew G. Knepley {
68417fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
68517fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
6868b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
68717fe8556SMatthew G. Knepley 
68817fe8556SMatthew G. Knepley   PetscFunctionBegin;
6891c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
6901c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
69117fe8556SMatthew G. Knepley   coords[0] = 0.0;
6927f07f362SMatthew G. Knepley   coords[1] = r;
69317fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
69417fe8556SMatthew G. Knepley }
69517fe8556SMatthew G. Knepley 
69617fe8556SMatthew G. Knepley #undef __FUNCT__
697741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto1D"
698741bfc07SMatthew G. Knepley /*@C
699741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
70028dbe442SToby Isaac 
701741bfc07SMatthew G. Knepley   Not collective
70228dbe442SToby Isaac 
703741bfc07SMatthew G. Knepley   Input Parameter:
704741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
705741bfc07SMatthew G. Knepley 
706741bfc07SMatthew G. Knepley   Output Parameters:
707741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x and z
708741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
709741bfc07SMatthew G. Knepley 
710741bfc07SMatthew 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
711741bfc07SMatthew G. Knepley 
712741bfc07SMatthew G. Knepley   Level: developer
713741bfc07SMatthew G. Knepley 
714741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
715741bfc07SMatthew G. Knepley @*/
716741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
71728dbe442SToby Isaac {
71828dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
71928dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
72028dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
72128dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
72228dbe442SToby Isaac   PetscReal      rinv = 1. / r;
72328dbe442SToby Isaac   PetscFunctionBegin;
72428dbe442SToby Isaac 
72528dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
72628dbe442SToby Isaac   if (x > 0.) {
72728dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
72828dbe442SToby Isaac 
72928dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
73028dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
73128dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
73228dbe442SToby Isaac   }
73328dbe442SToby Isaac   else {
73428dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
73528dbe442SToby Isaac 
73628dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
73728dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
73828dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
73928dbe442SToby Isaac   }
74028dbe442SToby Isaac   coords[0] = 0.0;
74128dbe442SToby Isaac   coords[1] = r;
74228dbe442SToby Isaac   PetscFunctionReturn(0);
74328dbe442SToby Isaac }
74428dbe442SToby Isaac 
74528dbe442SToby Isaac #undef __FUNCT__
746741bfc07SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D"
747741bfc07SMatthew G. Knepley /*@
748741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto2D - Rewrite coordinates to be the 2D projection of the 3D coordinates
749741bfc07SMatthew G. Knepley 
750741bfc07SMatthew G. Knepley   Not collective
751741bfc07SMatthew G. Knepley 
752741bfc07SMatthew G. Knepley   Input Parameter:
753741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
754741bfc07SMatthew G. Knepley 
755741bfc07SMatthew G. Knepley   Output Parameters:
756741bfc07SMatthew G. Knepley + coords - The new y- and z-coordinates, and 0 for x
757741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
758741bfc07SMatthew G. Knepley 
759741bfc07SMatthew G. Knepley   Level: developer
760741bfc07SMatthew G. Knepley 
761741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
762741bfc07SMatthew G. Knepley @*/
763741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
764ccd2543fSMatthew G Knepley {
7651ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
76699dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
7674a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
768ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
76999dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
770ccd2543fSMatthew G Knepley 
771ccd2543fSMatthew G Knepley   PetscFunctionBegin;
772ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
773ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
7741ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
7751ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
776ccd2543fSMatthew G Knepley   }
777ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
778ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
779ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
7808b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
781ccd2543fSMatthew G Knepley   n[0] /= norm;
782ccd2543fSMatthew G Knepley   n[1] /= norm;
783ccd2543fSMatthew G Knepley   n[2] /= norm;
784ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
785ccd2543fSMatthew G Knepley 
786ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
787ccd2543fSMatthew G Knepley 
788ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
789ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
790ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
791ccd2543fSMatthew G Knepley 
792ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
793ccd2543fSMatthew G Knepley   */
7948b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
79573868372SMatthew G. Knepley   /* Check for n = z */
79673868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
7977df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
7987df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
79999dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
80099dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
8017df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
80273868372SMatthew G. Knepley     }
80399dec3a6SMatthew G. Knepley     coords[0] = 0.0;
80499dec3a6SMatthew G. Knepley     coords[1] = 0.0;
8057df32b8bSSanderA     coords[2] = x1[0];
8067df32b8bSSanderA     coords[3] = x1[1] * s;
8077df32b8bSSanderA     coords[4] = x2[0];
8087df32b8bSSanderA     coords[5] = x2[1] * s;
8097df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
8107df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
8117df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
81273868372SMatthew G. Knepley     PetscFunctionReturn(0);
81373868372SMatthew G. Knepley   }
814da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
815ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
816ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
817ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
818ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
819ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
820ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
821ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
822ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
823ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
824ccd2543fSMatthew G Knepley     }
825ccd2543fSMatthew G Knepley   }
8268763be8eSMatthew G. Knepley   if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
8278763be8eSMatthew G. Knepley   if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
828ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
82999dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
83099dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
83199dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
83299dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
83399dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
83499dec3a6SMatthew G. Knepley       }
83599dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
83699dec3a6SMatthew G. Knepley     }
83799dec3a6SMatthew G. Knepley   }
838ccd2543fSMatthew G Knepley   coords[0] = 0.0;
839ccd2543fSMatthew G Knepley   coords[1] = 0.0;
840ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
841ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
842ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
843ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
8447f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
8457f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
8467f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
8477f07f362SMatthew G. Knepley       PetscReal tmp;
8487f07f362SMatthew G. Knepley 
8497f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
8507f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
8517f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
8527f07f362SMatthew G. Knepley     }
8537f07f362SMatthew G. Knepley   }
854ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
855ccd2543fSMatthew G Knepley }
856ccd2543fSMatthew G Knepley 
857ccd2543fSMatthew G Knepley #undef __FUNCT__
858834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal"
8596322fe33SJed Brown PETSC_UNUSED
860834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
861834e62ceSMatthew G. Knepley {
862834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
863834e62ceSMatthew G. Knepley 
864834e62ceSMatthew G. Knepley    |  1  1  1 |
865834e62ceSMatthew G. Knepley    | x0 x1 x2 |
866834e62ceSMatthew G. Knepley    | y0 y1 y2 |
867834e62ceSMatthew G. Knepley 
868834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
869834e62ceSMatthew G. Knepley 
870834e62ceSMatthew G. Knepley    | x1 x2 |
871834e62ceSMatthew G. Knepley    | y1 y2 |
872834e62ceSMatthew G. Knepley   */
873834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
874834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
875834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
876834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
87786623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
878923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
879834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
8803bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
881834e62ceSMatthew G. Knepley }
882834e62ceSMatthew G. Knepley 
883834e62ceSMatthew G. Knepley #undef __FUNCT__
884834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal"
885834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
886834e62ceSMatthew G. Knepley {
887923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
888834e62ceSMatthew G. Knepley   *vol *= 0.5;
889834e62ceSMatthew G. Knepley }
890834e62ceSMatthew G. Knepley 
891834e62ceSMatthew G. Knepley #undef __FUNCT__
892834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal"
8936322fe33SJed Brown PETSC_UNUSED
894834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
895834e62ceSMatthew G. Knepley {
896834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
897834e62ceSMatthew G. Knepley 
898834e62ceSMatthew G. Knepley    |  1  1  1  1 |
899834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
900834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
901834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
902834e62ceSMatthew G. Knepley 
903834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
904834e62ceSMatthew G. Knepley 
905834e62ceSMatthew G. Knepley    | x1 x2 x3 |
906834e62ceSMatthew G. Knepley    | y1 y2 y3 |
907834e62ceSMatthew G. Knepley    | z1 z2 z3 |
908834e62ceSMatthew G. Knepley   */
909834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
910834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
911834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
912834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
913834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
914834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
915834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
916923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
917b7ad821dSMatthew G. Knepley   *vol = -0.16666666666666666666666*detM;
9183bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
919834e62ceSMatthew G. Knepley }
920834e62ceSMatthew G. Knepley 
921834e62ceSMatthew G. Knepley #undef __FUNCT__
9220ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal"
9230ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
9240ec8681fSMatthew G. Knepley {
925923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
926b7ad821dSMatthew G. Knepley   *vol *= -0.16666666666666666666666;
9270ec8681fSMatthew G. Knepley }
9280ec8681fSMatthew G. Knepley 
9290ec8681fSMatthew G. Knepley #undef __FUNCT__
930cb92db44SToby Isaac #define __FUNCT__ "DMPlexComputePointGeometry_Internal"
931cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
932cb92db44SToby Isaac {
933cb92db44SToby Isaac   PetscSection   coordSection;
934cb92db44SToby Isaac   Vec            coordinates;
935cb92db44SToby Isaac   const PetscScalar *coords;
936cb92db44SToby Isaac   PetscInt       dim, d, off;
937cb92db44SToby Isaac   PetscErrorCode ierr;
938cb92db44SToby Isaac 
939cb92db44SToby Isaac   PetscFunctionBegin;
940cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
941cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
942cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
943cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
944cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
945cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
946cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
947cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
948cb92db44SToby Isaac   *detJ = 1.;
949cb92db44SToby Isaac   if (J) {
950cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
951cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
952cb92db44SToby Isaac     if (invJ) {
953cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
954cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
955cb92db44SToby Isaac     }
956cb92db44SToby Isaac   }
957cb92db44SToby Isaac   PetscFunctionReturn(0);
958cb92db44SToby Isaac }
959cb92db44SToby Isaac 
960cb92db44SToby Isaac #undef __FUNCT__
96117fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal"
96217fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
96317fe8556SMatthew G. Knepley {
96417fe8556SMatthew G. Knepley   PetscSection   coordSection;
96517fe8556SMatthew G. Knepley   Vec            coordinates;
966a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9678bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
96817fe8556SMatthew G. Knepley   PetscErrorCode ierr;
96917fe8556SMatthew G. Knepley 
97017fe8556SMatthew G. Knepley   PetscFunctionBegin;
97117fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
97269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
9738bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
9748bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
97517fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9768bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
977adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
9787f07f362SMatthew G. Knepley   *detJ = 0.0;
97928dbe442SToby Isaac   if (numCoords == 6) {
98028dbe442SToby Isaac     const PetscInt dim = 3;
98128dbe442SToby Isaac     PetscReal      R[9], J0;
98228dbe442SToby Isaac 
98328dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
984741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
98528dbe442SToby Isaac     if (J)    {
98628dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
98728dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
98828dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
98928dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
99028dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
99128dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
992adac9986SMatthew G. Knepley     }
99328dbe442SToby Isaac   } else if (numCoords == 4) {
9947f07f362SMatthew G. Knepley     const PetscInt dim = 2;
9957f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
9967f07f362SMatthew G. Knepley 
9977f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
998741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
99917fe8556SMatthew G. Knepley     if (J)    {
10007f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
10017f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
10027f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
1003923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1004923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1005adac9986SMatthew G. Knepley     }
10067f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
10077f07f362SMatthew G. Knepley     const PetscInt dim = 1;
10087f07f362SMatthew G. Knepley 
10097f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
10107f07f362SMatthew G. Knepley     if (J)    {
10117f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
101217fe8556SMatthew G. Knepley       *detJ = J[0];
10133bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
10143bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
1015adac9986SMatthew G. Knepley     }
1016796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
101717fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
101817fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
101917fe8556SMatthew G. Knepley }
102017fe8556SMatthew G. Knepley 
102117fe8556SMatthew G. Knepley #undef __FUNCT__
1022ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal"
1023ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1024ccd2543fSMatthew G Knepley {
1025ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1026ccd2543fSMatthew G Knepley   Vec            coordinates;
1027a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10287f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
1029ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1030ccd2543fSMatthew G Knepley 
1031ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1032ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
103369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1034ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
10357f07f362SMatthew G. Knepley   *detJ = 0.0;
1036ccd2543fSMatthew G Knepley   if (numCoords == 9) {
10377f07f362SMatthew G. Knepley     const PetscInt dim = 3;
10387f07f362SMatthew 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};
10397f07f362SMatthew G. Knepley 
10407f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1041741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
10427f07f362SMatthew G. Knepley     if (J)    {
1043b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1044b7ad821dSMatthew G. Knepley 
1045b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1046b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1047b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1048ccd2543fSMatthew G Knepley         }
10497f07f362SMatthew G. Knepley       }
10503bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1051923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
10527f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
10537f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
10547f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
10557f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
10567f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
10577f07f362SMatthew G. Knepley           }
10587f07f362SMatthew G. Knepley         }
10597f07f362SMatthew G. Knepley       }
10603bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
10617f07f362SMatthew G. Knepley     }
1062923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
10637f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
10647f07f362SMatthew G. Knepley     const PetscInt dim = 2;
10657f07f362SMatthew G. Knepley 
10667f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1067ccd2543fSMatthew G Knepley     if (J)    {
1068ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1069ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1070ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1071ccd2543fSMatthew G Knepley         }
1072ccd2543fSMatthew G Knepley       }
10733bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1074923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1075ccd2543fSMatthew G Knepley     }
1076923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1077796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1078ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1079ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1080ccd2543fSMatthew G Knepley }
1081ccd2543fSMatthew G Knepley 
1082ccd2543fSMatthew G Knepley #undef __FUNCT__
1083ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal"
1084ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1085ccd2543fSMatthew G Knepley {
1086ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1087ccd2543fSMatthew G Knepley   Vec            coordinates;
1088a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10890d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1090ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1091ccd2543fSMatthew G Knepley 
1092ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1093ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
109469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10950d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10960d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
109799dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
109871f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
10997f07f362SMatthew G. Knepley   *detJ = 0.0;
110099dec3a6SMatthew G. Knepley   if (numCoords == 12) {
110199dec3a6SMatthew G. Knepley     const PetscInt dim = 3;
110299dec3a6SMatthew 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};
110399dec3a6SMatthew G. Knepley 
110499dec3a6SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1105741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
110699dec3a6SMatthew G. Knepley     if (J)    {
110799dec3a6SMatthew G. Knepley       const PetscInt pdim = 2;
110899dec3a6SMatthew G. Knepley 
110999dec3a6SMatthew G. Knepley       for (d = 0; d < pdim; d++) {
111099dec3a6SMatthew G. Knepley         J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
111199dec3a6SMatthew G. Knepley         J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
111299dec3a6SMatthew G. Knepley       }
11133bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1114923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
111599dec3a6SMatthew G. Knepley       for (d = 0; d < dim; d++) {
111699dec3a6SMatthew G. Knepley         for (f = 0; f < dim; f++) {
111799dec3a6SMatthew G. Knepley           J[d*dim+f] = 0.0;
111899dec3a6SMatthew G. Knepley           for (g = 0; g < dim; g++) {
111999dec3a6SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
112099dec3a6SMatthew G. Knepley           }
112199dec3a6SMatthew G. Knepley         }
112299dec3a6SMatthew G. Knepley       }
11233bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
112499dec3a6SMatthew G. Knepley     }
1125923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
112671f58de1SToby Isaac   } else if (numCoords == 8) {
112799dec3a6SMatthew G. Knepley     const PetscInt dim = 2;
112899dec3a6SMatthew G. Knepley 
11297f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1130ccd2543fSMatthew G Knepley     if (J)    {
1131ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
113299dec3a6SMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
113399dec3a6SMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1134ccd2543fSMatthew G Knepley       }
11353bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1136923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1137ccd2543fSMatthew G Knepley     }
1138923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1139796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
114099dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1141ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1142ccd2543fSMatthew G Knepley }
1143ccd2543fSMatthew G Knepley 
1144ccd2543fSMatthew G Knepley #undef __FUNCT__
1145ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal"
1146ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1147ccd2543fSMatthew G Knepley {
1148ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1149ccd2543fSMatthew G Knepley   Vec            coordinates;
1150a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1151ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
115299dec3a6SMatthew G. Knepley   PetscInt       d;
1153ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1154ccd2543fSMatthew G Knepley 
1155ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1156ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
115769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1158ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11597f07f362SMatthew G. Knepley   *detJ = 0.0;
11607f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1161ccd2543fSMatthew G Knepley   if (J)    {
1162ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1163f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1164f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1165f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1166f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1167ccd2543fSMatthew G Knepley     }
11683bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1169923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1170ccd2543fSMatthew G Knepley   }
1171923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1172ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1173ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1174ccd2543fSMatthew G Knepley }
1175ccd2543fSMatthew G Knepley 
1176ccd2543fSMatthew G Knepley #undef __FUNCT__
1177ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal"
1178ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1179ccd2543fSMatthew G Knepley {
1180ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1181ccd2543fSMatthew G Knepley   Vec            coordinates;
1182a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1183ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1184ccd2543fSMatthew G Knepley   PetscInt       d;
1185ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1186ccd2543fSMatthew G Knepley 
1187ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1188ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
118969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1190ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11917f07f362SMatthew G. Knepley   *detJ = 0.0;
11927f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1193ccd2543fSMatthew G Knepley   if (J)    {
1194ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1195f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1196f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1197f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1198ccd2543fSMatthew G Knepley     }
11993bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1200923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1201ccd2543fSMatthew G Knepley   }
1202923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1203ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1204ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1205ccd2543fSMatthew G Knepley }
1206ccd2543fSMatthew G Knepley 
1207ccd2543fSMatthew G Knepley #undef __FUNCT__
12088e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM"
1209ccd2543fSMatthew G Knepley /*@C
12108e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1211ccd2543fSMatthew G Knepley 
1212ccd2543fSMatthew G Knepley   Collective on DM
1213ccd2543fSMatthew G Knepley 
1214ccd2543fSMatthew G Knepley   Input Arguments:
1215ccd2543fSMatthew G Knepley + dm   - the DM
1216ccd2543fSMatthew G Knepley - cell - the cell
1217ccd2543fSMatthew G Knepley 
1218ccd2543fSMatthew G Knepley   Output Arguments:
1219ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1220ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1221ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1222ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1223ccd2543fSMatthew G Knepley 
1224ccd2543fSMatthew G Knepley   Level: advanced
1225ccd2543fSMatthew G Knepley 
1226ccd2543fSMatthew G Knepley   Fortran Notes:
1227ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1228ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1229ccd2543fSMatthew G Knepley 
12308e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
1231ccd2543fSMatthew G Knepley @*/
12328e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1233ccd2543fSMatthew G Knepley {
123449dc4407SMatthew G. Knepley   PetscInt       depth, dim, coneSize;
1235cb92db44SToby Isaac   DMLabel        depthLabel;
1236ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1237ccd2543fSMatthew G Knepley 
1238ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1239139a35ccSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1240ccd2543fSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1241cb92db44SToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1242cb92db44SToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1243cb92db44SToby Isaac   if (depth == 1 && dim == 1) {
12448e0841e0SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12458e0841e0SMatthew G. Knepley   }
1246ccd2543fSMatthew G Knepley   switch (dim) {
1247cb92db44SToby Isaac   case 0:
1248cb92db44SToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1249cb92db44SToby Isaac     break;
125017fe8556SMatthew G. Knepley   case 1:
125117fe8556SMatthew G. Knepley     ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
125217fe8556SMatthew G. Knepley     break;
1253ccd2543fSMatthew G Knepley   case 2:
1254ccd2543fSMatthew G Knepley     switch (coneSize) {
1255ccd2543fSMatthew G Knepley     case 3:
1256ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1257ccd2543fSMatthew G Knepley       break;
1258ccd2543fSMatthew G Knepley     case 4:
1259ccd2543fSMatthew G Knepley       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1260ccd2543fSMatthew G Knepley       break;
1261ccd2543fSMatthew G Knepley     default:
12628e0841e0SMatthew G. Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1263ccd2543fSMatthew G Knepley     }
1264ccd2543fSMatthew G Knepley     break;
1265ccd2543fSMatthew G Knepley   case 3:
1266ccd2543fSMatthew G Knepley     switch (coneSize) {
1267ccd2543fSMatthew G Knepley     case 4:
1268ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1269ccd2543fSMatthew G Knepley       break;
12708e0841e0SMatthew G. Knepley     case 6: /* Faces */
12718e0841e0SMatthew G. Knepley     case 8: /* Vertices */
1272ccd2543fSMatthew G Knepley       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
1273ccd2543fSMatthew G Knepley       break;
1274ccd2543fSMatthew G Knepley     default:
12758e0841e0SMatthew G. Knepley         SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1276ccd2543fSMatthew G Knepley     }
1277ccd2543fSMatthew G Knepley       break;
1278ccd2543fSMatthew G Knepley   default:
1279ccd2543fSMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1280ccd2543fSMatthew G Knepley   }
12818e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
12828e0841e0SMatthew G. Knepley }
12838e0841e0SMatthew G. Knepley 
12848e0841e0SMatthew G. Knepley #undef __FUNCT__
12858e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal"
12868e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
12878e0841e0SMatthew G. Knepley {
12888e0841e0SMatthew G. Knepley   PetscQuadrature  quad;
12898e0841e0SMatthew G. Knepley   PetscSection     coordSection;
12908e0841e0SMatthew G. Knepley   Vec              coordinates;
12918e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
12928e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
1293*f960e424SToby Isaac   PetscReal       *basisDer, *basis, detJt;
1294*f960e424SToby Isaac   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, q;
12958e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
12968e0841e0SMatthew G. Knepley 
12978e0841e0SMatthew G. Knepley   PetscFunctionBegin;
12988e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
12998e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
13008e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
13018e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
13028e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
13038e0841e0SMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1304954b1791SMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
13058e0841e0SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1306*f960e424SToby Isaac   ierr = PetscFEGetDefaultTabulation(fe, &basis, &basisDer, NULL);CHKERRQ(ierr);
13078e0841e0SMatthew G. Knepley   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
13088e0841e0SMatthew 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);
1309*f960e424SToby Isaac   if (v0) {
1310*f960e424SToby Isaac     ierr = PetscMemzero(v0, Nq*cdim*sizeof(PetscReal));CHKERRQ(ierr);
1311*f960e424SToby Isaac     for (q = 0; q < Nq; ++q) {
1312*f960e424SToby Isaac       PetscInt i, k;
1313*f960e424SToby Isaac 
1314*f960e424SToby Isaac       for (k = 0; k < pdim; ++k)
1315*f960e424SToby Isaac         for (i = 0; i < cdim; ++i)
1316*f960e424SToby Isaac           v0[q*cdim + i] += basis[q*pdim + k] * PetscRealPart(coords[k*cdim + i]);
1317*f960e424SToby Isaac       ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1318*f960e424SToby Isaac     }
1319*f960e424SToby Isaac   }
1320*f960e424SToby Isaac   if (J) {ierr = PetscMemzero(J, Nq*cdim*dim*sizeof(PetscReal));CHKERRQ(ierr);}
1321*f960e424SToby Isaac   if (J || invJ || detJ) {
13228e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
13238e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
13248e0841e0SMatthew G. Knepley 
13258e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
13268e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
13278e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
13288e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
132971d6e60fSMatthew G. Knepley             J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
13303bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
13318e0841e0SMatthew G. Knepley       if (cdim > dim) {
13328e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
13338e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
13348e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
13358e0841e0SMatthew G. Knepley       }
1336*f960e424SToby Isaac       if (!detJ && !invJ) continue;
13378e0841e0SMatthew G. Knepley       switch (cdim) {
13388e0841e0SMatthew G. Knepley       case 3:
1339*f960e424SToby Isaac         DMPlex_Det3D_Internal(&detJt, J);
1340*f960e424SToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(invJ, J, detJt);}
134117fe8556SMatthew G. Knepley         break;
134249dc4407SMatthew G. Knepley       case 2:
13438e0841e0SMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1344*f960e424SToby Isaac         if (invJ) {DMPlex_Invert2D_Internal(invJ, J, detJt);}
134549dc4407SMatthew G. Knepley         break;
13468e0841e0SMatthew G. Knepley       case 1:
1347*f960e424SToby Isaac         detJt = J[0];
13488e0841e0SMatthew G. Knepley         if (invJ) invJ[0] = 1.0/J[0];
134949dc4407SMatthew G. Knepley       }
1350*f960e424SToby Isaac       if (detJ) detJ[q] = detJt;
135149dc4407SMatthew G. Knepley     }
13528e0841e0SMatthew G. Knepley   }
13538e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
13548e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
13558e0841e0SMatthew G. Knepley }
13568e0841e0SMatthew G. Knepley 
13578e0841e0SMatthew G. Knepley #undef __FUNCT__
13588e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
13598e0841e0SMatthew G. Knepley /*@C
13608e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
13618e0841e0SMatthew G. Knepley 
13628e0841e0SMatthew G. Knepley   Collective on DM
13638e0841e0SMatthew G. Knepley 
13648e0841e0SMatthew G. Knepley   Input Arguments:
13658e0841e0SMatthew G. Knepley + dm   - the DM
13668e0841e0SMatthew G. Knepley . cell - the cell
13678e0841e0SMatthew G. Knepley - fe   - the finite element containing the quadrature
13688e0841e0SMatthew G. Knepley 
13698e0841e0SMatthew G. Knepley   Output Arguments:
1370*f960e424SToby Isaac + v0   - if fe != NULL, the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
13718e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
13728e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
13738e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
13748e0841e0SMatthew G. Knepley 
13758e0841e0SMatthew G. Knepley   Level: advanced
13768e0841e0SMatthew G. Knepley 
13778e0841e0SMatthew G. Knepley   Fortran Notes:
13788e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
13798e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
13808e0841e0SMatthew G. Knepley 
13818e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
13828e0841e0SMatthew G. Knepley @*/
13838e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
13848e0841e0SMatthew G. Knepley {
13858e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
13868e0841e0SMatthew G. Knepley 
13878e0841e0SMatthew G. Knepley   PetscFunctionBegin;
13888e0841e0SMatthew G. Knepley   if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
13898e0841e0SMatthew G. Knepley   else     {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
1390ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1391ccd2543fSMatthew G Knepley }
1392834e62ceSMatthew G. Knepley 
1393834e62ceSMatthew G. Knepley #undef __FUNCT__
1394cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
1395011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1396cc08537eSMatthew G. Knepley {
1397cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1398cc08537eSMatthew G. Knepley   Vec            coordinates;
1399a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
140006e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1401cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1402cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1403cc08537eSMatthew G. Knepley 
1404cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1405cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
140669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1407cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1408011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
14092e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1410cc08537eSMatthew G. Knepley   if (centroid) {
141106e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
141206e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1413cc08537eSMatthew G. Knepley   }
1414cc08537eSMatthew G. Knepley   if (normal) {
1415a60a936bSMatthew G. Knepley     PetscReal norm;
1416a60a936bSMatthew G. Knepley 
141706e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
141806e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1419a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1420a60a936bSMatthew G. Knepley     normal[0] /= norm;
1421a60a936bSMatthew G. Knepley     normal[1] /= norm;
1422cc08537eSMatthew G. Knepley   }
1423cc08537eSMatthew G. Knepley   if (vol) {
142406e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1425cc08537eSMatthew G. Knepley   }
1426cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1427cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1428cc08537eSMatthew G. Knepley }
1429cc08537eSMatthew G. Knepley 
1430cc08537eSMatthew G. Knepley #undef __FUNCT__
1431cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1432cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1433011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1434cc08537eSMatthew G. Knepley {
1435cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1436cc08537eSMatthew G. Knepley   Vec            coordinates;
1437cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
14380a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
14390a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1440cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1441cc08537eSMatthew G. Knepley 
1442cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1443cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
14440a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
144569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1446cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
14470bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1448ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1449ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1450ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1451ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1452ceee4971SMatthew G. Knepley   }
1453011ea5d8SMatthew G. Knepley   if (normal) {
1454011ea5d8SMatthew G. Knepley     if (dim > 2) {
14551ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
14561ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
14571ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
14580a1d6728SMatthew G. Knepley       PetscReal       norm;
14590a1d6728SMatthew G. Knepley 
14600a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
14610a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
14620a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
14638b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
14640a1d6728SMatthew G. Knepley       normal[0] /= norm;
14650a1d6728SMatthew G. Knepley       normal[1] /= norm;
14660a1d6728SMatthew G. Knepley       normal[2] /= norm;
1467011ea5d8SMatthew G. Knepley     } else {
1468011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1469011ea5d8SMatthew G. Knepley     }
1470011ea5d8SMatthew G. Knepley   }
1471741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
14720a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
14730a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
14740a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14751ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
14761ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
14770a1d6728SMatthew G. Knepley     }
14780a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
14790a1d6728SMatthew G. Knepley     vsum += vtmp;
14800a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
14810a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
14820a1d6728SMatthew G. Knepley     }
14830a1d6728SMatthew G. Knepley   }
14840a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
14850a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
14860a1d6728SMatthew G. Knepley   }
14870a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1488ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
14890a1d6728SMatthew G. Knepley   if (centroid) {
14900a1d6728SMatthew G. Knepley     if (dim > 2) {
14910a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
14920a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
14930a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
14940a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
14950a1d6728SMatthew G. Knepley         }
14960a1d6728SMatthew G. Knepley       }
14970a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
14980a1d6728SMatthew G. Knepley   }
1499cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1500cc08537eSMatthew G. Knepley }
1501cc08537eSMatthew G. Knepley 
1502cc08537eSMatthew G. Knepley #undef __FUNCT__
15030ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
15040ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1505011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
15060ec8681fSMatthew G. Knepley {
15070ec8681fSMatthew G. Knepley   PetscSection    coordSection;
15080ec8681fSMatthew G. Knepley   Vec             coordinates;
15090ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
151086623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1511a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
15120ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
15130ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
15140ec8681fSMatthew G. Knepley 
15150ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1516f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
15170ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
151869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
15190ec8681fSMatthew G. Knepley 
1520d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
15210ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
15220ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1523a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
15240ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1525011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
15260ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
15270ec8681fSMatthew G. Knepley     switch (numCorners) {
15280ec8681fSMatthew G. Knepley     case 3:
15290ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15301ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
15311ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
15321ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
15330ec8681fSMatthew G. Knepley       }
15340ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1535a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15360ec8681fSMatthew G. Knepley       vsum += vtmp;
15374f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
15380ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15391ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15400ec8681fSMatthew G. Knepley         }
15410ec8681fSMatthew G. Knepley       }
15420ec8681fSMatthew G. Knepley       break;
15430ec8681fSMatthew G. Knepley     case 4:
15440ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
15450ec8681fSMatthew G. Knepley       /* First tet */
15460ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15471ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
15481ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
15491ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
15500ec8681fSMatthew G. Knepley       }
15510ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1552a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15530ec8681fSMatthew G. Knepley       vsum += vtmp;
15540ec8681fSMatthew G. Knepley       if (centroid) {
15550ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15560ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15570ec8681fSMatthew G. Knepley         }
15580ec8681fSMatthew G. Knepley       }
15590ec8681fSMatthew G. Knepley       /* Second tet */
15600ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
15611ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
15621ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
15631ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
15640ec8681fSMatthew G. Knepley       }
15650ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1566a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
15670ec8681fSMatthew G. Knepley       vsum += vtmp;
15680ec8681fSMatthew G. Knepley       if (centroid) {
15690ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
15700ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
15710ec8681fSMatthew G. Knepley         }
15720ec8681fSMatthew G. Knepley       }
15730ec8681fSMatthew G. Knepley       break;
15740ec8681fSMatthew G. Knepley     default:
1575796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
15760ec8681fSMatthew G. Knepley     }
15774f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
15780ec8681fSMatthew G. Knepley   }
15798763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
15800ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1581d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
15820ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
15830ec8681fSMatthew G. Knepley }
15840ec8681fSMatthew G. Knepley 
15850ec8681fSMatthew G. Knepley #undef __FUNCT__
1586834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1587834e62ceSMatthew G. Knepley /*@C
1588834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1589834e62ceSMatthew G. Knepley 
1590834e62ceSMatthew G. Knepley   Collective on DM
1591834e62ceSMatthew G. Knepley 
1592834e62ceSMatthew G. Knepley   Input Arguments:
1593834e62ceSMatthew G. Knepley + dm   - the DM
1594834e62ceSMatthew G. Knepley - cell - the cell
1595834e62ceSMatthew G. Knepley 
1596834e62ceSMatthew G. Knepley   Output Arguments:
1597834e62ceSMatthew G. Knepley + volume   - the cell volume
1598cc08537eSMatthew G. Knepley . centroid - the cell centroid
1599cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1600834e62ceSMatthew G. Knepley 
1601834e62ceSMatthew G. Knepley   Level: advanced
1602834e62ceSMatthew G. Knepley 
1603834e62ceSMatthew G. Knepley   Fortran Notes:
1604834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1605834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1606834e62ceSMatthew G. Knepley 
160769d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1608834e62ceSMatthew G. Knepley @*/
1609cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1610834e62ceSMatthew G. Knepley {
16110ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1612834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1613834e62ceSMatthew G. Knepley 
1614834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1615834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1616c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1617834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1618834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1619c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1620834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1621011ea5d8SMatthew G. Knepley   switch (depth) {
1622cc08537eSMatthew G. Knepley   case 1:
1623011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1624cc08537eSMatthew G. Knepley     break;
1625834e62ceSMatthew G. Knepley   case 2:
1626011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1627834e62ceSMatthew G. Knepley     break;
1628834e62ceSMatthew G. Knepley   case 3:
1629011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1630834e62ceSMatthew G. Knepley     break;
1631834e62ceSMatthew G. Knepley   default:
1632834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1633834e62ceSMatthew G. Knepley   }
1634834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1635834e62ceSMatthew G. Knepley }
1636113c68e6SMatthew G. Knepley 
1637113c68e6SMatthew G. Knepley #undef __FUNCT__
1638c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1639c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1640c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1641c0d900a5SMatthew G. Knepley {
1642c0d900a5SMatthew G. Knepley   DM             dmCell;
1643c0d900a5SMatthew G. Knepley   Vec            coordinates;
1644c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1645c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1646c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1647c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1648c0d900a5SMatthew G. Knepley 
1649c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1650c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1651c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1652c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1653c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1654c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1655c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1656c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1657c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1658c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1659c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1660c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
16619e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1662c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1663c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1664c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1665c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1666c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1667c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1668c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1669c0d900a5SMatthew G. Knepley 
1670c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1671c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1672c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1673c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1674c0d900a5SMatthew G. Knepley   }
1675c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1676c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1677c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1678c0d900a5SMatthew G. Knepley }
1679c0d900a5SMatthew G. Knepley 
1680c0d900a5SMatthew G. Knepley #undef __FUNCT__
1681113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1682891a9168SMatthew G. Knepley /*@
1683891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1684891a9168SMatthew G. Knepley 
1685891a9168SMatthew G. Knepley   Input Parameter:
1686891a9168SMatthew G. Knepley . dm - The DM
1687891a9168SMatthew G. Knepley 
1688891a9168SMatthew G. Knepley   Output Parameters:
1689891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1690891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1691891a9168SMatthew G. Knepley 
1692891a9168SMatthew G. Knepley   Level: developer
1693891a9168SMatthew G. Knepley 
1694891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1695891a9168SMatthew G. Knepley @*/
1696113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1697113c68e6SMatthew G. Knepley {
1698113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1699113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1700113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1701113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1702113c68e6SMatthew G. Knepley   Vec            coordinates;
1703113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1704113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1705113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1706113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1707113c68e6SMatthew G. Knepley 
1708113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1709113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1710113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1711113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1712113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1713113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1714113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1715113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1716113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1717113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1718113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1719113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
17209e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1721113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1722113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1723113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1724113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
172506348e87SToby Isaac   if (cEndInterior < 0) {
172606348e87SToby Isaac     cEndInterior = cEnd;
172706348e87SToby Isaac   }
1728113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1729113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1730113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1731113c68e6SMatthew G. Knepley 
1732113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1733113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1734113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1735113c68e6SMatthew G. Knepley   }
1736113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1737113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1738113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1739113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1740113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
17419e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1742113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1743113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1744113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1745113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1746113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1747c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1748113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1749113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1750113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1751113c68e6SMatthew G. Knepley     PetscReal        area;
175250d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
1753113c68e6SMatthew G. Knepley 
17549ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
175550d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
175650d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
1757113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1758113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1759113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1760113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1761113c68e6SMatthew G. Knepley     {
1762113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
176306348e87SToby Isaac       PetscInt         ncells;
1764113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1765113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
17660453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1767113c68e6SMatthew G. Knepley 
1768113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
176906348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
1770113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1771113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
177206348e87SToby Isaac       if (ncells > 1) {
177306348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1774113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
177506348e87SToby Isaac       }
177606348e87SToby Isaac       else {
177706348e87SToby Isaac         rcentroid = fg->centroid;
177806348e87SToby Isaac       }
17792e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
17802e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
17810453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1782113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1783113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1784113c68e6SMatthew G. Knepley       }
1785113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1786113c68e6SMatthew 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]);
1787113c68e6SMatthew 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]);
1788113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1789113c68e6SMatthew G. Knepley       }
1790113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1791113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1792113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1793113c68e6SMatthew G. Knepley       }
179406348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
1795113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
1796113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1797113c68e6SMatthew G. Knepley       }
1798113c68e6SMatthew G. Knepley     }
1799113c68e6SMatthew G. Knepley   }
1800b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1801113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
1802113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
1803113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
1804113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1805113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
1806113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
1807113c68e6SMatthew G. Knepley 
1808113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
1809113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
1810113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
1811113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
181250d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
1813113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
1814113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
1815113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
1816113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
1817113c68e6SMatthew G. Knepley       if (support[s] == c) {
1818640bce14SSatish Balay         PetscFVCellGeom       *ci;
1819113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
1820113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
1821113c68e6SMatthew G. Knepley 
1822113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
1823113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
1824113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
1825113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
1826113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
1827113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
1828113c68e6SMatthew G. Knepley       }
1829113c68e6SMatthew G. Knepley     }
1830113c68e6SMatthew G. Knepley   }
1831113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
1832113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1833113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1834113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
1835113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1836113c68e6SMatthew G. Knepley }
1837113c68e6SMatthew G. Knepley 
1838113c68e6SMatthew G. Knepley #undef __FUNCT__
1839113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
1840113c68e6SMatthew G. Knepley /*@C
1841113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
1842113c68e6SMatthew G. Knepley 
1843113c68e6SMatthew G. Knepley   Not collective
1844113c68e6SMatthew G. Knepley 
1845113c68e6SMatthew G. Knepley   Input Argument:
1846113c68e6SMatthew G. Knepley . dm - the DM
1847113c68e6SMatthew G. Knepley 
1848113c68e6SMatthew G. Knepley   Output Argument:
1849113c68e6SMatthew G. Knepley . minradius - the minium cell radius
1850113c68e6SMatthew G. Knepley 
1851113c68e6SMatthew G. Knepley   Level: developer
1852113c68e6SMatthew G. Knepley 
1853113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
1854113c68e6SMatthew G. Knepley @*/
1855113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
1856113c68e6SMatthew G. Knepley {
1857113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1858113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1859113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
1860113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
1861113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1862113c68e6SMatthew G. Knepley }
1863113c68e6SMatthew G. Knepley 
1864113c68e6SMatthew G. Knepley #undef __FUNCT__
1865113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
1866113c68e6SMatthew G. Knepley /*@C
1867113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
1868113c68e6SMatthew G. Knepley 
1869113c68e6SMatthew G. Knepley   Logically collective
1870113c68e6SMatthew G. Knepley 
1871113c68e6SMatthew G. Knepley   Input Arguments:
1872113c68e6SMatthew G. Knepley + dm - the DM
1873113c68e6SMatthew G. Knepley - minradius - the minium cell radius
1874113c68e6SMatthew G. Knepley 
1875113c68e6SMatthew G. Knepley   Level: developer
1876113c68e6SMatthew G. Knepley 
1877113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
1878113c68e6SMatthew G. Knepley @*/
1879113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
1880113c68e6SMatthew G. Knepley {
1881113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1882113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1883113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
1884113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1885113c68e6SMatthew G. Knepley }
1886856ac710SMatthew G. Knepley 
1887856ac710SMatthew G. Knepley #undef __FUNCT__
1888856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
1889856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1890856ac710SMatthew G. Knepley {
1891856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
1892856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
1893856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
1894856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1895856ac710SMatthew G. Knepley 
1896856ac710SMatthew G. Knepley   PetscFunctionBegin;
1897856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1898856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1899856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1900856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
1901856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1902c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1903856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1904856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
1905856ac710SMatthew G. Knepley     const PetscInt        *faces;
1906856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
1907640bce14SSatish Balay     PetscFVCellGeom        *cg;
1908856ac710SMatthew G. Knepley     PetscBool              boundary;
1909856ac710SMatthew G. Knepley     PetscInt               ghost;
1910856ac710SMatthew G. Knepley 
1911856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1912856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
1913856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
1914856ac710SMatthew 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);
1915856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1916640bce14SSatish Balay       PetscFVCellGeom       *cg1;
1917856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
1918856ac710SMatthew G. Knepley       const PetscInt        *fcells;
1919856ac710SMatthew G. Knepley       PetscInt               ncell, side;
1920856ac710SMatthew G. Knepley 
1921856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1922a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1923856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1924856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
1925856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
1926856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
1927856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
1928856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1929856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
1930856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
1931856ac710SMatthew G. Knepley     }
1932856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
1933856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
1934856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1935856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1936a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1937856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1938856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
1939856ac710SMatthew G. Knepley       ++usedFaces;
1940856ac710SMatthew G. Knepley     }
1941856ac710SMatthew G. Knepley   }
1942856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
1943856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1944856ac710SMatthew G. Knepley }
1945856ac710SMatthew G. Knepley 
1946856ac710SMatthew G. Knepley #undef __FUNCT__
1947b81db932SToby Isaac #define __FUNCT__ "BuildGradientReconstruction_Internal_Tree"
1948b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1949b81db932SToby Isaac {
1950b81db932SToby Isaac   DMLabel        ghostLabel;
1951b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
1952b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
1953b81db932SToby Isaac   PetscSection   neighSec;
1954b81db932SToby Isaac   PetscInt     (*neighbors)[2];
1955b81db932SToby Isaac   PetscInt      *counter;
1956b81db932SToby Isaac   PetscErrorCode ierr;
1957b81db932SToby Isaac 
1958b81db932SToby Isaac   PetscFunctionBegin;
1959b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1960b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1961b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
19625bc680faSToby Isaac   if (cEndInterior < 0) {
19635bc680faSToby Isaac     cEndInterior = cEnd;
19645bc680faSToby Isaac   }
1965b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
1966b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
1967b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1968c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1969b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1970b81db932SToby Isaac     const PetscInt        *fcells;
1971b81db932SToby Isaac     PetscBool              boundary;
19725bc680faSToby Isaac     PetscInt               ghost = -1;
1973b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
1974b81db932SToby Isaac 
197506348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1976a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
1977b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
1978b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
1979b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
198006348e87SToby Isaac     if (numCells == 2) {
1981b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
1982b81db932SToby Isaac       for (c = 0; c < 2; c++) {
1983b81db932SToby Isaac         PetscInt cell = fcells[c];
1984b81db932SToby Isaac 
1985e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
1986b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
1987b81db932SToby Isaac         }
1988b81db932SToby Isaac       }
1989b81db932SToby Isaac     }
199006348e87SToby Isaac   }
1991b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
1992b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
1993b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1994b81db932SToby Isaac   nStart = 0;
1995b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
1996b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
1997b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
1998b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
1999b81db932SToby Isaac     const PetscInt        *fcells;
2000b81db932SToby Isaac     PetscBool              boundary;
20015bc680faSToby Isaac     PetscInt               ghost = -1;
2002b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2003b81db932SToby Isaac 
200406348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2005a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2006b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2007b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2008b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
200906348e87SToby Isaac     if (numCells == 2) {
2010b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2011b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2012b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2013b81db932SToby Isaac 
2014e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2015b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2016b81db932SToby Isaac           off += counter[cell - cStart]++;
2017b81db932SToby Isaac           neighbors[off][0] = f;
2018b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2019b81db932SToby Isaac         }
2020b81db932SToby Isaac       }
2021b81db932SToby Isaac     }
202206348e87SToby Isaac   }
2023b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2024b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2025b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2026317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2027640bce14SSatish Balay     PetscFVCellGeom        *cg;
2028b81db932SToby Isaac 
2029b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2030b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2031b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2032317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2033317218b9SToby 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);
2034b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2035640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2036b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2037b81db932SToby Isaac       const PetscInt        *fcells;
2038b81db932SToby Isaac       PetscInt               ncell, side, nface;
2039b81db932SToby Isaac 
2040b81db932SToby Isaac       nface = neighbors[off + f][0];
2041b81db932SToby Isaac       ncell = neighbors[off + f][1];
2042b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2043b81db932SToby Isaac       side  = (c != fcells[0]);
2044b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2045b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2046b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2047b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2048b81db932SToby Isaac     }
2049b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2050b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2051b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2052b81db932SToby Isaac     }
2053b81db932SToby Isaac   }
2054b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
20555fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2056b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2057b81db932SToby Isaac   PetscFunctionReturn(0);
2058b81db932SToby Isaac }
2059b81db932SToby Isaac 
2060b81db932SToby Isaac #undef __FUNCT__
2061856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
2062856ac710SMatthew G. Knepley /*@
2063856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2064856ac710SMatthew G. Knepley 
2065856ac710SMatthew G. Knepley   Collective on DM
2066856ac710SMatthew G. Knepley 
2067856ac710SMatthew G. Knepley   Input Arguments:
2068856ac710SMatthew G. Knepley + dm  - The DM
2069856ac710SMatthew G. Knepley . fvm - The PetscFV
20708f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
20718f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2072856ac710SMatthew G. Knepley 
2073856ac710SMatthew G. Knepley   Output Parameters:
2074856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2075856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2076856ac710SMatthew G. Knepley 
2077856ac710SMatthew G. Knepley   Level: developer
2078856ac710SMatthew G. Knepley 
2079856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2080856ac710SMatthew G. Knepley @*/
2081856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2082856ac710SMatthew G. Knepley {
2083856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2084856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2085b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2086856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2087856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2088856ac710SMatthew G. Knepley 
2089856ac710SMatthew G. Knepley   PetscFunctionBegin;
2090856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2091856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2092856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2093856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2094856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2095856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2096856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2097856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2098856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2099b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2100b81db932SToby Isaac   if (!parentSection) {
2101856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2102b5a3613cSMatthew G. Knepley   } else {
2103b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2104b81db932SToby Isaac   }
2105856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2106856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2107856ac710SMatthew G. Knepley   /* Create storage for gradients */
2108856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2109856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2110856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2111856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2112856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2113856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2114856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2115856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2116856ac710SMatthew G. Knepley }
2117b27d5b9eSToby Isaac 
2118b27d5b9eSToby Isaac #undef __FUNCT__
2119b27d5b9eSToby Isaac #define __FUNCT__ "DMPlexGetDataFVM"
2120b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2121b27d5b9eSToby Isaac {
2122b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2123b27d5b9eSToby Isaac   PetscErrorCode ierr;
2124b27d5b9eSToby Isaac 
2125b27d5b9eSToby Isaac   PetscFunctionBegin;
2126b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2127b27d5b9eSToby Isaac   if (!cellgeomobj) {
2128b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2129b27d5b9eSToby Isaac 
2130b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2131b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2132b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2133b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2134b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2135b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2136b27d5b9eSToby Isaac   }
2137b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2138b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2139b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2140b27d5b9eSToby Isaac   if (gradDM) {
2141b27d5b9eSToby Isaac     PetscObject gradobj;
2142b27d5b9eSToby Isaac     PetscBool   computeGradients;
2143b27d5b9eSToby Isaac 
2144b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2145b27d5b9eSToby Isaac     if (!computeGradients) {
2146b27d5b9eSToby Isaac       *gradDM = NULL;
2147b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2148b27d5b9eSToby Isaac     }
2149b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2150b27d5b9eSToby Isaac     if (!gradobj) {
2151b27d5b9eSToby Isaac       DM dmGradInt;
2152b27d5b9eSToby Isaac 
2153b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2154b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2155b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2156b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2157b27d5b9eSToby Isaac     }
2158b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2159b27d5b9eSToby Isaac   }
2160b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2161b27d5b9eSToby Isaac }
2162